Skip to content

Commit 86cb23e

Browse files
authored
Merge branch 'dev' into wallet-error
2 parents 02e96f1 + 2360bb8 commit 86cb23e

File tree

9 files changed

+43
-165
lines changed

9 files changed

+43
-165
lines changed

src/Client.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,6 @@ export default class Client {
323323
return addresses
324324
}
325325

326-
async getAddressExtendedPubKeys (startingIndex = 0, numAddresses = 1) {
327-
const xpubkey = await this.getMethod('getAddressExtendedPubKeys')(startingIndex, numAddresses)
328-
329-
if (!isArray(xpubkey)) {
330-
throw new InvalidProviderResponseError('Provider returned an invalid response')
331-
}
332-
333-
return xpubkey
334-
}
335326
/**
336327
* Check if an address has been used or not.
337328
* @param {!string|Address} addresses - An address to check for.
@@ -434,7 +425,7 @@ export default class Client {
434425
* @return {Promise<string>} Resolves with secret
435426
*/
436427
async generateSecret (message) {
437-
const address = (await this.getMethod('getAddresses')())[0]
428+
const address = (await this.getMethod('getAddresses')())[0].address
438429
const signedMessage = await this.signMessage(message, address)
439430
const secret = sha256(signedMessage)
440431
return secret

src/providers/LedgerProvider.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class LedgerProvider extends Provider {
6262
return this._baseDerivationPath + changePath + index
6363
}
6464

65-
async getDerivationPathFromAddress (address) {
65+
async getWalletAddress (address) {
6666
let index = 0
6767
let change = false
6868

@@ -75,7 +75,7 @@ export default class LedgerProvider extends Provider {
7575
const addrs = await this.getAddresses(index, addressesPerCall)
7676
const addr = addrs.find(addr => addr.address === address)
7777
if (addr) {
78-
return addr.derivationPath
78+
return addr
7979
}
8080
index += addressesPerCall
8181
if (index === maxAddresses && change === false) {
@@ -97,18 +97,6 @@ export default class LedgerProvider extends Provider {
9797
return address
9898
}
9999

100-
async getAddressExtendedPubKeys (startingIndex = 0, numAddresses = 1) {
101-
const xpubkeys = []
102-
const lastIndex = startingIndex + numAddresses
103-
104-
for (let currentIndex = startingIndex; currentIndex < lastIndex; currentIndex++) {
105-
const xpubkey = await this.getAddressExtendedPubKey(currentIndex)
106-
xpubkeys.push(xpubkey)
107-
}
108-
109-
return xpubkeys
110-
}
111-
112100
async getAddresses (startingIndex = 0, numAddresses = 1, change = false) {
113101
return this.getAddresses(startingIndex, numAddresses, change)
114102
}

src/providers/bitcoin/BitcoinJsLibSwapProvider.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Provider from '../../Provider'
2-
import { addressToPubKeyHash, compressPubKey, pubKeyToAddress, reverseBuffer, scriptNumEncode } from './BitcoinUtil'
2+
import { addressToPubKeyHash, pubKeyToAddress, reverseBuffer, scriptNumEncode } from './BitcoinUtil'
33
import { sha256, padHexStart } from '../../crypto'
44
import networks from './networks'
55
import bitcoin from 'bitcoinjs-lib'
@@ -102,42 +102,7 @@ export default class BitcoinJsLibSwapProvider extends Provider {
102102
}
103103

104104
async refundSwap (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration) {
105-
return this._redeemSwap(initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, false)
106-
}
107-
108-
async _redeemSwap (initiationTxHash, recipientAddress, refundAddress, secretParam, expiration, isClaim) {
109-
const secretHash = isClaim ? sha256(secretParam) : secretParam
110-
const lockTime = isClaim ? 0 : expiration + 100
111-
const lockTimeHex = isClaim ? padHexStart('0', 8) : padHexStart(scriptNumEncode(lockTime).toString('hex'), 8)
112-
const to = isClaim ? recipientAddress : refundAddress
113-
const script = this.createSwapScript(recipientAddress, refundAddress, secretHash, expiration)
114-
const scriptPubKey = padHexStart(script)
115-
const p2shAddress = pubKeyToAddress(scriptPubKey, this._network.name, 'scriptHash')
116-
const sendScript = this.getMethod('createScript')(p2shAddress)
117-
const initiationTxRaw = await this.getMethod('getRawTransactionByHash')(initiationTxHash)
118-
const initiationTx = await this.getMethod('splitTransaction')(initiationTxRaw, true)
119-
const voutIndex = initiationTx.outputs.findIndex((output) => output.script.toString('hex') === sendScript)
120-
const txHashLE = Buffer.from(initiationTxHash, 'hex').reverse().toString('hex') // TX HASH IN LITTLE ENDIAN
121-
const newTxInput = this.generateSigTxInput(txHashLE, voutIndex, script)
122-
const newTx = this.generateRawTx(initiationTx, voutIndex, to, newTxInput, lockTimeHex)
123-
const splitNewTx = await this.getMethod('splitTransaction')(newTx, true)
124-
const outputScriptObj = await this.getMethod('serializeTransactionOutputs')(splitNewTx)
125-
const outputScript = outputScriptObj.toString('hex')
126-
const addressPath = await this.getMethod('getDerivationPathFromAddress')(to)
127-
const signature = await this.getMethod('signP2SHTransaction')(
128-
[[initiationTx, 0, script, 0]],
129-
[addressPath],
130-
outputScript,
131-
lockTime
132-
)
133-
const pubKeyInfo = await this.getMethod('getPubKey')(isClaim ? recipientAddress : refundAddress)
134-
const pubKey = compressPubKey(pubKeyInfo.publicKey)
135-
const spendSwap = this._spendSwap(signature[0], pubKey, isClaim, secretParam)
136-
const spendSwapInput = this._spendSwapInput(spendSwap, script)
137-
const rawClaimTxInput = this.generateRawTxInput(txHashLE, spendSwapInput)
138-
const rawClaimTx = this.generateRawTx(initiationTx, voutIndex, to, rawClaimTxInput, lockTimeHex)
139-
140-
return this.getMethod('sendRawTransaction')(rawClaimTx)
105+
throw new Error('BitcoinJsLibSwapProvider: Refunding not implemented')
141106
}
142107

143108
_spendSwap (signature, pubKey, isClaim, secret) {

src/providers/bitcoin/BitcoinLedgerProvider.js

Lines changed: 26 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import LedgerProvider from '../LedgerProvider'
22
import Bitcoin from '@ledgerhq/hw-app-btc'
33

44
import { 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'
77
import Address from '../../Address'
88
import networks from './networks'
99
import 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 = await this._getAddressExtendedPubKey(path)
69-
this._extendedPubKeyCache[path] = extendedPubKey
70-
return extendedPubKey
32+
const walletPublicKey = await 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
})

src/providers/bitcoin/BitcoinSwapProvider.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Provider from '../../Provider'
2-
import { addressToPubKeyHash, compressPubKey, pubKeyToAddress, reverseBuffer, scriptNumEncode } from './BitcoinUtil'
2+
import { addressToPubKeyHash, pubKeyToAddress, reverseBuffer, scriptNumEncode } from './BitcoinUtil'
33
import { BigNumber } from 'bignumber.js'
44
import { sha256, padHexStart } from '../../crypto'
55
import networks from './networks'
@@ -79,18 +79,16 @@ export default class BitcoinSwapProvider extends Provider {
7979
const outputScriptObj = await this.getMethod('serializeTransactionOutputs')(splitNewTx)
8080
const outputScript = outputScriptObj.toString('hex')
8181

82-
const addressPath = await this.getMethod('getDerivationPathFromAddress')(to)
82+
const walletAddress = await this.getMethod('getWalletAddress')(to)
8383

8484
const signature = await this.getMethod('signP2SHTransaction')(
8585
[[initiationTx, 0, script, 0]],
86-
[addressPath],
86+
[walletAddress.derivationPath],
8787
outputScript,
8888
lockTime
8989
)
9090

91-
const pubKeyInfo = await this.getMethod('getPubKey')(isClaim ? recipientAddress : refundAddress)
92-
const pubKey = compressPubKey(pubKeyInfo.publicKey)
93-
const spendSwap = this._spendSwap(signature[0], pubKey, isClaim, secretParam)
91+
const spendSwap = this._spendSwap(signature[0], walletAddress.publicKey, isClaim, secretParam)
9492
const spendSwapInput = this._spendSwapInput(spendSwap, script)
9593
const rawClaimTxInput = this.generateRawTxInput(txHashLE, spendSwapInput)
9694
const rawClaimTx = await this.generateRawTx(initiationTx, voutIndex, to, rawClaimTxInput, lockTimeHex, feePerByte)

src/providers/bitcoin/BitcoinUtil.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import {
55
base58
66
} from '../../crypto'
77

8-
import bitcoin from 'bitcoinjs-lib'
9-
import bs58 from 'bs58'
10-
11-
import padStart from 'lodash/padStart'
128
import networks from './networks'
139

1410
/**
@@ -103,24 +99,6 @@ function scriptNumEncode (number) {
10399
return buffer
104100
}
105101

106-
function parseHexString (str) {
107-
var result = []
108-
while (str.length >= 2) {
109-
result.push(parseInt(str.substring(0, 2), 16))
110-
str = str.substring(2, str.length)
111-
}
112-
return result
113-
}
114-
115-
function encodeBase58Check (vchIn) {
116-
vchIn = parseHexString(vchIn.toString())
117-
var chksum = bitcoin.crypto.sha256(Buffer.from(vchIn, 'hex'))
118-
chksum = bitcoin.crypto.sha256(chksum)
119-
chksum = chksum.slice(0, 4)
120-
var hash = vchIn.concat(Array.from(chksum))
121-
return bs58.encode(hash)
122-
}
123-
124102
function toHexDigit (number) {
125103
var digits = '0123456789abcdef'
126104
return digits.charAt(number >> 4) + digits.charAt(number & 0x0f)
@@ -135,20 +113,7 @@ function toHexInt (number) {
135113
)
136114
}
137115

138-
function createXPUB (depth, fingerprint, childnum, chaincode, publicKey, network) {
139-
var xpub = toHexInt(network)
140-
xpub = xpub + padStart(depth.toString(16), 2, '0')
141-
xpub = xpub + padStart(fingerprint.toString(16), 8, '0')
142-
xpub = xpub + padStart(childnum.toString(16), 8, '0')
143-
xpub = xpub + chaincode
144-
xpub = xpub + publicKey
145-
return xpub
146-
}
147-
148116
export {
149-
encodeBase58Check,
150-
createXPUB,
151-
parseHexString,
152117
toHexInt,
153118
compressPubKey,
154119
pubKeyToAddress,

src/providers/bitcoin/BitcoreRPCProvider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default class BitcoreRPCProvider extends BitcoinRPCProvider {
7575

7676
for (let currentIndex = startingIndex; currentIndex < lastIndex; currentIndex++) {
7777
const address = await this.getNewAddress()
78-
addresses.push(address)
78+
addresses.push({ address })
7979
}
8080

8181
return addresses

src/providers/ethereum/EthereumLedgerProvider.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ export default class EthereumLedgerProvider extends LedgerProvider {
1717

1818
async signMessage (message, from) {
1919
const app = await this.getApp()
20-
const derivationPath = from.derivationPath ||
21-
await this.getDerivationPathFromAddress(from)
22-
20+
const address = await this.getWalletAddress(from)
2321
const hex = Buffer.from(message).toString('hex')
24-
return app.signPersonalMessage(derivationPath, hex)
22+
return app.signPersonalMessage(address.derivationPath, hex)
2523
}
2624
}

src/providers/ethereum/EthereumRPCProvider.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export default class EthereumRPCProvider extends JsonRpcProvider {
1111
}
1212

1313
async getAddresses () {
14-
return this.jsonrpc('eth_accounts')
14+
const addresses = await this.jsonrpc('eth_accounts')
15+
return addresses.map(address => ({ address }))
1516
}
1617

1718
async generateBlock (numberOfBlocks) {
@@ -21,7 +22,7 @@ export default class EthereumRPCProvider extends JsonRpcProvider {
2122

2223
async getUnusedAddress () {
2324
var addresses = await this.getAddresses()
24-
return { address: addresses[0] }
25+
return addresses[0]
2526
}
2627

2728
async sendTransaction (to, value, data, from = null) {
@@ -31,7 +32,8 @@ export default class EthereumRPCProvider extends JsonRpcProvider {
3132

3233
if (from == null) {
3334
const addresses = await this.getAddresses()
34-
from = ensureHexEthFormat(('address' in addresses) ? addresses[0].address : addresses[0])
35+
const address = addresses[0].address
36+
from = ensureHexEthFormat(address)
3537
}
3638
value = BigNumber(value).toString(16)
3739

0 commit comments

Comments
 (0)