Script: rename toScriptHash to getHash and add tests

This commit is contained in:
Daniel Cousens 2014-05-09 12:56:51 +10:00
parent cace8316e4
commit f8e662e495
3 changed files with 13 additions and 17 deletions

View file

@ -184,19 +184,7 @@ function isSmallIntOp(opcode) {
((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16))) ((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16)))
} }
/** Script.prototype.getHash = function() {
* Returns the address corresponding to this output in hash160 form.
* Assumes strange scripts are P2SH
*/
Script.prototype.toScriptHash = function() {
if(isPubkeyhash.call(this)) {
return this.chunks[2]
}
if(isScripthash.call(this)) {
return crypto.hash160(this.buffer)
}
return crypto.hash160(this.buffer) return crypto.hash160(this.buffer)
} }

View file

@ -26,9 +26,19 @@ describe('Script', function() {
}) })
describe('fromHex/toHex', function() { describe('fromHex/toHex', function() {
fixtures.valid.forEach(function(f) {
it('decodes/encodes ' + f.description, function() {
assert.equal(Script.fromHex(f.hex).toHex(), f.hex)
})
})
})
describe('getHash', function() {
it('matches the test vectors', function() { it('matches the test vectors', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
assert.equal(Script.fromHex(f.hex).toHex(), f.hex) var script = Script.fromHex(f.hex)
assert.equal(script.getHash().toString('hex'), f.hash)
}) })
}) })
}) })

View file

@ -72,9 +72,7 @@ describe('Transaction', function() {
var output = tx.outs[0] var output = tx.outs[0]
assert.equal(output.value, 5000000000) assert.equal(output.value, 5000000000)
assert.equal(b2h(output.script.toScriptHash()), "dd40dedd8f7e37466624c4dacc6362d8e7be23dd") assert.deepEqual(output.script, Address.fromBase58Check('n1gqLjZbRH1biT5o4qiVMiNig8wcCPQeB9').toScriptPubKey())
// assert.equal(output.address.toString(), "n1gqLjZbRH1biT5o4qiVMiNig8wcCPQeB9")
// TODO: address is wrong because it's a testnet transaction. Transaction needs to support testnet
}) })
it('assigns hash to deserialized object', function(){ it('assigns hash to deserialized object', function(){