-
Notifications
You must be signed in to change notification settings - Fork 23
/
createmultisig.spec.ts
526 lines (452 loc) · 20.2 KB
/
createmultisig.spec.ts
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
'use strict';
import * as ByteBuffer from 'bytebuffer';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as sinon from 'sinon';
import { SinonSandbox, SinonSpy } from 'sinon';
import * as helpers from '../../../../src/helpers';
import { TransactionType } from '../../../../src/helpers';
import { MultiSignatureTransaction } from '../../../../src/logic/transactions';
import {
AccountLogicStub, AccountsModuleStub, ByteBufferStub, RoundsLogicStub, SystemModuleStub,
TransactionLogicStub, ZSchemaStub
} from '../../../stubs';
// tslint:disable-next-line no-var-requires
const assertArrays = require('chai-arrays');
const expect = chai.expect;
chai.use(chaiAsPromised);
chai.use(assertArrays);
// tslint:disable no-unused-expression
describe('logic/transactions/createmultisig', () => {
let sandbox: SinonSandbox;
let socketIOStub: any;
let zSchemaStub: ZSchemaStub;
let accountLogicStub: AccountLogicStub;
let transactionLogicStub: TransactionLogicStub;
let roundsLogicStub: RoundsLogicStub;
let accountsModuleStub: AccountsModuleStub;
let systemModuleStub: SystemModuleStub;
let instance: MultiSignatureTransaction;
let tx: any;
let sender: any;
let block: any;
beforeEach(() => {
sandbox = sinon.sandbox.create();
socketIOStub = {
sockets: {
emit: sandbox.stub(),
},
};
zSchemaStub = new ZSchemaStub();
accountLogicStub = new AccountLogicStub();
transactionLogicStub = new TransactionLogicStub();
accountsModuleStub = new AccountsModuleStub();
systemModuleStub = new SystemModuleStub();
roundsLogicStub = new RoundsLogicStub();
tx = {
asset : {
multisignature: {
min : 2,
lifetime : 33,
keysgroup: ['+key1', '+key2'],
},
},
type : TransactionType.MULTI,
amount : 0,
fee : 10,
timestamp : 0,
senderId : '1233456789012345R',
senderPublicKey: '6588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b3817e58fe3',
signatures : ['sig1', 'sig2'],
id : '8139741256612355994',
};
sender = {
balance : 10000000,
address : '1233456789012345R',
publicKey: '6588716f9c941530c74eabdf0b27b1a2bac0a1525e9605a37e6c0b3817e58fe3',
};
block = {
height: 8797,
id : '13191140260435645922',
};
instance = new MultiSignatureTransaction();
(instance as any).io = socketIOStub;
(instance as any).schema = zSchemaStub;
(instance as any).accountLogic = accountLogicStub;
(instance as any).transactionLogic = transactionLogicStub;
(instance as any).roundsLogic = roundsLogicStub;
(instance as any).accountsModule = accountsModuleStub;
(instance as any).systemModule = systemModuleStub;
return systemModuleStub.stubs.getFees.returns({ fees: { multisignature: 123 } });
});
afterEach(() => {
sandbox.restore();
});
describe('calculateFee', () => {
it('should call systemModule.getFees', () => {
instance.calculateFee(tx, sender, block.height);
expect(systemModuleStub.stubs.getFees.calledOnce).to.be.true;
expect(systemModuleStub.stubs.getFees.firstCall.args[0]).to.be.equal(block.height);
});
});
describe('getBytes', () => {
const expectedBuffer = Buffer.from('');
let sequence: any[];
let lastBB: any;
const toRestore = {} as any;
before(() => {
toRestore.writeByte = ByteBuffer.prototype.writeByte;
toRestore.writeInt = ByteBuffer.prototype.writeInt;
toRestore.writeLong = (ByteBuffer.prototype as any).writeLong;
toRestore.toBuffer = ByteBuffer.prototype.toBuffer;
toRestore.flip = ByteBuffer.prototype.flip;
});
beforeEach(() => {
lastBB = false;
sequence = [];
(ByteBuffer.prototype as any).writeByte = function(b) {
sequence.push(b);
lastBB = this;
};
ByteBuffer.prototype.toBuffer = sandbox.stub().returns(expectedBuffer);
ByteBuffer.prototype.flip = sandbox.stub();
});
after(() => {
(ByteBuffer.prototype as any).writeByte = toRestore.writeByte;
(ByteBuffer.prototype as any).writeInt = toRestore.writeInt;
(ByteBuffer.prototype as any).writeLong = toRestore.writeLong;
ByteBuffer.prototype.flip = toRestore.flip;
ByteBuffer.prototype.toBuffer = toRestore.toBuffer;
});
it('should call Buffer.from', () => {
const fromSpy = sandbox.spy(Buffer, 'from');
instance.getBytes(tx, false, false);
expect(fromSpy.calledOnce).to.be.true;
expect(fromSpy.firstCall.args[0]).to.be.equal(tx.asset.multisignature.keysgroup.join(''));
expect(fromSpy.firstCall.args[1]).to.be.equal('utf8');
});
it('should create a ByteBuffer', () => {
instance.getBytes(tx, false, false);
expect(lastBB).to.be.instanceof(ByteBuffer);
});
it('should write bytes to bytebuffer', () => {
instance.getBytes(tx, false, false);
expect(sequence[0]).to.be.equal(tx.asset.multisignature.min);
expect(sequence[1]).to.be.equal(tx.asset.multisignature.lifetime);
expect(lastBB.flip.calledOnce).to.be.true;
});
it('should call toBuffer and return a Buffer', () => {
const retVal = instance.getBytes(tx, false, false);
expect(lastBB.toBuffer.calledOnce).to.be.true;
expect(retVal).to.be.deep.equal(expectedBuffer);
});
});
describe('verify', () => {
beforeEach(() => {
transactionLogicStub.enqueueResponse('verifySignature', true);
transactionLogicStub.enqueueResponse('verifySignature', true);
});
it('should throw when !tx.asset || !tx.asset.multisignature', async () => {
delete tx.asset.multisignature;
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid transaction asset');
delete tx.asset;
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid transaction asset');
});
it('should throw when asset.multisignature.keygroup is not an array', async () => {
tx.asset.multisignature.keysgroup = null;
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid multisignature keysgroup. Must be an array');
});
it('should throw when asset.multisignature.keygroup is empty', async () => {
tx.asset.multisignature.keysgroup = [];
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid multisignature keysgroup. Must not be empty');
});
it('should throw when min and max are incompatible with constants', async () => {
tx.asset.multisignature.min = -1;
await expect(instance.verify(tx, sender)).to.be.rejectedWith(/Invalid multisignature min. Must be between/);
});
it('should throw when min is more than keysgroup length', async () => {
tx.asset.multisignature.min = tx.asset.multisignature.keysgroup.length + 1;
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid multisignature min. Must be less than or equal to keysgroup size');
});
it('should throw when lifetime is incompatible with constants', async () => {
tx.asset.multisignature.lifetime = 12312312313;
await expect(instance.verify(tx, sender)).to.be.rejectedWith(/Invalid multisignature lifetime./);
});
it('should throw when account has multisig enabled', async () => {
sender.multisignatures = ['senderSig1', 'senderSig2'];
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Account already has multisignatures enabled');
});
it('should throw when recipientId is invalid', async () => {
tx.recipientId = '15256762582730568272R';
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid recipient');
});
it('should throw when amount is invalid', async () => {
tx.amount = 100;
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid transaction amount');
});
it('should call transactionLogic.verifySignature on all signatures and keys', async () => {
const readySpy = sandbox.spy(instance, 'ready');
await instance.verify(tx, sender);
expect(readySpy.calledOnce).to.be.true;
});
it('should call transactionLogic.verifySignature on all signatures and keys', async () => {
await instance.verify(tx, sender);
expect(transactionLogicStub.stubs.verifySignature.called).to.be.true;
expect(transactionLogicStub.stubs.verifySignature.callCount).to.be.eq(2);
});
it('should throw if signature verification fails', async () => {
transactionLogicStub.reset();
transactionLogicStub.enqueueResponse('verifySignature', false);
transactionLogicStub.enqueueResponse('verifySignature', false);
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Failed to verify signature in multisignature keysgroup');
});
it('should throw if keysgroup contains the sender', async () => {
tx.asset.multisignature.keysgroup[0] = '+' + sender.publicKey;
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid multisignature keysgroup. Cannot contain sender');
});
it('should throw if keysgroup contains an invalid key', async () => {
// We make ready() return false so that we can skip another branch where it would throw because of invalid key
sandbox.stub(instance, 'ready').returns(false);
tx.asset.multisignature.keysgroup[0] = {};
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid member in keysgroup');
});
it('should throw if wrong math operator in keysgroup', async () => {
// We make ready() return false so that we can skip another branch where it would throw for invalid keysgroup
sandbox.stub(instance, 'ready').returns(false);
tx.asset.multisignature.keysgroup[0] = '-key1';
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid math operator in multisignature keysgroup');
});
it('should call schema.validate on the pubKey', async () => {
zSchemaStub.enqueueResponse('validate', true);
await instance.verify(tx, sender);
expect(zSchemaStub.stubs.validate.callCount).to.be.equal(tx.asset.multisignature.keysgroup.length);
expect(zSchemaStub.stubs.validate.firstCall.args[0]).to.be.equal(tx.asset.multisignature.keysgroup[0].substring(1));
expect(zSchemaStub.stubs.validate.firstCall.args[1]).to.be.deep.equal({ format: 'publicKey' });
});
it('should throw if pubKey is invalid', async () => {
zSchemaStub.enqueueResponse('validate', false);
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Invalid publicKey in multisignature keysgroup');
});
it('should throw if duplicate pubKey is found', async () => {
tx.asset.multisignature.keysgroup[1] = tx.asset.multisignature.keysgroup[0];
await expect(instance.verify(tx, sender)).to.be.rejectedWith('Encountered duplicate public key in multisignature keysgroup');
});
it('should resolve on successful execution', async () => {
await expect(instance.verify(tx, sender)).to.be.fulfilled;
});
});
describe('apply', () => {
beforeEach(() => {
accountLogicStub.stubs.merge.resolves();
roundsLogicStub.stubs.calcRound.returns(123);
accountLogicStub.stubs.generateAddressByPublicKey.returns('123123124125R');
accountsModuleStub.stubs.setAccountAndGet.resolves();
});
it('should call accountLogic.merge', async () => {
await instance.apply(tx, block, sender);
expect(accountLogicStub.stubs.merge.calledOnce).to.be.true;
expect(accountLogicStub.stubs.merge.firstCall.args[0]).to.be.equal(sender.address);
expect(accountLogicStub.stubs.merge.firstCall.args[1]).to.be.deep.equal({
blockId : block.id,
multilifetime : tx.asset.multisignature.lifetime,
multimin : tx.asset.multisignature.min,
multisignatures: tx.asset.multisignature.keysgroup,
round : 123,
});
});
it('should call roundsLogic.calcRound', async () => {
await instance.apply(tx, block, sender);
expect(roundsLogicStub.stubs.calcRound.calledOnce).to.be.true;
expect(roundsLogicStub.stubs.calcRound.firstCall.args[0]).to.be.equal(block.height);
});
it('should call accountLogic.generateAddressByPublicKey for each key', async () => {
await instance.apply(tx, block, sender);
expect(accountLogicStub.stubs.generateAddressByPublicKey.callCount).to.be.equal(tx.asset.multisignature.keysgroup.length);
expect(accountLogicStub.stubs.generateAddressByPublicKey.firstCall.args[0]).to.be.equal(tx.asset.multisignature.keysgroup[0].substring(1));
});
it('should call accountsModule.setAccountAndGet for each key', async () => {
await instance.apply(tx, block, sender);
expect(accountsModuleStub.stubs.setAccountAndGet.callCount).to.be.equal(tx.asset.multisignature.keysgroup.length);
expect(accountsModuleStub.stubs.setAccountAndGet.firstCall.args[0]).to.be.deep.equal({
address : '123123124125R',
publicKey: tx.asset.multisignature.keysgroup[0].substring(1),
});
});
});
describe('undo', () => {
let reverseSpy: SinonSpy;
beforeEach(() => {
accountLogicStub.stubs.merge.returns(true);
roundsLogicStub.stubs.calcRound.returns(123);
accountLogicStub.stubs.generateAddressByPublicKey.returns('123123124125R');
accountsModuleStub.stubs.setAccountAndGet.resolves();
reverseSpy = sandbox.spy(helpers.Diff, 'reverse');
});
afterEach(() => {
reverseSpy.restore();
});
it('should call Diff.reverse', async () => {
await instance.undo(tx, block, sender);
expect(reverseSpy.calledOnce).to.be.true;
expect(reverseSpy.firstCall.args[0]).to.be.equalTo(tx.asset.multisignature.keysgroup);
});
it('should set unconfirmedSignatures[sender.address] to true', async () => {
await instance.undo(tx, block, sender);
expect((instance as any).unconfirmedSignatures[sender.address]).to.be.true;
});
it('should call accountLogic.merge', async () => {
await instance.undo(tx, block, sender);
expect(accountLogicStub.stubs.merge.calledOnce).to.be.true;
expect(accountLogicStub.stubs.merge.firstCall.args[0]).to.be.equal(sender.address);
expect(accountLogicStub.stubs.merge.firstCall.args[1]).to.be.deep.equal({
blockId : block.id,
multilifetime : -tx.asset.multisignature.lifetime,
multimin : -tx.asset.multisignature.min,
multisignatures: tx.asset.multisignature.keysgroup.map((a) => a.replace('+', '-')),
round : 123,
});
});
it('should call roundsLogic.calcRound', async () => {
await instance.undo(tx, block, sender);
expect(roundsLogicStub.stubs.calcRound.calledOnce).to.be.true;
expect(roundsLogicStub.stubs.calcRound.firstCall.args[0]).to.be.equal(block.height);
});
});
describe('applyUnconfirmed', () => {
beforeEach(() => {
accountLogicStub.stubs.merge.returns(true);
});
it('should throw if signature is not confirmed yet', () => {
(instance as any).unconfirmedSignatures[sender.address] = true;
expect(() => {
instance.applyUnconfirmed(tx, sender);
}).to.throw('Signature on this account is pending confirmation');
});
it('should call accountLogic.merge', async () => {
await instance.applyUnconfirmed(tx, sender);
expect(accountLogicStub.stubs.merge.calledOnce).to.be.true;
expect(accountLogicStub.stubs.merge.firstCall.args[0]).to.be.equal(sender.address);
expect(accountLogicStub.stubs.merge.firstCall.args[1]).to.be.deep.equal({
u_multilifetime : tx.asset.multisignature.lifetime,
u_multimin : tx.asset.multisignature.min,
u_multisignatures: tx.asset.multisignature.keysgroup,
});
});
});
describe('undoUnconfirmed', () => {
let reverseSpy: SinonSpy;
beforeEach(() => {
accountLogicStub.stubs.merge.returns(true);
reverseSpy = sandbox.spy(helpers.Diff, 'reverse');
});
afterEach(() => {
reverseSpy.restore();
});
it('should call Diff.reverse', async () => {
await instance.undoUnconfirmed(tx, sender);
expect(reverseSpy.calledOnce).to.be.true;
expect(reverseSpy.firstCall.args[0]).to.be.equalTo(tx.asset.multisignature.keysgroup);
});
it('should delete unconfirmedSignatures[sender.address]', async () => {
await instance.undoUnconfirmed(tx, sender);
expect((instance as any).unconfirmedSignatures[sender.address]).to.be.undefined;
});
it('should call accountLogic.merge', async () => {
await instance.undoUnconfirmed(tx, sender);
expect(accountLogicStub.stubs.merge.calledOnce).to.be.true;
expect(accountLogicStub.stubs.merge.firstCall.args[0]).to.be.equal(sender.address);
expect(accountLogicStub.stubs.merge.firstCall.args[1]).to.be.deep.equal({
u_multilifetime : -tx.asset.multisignature.lifetime,
u_multimin : -tx.asset.multisignature.min,
u_multisignatures: tx.asset.multisignature.keysgroup.map((a) => a.replace('+', '-')),
});
});
});
describe('objectNormalize', () => {
it('should call schema.validate', () => {
instance.objectNormalize(tx);
expect(zSchemaStub.stubs.validate.calledOnce).to.be.true;
expect(zSchemaStub.stubs.validate.firstCall.args[0]).to.be.deep.equal(tx.asset.multisignature);
});
it('should throw if validation fails', () => {
zSchemaStub.enqueueResponse('validate', false);
zSchemaStub.enqueueResponse('getLastErrors', []);
expect(() => {
instance.objectNormalize(tx);
}).to.throw(/Failed to validate multisignature schema/);
});
it('should return the tx', () => {
zSchemaStub.enqueueResponse('validate', true);
const retVal = instance.objectNormalize(tx);
expect(retVal).to.be.deep.equal(tx);
});
});
describe('dbRead', () => {
it('should return null if !m_keysgroup', () => {
const retVal = instance.dbRead({});
expect(retVal).to.be.null;
});
it('should return the multisignature object', () => {
const retVal = instance.dbRead({
m_keysgroup: 'key1,key2',
m_lifetime : 10,
m_min : 2,
});
expect(retVal).to.be.deep.equal(
{
multisignature: {
keysgroup: [
'key1',
'key2',
],
lifetime : 10,
min : 2,
},
});
});
});
describe('dbSave', () => {
it('should return the expected object', () => {
expect(instance.dbSave(tx)).to.be.deep.equal({
table : 'multisignatures',
fields: ['min', 'lifetime', 'keysgroup', 'transactionId'],
values:
{
min : 2,
lifetime : 33,
keysgroup : '+key1,+key2',
transactionId: '8139741256612355994'
}
}
);
});
});
describe('afterSave', () => {
it('should emit on the socket and resolve', async () => {
await instance.afterSave(tx);
expect(socketIOStub.sockets.emit.calledOnce).to.be.true;
expect(socketIOStub.sockets.emit.firstCall.args[0]).to.be.equal('multisignatures/change');
expect(socketIOStub.sockets.emit.firstCall.args[1]).to.be.deep.equal(tx);
});
});
describe('ready', () => {
it('return false if tx.signatures is not an array', () => {
tx.signatures = [];
expect(instance.ready(tx, sender)).to.be.false;
});
it('return false if no sender.multisignatures and signatures arrays lengths are not the same', () => {
sender.multisignatures = ['1', '2', '3'];
tx.signatures = ['1', '2'];
expect(instance.ready(tx, sender)).to.be.false;
});
it('return true if no sender.multisignatures and signatures arrays lengths are the same', () => {
// precondition is already there
expect(instance.ready(tx, sender)).to.be.true;
});
it('return true if tx.signatures are more or equal to the sender.multimin', () => {
tx.signatures = ['1', '2', '3'];
sender.multimin = 2;
expect(instance.ready(tx, sender)).to.be.false;
});
});
});