Script: move all templates to templates.js
This commit is contained in:
parent
aded938ab6
commit
00cec9ce64
9 changed files with 331 additions and 366 deletions
|
@ -1,7 +1,7 @@
|
|||
var assert = require('assert')
|
||||
var base58check = require('./base58check')
|
||||
var networks = require('./networks')
|
||||
var Script = require('./script')
|
||||
var templates = require('./templates')
|
||||
|
||||
function findScriptTypeByVersion(queryVersion) {
|
||||
for (var networkName in networks) {
|
||||
|
@ -38,14 +38,13 @@ Address.fromBase58Check = function(string) {
|
|||
Address.fromScriptPubKey = function(script, network) {
|
||||
network = network || networks.bitcoin
|
||||
|
||||
var type = script.getOutType()
|
||||
var type = templates.classifyScriptPubKey(script)
|
||||
|
||||
if (type === 'pubkeyhash') {
|
||||
return new Address(new Buffer(script.chunks[2]), network.pubKeyHash)
|
||||
}
|
||||
return new Address(script.chunks[2], network.pubKeyHash)
|
||||
|
||||
else if (type === 'scripthash') {
|
||||
return new Address(new Buffer(script.chunks[1]), network.scriptHash)
|
||||
} else if (type === 'scripthash') {
|
||||
return new Address(script.chunks[1], network.scriptHash)
|
||||
}
|
||||
|
||||
assert(false, type + ' has no matching Address')
|
||||
|
@ -64,11 +63,10 @@ Address.prototype.toScriptPubKey = function() {
|
|||
var scriptType = findScriptTypeByVersion(this.version)
|
||||
|
||||
if (scriptType === 'pubKeyHash') {
|
||||
return Script.createPubKeyHashScriptPubKey(this.hash)
|
||||
}
|
||||
return templates.createPubKeyHashScriptPubKey(this.hash)
|
||||
|
||||
else if (scriptType === 'scriptHash') {
|
||||
return Script.createP2SHScriptPubKey(this.hash)
|
||||
} else if (scriptType === 'scriptHash') {
|
||||
return templates.createP2SHScriptPubKey(this.hash)
|
||||
}
|
||||
|
||||
assert(false, this.toString() + ' has no matching script')
|
||||
|
|
228
src/script.js
228
src/script.js
|
@ -110,232 +110,4 @@ Script.prototype.toHex = function() {
|
|||
return this.toBuffer().toString('hex')
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare the script to known templates of scriptPubKey.
|
||||
*
|
||||
* This method will compare the script to a small number of standard script
|
||||
* templates and return a string naming the detected type.
|
||||
*
|
||||
* Currently supported are:
|
||||
* Pubkeyhash (address)
|
||||
* Paying to a Bitcoin address which is the hash of a pubkey.
|
||||
* OP_DUP OP_HASH160 [pubKeyHash] OP_EQUALVERIFY OP_CHECKSIG
|
||||
*
|
||||
* Pubkey
|
||||
* Paying to a public key directly.
|
||||
* [pubKey] OP_CHECKSIG
|
||||
*
|
||||
* Scripthash (P2SH)
|
||||
* Paying to an address which is the hash of a script
|
||||
* OP_HASH160 [Scripthash] OP_EQUAL
|
||||
*
|
||||
* Multisig
|
||||
* Paying to multiple pubkeys and require a number of the signatures
|
||||
* m [pubkey] [pubkey] [pubkey] n OP_CHECKMULTISIG
|
||||
*
|
||||
* Nulldata
|
||||
* Provably prune-able outputs
|
||||
* OP_RETURN [data]
|
||||
*
|
||||
* Nonstandard:
|
||||
* Any other script (no template matched).
|
||||
*
|
||||
* https://github.com/bitcoin/bitcoin/blob/19e5b9d2dfcac4efadba636745485d9660fb1abe/src/script.cpp#L75
|
||||
*/
|
||||
|
||||
Script.prototype.getOutType = function() {
|
||||
if (isPubkeyhash.call(this)) {
|
||||
return 'pubkeyhash'
|
||||
} else if (isPubkey.call(this)) {
|
||||
return 'pubkey'
|
||||
} else if (isScripthash.call(this)) {
|
||||
return 'scripthash'
|
||||
} else if (isMultisig.call(this)) {
|
||||
return 'multisig'
|
||||
} else if (isNulldata.call(this)) {
|
||||
return 'nulldata'
|
||||
} else {
|
||||
return 'nonstandard'
|
||||
}
|
||||
}
|
||||
|
||||
function isPubkeyhash() {
|
||||
return this.chunks.length == 5 &&
|
||||
this.chunks[0] == opcodes.OP_DUP &&
|
||||
this.chunks[1] == opcodes.OP_HASH160 &&
|
||||
Buffer.isBuffer(this.chunks[2]) &&
|
||||
this.chunks[2].length === 20 &&
|
||||
this.chunks[3] == opcodes.OP_EQUALVERIFY &&
|
||||
this.chunks[4] == opcodes.OP_CHECKSIG
|
||||
}
|
||||
|
||||
function isPubkey() {
|
||||
return this.chunks.length === 2 &&
|
||||
Buffer.isBuffer(this.chunks[0]) &&
|
||||
this.chunks[1] === opcodes.OP_CHECKSIG
|
||||
}
|
||||
|
||||
function isScripthash() {
|
||||
return this.chunks[this.chunks.length - 1] == opcodes.OP_EQUAL &&
|
||||
this.chunks[0] == opcodes.OP_HASH160 &&
|
||||
Buffer.isBuffer(this.chunks[1]) &&
|
||||
this.chunks[1].length === 20 &&
|
||||
this.chunks.length == 3
|
||||
}
|
||||
|
||||
function isMultisig() {
|
||||
return this.chunks.length > 3 &&
|
||||
// m is a smallint
|
||||
isSmallIntOp(this.chunks[0]) &&
|
||||
// n is a smallint
|
||||
isSmallIntOp(this.chunks[this.chunks.length - 2]) &&
|
||||
// n greater or equal to m
|
||||
this.chunks[0] <= this.chunks[this.chunks.length - 2] &&
|
||||
// n cannot be 0
|
||||
this.chunks[this.chunks.length - 2] !== opcodes.OP_0 &&
|
||||
// n is the size of chunk length minus 3 (m, n, OP_CHECKMULTISIG)
|
||||
this.chunks.length - 3 === this.chunks[this.chunks.length - 2] - opcodes.OP_RESERVED &&
|
||||
// last chunk is OP_CHECKMULTISIG
|
||||
this.chunks[this.chunks.length - 1] == opcodes.OP_CHECKMULTISIG
|
||||
}
|
||||
|
||||
function isNulldata() {
|
||||
return this.chunks[0] === opcodes.OP_RETURN
|
||||
}
|
||||
|
||||
function isSmallIntOp(opcode) {
|
||||
return ((opcode == opcodes.OP_0) ||
|
||||
((opcode >= opcodes.OP_1) && (opcode <= opcodes.OP_16)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare the script to known templates of scriptSig.
|
||||
*
|
||||
* This method will compare the script to a small number of standard script
|
||||
* templates and return a string naming the detected type.
|
||||
*
|
||||
* WARNING: Use this method with caution. It merely represents a heuristic
|
||||
* based on common transaction formats. A non-standard transaction could
|
||||
* very easily match one of these templates by accident.
|
||||
*
|
||||
* Currently supported are:
|
||||
* Address:
|
||||
* Paying to a Bitcoin address which is the hash of a pubkey.
|
||||
* [sig] [pubKey]
|
||||
*
|
||||
* Pubkey:
|
||||
* Paying to a public key directly.
|
||||
* [sig]
|
||||
*
|
||||
* Multisig:
|
||||
* Paying to M-of-N public keys.
|
||||
*
|
||||
* Nonstandard:
|
||||
* Any other script (no template matched).
|
||||
*/
|
||||
Script.prototype.getInType = function() {
|
||||
if (this.chunks.length == 1 &&
|
||||
Buffer.isBuffer(this.chunks[0])) {
|
||||
// Direct IP to IP transactions only have the signature in their scriptSig.
|
||||
// TODO: We could also check that the length of the data is correct.
|
||||
return 'pubkey'
|
||||
} else if (this.chunks.length == 2 &&
|
||||
Buffer.isBuffer(this.chunks[0]) &&
|
||||
Buffer.isBuffer(this.chunks[1])) {
|
||||
return 'pubkeyhash'
|
||||
} else if (this.chunks[0] == opcodes.OP_0 &&
|
||||
this.chunks.slice(1).reduce(function(t, chunk, i) {
|
||||
return t && Buffer.isBuffer(chunk) && (chunk[0] == 48 || i == this.chunks.length - 1)
|
||||
}, true)) {
|
||||
return 'multisig'
|
||||
} else {
|
||||
return 'nonstandard'
|
||||
}
|
||||
}
|
||||
|
||||
// {pubKey} OP_CHECKSIG
|
||||
Script.createPubKeyScriptPubKey = function(pubKey) {
|
||||
return Script.fromChunks([
|
||||
pubKey.toBuffer(),
|
||||
opcodes.OP_CHECKSIG
|
||||
])
|
||||
}
|
||||
|
||||
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
|
||||
Script.createPubKeyHashScriptPubKey = function(hash) {
|
||||
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
|
||||
|
||||
return Script.fromChunks([
|
||||
opcodes.OP_DUP,
|
||||
opcodes.OP_HASH160,
|
||||
hash,
|
||||
opcodes.OP_EQUALVERIFY,
|
||||
opcodes.OP_CHECKSIG
|
||||
])
|
||||
}
|
||||
|
||||
// OP_HASH160 {scriptHash} OP_EQUAL
|
||||
Script.createP2SHScriptPubKey = function(hash) {
|
||||
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
|
||||
|
||||
return Script.fromChunks([
|
||||
opcodes.OP_HASH160,
|
||||
hash,
|
||||
opcodes.OP_EQUAL
|
||||
])
|
||||
}
|
||||
|
||||
// m [pubKeys ...] n OP_CHECKMULTISIG
|
||||
Script.createMultisigScriptPubKey = function(m, pubKeys) {
|
||||
assert(Array.isArray(pubKeys), 'Expected Array, got ' + pubKeys)
|
||||
assert(pubKeys.length >= m, 'Not enough pubKeys provided')
|
||||
|
||||
var pubKeyBuffers = pubKeys.map(function(pubKey) {
|
||||
return pubKey.toBuffer()
|
||||
})
|
||||
var n = pubKeys.length
|
||||
|
||||
return Script.fromChunks([].concat(
|
||||
(opcodes.OP_1 - 1) + m,
|
||||
pubKeyBuffers,
|
||||
(opcodes.OP_1 - 1) + n,
|
||||
opcodes.OP_CHECKMULTISIG
|
||||
))
|
||||
}
|
||||
|
||||
// {signature}
|
||||
Script.createPubKeyScriptSig = function(signature) {
|
||||
assert(Buffer.isBuffer(signature), 'Expected Buffer, got ' + signature)
|
||||
|
||||
return Script.fromChunks(signature)
|
||||
}
|
||||
|
||||
// {signature} {pubKey}
|
||||
Script.createPubKeyHashScriptSig = function(signature, pubKey) {
|
||||
assert(Buffer.isBuffer(signature), 'Expected Buffer, got ' + signature)
|
||||
|
||||
return Script.fromChunks([signature, pubKey.toBuffer()])
|
||||
}
|
||||
|
||||
// <scriptSig> {serialized scriptPubKey script}
|
||||
Script.createP2SHScriptSig = function(scriptSig, scriptPubKey) {
|
||||
return Script.fromChunks([].concat(
|
||||
scriptSig.chunks,
|
||||
scriptPubKey.toBuffer()
|
||||
))
|
||||
}
|
||||
|
||||
// OP_0 [signatures ...]
|
||||
Script.createMultisigScriptSig = function(signatures, scriptPubKey) {
|
||||
if (scriptPubKey) {
|
||||
assert(isMultisig.call(scriptPubKey))
|
||||
|
||||
var m = scriptPubKey.chunks[0]
|
||||
var k = m - (opcodes.OP_1 - 1)
|
||||
assert(k <= signatures.length, 'Not enough signatures provided')
|
||||
}
|
||||
|
||||
return Script.fromChunks([].concat(opcodes.OP_0, signatures))
|
||||
}
|
||||
|
||||
module.exports = Script
|
||||
|
|
180
src/templates.js
Normal file
180
src/templates.js
Normal file
|
@ -0,0 +1,180 @@
|
|||
var assert = require('assert')
|
||||
var opcodes = require('./opcodes')
|
||||
var Script = require('./script')
|
||||
|
||||
function classifyScriptPubKey(script) {
|
||||
if (isPubkeyhash.call(script)) {
|
||||
return 'pubkeyhash'
|
||||
} else if (isPubkey.call(script)) {
|
||||
return 'pubkey'
|
||||
} else if (isScripthash.call(script)) {
|
||||
return 'scripthash'
|
||||
} else if (isMultisig.call(script)) {
|
||||
return 'multisig'
|
||||
} else if (isNulldata.call(script)) {
|
||||
return 'nulldata'
|
||||
} else {
|
||||
return 'nonstandard'
|
||||
}
|
||||
}
|
||||
|
||||
function classifyScriptSig(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])) {
|
||||
return 'pubkeyhash'
|
||||
} else if (script.chunks[0] == opcodes.OP_0 && script.chunks.slice(1).reduce(function(t, chunk, i) {
|
||||
return t && Buffer.isBuffer(chunk) && (chunk[0] == 48 || i == script.chunks.length - 1)
|
||||
}, true)) {
|
||||
return 'multisig'
|
||||
} else {
|
||||
return 'nonstandard'
|
||||
}
|
||||
}
|
||||
|
||||
function isPubkeyhash() {
|
||||
return this.chunks.length == 5 &&
|
||||
this.chunks[0] == opcodes.OP_DUP &&
|
||||
this.chunks[1] == opcodes.OP_HASH160 &&
|
||||
Buffer.isBuffer(this.chunks[2]) &&
|
||||
this.chunks[2].length === 20 &&
|
||||
this.chunks[3] == opcodes.OP_EQUALVERIFY &&
|
||||
this.chunks[4] == opcodes.OP_CHECKSIG
|
||||
}
|
||||
|
||||
function isPubkey() {
|
||||
return this.chunks.length === 2 &&
|
||||
Buffer.isBuffer(this.chunks[0]) &&
|
||||
this.chunks[1] === opcodes.OP_CHECKSIG
|
||||
}
|
||||
|
||||
function isScripthash() {
|
||||
return this.chunks[this.chunks.length - 1] == opcodes.OP_EQUAL &&
|
||||
this.chunks[0] == opcodes.OP_HASH160 &&
|
||||
Buffer.isBuffer(this.chunks[1]) &&
|
||||
this.chunks[1].length === 20 &&
|
||||
this.chunks.length == 3
|
||||
}
|
||||
|
||||
function isMultisig() {
|
||||
return this.chunks.length > 3 &&
|
||||
// m is a smallint
|
||||
isSmallIntOp(this.chunks[0]) &&
|
||||
// n is a smallint
|
||||
isSmallIntOp(this.chunks[this.chunks.length - 2]) &&
|
||||
// n greater or equal to m
|
||||
this.chunks[0] <= this.chunks[this.chunks.length - 2] &&
|
||||
// n cannot be 0
|
||||
this.chunks[this.chunks.length - 2] !== opcodes.OP_0 &&
|
||||
// n is the size of chunk length minus 3 (m, n, OP_CHECKMULTISIG)
|
||||
this.chunks.length - 3 === this.chunks[this.chunks.length - 2] - opcodes.OP_RESERVED &&
|
||||
// last chunk is OP_CHECKMULTISIG
|
||||
this.chunks[this.chunks.length - 1] == opcodes.OP_CHECKMULTISIG
|
||||
}
|
||||
|
||||
function isNulldata() {
|
||||
return this.chunks[0] === opcodes.OP_RETURN
|
||||
}
|
||||
|
||||
function isSmallIntOp(opcode) {
|
||||
return ((opcode == opcodes.OP_0) || ((opcode >= opcodes.OP_1) && (opcode <= opcodes.OP_16)))
|
||||
}
|
||||
|
||||
// Standard Script Templates
|
||||
// {pubKey} OP_CHECKSIG
|
||||
function createPubKeyScriptPubKey(pubKey) {
|
||||
return Script.fromChunks([
|
||||
pubKey.toBuffer(),
|
||||
opcodes.OP_CHECKSIG
|
||||
])
|
||||
}
|
||||
|
||||
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
|
||||
function createPubKeyHashScriptPubKey(hash) {
|
||||
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
|
||||
|
||||
return Script.fromChunks([
|
||||
opcodes.OP_DUP,
|
||||
opcodes.OP_HASH160,
|
||||
hash,
|
||||
opcodes.OP_EQUALVERIFY,
|
||||
opcodes.OP_CHECKSIG
|
||||
])
|
||||
}
|
||||
|
||||
// OP_HASH160 {scriptHash} OP_EQUAL
|
||||
function createP2SHScriptPubKey(hash) {
|
||||
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
|
||||
|
||||
return Script.fromChunks([
|
||||
opcodes.OP_HASH160,
|
||||
hash,
|
||||
opcodes.OP_EQUAL
|
||||
])
|
||||
}
|
||||
|
||||
// m [pubKeys ...] n OP_CHECKMULTISIG
|
||||
function createMultisigScriptPubKey(m, pubKeys) {
|
||||
assert(Array.isArray(pubKeys), 'Expected Array, got ' + pubKeys)
|
||||
assert(pubKeys.length >= m, 'Not enough pubKeys provided')
|
||||
|
||||
var pubKeyBuffers = pubKeys.map(function(pubKey) {
|
||||
return pubKey.toBuffer()
|
||||
})
|
||||
var n = pubKeys.length
|
||||
|
||||
return Script.fromChunks([].concat(
|
||||
(opcodes.OP_1 - 1) + m,
|
||||
pubKeyBuffers,
|
||||
(opcodes.OP_1 - 1) + n,
|
||||
opcodes.OP_CHECKMULTISIG
|
||||
))
|
||||
}
|
||||
|
||||
// {signature}
|
||||
function createPubKeyScriptSig(signature) {
|
||||
assert(Buffer.isBuffer(signature), 'Expected Buffer, got ' + signature)
|
||||
|
||||
return Script.fromChunks(signature)
|
||||
}
|
||||
|
||||
// {signature} {pubKey}
|
||||
function createPubKeyHashScriptSig(signature, pubKey) {
|
||||
assert(Buffer.isBuffer(signature), 'Expected Buffer, got ' + signature)
|
||||
|
||||
return Script.fromChunks([signature, pubKey.toBuffer()])
|
||||
}
|
||||
|
||||
// <scriptSig> {serialized scriptPubKey script}
|
||||
function createP2SHScriptSig(scriptSig, scriptPubKey) {
|
||||
return Script.fromChunks([].concat(
|
||||
scriptSig.chunks,
|
||||
scriptPubKey.toBuffer()
|
||||
))
|
||||
}
|
||||
|
||||
// OP_0 [signatures ...]
|
||||
function createMultisigScriptSig(signatures, scriptPubKey) {
|
||||
if (scriptPubKey) {
|
||||
assert(isMultisig.call(scriptPubKey))
|
||||
|
||||
var m = scriptPubKey.chunks[0]
|
||||
var k = m - (opcodes.OP_1 - 1)
|
||||
assert(k <= signatures.length, 'Not enough signatures provided')
|
||||
}
|
||||
|
||||
return Script.fromChunks([].concat(opcodes.OP_0, signatures))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
classifyScriptPubKey: classifyScriptPubKey,
|
||||
classifyScriptSig: classifyScriptSig,
|
||||
createMultisigScriptPubKey: createMultisigScriptPubKey,
|
||||
createMultisigScriptSig: createMultisigScriptSig,
|
||||
createP2SHScriptPubKey: createP2SHScriptPubKey,
|
||||
createP2SHScriptSig: createP2SHScriptSig,
|
||||
createPubKeyHashScriptPubKey: createPubKeyHashScriptPubKey,
|
||||
createPubKeyHashScriptSig: createPubKeyHashScriptSig,
|
||||
createPubKeyScriptPubKey: createPubKeyScriptPubKey,
|
||||
createPubKeyScriptSig: createPubKeyScriptSig
|
||||
}
|
|
@ -5,10 +5,11 @@ var bufferutils = require('./bufferutils')
|
|||
var crypto = require('./crypto')
|
||||
var ecdsa = require('./ecdsa')
|
||||
var opcodes = require('./opcodes')
|
||||
var templates = require('./templates')
|
||||
|
||||
var Address = require('./address')
|
||||
var Script = require('./script')
|
||||
var ECKey = require('./eckey')
|
||||
var Script = require('./script')
|
||||
|
||||
var DEFAULT_SEQUENCE = 0xffffffff
|
||||
|
||||
|
@ -345,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 = Script.createPubKeyHashScriptSig(signature, key.pub)
|
||||
var scriptSig = templates.createPubKeyHashScriptSig(signature, key.pub)
|
||||
this.setScriptSig(index, scriptSig)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue