wallet json and prefix. style. cleanup.

This commit is contained in:
Christopher Jeffrey 2015-12-20 01:43:09 -08:00
parent 61699cca3a
commit 1db84a0528
7 changed files with 53 additions and 36 deletions

View File

@ -167,11 +167,10 @@ HDPrivateKey.prototype._normalize = function _normalize(data, version) {
data.publicKey = data.publicKey || data.pub;
// version = uint_32be
if (typeof data.version === 'string') {
if (typeof data.version === 'string')
data.version = utils.toArray(data.version, 'hex');
} else if (typeof data.version === 'number') {
else if (typeof data.version === 'number')
data.version = array32(data.version);
}
// depth = unsigned char
if (typeof data.depth === 'string')

View File

@ -394,11 +394,14 @@ Peer.prototype._handleGetData = function handleGetData(items) {
items.forEach(function(item) {
// Filter out not broadcasted things
var hash = utils.toHex(item.hash);
if (!this._broadcast.map[hash])
return;
var entry = this._broadcast.map[hash];
this._write(this.framer.packet(entry.type, entry.value));
entry.e.emit('request');
}, this);
};

View File

@ -147,7 +147,7 @@ exports.hashType = {
anyonecanpay: 0x80
};
exports.rhashType = Object.keys(exports.hashType).reduce(function(out, type) {
exports.hashTypeByVal = Object.keys(exports.hashType).reduce(function(out, type) {
out[exports.hashType[type]] = type;
return out;
}, {});

View File

@ -602,7 +602,6 @@ script.execute = function execute(s, stack, tx, index, recurse) {
return false;
} else {
stack.push(n.toArray());
// stack.push(res ? [ 1 ] : []);
}
break;
case 'within':
@ -673,7 +672,7 @@ script.execute = function execute(s, stack, tx, index, recurse) {
pub = stack.pop();
sig = stack.pop();
type = sig[sig.length - 1];
if (!constants.rhashType[type & 0x1f])
if (!constants.hashTypeByVal[type & 0x1f])
return false;
if (!script.isValidSig(sig))
@ -729,7 +728,7 @@ script.execute = function execute(s, stack, tx, index, recurse) {
for (i = 0, j = 0; i < m && j < n; i++) {
sig = stack.pop();
type = sig[sig.length - 1];
if (!constants.rhashType[type & 0x1f])
if (!constants.hashTypeByVal[type & 0x1f])
return false;
if (!script.isValidSig(sig))
@ -846,7 +845,7 @@ script.exec = function exec(input, output, tx, i, recurse) {
};
script.multisig = function multisig(keys, m, n) {
if (keys.length < m)
if (keys.length !== n)
throw new Error('Wrong amount of pubkeys for multisig script');
assert(m >= 1 && m <= n);

View File

@ -122,7 +122,7 @@ TX.prototype._input = function _input(i, index) {
if (lock) {
if (this._lock === 0)
this.lock = Math.max(lock.toNumber(), this.lock);
if (!bcoin.script.spendable(this, this.lock))
if (!bcoin.script.spendable(prev, this.lock))
throw new Error('Cannot spend ' + utils.revHex(input.out.hash));
}
}
@ -545,7 +545,7 @@ TX.prototype.subscriptHash = function subscriptHash(index, s, type) {
// bitcoind used to return 1 as an error code:
// it ended up being treated like a hash.
if (index >= copy.inputs.length)
return constants.oneHash;
return constants.oneHash.slice();
copy.inputs.forEach(function(input, i) {
input.script = index === i ? s : [];
@ -562,7 +562,7 @@ TX.prototype.subscriptHash = function subscriptHash(index, s, type) {
} else if ((type & 0x1f) === constants.hashType.single) {
// bitcoind sighash_single bug:
if (index >= copy.outputs.length)
return constants.oneHash;
return constants.oneHash.slice();
while (copy.outputs.length < index + 1)
copy.outputs.push({});
while (copy.outputs.length > index + 1)
@ -663,9 +663,6 @@ TX.prototype.maxSize = function maxSize() {
copy.inputs.forEach(function(input, i) {
var s, m, n, script, redeem;
// Get the previous output's script
// s = input.out.tx.outputs[input.out.index].script;
// Get the previous output's subscript
s = input.out.tx.getSubscript(input.out.index);

View File

@ -324,7 +324,6 @@ utils.ascii2array = function ascii2array(str) {
return dst;
};
// stringify
utils.array2ascii = function array2ascii(arr) {
return utils.readAscii(arr, 0, arr.length);
};
@ -355,8 +354,7 @@ utils.stringify = function stringify(arr) {
function zero2(word) {
if (word.length === 1)
return '0' + word;
else
return word;
return word;
}
function toHex(msg) {
@ -484,7 +482,6 @@ function RequestCache() {
this.map = {};
this.count = 0;
}
utils.RequestCache = RequestCache;
RequestCache.prototype.add = function add(id, cb) {
if (this.map[id]) {
@ -511,6 +508,8 @@ RequestCache.prototype.fullfill = function fullfill(id, err, data) {
});
};
utils.RequestCache = RequestCache;
utils.asyncify = function asyncify(fn) {
return function _asynicifedFn(err, data1, data2) {
if (!fn)
@ -572,9 +571,7 @@ utils.toBTC = function toBTC(satoshi, strict) {
utils.fromBTC = function fromBTC(btc, strict) {
var m = new bn(10000000).mul(new bn(10));
var satoshi;
var parts;
var satoshi, parts;
if (typeof btc === 'string' && /^\d+(?:\.\d+)?$/.test(btc)) {
parts = btc.split('.');

View File

@ -84,7 +84,6 @@ function Wallet(options, passphrase) {
this.multisig(options.multisig || {});
this.prefix = 'bt/' + this.getOwnAddress() + '/';
this.tx = new bcoin.txPool(this);
// Just a constants, actually
@ -129,6 +128,13 @@ Wallet.prototype._init = function init() {
});
};
Wallet.prototype.__defineGetter__('prefix', function() {
if (this.n !== this.keys.length)
throw new Error('Not enough keys to generate prefix');
return 'bt/' + this.getFullAddress() + '/';
});
Wallet.prototype.multisig = function multisig(options) {
var pub = this.getOwnPublicKey();
@ -136,7 +142,6 @@ Wallet.prototype.multisig = function multisig(options) {
options.keys = options.keys || options.pubkeys || [];
this.type = options.type || 'pubkeyhash';
// this.keys = (options.keys || []).map(utils.toKeyArray);
this.keys = [];
this.m = options.m || 1;
this.n = options.n || 1;
@ -164,7 +169,7 @@ Wallet.prototype.multisig = function multisig(options) {
throw new Error('n ranges between 1 and ' + this.nmax);
if (this.keys.length > this.n)
throw new Error('No more than ' + this.n + ' are necessary');
throw new Error('No more than ' + this.n + ' keys are necessary');
};
Wallet.prototype.addKey = function addKey(key) {
@ -179,6 +184,9 @@ Wallet.prototype.addKey = function addKey(key) {
this.keys.push(key);
if (this.keys.length > this.n)
throw new Error('No more than ' + this.n + ' keys are necessary');
this.keys = utils.sortKeys(this.keys);
};
@ -309,7 +317,7 @@ Wallet.prototype.getAddress = function getAddress() {
Wallet.key2hash = function key2hash(key) {
if (typeof key === 'string')
key = utils.toArray(key, 'hex');
key = utils.toKeyArray(key);
return utils.ripesha(key);
};
@ -363,9 +371,12 @@ Wallet.addr2hash = function addr2hash(addr, prefix) {
Wallet.prototype.validateAddress = function validateAddress(addr, prefix) {
if (!addr)
return false;
var p = Wallet.addr2hash(addr, prefix);
return p.length !== 0;
};
Wallet.validateAddress = Wallet.prototype.validateAddress;
Wallet.prototype.ownOutput = function ownOutput(tx, index) {
@ -591,18 +602,22 @@ Wallet.prototype.toJSON = function toJSON(encrypt) {
tx: this.tx.toJSON(),
ntx: this.tx.all().length,
hd: this.hd ? {
// seed: this.hd.seed ? {
// mnemonic: this.hd.seed.mnemonic,
// passphrase: this.hd.seed.passphrase
// } : undefined,
depth: this.hd.data.depth,
seed: this.hd.seed ? {
mnemonic: encrypt
? encrypt(this.hd.seed.mnemonic)
: this.hd.seed.mnemonic,
passphrase: encrypt
? encrypt(this.hd.seed.passphrase)
: this.hd.seed.passphrase
} : undefined,
depth: new bn(this.hd.data.depth).toNumber(),
parentFingerPrint: utils.toHex(this.hd.data.parentFingerPrint),
childIndex: this.hd.data.childIndex,
childIndex: new bn(this.hd.data.childIndex).toNumber(),
chainCode: utils.toHex(this.hd.data.chainCode)
} : undefined,
multisig: this.n > 1 ? {
type: this.type,
keys: this.keys.map(utils.toHex),
keys: this.keys.map(utils.toBase58),
m: this.m,
n: this.n
} : undefined
@ -610,16 +625,19 @@ Wallet.prototype.toJSON = function toJSON(encrypt) {
};
Wallet.fromJSON = function fromJSON(json, decrypt) {
var priv, pub, compressed, key, w;
assert.equal(json.v, 1);
assert.equal(json.type, 'wallet');
if (json.network)
assert.equal(json.network, network.type);
if (json.encrypted && decrypt)
json.priv = decrypt(json.priv);
if (json.encrypted && !decrypt)
throw new Error('Cannot decrypt wallet');
var priv, pub, compressed, key, w;
if (json.encrypted)
json.priv = decrypt(json.priv);
if (json.priv) {
key = bcoin.utils.fromBase58(json.priv);
@ -641,10 +659,14 @@ Wallet.fromJSON = function fromJSON(json, decrypt) {
}
if (json.multisig)
json.multisig.keys = json.multisig.keys.map(utils.toKeyArray);
json.multisig.keys = json.multisig.keys.map(utils.fromBase58);
if (json.hd) {
json.hd.privateKey = priv;
if (json.encrypted && json.hd.seed) {
json.hd.seed.mnemonic = decrypt(json.hd.seed.mnemonic);
json.hd.seed.passphrase = decrypt(json.hd.seed.passphrase);
}
priv = new hd.priv(json.hd);
}