Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #448 from blockchain/v3.39-release
Browse files Browse the repository at this point in the history
v3.39 release
  • Loading branch information
plondon authored Nov 7, 2017
2 parents eda1257 + 890e492 commit e3cf536
Show file tree
Hide file tree
Showing 28 changed files with 552 additions and 157 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blockchain-wallet-client",
"version": "3.38.5",
"version": "3.39.7",
"description": "Blockchain.info JavaScript Wallet",
"homepage": "https://github.com/blockchain/my-wallet-v3",
"bugs": {
Expand Down Expand Up @@ -47,9 +47,8 @@
"bigi": "1.4.*",
"bignumber.js": "^4.0.2",
"bip39": "2.1.*",
"bitcoin-coinify-client": "^0.6.9",
"bitcoin-exchange-client": "^0.4.6",
"bitcoin-sfox-client": "^0.1.11",
"bitcoin-coinify-client": "^0.7.3",
"bitcoin-sfox-client": "^0.2.1",
"bitcoin-unocoin-client": "^0.3.4",
"bitcoincashjs-lib": "https://github.com/bitcoinjs/bitcoinjs-lib#9ac221c80dbc3462d7a2392cec2045ae2b590461",
"bitcoinjs-lib": "2.1.*",
Expand Down
5 changes: 5 additions & 0 deletions src/bch/bch-account.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable semi */
const BchSpendable = require('./bch-spendable')
const BchShiftPayment = require('../shift/bch-payment');

const ACCOUNT_LABEL_PREFIX = 'Bitcoin Cash - '

Expand Down Expand Up @@ -50,6 +51,10 @@ class BchAccount extends BchSpendable {
createPayment () {
return super.createPayment().from(this.index, this.changeAddress)
}

createShiftPayment (wallet) {
return BchShiftPayment.fromWallet(wallet, this)
}
}

module.exports = BchAccount
12 changes: 9 additions & 3 deletions src/bch/bch-api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable semi */
const { curry, is, prop, lensProp, compose, assoc, over, map } = require('ramda');
const { curry, is, prop, lensProp, assoc, over, map } = require('ramda');
const { mapped } = require('ramda-lens');
const API = require('../api');
const Coin = require('./coin.js');
const Coin = require('../coin');
const Bitcoin = require('bitcoincashjs-lib');
const constants = require('../constants');
const Helpers = require('../helpers');
Expand Down Expand Up @@ -50,14 +50,19 @@ const multiaddr = (addresses, n = 1) => {
}).then(r => r.status === 200 ? r.json() : r.json().then(e => Promise.reject(e)));
};

const addIndexToOutput = curry((hdwallet, output) => {
let addIndex = (xpub) => assoc('index', hdwallet.account(xpub.m).index, xpub)
return over(lensProp('xpub'), addIndex, output)
})

