From 2dede909c36fa4f611bffe530c650d119ccdd2a5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 1 Apr 2016 19:02:08 -0700 Subject: [PATCH] hd. bcoin-cli. --- bin/bcoin-cli | 107 +++++++++++++++++++++++++---------------- lib/bcoin/hd.js | 125 ++---------------------------------------------- 2 files changed, 71 insertions(+), 161 deletions(-) diff --git a/bin/bcoin-cli b/bin/bcoin-cli index 173e0074..fd1b0958 100755 --- a/bin/bcoin-cli +++ b/bin/bcoin-cli @@ -14,10 +14,44 @@ var client = new Client(argv.url || 'localhost:8080', { setNetwork: argv.network == null }); +function getID() { + if (process.env.BCOIN_WALLET_ID) + return process.env.BCOIN_WALLET_ID; + if (argv.id) + return argv.id; + return argv.args.shift(); +} + function createWallet(callback) { - var options = utils.merge({ - id: argv.args[1] - }, argv); + var options = { id: getID() }; + + if (argv.master) + options.master = argv.master; + + if (argv.keys) + options.keys = argv.keys.split(/[,:]/); + + if (argv.lookahead) + options.lookahead = argv.lookahead >>> 0; + + if (argv.derivation) + options.derivation = argv.derivation; + + if (argv.m) + options.m = argv.m >>> 0; + + if (argv.n) + options.n = argv.n >>> 0; + + if (argv.copay != null) + options.copayBIP45 = !!argv.copay; + + if (argv.witness != null) + options.witness = !!argv.witness; + + if (argv.passphrase) + options.passphrase = argv.passphrase; + client.createWallet(options, function(err, wallet) { if (err) return callback(err); @@ -28,9 +62,7 @@ function createWallet(callback) { } function getWallet(callback) { - var options = utils.merge({ - id: argv.args[1] - }, argv); + var options = { id: getID(), passphrase: argv.args[0] }; client.getWallet(options.id, options.passphrase, function(err, wallet) { if (err) return callback(err); @@ -41,7 +73,7 @@ function getWallet(callback) { } function getTX(callback) { - var hash = argv.args[1]; + var hash = argv.args[0]; if (bcoin.address.validate(hash)) { return client.getTXByAddress(hash, function(err, txs) { if (err) @@ -60,7 +92,7 @@ function getTX(callback) { } function getBlock(callback) { - var hash = argv.args[1]; + var hash = argv.args[0]; hash = utils.revHex(hash); client.getBlock(hash, function(err, block) { if (err) @@ -71,8 +103,8 @@ function getBlock(callback) { } function getCoin(callback) { - var hash = argv.args[1]; - var index = argv.args[2]; + var hash = argv.args[0]; + var index = argv.args[1]; if (bcoin.address.validate(hash)) { return client.getCoinsByAddress(hash, function(err, coins) { if (err) @@ -91,7 +123,7 @@ function getCoin(callback) { } function getWalletAll(callback) { - var id = argv.args[1]; + var id = getID(); client.getWalletAll(id, function(err, txs) { if (err) return callback(err); @@ -101,7 +133,7 @@ function getWalletAll(callback) { } function getBalance(callback) { - var id = argv.args[1]; + var id = getID(); client.getWalletBalance(id, function(err, balance) { if (err) return callback(err); @@ -121,13 +153,15 @@ function getMempool(callback) { } function send(callback) { - var id = argv.args[1]; - var address = argv.args[2]; - var value = utils.satoshi(argv.args[3]); - var options = { - address: address, - value: value - }; + var id = getID(); + var options = {}; + if (argv.script) { + options.script = new bcoin.script(new Buffer(argv.script, 'hex')); + options.value = utils.satoshi(argv.value || argv.args[0]); + } else { + options.address = argv.address || argv.args[0]; + options.value = utils.satoshi(argv.value || argv.args[1]); + } client.walletSend(id, options, function(err, tx) { if (err) return callback(err); @@ -137,9 +171,9 @@ function send(callback) { } function zap(callback) { - var id = argv.args[1]; - var now = utils.now(); - var age = 72 * 60 * 60; + var id = getID(); + var now = (argv.now >>> 0) || utils.now(); + var age = (argv.age >>> 0) || 72 * 60 * 60; client.walletZap(id, now, age, function(err) { if (err) return callback(err); @@ -149,7 +183,7 @@ function zap(callback) { } function main(callback) { - switch (argv.args[0]) { + switch (argv.args.shift()) { case 'wallet': return createWallet(callback); case 'getwallet': @@ -177,7 +211,7 @@ function main(callback) { function parseArg(argv) { var args = []; var options = {}; - var arg; + var arg, negate; argv = argv.slice(); @@ -211,23 +245,14 @@ function parseArg(argv) { while (argv.length) { arg = getarg(); - switch (arg) { - case '--url': - options.url = argv.shift(); - break; - case '-h': - case '--help': - return process.exit(1); - default: - if (arg.indexOf('--') === 0) { - opt = arg.replace(/^--(no-)?/, ''); - options[opt] = !argv[0] || argv[0][0] === '-' - ? true - : argv.shift(); - } else { - args.push(arg); - } - break; + if (arg.indexOf('--') === 0) { + negate = arg.indexOf('--no-') === 0; + opt = arg.replace(/^--(no-)?/, ''); + options[opt] = !argv[0] || argv[0][0] === '-' + ? (negate ? false : true) + : argv.shift(); + } else { + args.push(arg); } } diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index f30b1c29..6fcad203 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -230,48 +230,6 @@ HDPrivateKey.prototype.deriveAccount44 = function deriveAccount44(options) { return child; }; -HDPrivateKey.prototype.deriveBIP44 = function deriveBIP44(options) { - var chain = options.chain; - var addressIndex = options.addressIndex; - - if (chain == null) - chain = options.change ? 1 : 0; - - assert(utils.isFinite(chain)); - assert(utils.isFinite(addressIndex)); - - return this - .deriveAccount44(options) - .derive(chain) - .derive(addressIndex); -}; - -HDPrivateKey.prototype.deriveChange = function deriveChange(accountIndex, addressIndex) { - if (this instanceof HDPublicKey) { - addressIndex = accountIndex; - accountIndex = null; - } - - return this.deriveBIP44({ - accountIndex: accountIndex, - chain: 1, - addressIndex: addressIndex - }); -}; - -HDPrivateKey.prototype.deriveAddress = function deriveAddress(accountIndex, addressIndex) { - if (this instanceof HDPublicKey) { - addressIndex = accountIndex; - accountIndex = null; - } - - return this.deriveBIP44({ - accountIndex: accountIndex, - chain: 0, - addressIndex: addressIndex - }); -}; - HDPrivateKey.prototype.derivePurpose45 = function derivePurpose45() { var child; @@ -287,41 +245,6 @@ HDPrivateKey.prototype.derivePurpose45 = function derivePurpose45() { return child; }; -HDPrivateKey.prototype.deriveBIP45 = function deriveBIP45(options) { - var cosignerIndex = options.cosignerIndex; - var chain = options.chain; - var addressIndex = options.addressIndex; - - if (chain == null) - chain = options.change ? 1 : 0; - - assert(utils.isFinite(cosignerIndex)); - assert(utils.isFinite(chain)); - assert(utils.isFinite(addressIndex)); - - return this - .derivePurpose45(options) - .derive(cosignerIndex) - .derive(chain) - .derive(addressIndex); -}; - -HDPrivateKey.prototype.deriveCosignerChange = function deriveCosignerChange(cosignerIndex, addressIndex) { - return this.deriveBIP45({ - cosignerIndex: cosignerIndex, - chain: 1, - addressIndex: addressIndex - }); -}; - -HDPrivateKey.prototype.deriveCosignerAddress = function deriveCosignerAddress(cosignerIndex, addressIndex) { - return this.deriveBIP45({ - cosignerIndex: cosignerIndex, - chain: 0, - addressIndex: addressIndex - }); -}; - HDPrivateKey.prototype.isPurpose45 = function isPurpose45() { if (utils.readU8(this.depth) !== 1) return false; @@ -334,34 +257,6 @@ HDPrivateKey.prototype.isAccount44 = function isAccount44() { return utils.readU8(this.depth) === 3; }; -HDPrivateKey.getPath = function getPath(options) { - var purpose, coinType, accountIndex, chain, addressIndex; - - if (!options) - options = {}; - - purpose = options.purpose; - coinType = options.coinType; - accountIndex = options.accountIndex; - chain = options.chain; - addressIndex = options.addressIndex; - - if (purpose == null) - purpose = 44; - - if (coinType == null) - coinType = network[this.network].type === 'main' ? 0 : 1; - - if (chain == null) - chain = options.change ? 1 : 0; - - return 'm/' + purpose + '\'/' - + coinType + '\'/' - + accountIndex + '\'/' - + chain + '/' - + addressIndex; -}; - HDPrivateKey.isExtended = function isExtended(data) { if (typeof data !== 'string') return false; @@ -384,7 +279,7 @@ HDPrivateKey.prototype._normalize = function _normalize(data) { if (typeof data.depth === 'number') data.depth = new Buffer([data.depth]); - if (utils.readU8(data.depth) > 0xff) + if (data.depth.length > 1) throw new Error('Depth is too high'); // parent finger print = uint_32be @@ -423,7 +318,7 @@ HDPrivateKey.prototype._seed = function _seed(seed) { hash = utils.sha512hmac(seed, 'Bitcoin seed'); return { - version: network[this.network].prefixes.xprivkey, + version: array32(network[this.network].prefixes.xprivkey), depth: new Buffer([0]), parentFingerPrint: new Buffer([0, 0, 0, 0]), childIndex: new Buffer([0, 0, 0, 0]), @@ -446,7 +341,7 @@ HDPrivateKey._generate = function _generate(privateKey, entropy) { entropy = ec.random(32); return { - version: null, + version: array32(network.prefixes.xprivkey), depth: new Buffer([0]), parentFingerPrint: new Buffer([0, 0, 0, 0]), childIndex: new Buffer([0, 0, 0, 0]), @@ -462,7 +357,7 @@ HDPrivateKey.generate = function generate(privateKey, entropy) { HDPrivateKey.prototype._generate = function _generate(privateKey, entropy) { var data = HDPrivateKey._generate(privateKey, entropy); - data.version = network[this.network].prefixes.xprivkey; + data.version = array32(network[this.network].prefixes.xprivkey); return data; }; @@ -545,7 +440,7 @@ HDPrivateKey.prototype.__defineGetter__('hdPublicKey', function() { this._hdPublicKey = new HDPublicKey({ network: this.network, data: { - version: network[this.network].prefixes.xpubkey, + version: array32(network[this.network].prefixes.xpubkey), depth: this.depth, parentFingerPrint: this.parentFingerPrint, childIndex: this.childIndex, @@ -827,18 +722,8 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) { return obj && obj.isPublic && typeof obj._unbuild === 'function'; }; -HDPublicKey.prototype.scan44 = HDPrivateKey.prototype.scan44; HDPublicKey.prototype.deriveAccount44 = HDPrivateKey.prototype.deriveAccount44; -HDPublicKey.prototype.deriveBIP44 = HDPrivateKey.prototype.deriveBIP44; -HDPublicKey.prototype.deriveChange = HDPrivateKey.prototype.deriveChange; -HDPublicKey.prototype.deriveAddress = HDPrivateKey.prototype.deriveAddress; - -HDPublicKey.prototype.scan45 = HDPrivateKey.prototype.scan45; HDPublicKey.prototype.derivePurpose45 = HDPrivateKey.prototype.derivePurpose45; -HDPublicKey.prototype.deriveBIP45 = HDPrivateKey.prototype.deriveBIP45; -HDPublicKey.prototype.deriveCosignerChange = HDPrivateKey.prototype.deriveCosignerChange; -HDPublicKey.prototype.deriveCosignerAddress = HDPrivateKey.prototype.deriveCosignerAddress; - HDPublicKey.prototype.isPurpose45 = HDPrivateKey.prototype.isPurpose45; HDPublicKey.prototype.isAccount44 = HDPrivateKey.prototype.isAccount44; HDPublicKey.prototype.toJSON = HDPrivateKey.prototype.toJSON;