refactor getData for inputs and outputs.

This commit is contained in:
Christopher Jeffrey 2015-12-23 19:33:23 -08:00
parent b14c4daee0
commit b77c34789e
2 changed files with 107 additions and 158 deletions

View File

@ -78,6 +78,10 @@ Input.prototype.__defineGetter__('lock', function() {
return this.output.lock; return this.output.lock;
}); });
Input.prototype.__defineGetter__('text', function() {
return this.data.text;
});
Input.prototype.__defineGetter__('output', function() { Input.prototype.__defineGetter__('output', function() {
if (!this.out.tx) if (!this.out.tx)
return; return;
@ -100,56 +104,59 @@ Input.getData = function getData(input) {
var s = input.script; var s = input.script;
var sub = bcoin.script.subscript(input.script); var sub = bcoin.script.subscript(input.script);
var sig, pub, hash, addr, redeem, data, output; var schema, sig, pub, hash, addr, redeem, data, output;
schema = {
type: null,
side: 'input',
coinbase: null,
height: -1,
sig: null,
pub: null,
hash: null,
addr: null,
multisig: null,
redeem: null,
flags: null,
text: null,
value: new bn(0),
lock: 0,
script: s,
seq: input.seq,
tx: null,
txid: null,
index: null,
_script: null,
none: false
};
if (bcoin.script.lockTime(sub)) if (bcoin.script.lockTime(sub))
sub = sub.slice(3); sub = sub.slice(3);
if (!input.out) { if (!input.out) {
return { return utils.merge(schema, {
type: 'unknown', type: 'unknown',
side: 'input',
sig: null,
pub: null,
hash: '[unknown]',
addr: '[unknown]', addr: '[unknown]',
multisig: null,
redeem: null,
flags: null,
text: null,
value: new bn(0),
script: s,
seq: input.seq,
none: true none: true
}; });
} }
if (+input.out.hash === 0) { if (+input.out.hash === 0) {
data = bcoin.script.coinbase(input.script); data = bcoin.script.coinbase(input.script);
return { return utils.merge(schema, {
type: 'coinbase', type: 'coinbase',
side: 'input',
coinbase: data, coinbase: data,
height: data.height || -1, height: data.height || -1,
sig: null,
pub: null,
hash: '[coinbase]',
addr: '[coinbase]', addr: '[coinbase]',
multisig: null,
redeem: null,
flags: data.flags, flags: data.flags,
text: data.text, text: data.text.join(''),
value: new bn(0),
script: s,
seq: input.seq,
none: true none: true
}; });
} }
if (input.out.tx) { if (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);
data.seq = input.seq;
if (data.type === 'pubkey' || data.type === 'pubkeyhash') { if (data.type === 'pubkey' || data.type === 'pubkeyhash') {
data.sig = sub[0]; data.sig = sub[0];
} else if (data.type === 'multisig') { } else if (data.type === 'multisig') {
@ -159,52 +166,35 @@ Input.getData = function getData(input) {
data.multisig.sigs = sub.slice(1, -1); data.multisig.sigs = sub.slice(1, -1);
data.sig = data.multisig.sigs[0]; data.sig = data.multisig.sigs[0];
} }
// data.tx = input.out.tx; return utils.merge(data, {
data.hash = input.out.hash; seq: input.seq,
data.index = input.out.index; // tx: input.out.tx,
data._script = s; txid: input.out.hash,
return data; index: input.out.index,
_script: s
});
} }
if (bcoin.script.isPubkeyInput(s)) { if (bcoin.script.isPubkeyInput(s)) {
return { return utils.merge(schema, {
type: 'pubkey', type: 'pubkey',
side: 'input',
sig: sub[0], sig: sub[0],
pub: null,
hash: '[unknown]',
addr: '[unknown]', addr: '[unknown]',
multisig: null,
redeem: null,
flags: null,
text: null,
value: new bn(0),
script: s,
seq: input.seq,
none: true none: true
}; });
} }
if (bcoin.script.isPubkeyhashInput(s)) { if (bcoin.script.isPubkeyhashInput(s)) {
pub = sub[1]; pub = sub[1];
hash = utils.ripesha(pub); hash = utils.ripesha(pub);
addr = bcoin.wallet.hash2addr(hash); addr = bcoin.wallet.hash2addr(hash);
return { return utils.merge(schema, {
type: 'pubkeyhash', type: 'pubkeyhash',
side: 'input',
sig: sub[0], sig: sub[0],
pub: pub, pub: pub,
hash: hash, hash: hash,
addr: addr, addr: addr
multisig: null, });
redeem: null,
flags: null,
text: null,
value: new bn(0),
script: s,
seq: input.seq,
none: false
};
} }
if (bcoin.script.isScripthashInput(s)) { if (bcoin.script.isScripthashInput(s)) {
@ -217,26 +207,24 @@ Input.getData = function getData(input) {
script: redeem, script: redeem,
value: new bn(0) value: new bn(0)
}); });
data.type = 'scripthash';
data.side = 'input';
data.sig = sig[0];
data.hash = hash;
data.addr = addr;
data.multisig.sig = sig; data.multisig.sig = sig;
data.redeem = redeem; return utils.merge(data, {
data.script = s; type: 'scripthash',
data.seq = input.seq; side: 'input',
return data; sig: sig[0],
hash: hash,
addr: addr,
redeem: redeem,
script: s,
seq: input.seq
});
} }
if (bcoin.script.isMultisigInput(s)) { if (bcoin.script.isMultisigInput(s)) {
sig = sub.slice(1); sig = sub.slice(1);
return { return utils.merge(schema, {
type: 'multisig', type: 'multisig',
side: 'input',
sig: sub[0], sig: sub[0],
pub: null,
hash: '[unknown]',
addr: '[unknown]', addr: '[unknown]',
multisig: { multisig: {
m: sig.length, m: sig.length,
@ -246,34 +234,18 @@ Input.getData = function getData(input) {
hashes: null, hashes: null,
addrs: null addrs: null
}, },
redeem: null,
value: new bn(0),
script: s,
seq: input.seq,
none: true none: true
}; });
} }
return { return utils.merge(schema, {
type: 'unknown', type: 'unknown',
side: 'input',
sig: null,
pub: null,
hash: '[unknown]',
addr: '[unknown]', addr: '[unknown]',
multisig: null,
redeem: null,
flags: null,
text: null,
value: new bn(0),
script: s,
seq: input.seq,
none: true none: true
}; });
}; };
Input.prototype.inspect = function inspect() { Input.prototype.inspect = function inspect() {
var data = this.data;
var output = this.output var output = this.output
? this.output.inspect() ? this.output.inspect()
: { type: 'unknown', value: '0.0' }; : { type: 'unknown', value: '0.0' };
@ -285,6 +257,7 @@ Input.prototype.inspect = function inspect() {
return { return {
type: this.type, type: this.type,
addr: this.addr, addr: this.addr,
text: this.text,
lock: this.lock, lock: this.lock,
script: bcoin.script.format(this.script)[0], script: bcoin.script.format(this.script)[0],
value: utils.btc(output.value), value: utils.btc(output.value),

View File

@ -63,6 +63,10 @@ Output.prototype.__defineGetter__('lock', function() {
return lock.toNumber(); return lock.toNumber();
}); });
Output.prototype.__defineGetter__('text', function() {
return this.data.text;
});
Output.getData = function getData(output) { Output.getData = function getData(output) {
if (!output || !output.script) if (!output || !output.script)
return; return;
@ -70,7 +74,31 @@ Output.getData = function getData(output) {
var s = output.script; var s = output.script;
var sub = bcoin.script.subscript(output.script); var sub = bcoin.script.subscript(output.script);
var lock = bcoin.script.lockTime(s); var lock = bcoin.script.lockTime(s);
var pub, hash, addr, pubs, ret; var schema, pub, hash, addr, pubs, ret;
schema = {
type: null,
side: 'output',
coinbase: null,
height: -1,
sig: null,
pub: null,
hash: null,
addr: null,
multisig: null,
redeem: null,
flags: null,
text: null,
value: new bn(0),
lock: lock ? lock.toNumber() : 0,
script: s,
seq: null,
tx: null,
txid: null,
index: null,
_script: null,
none: false
};
if (lock) if (lock)
sub = sub.slice(3); sub = sub.slice(3);
@ -79,53 +107,33 @@ Output.getData = function getData(output) {
pub = sub[0]; pub = sub[0];
hash = utils.ripesha(pub); hash = utils.ripesha(pub);
addr = bcoin.wallet.hash2addr(hash); addr = bcoin.wallet.hash2addr(hash);
return { return utils.merge(schema, {
type: 'pubkey', type: 'pubkey',
side: 'output',
sig: null,
pub: pub, pub: pub,
hash: hash, hash: hash,
addr: addr, addr: addr,
multisig: null, value: output.value
redeem: null, });
flags: null,
text: null,
value: output.value,
script: s,
lock: lock,
none: false
};
} }
if (bcoin.script.isPubkeyhash(s)) { if (bcoin.script.isPubkeyhash(s)) {
hash = sub[2]; hash = sub[2];
addr = bcoin.wallet.hash2addr(hash); addr = bcoin.wallet.hash2addr(hash);
return { return utils.merge(schema, {
type: 'pubkeyhash', type: 'pubkeyhash',
side: 'output', side: 'output',
sig: null,
pub: null,
hash: hash, hash: hash,
addr: addr, addr: addr,
multisig: null, value: output.value
redeem: null, });
flags: null,
text: null,
value: output.value,
script: s,
lock: lock,
none: false
};
} }
pubs = bcoin.script.isMultisig(s); pubs = bcoin.script.isMultisig(s);
if (pubs) { if (pubs) {
hash = utils.ripesha(pubs[0]); hash = utils.ripesha(pubs[0]);
addr = bcoin.wallet.hash2addr(hash); addr = bcoin.wallet.hash2addr(hash);
return { return utils.merge(schema, {
type: 'multisig', type: 'multisig',
side: 'output',
sig: null,
pub: pubs[0], pub: pubs[0],
hash: hash, hash: hash,
addr: addr, addr: addr,
@ -142,24 +150,16 @@ Output.getData = function getData(output) {
return bcoin.wallet.hash2addr(hash); return bcoin.wallet.hash2addr(hash);
}) })
}, },
redeem: null, value: output.value
flags: null, });
text: null,
value: output.value,
script: s,
lock: lock,
none: false
};
} }
if (bcoin.script.isScripthash(s)) { if (bcoin.script.isScripthash(s)) {
hash = utils.toHex(sub[1]); hash = utils.toHex(sub[1]);
addr = bcoin.wallet.hash2addr(hash, 'scripthash'); addr = bcoin.wallet.hash2addr(hash, 'scripthash');
return { return utils.merge(schema, {
type: 'scripthash', type: 'scripthash',
side: 'output', side: 'output',
sig: null,
pub: null,
hash: hash, hash: hash,
addr: addr, addr: addr,
multisig: { multisig: {
@ -170,52 +170,27 @@ Output.getData = function getData(output) {
hashes: null, hashes: null,
addrs: null addrs: null
}, },
redeem: null, value: output.value
flags: null, });
text: null,
value: output.value,
script: s,
lock: lock,
none: false
};
} }
if (bcoin.script.isColored(s)) { if (bcoin.script.isColored(s)) {
ret = bcoin.script.colored(s); ret = bcoin.script.colored(s);
return { return utils.merge(schema, {
type: 'colored', type: 'colored',
side: 'output',
sig: null,
pub: null,
hash: '[colored]',
addr: '[colored]', addr: '[colored]',
multisig: null,
redeem: null,
flags: ret, flags: ret,
text: utils.array2utf8(ret), text: utils.array2utf8(ret),
value: output.value, value: output.value,
script: s,
lock: lock,
none: true none: true
}; });
} }
return { return utils.merge(schema, {
type: 'unknown', type: 'unknown',
side: 'output',
sig: null,
pub: null,
hash: '[unknown]',
addr: '[unknown]', addr: '[unknown]',
multisig: null,
redeem: null,
flags: null,
text: null,
value: new bn(0),
script: s,
lock: lock,
none: true none: true
}; });
}; };
Output.prototype.inspect = function inspect() { Output.prototype.inspect = function inspect() {
@ -238,6 +213,7 @@ Output.prototype.inspect = function inspect() {
return { return {
type: this.type, type: this.type,
addr: this.addr, addr: this.addr,
text: this.text,
lock: this.lock, lock: this.lock,
script: bcoin.script.format(this.script)[0], script: bcoin.script.format(this.script)[0],
value: utils.btc(this.value), value: utils.btc(this.value),