// source can be a list of legacy addresses or a single integer for account index
const getUnspents = curry((wallet, source) => {
switch (true) {
case is(Number, source):
const accIdx = wallet.hdwallet.accounts[source].extendedPublicKey
return apiGetUnspents([accIdx])
.then(prop('unspent_outputs'))
.then(over(compose(mapped, lensProp('xpub')), assoc('index', source)))
.then(map(addIndexToOutput(wallet.hdwallet)))
.then(map(Coin.fromJS));
case is(Array, source):
return apiGetUnspents(source)
Expand All @@ -70,6 +75,7 @@ const getUnspents = curry((wallet, source) => {
})

module.exports = {
addIndexToOutput,
getUnspents,
pushTx,
multiaddr
Expand Down
2 changes: 1 addition & 1 deletion src/bch/bch-imported.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const sumNonNull = compose(reduce(add, 0), filter(x => x != null))

class BchImported extends BchSpendable {
get addresses () {
return this._wallet.spendableActiveAddresses
return this._wallet.spendableAddresses
}

get label () {
Expand Down
8 changes: 4 additions & 4 deletions src/bch/bch-payment.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable semi */
const { compose, clone, assoc, is, all } = require('ramda')
const Coin = require('./coin')
const Coin = require('../coin')
const BchApi = require('./bch-api')
const { isBitcoinAddress, isPositiveInteger } = require('../helpers')
const { selectAll, descentDraw } = require('./coin-selection')
const { sign } = require('./signer')
const { selectAll, descentDraw } = require('../coin-selection')
const signer = require('../signer')

const isValidFrom = (from) => (
is(Number, from) ||
Expand Down Expand Up @@ -123,7 +123,7 @@ class BchPayment {
if (payment.selection == null) {
throw new PaymentError('cannot sign an unbuilt transaction', payment)
}
let tx = sign(secPass, this._wallet, payment.selection)
let tx = signer.signBitcoinCash(secPass, this._wallet, payment.selection)
let setData = compose(assoc('hash', tx.getId()), assoc('rawTx', tx.toHex()))
return setData(payment)
})
Expand Down
2 changes: 1 addition & 1 deletion src/bch/bch-spendable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable semi */
const BchApi = require('./bch-api')
const { selectAll } = require('./coin-selection')
const { selectAll } = require('../coin-selection')

class BchSpendable {
constructor (bchWallet, wallet) {
Expand Down
76 changes: 0 additions & 76 deletions src/bch/signer.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/blockchain-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ Object.defineProperties(Wallet.prototype, {
configurable: false,
get: function () { return this.activeKeys.map(function (k) { return k.address; }); }
},
'spendableAddresses': {
configurable: false,
get: function () {
return this.keys
.filter(function (k) { return !k.isWatchOnly; })
.map(function (k) { return k.address; });
}
},
'spendableActiveAddresses': {
configurable: false,
get: function () {
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions src/eth/eth-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const EthTxBuilder = require('./eth-tx-builder');
const EthWalletTx = require('./eth-wallet-tx');
const API = require('../api');
const { toBigNumber, toWei, fromWei } = require('../helpers');
const EthShiftPayment = require('../shift/eth-payment');

class EthAccount {
constructor (obj) {
Expand All @@ -22,6 +23,10 @@ class EthAccount {
return this._addr;
}

get receiveAddress () {
return this.address;
}

get privateKey () {
return this._priv;
}
Expand Down Expand Up @@ -139,6 +144,10 @@ class EthAccount {
});
}

createShiftPayment (wallet) {
return EthShiftPayment.fromWallet(wallet, this);
}

static privateKeyToAddress (privateKey) {
return ethUtil.toChecksumAddress(ethUtil.privateToAddress(privateKey).toString('hex'));
}
Expand Down
5 changes: 5 additions & 0 deletions src/hd-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var MyWallet = require('./wallet'); // This cyclic import should be avoided once
var API = require('./api');
var Transaction = require('./transaction');
var constants = require('./constants');
var BtcShiftPayment = require('./shift/btc-payment');

// HDAccount Class

Expand Down Expand Up @@ -318,3 +319,7 @@ HDAccount.prototype.getAvailableBalance = function (feeType) {
return { amount, fee: fees[feeType] };
});
};

HDAccount.prototype.createShiftPayment = function (wallet) {
return BtcShiftPayment.fromWallet(wallet, this);
};
25 changes: 25 additions & 0 deletions src/hd-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ module.exports = HDWallet;

var Bitcoin = require('bitcoinjs-lib');
var assert = require('assert');
var R = require('ramda');
var Helpers = require('./helpers');
var HDAccount = require('./hd-account');
var BIP39 = require('bip39');
var MyWallet = require('./wallet'); // This cyclic import should be avoided once the refactor is complete
var API = require('./api');
var { addIndexToOutput } = require('./bch/bch-api');
var { selectAll } = require('./coin-selection');
var signer = require('./signer');
var Coin = require('./coin');
var constants = require('./constants');

function HDWallet (object) {
Expand Down Expand Up @@ -280,3 +286,22 @@ HDWallet.prototype.persist = function () {
HDWallet.prototype.isValidAccountIndex = function (index) {
return Helpers.isPositiveInteger(index) && index < this._accounts.length;
};

HDWallet.prototype.createConsolidationPayment = function (toAccount, feePerByte, secPass) {
let outputToCoin = R.compose(Coin.fromJS, addIndexToOutput(this));

let createPayment = (selection, tx) => ({
get fee () { return selection.fee; },
publish () { return API.pushTx(tx.toHex()).then(() => ({ hash: tx.getId() })); }
});

let paymentFromCoins = (coins) => {
let selection = selectAll(feePerByte, coins, toAccount.receiveAddress);
let tx = signer.signBitcoin(secPass, MyWallet.wallet, selection);
return createPayment(selection, tx);
};

return API.getUnspent(this.xpubs)
.then(result => R.map(outputToCoin, result.unspent_outputs))
.then(paymentFromCoins);
};
48 changes: 10 additions & 38 deletions src/shift/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ const { delay, asyncOnce, trace } = require('../helpers')
const Api = require('./api')
const Trade = require('./trade')
const Quote = require('./quote')
const BtcPayment = require('./btc-payment')
const EthPayment = require('./eth-payment')
const BchPayment = require('./bch-payment')

const METADATA_TYPE_SHAPE_SHIFT = 6;

Expand All @@ -31,47 +28,35 @@ class ShapeShift {
return this._api.getRate(coinPair)
}

getQuote (coinPair, amount) {
getQuote (from, to, amount) {
trace('getting quote')
let [from, to] = coinPair.split('_')

let withdrawalAddress = this.nextAddressForCurrency(to)
let returnAddress = this.nextAddressForCurrency(from)
let returnAddress = from.receiveAddress;
let withdrawalAddress = to.receiveAddress;
let coinPair = from.coinCode + '_' + to.coinCode;

return this._api.getQuote(coinPair, amount, withdrawalAddress, returnAddress)
.then(Quote.fromApiResponse)
}

getApproximateQuote (coinPair, amount) {
getApproximateQuote (from, to, amount) {
trace('getting approximate quote')

let coinPair = from.coinCode + '_' + to.coinCode;

return this._api.getQuote(coinPair, amount)
.then(Quote.fromApiResponse)
}

buildPayment (quote, fee, fromAccount) {
trace('building payment')
let payment
if (quote.depositAddress == null) {
throw new Error('Quote is missing deposit address')
}
if (fromAccount != null && fromAccount.coinCode !== quote.fromCurrency) {
if (fromAccount.coinCode !== quote.fromCurrency) {
throw new Error('Sending account currency does not match quote deposit currency')
}
if (quote.fromCurrency === 'btc') {
let account = fromAccount || this._wallet.hdwallet.defaultAccount
payment = BtcPayment.fromWallet(this._wallet, account)
}
if (quote.fromCurrency === 'eth') {
let account = fromAccount || this._wallet.eth.defaultAccount
payment = EthPayment.fromWallet(this._wallet, account)
}
if (quote.fromCurrency === 'bch') {
let account = fromAccount || this._wallet.bch.defaultAccount
payment = BchPayment.fromWallet(this._wallet, account)
}
if (payment == null) {
throw new Error(`Tried to build for unsupported currency ${quote.fromCurrency}`)
}
let payment = fromAccount.createShiftPayment(this._wallet)
return payment.setFromQuote(quote, fee)
}

Expand Down Expand Up @@ -121,19 +106,6 @@ class ShapeShift {
return Promise.all(requests);
}

nextAddressForCurrency (currency) {
if (currency === 'btc') {
return this._wallet.hdwallet.defaultAccount.receiveAddress
}
if (currency === 'eth') {
return this._wallet.eth.defaultAccount.address
}
if (currency === 'bch') {
return this._wallet.bch.defaultAccount.receiveAddress
}
throw new Error(`Currency '${currency}' is not supported`)
}

saveBtcWithdrawalLabel (quote) {
let label = `ShapeShift order #${quote.orderId}`
let account = this._wallet.hdwallet.defaultAccount
Expand Down
Loading

0 comments on commit e3cf536

Please sign in to comment.