From 5d93ce39a4a88775b8db1cb151511c58f8f1383d Mon Sep 17 00:00:00 2001 From: Gaoqi Date: Sun, 15 Mar 2020 15:05:40 -0700 Subject: [PATCH 1/4] Leverage constants from BitGoJS.statics Use decimals from BitGoJS.statics in the following functions: - handleSignUtxo - signEthTx - handleSignXrp Use networks from BitGoJS.statics in the following functions: - handleSignUtxo --- app/sign.js | 92 ++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 43 deletions(-) diff --git a/app/sign.js b/app/sign.js index 7e65e0d..4d0145f 100644 --- a/app/sign.js +++ b/app/sign.js @@ -11,45 +11,45 @@ const bip39 = require('bip39'); const bitgojs = require('bitgo'); let bitgo; -const utxoNetworks = { - btc: utxoLib.networks.bitcoin, - ltc: utxoLib.networks.litecoin, - bch: utxoLib.networks.bitcoincash, - bsv: utxoLib.networks.bitcoinsv, - zec: utxoLib.networks.zcash, - dash: utxoLib.networks.dash, - tltc: utxoLib.networks.litecoin, - tbtc: utxoLib.networks.testnet, - tbch: utxoLib.networks.bitcoincashTestnet, - tbsv: utxoLib.networks.bitcoinsvTestnet, - tzec: utxoLib.networks.zcashTest, - tdash: utxoLib.networks.dashTest, -}; - -const coinDecimals = { - btc: 8, - eth: 18, - eos: 4, - trx: 6, - xrp: 6, - bch: 8, - bsv: 8, - ltc: 8, - zec: 8, - dash: 8, - xlm: 7, - tbtc: 8, - teth: 18, - teos: 4, - ttrx: 6, - txrp: 6, - tltc: 8, - txlm: 7, - tbch: 8, - tbsv: 8, - tzec: 8, - tdash: 8, -}; +// const utxoNetworks = { +// btc: utxoLib.networks.bitcoin, +// ltc: utxoLib.networks.litecoin, +// bch: utxoLib.networks.bitcoincash, +// bsv: utxoLib.networks.bitcoinsv, +// zec: utxoLib.networks.zcash, +// dash: utxoLib.networks.dash, +// tltc: utxoLib.networks.litecoin, +// tbtc: utxoLib.networks.testnet, +// tbch: utxoLib.networks.bitcoincashTestnet, +// tbsv: utxoLib.networks.bitcoinsvTestnet, +// tzec: utxoLib.networks.zcashTest, +// tdash: utxoLib.networks.dashTest, +// }; + +// const coinDecimals = { +// btc: 8, +// eth: 18, +// eos: 4, +// trx: 6, +// xrp: 6, +// bch: 8, +// bsv: 8, +// ltc: 8, +// zec: 8, +// dash: 8, +// xlm: 7, +// tbtc: 8, +// teth: 18, +// teos: 4, +// ttrx: 6, +// txrp: 6, +// tltc: 8, +// txlm: 7, +// tbch: 8, +// tbsv: 8, +// tzec: 8, +// tdash: 8, +// }; const BCH_COINS = ['bch', 'tbch', 'bsv', 'tbsv']; const TEN = new BN(10); @@ -131,8 +131,11 @@ const getBackupSigningKey = function(key, expectedXpub) { } const handleSignUtxo = function(recoveryRequest, key, skipConfirm) { - const network = utxoNetworks[recoveryRequest.coin]; - const decimals = coinDecimals[recoveryRequest.coin]; + + // const network = utxoNetworks[recoveryRequest.coin]; + // const decimals = coinDecimals[recoveryRequest.coin]; + const network = statics.coins.get[recoveryRequest.coin].network; + const decimals = statics.coins.get[recoveryRequest.coin].decimalPlaces; if (!network) { throw new Error(`Unsupported coin: ${recoveryRequest.coin}`); @@ -232,7 +235,9 @@ const signEthTx = function(recoveryRequest, key, skipConfirm, isToken) { // if request is for ETH, need to correct the amount decimals. if (!isToken) { - const decimals = coinDecimals[recoveryRequest.coin]; + // const decimals = coinDecimals[recoveryRequest.coin]; + const decimals = statics.coins.get[recoveryRequest.coin].decimalPlaces; + outputs[0].amount = outputs[0].amount.div(TEN.pow(decimals)); } @@ -305,7 +310,8 @@ const handleSignXrp = function(recoveryRequest, key, skipConfirm) { const txHex = getTransactionHexFromRequest(recoveryRequest); - const decimals = coinDecimals[recoveryRequest.coin]; + // const decimals = coinDecimals[recoveryRequest.coin]; + const decimals = statics.coins.get[recoveryRequest.coin].decimalPlaces; const transaction = rippleParse.decode(txHex); const outputs = [{ From 3ac7a5897602667ed61f4b6c612a42dccb7bab8f Mon Sep 17 00:00:00 2001 From: Gaoqi Date: Mon, 16 Mar 2020 15:32:01 -0700 Subject: [PATCH 2/4] Remove unnecessary code --- app/sign.js | 43 ------------------------------------------- 1 file changed, 43 deletions(-) diff --git a/app/sign.js b/app/sign.js index 4d0145f..7dc40df 100644 --- a/app/sign.js +++ b/app/sign.js @@ -11,45 +11,6 @@ const bip39 = require('bip39'); const bitgojs = require('bitgo'); let bitgo; -// const utxoNetworks = { -// btc: utxoLib.networks.bitcoin, -// ltc: utxoLib.networks.litecoin, -// bch: utxoLib.networks.bitcoincash, -// bsv: utxoLib.networks.bitcoinsv, -// zec: utxoLib.networks.zcash, -// dash: utxoLib.networks.dash, -// tltc: utxoLib.networks.litecoin, -// tbtc: utxoLib.networks.testnet, -// tbch: utxoLib.networks.bitcoincashTestnet, -// tbsv: utxoLib.networks.bitcoinsvTestnet, -// tzec: utxoLib.networks.zcashTest, -// tdash: utxoLib.networks.dashTest, -// }; - -// const coinDecimals = { -// btc: 8, -// eth: 18, -// eos: 4, -// trx: 6, -// xrp: 6, -// bch: 8, -// bsv: 8, -// ltc: 8, -// zec: 8, -// dash: 8, -// xlm: 7, -// tbtc: 8, -// teth: 18, -// teos: 4, -// ttrx: 6, -// txrp: 6, -// tltc: 8, -// txlm: 7, -// tbch: 8, -// tbsv: 8, -// tzec: 8, -// tdash: 8, -// }; const BCH_COINS = ['bch', 'tbch', 'bsv', 'tbsv']; const TEN = new BN(10); @@ -132,8 +93,6 @@ const getBackupSigningKey = function(key, expectedXpub) { const handleSignUtxo = function(recoveryRequest, key, skipConfirm) { - // const network = utxoNetworks[recoveryRequest.coin]; - // const decimals = coinDecimals[recoveryRequest.coin]; const network = statics.coins.get[recoveryRequest.coin].network; const decimals = statics.coins.get[recoveryRequest.coin].decimalPlaces; @@ -235,7 +194,6 @@ const signEthTx = function(recoveryRequest, key, skipConfirm, isToken) { // if request is for ETH, need to correct the amount decimals. if (!isToken) { - // const decimals = coinDecimals[recoveryRequest.coin]; const decimals = statics.coins.get[recoveryRequest.coin].decimalPlaces; outputs[0].amount = outputs[0].amount.div(TEN.pow(decimals)); @@ -310,7 +268,6 @@ const handleSignXrp = function(recoveryRequest, key, skipConfirm) { const txHex = getTransactionHexFromRequest(recoveryRequest); - // const decimals = coinDecimals[recoveryRequest.coin]; const decimals = statics.coins.get[recoveryRequest.coin].decimalPlaces; const transaction = rippleParse.decode(txHex); From 1730b1b7a42ed25ea9b537326c5d7cbb618a5a9b Mon Sep 17 00:00:00 2001 From: Gaoqi Date: Mon, 16 Mar 2020 15:48:34 -0700 Subject: [PATCH 3/4] GetCoin alias Using getCoinConfig(coin, data) instead of statics.coins.get[coin].data https://github.com/BitGo/key-recovery-service-v2/issues/61 --- app/sign.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/sign.js b/app/sign.js index 7dc40df..0fc1518 100644 --- a/app/sign.js +++ b/app/sign.js @@ -18,6 +18,10 @@ const TEN = new BN(10); const EOS_MAINNET_CHAIN_ID = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906'; const EOS_TESTNET_CHAIN_ID = 'e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473'; +const getCoinConfig = function(coin, data) { + return statics.coins.get[coin].data; +} + const confirmRecovery = function(backupKey, outputs, customMessage, skipConfirm) { console.log('Sign Recovery Transaction'); console.log('========================='); @@ -93,8 +97,8 @@ const getBackupSigningKey = function(key, expectedXpub) { const handleSignUtxo = function(recoveryRequest, key, skipConfirm) { - const network = statics.coins.get[recoveryRequest.coin].network; - const decimals = statics.coins.get[recoveryRequest.coin].decimalPlaces; + const network = getCoinConfig(recoveryRequest.coin, network); + const decimals = getCoinConfig(recoveryRequest.coin, decimalPlaces); if (!network) { throw new Error(`Unsupported coin: ${recoveryRequest.coin}`); @@ -194,7 +198,7 @@ const signEthTx = function(recoveryRequest, key, skipConfirm, isToken) { // if request is for ETH, need to correct the amount decimals. if (!isToken) { - const decimals = statics.coins.get[recoveryRequest.coin].decimalPlaces; + const decimals = getCoinConfig(recoveryRequest.coin, decimalPlaces); outputs[0].amount = outputs[0].amount.div(TEN.pow(decimals)); } @@ -268,7 +272,7 @@ const handleSignXrp = function(recoveryRequest, key, skipConfirm) { const txHex = getTransactionHexFromRequest(recoveryRequest); - const decimals = statics.coins.get[recoveryRequest.coin].decimalPlaces; + const decimals = getCoinConfig(recoveryRequest.coin, decimalPlaces); const transaction = rippleParse.decode(txHex); const outputs = [{ From cb650e885ba34d92d6c8074b06bc7515253d669d Mon Sep 17 00:00:00 2001 From: Gaoqi Date: Wed, 18 Mar 2020 15:37:23 -0700 Subject: [PATCH 4/4] Update sign.js Change var to string --- app/sign.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/sign.js b/app/sign.js index 0fc1518..f9ced33 100644 --- a/app/sign.js +++ b/app/sign.js @@ -19,7 +19,7 @@ const EOS_MAINNET_CHAIN_ID = 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbea const EOS_TESTNET_CHAIN_ID = 'e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473'; const getCoinConfig = function(coin, data) { - return statics.coins.get[coin].data; + return statics.coins.get[coin].[data]; } const confirmRecovery = function(backupKey, outputs, customMessage, skipConfirm) { @@ -97,8 +97,8 @@ const getBackupSigningKey = function(key, expectedXpub) { const handleSignUtxo = function(recoveryRequest, key, skipConfirm) { - const network = getCoinConfig(recoveryRequest.coin, network); - const decimals = getCoinConfig(recoveryRequest.coin, decimalPlaces); + const network = getCoinConfig(recoveryRequest.coin, 'network'); + const decimals = getCoinConfig(recoveryRequest.coin, 'decimalPlaces'); if (!network) { throw new Error(`Unsupported coin: ${recoveryRequest.coin}`); @@ -198,7 +198,7 @@ const signEthTx = function(recoveryRequest, key, skipConfirm, isToken) { // if request is for ETH, need to correct the amount decimals. if (!isToken) { - const decimals = getCoinConfig(recoveryRequest.coin, decimalPlaces); + const decimals = getCoinConfig(recoveryRequest.coin, 'decimalPlaces'); outputs[0].amount = outputs[0].amount.div(TEN.pow(decimals)); } @@ -272,7 +272,7 @@ const handleSignXrp = function(recoveryRequest, key, skipConfirm) { const txHex = getTransactionHexFromRequest(recoveryRequest); - const decimals = getCoinConfig(recoveryRequest.coin, decimalPlaces); + const decimals = getCoinConfig(recoveryRequest.coin, 'decimalPlaces'); const transaction = rippleParse.decode(txHex); const outputs = [{