refactor input and output data.

This commit is contained in:
Christopher Jeffrey 2015-12-23 23:01:36 -08:00
parent 0ccf67aef6
commit 207d9e1a30
2 changed files with 155 additions and 125 deletions

View File

@ -64,14 +64,52 @@ Input.prototype.__defineGetter__('data', function() {
return data; return data;
}); });
Input.prototype.__defineGetter__('addr', function() {
return this.data.addr;
});
Input.prototype.__defineGetter__('type', function() { Input.prototype.__defineGetter__('type', function() {
return this.data.type; return this.data.type;
}); });
Input.prototype.__defineGetter__('sig', function() {
return this.data.sigs[0];
});
Input.prototype.__defineGetter__('pub', function() {
return this.data.pubs[0];
});
Input.prototype.__defineGetter__('hash', function() {
return this.data.hashes[0];
});
Input.prototype.__defineGetter__('addr', function() {
return this.data.scriptaddr
|| this.data.addrs[0]
|| this._id(this.type);
});
Input.prototype.__defineGetter__('sigs', function() {
return this.data.sigs;
});
Input.prototype.__defineGetter__('pubs', function() {
return this.data.pubs;
});
Input.prototype.__defineGetter__('hashes', function() {
return this.data.hashes;
});
Input.prototype.__defineGetter__('addrs', function() {
return this.data.addrs;
});
Input.prototype.__defineGetter__('m', function() {
return this.data.m || 1;
});
Input.prototype.__defineGetter__('n', function() {
return this.data.n || this.data.m || 1;
});
Input.prototype.__defineGetter__('lock', function() { Input.prototype.__defineGetter__('lock', function() {
if (!this.output) if (!this.output)
return 0; return 0;
@ -120,22 +158,23 @@ Input.getData = function getData(input) {
schema = { schema = {
type: null, type: null,
side: 'input', side: 'input',
coinbase: null, sigs: [],
height: -1, pubs: [],
sig: null, hashes: [],
pub: null, addrs: [],
hash: null,
addr: null,
multisig: null,
redeem: null, redeem: null,
scripthash: null,
scriptaddr: null,
m: 0,
n: 0,
height: -1,
flags: null, flags: null,
text: null, text: null,
value: new bn(0),
lock: 0, lock: 0,
value: new bn(0),
script: s, script: s,
seq: input.seq, seq: input.seq,
tx: null, tx: null,
txid: null,
index: null, index: null,
_script: null, _script: null,
none: false none: false
@ -144,35 +183,25 @@ Input.getData = function getData(input) {
if (bcoin.script.lockTime(sub)) if (bcoin.script.lockTime(sub))
sub = sub.slice(3); sub = sub.slice(3);
if (!input.out) { if (input.out && +input.out.hash === 0) {
return utils.merge(schema, {
type: 'unknown',
addr: input._id('unknown'),
none: true
});
}
if (+input.out.hash === 0) {
data = bcoin.script.coinbase(input.script); data = bcoin.script.coinbase(input.script);
return utils.merge(schema, { return utils.merge(schema, {
type: 'coinbase', type: 'coinbase',
coinbase: data, height: data.height != null ? data.height : -1,
height: data.height || -1,
addr: input._id('coinbase'),
flags: data.flags, flags: data.flags,
text: data.text.join('').replace(/[\r\n\t\v]/g, ''), text: data.text.join('').replace(/[\r\n\t\v]/g, ''),
none: true none: true
}); });
} }
if (input.out.tx) { if (input.out && input.out.tx) {
output = input.out.tx.outputs[input.out.index]; output = input.out.tx.outputs[input.out.index];
data = bcoin.output.getData(output); data = bcoin.output.getData(output);
if (data.type === 'pubkey' ) { if (data.type === 'pubkey' ) {
data.sig = sub[0]; data.sigs = [sub[0]];
} else if (data.type === 'pubkeyhash') { } else if (data.type === 'pubkeyhash') {
data.sig = sub[0]; data.sigs = [sub[0]];
data.pub = sub[1]; data.pubs = [sub[1]];
} else if (data.type === 'scripthash') { } else if (data.type === 'scripthash') {
// We work backwards here: scripthash is one of the few cases // We work backwards here: scripthash is one of the few cases
// where we get more data from the input than the output. // where we get more data from the input than the output.
@ -186,13 +215,11 @@ Input.getData = function getData(input) {
val.script = data.script; val.script = data.script;
data = val; data = val;
} else if (data.type === 'multisig') { } else if (data.type === 'multisig') {
data.multisig.sigs = sub.slice(1); data.sigs = sub.slice(1);
data.sig = sub[1];
} }
return utils.merge(data, { return utils.merge(data, {
seq: input.seq, seq: input.seq,
// tx: input.out.tx, tx: input.out.hash,
txid: input.out.hash,
index: input.out.index, index: input.out.index,
_script: s _script: s
}); });
@ -201,8 +228,7 @@ Input.getData = function getData(input) {
if (bcoin.script.isPubkeyInput(s)) { if (bcoin.script.isPubkeyInput(s)) {
return utils.merge(schema, { return utils.merge(schema, {
type: 'pubkey', type: 'pubkey',
sig: sub[0], sigs: [sub[0]],
addr: input._id('unknown'),
none: true none: true
}); });
} }
@ -213,31 +239,30 @@ Input.getData = function getData(input) {
addr = bcoin.wallet.hash2addr(hash); addr = bcoin.wallet.hash2addr(hash);
return utils.merge(schema, { return utils.merge(schema, {
type: 'pubkeyhash', type: 'pubkeyhash',
sig: sub[0], sigs: [sub[0]],
pub: pub, pubs: [pub],
hash: hash, hashes: [hash],
addr: addr addrs: [addr]
}); });
} }
if (bcoin.script.isScripthashInput(s)) { if (bcoin.script.isScripthashInput(s)) {
sig = sub.slice(1, -1); sig = sub.slice(1, -1);
pub = sub[sub.length - 1]; redeem = sub[sub.length - 1];
hash = utils.ripesha(pub); hash = utils.ripesha(redeem);
addr = bcoin.wallet.hash2addr(hash, 'scripthash'); addr = bcoin.wallet.hash2addr(hash, 'scripthash');
redeem = bcoin.script.decode(pub); redeem = bcoin.script.decode(redeem);
data = bcoin.output.getData({ data = bcoin.output.getData({
script: redeem, script: redeem,
value: new bn(0) value: new bn(0)
}); });
data.multisig.sig = sig;
return utils.merge(data, { return utils.merge(data, {
type: 'scripthash', type: 'scripthash',
side: 'input', side: 'input',
sig: sig[0], sigs: sig,
hash: hash,
addr: addr,
redeem: redeem, redeem: redeem,
scripthash: hash,
scriptaddr: addr,
script: s, script: s,
seq: input.seq seq: input.seq
}); });
@ -247,23 +272,14 @@ Input.getData = function getData(input) {
sig = sub.slice(1); sig = sub.slice(1);
return utils.merge(schema, { return utils.merge(schema, {
type: 'multisig', type: 'multisig',
sig: sub[0], sigs: sig,
addr: input._id('unknown'), m: sig.length,
multisig: {
m: sig.length,
n: null,
sigs: sig,
pubs: null,
hashes: null,
addrs: null
},
none: true none: true
}); });
} }
return utils.merge(schema, { return utils.merge(schema, {
type: 'unknown', type: 'unknown',
addr: input._id('unknown'),
none: true none: true
}); });
}; };
@ -280,10 +296,12 @@ Input.prototype.inspect = function inspect() {
return { return {
type: this.type, type: this.type,
addr: this.addr, addr: this.addr,
sigs: this.sigs.map(utils.toHex),
pubs: this.pubs.map(utils.toHex),
text: this.text, text: this.text,
lock: this.lock, lock: this.lock,
script: bcoin.script.format(this.script)[0],
value: utils.btc(output.value), value: utils.btc(output.value),
script: bcoin.script.format(this.script)[0],
seq: this.seq, seq: this.seq,
output: output output: output
}; };

View File

@ -48,14 +48,52 @@ Output.prototype.__defineGetter__('data', function() {
return data; return data;
}); });
Output.prototype.__defineGetter__('addr', function() {
return this.data.addr;
});
Output.prototype.__defineGetter__('type', function() { Output.prototype.__defineGetter__('type', function() {
return this.data.type; return this.data.type;
}); });
Output.prototype.__defineGetter__('sig', function() {
return this.data.sigs[0];
});
Output.prototype.__defineGetter__('pub', function() {
return this.data.pubs[0];
});
Output.prototype.__defineGetter__('hash', function() {
return this.data.hashes[0];
});
Output.prototype.__defineGetter__('addr', function() {
return this.data.scriptaddr
|| this.data.addrs[0]
|| this._id(this.type);
});
Output.prototype.__defineGetter__('sigs', function() {
return this.data.sigs;
});
Output.prototype.__defineGetter__('pubs', function() {
return this.data.pubs;
});
Output.prototype.__defineGetter__('hashes', function() {
return this.data.hashes;
});
Output.prototype.__defineGetter__('addrs', function() {
return this.data.addrs;
});
Output.prototype.__defineGetter__('m', function() {
return this.data.m || 1;
});
Output.prototype.__defineGetter__('n', function() {
return this.data.n || this.data.m || 1;
});
Output.prototype.__defineGetter__('lock', function() { Output.prototype.__defineGetter__('lock', function() {
var lock = bcoin.script.lockTime(this.script); var lock = bcoin.script.lockTime(this.script);
if (!lock) if (!lock)
@ -88,22 +126,23 @@ Output.getData = function getData(output) {
schema = { schema = {
type: null, type: null,
side: 'output', side: 'output',
coinbase: null, sigs: [],
height: -1, pubs: [],
sig: null, hashes: [],
pub: null, addrs: [],
hash: null,
addr: null,
multisig: null,
redeem: null, redeem: null,
scripthash: null,
scriptaddr: null,
m: 0,
n: 0,
height: -1,
flags: null, flags: null,
text: null, text: null,
value: new bn(0),
lock: lock ? lock.toNumber() : 0, lock: lock ? lock.toNumber() : 0,
value: new bn(0),
script: s, script: s,
seq: null, seq: null,
tx: null, tx: null,
txid: null,
index: null, index: null,
_script: null, _script: null,
none: false none: false
@ -118,9 +157,9 @@ Output.getData = function getData(output) {
addr = bcoin.wallet.hash2addr(hash); addr = bcoin.wallet.hash2addr(hash);
return utils.merge(schema, { return utils.merge(schema, {
type: 'pubkey', type: 'pubkey',
pub: pub, pubs: [pub],
hash: hash, hashes: [hash],
addr: addr, addrs: [addr],
value: output.value value: output.value
}); });
} }
@ -131,34 +170,27 @@ Output.getData = function getData(output) {
return utils.merge(schema, { return utils.merge(schema, {
type: 'pubkeyhash', type: 'pubkeyhash',
side: 'output', side: 'output',
hash: hash, hashes: [hash],
addr: addr, addrs: [addr],
value: output.value value: output.value
}); });
} }
pubs = bcoin.script.isMultisig(s); pubs = bcoin.script.isMultisig(s);
if (pubs) { if (pubs) {
hash = utils.ripesha(pubs[0]); hash = pubs.map(function(key) {
addr = bcoin.wallet.hash2addr(hash); return utils.ripesha(key);
});
addr = hash.map(function(hash) {
return bcoin.wallet.hash2addr(hash);
});
return utils.merge(schema, { return utils.merge(schema, {
type: 'multisig', type: 'multisig',
pub: pubs[0], pubs: pubs,
hash: hash, hashes: hash,
addr: addr, addrs: addr,
multisig: { m: new bn(sub[0]).toNumber(),
m: new bn(sub[0]).toNumber(), n: new bn(sub[sub.length - 2]).toNumber(),
n: new bn(sub[sub.length - 2]).toNumber(),
sigs: null,
pubs: pubs,
hashes: pubs.map(function(key) {
return utils.ripesha(key);
}),
addrs: pubs.map(function(key) {
var hash = utils.ripesha(key);
return bcoin.wallet.hash2addr(hash);
})
},
value: output.value value: output.value
}); });
} }
@ -169,16 +201,8 @@ Output.getData = function getData(output) {
return utils.merge(schema, { return utils.merge(schema, {
type: 'scripthash', type: 'scripthash',
side: 'output', side: 'output',
hash: hash, scripthash: hash,
addr: addr, scriptaddr: addr,
multisig: {
m: null,
n: null,
sigs: null,
pubs: null,
hashes: null,
addrs: null
},
value: output.value value: output.value
}); });
} }
@ -187,7 +211,6 @@ Output.getData = function getData(output) {
ret = bcoin.script.colored(s); ret = bcoin.script.colored(s);
return utils.merge(schema, { return utils.merge(schema, {
type: 'colored', type: 'colored',
addr: output._id('colored'),
flags: ret, flags: ret,
text: utils.array2utf8(ret), text: utils.array2utf8(ret),
value: output.value, value: output.value,
@ -197,37 +220,26 @@ Output.getData = function getData(output) {
return utils.merge(schema, { return utils.merge(schema, {
type: 'unknown', type: 'unknown',
addr: output._id('unknown'),
none: true none: true
}); });
}; };
Output.prototype.inspect = function inspect() { Output.prototype.inspect = function inspect() {
var multisig = this.data.multisig || null;
var redeem = this.type === 'scripthash'
? bcoin.script.format(this.data.redeem)[0]
: null;
if (multisig) {
multisig = {
m: multisig.m,
n: multisig.n,
sigs: (multisig.sigs || []).map(utils.toHex),
pubs: (multisig.pubs || []).map(utils.toHex),
hashes: multisig.hashes || [],
addrs: multisig.addrs || []
};
}
return { return {
type: this.type, type: this.type,
addr: this.addr, addr: this.addr,
pubs: this.pubs.map(utils.toHex),
hashes: this.hashes.map(utils.toHex),
addrs: this.addrs,
redeem: this.type === 'scripthash'
? bcoin.script.format(this.data.redeem)[0]
: null,
m: this.m,
n: this.n,
text: this.text, text: this.text,
lock: this.lock, lock: this.lock,
script: bcoin.script.format(this.script)[0],
value: utils.btc(this.value), value: utils.btc(this.value),
multisig: multisig, script: bcoin.script.format(this.script)[0]
redeem: redeem
}; };
}; };