Skip to content

Commit 40bd6cc

Browse files
Seongjae ChoiSeongjae Choi
authored andcommitted
Add test for sending Pay transaction
1 parent cad3910 commit 40bd6cc

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package transaction
2+
3+
import (
4+
"bytes"
5+
"encoding/hex"
6+
"testing"
7+
8+
rpc "github.com/CodeChain-io/codechain-rpc-go"
9+
"github.com/CodeChain-io/codechain-rpc-go/primitives"
10+
)
11+
12+
func TestSigning(t *testing.T) {
13+
id, _ := hex.DecodeString("0000000000000000000000000000000000000000")
14+
accountID := primitives.NewH160(id)
15+
16+
p, _ := primitives.PlatformAddressFromAccountID(accountID, "tc")
17+
q := primitives.NewU64("11")
18+
19+
pay := NewPay(p, q, "tc")
20+
21+
secret, _ := primitives.StringToH256("ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd")
22+
fee := primitives.NewU64("0")
23+
24+
signed := pay.Sign(secret, uint(0), fee)
25+
26+
if bytes.Compare(
27+
signed.RlpBytes(),
28+
[]byte{248, 96, 128, 128, 130, 116, 99, 215, 2, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 184, 65, 63, 155, 207, 244, 132, 189, 95, 29, 85, 73, 249, 18, 249, 238, 175, 140, 47, 227, 73, 178, 87, 189, 226, 182, 31, 177, 3, 96, 19, 212, 228, 76, 32, 74, 66, 21, 210, 108, 184, 121, 234, 173, 32, 40, 254, 26, 120, 152, 228, 207, 154, 93, 151, 158, 179, 131, 224, 163, 132, 20, 13, 110, 4, 193, 1}) != 0 {
29+
t.Fatal("Signing Pay transaction Error")
30+
}
31+
32+
}
33+
34+
func TestSendSignedTransaction(t *testing.T) {
35+
rpcClient := rpc.NewRPC("https://corgi-rpc.codechain.io/")
36+
37+
recipient, err1 := primitives.PlatformAddressFromString("wccq95dss644nd3xgddje38k2kdldzsy6ns5gmscxu2")
38+
if err1 != nil {
39+
t.Fatal(err1)
40+
}
41+
42+
quantity := primitives.NewU64("12345")
43+
44+
pay := NewPay(recipient, quantity, "wc")
45+
46+
secret, err2 := primitives.StringToH256("6692200de85d2ae6fbb817c656cc9f5a0b8ee1ed82795289aaf4400f1d817d62")
47+
if err2 != nil {
48+
t.Fatal(err2)
49+
}
50+
51+
fee := primitives.NewU64("100")
52+
accountID, err3 := primitives.PlatformAddressFromString("wccq9dddym9sc6rn3jgsnmp8qel57s5mwjq6v5ye68e")
53+
54+
if err3 != nil {
55+
t.Fatal(err3)
56+
}
57+
58+
seq, err4 := rpcClient.Chain.GetLatestSeq(accountID.Value)
59+
60+
if err4 != nil {
61+
t.Fatal(err4)
62+
}
63+
64+
signed := pay.Sign(secret, seq, fee)
65+
66+
_, err5 := rpcClient.Mempool.SendSignedTransaction(hex.EncodeToString(signed.RlpBytes()))
67+
68+
if err5 != nil {
69+
t.Fatal(err5)
70+
}
71+
72+
}

primitives/platformAddress.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ func PlatformAddressFromPublic(publicKey H512, networkID string) (PlatformAddres
3030
return PlatformAddressFromAccountID(accountID, networkID)
3131
}
3232

33+
func PlatformAddressFromString(address string) (p PlatformAddress, err error) {
34+
words := bech32Decode(address, address[0:3])
35+
bytes := fromWords(words)
36+
version := bytes[0]
37+
38+
if version != 1 {
39+
err = errors.New("Unsupported version for PlatformAddress")
40+
}
41+
42+
accountID := NewH160(bytes[1:])
43+
44+
return PlatformAddress{accountID, address}, nil
45+
}
46+
3347
func GetAddcountIDFromPublic(publicKey string) H160 {
3448
return blake160(publicKey)
3549
}

0 commit comments

Comments
 (0)