diff --git a/src/script.js b/src/script.js index a58ab73..97aa070 100644 --- a/src/script.js +++ b/src/script.js @@ -356,35 +356,45 @@ Script.prototype.writeBytes = function(data) { /** * Create an output for an address */ -Script.createOutputScript = function(address, network) { +Script.createScriptPubKey = function(address, network) { assert(address instanceof Address) network = network || networks.bitcoin - var script = new Script() - - // 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 + if (address.version === network.pubKeyHash) { + return Script.createPubKeyHashScriptPubKey(address.hash) } - 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_HASH160) - script.writeBytes(address.hash) + script.writeBytes(hash) script.writeOp(Opcode.map.OP_EQUALVERIFY) script.writeOp(Opcode.map.OP_CHECKSIG) 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 -Script.createMultisigOutputScript = function(m, pubKeys) { +Script.createMultisigScriptPubKey = function(m, pubKeys) { var script = new Script() pubKeys = pubKeys.sort() diff --git a/src/transaction.js b/src/transaction.js index f6660bc..49b1a4a 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -116,7 +116,7 @@ Transaction.prototype.addOutput = function (address, value, network) { this.outs.push(new TransactionOut({ value: value, - script: Script.createOutputScript(address, network), + script: Script.createScriptPubKey(address, network), network: network })) } @@ -373,7 +373,7 @@ Transaction.prototype.sign = function(index, key, type, network) { var address = key.pub.getAddress(network.pubKeyHash) // 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 scriptSig = Script.createPubKeyHashScriptSig(signature, key.pub) diff --git a/test/script.js b/test/script.js index 90f6ef3..303d181 100644 --- a/test/script.js +++ b/test/script.js @@ -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() { it('works for p2sh type output', function() { var script = Script.fromHex(p2shScriptPubKey) @@ -129,7 +147,7 @@ describe('Script', 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 multisigAddress = new Address(hash160, networks.bitcoin.scriptHash) @@ -150,7 +168,7 @@ describe('Script', function() { var expected = '0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae' 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) assert.equal(b2h(actual.buffer), expected) diff --git a/test/transaction.js b/test/transaction.js index 55f74e7..5716a48 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -260,7 +260,7 @@ describe('Transaction', function() { }) var pubKeys = privKeys.map(function(eck) { return eck.pub }) 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) { return tx.signScriptSig(0, redeemScript, privKey)