-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreqobt_test.go
323 lines (302 loc) · 8.9 KB
/
reqobt_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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package fio
import (
"bytes"
"encoding/hex"
"fmt"
"math/rand"
"testing"
"time"
)
func TestOBT(t *testing.T) {
alice, api, _, err := newApi()
if err != nil {
t.Error(err)
return
}
bob, err := NewAccountFromWif(`5KQ6f9ZgUtagD3LZ4wcMKhhvK9qy4BuwL3L1pkm6E2v62HCne2R`)
if err != nil {
t.Error(err)
return
}
apiB, _, err := NewConnection(bob.KeyBag, api.BaseURL)
if err != nil {
t.Error(err)
return
}
aliceAddresses, err := api.GetFioAddresses(alice.PubKey, 0, 100)
if err != nil {
t.Error(err)
}
bobAddresses, err := api.GetFioAddresses(bob.PubKey, 0, 100)
if err != nil {
t.Error(err)
}
// already have it, but be thorough and walk through entire process
bobPub, found, err := api.PubAddressLookup(Address(bobAddresses.FioAddresses[0].FioAddress), "FIO", "FIO")
if err != nil || !found {
t.Error("can't get pubaddress, giving up ", err)
return
}
// encrypt 3 requests
requests := make([]string, 3)
for i := 1; i <= 3; i++ {
requests[i-1], err = ObtRequestContent{
PayeePublicAddress: alice.PubKey,
Amount: fmt.Sprintf("%d", i),
ChainCode: "FIO",
TokenCode: "FIO",
Memo: fmt.Sprintf("request %d", i),
}.Encrypt(alice, bobPub.PublicAddress)
if err != nil {
t.Error(err)
}
}
// alice sends them to bob
for _, r := range requests {
_, err := api.SignPushActions(
NewFundsReq(alice.Actor, bobAddresses.FioAddresses[0].FioAddress, aliceAddresses.FioAddresses[0].FioAddress, r),
)
if err != nil {
t.Error(err)
return
}
time.Sleep(500 * time.Millisecond)
}
// check if we have sent requests, and cancel the last one
sent, ok, err := api.GetSentFioRequests(alice.PubKey, 100, 0)
if err != nil {
t.Error(err)
return
}
if !ok {
t.Error("no sent requests")
return
}
cnlReq := NewCancelFndReq(alice.Actor, sent.Requests[len(sent.Requests)-1].FioRequestId)
_, err = api.SignPushActions(
cnlReq,
)
if err != nil {
t.Error(err)
}
time.Sleep(250 * time.Millisecond)
// ensure it's on the list of cancelled requests
cancelled, err := api.GetCancelledRequests(alice.PubKey, 100, 0)
if err != nil {
t.Error(err)
} else if cancelled.Requests == nil || len(cancelled.Requests) == 0 {
t.Error("did not have any cancelled requests")
} else {
if cancelled.Requests[len(cancelled.Requests)-1].FioRequestId != sent.Requests[len(sent.Requests)-1].FioRequestId {
t.Error("did not find cancelled request")
}
}
// now bob's turn, get pending requests
pending, ok, err := apiB.GetPendingFioRequests(bob.PubKey, 100, 0)
if err != nil {
t.Error(err)
return
}
if !ok {
t.Error("no pending requests")
return
}
// find the last one from alice, ensure it's request 2, then reject
for i := len(pending.Requests) - 1; i >= 0; i-- {
if pending.Requests[i].PayeeFioPublicKey == alice.PubKey {
fndReq, err := DecryptContent(bob, alice.PubKey, pending.Requests[i].Content, ObtRequestType)
if err != nil {
t.Error(err)
break
}
if fndReq.Request.Amount != "2" || fndReq.Request.Memo != "request 2" {
t.Error("fund request did not have expected content")
if j, err := fndReq.ToJson(); err == nil {
fmt.Println(string(j))
}
break
}
_, err = apiB.SignPushActions(
NewRejectFndReq(bob.Actor, fmt.Sprintf("%d", pending.Requests[i].FioRequestId)),
)
if err != nil {
t.Error(err)
break
}
break
}
}
// ensure we have one less pending request
afterRej, ok, err := apiB.GetPendingFioRequests(bob.PubKey, 100, 0)
if err != nil {
t.Error(err)
return
}
if !ok {
t.Error("no pending requests")
return
}
if len(pending.Requests)-1 != len(afterRej.Requests) {
t.Error("rejecting fund request did not remove from pending list")
}
// finally record a response to the remaining request
for i := len(afterRej.Requests) - 1; i >= 0; i-- {
if pending.Requests[i].PayeeFioPublicKey == alice.PubKey {
fndReq, err := DecryptContent(bob, alice.PubKey, pending.Requests[i].Content, ObtRequestType)
if err != nil {
t.Error(err)
break
}
if fndReq.Request.Amount != "1" || fndReq.Request.Memo != "request 1" {
t.Error("fund request did not have expected content")
if j, err := fndReq.ToJson(); err == nil {
fmt.Println(string(j))
}
break
}
content, err := ObtRecordContent{
PayerPublicAddress: bob.PubKey,
PayeePublicAddress: alice.PubKey,
Amount: "1",
ChainCode: "FIO",
TokenCode: "FIO",
ObtId: "here is your money",
}.Encrypt(bob, alice.PubKey)
if err != nil {
t.Error(err)
break
}
_, err = apiB.SignPushActions(
NewRecordSend(
bob.Actor,
fmt.Sprintf("%d", afterRej.Requests[i].FioRequestId),
bobAddresses.FioAddresses[0].FioAddress,
aliceAddresses.FioAddresses[0].FioAddress,
content,
),
)
if err != nil {
t.Error(err)
break
}
break
}
}
}
func TestEciesSecret(t *testing.T) {
// test is based on the example in the fiojs package, and ensures we get the same secret ...
// https://github.com/fioprotocol/fiojs/blob/master/docs/message_encryption.md
bob, _ := NewAccountFromWif(`5JoQtsKQuH8hC9MyvfJAqo6qmKLm8ePYNucs7tPu2YxG12trzBt`)
alice, _ := NewAccountFromWif(`5J9bWm2ThenDm3tjvmUgHtWCVMUdjRR1pxnRtnJjvKA4b2ut5WK`)
_, a, e := EciesSecret(bob, alice.PubKey)
if e != nil {
t.Error(e.Error())
}
_, b, e := EciesSecret(alice, bob.PubKey)
if e != nil {
t.Error(e.Error())
}
if !bytes.Equal(a[:], b[:]) {
t.Error("dh-ecdsa secret did not match")
return
}
// the example only gives the first 50 bytes, but that's good enough.
known, _ := hex.DecodeString(`a71b4ec5a9577926a1d2aa1d9d99327fd3b68f6a1ea597200a0d890bd3331df300a2d49fec0b2b3e6969ce9263c5d6cf47c1`)
if !bytes.Equal(known, a[:len(known)]) {
t.Error("secret did not decode to known good value")
}
}
func TestEciesSecret2(t *testing.T) {
const expectCipherText = "f300888ca4f512cebdc0020ff0f7224c0db2984c4ad9afb12629f01a8c6a76328bbde17405655dc4e3cb30dad272996fb1dea8e662e640be193e25d41147a904c571b664a7381ab41ef062448ac1e205"
// hard coding values from typescript unit tests to ensure same result ...
// Check shared-secret derivation
aWif := "5J9bWm2ThenDm3tjvmUgHtWCVMUdjRR1pxnRtnJjvKA4b2ut5WK"
bWif := "5JoQtsKQuH8hC9MyvfJAqo6qmKLm8ePYNucs7tPu2YxG12trzBt"
alice, _ := NewAccountFromWif(aWif)
bob, _ := NewAccountFromWif(bWif)
expectSecret := []byte{167, 27, 78, 197, 169, 87, 121, 38, 161, 210, 170, 29, 157, 153, 50, 127, 211, 182, 143, 106, 30, 165, 151, 32, 10, 13, 137, 11, 211, 51, 29, 243, 0, 162, 212, 159, 236, 11, 43, 62, 105, 105, 206, 146, 99, 197, 214, 207, 71, 193, 145, 193, 239, 20, 147, 115, 236, 201, 240, 217, 129, 22, 181, 152}
_, secretHash, err := EciesSecret(alice, bob.PubKey)
if err != nil {
t.Error(err)
}
if !bytes.Equal(expectSecret, secretHash[:]) {
t.Error("derived secret does not match expected (hard-coded) value")
}
_, secretHash2, err := EciesSecret(bob, alice.PubKey)
if err != nil {
t.Error(err)
}
if !bytes.Equal(secretHash[:], secretHash2[:]) {
t.Error("bob and alice didn't agree on a shared secret")
}
}
func TestEncryptDecrypt(t *testing.T) {
rand.Seed(time.Now().UnixNano())
// run through it several times with random data, keys, and length to ensure padding, etc works.
for i := 0; i < 40; i++ {
size := rand.Intn(128) + 128
someData := make([]byte, size)
_, e := rand.Read(someData)
if e != nil {
t.Error(e.Error())
return
}
sender, e := NewRandomAccount()
if e != nil {
t.Error(e.Error())
return
}
recipient, e := NewRandomAccount()
if e != nil {
t.Error(e.Error())
return
}
// test the encrypt/decrypt on raw bytes first
cipherText, e := EciesEncrypt(sender, recipient.PubKey, someData, nil)
if e != nil {
t.Error(e.Error())
return
}
decrypted, e := EciesDecrypt(recipient, sender.PubKey, cipherText)
if e != nil {
t.Error(e.Error())
return
}
if hex.EncodeToString(someData) != hex.EncodeToString(decrypted) {
fmt.Println(hex.EncodeToString(someData))
fmt.Println(hex.EncodeToString(decrypted))
t.Error("decrypted content from EciesEncrypt did not match EciesDecrypt output")
return
}
// now do it again vs an ObtContent struct.
req := ObtRecordContent{
PayerPublicAddress: "aaaaaaaaaa",
PayeePublicAddress: "bbbbbbbbbb",
Amount: "1111111111",
TokenCode: "zzzzzzzzzz",
Status: "xxxxxxxxxx",
ObtId: "2222222222",
Memo: "ffffffffff",
}
content, e := req.Encrypt(sender, recipient.PubKey)
if e != nil {
t.Error(e.Error())
return
} else if len(content) == 0 {
t.Error("got empty result for encrypted data")
return
}
resp, e := DecryptContent(recipient, sender.PubKey, content, ObtResponseType)
if e != nil {
t.Error(e.Error())
return
} else if resp == nil {
t.Error("resp is nil")
return
}
if req.PayerPublicAddress != resp.Record.PayerPublicAddress {
t.Error("decrypted content does not match")
return
}
}
}