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
*/
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()

View file

@ -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)