diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index d48e3fe5..60045a01 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -55,6 +55,10 @@ Witness.prototype.getInputAddress = function getInputAddress(prev) { return Script.getInputAddress(this.items, prev, true); }; +Witness.prototype.getInputHash = function getInputHash(prev) { + return Script.getInputHash(this.items, prev, true); +}; + Witness.prototype.isPubkeyInput = function isPubkeyInput(key) { return Script.isPubkeyInput(this.items, key); }; @@ -1406,23 +1410,27 @@ Script.prototype.getLocktime = function getLocktime() { return { type: 'time', value: locktime }; }; -Script.prototype.getInputAddress = function getInputAddress(prev, isWitness) { +Script.prototype.getInputAddress = function getInputAddress(prev) { + return Script.getInputAddress(this.code, prev, false); +}; + +Script.getInputAddress = function getInputAddress(code, prev, isWitness) { if (prev) return prev.getAddress(); - if (this.isPubkeyInput()) + if (Script.isPubkeyInput(code)) return; - if (this.isPubkeyhashInput()) { - return bcoin.address.compileData(this.code[1], + if (Script.isPubkeyhashInput(code)) { + return bcoin.address.compileData(code[1], isWitness ? 'witnesspubkeyhash' : 'pubkeyhash'); } - if (this.isMultisigInput(null, isWitness)) + if (Script.isMultisigInput(code, null, isWitness)) return; - if (this.isScripthashInput(null, isWitness)) { - return bcoin.address.compileData(this.code[this.code.length - 1], + if (Script.isScripthashInput(code, null, isWitness)) { + return bcoin.address.compileData(code[code.length - 1], isWitness ? 'witnessscripthash' : 'scripthash'); } }; @@ -1452,6 +1460,53 @@ Script.prototype.getAddress = function getAddress() { return bcoin.address.compileHash(this.code[1], 'scripthash'); }; +Script.prototype.getInputHash = function getInputHash(prev) { + return Script.getInputHash(this.code, prev, false); +}; + +Script.getInputHash = function getInputHash(prev, isWitness) { + if (prev) + return prev.getHash(); + + if (Script.isPubkeyInput(code)) + return; + + if (Script.isPubkeyhashInput(code)) + return utils.ripesha(code[1]); + + if (Script.isMultisigInput(code, null, isWitness)) + return; + + if (Script.isScripthashInput(code, null, isWitness)) { + return isWitness + ? utils.sha256(code[code.length - 1]) + : utils.ripesha(code[code.length - 1]); + } +}; + +Script.prototype.getHash = function getHash() { + var program; + + if (this.isWitnessProgram()) { + program = this.getWitnessProgram(); + if (!program.type || program.type === 'unknown') + return; + return program.data; + } + + if (this.isPubkey()) + return utils.ripesha(this.code[0]); + + if (this.isPubkeyhash()) + return this.code[2]; + + if (this.isMultisig()) + return utils.ripesha(this.encode()); + + if (this.isScripthash()) + return this.code[1]; +}; + Script.prototype.isPubkey = function isPubkey(key) { var res;