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