add getKey functions to TX.

This commit is contained in:
Christopher Jeffrey 2015-12-10 17:54:34 -08:00
parent bb9974b818
commit 1caa0804d9

View File

@ -713,6 +713,118 @@ TX.prototype.inputAddrs = function inputAddrs() {
});
};
TX.getInputKey = function(input) {
if (!input || !input.script) return;
var script = input.script;
if (bcoin.script.isPubkeyhashInput(script)) {
var scriptSig = utils.toHex(script[0]);
var pubKey = script[1];
var hash = utils.ripesha(pubKey);
var addr = bcoin.wallet.hash2addr(hash);
return {
sig: scriptSig,
pub: pubKey,
hash: hash,
addr: addr
};
}
if (!input.out.tx)
return false;
var output = input.out.tx.outputs[input.out.index];
if (bcoin.script.isScripthash(script)) {
var pub = script[script.length - 1];
var hash = utils.ripesha(pub);
var addr = bcoin.wallet.hash2addr(hash, 'script');
var redeem = bcoin.script.decode(pub);
var keys = TX.getOutputKey({ script: redeem });
keys.pub = pub;
keys.hash = hash;
keys.addr = addr;
keys.p2sh = {
sig: null,
redeem: redeem,
pub: pub,
hash: hash,
addr: addr
};
return keys;
}
return TX.getOutputKey(output);
};
TX.getOutputKey = function(output) {
if (!output || !output.script) return;
var script = output.script;
if (bcoin.script.isPubkeyhash(script)) {
var hash = script[2];
var addr = bcoin.wallet.hash2addr(hash);
return {
sig: null,
pub: null,
hash: hash,
addr: addr
};
}
if (bcoin.script.isSimplePubkeyhash(script)) {
var pubKey = script[0];
var hash = utils.ripesha(pubKey);
var addr = bcoin.wallet.hash2addr(hash);
return {
sig: null,
pub: pubKey,
hash: hash,
addr: addr
};
}
var pubKeys = bcoin.script.isMultisig(script);
if (pubKeys) {
var keys = pubKeys.map(function(pubKey) {
var hash = utils.ripesha(pubKey);
var addr = bcoin.wallet.hash2addr(hash);
return {
pub: pubKey,
hash: hash,
addr: addr
};
});
return {
sig: null,
pub: keys[0].pub,
hash: keys[0].hash,
addr: keys[0].addr,
multisig: keys
};
}
if (bcoin.script.isScripthash(script, scriptHash)) {
var hash = utils.toHex(s[1]);
var addr = bcoin.wallet.hash2addr(hash, 'script');
return {
sig: null,
pub: null,
hash: hash,
addr: addr,
p2sh: {
sig: null,
redeem: null,
pub: null,
hash: hash,
addr: addr
}
};
}
};
TX.prototype.getFee = function getFee() {
return this.funds('in').sub(this.funds('out'));
};