forked from FactomProject/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_test.go
255 lines (217 loc) · 7.05 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom_test
import (
. "github.com/FactomProject/factom"
"testing"
"os"
"bytes"
"encoding/json"
"fmt"
"github.com/FactomProject/factom/wallet"
"github.com/FactomProject/factom/wallet/wsapi"
"io/ioutil"
"net/http"
)
func TestImportAddress(t *testing.T) {
var (
fs1 = "Fs2TCa7Mo4XGy9FQSoZS8JPnDfv7SjwUSGqrjMWvc1RJ9sKbJeXA"
fa1 = "FA3T1gTkuKGG2MWpAkskSoTnfjxZDKVaAYwziNTC1pAYH5B9A1rh"
es1 = "Es4KmwK65t9HCsibYzVDFrijvkgTFZKdEaEAgfMtYTPSVtM3NDSx"
ec1 = "EC2CyGKaNddLFxrjkFgiaRZnk77b8iQia3Zj6h5fxFReAcDwCo3i"
bads = []string{
"Fs2TCa7Mo4XGy9FQSoZS8JPnDfv7SjwUSGqrjMWvc1RJ9sKbJeX", //short
"Fs2TCa7Mo4XGy9FQSoZS8JPnDfv7SjwUSGqrjMWvc1RJ9sKbJeeA", //check
"", //empty
"Fc2TCa7Mo4XGy9FQSoZS8JPnDfv7SjwUSGqrjMWvc1RJ9sKbJeXA", //prefix
}
)
// start the test wallet
done, err := StartTestWallet()
if err != nil {
t.Error(err)
}
defer func() { done <- 1 }()
// import the good addresses
if _, _, err := ImportAddresses(fs1, es1); err != nil {
t.Error(err)
}
if f, err := FetchFactoidAddress(fa1); err != nil {
t.Error(err)
} else if f == nil {
t.Error("Wallet returned nil factoid address")
} else if f.SecString() != fs1 {
t.Error("Wallet returned incorrect address", fs1, f.SecString())
}
if e, err := FetchECAddress(ec1); err != nil {
t.Error(err)
} else if e == nil {
t.Error("Wallet returned nil ec address")
} else if e.SecString() != es1 {
t.Error("Wallet returned incorrect address", es1, e.SecString())
}
// try to import the bad addresses
for _, bad := range bads {
if _, _, err := ImportAddresses(bad); err == nil {
t.Error("Bad address was imported without error", bad)
}
}
}
func TestHandleWalletBalances(t *testing.T) {
// start the test wallet
done, err := StartTestWallet()
if err != nil {
t.Error(err)
}
defer func() { done <- 1 }()
// Testing when all accounts dont have balances #2
noBalFCT := "Fs1itDLe8GoFCLsdbqb2rs6U67wQX4TikkTJV69BxGuG1tDvs41q"
noBalEC := "Es3W3R2u85aN2MNr2EoyMazAy7yGTZZg8eDaW7vfjorkrWAANv6t"
addr2 := []string{noBalFCT, noBalEC}
testingVar2, _ := helper(t, addr2)
if testingVar2.Result.FactoidAccountBalances.TempBal != 0 && testingVar2.Result.FactoidAccountBalances.PermBal != 0 && testingVar2.Result.EntryCreditAccountBalances.TempBal != 0 && testingVar2.Result.EntryCreditAccountBalances.PermBal != 0 {
t.Error("balances are not what they should be")
}
fmt.Println("Passed balance of 0 #2")
// Testing when all accounts have balances #3
hasBalFCT := "Fs1vEcszU16mC72CBMAfAnxVvKQKTtrTqiCfdGF8hycMn1j1DBKy"
hasBalEC := "Es2nSXmiaUuk9AxX2X43Ws4XjXPCxehTyHZAEn5NJH9ei1gLW1FR"
addr3 := []string{hasBalFCT, hasBalEC}
testingVar3, _ := helper(t, addr3)
if testingVar3.Result.EntryCreditAccountBalances.TempBal != 40 && testingVar3.Result.EntryCreditAccountBalances.PermBal != 40 && testingVar3.Result.FactoidAccountBalances.TempBal != 0 && testingVar3.Result.FactoidAccountBalances.PermBal != 0 {
t.Error("balances are not what they should be")
}
fmt.Println("Passed when some have values #3")
}
type walletcallHelper struct {
FactoidAccountBalances *wsapi.StructToReturnValues `json:"fctaccountbalances"`
EntryCreditAccountBalances *wsapi.StructToReturnValues `json:"ecaccountbalances"`
}
type walletcall struct {
Jsonrpc string `json:"jsonrps"`
Id int `json:"id"`
Result walletcallHelper `json:"result"`
}
func helper(t *testing.T, addr []string) (*walletcall, string) {
for _, k := range addr {
if _, _, err := ImportAddresses(k); err != nil {
return nil, "failed"
}
}
url := "http://localhost:8089/v2"
jsonStrEC := []byte(`{"jsonrpc": "2.0", "id": 0, "method": "wallet-balances"}`)
reqEC, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStrEC))
reqEC.Header.Set("content-type", "text/plain;")
clientEC := &http.Client{}
callRespEC, err := clientEC.Do(reqEC)
if err != nil {
t.Error(err)
}
defer callRespEC.Body.Close()
bodyEC, _ := ioutil.ReadAll(callRespEC.Body)
fmt.Println("BODY: ", string(bodyEC))
respEC := new(walletcall)
errEC := json.Unmarshal([]byte(bodyEC), &respEC)
if errEC != nil {
t.Error(errEC)
}
return respEC, ""
}
func TestImportKoinify(t *testing.T) {
var (
good_mnemonic = "yellow yellow yellow yellow yellow yellow yellow" +
" yellow yellow yellow yellow yellow" // good
koinifyexpect = "FA3cih2o2tjEUsnnFR4jX1tQXPpSXFwsp3rhVp6odL5PNCHWvZV1"
bad_mnemonic = []string{
"", // bad empty
"yellow yellow yellow yellow yellow yellow yellow yellow yellow" +
" yellow yellow", // bad short
"yellow yellow yellow yellow yellow yellow yellow yellow yellow" +
" yellow yellow asdfasdf", // bad word
}
)
// start the test wallet
done, err := StartTestWallet()
if err != nil {
t.Error(err)
}
defer func() { done <- 1 }()
// check the import for koinify names
fa, err := ImportKoinify(good_mnemonic)
if err != nil {
t.Error(err)
}
if fa.String() != koinifyexpect {
t.Error("Incorrect address from Koinify mnemonic", fa, koinifyexpect)
}
for _, m := range bad_mnemonic {
if _, err := ImportKoinify(m); err == nil {
t.Error("No error for bad address:", m)
}
}
}
// helper functions for testing
func populateTestWallet() error {
//FA3T1gTkuKGG2MWpAkskSoTnfjxZDKVaAYwziNTC1pAYH5B9A1rh
//Fs2TCa7Mo4XGy9FQSoZS8JPnDfv7SjwUSGqrjMWvc1RJ9sKbJeXA
//
//FA3oaS2D2GkrZJuWuiDohnLruxV3AWbrM3PmG3HSSE7DHzPWio36
//Fs1os7xg2mN9fTuJmaYZLk6EXz51x2wmmHr2365UAuPMJW3aNr25
//
//EC2CyGKaNddLFxrjkFgiaRZnk77b8iQia3Zj6h5fxFReAcDwCo3i
//Es4KmwK65t9HCsibYzVDFrijvkgTFZKdEaEAgfMtYTPSVtM3NDSx
//
//EC2R4bPDj9WQ8eWA4X3K8NYfTkBh4HFvCopLBq48FyrNXNumSK6w
//Es355qB6tWo1ZZRTK8cXpHjxGECXaPGw98AFCRJ6kxZ3J6vp1M2i
_, _, err := ImportAddresses(
"Fs2TCa7Mo4XGy9FQSoZS8JPnDfv7SjwUSGqrjMWvc1RJ9sKbJeXA",
"Fs1os7xg2mN9fTuJmaYZLk6EXz51x2wmmHr2365UAuPMJW3aNr25",
"Es4KmwK65t9HCsibYzVDFrijvkgTFZKdEaEAgfMtYTPSVtM3NDSx",
"Es355qB6tWo1ZZRTK8cXpHjxGECXaPGw98AFCRJ6kxZ3J6vp1M2i",
)
if err != nil {
return err
}
return nil
}
// StartTestWallet runs a test wallet and serves the wallet api. The caller
// must write an int to the chan when compleate to stop the wallet api and
// remove the test db.
func StartTestWallet() (chan int, error) {
var (
walletdbfile = os.TempDir() + "/testingwallet.bolt"
txdbfile = os.TempDir() + "/testingtxdb.bolt"
)
// make a chan to signal when the test is finished with the wallet
done := make(chan int, 1)
// setup a testing wallet
fctWallet, err := wallet.NewOrOpenBoltDBWallet(walletdbfile)
if err != nil {
return nil, err
}
defer os.Remove(walletdbfile)
txdb, err := wallet.NewTXBoltDB(txdbfile)
if err != nil {
return nil, err
} else {
fctWallet.AddTXDB(txdb)
}
defer os.Remove(txdbfile)
RpcConfig = &RPCConfig{
WalletTLSEnable: false,
WalletTLSKeyFile: "",
WalletTLSCertFile: "",
WalletRPCUser: "",
WalletRPCPassword: "",
WalletServer: "localhost:8089",
}
go wsapi.Start(fctWallet, ":8089", *RpcConfig)
go func() {
<-done
wsapi.Stop()
fctWallet.Close()
txdb.Close()
}()
return done, nil
}