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

View File

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