clean up input and output schema.

This commit is contained in:
Christopher Jeffrey 2015-12-23 23:30:33 -08:00
parent 207d9e1a30
commit 1fb6cb04b4
2 changed files with 106 additions and 89 deletions

View File

@ -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
});

View File

@ -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
});