Skip to content

Commit c82f810

Browse files
committed
BitcoinLedgerProvider: Implement getConnectedNetwork
1 parent 0757c3e commit c82f810

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/providers/LedgerProvider.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ export default class LedgerProvider extends WalletProvider {
77
return Transport.isSupported()
88
}
99

10-
constructor (App, baseDerivationPath) {
11-
super()
10+
constructor (App, baseDerivationPath, network) {
11+
super(network)
1212

1313
this._App = App
1414
this._baseDerivationPath = baseDerivationPath
15+
this._network = network
1516
this._addressCache = {}
1617
}
1718

src/providers/bitcoin/BitcoinLedgerProvider.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import Bitcoin from '@ledgerhq/hw-app-btc'
33

44
import { BigNumber } from 'bignumber.js'
55
import { base58, padHexStart } from '../../crypto'
6-
import { pubKeyToAddress, addressToPubKeyHash, compressPubKey } from './BitcoinUtil'
6+
import { pubKeyToAddress, addressToPubKeyHash, compressPubKey, getAddressNetwork } from './BitcoinUtil'
77
import Address from '../../Address'
88
import networks from './networks'
99
import bip32 from 'bip32'
1010

1111
export default class BitcoinLedgerProvider extends LedgerProvider {
1212
constructor (chain = { network: networks.bitcoin, segwit: false }, numberOfBlockConfirmation = 1) {
13-
super(Bitcoin, `${chain.segwit ? '49' : '44'}'/${chain.network.coinType}'/0'/`)
13+
super(Bitcoin, `${chain.segwit ? '49' : '44'}'/${chain.network.coinType}'/0'/`, chain.network)
1414
this._derivationPath = `${chain.segwit ? '49' : '44'}'/${chain.network.coinType}'/0'/`
1515
this._network = chain.network
1616
this._bjsnetwork = chain.network.name.replace('bitcoin_', '') // for bitcoin js
@@ -502,4 +502,10 @@ export default class BitcoinLedgerProvider extends LedgerProvider {
502502
async getAddresses (startingIndex = 0, numAddresses = 1, change = false) {
503503
return this.getLedgerAddresses(startingIndex, numAddresses, change)
504504
}
505+
506+
async getConnectedNetwork () {
507+
const walletPubKey = await this.getWalletPublicKey(this._baseDerivationPath)
508+
const network = getAddressNetwork(walletPubKey.bitcoinAddress)
509+
return network
510+
}
505511
}

src/providers/bitcoin/BitcoinUtil.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import _ from 'lodash'
2+
13
import {
24
ensureBuffer,
35
hash160,
@@ -112,6 +114,17 @@ function toHexInt (number) {
112114
toHexDigit(number & 0xff)
113115
)
114116
}
117+
/**
118+
* Get a network object from an address
119+
* @param {string} address The bitcoin address
120+
* @return {Network}
121+
*/
122+
function getAddressNetwork (address) {
123+
const prefix = base58.decode(address).toString('hex').substring(0, 2).toUpperCase()
124+
const networkKey = _.findKey(networks,
125+
network => [network.pubKeyHash, network.scriptHash].includes(prefix))
126+
return networks[networkKey]
127+
}
115128

116129
export {
117130
toHexInt,
@@ -120,5 +133,6 @@ export {
120133
pubKeyHashToAddress,
121134
addressToPubKeyHash,
122135
reverseBuffer,
123-
scriptNumEncode
136+
scriptNumEncode,
137+
getAddressNetwork
124138
}

0 commit comments

Comments
 (0)