bitcoinjs-lib/test/scripts.js

175 lines
5.2 KiB
JavaScript
Raw Normal View History

var assert = require('assert')
2014-06-13 01:58:52 +02:00
var scripts = require('../src/scripts')
var Address = require('../src/address')
var ECPubKey = require('../src/ecpubkey')
var Script = require('../src/script')
2014-06-24 09:41:08 +02:00
var fixtures = require('./fixtures/scripts.json')
2014-06-13 01:58:52 +02:00
describe('Scripts', function() {
describe('classifyInput', function() {
2014-06-24 09:41:08 +02:00
fixtures.valid.forEach(function(f) {
if (!f.scriptSig) return
it('classifies ' + f.scriptSig + ' as ' + f.type, function() {
2014-06-25 07:44:15 +02:00
var script = Script.fromASM(f.scriptSig)
2014-06-13 01:58:52 +02:00
var type = scripts.classifyInput(script)
assert.equal(type, f.type)
})
})
})
2014-06-13 01:58:52 +02:00
describe('classifyOutput', function() {
2014-06-24 09:41:08 +02:00
fixtures.valid.forEach(function(f) {
if (!f.scriptPubKey) return
it('classifies ' + f.scriptPubKey + ' as ' + f.type, function() {
2014-06-25 07:44:15 +02:00
var script = Script.fromASM(f.scriptPubKey)
2014-06-13 01:58:52 +02:00
var type = scripts.classifyOutput(script)
assert.equal(type, f.type)
})
})
fixtures.invalid.classify.forEach(function(f) {
it('returns nonstandard for ' + f.description, function() {
var script = Script.fromASM(f.scriptPubKey)
var type = scripts.classifyOutput(script)
assert.equal(type, 'nonstandard')
})
})
})
2014-06-14 15:30:34 +02:00
describe('pubKey', function() {
2014-06-24 09:41:08 +02:00
fixtures.valid.forEach(function(f) {
if (f.type !== 'pubkey') return
2014-06-14 15:30:34 +02:00
describe('input script', function() {
it('is generated correctly for ' + f.pubKey, function() {
var signature = new Buffer(f.signature, 'hex')
2014-06-14 15:30:34 +02:00
var scriptSig = scripts.pubKeyInput(signature)
2014-06-25 07:44:15 +02:00
assert.equal(scriptSig.toASM(), f.scriptSig)
2014-06-14 15:30:34 +02:00
})
})
2014-06-14 15:30:34 +02:00
describe('output script', function() {
it('is generated correctly for ' + f.pubKey, function() {
var pubKey = ECPubKey.fromHex(f.pubKey)
2014-06-14 15:30:34 +02:00
var scriptPubKey = scripts.pubKeyOutput(pubKey)
2014-06-25 07:44:15 +02:00
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
2014-06-14 15:30:34 +02:00
})
})
})
})
2014-06-14 15:30:34 +02:00
describe('pubKeyHash', function() {
2014-06-24 09:41:08 +02:00
fixtures.valid.forEach(function(f) {
if (f.type !== 'pubkeyhash') return
2014-06-14 15:30:34 +02:00
var pubKey = ECPubKey.fromHex(f.pubKey)
var address = pubKey.getAddress()
2014-06-14 15:30:34 +02:00
describe('input script', function() {
it('is generated correctly for ' + address, function() {
var signature = new Buffer(f.signature, 'hex')
2014-06-14 15:30:34 +02:00
var scriptSig = scripts.pubKeyHashInput(signature, pubKey)
2014-06-25 07:44:15 +02:00
assert.equal(scriptSig.toASM(), f.scriptSig)
2014-06-14 15:30:34 +02:00
})
})
2014-06-14 15:30:34 +02:00
describe('output script', function() {
it('is generated correctly for ' + address, function() {
var scriptPubKey = scripts.pubKeyHashOutput(address.hash)
2014-06-25 07:44:15 +02:00
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
2014-06-14 15:30:34 +02:00
})
})
})
2014-06-14 15:30:34 +02:00
})
2014-06-14 15:30:34 +02:00
describe('multisig', function() {
2014-06-24 09:41:08 +02:00
fixtures.valid.forEach(function(f) {
if (f.type !== 'multisig') return
2014-06-14 15:30:34 +02:00
var pubKeys = f.pubKeys.map(ECPubKey.fromHex)
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
2014-06-25 07:20:28 +02:00
describe('input script', function() {
it('is generated correctly for ' + f.scriptPubKey, function() {
2014-06-25 07:20:28 +02:00
var signatures = f.signatures.map(function(signature) {
return new Buffer(signature, 'hex')
2014-06-14 15:30:34 +02:00
})
2014-06-25 07:20:28 +02:00
var scriptSig = scripts.multisigInput(signatures)
2014-06-25 07:44:15 +02:00
assert.equal(scriptSig.toASM(), f.scriptSig)
2014-06-14 15:30:34 +02:00
})
2014-06-25 07:20:28 +02:00
})
2014-06-14 15:30:34 +02:00
describe('output script', function() {
it('is generated correctly for ' + f.scriptPubKey, function() {
2014-06-25 07:44:15 +02:00
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
2014-06-14 15:30:34 +02:00
})
})
})
2014-06-24 09:41:08 +02:00
fixtures.invalid.multisig.forEach(function(f) {
2014-06-14 15:30:34 +02:00
var pubKeys = f.pubKeys.map(ECPubKey.fromHex)
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
if (f.scriptPubKey) {
describe('output script', function() {
it('throws on ' + f.exception, function() {
assert.throws(function() {
scripts.multisigOutput(f.m, pubKeys)
}, new RegExp(f.exception))
})
})
} else {
describe('input script', function() {
it('throws on ' + f.exception, function() {
var signatures = f.signatures.map(function(signature) {
return new Buffer(signature, 'hex')
})
assert.throws(function() {
scripts.multisigInput(signatures, scriptPubKey)
}, new RegExp(f.exception))
})
})
}
})
})
2014-06-14 15:30:34 +02:00
describe('scripthash', function() {
2014-06-24 09:41:08 +02:00
fixtures.valid.forEach(function(f) {
if (f.type !== 'scripthash') return
2014-06-25 07:44:15 +02:00
var redeemScript = Script.fromASM(f.redeemScript)
var redeemScriptSig = Script.fromASM(f.redeemScriptSig)
2014-06-25 07:44:15 +02:00
var address = Address.fromOutputScript(Script.fromASM(f.scriptPubKey))
2014-06-14 15:30:34 +02:00
describe('input script', function() {
it('is generated correctly for ' + address, function() {
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
2014-06-25 07:44:15 +02:00
assert.equal(scriptSig.toASM(), f.scriptSig)
2014-06-14 15:30:34 +02:00
})
})
describe('output script', function() {
it('is generated correctly for ' + address, function() {
var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash())
2014-06-25 07:44:15 +02:00
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
2014-06-14 15:30:34 +02:00
})
})
})
})
})