99
1010var BitGoJS = require ( '../src/index.js' ) ;
1111var readline = require ( 'readline' ) ;
12+ var HDNode = require ( '../src/hdnode' ) ;
13+ var Address = require ( 'bitcoinjs-lib/src/address' ) ;
14+ var Script = require ( 'bitcoinjs-lib/src/script' ) ;
15+ var Scripts = require ( 'bitcoinjs-lib/src/scripts' ) ;
16+ var Transaction = require ( 'bitcoinjs-lib/src/transaction' ) ;
17+ var TransactionBuilder = require ( 'bitcoinjs-lib/src/transaction_builder' ) ;
18+ var networks = require ( 'bitcoinjs-lib/src/networks' ) ;
19+ var Util = require ( '../src/util' ) ;
1220var Q = require ( 'q' ) ;
1321
1422var chain = require ( 'chain-node' ) ; // Use chain.com as our alternate blockchain api provider
@@ -23,7 +31,7 @@ var bitgoKey; // The BIP32 xpub for the bitgo public key
2331var subAddresses = { } ; // A map of addresses containing funds to recover
2432var unspents ; // The unspents from the HD wallet
2533var transaction ; // The transaction to send
26- var bitcoinNetwork = 'prod ' ;
34+ var bitcoinNetwork = 'bitcoin ' ;
2735
2836var info = '\n' +
2937'**********************************\n' +
@@ -36,7 +44,6 @@ var info = '\n' +
3644'Please enter a blank line after each input.\n' ;
3745console . log ( info ) ;
3846
39-
4047//
4148// collectInputs
4249// Function to asynchronously collect inputs for the recovery tool.
@@ -114,7 +121,7 @@ var decryptKeys = function() {
114121 throw new Error ( 'must be xprv key' ) ;
115122 }
116123 }
117- return new BitGoJS . BIP32 ( key ) ;
124+ return HDNode . fromBase58 ( key ) ;
118125 } catch ( e ) {
119126 throw new Error ( 'invalid key: ' + e ) ;
120127 }
@@ -168,11 +175,12 @@ var findBaseAddress = function() {
168175
169176 for ( var key in keys ) {
170177 var keyData = keys [ key ] ;
171- keyData . derived = keyData . key . derive ( keyData . path ) ;
172- keyData . derivedPubKey = keyData . derived . eckey . getPub ( ) ;
178+ keyData . derived = keyData . key . deriveFromPath ( keyData . path ) ;
179+ keyData . derivedPubKey = keyData . derived . pubKey ;
173180 pubKeys . push ( keyData . derivedPubKey ) ;
174181 }
175- var baseAddress = BitGoJS . Address . createMultiSigAddress ( pubKeys , 2 ) ;
182+ var network = BitGoJS . getNetwork ( ) === 'bitcoin' ? networks . bitcoin : networks . testnet ;
183+ var baseAddress = Address . fromOutputScript ( Util . p2shMultisigOutputScript ( 2 , pubKeys ) , network ) . toBase58Check ( ) ;
176184
177185 console . log ( "\tTrying address: " + baseAddress ) ;
178186 chain . getAddress ( baseAddress , function ( err , data ) {
@@ -280,13 +288,14 @@ var findSubAddresses = function(baseAddress) {
280288 for ( var key in baseAddress . keys ) {
281289 var keyData = baseAddress . keys [ key ] ;
282290 var path = keyData . path + '/' + keyIndex + '/' + addressIndex ;
283- keyData . derived = keyData . key . derive ( path ) ;
284- keyData . derivedPubKey = keyData . derived . eckey . getPub ( ) ;
291+ keyData . derived = keyData . key . deriveFromPath ( path ) ;
292+ keyData . derivedPubKey = keyData . derived . pubKey ;
285293 pubKeys . push ( keyData . derivedPubKey ) ;
286294 }
287295
288- var subAddress = BitGoJS . Address . createMultiSigAddress ( pubKeys , 2 ) ;
289- var subAddressString = subAddress . toString ( ) ;
296+ var network = BitGoJS . getNetwork ( ) === 'bitcoin' ? networks . bitcoin : networks . testnet ;
297+ var redeemScript = Scripts . multisigOutput ( 2 , pubKeys ) ;
298+ var subAddressString = Address . fromOutputScript ( Util . p2shMultisigOutputScript ( 2 , pubKeys ) , network ) . toBase58Check ( ) ;
290299
291300 console . log ( "Trying keyIndex " + keyIndex + " addressIndex " + addressIndex + ": " + subAddressString + "..." ) ;
292301 chain . getAddress ( subAddressString , function ( err , data ) {
@@ -297,15 +306,15 @@ var findSubAddresses = function(baseAddress) {
297306 lookahead = 0 ;
298307 console . log ( '\tfound: ' + data . balance + ' at ' + subAddressString ) ;
299308 subAddresses [ subAddressString ] = {
300- address : subAddress ,
309+ address : subAddressString ,
301310 keyIndex : keyIndex ,
302311 addressIndex : addressIndex ,
303312 keys : [
304313 { key : baseAddress . keys [ 0 ] . derived , path : baseAddress . keys [ 0 ] . path + '/' + keyIndex + '/' + addressIndex } ,
305314 { key : baseAddress . keys [ 1 ] . derived , path : baseAddress . keys [ 1 ] . path + '/' + keyIndex + '/' + addressIndex } ,
306315 { key : baseAddress . keys [ 2 ] . derived , path : baseAddress . keys [ 2 ] . path + '/' + keyIndex + '/' + addressIndex } ,
307316 ] ,
308- redeemScript : BitGoJS . Util . bytesToHex ( subAddress . redeemScript )
317+ redeemScript : redeemScript . toBuffer ( ) . toString ( 'hex' )
309318 } ;
310319 } else {
311320 if ( ++ lookahead >= MAX_LOOKAHEAD_ADDRESSES ) {
@@ -358,22 +367,23 @@ var findUnspents = function() {
358367
359368var createTransaction = function ( ) {
360369 var totalValue = 0 ;
361- transaction = new BitGoJS . Transaction ( ) ;
370+ transaction = new Transaction ( ) ;
362371
363372 // Add the inputs
364373 for ( var index in unspents ) {
365374 var unspent = unspents [ index ] ;
366- transaction . addInput ( new BitGoJS . TransactionIn ( {
367- outpoint : { hash : unspent . transaction_hash , index : unspent . output_index } ,
368- script : new BitGoJS . Script ( unspent . script_hex ) ,
369- sequence : 4294967295
370- } ) ) ;
375+ var hash = new Buffer ( unspent . transaction_hash , 'hex' ) ;
376+ hash = new Buffer ( Array . prototype . reverse . call ( hash ) ) ;
377+ var index = unspent . output_index ;
378+ var script = Script . fromHex ( unspent . script_hex ) ;
379+ var sequence = 0xffffffff ;
380+ transaction . addInput ( hash , index , sequence , script ) ;
371381 totalValue += unspent . value ;
372382 }
373383
374384 // Note: we haven't signed the inputs yet. When we sign them, the transaction will grow by
375385 // about 232 bytes per input (2 sigs + redeemscript + misc)
376- var approximateSize = transaction . serialize ( ) . length + ( 232 * unspents . length ) ;
386+ var approximateSize = transaction . toBuffer ( ) . length + ( 232 * unspents . length ) ;
377387 var approximateFee = ( ( Math . floor ( approximateSize / 1024 ) ) + 1 ) * 0.0001 * 1e8 ;
378388 if ( approximateFee > totalValue ) {
379389 throw new Error ( "Insufficient funds to recover (Have your transactions confirmed yet?)" ) ;
@@ -384,22 +394,32 @@ var createTransaction = function() {
384394 console . log ( "Fee: " + approximateFee / 1e8 + "BTC" ) ;
385395
386396 // Create the output
387- transaction . addOutput ( new BitGoJS . Address ( inputs . destination ) , totalValue ) ;
397+ var script = Address . fromBase58Check ( inputs . destination ) . toOutputScript ( ) ;
398+ transaction . addOutput ( Address . fromBase58Check ( inputs . destination ) , totalValue ) ;
399+
400+ var txb = TransactionBuilder . fromTransaction ( transaction ) ;
388401
389402 for ( index in unspents ) {
403+ index = parseInt ( index ) ;
390404 var unspent = unspents [ index ] ;
391- var redeemScript = new BitGoJS . Script ( unspent . redeemScript ) ;
405+ var redeemScript = Script . fromHex ( unspent . redeemScript ) ;
392406
393- console . log ( "Signing input " + ( parseInt ( index ) + 1 ) + " of " + unspents . length ) ;
407+ console . log ( "Signing input " + ( index + 1 ) + " of " + unspents . length ) ;
394408
395- if ( ! transaction . signMultiSigWithKey ( index , unspent . keys [ 0 ] . key . eckey , redeemScript ) ) {
396- throw new Error ( 'Signature failure for user key' ) ;
409+ try {
410+ txb . sign ( index , unspent . keys [ 0 ] . key . privKey , redeemScript ) ;
411+ } catch ( e ) {
412+ throw new Error ( 'Signature failure for user key: ' + e ) ;
397413 }
398- if ( ! transaction . signMultiSigWithKey ( index , unspent . keys [ 1 ] . key . eckey , redeemScript ) ) {
399- throw new Error ( 'Signature failure for backup key' ) ;
414+ try {
415+ txb . sign ( index , unspent . keys [ 1 ] . key . privKey , redeemScript ) ;
416+ } catch ( e ) {
417+ throw new Error ( 'Signature failure for backup key: ' + e ) ;
400418 }
401419 }
402420
421+ transaction = txb . build ( ) ;
422+
403423 return Q . when ( ) ;
404424} ;
405425
@@ -408,7 +428,7 @@ var createTransaction = function() {
408428// Actually send the fully created transaction to the bitcoin network.
409429//
410430var sendTransaction = function ( ) {
411- var tx = BitGoJS . Util . bytesToHex ( transaction . serialize ( ) ) ;
431+ var tx = transaction . toBuffer ( ) . toString ( 'hex' ) ;
412432
413433 console . log ( "Sending transaction: " + tx ) ;
414434
0 commit comments