Skip to content

Commit 2541dba

Browse files
author
Mark Toda
committed
Fix Tests
Some tests were broken, causing CI to fail and blocking merges. This commit fixes those tests: - Fix usage of bitgo-account-lib interface since it has changed - Yield promises Ticket: BG-19296
1 parent 50947c9 commit 2541dba

File tree

6 files changed

+60
-51
lines changed

6 files changed

+60
-51
lines changed

app/admin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ const run = co(function *(testArgs) {
445445
yield handleImportKeys(args);
446446
break;
447447
case 'sign':
448-
signingTool.handleSign(args);
448+
yield signingTool.handleSign(args);
449449
break;
450450
case 'derive':
451451
handleDeriveKey(args);

app/sign.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const Promise = require('bluebird');
2+
const co = Promise.coroutine;
13
const utxoLib = require('bitgo-utxo-lib');
24
const accountLib = require('@bitgo/account-lib');
35
const statics = require('@bitgo/statics');
@@ -175,7 +177,7 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
175177

176178
// Derive signing key from chain path
177179
const derivedHDNode = backupKeyNode.derivePath(input.chainPath);
178-
console.log(`Signing input ${ i + 1 } of ${ recoveryRequest.inputs.length }
180+
console.log(`Signing input ${ i + 1 } of ${ recoveryRequest.inputs.length } \
179181
with ${ derivedHDNode.neutered().toBase58() } (${ input.chainPath })`);
180182

181183
// Handle BCH
@@ -214,9 +216,9 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
214216
return txBuilder.build().toHex();
215217
};
216218

217-
const handleHalfSignEth = function(recoveryRequest, key, skipConfirm, basecoin) {
218-
return utils.halfSignEthTransaction(basecoin, recoveryRequest, key);
219-
};
219+
const handleHalfSignEth = co(function *(recoveryRequest, key, skipConfirm, basecoin) {
220+
return yield utils.halfSignEthTransaction(basecoin, recoveryRequest, key);
221+
});
220222

221223
const handleSignEthereum = function(recoveryRequest, key, skipConfirm) {
222224
return signEthTx(recoveryRequest, key, skipConfirm, false);
@@ -268,26 +270,28 @@ const signEthTx = function(recoveryRequest, key, skipConfirm, isToken) {
268270
return transaction.serialize().toString('hex');
269271
};
270272

271-
const handleSignTrx = function(recoveryRequest, key, skipConfirm) {
273+
const handleSignTrx = co(function *(recoveryRequest, key, skipConfirm) {
272274
const coin = recoveryRequest.coin;
273275

274276
const txHex = getTransactionHexFromRequest(recoveryRequest);
275-
const builder = new accountLib.TransactionBuilder({ coinName: coin });
277+
const builder = new accountLib.getBuilder(coin);
276278
builder.from(txHex);
279+
const builtTransaction = yield builder.build();
277280

278-
const outputs = builder.build().destinations.map(d => {
281+
const outputs = builtTransaction.outputs.map(d => {
279282
return {
280283
address: d.address,
281-
amount: d.value.toString(10)
284+
amount: d.value
282285
};
283286
});
284287

285288
key = promptForConfirmationAndKey(recoveryRequest, outputs, skipConfirm, key);
286289
const signingKey = getBackupSigningKey(key, recoveryRequest.backupKey);
287290

288-
builder.sign({ key: signingKey });
289-
return JSON.stringify(builder.build().toJson());
290-
};
291+
builder.sign({ key: signingKey.toString('hex') });
292+
const signedTransaction = yield builder.build();
293+
return JSON.stringify(signedTransaction.toJson());
294+
});
291295

292296
const handleSignEos = function(recoveryRequest, key, skipConfirm) {
293297
const EosJs = require('eosjs');
@@ -402,7 +406,7 @@ const handleSignErc20 = function(recoveryRequest, key, skipConfirm) {
402406
Takes in either an xprv, xlmsecret, or 24 words.
403407
Returns an xprv or xlmsecret
404408
*/
405-
const parseKey = function(rawkey, coin, path) {
409+
const parseKey = co(function *(rawkey, coin, path) {
406410

407411
if (rawkey.includes(',') && rawkey.split(',').length === 24) {
408412
const mnemonic = rawkey.replace(/,/g, ' '); // replace commas with spaces
@@ -416,7 +420,7 @@ const parseKey = function(rawkey, coin, path) {
416420
if (!bip39.validateMnemonic(mnemonic)) {
417421
throw new Error('Invalid mnemonic');
418422
}
419-
const seed = bip39.mnemonicToSeed(mnemonic);
423+
const seed = yield bip39.mnemonicToSeed(mnemonic);
420424
let node = utxoLib.HDNode.fromSeedBuffer(seed);
421425
if (path) {
422426
node = node.derivePath(path);
@@ -432,7 +436,7 @@ const parseKey = function(rawkey, coin, path) {
432436
return node.toBase58();
433437
}
434438
return rawkey;
435-
};
439+
});
436440

437441
/**
438442
Not all recoveryRequest files are formatted the same. Sometimes they have 'tx', 'txHex', or 'transactionHex'
@@ -451,7 +455,7 @@ const getTransactionHexFromRequest = function(recoveryRequest) {
451455
throw new Error('The recovery request did not provide a transaction hex');
452456
};
453457

454-
const handleSign = function(args) {
458+
const handleSign = co(function *(args) {
455459
const file = args.file;
456460

457461
const recoveryRequest = JSON.parse(fs.readFileSync(file, { encoding: 'utf8' }));
@@ -469,7 +473,7 @@ const handleSign = function(args) {
469473
args.key = prompt('Key: ');
470474
}
471475

472-
const key = parseKey(args.key, coin.name, args.path);
476+
const key = yield parseKey(args.key, coin.name, args.path);
473477

474478
let txHex, halfSignedInfo;
475479

@@ -480,7 +484,7 @@ const handleSign = function(args) {
480484
switch (basecoin.getFamily()) {
481485
case 'eth':
482486
if (recoveryRequest.txPrebuild) {
483-
halfSignedInfo = handleHalfSignEth(recoveryRequest, key, args.confirm, basecoin);
487+
halfSignedInfo = yield handleHalfSignEth(recoveryRequest, key, args.confirm, basecoin);
484488
} else {
485489
if (coin.family === 'eth' && !coin.isToken) {
486490
txHex = handleSignEthereum(recoveryRequest, key, args.confirm);
@@ -493,7 +497,7 @@ const handleSign = function(args) {
493497
txHex = handleSignEos(recoveryRequest, key, args.confirm);
494498
break;
495499
case 'trx':
496-
txHex = handleSignTrx(recoveryRequest, key, args.confirm);
500+
txHex = yield handleSignTrx(recoveryRequest, key, args.confirm);
497501
break;
498502
case 'xrp':
499503
txHex = handleSignXrp(recoveryRequest, key, args.confirm);
@@ -528,7 +532,7 @@ const handleSign = function(args) {
528532
console.log('Done');
529533
}
530534
return finalRecovery;
531-
};
535+
});
532536

533537
module.exports = { handleSign, handleSignUtxo, handleSignEthereum,
534538
handleSignXrp, handleSignXlm, handleSignErc20, handleSignEos, handleSignTrx, parseKey };

app/utils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const Promise = require('bluebird');
2+
const co = Promise.coroutine;
13
const Q = require('q');
24
const binary = require('ripple-binary-codec');
35
const nodemailer = require('nodemailer');
@@ -187,7 +189,7 @@ exports.signXrpWithPrivateKey = function(txHex, privateKey, options) {
187189
};
188190
};
189191

190-
exports.halfSignEthTransaction = function(basecoin, recoveryRequest, key) {
192+
exports.halfSignEthTransaction = co(function *(basecoin, recoveryRequest, key) {
191193
const txPrebuild = recoveryRequest.txPrebuild;
192194
const obj = {
193195
prv: key,
@@ -196,7 +198,7 @@ exports.halfSignEthTransaction = function(basecoin, recoveryRequest, key) {
196198
expireTime: txPrebuild.expireTime,
197199
txPrebuild
198200
};
199-
const signedtx = basecoin.signTransaction(obj);
201+
const signedtx = yield basecoin.signTransaction(obj);
200202
const outFile = {
201203
txInfo: signedtx.halfSigned
202204
};
@@ -209,7 +211,7 @@ exports.halfSignEthTransaction = function(basecoin, recoveryRequest, key) {
209211
outFile.tokenContractAddress = recoveryRequest.tokenContractAddress;
210212
}
211213
return outFile;
212-
};
214+
});
213215

214216

215217
/**

package-lock.json

Lines changed: 15 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Reference implementation of the Key Recovery Service protocol for dispensing public keys",
55
"main": "index.js",
66
"scripts": {
7-
"test": "./node_modules/mocha/bin/_mocha --timeout 20000 --recursive test/",
7+
"test": "./node_modules/mocha/bin/_mocha --exit --timeout 20000 --recursive test/",
88
"start": "node -r dotenv/config bin/server.js",
99
"lint": "./node_modules/.bin/eslint --ext .js .",
1010
"audit": "npm audit"
@@ -18,7 +18,7 @@
1818
"supertest": "^3.3.0"
1919
},
2020
"dependencies": {
21-
"@bitgo/account-lib": "^1.0.2",
21+
"@bitgo/account-lib": "1.1.0",
2222
"@bitgo/statics": "^3.4.2",
2323
"argparse": "^1.0.10",
2424
"bignumber.js": "^7.2.1",

test/sign.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const Promise = require('bluebird');
2+
const co = Promise.coroutine;
13
const fs = require('fs');
24
const should = require('should');
35

@@ -148,12 +150,12 @@ describe('Offline Signing Tool', function() {
148150
+ 'JhnuffK8BCMXPBdht77jyoWZHJQzZbwwEvCY4LhTv6Fnnuey1ibnZniJWzrF6y5sZ"]}');
149151
});
150152

151-
it('cosigns a trx transaction', function() {
153+
it('cosigns a trx transaction', co(function *() {
152154
const recoveryRequest = JSON.parse(fs.readFileSync('./test/transactions/ttrx.json', { encoding: 'utf8' }));
153155
const key = 'xprv9s21ZrQH143K2SGfLqMk9eaSbix4XUqXg2wqXkATpfnQsyvaXBTnEqi71aLSq1rL3qJh32FRrA2VnrfMMEmbN'
154156
+ 'S5xnRCiNSHKdAVR6Ep5Ptx';
155157

156-
const txHex = signingTool.handleSignTrx(recoveryRequest, key, true);
158+
const txHex = yield signingTool.handleSignTrx(recoveryRequest, key, true);
157159
txHex.should.equal('{"visible":false,"txID":"26d73a8892e9a5ed6bccc07da7b8113ced08749fcb7b2600bb96a4076'
158160
+ '6eed8da","raw_data":{"contract":[{"parameter":{"value":{"amount":22000000,"owner_address":"41becdc3'
159161
+ '8018e2202ec67679257ba97fce9b3995a4","to_address":"41979719d19c20cb8480ed0f1135285ff14c8dad58"},"type'
@@ -167,7 +169,7 @@ describe('Offline Signing Tool', function() {
167169
+ '929f3d59dac0e434d4c07c553a951e0c42fe01f4677d587ae7a226a9d7521a8a7200"]}');
168170
const tx = JSON.parse(txHex);
169171
tx.signature.length.should.equal(2);
170-
});
172+
}));
171173

172174
// function called in transaction: sendMultiSigToken, see https://bloxy.info/functions/0dcd7a6c, pre-EIP155
173175
it('cosigns an erc20 transaction', function() {
@@ -254,39 +256,39 @@ describe('Offline Signing Tool', function() {
254256
}
255257
});
256258

257-
it('parses a private key from 24 words and a path', function() {
259+
it('parses a private key from 24 words and a path', co(function *() {
258260
const key = 'bone,penalty,bundle,plug,february,roof,rely,angry,inspire,auto,indicate,shell,assist,unhappy,unab'
259261
+ 'le,clarify,pond,check,size,key,donor,midnight,inquiry,avoid';
260262
const path = 'm/0';
261-
const xprv = signingTool.parseKey(key, 'eth', path);
263+
const xprv = yield signingTool.parseKey(key, 'eth', path);
262264
xprv.should.equal('xprv9u4GesLhZXFMtAFY2vT1QpXQrgJHRcbTKnw2J1Bqm2m2HpDztvS7D3AwF69fYZnuBJyJQm4v8hegzKY'
263265
+ '3rLCmBgujbZ3sFRzzLT42Z9oTqBt');
264-
});
266+
}));
265267

266-
it('half-signs an ETH transaction', function() {
268+
it('half-signs an ETH transaction', co(function *() {
267269
const key = 'xprv9s21ZrQH143K2VPbcq9NwP51S43YX67rUG834oU8BvHgvSayfp9DRuPs6xfKThGbHbdaiGNWdyS5LmTc9GdCV'
268270
+ 'CpNUs6wfyaLukHsvVB8PwP';
269271
const file = './test/transactions/unsigned-teth.json';
270272
const expectedOut = JSON.parse(fs.readFileSync('./test/transactions/half-signed-teth.json'));
271273

272274
const args = { file, key, noWrite: true };
273-
const outFile = signingTool.handleSign(args);
275+
const outFile = yield signingTool.handleSign(args);
274276
should(outFile.txInfo.sequenceId).not.be.ok();
275277
delete outFile.txInfo.sequenceId;
276278
delete outFile.txInfo.hopTransaction;
277279
outFile.should.deepEqual(expectedOut);
278-
});
280+
}));
279281

280-
it('half-signs an ERC-20 transaction', function() {
282+
it('half-signs an ERC-20 transaction', co(function *() {
281283
const key = 'xprv9s21ZrQH143K2VPbcq9NwP51S43YX67rUG834oU8BvHgvSayfp9DRuPs6xfKThGbHbdaiGNWdyS5LmTc9GdCV'
282284
+ 'CpNUs6wfyaLukHsvVB8PwP';
283285
const file = './test/transactions/unsigned-terc.json';
284286
const expectedOut = JSON.parse(fs.readFileSync('./test/transactions/half-signed-terc.json'));
285287
const args = { file, key, noWrite: true };
286-
const outFile = signingTool.handleSign(args);
288+
const outFile = yield signingTool.handleSign(args);
287289
should(outFile.txInfo.sequenceId).not.be.ok();
288290
delete outFile.txInfo.sequenceId;
289291
delete outFile.txInfo.hopTransaction;
290292
outFile.should.deepEqual(expectedOut);
291-
});
293+
}));
292294
});

0 commit comments

Comments
 (0)