-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtransaction-tests.js
381 lines (366 loc) · 19 KB
/
transaction-tests.js
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
const { expect } = require('chai').use(require('chai-bytes'));
const { Transaction, ErgoBox } = require('ergo-lib-wasm-nodejs');
const { toNetwork, getApplication, removeMasterNode, ellipsize } = require('./helpers/common');
const { TEST_DATA } = require('./helpers/data');
const { authTokenFlows } = require('./helpers/flow');
const { TxBuilder } = require('./helpers/transaction');
const txId = "0000000000000000000000000000000000000000000000000000000000000000";
function signTxFlows({ device }, auth, from, to, change, is_blind, tokens_to = undefined, tokens_tx = undefined, script_hash = undefined) {
let i = 0;
const flows = [];
// attest input screen
flows[i] = [{ header: null, body: 'Confirm Attest Input' }];
if (auth) {
flows[i].push({ header: 'Application', body: getApplication(device) });
}
flows[i++].push({ header: null, body: 'Approve' }, { header: null, body: 'Reject' });
// accept tx screen
if (to || change) {
flows[i] = [{ header: 'Review transaction', body: removeMasterNode(from.path.toString()) }];
}
if (is_blind) {
flows[i].push({ header: 'Blind', body: 'signing' });
}
// output screen
if (to && !script_hash) {
flows[i].push(...[
{ header: 'To', body: to.toBase58() },
{ header: 'Amount', body: '0.100000000 ERG' }]);
if (tokens_to) { flows[i].push(...tokens_to); }
}
// script hash
if(script_hash) {
flows[i].push(...[
{ header: 'Script Hash', body: script_hash },
{ header: 'Amount', body: '0.100000000 ERG' }]);
if (tokens_to) { flows[i].push(...tokens_to); }
}
// change screen
if (change && (from.acc_index != change.acc_index || change.addr_index >= 19)) {
flows[i].push({ header: 'Change', body: removeMasterNode(change.path.toString()) });
}
if (to && change) {
flows[i].push({ header: 'Fee', body: '0.001000000 ERG' });
if (tokens_tx) { flows[i].push(...tokens_tx); }
flows[i++].push({ header: null, body: 'Sign transaction' }, { header: null, body: 'Reject' });
}
return flows;
}
function verifySignatures(unsigned, signatures, ergoBox) {
const signed = Transaction.from_unsigned_tx(unsigned, signatures);
const verificationResult = signed.verify_p2pk_input(ergoBox);
expect(verificationResult).to.be.true;
}
describe("Transaction Tests", function () {
context("Transaction Commands", function () {
authTokenFlows("can attest input")
.init(async ({test, auth}) => {
const unsignedBox = new TxBuilder()
.input(TEST_DATA.address0, txId, 0, '1000000000')
.inputs[0].box;
const flow = [{ header: null, body: 'Confirm Attest Input' }];
if (auth) {
flow.push({ header: 'Application', body: getApplication(test.device) });
}
flow.push({ header: null, body: 'Approve' }, { header: null, body: 'Reject' });
return { unsignedBox, flow, flowsCount: 1 };
})
.shouldSucceed(({flow, flows, unsignedBox}, attestedBox) => {
expect(flows[0]).to.be.deep.equal(flow);
expect(attestedBox).to.have.property('box');
expect(attestedBox.box).to.be.deep.equal(unsignedBox);
expect(attestedBox).to.have.property('frames');
expect(attestedBox.frames).to.have.length(1);
const frame = attestedBox.frames[0];
expect(frame.boxId).to.exist;
expect(frame.count).to.be.equal(1);
expect(frame.index).to.be.equal(0);
expect(frame.amount).to.be.equal('1000000000');
expect(frame.tokens).to.be.empty;
expect(frame.attestation).to.exist;
expect(frame.bytes).to.exist;
})
.run(({test, unsignedBox}) => test.device.attestInput(unsignedBox));
authTokenFlows("can sign tx")
.init(async ({test, auth}) => {
const from = TEST_DATA.address0;
const to = TEST_DATA.address1;
const change = TEST_DATA.changeAddress;
const {appTx, ergoTx, uInputs} = new TxBuilder()
.input(from, txId, 0, '1000000000')
.dataInput(from.address, txId, 1)
.output(to.address, '100000000')
.fee('1000000')
.change(change)
.build();
const expectedFlows = signTxFlows(test, auth, from, to, change, false);
return { appTx, ergoTx, input: uInputs[0],
expectedFlows, flowsCount: expectedFlows.length,
network: TEST_DATA.network };
})
.shouldSucceed(({ergoTx, input, expectedFlows, flows}, signatures) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(signatures).to.have.length(1);
verifySignatures(ergoTx, signatures, input);
})
.run(({test, appTx, network}) => test.device.signTx(appTx, toNetwork(network)));
authTokenFlows("can blind sign tx")
.init(async ({test, auth}) => {
const from = TEST_DATA.address0;
const to = TEST_DATA.addressScript;
const to_hash = TEST_DATA.addressScriptHash;
const change = TEST_DATA.changeAddress;
const {appTx, ergoTx, uInputs} = new TxBuilder()
.input(from, txId, 0, '1000000000')
.dataInput(from.address, txId, 1)
.output(to.address, '100000000')
.fee('1000000')
.change(change)
.build();
const expectedFlows = signTxFlows(test, auth, from, to, change, true, null, null, to_hash);
return { appTx, ergoTx, input: uInputs[0],
expectedFlows, flowsCount: expectedFlows.length,
network: TEST_DATA.network };
})
.shouldSucceed(({ergoTx, input, expectedFlows, flows}, signatures) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(signatures).to.have.length(1);
verifySignatures(ergoTx, signatures, input);
})
.run(({test, appTx, network}) => test.device.signTx(appTx, toNetwork(network)));
authTokenFlows("can sign tx change 22")
.init(async ({test, auth}) => {
const from = TEST_DATA.address0;
const to = TEST_DATA.address1;
const change = TEST_DATA.changeAddress22;
const {appTx, ergoTx, uInputs} = new TxBuilder()
.input(from, txId, 0, '1000000000')
.dataInput(from.address, txId, 1)
.output(to.address, '100000000')
.fee('1000000')
.change(change)
.build();
const expectedFlows = signTxFlows(test, auth, from, to, change, false);
return { appTx, ergoTx, input: uInputs[0],
expectedFlows, flowsCount: expectedFlows.length };
})
.shouldSucceed(({ergoTx, input, expectedFlows, flows}, signatures) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(signatures).to.have.length(1);
verifySignatures(ergoTx, signatures, input);
})
.run(({test, appTx}) => test.device.signTx(appTx, toNetwork(TEST_DATA.network)));
authTokenFlows("can sign tx with additional registers")
.init(({test, auth}) => {
const from = TEST_DATA.address0;
const to = TEST_DATA.address1;
const change = TEST_DATA.changeAddress;
const ergoBox = ErgoBox.from_json(`{
"boxId": "ef16f4a6db61a1c31aea55d3bf10e1fb6443cf08cff4a1cf2e3a4780e1312dba",
"value": 1000000000,
"ergoTree": "${from.address.to_ergo_tree().to_base16_bytes()}",
"assets": [],
"additionalRegisters": {
"R5": "0e050102030405",
"R4": "04f601"
},
"creationHeight": 0,
"transactionId": "0000000000000000000000000000000000000000000000000000000000000000",
"index": 0
}`);
const {appTx, ergoTx, uInputs} = new TxBuilder()
.boxInput(ergoBox, from.path)
.output(to.address, '100000000')
.fee('1000000')
.change(change)
.build();
const expectedFlows = signTxFlows(test, auth, from, to, change, false);
return { appTx, ergoTx, input: uInputs[0],
expectedFlows, flowsCount: expectedFlows.length };
})
.shouldSucceed(({ergoTx, input, expectedFlows, flows}, signatures) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(signatures).to.have.length(1);
verifySignatures(ergoTx, signatures, input);
})
.run(({test, appTx}) => test.device.signTx(appTx, toNetwork(TEST_DATA.network)));
authTokenFlows("can sign tx with zero data inputs")
.init(async ({test, auth}) => {
const from = TEST_DATA.address0;
const to = TEST_DATA.address1;
const change = TEST_DATA.changeAddress;
const {appTx, ergoTx, uInputs} = new TxBuilder()
.input(from, txId, 0, '1000000000')
.output(to.address, '100000000')
.fee('1000000')
.change(change)
.build();
const expectedFlows = signTxFlows(test, auth, from, to, change, false);
return { appTx, ergoTx, input: uInputs[0],
expectedFlows, flowsCount: expectedFlows.length };
})
.shouldSucceed(({ergoTx, input, expectedFlows, flows}, signatures) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(signatures).to.have.length(1);
verifySignatures(ergoTx, signatures, input);
})
.run(({test, appTx}) => test.device.signTx(appTx, toNetwork(TEST_DATA.network)));
authTokenFlows("can not sign tx with zero inputs")
.init(async () => {
const tx = new TxBuilder()
.dataInput(TEST_DATA.address0.address, txId, 0)
.output(TEST_DATA.address1.address, '100000000')
.fee(null)
.buildAppTx();
return { tx, flowsCount: 0 };
})
.shouldFail((_, error) => {
expect(error).to.be.an('error');
expect(error.name).to.be.equal('DeviceError');
expect(error.message).to.be.equal('Bad input count');
})
.run(({test, tx}) => test.device.signTx(tx, toNetwork(TEST_DATA.network)));
authTokenFlows("can not sign tx with zero outputs")
.init(async ({test, auth}) => {
const from = TEST_DATA.address0;
const tx = new TxBuilder()
.input(from, txId, 0, '1000000000')
.dataInput(from.address, txId, 0)
.fee(null)
.buildAppTx();
const expectedFlows = signTxFlows(test, auth, from, null, null, false);
return { tx, expectedFlows, flowsCount: expectedFlows.length };
})
.shouldFail(({flows, expectedFlows}, error) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(error).to.be.an('error');
expect(error.name).to.be.equal('DeviceError');
expect(error.message).to.be.equal('Bad output count');
})
.run(({test, tx}) => test.device.signTx(tx, toNetwork(TEST_DATA.network)));
authTokenFlows("can sign tx with tokens")
.init(async ({test, auth}) => {
const from = TEST_DATA.address0;
const to = TEST_DATA.address1;
const change = TEST_DATA.changeAddress;
const tokenId = '1111111111111111111111111111111111111111111111111111111111111111';
const tokens = [{id: tokenId, amount: '1000'}];
const {appTx, ergoTx, uInputs} = new TxBuilder()
.input(from, txId, 0, '1000000000', tokens)
.dataInput(from.address, txId, 0)
.output(to.address, '100000000', tokens)
.fee('1000000')
.change(change)
.build();
const tokensFlow = [
{ header: 'Token [1]', body: ellipsize(test.model, tokenId) },
{ header: 'Token [1] Raw Value', body: '1000' }
];
const expectedFlows = signTxFlows(test, auth, from, to, change, false, tokensFlow);
return { appTx, ergoTx, input: uInputs[0],
expectedFlows, flowsCount: expectedFlows.length };
})
.shouldSucceed(({ergoTx, input, flows, expectedFlows}, signatures) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(signatures).to.have.length(1);
verifySignatures(ergoTx, signatures, input);
})
.run(({test, appTx}) => test.device.signTx(appTx, toNetwork(TEST_DATA.network)));
authTokenFlows("can sign tx with burned tokens")
.init(async ({test, auth}) => {
const from = TEST_DATA.address0;
const to = TEST_DATA.address1;
const change = TEST_DATA.changeAddress;
const tokenId = '1111111111111111111111111111111111111111111111111111111111111111';
const tokens = [{id: tokenId, amount: '1000'}];
const {appTx, ergoTx, uInputs} = new TxBuilder()
.input(from, txId, 0, '1000000000', tokens)
.dataInput(from.address, txId, 0)
.output(to.address, '100000000')
.fee('1000000')
.change(change)
.burn(tokens)
.build();
const tokensFlow = [
{ header: 'Token [1]', body: ellipsize(test.model, tokenId) },
{ header: 'Token [1] Raw Value', body: 'Burning: 1000' }
];
const expectedFlows = signTxFlows(test, auth, from, to, change, false, null, tokensFlow);
return { appTx, ergoTx, input: uInputs[0],
expectedFlows, flowsCount: expectedFlows.length };;
})
.shouldSucceed(({flows, expectedFlows, ergoTx, input}, signatures) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(signatures).to.have.length(1);
verifySignatures(ergoTx, signatures, input);
})
.run(({test, appTx}) => test.device.signTx(appTx, toNetwork(TEST_DATA.network)));
authTokenFlows("can sign tx with minted tokens")
.init(async ({test, auth}) => {
const from = TEST_DATA.address0;
const to = TEST_DATA.address1;
const change = TEST_DATA.changeAddress;
const {appTx, ergoTx, uInputs} = new TxBuilder()
.input(from, txId, 0, '1000000000')
.dataInput(from.address, txId, 0)
.output(to.address, '100000000', null, '1000')
.fee('1000000')
.change(change)
.build();
const tokenId = uInputs[0].box_id().to_str().toUpperCase();
const tokensOutFlow = [
{ header: 'Token [1]', body: ellipsize(test.model, tokenId) },
{ header: 'Token [1] Raw Value', body: '1000' }
];
const tokensTxFlow = [
{ header: 'Token [1]', body: ellipsize(test.model, tokenId) },
{ header: 'Token [1] Raw Value', body: 'Minting: 1000' }
];
const expectedFlows = signTxFlows(test, auth, from, to, change, false, tokensOutFlow, tokensTxFlow);
return { appTx, ergoTx, input: uInputs[0],
expectedFlows, flowsCount: expectedFlows.length };
})
.shouldSucceed(({flows, expectedFlows, ergoTx, input}, signatures) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(signatures).to.have.length(1);
verifySignatures(ergoTx, signatures, input);
})
.run(({test, appTx}) => test.device.signTx(appTx, toNetwork(TEST_DATA.network)));
authTokenFlows("can sign tx with few token ids")
.init(async ({test, auth}) => {
const from = TEST_DATA.address0;
const to = TEST_DATA.address1;
const change = TEST_DATA.changeAddress;
const iTokenId = '1111111111111111111111111111111111111111111111111111111111111111';
const iTokens = [{id: iTokenId, amount: '1234'}];
const {appTx, ergoTx, uInputs} = new TxBuilder()
.input(from, txId, 0, '1000000000', iTokens)
.dataInput(from.address, txId, 0)
.output(to.address, '100000000', null, '5678')
.fee('1000000')
.change(change)
.burn(iTokens)
.build();
const oTokenId = uInputs[0].box_id().to_str().toUpperCase();
const tokensOutFlow = [
{ header: 'Token [1]', body: ellipsize(test.model, oTokenId) },
{ header: 'Token [1] Raw Value', body: '5678' }
];
const tokensTxFlow = [
{ header: 'Token [1]', body: ellipsize(test.model, oTokenId) },
{ header: 'Token [1] Raw Value', body: 'Minting: 5678' },
{ header: 'Token [2]', body: ellipsize(test.model, iTokenId) },
{ header: 'Token [2] Raw Value', body: 'Burning: 1234' }
];
const expectedFlows = signTxFlows(test, auth, from, to, change, false, tokensOutFlow, tokensTxFlow);
return { appTx, ergoTx, input: uInputs[0],
expectedFlows, flowsCount: expectedFlows.length };
})
.shouldSucceed(({flows, expectedFlows, ergoTx, input}, signatures) => {
expect(flows).to.be.deep.equal(expectedFlows);
expect(signatures).to.have.length(1);
verifySignatures(ergoTx, signatures, input);
})
.run(({test, appTx}) => test.device.signTx(appTx, toNetwork(TEST_DATA.network)));
});
});