1
1
const Promise = require ( 'bluebird' ) ;
2
2
const co = Promise . coroutine ;
3
+ const bip32 = require ( 'bip32' ) ;
3
4
const utxolib = require ( '@bitgo/utxo-lib' ) ;
4
5
const accountLib = require ( '@bitgo/account-lib' ) ;
5
6
const statics = require ( '@bitgo/statics' ) ;
@@ -90,7 +91,7 @@ const getHDNodeAndVerify = function(xprv, expectedXpub) {
90
91
let node ;
91
92
92
93
try {
93
- node = utxolib . HDNode . fromBase58 ( xprv ) ;
94
+ node = bip32 . fromBase58 ( xprv ) ;
94
95
} catch ( e ) {
95
96
throw new Error ( 'invalid private key' ) ;
96
97
}
@@ -131,12 +132,10 @@ const promptForConfirmationAndKey = function(recoveryRequest, outputs, skipConfi
131
132
* Gets the backup private key that can be used to sign the transaction.
132
133
* @param xprv The provided extended private key (BIP32).
133
134
* @param expectedXpub The public key specified with the request.
134
- * @returns The private key to sign the transaction.
135
+ * @returns Buffer - the private key buffer to sign the transaction.
135
136
*/
136
137
const getBackupSigningKey = function ( xprv , expectedXpub ) {
137
- const backupKeyNode = getHDNodeAndVerify ( xprv , expectedXpub ) ;
138
-
139
- return backupKeyNode . keyPair . getPrivateKeyBuffer ( ) ;
138
+ return getHDNodeAndVerify ( xprv , expectedXpub ) . privateKey ;
140
139
} ;
141
140
142
141
const handleSignUtxo = function ( recoveryRequest , key , skipConfirm ) {
@@ -157,10 +156,11 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
157
156
158
157
key = promptForConfirmationAndKey ( recoveryRequest , outputs , skipConfirm , key ) ;
159
158
160
- const backupKeyNode = getHDNodeAndVerify ( key , recoveryRequest . backupKey ) ;
161
-
162
159
// force override network as we use btc mainnet xpubs for all utxo coins
163
- backupKeyNode . keyPair . network = network ;
160
+ const backupKeyNode = Object . assign (
161
+ getHDNodeAndVerify ( key , recoveryRequest . backupKey ) ,
162
+ { network }
163
+ ) ;
164
164
165
165
transaction . ins . forEach ( function ( input , i ) {
166
166
transaction . ins [ i ] . value = recoveryRequest . inputs [ i ] . amount ;
@@ -183,7 +183,7 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
183
183
// Handle BCH
184
184
if ( BCH_COINS . includes ( recoveryRequest . coin ) ) {
185
185
const redeemScript = new Buffer ( input . redeemScript , 'hex' ) ;
186
- txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript ,
186
+ txBuilder . sign ( i , derivedHDNode , redeemScript ,
187
187
utxolib . Transaction . SIGHASH_BITCOINCASHBIP143 | utxolib . Transaction . SIGHASH_ALL , input . amount ) ;
188
188
// in a Lodash forEach loop, 'return' operates like 'continue' does
189
189
// in a regular javascript loop. It finishes this iteration and moves to the next iteration
@@ -195,7 +195,7 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
195
195
const witnessScript = Buffer . from ( input . witnessScript , 'hex' ) ;
196
196
const witnessScriptHash = utxolib . crypto . sha256 ( witnessScript ) ;
197
197
const prevOutScript = utxolib . script . witnessScriptHash . output . encode ( witnessScriptHash ) ;
198
- txBuilder . sign ( i , derivedHDNode . keyPair , prevOutScript ,
198
+ txBuilder . sign ( i , derivedHDNode , prevOutScript ,
199
199
utxolib . Transaction . SIGHASH_ALL , input . amount , witnessScript ) ;
200
200
return ;
201
201
}
@@ -204,13 +204,13 @@ const handleSignUtxo = function(recoveryRequest, key, skipConfirm) {
204
204
const redeemScript = new Buffer ( input . redeemScript , 'hex' ) ;
205
205
if ( input . witnessScript ) {
206
206
const witnessScript = new Buffer ( input . witnessScript , 'hex' ) ;
207
- txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript ,
207
+ txBuilder . sign ( i , derivedHDNode , redeemScript ,
208
208
utxolib . Transaction . SIGHASH_ALL , input . amount , witnessScript ) ;
209
209
return ;
210
210
}
211
211
212
212
// Handle all other requests
213
- txBuilder . sign ( i , derivedHDNode . keyPair , redeemScript , utxolib . Transaction . SIGHASH_ALL , input . amount ) ;
213
+ txBuilder . sign ( i , derivedHDNode , redeemScript , utxolib . Transaction . SIGHASH_ALL , input . amount ) ;
214
214
} ) ;
215
215
216
216
return txBuilder . build ( ) . toHex ( ) ;
@@ -347,8 +347,8 @@ const handleSignXrp = function(recoveryRequest, key, skipConfirm) {
347
347
348
348
const backupKeyNode = getHDNodeAndVerify ( key , recoveryRequest . backupKey ) ;
349
349
350
- const backupAddress = rippleKeypairs . deriveAddress ( backupKeyNode . keyPair . getPublicKeyBuffer ( ) . toString ( 'hex' ) ) ;
351
- const privateKeyHex = backupKeyNode . keyPair . getPrivateKeyBuffer ( ) . toString ( 'hex' ) ;
350
+ const backupAddress = rippleKeypairs . deriveAddress ( backupKeyNode . publicKey . toString ( 'hex' ) ) ;
351
+ const privateKeyHex = backupKeyNode . privateKey . toString ( 'hex' ) ;
352
352
const cosignedTx = utils . signXrpWithPrivateKey ( txHex , privateKeyHex , { signAs : backupAddress } ) ;
353
353
354
354
return rippleApi . combine ( [ txHex , cosignedTx . signedTransaction ] ) . signedTransaction ;
@@ -421,7 +421,7 @@ const parseKey = co(function *(rawkey, coin, path) {
421
421
throw new Error ( 'Invalid mnemonic' ) ;
422
422
}
423
423
const seed = yield bip39 . mnemonicToSeed ( mnemonic ) ;
424
- let node = utxolib . HDNode . fromSeedBuffer ( seed ) ;
424
+ let node = bip32 . fromSeed ( seed ) ;
425
425
if ( path ) {
426
426
node = node . derivePath ( path ) ;
427
427
}
0 commit comments