From 1fb6cb04b4651a7a035508d49055180b931148a7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 23 Dec 2015 23:30:33 -0800 Subject: [PATCH] clean up input and output schema. --- lib/bcoin/input.js | 91 +++++++++++++++++++++----------------- lib/bcoin/output.js | 104 +++++++++++++++++++++++--------------------- 2 files changed, 106 insertions(+), 89 deletions(-) diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index f5517f7c..e768077e 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -69,37 +69,37 @@ Input.prototype.__defineGetter__('type', function() { }); Input.prototype.__defineGetter__('sig', function() { - return this.data.sigs[0]; + return this.sigs[0]; }); Input.prototype.__defineGetter__('pub', function() { - return this.data.pubs[0]; + return this.pubs[0]; }); Input.prototype.__defineGetter__('hash', function() { - return this.data.hashes[0]; + return this.hashes[0]; }); Input.prototype.__defineGetter__('addr', function() { return this.data.scriptaddr - || this.data.addrs[0] + || this.addrs[0] || this._id(this.type); }); Input.prototype.__defineGetter__('sigs', function() { - return this.data.sigs; + return this.data.sigs || []; }); Input.prototype.__defineGetter__('pubs', function() { - return this.data.pubs; + return this.data.pubs || []; }); Input.prototype.__defineGetter__('hashes', function() { - return this.data.hashes; + return this.data.hashes || []; }); Input.prototype.__defineGetter__('addrs', function() { - return this.data.addrs; + return this.data.addrs || []; }); Input.prototype.__defineGetter__('m', function() { @@ -113,7 +113,7 @@ Input.prototype.__defineGetter__('n', function() { Input.prototype.__defineGetter__('lock', function() { if (!this.output) return 0; - return this.output.lock; + return this.output.lock || 0; }); Input.prototype.__defineGetter__('text', function() { @@ -147,45 +147,58 @@ Input.prototype._id = function _id(prefix) { return '[' + prefix + ':' + hash.slice(0, 7) + ']'; }; +// Schema and defaults for data object: +// { +// type: null, +// side: 'input', +// sigs: [], +// pubs: [], +// hashes: [], +// addrs: [], +// redeem: null, +// scripthash: null, +// scriptaddr: null, +// m: 0, +// n: 0, +// height: -1, +// flags: null, +// text: null, +// lock: 0, +// value: new bn(0), +// script: s, +// seq: input.seq, +// prev: null, +// index: null, +// _script: null, +// none: false +// } + Input.getData = function getData(input) { if (!input || !input.script) return; var s = input.script; var sub = bcoin.script.subscript(input.script); - var schema, sig, pub, hash, addr, redeem, data, output, val; + var def, sig, pub, hash, addr, redeem, data, output, val; - schema = { - type: null, + def = { side: 'input', - sigs: [], - pubs: [], - hashes: [], - addrs: [], - redeem: null, - scripthash: null, - scriptaddr: null, - m: 0, - n: 0, - height: -1, - flags: null, - text: null, - lock: 0, value: new bn(0), script: s, - seq: input.seq, - tx: null, - index: null, - _script: null, - none: false + seq: input.seq }; if (bcoin.script.lockTime(sub)) sub = sub.slice(3); + if (input.out) { + def.prev = input.out.hash; + def.index = input.out.index; + } + if (input.out && +input.out.hash === 0) { data = bcoin.script.coinbase(input.script); - return utils.merge(schema, { + return utils.merge(def, { type: 'coinbase', height: data.height != null ? data.height : -1, flags: data.flags, @@ -218,15 +231,15 @@ Input.getData = function getData(input) { data.sigs = sub.slice(1); } return utils.merge(data, { - seq: input.seq, - tx: input.out.hash, - index: input.out.index, - _script: s + seq: def.seq, + prev: def.prev, + index: def.index, + _script: def.script }); } if (bcoin.script.isPubkeyInput(s)) { - return utils.merge(schema, { + return utils.merge(def, { type: 'pubkey', sigs: [sub[0]], none: true @@ -237,7 +250,7 @@ Input.getData = function getData(input) { pub = sub[1]; hash = utils.ripesha(pub); addr = bcoin.wallet.hash2addr(hash); - return utils.merge(schema, { + return utils.merge(def, { type: 'pubkeyhash', sigs: [sub[0]], pubs: [pub], @@ -270,7 +283,7 @@ Input.getData = function getData(input) { if (bcoin.script.isMultisigInput(s)) { sig = sub.slice(1); - return utils.merge(schema, { + return utils.merge(def, { type: 'multisig', sigs: sig, m: sig.length, @@ -278,7 +291,7 @@ Input.getData = function getData(input) { }); } - return utils.merge(schema, { + return utils.merge(def, { type: 'unknown', none: true }); diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 7c04f2a7..a8fbb578 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -53,37 +53,37 @@ Output.prototype.__defineGetter__('type', function() { }); Output.prototype.__defineGetter__('sig', function() { - return this.data.sigs[0]; + return this.sigs[0]; }); Output.prototype.__defineGetter__('pub', function() { - return this.data.pubs[0]; + return this.pubs[0]; }); Output.prototype.__defineGetter__('hash', function() { - return this.data.hashes[0]; + return this.hashes[0]; }); Output.prototype.__defineGetter__('addr', function() { return this.data.scriptaddr - || this.data.addrs[0] + || this.addrs[0] || this._id(this.type); }); Output.prototype.__defineGetter__('sigs', function() { - return this.data.sigs; + return this.data.sigs || []; }); Output.prototype.__defineGetter__('pubs', function() { - return this.data.pubs; + return this.data.pubs || []; }); Output.prototype.__defineGetter__('hashes', function() { - return this.data.hashes; + return this.data.hashes || []; }); Output.prototype.__defineGetter__('addrs', function() { - return this.data.addrs; + return this.data.addrs || []; }); Output.prototype.__defineGetter__('m', function() { @@ -114,6 +114,32 @@ Output.prototype._id = function _id(prefix) { return '[' + prefix + ':' + hash.slice(0, 7) + ']'; }; +// Schema and defaults for data object: +// { +// type: null, +// side: 'output', +// sigs: [], +// pubs: [], +// hashes: [], +// addrs: [], +// redeem: null, +// scripthash: null, +// scriptaddr: null, +// m: 0, +// n: 0, +// height: -1, +// flags: null, +// text: null, +// lock: lock ? lock.toNumber() : 0, +// value: output.value, +// script: s, +// seq: null, +// prev: null, +// index: null, +// _script: null, +// none: false +// } + Output.getData = function getData(output) { if (!output || !output.script) return; @@ -121,58 +147,39 @@ Output.getData = function getData(output) { var s = output.script; var sub = bcoin.script.subscript(output.script); var lock = bcoin.script.lockTime(s); - var schema, pub, hash, addr, pubs, ret; + var def, pub, hash, addr, pubs, data; - schema = { - type: null, + def = { side: 'output', - sigs: [], - pubs: [], - hashes: [], - addrs: [], - redeem: null, - scripthash: null, - scriptaddr: null, - m: 0, - n: 0, - height: -1, - flags: null, - text: null, - lock: lock ? lock.toNumber() : 0, - value: new bn(0), - script: s, - seq: null, - tx: null, - index: null, - _script: null, - none: false + value: output.value, + script: s }; - if (lock) + if (lock) { sub = sub.slice(3); + def.lock = lock.toNumber(); + } if (bcoin.script.isPubkey(s)) { pub = sub[0]; hash = utils.ripesha(pub); addr = bcoin.wallet.hash2addr(hash); - return utils.merge(schema, { + return utils.merge(def, { type: 'pubkey', pubs: [pub], hashes: [hash], - addrs: [addr], - value: output.value + addrs: [addr] }); } if (bcoin.script.isPubkeyhash(s)) { hash = sub[2]; addr = bcoin.wallet.hash2addr(hash); - return utils.merge(schema, { + return utils.merge(def, { type: 'pubkeyhash', side: 'output', hashes: [hash], - addrs: [addr], - value: output.value + addrs: [addr] }); } @@ -184,41 +191,38 @@ Output.getData = function getData(output) { addr = hash.map(function(hash) { return bcoin.wallet.hash2addr(hash); }); - return utils.merge(schema, { + return utils.merge(def, { type: 'multisig', pubs: pubs, hashes: hash, addrs: addr, m: new bn(sub[0]).toNumber(), - n: new bn(sub[sub.length - 2]).toNumber(), - value: output.value + n: new bn(sub[sub.length - 2]).toNumber() }); } if (bcoin.script.isScripthash(s)) { hash = utils.toHex(sub[1]); addr = bcoin.wallet.hash2addr(hash, 'scripthash'); - return utils.merge(schema, { + return utils.merge(def, { type: 'scripthash', side: 'output', scripthash: hash, - scriptaddr: addr, - value: output.value + scriptaddr: addr }); } if (bcoin.script.isColored(s)) { - ret = bcoin.script.colored(s); - return utils.merge(schema, { + data = bcoin.script.colored(s); + return utils.merge(def, { type: 'colored', - flags: ret, - text: utils.array2utf8(ret), - value: output.value, + flags: data, + text: utils.array2utf8(data), none: true }); } - return utils.merge(schema, { + return utils.merge(def, { type: 'unknown', none: true });