From 28f70b4b8aa982fae0b21680f821f8bd121a42c7 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Mon, 17 Jan 2022 15:22:15 +0000 Subject: [PATCH] REF: changed interface of bip32 dep --- class/multisig-cosigner.js | 6 +++-- class/wallets/abstract-hd-electrum-wallet.js | 21 ++++++++------- class/wallets/hd-aezeed-wallet.js | 5 +++- class/wallets/hd-legacy-breadwallet-wallet.js | 5 +++- .../hd-legacy-electrum-seed-p2pkh-wallet.js | 16 ++++++----- class/wallets/hd-legacy-p2pkh-wallet.js | 10 ++++--- .../hd-segwit-electrum-seed-p2wpkh-wallet.js | 16 ++++++----- class/wallets/hd-segwit-p2sh-wallet.js | 10 ++++--- class/wallets/multisig-hd-wallet.js | 27 ++++++++++--------- class/wallets/watch-only-wallet.js | 6 +++-- screen/selftest.js | 4 ++- 11 files changed, 74 insertions(+), 52 deletions(-) diff --git a/class/multisig-cosigner.js b/class/multisig-cosigner.js index 30d941438..434b52a3a 100644 --- a/class/multisig-cosigner.js +++ b/class/multisig-cosigner.js @@ -1,6 +1,8 @@ import b58 from 'bs58check'; import { MultisigHDWallet } from './wallets/multisig-hd-wallet'; -const HDNode = require('bip32'); +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; +const bip32 = BIP32Factory(ecc); export class MultisigCosigner { constructor(data) { @@ -136,7 +138,7 @@ export class MultisigCosigner { try { xpub = MultisigCosigner._zpubToXpub(key); - HDNode.fromBase58(xpub); + bip32.fromBase58(xpub); return true; } catch (_) {} diff --git a/class/wallets/abstract-hd-electrum-wallet.js b/class/wallets/abstract-hd-electrum-wallet.js index d307df085..8574abbfe 100644 --- a/class/wallets/abstract-hd-electrum-wallet.js +++ b/class/wallets/abstract-hd-electrum-wallet.js @@ -1,16 +1,17 @@ import * as bip39 from 'bip39'; import BigNumber from 'bignumber.js'; import b58 from 'bs58check'; +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; import { randomBytes } from '../rng'; import { AbstractHDWallet } from './abstract-hd-wallet'; import { ECPairFactory } from 'ecpair'; -const ecc = require('tiny-secp256k1'); const ECPair = ECPairFactory(ecc); const bitcoin = require('bitcoinjs-lib'); const BlueElectrum = require('../../blue_modules/BlueElectrum'); -const HDNode = require('bip32'); const reverse = require('buffer-reverse'); +const bip32 = BIP32Factory(ecc); /** * Electrum - means that it utilizes Electrum protocol for blockchain data @@ -90,7 +91,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet { _getWIFByIndex(internal, index) { if (!this.secret) return false; const seed = this._getSeed(); - const root = HDNode.fromSeed(seed); + const root = bip32.fromSeed(seed); const path = `${this.getDerivationPath()}/${internal ? 1 : 0}/${index}`; const child = root.derivePath(path); @@ -109,13 +110,13 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet { if (node === 0 && !this._node0) { const xpub = this.constructor._zpubToXpub(this.getXpub()); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node0 = hdNode.derive(node); } if (node === 1 && !this._node1) { const xpub = this.constructor._zpubToXpub(this.getXpub()); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node1 = hdNode.derive(node); } @@ -142,13 +143,13 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet { if (node === 0 && !this._node0) { const xpub = this.constructor._zpubToXpub(this.getXpub()); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node0 = hdNode.derive(node); } if (node === 1 && !this._node1) { const xpub = this.constructor._zpubToXpub(this.getXpub()); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node1 = hdNode.derive(node); } @@ -181,7 +182,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet { } // first, getting xpub const seed = this._getSeed(); - const root = HDNode.fromSeed(seed); + const root = bip32.fromSeed(seed); const path = this.getDerivationPath(); const child = root.derivePath(path).neutered(); @@ -1091,7 +1092,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet { */ cosignPsbt(psbt) { const seed = this._getSeed(); - const hdRoot = HDNode.fromSeed(seed); + const hdRoot = bip32.fromSeed(seed); for (let cc = 0; cc < psbt.inputCount; cc++) { try { @@ -1125,7 +1126,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet { * @returns {string} Hex string of fingerprint derived from mnemonics. Always has lenght of 8 chars and correct leading zeroes. All caps */ static seedToFingerprint(seed) { - const root = HDNode.fromSeed(seed); + const root = bip32.fromSeed(seed); let hex = root.fingerprint.toString('hex'); while (hex.length < 8) hex = '0' + hex; // leading zeroes return hex.toUpperCase(); diff --git a/class/wallets/hd-aezeed-wallet.js b/class/wallets/hd-aezeed-wallet.js index 794e4123b..5033872ba 100644 --- a/class/wallets/hd-aezeed-wallet.js +++ b/class/wallets/hd-aezeed-wallet.js @@ -1,8 +1,11 @@ import { AbstractHDElectrumWallet } from './abstract-hd-electrum-wallet'; import b58 from 'bs58check'; +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; + const bitcoin = require('bitcoinjs-lib'); const { CipherSeed } = require('aezeed'); -const bip32 = require('bip32'); +const bip32 = BIP32Factory(ecc); /** * AEZEED mnemonics support, which is used in LND diff --git a/class/wallets/hd-legacy-breadwallet-wallet.js b/class/wallets/hd-legacy-breadwallet-wallet.js index 1c0e0d731..39aa75d66 100644 --- a/class/wallets/hd-legacy-breadwallet-wallet.js +++ b/class/wallets/hd-legacy-breadwallet-wallet.js @@ -1,8 +1,11 @@ import * as bitcoinjs from 'bitcoinjs-lib'; import { HDLegacyP2PKHWallet } from './hd-legacy-p2pkh-wallet'; import { AbstractHDElectrumWallet } from './abstract-hd-electrum-wallet'; +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; + const BlueElectrum = require('../../blue_modules/BlueElectrum'); -const bip32 = require('bip32'); +const bip32 = BIP32Factory(ecc); /** * HD Wallet (BIP39). diff --git a/class/wallets/hd-legacy-electrum-seed-p2pkh-wallet.js b/class/wallets/hd-legacy-electrum-seed-p2pkh-wallet.js index 0ba6a0c41..0108de30d 100644 --- a/class/wallets/hd-legacy-electrum-seed-p2pkh-wallet.js +++ b/class/wallets/hd-legacy-electrum-seed-p2pkh-wallet.js @@ -1,8 +1,10 @@ import { HDLegacyP2PKHWallet } from './hd-legacy-p2pkh-wallet'; +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; const bitcoin = require('bitcoinjs-lib'); const mn = require('electrum-mnemonic'); -const HDNode = require('bip32'); +const bip32 = BIP32Factory(ecc); const PREFIX = mn.PREFIXES.standard; @@ -31,7 +33,7 @@ export class HDLegacyElectrumSeedP2PKHWallet extends HDLegacyP2PKHWallet { } const args = { prefix: PREFIX }; if (this.passphrase) args.passphrase = this.passphrase; - const root = HDNode.fromSeed(mn.mnemonicToSeedSync(this.secret, args)); + const root = bip32.fromSeed(mn.mnemonicToSeedSync(this.secret, args)); this._xpub = root.neutered().toBase58(); return this._xpub; } @@ -40,7 +42,7 @@ export class HDLegacyElectrumSeedP2PKHWallet extends HDLegacyP2PKHWallet { index = index * 1; // cast to int if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit - const node = HDNode.fromBase58(this.getXpub()); + const node = bip32.fromBase58(this.getXpub()); const address = bitcoin.payments.p2pkh({ pubkey: node.derive(1).derive(index).publicKey, }).address; @@ -52,7 +54,7 @@ export class HDLegacyElectrumSeedP2PKHWallet extends HDLegacyP2PKHWallet { index = index * 1; // cast to int if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit - const node = HDNode.fromBase58(this.getXpub()); + const node = bip32.fromBase58(this.getXpub()); const address = bitcoin.payments.p2pkh({ pubkey: node.derive(0).derive(index).publicKey, }).address; @@ -64,7 +66,7 @@ export class HDLegacyElectrumSeedP2PKHWallet extends HDLegacyP2PKHWallet { if (!this.secret) return false; const args = { prefix: PREFIX }; if (this.passphrase) args.passphrase = this.passphrase; - const root = HDNode.fromSeed(mn.mnemonicToSeedSync(this.secret, args)); + const root = bip32.fromSeed(mn.mnemonicToSeedSync(this.secret, args)); const path = `m/${internal ? 1 : 0}/${index}`; const child = root.derivePath(path); @@ -76,13 +78,13 @@ export class HDLegacyElectrumSeedP2PKHWallet extends HDLegacyP2PKHWallet { if (node === 0 && !this._node0) { const xpub = this.getXpub(); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node0 = hdNode.derive(node); } if (node === 1 && !this._node1) { const xpub = this.getXpub(); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node1 = hdNode.derive(node); } diff --git a/class/wallets/hd-legacy-p2pkh-wallet.js b/class/wallets/hd-legacy-p2pkh-wallet.js index a1746bdf4..c1a012d47 100644 --- a/class/wallets/hd-legacy-p2pkh-wallet.js +++ b/class/wallets/hd-legacy-p2pkh-wallet.js @@ -1,5 +1,7 @@ import { AbstractHDElectrumWallet } from './abstract-hd-electrum-wallet'; -const HDNode = require('bip32'); +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; +const bip32 = BIP32Factory(ecc); const BlueElectrum = require('../../blue_modules/BlueElectrum'); /** @@ -37,7 +39,7 @@ export class HDLegacyP2PKHWallet extends AbstractHDElectrumWallet { return this._xpub; // cache hit } const seed = this._getSeed(); - const root = HDNode.fromSeed(seed); + const root = bip32.fromSeed(seed); const path = this.getDerivationPath(); const child = root.derivePath(path).neutered(); @@ -58,13 +60,13 @@ export class HDLegacyP2PKHWallet extends AbstractHDElectrumWallet { if (node === 0 && !this._node0) { const xpub = this.getXpub(); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node0 = hdNode.derive(node); } if (node === 1 && !this._node1) { const xpub = this.getXpub(); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node1 = hdNode.derive(node); } diff --git a/class/wallets/hd-segwit-electrum-seed-p2wpkh-wallet.js b/class/wallets/hd-segwit-electrum-seed-p2wpkh-wallet.js index 6766a3146..6566ace64 100644 --- a/class/wallets/hd-segwit-electrum-seed-p2wpkh-wallet.js +++ b/class/wallets/hd-segwit-electrum-seed-p2wpkh-wallet.js @@ -1,9 +1,11 @@ import b58 from 'bs58check'; import { HDSegwitBech32Wallet } from './hd-segwit-bech32-wallet'; +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; const bitcoin = require('bitcoinjs-lib'); const mn = require('electrum-mnemonic'); -const HDNode = require('bip32'); +const bip32 = BIP32Factory(ecc); const PREFIX = mn.PREFIXES.segwit; @@ -32,7 +34,7 @@ export class HDSegwitElectrumSeedP2WPKHWallet extends HDSegwitBech32Wallet { } const args = { prefix: PREFIX }; if (this.passphrase) args.passphrase = this.passphrase; - const root = HDNode.fromSeed(mn.mnemonicToSeedSync(this.secret, args)); + const root = bip32.fromSeed(mn.mnemonicToSeedSync(this.secret, args)); const xpub = root.derivePath("m/0'").neutered().toBase58(); // bitcoinjs does not support zpub yet, so we just convert it from xpub @@ -49,7 +51,7 @@ export class HDSegwitElectrumSeedP2WPKHWallet extends HDSegwitBech32Wallet { if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit const xpub = this.constructor._zpubToXpub(this.getXpub()); - const node = HDNode.fromBase58(xpub); + const node = bip32.fromBase58(xpub); const address = bitcoin.payments.p2wpkh({ pubkey: node.derive(1).derive(index).publicKey, }).address; @@ -62,7 +64,7 @@ export class HDSegwitElectrumSeedP2WPKHWallet extends HDSegwitBech32Wallet { if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit const xpub = this.constructor._zpubToXpub(this.getXpub()); - const node = HDNode.fromBase58(xpub); + const node = bip32.fromBase58(xpub); const address = bitcoin.payments.p2wpkh({ pubkey: node.derive(0).derive(index).publicKey, }).address; @@ -74,7 +76,7 @@ export class HDSegwitElectrumSeedP2WPKHWallet extends HDSegwitBech32Wallet { if (!this.secret) return false; const args = { prefix: PREFIX }; if (this.passphrase) args.passphrase = this.passphrase; - const root = HDNode.fromSeed(mn.mnemonicToSeedSync(this.secret, args)); + const root = bip32.fromSeed(mn.mnemonicToSeedSync(this.secret, args)); const path = `m/0'/${internal ? 1 : 0}/${index}`; const child = root.derivePath(path); @@ -86,13 +88,13 @@ export class HDSegwitElectrumSeedP2WPKHWallet extends HDSegwitBech32Wallet { if (node === 0 && !this._node0) { const xpub = this.constructor._zpubToXpub(this.getXpub()); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node0 = hdNode.derive(node); } if (node === 1 && !this._node1) { const xpub = this.constructor._zpubToXpub(this.getXpub()); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node1 = hdNode.derive(node); } diff --git a/class/wallets/hd-segwit-p2sh-wallet.js b/class/wallets/hd-segwit-p2sh-wallet.js index 5df62ce25..d5fff5f16 100644 --- a/class/wallets/hd-segwit-p2sh-wallet.js +++ b/class/wallets/hd-segwit-p2sh-wallet.js @@ -1,7 +1,9 @@ import b58 from 'bs58check'; import { AbstractHDElectrumWallet } from './abstract-hd-electrum-wallet'; +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; +const bip32 = BIP32Factory(ecc); const bitcoin = require('bitcoinjs-lib'); -const HDNode = require('bip32'); /** * HD Wallet (BIP39). @@ -50,13 +52,13 @@ export class HDSegwitP2SHWallet extends AbstractHDElectrumWallet { if (node === 0 && !this._node0) { const xpub = this.constructor._ypubToXpub(this.getXpub()); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node0 = hdNode.derive(0); } if (node === 1 && !this._node1) { const xpub = this.constructor._ypubToXpub(this.getXpub()); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); this._node1 = hdNode.derive(1); } @@ -90,7 +92,7 @@ export class HDSegwitP2SHWallet extends AbstractHDElectrumWallet { } // first, getting xpub const seed = this._getSeed(); - const root = HDNode.fromSeed(seed); + const root = bip32.fromSeed(seed); const path = this.getDerivationPath(); const child = root.derivePath(path).neutered(); diff --git a/class/wallets/multisig-hd-wallet.js b/class/wallets/multisig-hd-wallet.js index 60f09e47a..563ff0249 100644 --- a/class/wallets/multisig-hd-wallet.js +++ b/class/wallets/multisig-hd-wallet.js @@ -3,10 +3,11 @@ import * as bip39 from 'bip39'; import b58 from 'bs58check'; import { decodeUR } from '../../blue_modules/ur'; import { ECPairFactory } from 'ecpair'; -const ecc = require('tiny-secp256k1'); +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; const ECPair = ECPairFactory(ecc); const BlueElectrum = require('../../blue_modules/BlueElectrum'); -const HDNode = require('bip32'); +const bip32 = BIP32Factory(ecc); const bitcoin = require('bitcoinjs-lib'); const createHash = require('create-hash'); const reverse = require('buffer-reverse'); @@ -134,7 +135,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { try { xpub = super._zpubToXpub(key); - HDNode.fromBase58(xpub); + bip32.fromBase58(xpub); return true; } catch (_) {} @@ -144,7 +145,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { static isXprvValid(xprv) { try { xprv = MultisigHDWallet.convertMultisigXprvToRegularXprv(xprv); - HDNode.fromBase58(xprv); + bip32.fromBase58(xprv); return true; } catch (_) { return false; @@ -209,7 +210,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { } static convertXprvToXpub(xprv) { - const restored = HDNode.fromBase58(MultisigHDWallet.convertMultisigXprvToRegularXprv(xprv)); + const restored = bip32.fromBase58(MultisigHDWallet.convertMultisigXprvToRegularXprv(xprv)); return restored.neutered().toBase58(); } @@ -250,7 +251,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { if (!this._nodes[nodeIndex][cosignerIndex]) { const xpub = this._getXpubFromCosigner(cosigner); - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); _node = hdNode.derive(nodeIndex); this._nodes[nodeIndex][cosignerIndex] = _node; } else { @@ -304,7 +305,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { seed = bip39.mnemonicToSeedSync(mnemonic); } - const root = HDNode.fromSeed(seed); + const root = bip32.fromSeed(seed); const child = root.derivePath(path).neutered(); return child.toBase58(); } @@ -646,7 +647,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { const masterFingerprint = Buffer.from(this._cosignersFingerprints[c], 'hex'); const xpub = this._getXpubFromCosigner(cosigner); - const hdNode0 = HDNode.fromBase58(xpub); + const hdNode0 = bip32.fromBase58(xpub); const splt = path.split('/'); const internal = +splt[splt.length - 2]; const index = +splt[splt.length - 1]; @@ -740,7 +741,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { const masterFingerprint = Buffer.from(this._cosignersFingerprints[c], 'hex'); const xpub = this._getXpubFromCosigner(cosigner); - const hdNode0 = HDNode.fromBase58(xpub); + const hdNode0 = bip32.fromBase58(xpub); const splt = path.split('/'); const internal = +splt[splt.length - 2]; const index = +splt[splt.length - 1]; @@ -855,7 +856,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { seed = MultisigHDWallet.convertElectrumMnemonicToSeed(cosigner); } - const hdRoot = HDNode.fromSeed(seed); + const hdRoot = bip32.fromSeed(seed); psbt.signInputHD(cc, hdRoot); signaturesMade++; } @@ -900,7 +901,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { } static isPathValid(path) { - const root = HDNode.fromSeed(Buffer.alloc(32)); + const root = bip32.fromSeed(Buffer.alloc(32)); try { root.derivePath(path); return true; @@ -999,7 +1000,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { if (!MultisigHDWallet.isXpubString(cosigner)) { // ok this is a mnemonic, lets try to sign const seed = bip39.mnemonicToSeedSync(cosigner); - const hdRoot = HDNode.fromSeed(seed); + const hdRoot = bip32.fromSeed(seed); try { psbt.signInputHD(cc, hdRoot); } catch (_) {} // protects agains duplicate cosignings @@ -1015,7 +1016,7 @@ export class MultisigHDWallet extends AbstractHDElectrumWallet { // match it to the one provided in PSBT's input, and if we have a match - we are in luck! we can sign // with this private key. const seed = bip39.mnemonicToSeedSync(cosigner); - const root = HDNode.fromSeed(seed); + const root = bip32.fromSeed(seed); const splt = derivation.path.split('/'); const internal = +splt[splt.length - 2]; const index = +splt[splt.length - 1]; diff --git a/class/wallets/watch-only-wallet.js b/class/wallets/watch-only-wallet.js index a55b06abb..5dfcea5bc 100644 --- a/class/wallets/watch-only-wallet.js +++ b/class/wallets/watch-only-wallet.js @@ -2,9 +2,11 @@ import { LegacyWallet } from './legacy-wallet'; import { HDSegwitP2SHWallet } from './hd-segwit-p2sh-wallet'; import { HDLegacyP2PKHWallet } from './hd-legacy-p2pkh-wallet'; import { HDSegwitBech32Wallet } from './hd-segwit-bech32-wallet'; +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; const bitcoin = require('bitcoinjs-lib'); -const HDNode = require('bip32'); +const bip32 = BIP32Factory(ecc); export class WatchOnlyWallet extends LegacyWallet { static type = 'watchOnly'; @@ -268,7 +270,7 @@ export class WatchOnlyWallet extends LegacyWallet { xpub = this.secret; } - const hdNode = HDNode.fromBase58(xpub); + const hdNode = bip32.fromBase58(xpub); hdNode.derive(0); return true; } catch (_) {} diff --git a/screen/selftest.js b/screen/selftest.js index 91a1d5f8d..57ea14c96 100644 --- a/screen/selftest.js +++ b/screen/selftest.js @@ -3,6 +3,8 @@ import PropTypes from 'prop-types'; import { ScrollView, View, StyleSheet } from 'react-native'; import wif from 'wif'; import bip38 from 'bip38'; +import BIP32Factory from 'bip32'; +import * as ecc from 'tiny-secp256k1'; import loc from '../loc'; import { BlueSpacing20, SafeBlueArea, BlueCard, BlueText, BlueLoading } from '../BlueComponents'; @@ -19,7 +21,7 @@ const bitcoin = require('bitcoinjs-lib'); const BlueCrypto = require('react-native-blue-crypto'); const encryption = require('../blue_modules/encryption'); const BlueElectrum = require('../blue_modules/BlueElectrum'); -const bip32 = require('bip32'); +const bip32 = BIP32Factory(ecc); const styles = StyleSheet.create({ center: {