Script: add standard Script.create*ScriptPubKey

Extracts the two Script types out of Script.createOutputScript, and puts
them both under test.

Also renames Script.createMultiSigOutputScript to adhere to the same
convention.
This commit is contained in:
Daniel Cousens 2014-05-05 16:20:54 +10:00
parent d0f684844c
commit 0822def7e0
4 changed files with 47 additions and 19 deletions

View file

@ -356,35 +356,45 @@ Script.prototype.writeBytes = function(data) {
/** /**
* Create an output for an address * Create an output for an address
*/ */
Script.createOutputScript = function(address, network) { Script.createScriptPubKey = function(address, network) {
assert(address instanceof Address) assert(address instanceof Address)
network = network || networks.bitcoin network = network || networks.bitcoin
var script = new Script() if (address.version === network.pubKeyHash) {
return Script.createPubKeyHashScriptPubKey(address.hash)
// Standard pay-to-script-hash
if (address.version === network.scriptHash) {
script.writeOp(Opcode.map.OP_HASH160)
script.writeBytes(address.hash)
script.writeOp(Opcode.map.OP_EQUAL)
return script
} }
assert.strictEqual(address.version, network.pubKeyHash, 'Unknown address type') assert.strictEqual(address.version, network.scriptHash, 'Unknown address type')
return Script.createP2SHScriptPubKey(address.hash)
}
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
Script.createPubKeyHashScriptPubKey = function(hash) {
var script = new Script()
// Standard pay-to-pubkey-hash
script.writeOp(Opcode.map.OP_DUP) script.writeOp(Opcode.map.OP_DUP)
script.writeOp(Opcode.map.OP_HASH160) script.writeOp(Opcode.map.OP_HASH160)
script.writeBytes(address.hash) script.writeBytes(hash)
script.writeOp(Opcode.map.OP_EQUALVERIFY) script.writeOp(Opcode.map.OP_EQUALVERIFY)
script.writeOp(Opcode.map.OP_CHECKSIG) script.writeOp(Opcode.map.OP_CHECKSIG)
return script return script
} }
// OP_HASH160 {scriptHash} OP_EQUAL
Script.createP2SHScriptPubKey = function(hash) {
var script = new Script()
script.writeOp(Opcode.map.OP_HASH160)
script.writeBytes(hash)
script.writeOp(Opcode.map.OP_EQUAL)
return script
}
// m [pubKeys ...] n OP_CHECKMULTISIG // m [pubKeys ...] n OP_CHECKMULTISIG
Script.createMultisigOutputScript = function(m, pubKeys) { Script.createMultisigScriptPubKey = function(m, pubKeys) {
var script = new Script() var script = new Script()
pubKeys = pubKeys.sort() pubKeys = pubKeys.sort()

View file

@ -116,7 +116,7 @@ Transaction.prototype.addOutput = function (address, value, network) {
this.outs.push(new TransactionOut({ this.outs.push(new TransactionOut({
value: value, value: value,
script: Script.createOutputScript(address, network), script: Script.createScriptPubKey(address, network),
network: network network: network
})) }))
} }
@ -373,7 +373,7 @@ Transaction.prototype.sign = function(index, key, type, network) {
var address = key.pub.getAddress(network.pubKeyHash) var address = key.pub.getAddress(network.pubKeyHash)
// FIXME: Assumed prior TX was pay-to-pubkey-hash // FIXME: Assumed prior TX was pay-to-pubkey-hash
var script = Script.createOutputScript(address, network) var script = Script.createScriptPubKey(address, network)
var signature = this.signScriptSig(index, script, key, type) var signature = this.signScriptSig(index, script, key, type)
var scriptSig = Script.createPubKeyHashScriptSig(signature, key.pub) var scriptSig = Script.createPubKeyHashScriptSig(signature, key.pub)

View file

@ -98,6 +98,24 @@ describe('Script', function() {
}) })
}) })
describe('pay-to-pubKeyHash', function() {
it('matches the test data', function() {
var address = Address.fromBase58Check('19E6FV3m3kEPoJD5Jz6dGKdKwTVvjsWUvu')
var script = Script.createPubKeyHashScriptPubKey(address.hash)
assert.equal(script.toHex(), pubKeyScriptPubKey)
})
})
describe('pay-to-scriptHash', function() {
it('matches the test data', function() {
var hash = new Buffer('e8c300c87986efa84c37c0519929019ef86eb5b4', 'hex')
var script = Script.createP2SHScriptPubKey(hash)
assert.equal(script.toHex(), p2shScriptPubKey)
})
})
describe('getToAddress', function() { describe('getToAddress', function() {
it('works for p2sh type output', function() { it('works for p2sh type output', function() {
var script = Script.fromHex(p2shScriptPubKey) var script = Script.fromHex(p2shScriptPubKey)
@ -129,7 +147,7 @@ describe('Script', function() {
}) })
it('should create valid redeemScript', function() { it('should create valid redeemScript', function() {
var redeemScript = Script.createMultisigOutputScript(2, pubKeys) var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
var hash160 = crypto.hash160(redeemScript.buffer) var hash160 = crypto.hash160(redeemScript.buffer)
var multisigAddress = new Address(hash160, networks.bitcoin.scriptHash) var multisigAddress = new Address(hash160, networks.bitcoin.scriptHash)
@ -150,7 +168,7 @@ describe('Script', function() {
var expected = '0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae' var expected = '0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae'
it('should create a valid P2SH multisig scriptSig', function() { it('should create a valid P2SH multisig scriptSig', function() {
var redeemScript = Script.createMultisigOutputScript(2, pubKeys) var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
var actual = Script.createP2SHMultisigScriptSig(signatures, redeemScript) var actual = Script.createP2SHMultisigScriptSig(signatures, redeemScript)
assert.equal(b2h(actual.buffer), expected) assert.equal(b2h(actual.buffer), expected)

View file

@ -260,7 +260,7 @@ describe('Transaction', function() {
}) })
var pubKeys = privKeys.map(function(eck) { return eck.pub }) var pubKeys = privKeys.map(function(eck) { return eck.pub })
var pubKeyBuffers = pubKeys.map(function(q) { return q.toBuffer() }) var pubKeyBuffers = pubKeys.map(function(q) { return q.toBuffer() })
var redeemScript = Script.createMultisigOutputScript(2, pubKeyBuffers) var redeemScript = Script.createMultisigScriptPubKey(2, pubKeyBuffers)
var signatures = privKeys.map(function(privKey) { var signatures = privKeys.map(function(privKey) {
return tx.signScriptSig(0, redeemScript, privKey) return tx.signScriptSig(0, redeemScript, privKey)