@@ -2,8 +2,8 @@ import LedgerProvider from '../LedgerProvider'
22import Bitcoin from '@ledgerhq/hw-app-btc'
33
44import { BigNumber } from 'bignumber.js'
5- import { base58 , padHexStart , sha256 , ripemd160 } from '../../crypto'
6- import { pubKeyToAddress , addressToPubKeyHash , compressPubKey , createXPUB , encodeBase58Check , parseHexString } from './BitcoinUtil'
5+ import { base58 , padHexStart } from '../../crypto'
6+ import { pubKeyToAddress , addressToPubKeyHash , compressPubKey } from './BitcoinUtil'
77import Address from '../../Address'
88import networks from './networks'
99import bip32 from 'bip32'
@@ -16,58 +16,22 @@ export default class BitcoinLedgerProvider extends LedgerProvider {
1616 this . _bjsnetwork = chain . network . name . replace ( 'bitcoin_' , '' ) // for bitcoin js
1717 this . _segwit = chain . segwit
1818 this . _coinType = chain . network . coinType
19- this . _extendedPubKeyCache = { }
19+ this . _walletPublicKeyCache = { }
2020 }
2121
22- async getPubKey ( from ) {
22+ async _getWalletPublicKey ( path ) {
2323 const app = await this . getApp ( )
24- const derivationPath = from . derivationPath ||
25- await this . getDerivationPathFromAddress ( from )
26- return app . getWalletPublicKey ( derivationPath )
24+ return app . getWalletPublicKey ( path , undefined , this . _segwit )
2725 }
2826
29- async _getAddressExtendedPubKey ( path ) {
30- const app = await this . getApp ( )
31- var parts = path . split ( '/' )
32- var prevPath = parts [ 0 ] + '/' + parts [ 1 ]
33- var account = parseInt ( parts [ 2 ] )
34- var segwit = this . _segwit
35- var network = this . _network . bip32 . public
36- const finalize = async fingerprint => {
37- // var path = prevPath + '/' + account
38- let nodeData = await app . getWalletPublicKey ( path , undefined , segwit )
39- var publicKey = compressPubKey ( nodeData . publicKey )
40- var childnum = ( 0x80000000 | account ) >>> 0
41- var xpub = createXPUB (
42- 3 ,
43- fingerprint ,
44- childnum ,
45- nodeData . chainCode ,
46- publicKey ,
47- network
48- )
49- return encodeBase58Check ( xpub )
27+ async getWalletPublicKey ( path ) {
28+ if ( path in this . _walletPublicKeyCache ) {
29+ return this . _walletPublicKeyCache [ path ]
5030 }
5131
52- let nodeData = await app . getWalletPublicKey ( prevPath , undefined , segwit )
53- var publicKey = compressPubKey ( nodeData . publicKey )
54- publicKey = parseHexString ( publicKey )
55- var result = sha256 ( Buffer . from ( publicKey , 'hex' ) )
56- result = ripemd160 ( result )
57- var fingerprint =
58- ( ( result [ 0 ] << 24 ) | ( result [ 1 ] << 16 ) | ( result [ 2 ] << 8 ) | result [ 3 ] ) >>>
59- 0
60- return finalize ( fingerprint )
61- }
62-
63- async getAddressExtendedPubKey ( path ) {
64- if ( path in this . _extendedPubKeyCache ) {
65- return this . _extendedPubKeyCache [ path ]
66- }
67-
68- const extendedPubKey = this . _getAddressExtendedPubKey ( path )
69- this . _extendedPubKeyCache [ path ] = extendedPubKey
70- return extendedPubKey
32+ const walletPublicKey = this . _getWalletPublicKey ( path )
33+ this . _walletPublicKeyCache [ path ] = walletPublicKey
34+ return walletPublicKey
7135 }
7236
7337 async getAddressFromDerivationPath ( path ) {
@@ -78,11 +42,9 @@ export default class BitcoinLedgerProvider extends LedgerProvider {
7842
7943 async signMessage ( message , from ) {
8044 const app = await this . getApp ( )
81- const derivationPath = from . derivationPath ||
82- await this . getDerivationPathFromAddress ( from )
83-
45+ const address = await this . getWalletAddress ( from )
8446 const hex = Buffer . from ( message ) . toString ( 'hex' )
85- return app . signMessageNew ( derivationPath , hex )
47+ return app . signMessageNew ( address . derivationPath , hex )
8648 }
8749
8850 // async getUnusedAddress (from = {}) {
@@ -432,16 +394,25 @@ export default class BitcoinLedgerProvider extends LedgerProvider {
432394 }
433395
434396 async getLedgerAddresses ( startingIndex , numAddresses , change = false ) {
397+ const walletPubKey = await this . getWalletPublicKey ( this . _baseDerivationPath )
398+ const compressedPubKey = compressPubKey ( walletPubKey . publicKey )
399+ const node = bip32 . fromPublicKey (
400+ Buffer . from ( compressedPubKey , 'hex' ) ,
401+ Buffer . from ( walletPubKey . chainCode , 'hex' ) ,
402+ this . _network
403+ )
404+
435405 const addresses = [ ]
436406 const lastIndex = startingIndex + numAddresses
437407 const changeVal = change ? '1' : '0'
438- const xpubkeys = await this . getAddressExtendedPubKeys ( this . _baseDerivationPath )
439- const node = bip32 . fromBase58 ( xpubkeys [ 0 ] , this . _network )
440408 for ( let currentIndex = startingIndex ; currentIndex < lastIndex ; currentIndex ++ ) {
441- const address = pubKeyToAddress ( node . derivePath ( changeVal + '/' + currentIndex ) . __Q , this . _network . name , 'pubKeyHash' )
442- const path = this . _baseDerivationPath + changeVal + '/' + currentIndex
409+ const subPath = changeVal + '/' + currentIndex
410+ const publicKey = node . derivePath ( subPath ) . publicKey
411+ const address = pubKeyToAddress ( publicKey , this . _network . name , 'pubKeyHash' )
412+ const path = this . _baseDerivationPath + subPath
443413 addresses . push ( {
444414 address,
415+ publicKey : publicKey . toString ( 'hex' ) ,
445416 derivationPath : path ,
446417 index : currentIndex
447418 } )
0 commit comments