Browse Source

templates: rename to scripts

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
41c3b68293
  1. 9
      src/address.js
  2. 1
      src/index.js
  3. 40
      src/scripts.js
  4. 4
      src/transaction.js
  5. 9
      test/integration/p2sh.js
  6. 32
      test/scripts.js
  7. 8
      test/transaction.js
  8. 4
      test/wallet.js

9
src/address.js

@ -1,7 +1,7 @@
var assert = require('assert')
var base58check = require('./base58check')
var networks = require('./networks')
var templates = require('./templates')
var scripts = require('./scripts')
function findScriptTypeByVersion(queryVersion) {
for (var networkName in networks) {
@ -38,7 +38,7 @@ Address.fromBase58Check = function(string) {
Address.fromScriptPubKey = function(script, network) {
network = network || networks.bitcoin
var type = templates.classifyScriptPubKey(script)
var type = scripts.classifyOutput(script)
if (type === 'pubkeyhash') {
return new Address(script.chunks[2], network.pubKeyHash)
@ -63,10 +63,11 @@ Address.prototype.toScriptPubKey = function() {
var scriptType = findScriptTypeByVersion(this.version)
if (scriptType === 'pubkeyhash') {
return templates.createPubKeyHashScriptPubKey(this.hash)
return scripts.pubKeyHashOutput(this.hash)
} else if (scriptType === 'scripthash') {
return templates.createP2SHScriptPubKey(this.hash)
return scripts.scriptHashOutput(this.hash)
}
assert(false, this.toString() + ' has no matching Script')

1
src/index.js

@ -14,6 +14,7 @@ module.exports = {
opcodes: require('./opcodes'),
HDNode: require('./hdnode'),
Script: require('./script'),
scripts: require('./scripts'),
Transaction: T.Transaction,
TransactionIn: T.TransactionIn,
TransactionOut: T.TransactionOut,

40
src/templates.js → src/scripts.js

@ -2,7 +2,7 @@ var assert = require('assert')
var opcodes = require('./opcodes')
var Script = require('./script')
function classifyScriptPubKey(script) {
function classifyOutput(script) {
if (isPubkeyhash.call(script)) {
return 'pubkeyhash'
} else if (isPubkey.call(script)) {
@ -18,7 +18,7 @@ function classifyScriptPubKey(script) {
}
}
function classifyScriptSig(script) {
function classifyInput(script) {
if (script.chunks.length == 1 && Buffer.isBuffer(script.chunks[0])) {
return 'pubkey'
} else if (script.chunks.length == 2 && Buffer.isBuffer(script.chunks[0]) && Buffer.isBuffer(script.chunks[1])) {
@ -82,7 +82,7 @@ function isSmallIntOp(opcode) {
// Standard Script Templates
// {pubKey} OP_CHECKSIG
function createPubKeyScriptPubKey(pubKey) {
function pubKeyOutput(pubKey) {
return Script.fromChunks([
pubKey.toBuffer(),
opcodes.OP_CHECKSIG
@ -90,7 +90,7 @@ function createPubKeyScriptPubKey(pubKey) {
}
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
function createPubKeyHashScriptPubKey(hash) {
function pubKeyHashOutput(hash) {
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
return Script.fromChunks([
@ -103,7 +103,7 @@ function createPubKeyHashScriptPubKey(hash) {
}
// OP_HASH160 {scriptHash} OP_EQUAL
function createP2SHScriptPubKey(hash) {
function scriptHashOutput(hash) {
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
return Script.fromChunks([
@ -114,7 +114,7 @@ function createP2SHScriptPubKey(hash) {
}
// m [pubKeys ...] n OP_CHECKMULTISIG
function createMultisigScriptPubKey(m, pubKeys) {
function multisigOutput(m, pubKeys) {
assert(Array.isArray(pubKeys), 'Expected Array, got ' + pubKeys)
assert(pubKeys.length >= m, 'Not enough pubKeys provided')
@ -132,21 +132,21 @@ function createMultisigScriptPubKey(m, pubKeys) {
}
// {signature}
function createPubKeyScriptSig(signature) {
function pubKeyInput(signature) {
assert(Buffer.isBuffer(signature), 'Expected Buffer, got ' + signature)
return Script.fromChunks(signature)
}
// {signature} {pubKey}
function createPubKeyHashScriptSig(signature, pubKey) {
function pubKeyHashInput(signature, pubKey) {
assert(Buffer.isBuffer(signature), 'Expected Buffer, got ' + signature)
return Script.fromChunks([signature, pubKey.toBuffer()])
}
// <scriptSig> {serialized scriptPubKey script}
function createP2SHScriptSig(scriptSig, scriptPubKey) {
function scriptHashInput(scriptSig, scriptPubKey) {
return Script.fromChunks([].concat(
scriptSig.chunks,
scriptPubKey.toBuffer()
@ -154,7 +154,7 @@ function createP2SHScriptSig(scriptSig, scriptPubKey) {
}
// OP_0 [signatures ...]
function createMultisigScriptSig(signatures, scriptPubKey) {
function multisigInput(signatures, scriptPubKey) {
if (scriptPubKey) {
assert(isMultisig.call(scriptPubKey))
@ -167,14 +167,14 @@ function createMultisigScriptSig(signatures, scriptPubKey) {
}
module.exports = {
classifyScriptPubKey: classifyScriptPubKey,
classifyScriptSig: classifyScriptSig,
createMultisigScriptPubKey: createMultisigScriptPubKey,
createMultisigScriptSig: createMultisigScriptSig,
createP2SHScriptPubKey: createP2SHScriptPubKey,
createP2SHScriptSig: createP2SHScriptSig,
createPubKeyHashScriptPubKey: createPubKeyHashScriptPubKey,
createPubKeyHashScriptSig: createPubKeyHashScriptSig,
createPubKeyScriptPubKey: createPubKeyScriptPubKey,
createPubKeyScriptSig: createPubKeyScriptSig
classifyInput: classifyInput,
classifyOutput: classifyOutput,
multisigInput: multisigInput,
multisigOutput: multisigOutput,
pubKeyHashInput: pubKeyHashInput,
pubKeyHashOutput: pubKeyHashOutput,
pubKeyInput: pubKeyInput,
pubKeyOutput: pubKeyOutput,
scriptHashInput: scriptHashInput,
scriptHashOutput: scriptHashOutput
}

4
src/transaction.js

@ -5,7 +5,7 @@ var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var ecdsa = require('./ecdsa')
var opcodes = require('./opcodes')
var templates = require('./templates')
var scripts = require('./scripts')
var Address = require('./address')
var ECKey = require('./eckey')
@ -346,7 +346,7 @@ Transaction.prototype.sign = function(index, key, type) {
var signature = this.signScriptSig(index, script, key, type)
// FIXME: Assumed prior TX was pay-to-pubkey-hash
var scriptSig = templates.createPubKeyHashScriptSig(signature, key.pub)
var scriptSig = scripts.pubKeyHashInput(signature, key.pub)
this.setScriptSig(index, scriptSig)
}

9
test/integration/p2sh.js

@ -3,6 +3,7 @@ var assert = require('assert')
var bitcoin = require('../../')
var crypto = bitcoin.crypto
var networks = bitcoin.networks
var scripts = bitcoin.scripts
var Address = bitcoin.Address
var ECKey = bitcoin.ECKey
@ -28,8 +29,8 @@ describe('Bitcoin-js', function() {
var outputAmount = 1e4
var pubKeys = privKeys.map(function(eck) { return eck.pub })
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
var scriptPubKey = Script.createP2SHScriptPubKey(redeemScript.getHash())
var redeemScript = scripts.multisigOutput(2, pubKeys)
var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash())
var multisigAddress = Address.fromScriptPubKey(scriptPubKey, networks.testnet).toString()
@ -57,8 +58,8 @@ describe('Bitcoin-js', function() {
return tx.signScriptSig(0, redeemScript, privKey)
})
var redeemScriptSig = Script.createMultisigScriptSig(signatures)
var scriptSig = Script.createP2SHScriptSig(redeemScriptSig, redeemScript)
var redeemScriptSig = scripts.multisigInput(signatures)
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
tx.setScriptSig(0, scriptSig)
// broadcast our transaction

32
test/templates.js → test/scripts.js

@ -1,7 +1,7 @@
var assert = require('assert')
var crypto = require('../src/crypto')
var networks = require('../src/networks')
var templates = require('../src/templates')
var scripts = require('../src/scripts')
var Address = require('../src/address')
var ECPubKey = require('../src/ecpubkey')
@ -12,27 +12,27 @@ var fixtures = require('./fixtures/script.json')
function b2h(b) { return new Buffer(b).toString('hex') }
function h2b(h) { return new Buffer(h, 'hex') }
describe('Templates', function() {
describe('classifyScriptSig', function() {
describe('Scripts', function() {
describe('classifyInput', function() {
fixtures.valid.forEach(function(f) {
if (f.scriptPubKey) return
it('supports ' + f.type, function() {
var script = Script.fromHex(f.hex)
var type = templates.classifyScriptSig(script)
var type = scripts.classifyInput(script)
assert.equal(type, f.type)
})
})
})
describe('classifyScriptPubKey', function() {
describe('classifyOutput', function() {
fixtures.valid.forEach(function(f) {
if (!f.scriptPubKey) return
it('supports ' + f.type, function() {
var script = Script.fromHex(f.hex)
var type = templates.classifyScriptPubKey(script)
var type = scripts.classifyOutput(script)
assert.equal(type, f.type)
})
@ -44,7 +44,7 @@ describe('Templates', function() {
it('matches the test data', function() {
var f = fixtures.valid[2]
var address = Address.fromBase58Check('19E6FV3m3kEPoJD5Jz6dGKdKwTVvjsWUvu')
var script = templates.createPubKeyHashScriptPubKey(address.hash)
var script = scripts.pubKeyHashOutput(address.hash)
assert.equal(script.toHex(), f.hex)
})
@ -55,7 +55,7 @@ describe('Templates', function() {
it('matches the test data', function() {
var f = fixtures.valid[0]
var pubKey = ECPubKey.fromHex(f.pubKey)
var script = templates.createPubKeyScriptPubKey(pubKey)
var script = scripts.pubKeyOutput(pubKey)
assert.equal(script.toHex(), f.hex)
})
@ -66,7 +66,7 @@ describe('Templates', function() {
it('matches the test data', function() {
var f = fixtures.valid[1]
var address = Address.fromBase58Check('3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8')
var script = templates.createP2SHScriptPubKey(address.hash)
var script = scripts.scriptHashOutput(address.hash)
assert.equal(script.toHex(), f.hex)
})
@ -85,7 +85,7 @@ describe('Templates', function() {
})
it('should create valid redeemScript', function() {
var redeemScript = templates.createMultisigScriptPubKey(2, pubKeys)
var redeemScript = scripts.multisigOutput(2, pubKeys)
var hash160 = crypto.hash160(new Buffer(redeemScript.buffer))
var multisigAddress = new Address(hash160, networks.bitcoin.scriptHash)
@ -95,7 +95,7 @@ describe('Templates', function() {
it('should throw on not enough pubKeys provided', function() {
assert.throws(function() {
templates.createMultisigScriptPubKey(4, pubKeys)
scripts.multisigOutput(4, pubKeys)
}, /Not enough pubKeys provided/)
})
})
@ -113,19 +113,19 @@ describe('Templates', function() {
var expected = '0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae'
it('should create a valid P2SH multisig scriptSig', function() {
var redeemScript = templates.createMultisigScriptPubKey(2, pubKeys)
var redeemScriptSig = templates.createMultisigScriptSig(signatures)
var redeemScript = scripts.multisigOutput(2, pubKeys)
var redeemInput = scripts.multisigInput(signatures)
var scriptSig = templates.createP2SHScriptSig(redeemScriptSig, redeemScript)
var scriptSig = scripts.scriptHashInput(redeemInput, redeemScript)
assert.equal(b2h(scriptSig.buffer), expected)
})
it('should throw on not enough signatures', function() {
var redeemScript = templates.createMultisigScriptPubKey(2, pubKeys)
var redeemScript = scripts.multisigOutput(2, pubKeys)
assert.throws(function() {
templates.createMultisigScriptSig(signatures.slice(1), redeemScript)
scripts.multisigInput(signatures.slice(1), redeemScript)
}, /Not enough signatures provided/)
})
})

8
test/transaction.js

@ -1,6 +1,6 @@
var assert = require('assert')
var networks = require('../src/networks')
var templates = require('../src/templates')
var scripts = require('../src/scripts')
var Address = require('../src/address')
var ECKey = require('../src/eckey')
@ -256,14 +256,14 @@ describe('Transaction', function() {
return ECKey.fromWIF(wif)
})
var pubKeys = privKeys.map(function(eck) { return eck.pub })
var redeemScript = templates.createMultisigScriptPubKey(2, pubKeys)
var redeemScript = scripts.multisigOutput(2, pubKeys)
var signatures = privKeys.map(function(privKey) {
return tx.signScriptSig(0, redeemScript, privKey)
})
var redeemScriptSig = templates.createMultisigScriptSig(signatures)
var scriptSig = templates.createP2SHScriptSig(redeemScriptSig, redeemScript)
var redeemScriptSig = scripts.multisigInput(signatures)
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
tx.setScriptSig(0, scriptSig)
signatures.forEach(function(sig, i){

4
test/wallet.js

@ -2,7 +2,7 @@ var assert = require('assert')
var crypto = require('../src/crypto')
var networks = require('../src/networks')
var sinon = require('sinon')
var templates = require('../src/templates')
var scripts = require('../src/scripts')
var Address = require('../src/address')
var HDNode = require('../src/hdnode')
@ -268,7 +268,7 @@ describe('Wallet', function() {
it('does not fail on scripts with no corresponding Address', function() {
var pubKey = wallet.getPrivateKey(0).pub
var script = templates.createPubKeyScriptPubKey(pubKey)
var script = scripts.pubKeyOutput(pubKey)
var tx2 = new Transaction()
tx2.addInput(fakeTxHash(1), 0)

Loading…
Cancel
Save