Browse Source

Merge pull request #322 from bitcoinjs/scriptexp

Script Is* functions
hk-custom-address
Wei Lu 10 years ago
parent
commit
b1cb56ce00
  1. 8
      src/address.js
  2. 35
      src/scripts.js
  3. 24
      test/address.js
  4. 34
      test/fixtures/address.json
  5. 87
      test/fixtures/scripts.json
  6. 2
      test/integration/advanced.js
  7. 218
      test/scripts.js

8
src/address.js

@ -35,12 +35,10 @@ Address.fromBase58Check = function(string) {
Address.fromOutputScript = function(script, network) { Address.fromOutputScript = function(script, network) {
network = network || networks.bitcoin network = network || networks.bitcoin
var type = scripts.classifyOutput(script) if (scripts.isPubKeyHashOutput(script)) return new Address(script.chunks[2], network.pubKeyHash)
if (scripts.isScriptHashOutput(script)) return new Address(script.chunks[1], network.scriptHash)
if (type === 'pubkeyhash') return new Address(script.chunks[2], network.pubKeyHash) assert(false, script.toASM() + ' has no matching Address')
if (type === 'scripthash') return new Address(script.chunks[1], network.scriptHash)
assert(false, type + ' has no matching Address')
} }
// Export functions // Export functions

35
src/scripts.js

@ -112,7 +112,7 @@ function isMultisigOutput(script) {
return pubKeys.every(isCanonicalPubKey) return pubKeys.every(isCanonicalPubKey)
} }
function isNulldataOutput(script) { function isNullDataOutput(script) {
return script.chunks[0] === ops.OP_RETURN return script.chunks[0] === ops.OP_RETURN
} }
@ -127,7 +127,7 @@ function classifyOutput(script) {
return 'multisig' return 'multisig'
} else if (isPubKeyOutput(script)) { } else if (isPubKeyOutput(script)) {
return 'pubkey' return 'pubkey'
} else if (isNulldataOutput(script)) { } else if (isNullDataOutput(script)) {
return 'nulldata' return 'nulldata'
} }
@ -241,20 +241,35 @@ function multisigInput(signatures, scriptPubKey) {
return Script.fromChunks([].concat(ops.OP_0, signatures)) return Script.fromChunks([].concat(ops.OP_0, signatures))
} }
function dataOutput(data) { function nullDataOutput(data) {
return Script.fromChunks([ops.OP_RETURN, data]) return Script.fromChunks([ops.OP_RETURN, data])
} }
module.exports = { module.exports = {
classifyInput: classifyInput, isCanonicalPubKey: isCanonicalPubKey,
isCanonicalSignature: isCanonicalSignature,
isPubKeyHashInput: isPubKeyHashInput,
isPubKeyHashOutput: isPubKeyHashOutput,
isPubKeyInput: isPubKeyInput,
isPubKeyOutput: isPubKeyOutput,
isScriptHashInput: isScriptHashInput,
isScriptHashOutput: isScriptHashOutput,
isMultisigInput: isMultisigInput,
isMultisigOutput: isMultisigOutput,
isNullDataOutput: isNullDataOutput,
classifyOutput: classifyOutput, classifyOutput: classifyOutput,
dataOutput: dataOutput, classifyInput: classifyInput,
multisigInput: multisigInput, pubKeyOutput: pubKeyOutput,
multisigOutput: multisigOutput,
pubKeyHashInput: pubKeyHashInput,
pubKeyHashOutput: pubKeyHashOutput, pubKeyHashOutput: pubKeyHashOutput,
scriptHashOutput: scriptHashOutput,
multisigOutput: multisigOutput,
pubKeyInput: pubKeyInput, pubKeyInput: pubKeyInput,
pubKeyOutput: pubKeyOutput, pubKeyHashInput: pubKeyHashInput,
scriptHashInput: scriptHashInput, scriptHashInput: scriptHashInput,
scriptHashOutput: scriptHashOutput multisigInput: multisigInput,
dataOutput: function(data) {
console.warn('dataOutput is deprecated, use nullDataOutput by 2.0.0')
return nullDataOutput(data)
},
nullDataOutput: nullDataOutput
} }

24
test/address.js

@ -10,22 +10,22 @@ describe('Address', function() {
describe('Constructor', function() { describe('Constructor', function() {
it('does not mutate the input', function() { it('does not mutate the input', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
var hash = new Buffer(f.hex, 'hex') var hash = new Buffer(f.hash, 'hex')
var addr = new Address(hash, f.version) var addr = new Address(hash, f.version)
assert.equal(addr.version, f.version) assert.equal(addr.version, f.version)
assert.equal(addr.hash.toString('hex'), f.hex) assert.equal(addr.hash.toString('hex'), f.hash)
}) })
}) })
}) })
describe('fromBase58Check', function() { describe('fromBase58Check', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() { it('imports ' + f.script + ' (' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check) var addr = Address.fromBase58Check(f.base58check)
assert.equal(addr.version, f.version) assert.equal(addr.version, f.version)
assert.equal(addr.hash.toString('hex'), f.hex) assert.equal(addr.hash.toString('hex'), f.hash)
}) })
}) })
@ -40,18 +40,18 @@ describe('Address', function() {
describe('fromOutputScript', function() { describe('fromOutputScript', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() { it('imports ' + f.script + ' (' + f.network + ') correctly', function() {
var script = Script.fromHex(f.script) var script = Script.fromASM(f.script)
var addr = Address.fromOutputScript(script, networks[f.network]) var addr = Address.fromOutputScript(script, networks[f.network])
assert.equal(addr.version, f.version) assert.equal(addr.version, f.version)
assert.equal(addr.hash.toString('hex'), f.hex) assert.equal(addr.hash.toString('hex'), f.hash)
}) })
}) })
fixtures.invalid.fromOutputScript.forEach(function(f) { fixtures.invalid.fromOutputScript.forEach(function(f) {
it('throws when ' + f.description, function() { it('throws when ' + f.description, function() {
var script = Script.fromHex(f.hex) var script = Script.fromASM(f.script)
assert.throws(function() { assert.throws(function() {
Address.fromOutputScript(script) Address.fromOutputScript(script)
@ -62,7 +62,7 @@ describe('Address', function() {
describe('toBase58Check', function() { describe('toBase58Check', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('exports ' + f.description + '(' + f.network + ') correctly', function() { it('exports ' + f.script + ' (' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check) var addr = Address.fromBase58Check(f.base58check)
var result = addr.toBase58Check() var result = addr.toBase58Check()
@ -73,17 +73,17 @@ describe('Address', function() {
describe('toOutputScript', function() { describe('toOutputScript', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('imports ' + f.description + '(' + f.network + ') correctly', function() { it('imports ' + f.script + ' (' + f.network + ') correctly', function() {
var addr = Address.fromBase58Check(f.base58check) var addr = Address.fromBase58Check(f.base58check)
var script = addr.toOutputScript() var script = addr.toOutputScript()
assert.equal(script.toHex(), f.script) assert.equal(script.toASM(), f.script)
}) })
}) })
fixtures.invalid.toOutputScript.forEach(function(f) { fixtures.invalid.toOutputScript.forEach(function(f) {
it('throws when ' + f.description, function() { it('throws when ' + f.description, function() {
var addr = new Address(new Buffer(f.hex, 'hex'), f.version) var addr = new Address(new Buffer(f.hash, 'hex'), f.version)
assert.throws(function() { assert.throws(function() {
addr.toOutputScript() addr.toOutputScript()

34
test/fixtures/address.json

@ -1,36 +1,32 @@
{ {
"valid": [ "valid": [
{ {
"description": "pubKeyHash",
"network": "bitcoin", "network": "bitcoin",
"version": 0, "version": 0,
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6", "hash": "751e76e8199196d454941c45d1b3a323f1433bd6",
"base58check": "1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH", "base58check": "1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH",
"script": "76a914751e76e8199196d454941c45d1b3a323f1433bd688ac" "script": "OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG"
}, },
{ {
"description": "scriptHash",
"network": "bitcoin", "network": "bitcoin",
"version": 5, "version": 5,
"hex": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6", "hash": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6",
"base58check": "3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr", "base58check": "3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr",
"script": "a914cd7b44d0b03f2d026d1e586d7ae18903b0d385f687" "script": "OP_HASH160 cd7b44d0b03f2d026d1e586d7ae18903b0d385f6 OP_EQUAL"
}, },
{ {
"description": "pubKeyHash",
"network": "testnet", "network": "testnet",
"version": 111, "version": 111,
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6", "hash": "751e76e8199196d454941c45d1b3a323f1433bd6",
"base58check": "mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r", "base58check": "mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r",
"script": "76a914751e76e8199196d454941c45d1b3a323f1433bd688ac" "script": "OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG"
}, },
{ {
"description": "scriptHash",
"network": "testnet", "network": "testnet",
"version": 196, "version": 196,
"hex": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6", "hash": "cd7b44d0b03f2d026d1e586d7ae18903b0d385f6",
"base58check": "2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w", "base58check": "2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w",
"script": "a914cd7b44d0b03f2d026d1e586d7ae18903b0d385f687" "script": "OP_HASH160 cd7b44d0b03f2d026d1e586d7ae18903b0d385f6 OP_EQUAL"
} }
], ],
"invalid": { "invalid": {
@ -48,22 +44,22 @@
], ],
"fromOutputScript": [ "fromOutputScript": [
{ {
"description": "pubkey has no matching Address", "description": "has no matching Address",
"hex": "21031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95ac" "script": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG"
}, },
{ {
"description": "multisig has no matching Address", "description": "has no matching Address",
"hex": "5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae" "script": "OP_TRUE 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG"
}, },
{ {
"description": "nulldata has no matching Address", "description": "has no matching Address",
"hex": "6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474" "script": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474"
} }
], ],
"toOutputScript": [ "toOutputScript": [
{ {
"description": "24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE has no matching Script", "description": "24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE has no matching Script",
"hex": "751e76e8199196d454941c45d1b3a323f1433bd6", "hash": "751e76e8199196d454941c45d1b3a323f1433bd6",
"version": 153 "version": 153
} }
] ]

87
test/fixtures/scripts.json

@ -56,78 +56,71 @@
} }
], ],
"invalid": { "invalid": {
"classify": [ "isPubKeyHashInput": [
{ {
"description": "multisig output : OP_CHECKMULTISIG not found", "description": "pubKeyHash input : extraneous data",
"scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 ffffffff"
}
],
"isScriptHashInput": [
{
"description": "redeemScript not data",
"scriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501 OP_RESERVED"
}
],
"isPubKeyInput": [
{
"description": "non-canonical signature",
"scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf7593ffffffffffffffff"
}
],
"isMultisigOutput": [
{
"description": "OP_CHECKMULTISIG not found",
"scriptPubKey": "OP_0 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_2 OP_HASH160" "scriptPubKey": "OP_0 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_2 OP_HASH160"
}, },
{ {
"description": "multisig output : less than 4 chunks", "description": "less than 4 chunks",
"scriptPubKey": "OP_0 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_HASH160" "scriptPubKey": "OP_0 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_HASH160"
}, },
{ {
"description": "multisig output : m === 0", "description": "m === 0",
"scriptPubKey": "OP_0 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_2 OP_CHECKMULTISIG" "scriptPubKey": "OP_0 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_2 OP_CHECKMULTISIG"
}, },
{ {
"description": "multisig output : m < OP_1", "description": "m < OP_1",
"scriptPubKey": "OP_1NEGATE 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_2 OP_CHECKMULTISIG" "scriptPubKey": "OP_1NEGATE 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_2 OP_CHECKMULTISIG"
}, },
{ {
"description": "multisig output : m > OP_16", "description": "m > OP_16",
"scriptPubKey": "OP_NOP 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_2 OP_CHECKMULTISIG" "scriptPubKey": "OP_NOP 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_2 OP_CHECKMULTISIG"
}, },
{ {
"description": "multisig output : n === 0", "description": "n === 0",
"scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_0 OP_CHECKMULTISIG" "scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_0 OP_CHECKMULTISIG"
}, },
{ {
"description": "multisig output : n < OP_1", "description": "n < OP_1",
"scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_1NEGATE OP_CHECKMULTISIG" "scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_1NEGATE OP_CHECKMULTISIG"
}, },
{ {
"description": "multisig output : n > OP_16", "description": "n > OP_16",
"scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_NOP OP_CHECKMULTISIG" "scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_NOP OP_CHECKMULTISIG"
}, },
{ {
"description": "multisig output : n < m", "description": "n < m",
"scriptPubKey": "OP_2 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_1 OP_CHECKMULTISIG" "scriptPubKey": "OP_2 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 OP_1 OP_CHECKMULTISIG"
}, },
{ {
"description": "multisig output : n < len(pubKeys)", "description": "n < len(pubKeys)",
"scriptPubKey": "OP_2 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34 OP_2 OP_CHECKMULTISIG" "scriptPubKey": "OP_2 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34 OP_2 OP_CHECKMULTISIG"
}, },
{ {
"description": "multisig output : non-canonical pubKey (bad length)", "description": "non-canonical pubKey (bad length)",
"scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffff OP_1 OP_CHECKMULTISIG" "scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffff OP_1 OP_CHECKMULTISIG"
},
{
"description": "pubKeyHash input : extraneous data",
"scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 ffffffff"
},
{
"description": "scriptHash input : redeemScript not data",
"scriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501 OP_RESERVED"
},
{
"description": "pubKey input : non-canonical signature",
"scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf7593ffffffffffffffff"
} }
], ],
"multisig": [ "multisigInput": [
{
"exception": "Not enough pubKeys provided",
"m": 4,
"pubKeys": [
"02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f",
"02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f",
"036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19"
],
"signatures": [
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801"
],
"scriptPubKey": true
},
{ {
"exception": "Not enough signatures provided", "exception": "Not enough signatures provided",
"pubKeys": [ "pubKeys": [
@ -136,8 +129,7 @@
], ],
"signatures": [ "signatures": [
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801" "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801"
], ]
"scriptPubKey": false
}, },
{ {
"exception": "Too many signatures provided", "exception": "Too many signatures provided",
@ -149,8 +141,21 @@
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
"3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501", "3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501",
"3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501" "3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501"
]
}
],
"multisigOutput": [
{
"exception": "Not enough pubKeys provided",
"m": 4,
"pubKeys": [
"02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f",
"02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f",
"036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19"
], ],
"scriptPubKey": false "signatures": [
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801"
]
} }
] ]
} }

2
test/integration/advanced.js

@ -78,7 +78,7 @@ describe('bitcoinjs-lib (advanced)', function() {
var tx = new bitcoin.TransactionBuilder() var tx = new bitcoin.TransactionBuilder()
var data = new Buffer('cafedeadbeef', 'hex') var data = new Buffer('cafedeadbeef', 'hex')
var dataScript = bitcoin.scripts.dataOutput(data) var dataScript = bitcoin.scripts.nullDataOutput(data)
tx.addInput(unspent.txHash, unspent.index) tx.addInput(unspent.txHash, unspent.index)
tx.addOutput(dataScript, 1000) tx.addOutput(dataScript, 1000)

218
test/scripts.js

@ -1,13 +1,16 @@
var assert = require('assert') var assert = require('assert')
var scripts = require('../src/scripts') var scripts = require('../src/scripts')
var Address = require('../src/address')
var ECPubKey = require('../src/ecpubkey') var ECPubKey = require('../src/ecpubkey')
var Script = require('../src/script') var Script = require('../src/script')
var fixtures = require('./fixtures/scripts.json') var fixtures = require('./fixtures/scripts.json')
describe('Scripts', function() { describe('Scripts', function() {
// TODO
describe.skip('isCanonicalPubKey', function() {})
describe.skip('isCanonicalSignature', function() {})
describe('classifyInput', function() { describe('classifyInput', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
if (!f.scriptSig) return if (!f.scriptSig) return
@ -19,17 +22,6 @@ describe('Scripts', function() {
assert.equal(type, f.type) assert.equal(type, f.type)
}) })
}) })
fixtures.invalid.classify.forEach(function(f) {
if (!f.scriptSig) return
it('returns nonstandard for ' + f.description, function() {
var script = Script.fromASM(f.scriptSig)
var type = scripts.classifyInput(script)
assert.equal(type, 'nonstandard')
})
})
}) })
describe('classifyOutput', function() { describe('classifyOutput', function() {
@ -43,159 +35,187 @@ describe('Scripts', function() {
assert.equal(type, f.type) assert.equal(type, f.type)
}) })
}) })
})
fixtures.invalid.classify.forEach(function(f) { ;['PubKey', 'PubKeyHash', 'ScriptHash', 'Multisig', 'NullData'].forEach(function(type) {
if (!f.scriptPubKey) return var inputFn = scripts['is' + type + 'Input']
var outputFn= scripts['is' + type + 'Output']
it('returns nonstandard for ' + f.description, function() { describe('is' + type + 'Input', function() {
var script = Script.fromASM(f.scriptPubKey) fixtures.valid.forEach(function(f) {
var type = scripts.classifyOutput(script) var expected = type.toLowerCase() === f.type
if (inputFn && f.scriptSig) {
it('returns ' + expected + ' for ' + f.scriptSig, function() {
var script = Script.fromASM(f.scriptSig)
assert.equal(inputFn(script), expected)
})
}
})
})
assert.equal(type, 'nonstandard') describe('is' + type + 'Output', function() {
fixtures.valid.forEach(function(f) {
var expected = type.toLowerCase() === f.type
if (outputFn && f.scriptPubKey) {
it('returns ' + expected + ' for ' + f.scriptPubKey, function() {
var script = Script.fromASM(f.scriptPubKey)
assert.equal(outputFn(script), expected)
})
}
}) })
}) })
}) })
describe('pubKey', function() { describe('pubKeyInput', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
if (f.type !== 'pubkey') return if (f.type !== 'pubkey') return
describe('input script', function() { it('returns ' + f.scriptSig, function() {
it('is generated correctly for ' + f.pubKey, function() { var signature = new Buffer(f.signature, 'hex')
var signature = new Buffer(f.signature, 'hex')
var scriptSig = scripts.pubKeyInput(signature) var scriptSig = scripts.pubKeyInput(signature)
assert.equal(scriptSig.toASM(), f.scriptSig) assert.equal(scriptSig.toASM(), f.scriptSig)
})
}) })
})
})
describe('output script', function() { describe('pubKeyOutput', function() {
it('is generated correctly for ' + f.pubKey, function() { fixtures.valid.forEach(function(f) {
var pubKey = ECPubKey.fromHex(f.pubKey) if (f.type !== 'pubkey') return
var scriptPubKey = scripts.pubKeyOutput(pubKey) it('returns ' + f.scriptPubKey, function() {
assert.equal(scriptPubKey.toASM(), f.scriptPubKey) var pubKey = ECPubKey.fromHex(f.pubKey)
})
var scriptPubKey = scripts.pubKeyOutput(pubKey)
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
}) })
}) })
}) })
describe('pubKeyHash', function() { describe('pubKeyHashInput', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
if (f.type !== 'pubkeyhash') return if (f.type !== 'pubkeyhash') return
var pubKey = ECPubKey.fromHex(f.pubKey) var pubKey = ECPubKey.fromHex(f.pubKey)
var address = pubKey.getAddress()
describe('input script', function() { it('returns ' + f.scriptSig, function() {
it('is generated correctly for ' + address, function() { var signature = new Buffer(f.signature, 'hex')
var signature = new Buffer(f.signature, 'hex')
var scriptSig = scripts.pubKeyHashInput(signature, pubKey) var scriptSig = scripts.pubKeyHashInput(signature, pubKey)
assert.equal(scriptSig.toASM(), f.scriptSig) assert.equal(scriptSig.toASM(), f.scriptSig)
})
}) })
})
})
describe('output script', function() { describe('pubKeyHashOutput', function() {
it('is generated correctly for ' + address, function() { fixtures.valid.forEach(function(f) {
var scriptPubKey = scripts.pubKeyHashOutput(address.hash) if (f.type !== 'pubkeyhash') return
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
}) var pubKey = ECPubKey.fromHex(f.pubKey)
var address = pubKey.getAddress()
it('returns ' + f.scriptPubKey, function() {
var scriptPubKey = scripts.pubKeyHashOutput(address.hash)
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
}) })
}) })
}) })
describe('multisig', function() { describe('multisigInput', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
if (f.type !== 'multisig') return if (f.type !== 'multisig') return
it('returns ' + f.scriptSig, function() {
var signatures = f.signatures.map(function(signature) {
return new Buffer(signature, 'hex')
})
var scriptSig = scripts.multisigInput(signatures)
assert.equal(scriptSig.toASM(), f.scriptSig)
})
})
fixtures.invalid.multisigInput.forEach(function(f) {
var pubKeys = f.pubKeys.map(ECPubKey.fromHex) var pubKeys = f.pubKeys.map(ECPubKey.fromHex)
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys) var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
describe('input script', function() { it('throws on ' + f.exception, function() {
it('is generated correctly for ' + f.scriptPubKey, function() { var signatures = f.signatures.map(function(signature) {
var signatures = f.signatures.map(function(signature) { return new Buffer(signature, 'hex')
return new Buffer(signature, 'hex')
})
var scriptSig = scripts.multisigInput(signatures)
assert.equal(scriptSig.toASM(), f.scriptSig)
}) })
})
describe('output script', function() { assert.throws(function() {
it('is generated correctly for ' + f.scriptPubKey, function() { scripts.multisigInput(signatures, scriptPubKey)
assert.equal(scriptPubKey.toASM(), f.scriptPubKey) }, new RegExp(f.exception))
})
}) })
}) })
})
describe('multisigOutput', function() {
fixtures.valid.forEach(function(f) {
if (f.type !== 'multisig') return
fixtures.invalid.multisig.forEach(function(f) {
var pubKeys = f.pubKeys.map(ECPubKey.fromHex) var pubKeys = f.pubKeys.map(ECPubKey.fromHex)
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys) var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
if (f.scriptPubKey) { it('returns ' + f.scriptPubKey, function() {
describe('output script', function() { assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
it('throws on ' + f.exception, function() { })
assert.throws(function() { })
scripts.multisigOutput(f.m, pubKeys)
}, new RegExp(f.exception)) fixtures.invalid.multisigOutput.forEach(function(f) {
}) var pubKeys = f.pubKeys.map(function(p) { return new Buffer(p, 'hex') })
})
} else { it('throws on ' + f.exception, function() {
describe('input script', function() { assert.throws(function() {
it('throws on ' + f.exception, function() { scripts.multisigOutput(f.m, pubKeys)
var signatures = f.signatures.map(function(signature) { }, new RegExp(f.exception))
return new Buffer(signature, 'hex') })
})
assert.throws(function() {
scripts.multisigInput(signatures, scriptPubKey)
}, new RegExp(f.exception))
})
})
}
}) })
}) })
describe('scripthash', function() { describe('scriptHashInput', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
if (f.type !== 'scripthash') return if (f.type !== 'scripthash') return
var redeemScript = Script.fromASM(f.redeemScript) var redeemScript = Script.fromASM(f.redeemScript)
var redeemScriptSig = Script.fromASM(f.redeemScriptSig) var redeemScriptSig = Script.fromASM(f.redeemScriptSig)
var address = Address.fromOutputScript(Script.fromASM(f.scriptPubKey)) it('returns ' + f.scriptSig, function() {
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
describe('input script', function() { assert.equal(scriptSig.toASM(), f.scriptSig)
it('is generated correctly for ' + address, function() {
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
assert.equal(scriptSig.toASM(), f.scriptSig)
})
}) })
})
})
describe('output script', function() { describe('scriptHashOutput', function() {
it('is generated correctly for ' + address, function() { fixtures.valid.forEach(function(f) {
var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash()) if (f.type !== 'scripthash') return
assert.equal(scriptPubKey.toASM(), f.scriptPubKey) var redeemScript = Script.fromASM(f.redeemScript)
})
it('returns ' + f.scriptPubKey, function() {
var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash())
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
}) })
}) })
}) })
describe('data', function() { describe('nullDataOutput', function() {
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
if (f.type !== 'nulldata') return if (f.type !== 'nulldata') return
var data = new Buffer(f.data, 'hex') var data = new Buffer(f.data, 'hex')
var scriptPubKey = scripts.dataOutput(data) var scriptPubKey = scripts.nullDataOutput(data)
describe('output script', function() { it('returns ' + f.scriptPubKey, function() {
it('is generated correctly for ' + f.scriptPubKey, function() { assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
})
}) })
}) })
}) })

Loading…
Cancel
Save