-
Notifications
You must be signed in to change notification settings - Fork 51
/
wallet_test.go
171 lines (159 loc) · 5.2 KB
/
wallet_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package wallet
import (
"context"
"crypto/ed25519"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"testing"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/liteapi"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
"github.com/tonkeeper/tongo/tontest"
)
func TestGetCodeByVer(t *testing.T) {
for ver := V1R1; ver < HighLoadV2; ver++ {
_ = GetCodeByVer(ver)
}
}
func TestVersionToString(t *testing.T) {
testData := map[Version]string{
V1R1: "v1R1",
V3R1: "v3R1",
V3R2: "v3R2",
V4R1: "v4R1",
V4R2: "v4R2",
HighLoadV2: "highload_v2",
}
for ver, name := range testData {
if ver.ToString() != name {
t.Fatal("invalid mapping version to string")
}
}
}
func TestGenerateWalletAddress(t *testing.T) {
type walletData struct {
Address string
PublicKey string
}
testData := map[Version]walletData{
// TODO: add other versions
V3R2: {"0:f3a069b7fc4631da4401de03eddd7cd30caca618c6ad0e3ac3fa454370b73a96",
"f96db56e72de2e84e0aef780428e439a6c84e0b27bc2b2591075785479f2e9c3"},
V4R1: {"0:17afeaaa61cb575e3e340a296da6bf55bc6b996cfab1d9f87840b2b6dc4cf613",
"6f58b9fecb87e847825a7ecf3ae1f32b5578eee156ac10b398e2f1d67c12ca05"},
V4R2: {"0:8f2983152d1480ba6af25e087d672232080b294dc8992525e35e4ff6d601f405",
"7843fd9de6cd858154d9a914b8c3cd0bf1dc5af3a0c1dd273586568fc4d1c002"},
}
for ver, data := range testData {
key, _ := hex.DecodeString(data.PublicKey)
publicKey := ed25519.PublicKey(key)
address, err := GenerateWalletAddress(publicKey, ver, nil, 0, nil)
if err != nil {
t.Fatalf("address generation failed: %v", err)
}
if address.ToRaw() != data.Address {
t.Fatal("addresses mismatch")
}
}
}
func TestLongCommentSerialization(t *testing.T) {
// TODO: add real serialized data
longComment := `
The Quick Brown Fox Jumps Over The Lazy Dog
The Quick Brown Fox Jumps Over The Lazy Dog
The Quick Brown Fox Jumps Over The Lazy Dog
The Quick Brown Fox Jumps Over The Lazy Dog
The Quick Brown Fox Jumps Over The Lazy Dog
The Quick Brown Fox Jumps Over The Lazy Dog`
cell := boc.NewCell()
err := tlb.Marshal(cell, TextComment(longComment))
if err != nil {
t.Fatalf("long comment serialization error: %v", err)
}
var text TextComment
err = tlb.Unmarshal(cell, &text)
if err != nil {
t.Fatalf("long comment deserialization error: %v", err)
}
if string(text) != longComment {
t.Fatal("TextComment invalid serialization/deserialization")
}
}
func TestSimpleSend(t *testing.T) {
t.Skip()
recipientAddr, _ := ton.AccountIDFromRaw("0:507dea7d606f22d9e85678d3eede39bbe133a868d2a0e3e07f5502cb70b8a512")
client, err := liteapi.NewClientWithDefaultTestnet()
if err != nil {
log.Fatalf("Unable to create tongo client: %v", err)
}
w := initDefaultWallet(client)
tonTransfer := SimpleTransfer{
Amount: 10000,
Address: recipientAddr,
Comment: "hello",
}
err = w.Send(context.Background(), tonTransfer)
if err != nil {
t.Fatalf("Unable to generate transfer message: %v", err)
}
}
func TestGetSeqno(t *testing.T) {
client, err := liteapi.NewClientWithDefaultTestnet()
if err != nil {
log.Fatalf("Unable to create tongo client: %v", err)
}
w := initDefaultWallet(client)
seqno, err := client.GetSeqno(context.Background(), w.GetAddress())
if err != nil {
t.Fatalf("Unable to get wallet seqno: %v", err)
}
fmt.Printf("Seqno: %v\n", seqno)
}
func TestMockBlockchain(t *testing.T) {
recipientAddr, _ := ton.AccountIDFromRaw("0:507dea7d606f22d9e85678d3eede39bbe133a868d2a0e3e07f5502cb70b8a512")
client, c := NewMockBlockchain(1, tontest.Account().Balance(10000).Address(ton.AccountID{}).MustShardAccount())
w := initDefaultWallet(client)
tonTransfer := SimpleTransfer{
Amount: 10000,
Address: recipientAddr,
Comment: "hello",
// Body: *boc.Cell, // empty
// Init: *tongo.StateInit, // empty
// Bounce: *bool, // default
// Mode: *byte, // default
}
err := w.Send(context.Background(), tonTransfer)
if err != nil {
t.Fatalf("Unable to send message: %v", err)
}
res := <-c
fmt.Printf("Transfer message: %x\n", res)
b, _ := w.GetBalance(context.Background())
fmt.Printf("Wallet balance: %v\n", b)
}
func initDefaultWallet(blockchain blockchain) Wallet {
pk, _ := base64.StdEncoding.DecodeString("OyAWIb4FeP1bY1VhALWrU2JN9/8O1Kv8kWZ0WfXXpOM=")
privateKey := ed25519.NewKeyFromSeed(pk)
w, err := New(privateKey, V4R2, blockchain)
if err != nil {
panic("unable to create wallet")
}
fmt.Printf("Wallet address: %v\n", w.GetAddress())
return w
}
func TestDeserializeMessage(t *testing.T) {
raw := "te6ccgECBgEAAXgAAZyTKd6JrxqOdZVwzAQwORTSmzEWraYc1v/B1trCjeQrl2o3reaq5d5fnMSdsStxpuQiWtclR30WuOqT4B5llBUBKamjF2NhSmsAAAAhAAMBAWpiAGyOLO7S4cYY8vrxnyGjmQy4fbFHsBTVru7/HlYASPPFKAlQL5AAAAAAAAAAAAAAAAAAAQIDtUY3KJoNoPRt29Q+0/k+b+3eMh/+pICK4Bdz8Cp7zpaXiLqHOGifBStWBqk5GdQn02DkhGQ+wOAtjn9w4gNcdEaIjZ0HAAAnE2NhShBjYUqmCXRlc3RlZWRlMcADBAUAWgFodHRwczovL25mdC5zdGVsLmNvbS91c2VybmFtZS90ZXN0ZWVkZTEuanNvbgBrgBCxVofvaKaMhDVkiORUzmkG5xwA0tZBmR3uDe2eRLI7agJUC+QAoCVAvkAAoAABwgAAACWQAEsABQBkgBCxVofvaKaMhDVkiORUzmkG5xwA0tZBmR3uDe2eRLI7cA=="
cells, err := boc.DeserializeBocBase64(raw)
if err != nil {
panic(err)
}
var msg MessageV4
cells[0].Skip(512)
err = tlb.Unmarshal(cells[0], &msg)
if err != nil {
panic(err)
}
}