hd. bcoin-cli.
This commit is contained in:
parent
8f4bed174d
commit
2dede909c3
107
bin/bcoin-cli
107
bin/bcoin-cli
@ -14,10 +14,44 @@ var client = new Client(argv.url || 'localhost:8080', {
|
|||||||
setNetwork: argv.network == null
|
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) {
|
function createWallet(callback) {
|
||||||
var options = utils.merge({
|
var options = { id: getID() };
|
||||||
id: argv.args[1]
|
|
||||||
}, argv);
|
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) {
|
client.createWallet(options, function(err, wallet) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -28,9 +62,7 @@ function createWallet(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getWallet(callback) {
|
function getWallet(callback) {
|
||||||
var options = utils.merge({
|
var options = { id: getID(), passphrase: argv.args[0] };
|
||||||
id: argv.args[1]
|
|
||||||
}, argv);
|
|
||||||
client.getWallet(options.id, options.passphrase, function(err, wallet) {
|
client.getWallet(options.id, options.passphrase, function(err, wallet) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -41,7 +73,7 @@ function getWallet(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getTX(callback) {
|
function getTX(callback) {
|
||||||
var hash = argv.args[1];
|
var hash = argv.args[0];
|
||||||
if (bcoin.address.validate(hash)) {
|
if (bcoin.address.validate(hash)) {
|
||||||
return client.getTXByAddress(hash, function(err, txs) {
|
return client.getTXByAddress(hash, function(err, txs) {
|
||||||
if (err)
|
if (err)
|
||||||
@ -60,7 +92,7 @@ function getTX(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getBlock(callback) {
|
function getBlock(callback) {
|
||||||
var hash = argv.args[1];
|
var hash = argv.args[0];
|
||||||
hash = utils.revHex(hash);
|
hash = utils.revHex(hash);
|
||||||
client.getBlock(hash, function(err, block) {
|
client.getBlock(hash, function(err, block) {
|
||||||
if (err)
|
if (err)
|
||||||
@ -71,8 +103,8 @@ function getBlock(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getCoin(callback) {
|
function getCoin(callback) {
|
||||||
var hash = argv.args[1];
|
var hash = argv.args[0];
|
||||||
var index = argv.args[2];
|
var index = argv.args[1];
|
||||||
if (bcoin.address.validate(hash)) {
|
if (bcoin.address.validate(hash)) {
|
||||||
return client.getCoinsByAddress(hash, function(err, coins) {
|
return client.getCoinsByAddress(hash, function(err, coins) {
|
||||||
if (err)
|
if (err)
|
||||||
@ -91,7 +123,7 @@ function getCoin(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getWalletAll(callback) {
|
function getWalletAll(callback) {
|
||||||
var id = argv.args[1];
|
var id = getID();
|
||||||
client.getWalletAll(id, function(err, txs) {
|
client.getWalletAll(id, function(err, txs) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -101,7 +133,7 @@ function getWalletAll(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getBalance(callback) {
|
function getBalance(callback) {
|
||||||
var id = argv.args[1];
|
var id = getID();
|
||||||
client.getWalletBalance(id, function(err, balance) {
|
client.getWalletBalance(id, function(err, balance) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -121,13 +153,15 @@ function getMempool(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function send(callback) {
|
function send(callback) {
|
||||||
var id = argv.args[1];
|
var id = getID();
|
||||||
var address = argv.args[2];
|
var options = {};
|
||||||
var value = utils.satoshi(argv.args[3]);
|
if (argv.script) {
|
||||||
var options = {
|
options.script = new bcoin.script(new Buffer(argv.script, 'hex'));
|
||||||
address: address,
|
options.value = utils.satoshi(argv.value || argv.args[0]);
|
||||||
value: value
|
} else {
|
||||||
};
|
options.address = argv.address || argv.args[0];
|
||||||
|
options.value = utils.satoshi(argv.value || argv.args[1]);
|
||||||
|
}
|
||||||
client.walletSend(id, options, function(err, tx) {
|
client.walletSend(id, options, function(err, tx) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -137,9 +171,9 @@ function send(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function zap(callback) {
|
function zap(callback) {
|
||||||
var id = argv.args[1];
|
var id = getID();
|
||||||
var now = utils.now();
|
var now = (argv.now >>> 0) || utils.now();
|
||||||
var age = 72 * 60 * 60;
|
var age = (argv.age >>> 0) || 72 * 60 * 60;
|
||||||
client.walletZap(id, now, age, function(err) {
|
client.walletZap(id, now, age, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -149,7 +183,7 @@ function zap(callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function main(callback) {
|
function main(callback) {
|
||||||
switch (argv.args[0]) {
|
switch (argv.args.shift()) {
|
||||||
case 'wallet':
|
case 'wallet':
|
||||||
return createWallet(callback);
|
return createWallet(callback);
|
||||||
case 'getwallet':
|
case 'getwallet':
|
||||||
@ -177,7 +211,7 @@ function main(callback) {
|
|||||||
function parseArg(argv) {
|
function parseArg(argv) {
|
||||||
var args = [];
|
var args = [];
|
||||||
var options = {};
|
var options = {};
|
||||||
var arg;
|
var arg, negate;
|
||||||
|
|
||||||
argv = argv.slice();
|
argv = argv.slice();
|
||||||
|
|
||||||
@ -211,23 +245,14 @@ function parseArg(argv) {
|
|||||||
|
|
||||||
while (argv.length) {
|
while (argv.length) {
|
||||||
arg = getarg();
|
arg = getarg();
|
||||||
switch (arg) {
|
if (arg.indexOf('--') === 0) {
|
||||||
case '--url':
|
negate = arg.indexOf('--no-') === 0;
|
||||||
options.url = argv.shift();
|
opt = arg.replace(/^--(no-)?/, '');
|
||||||
break;
|
options[opt] = !argv[0] || argv[0][0] === '-'
|
||||||
case '-h':
|
? (negate ? false : true)
|
||||||
case '--help':
|
: argv.shift();
|
||||||
return process.exit(1);
|
} else {
|
||||||
default:
|
args.push(arg);
|
||||||
if (arg.indexOf('--') === 0) {
|
|
||||||
opt = arg.replace(/^--(no-)?/, '');
|
|
||||||
options[opt] = !argv[0] || argv[0][0] === '-'
|
|
||||||
? true
|
|
||||||
: argv.shift();
|
|
||||||
} else {
|
|
||||||
args.push(arg);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
125
lib/bcoin/hd.js
125
lib/bcoin/hd.js
@ -230,48 +230,6 @@ HDPrivateKey.prototype.deriveAccount44 = function deriveAccount44(options) {
|
|||||||
return child;
|
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() {
|
HDPrivateKey.prototype.derivePurpose45 = function derivePurpose45() {
|
||||||
var child;
|
var child;
|
||||||
|
|
||||||
@ -287,41 +245,6 @@ HDPrivateKey.prototype.derivePurpose45 = function derivePurpose45() {
|
|||||||
return child;
|
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() {
|
HDPrivateKey.prototype.isPurpose45 = function isPurpose45() {
|
||||||
if (utils.readU8(this.depth) !== 1)
|
if (utils.readU8(this.depth) !== 1)
|
||||||
return false;
|
return false;
|
||||||
@ -334,34 +257,6 @@ HDPrivateKey.prototype.isAccount44 = function isAccount44() {
|
|||||||
return utils.readU8(this.depth) === 3;
|
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) {
|
HDPrivateKey.isExtended = function isExtended(data) {
|
||||||
if (typeof data !== 'string')
|
if (typeof data !== 'string')
|
||||||
return false;
|
return false;
|
||||||
@ -384,7 +279,7 @@ HDPrivateKey.prototype._normalize = function _normalize(data) {
|
|||||||
if (typeof data.depth === 'number')
|
if (typeof data.depth === 'number')
|
||||||
data.depth = new Buffer([data.depth]);
|
data.depth = new Buffer([data.depth]);
|
||||||
|
|
||||||
if (utils.readU8(data.depth) > 0xff)
|
if (data.depth.length > 1)
|
||||||
throw new Error('Depth is too high');
|
throw new Error('Depth is too high');
|
||||||
|
|
||||||
// parent finger print = uint_32be
|
// parent finger print = uint_32be
|
||||||
@ -423,7 +318,7 @@ HDPrivateKey.prototype._seed = function _seed(seed) {
|
|||||||
hash = utils.sha512hmac(seed, 'Bitcoin seed');
|
hash = utils.sha512hmac(seed, 'Bitcoin seed');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
version: network[this.network].prefixes.xprivkey,
|
version: array32(network[this.network].prefixes.xprivkey),
|
||||||
depth: new Buffer([0]),
|
depth: new Buffer([0]),
|
||||||
parentFingerPrint: new Buffer([0, 0, 0, 0]),
|
parentFingerPrint: new Buffer([0, 0, 0, 0]),
|
||||||
childIndex: 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);
|
entropy = ec.random(32);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
version: null,
|
version: array32(network.prefixes.xprivkey),
|
||||||
depth: new Buffer([0]),
|
depth: new Buffer([0]),
|
||||||
parentFingerPrint: new Buffer([0, 0, 0, 0]),
|
parentFingerPrint: new Buffer([0, 0, 0, 0]),
|
||||||
childIndex: 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) {
|
HDPrivateKey.prototype._generate = function _generate(privateKey, entropy) {
|
||||||
var data = HDPrivateKey._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;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -545,7 +440,7 @@ HDPrivateKey.prototype.__defineGetter__('hdPublicKey', function() {
|
|||||||
this._hdPublicKey = new HDPublicKey({
|
this._hdPublicKey = new HDPublicKey({
|
||||||
network: this.network,
|
network: this.network,
|
||||||
data: {
|
data: {
|
||||||
version: network[this.network].prefixes.xpubkey,
|
version: array32(network[this.network].prefixes.xpubkey),
|
||||||
depth: this.depth,
|
depth: this.depth,
|
||||||
parentFingerPrint: this.parentFingerPrint,
|
parentFingerPrint: this.parentFingerPrint,
|
||||||
childIndex: this.childIndex,
|
childIndex: this.childIndex,
|
||||||
@ -827,18 +722,8 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) {
|
|||||||
return obj && obj.isPublic && typeof obj._unbuild === 'function';
|
return obj && obj.isPublic && typeof obj._unbuild === 'function';
|
||||||
};
|
};
|
||||||
|
|
||||||
HDPublicKey.prototype.scan44 = HDPrivateKey.prototype.scan44;
|
|
||||||
HDPublicKey.prototype.deriveAccount44 = HDPrivateKey.prototype.deriveAccount44;
|
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.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.isPurpose45 = HDPrivateKey.prototype.isPurpose45;
|
||||||
HDPublicKey.prototype.isAccount44 = HDPrivateKey.prototype.isAccount44;
|
HDPublicKey.prototype.isAccount44 = HDPrivateKey.prototype.isAccount44;
|
||||||
HDPublicKey.prototype.toJSON = HDPrivateKey.prototype.toJSON;
|
HDPublicKey.prototype.toJSON = HDPrivateKey.prototype.toJSON;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user