Browse Source

Script: rename toScriptHash to getHash and add tests

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
f8e662e495
  1. 14
      src/script.js
  2. 12
      test/script.js
  3. 4
      test/transaction.js

14
src/script.js

@ -184,19 +184,7 @@ function isSmallIntOp(opcode) {
((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16)))
}
/**
* 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)
}
Script.prototype.getHash = function() {
return crypto.hash160(this.buffer)
}

12
test/script.js

@ -26,9 +26,19 @@ describe('Script', 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() {
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)
})
})
})

4
test/transaction.js

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

Loading…
Cancel
Save