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

View File

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