Skip to content

Commit

Permalink
BC BREAK: Wallet constructor signature, pass network instead of 'test…
Browse files Browse the repository at this point in the history
…net'
  • Loading branch information
Thomas Kerin committed Jul 6, 2017
1 parent bf72611 commit 6bf0962
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
30 changes: 16 additions & 14 deletions lib/api_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var regtest = {
* apiKey: 'API_KEY',
* apiSecret: 'API_SECRET',
* host: 'defaults to api.blocktrail.com',
* network: 'BTC|LTC',
* testnet: true|false
* network: 'BTC|TBTC|RBTC', // specific network
* testnet: true|false // assumes network == 'btc'
* }
* @constructor
*/
Expand Down Expand Up @@ -86,27 +86,29 @@ var APIClient = function(options) {
};

APIClient.prototype.initNetwork = function(options) {
this.testnet = options.testnet = options.testnet || false;

if (options.network) {
var upper = options.network.toUpperCase();
if (upper === 'RBTC') {
options.network = 'rBTC';
this.network = regtest;
this.testnet = false;
return;
} else if (upper === 'TBTC') {
options.network = 'tBTC';
this.network = bitcoin.networks.testnet;
this.testnet = true;
return;
}
}

if (options.testnet) {
options.network = 'tBTC';
this.network = bitcoin.networks.testnet;
this.testnet = true;
} else {
options.network = 'BTC';
this.network = bitcoin.networks.bitcoin;
this.testnet = false;
}
};

Expand Down Expand Up @@ -301,7 +303,7 @@ APIClient.prototype.mnemonicToPrivateKey = function(mnemonic, passphrase, cb) {
var deferred = q.defer();
deferred.promise.spreadNodeify(cb);

var network = self.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
var network = self.network;

deferred.resolve(q.fcall(function() {
return self.mnemonicToSeedHex(mnemonic, passphrase).then(function(seedHex) {
Expand Down Expand Up @@ -337,7 +339,7 @@ APIClient.prototype.resolvePrimaryPrivateKeyFromOptions = function(options, cb)
var deferred = q.defer();
deferred.promise.nodeify(cb);

var network = self.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
var network = self.network;

try {
// avoid conflicting options
Expand Down Expand Up @@ -398,7 +400,7 @@ APIClient.prototype.resolveBackupPublicKeyFromOptions = function(options, cb) {
var deferred = q.defer();
deferred.promise.nodeify(cb);

var network = self.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
var network = self.network;

try {
// avoid conflicting options
Expand Down Expand Up @@ -898,7 +900,7 @@ APIClient.prototype.initWallet = function(options, cb) {
var deferred = q.defer();
deferred.promise.spreadNodeify(cb);

var network = self.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
var network = self.network;

var identifier = options.identifier;

Expand Down Expand Up @@ -932,7 +934,7 @@ APIClient.prototype.initWallet = function(options, cb) {
backupPublicKey,
blocktrailPublicKeys,
keyIndex,
self.testnet,
self.network,
result.checksum,
result.upgrade_key_index,
options.bypassNewAddressCheck
Expand Down Expand Up @@ -1114,7 +1116,7 @@ APIClient.prototype._createNewWalletV1 = function(options) {
options.backupPublicKey,
blocktrailPublicKeys,
keyIndex,
self.testnet,
self.network,
checksum,
result.upgrade_key_index,
options.bypassNewAddressCheck
Expand Down Expand Up @@ -1163,7 +1165,7 @@ APIClient.prototype._createNewWalletV2 = function(options) {
// avoid modifying passed options
options = _.merge({}, options);

var network = self.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
var network = self.network;

determineDataStorageV2_3(options)
.then(function(options) {
Expand Down Expand Up @@ -1222,7 +1224,7 @@ APIClient.prototype._createNewWalletV2 = function(options) {
options.backupPublicKey,
blocktrailPublicKeys,
keyIndex,
self.testnet,
self.network,
checksum,
result.upgrade_key_index,
options.bypassNewAddressCheck
Expand Down Expand Up @@ -1270,7 +1272,7 @@ APIClient.prototype._createNewWalletV3 = function(options) {
// avoid modifying passed options
options = _.merge({}, options);

var network = self.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
var network = self.network;

determineDataStorageV2_3(options)
.then(function(options) {
Expand Down Expand Up @@ -1331,7 +1333,7 @@ APIClient.prototype._createNewWalletV3 = function(options) {
options.backupPublicKey,
blocktrailPublicKeys,
keyIndex,
self.testnet,
self.network,
checksum,
result.upgrade_key_index,
options.bypassNewAddressCheck
Expand Down
3 changes: 3 additions & 0 deletions lib/services/blocktrail_bitcoin_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ BlocktrailBitcoinService.prototype.normaliseNetwork = function(network, testnet
case 'tbtc':
case 'bitcoin-testnet':
return {network: "BTC", testnet: true};
case 'rbtc':
case 'bitcoin-regtest':
return {network: "rBTC", testnet: false};
default:
throw new Error("Unknown network " + network);
}
Expand Down
11 changes: 3 additions & 8 deletions lib/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var bip39 = require('bip39');
* @param backupPublicKey string BIP32 master pubKey M/
* @param blocktrailPublicKeys array list of blocktrail pubKeys indexed by keyIndex
* @param keyIndex int key index to use
* @param testnet bool testnet
* @param network object network
* @param checksum string
* @param upgradeToKeyIndex int
* @param bypassNewAddressCheck bool flag to indicate if wallet should/shouldn't derive new address locally to verify api
Expand All @@ -39,7 +39,7 @@ var Wallet = function(
backupPublicKey,
blocktrailPublicKeys,
keyIndex,
testnet,
network,
checksum,
upgradeToKeyIndex,
bypassNewAddressCheck
Expand All @@ -53,12 +53,7 @@ var Wallet = function(
self.locked = true;
self.bypassNewAddressCheck = !!bypassNewAddressCheck;

self.testnet = testnet;
if (self.testnet) {
self.network = bitcoin.networks.testnet;
} else {
self.network = bitcoin.networks.bitcoin;
}
self.network = network;

assert(backupPublicKey instanceof bitcoin.HDNode);
assert(_.every(primaryPublicKeys, function(primaryPublicKey) { return primaryPublicKey instanceof bitcoin.HDNode; }));
Expand Down
4 changes: 2 additions & 2 deletions test/wallet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var TRANSACTION_TEST_WALLET_PRIMARY_MNEMONIC = "give pause forget seed dance cra

var _createTestWallet = function(identifier, passphrase, primaryMnemonic, backupMnemonic, cb) {
var keyIndex = 9999;
var network = client.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
var network = client.network;

var primarySeed = bip39.mnemonicToSeed(primaryMnemonic, passphrase);
var primaryPrivateKey = bitcoin.HDNode.fromSeedBuffer(primarySeed, network);
Expand Down Expand Up @@ -62,7 +62,7 @@ var _createTestWallet = function(identifier, passphrase, primaryMnemonic, backup
backupPublicKey,
blocktrailPublicKeys,
keyIndex,
client.testnet,
client.network,
checksum
);

Expand Down

0 comments on commit 6bf0962

Please sign in to comment.