mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-22 23:08:07 +01:00
Refactor: Cosmetic changes and some ❤️
This commit is contained in:
parent
96af02b6cd
commit
52ad5c14b7
3 changed files with 71 additions and 65 deletions
|
@ -1,7 +1,7 @@
|
|||
import { AbstractHDWallet } from './abstract-hd-wallet';
|
||||
import Frisbee from 'frisbee';
|
||||
const bitcoin = require('bitcoinjs-lib');
|
||||
const bip39 = require('bip39');
|
||||
import bitcoin from 'bitcoinjs-lib';
|
||||
import bip39 from 'bip39';
|
||||
|
||||
/**
|
||||
* HD Wallet (BIP39).
|
||||
|
@ -20,25 +20,26 @@ export class HDLegacyBreadwalletWallet extends AbstractHDWallet {
|
|||
if (this._xpub) {
|
||||
return this._xpub; // cache hit
|
||||
}
|
||||
let mnemonic = this.secret;
|
||||
let seed = bip39.mnemonicToSeed(mnemonic);
|
||||
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
const mnemonic = this.secret;
|
||||
const seed = bip39.mnemonicToSeed(mnemonic);
|
||||
const root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
|
||||
let path = "m/0'";
|
||||
let child = root.derivePath(path).neutered();
|
||||
const path = "m/0'";
|
||||
const child = root.derivePath(path).neutered();
|
||||
this._xpub = child.toBase58();
|
||||
|
||||
return this._xpub;
|
||||
}
|
||||
|
||||
_getExternalAddressByIndex(index) {
|
||||
index = index * 1; // cast to int
|
||||
if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit
|
||||
let mnemonic = this.secret;
|
||||
let seed = bip39.mnemonicToSeed(mnemonic);
|
||||
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
const mnemonic = this.secret;
|
||||
const seed = bip39.mnemonicToSeed(mnemonic);
|
||||
const root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
|
||||
let path = "m/0'/0/" + index;
|
||||
let child = root.derivePath(path);
|
||||
const path = "m/0'/0/" + index;
|
||||
const child = root.derivePath(path);
|
||||
|
||||
return (this.external_addresses_cache[index] = child.getAddress());
|
||||
}
|
||||
|
@ -46,12 +47,12 @@ export class HDLegacyBreadwalletWallet extends AbstractHDWallet {
|
|||
_getInternalAddressByIndex(index) {
|
||||
index = index * 1; // cast to int
|
||||
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
|
||||
let mnemonic = this.secret;
|
||||
let seed = bip39.mnemonicToSeed(mnemonic);
|
||||
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
const mnemonic = this.secret;
|
||||
const seed = bip39.mnemonicToSeed(mnemonic);
|
||||
const root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
|
||||
let path = "m/0'/1/" + index;
|
||||
let child = root.derivePath(path);
|
||||
const path = "m/0'/1/" + index;
|
||||
const child = root.derivePath(path);
|
||||
|
||||
return (this.internal_addresses_cache[index] = child.getAddress());
|
||||
}
|
||||
|
@ -72,11 +73,11 @@ export class HDLegacyBreadwalletWallet extends AbstractHDWallet {
|
|||
* @private
|
||||
*/
|
||||
_getWIFByIndex(internal, index) {
|
||||
let mnemonic = this.secret;
|
||||
let seed = bip39.mnemonicToSeed(mnemonic);
|
||||
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
let path = `m/0'/${internal ? 1 : 0}/${index}`;
|
||||
let child = root.derivePath(path);
|
||||
const mnemonic = this.secret;
|
||||
const seed = bip39.mnemonicToSeed(mnemonic);
|
||||
const root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
const path = `m/0'/${internal ? 1 : 0}/${index}`;
|
||||
const child = root.derivePath(path);
|
||||
|
||||
return child.keyPair.toWIF();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { AbstractHDWallet } from './abstract-hd-wallet';
|
||||
const bitcoin = require('bitcoinjs-lib');
|
||||
const bip39 = require('bip39');
|
||||
const BigNumber = require('bignumber.js');
|
||||
const signer = require('../models/signer');
|
||||
import bitcoin from 'bitcoinjs-lib';
|
||||
import bip39 from 'bip39';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import signer from '../models/signer';
|
||||
|
||||
/**
|
||||
* HD Wallet (BIP39).
|
||||
|
@ -21,13 +21,14 @@ export class HDLegacyP2PKHWallet extends AbstractHDWallet {
|
|||
if (this._xpub) {
|
||||
return this._xpub; // cache hit
|
||||
}
|
||||
let mnemonic = this.secret;
|
||||
let seed = bip39.mnemonicToSeed(mnemonic);
|
||||
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
const mnemonic = this.secret;
|
||||
const seed = bip39.mnemonicToSeed(mnemonic);
|
||||
const root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
|
||||
let path = "m/44'/0'/0'";
|
||||
let child = root.derivePath(path).neutered();
|
||||
const path = "m/44'/0'/0'";
|
||||
const child = root.derivePath(path).neutered();
|
||||
this._xpub = child.toBase58();
|
||||
|
||||
return this._xpub;
|
||||
}
|
||||
|
||||
|
@ -47,16 +48,16 @@ export class HDLegacyP2PKHWallet extends AbstractHDWallet {
|
|||
* @private
|
||||
*/
|
||||
_getWIFByIndex(internal, index) {
|
||||
let mnemonic = this.secret;
|
||||
let seed = bip39.mnemonicToSeed(mnemonic);
|
||||
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
let path = `m/44'/0'/0'/${internal ? 1 : 0}/${index}`;
|
||||
let child = root.derivePath(path);
|
||||
const mnemonic = this.secret;
|
||||
const seed = bip39.mnemonicToSeed(mnemonic);
|
||||
const root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
const path = `m/44'/0'/0'/${internal ? 1 : 0}/${index}`;
|
||||
const child = root.derivePath(path);
|
||||
|
||||
return child.keyPair.toWIF();
|
||||
}
|
||||
|
||||
_getExternalAddressByIndex (index) {
|
||||
_getExternalAddressByIndex(index) {
|
||||
index = index * 1; // cast to int
|
||||
if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit
|
||||
|
||||
|
@ -69,7 +70,7 @@ export class HDLegacyP2PKHWallet extends AbstractHDWallet {
|
|||
return (this.external_addresses_cache[index] = address);
|
||||
}
|
||||
|
||||
_getInternalAddressByIndex (index) {
|
||||
_getInternalAddressByIndex(index) {
|
||||
index = index * 1; // cast to int
|
||||
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
|
||||
|
||||
|
|
|
@ -1,22 +1,24 @@
|
|||
import { AbstractHDWallet } from './abstract-hd-wallet';
|
||||
import Frisbee from 'frisbee';
|
||||
import { NativeModules } from 'react-native';
|
||||
import bitcoin from 'bitcoinjs-lib';
|
||||
import bip39 from 'bip39';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import b58 from 'bs58check';
|
||||
import signer from '../models/signer';
|
||||
|
||||
const { RNRandomBytes } = NativeModules;
|
||||
const bitcoin = require('bitcoinjs-lib');
|
||||
const bip39 = require('bip39');
|
||||
const BigNumber = require('bignumber.js');
|
||||
const b58 = require('bs58check');
|
||||
const signer = require('../models/signer');
|
||||
|
||||
/**
|
||||
* Converts ypub to xpub
|
||||
* @param {String} ypub - wallet ypub
|
||||
* @returns {*}
|
||||
*/
|
||||
function ypubToXpub (ypub) {
|
||||
function ypubToXpub(ypub) {
|
||||
let data = b58.decode(ypub);
|
||||
data = data.slice(4);
|
||||
data = Buffer.concat([Buffer.from("0488b21e", "hex"), data]);
|
||||
data = Buffer.concat([Buffer.from('0488b21e', 'hex'), data]);
|
||||
|
||||
return b58.encode(data);
|
||||
}
|
||||
|
||||
|
@ -25,12 +27,13 @@ function ypubToXpub (ypub) {
|
|||
* @param hdNode
|
||||
* @returns {String}
|
||||
*/
|
||||
function nodeToP2shSegwitAddress (hdNode) {
|
||||
function nodeToP2shSegwitAddress(hdNode) {
|
||||
const pubkeyBuf = hdNode.keyPair.getPublicKeyBuffer();
|
||||
const hash = bitcoin.crypto.hash160(pubkeyBuf);
|
||||
const redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(hash);
|
||||
const hash2 = bitcoin.crypto.hash160(redeemScript);
|
||||
const scriptPubkey = bitcoin.script.scriptHash.output.encode(hash2);
|
||||
|
||||
return bitcoin.address.fromOutputScript(scriptPubkey);
|
||||
}
|
||||
|
||||
|
@ -86,33 +89,33 @@ export class HDSegwitP2SHWallet extends AbstractHDWallet {
|
|||
* @private
|
||||
*/
|
||||
_getWIFByIndex(internal, index) {
|
||||
let mnemonic = this.secret;
|
||||
let seed = bip39.mnemonicToSeed(mnemonic);
|
||||
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
let path = `m/49'/0'/0'/${internal ? 1 : 0}/${index}`;
|
||||
let child = root.derivePath(path);
|
||||
const mnemonic = this.secret;
|
||||
const seed = bip39.mnemonicToSeed(mnemonic);
|
||||
const root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
const path = `m/49'/0'/0'/${internal ? 1 : 0}/${index}`;
|
||||
const child = root.derivePath(path);
|
||||
|
||||
return child.keyPair.toWIF();
|
||||
}
|
||||
|
||||
_getExternalAddressByIndex (index) {
|
||||
_getExternalAddressByIndex(index) {
|
||||
index = index * 1; // cast to int
|
||||
if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit
|
||||
|
||||
let xpub = ypubToXpub(this.getXpub());
|
||||
let hdNode = bitcoin.HDNode.fromBase58(xpub);
|
||||
let address = nodeToP2shSegwitAddress(hdNode.derive(0).derive(index));
|
||||
const xpub = ypubToXpub(this.getXpub());
|
||||
const hdNode = bitcoin.HDNode.fromBase58(xpub);
|
||||
const address = nodeToP2shSegwitAddress(hdNode.derive(0).derive(index));
|
||||
|
||||
return (this.external_addresses_cache[index] = address);
|
||||
}
|
||||
|
||||
_getInternalAddressByIndex (index) {
|
||||
_getInternalAddressByIndex(index) {
|
||||
index = index * 1; // cast to int
|
||||
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
|
||||
|
||||
let xpub = ypubToXpub(this.getXpub());
|
||||
let hdNode = bitcoin.HDNode.fromBase58(xpub);
|
||||
let address = nodeToP2shSegwitAddress(hdNode.derive(1).derive(index));
|
||||
const xpub = ypubToXpub(this.getXpub());
|
||||
const hdNode = bitcoin.HDNode.fromBase58(xpub);
|
||||
const address = nodeToP2shSegwitAddress(hdNode.derive(1).derive(index));
|
||||
|
||||
return (this.internal_addresses_cache[index] = address);
|
||||
}
|
||||
|
@ -128,19 +131,20 @@ export class HDSegwitP2SHWallet extends AbstractHDWallet {
|
|||
return this._xpub; // cache hit
|
||||
}
|
||||
// first, getting xpub
|
||||
let mnemonic = this.secret;
|
||||
let seed = bip39.mnemonicToSeed(mnemonic);
|
||||
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
const mnemonic = this.secret;
|
||||
const seed = bip39.mnemonicToSeed(mnemonic);
|
||||
const root = bitcoin.HDNode.fromSeedBuffer(seed);
|
||||
|
||||
let path = "m/49'/0'/0'";
|
||||
let child = root.derivePath(path).neutered();
|
||||
let xpub = child.toBase58();
|
||||
const path = "m/49'/0'/0'";
|
||||
const child = root.derivePath(path).neutered();
|
||||
const xpub = child.toBase58();
|
||||
|
||||
// bitcoinjs does not support ypub yet, so we just convert it from xpub
|
||||
let data = b58.decode(xpub);
|
||||
data = data.slice(4);
|
||||
data = Buffer.concat([Buffer.from('049d7cb2', 'hex'), data]);
|
||||
this._xpub = b58.encode(data);
|
||||
|
||||
return this._xpub;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue