2014-03-09 06:43:36 +01:00
|
|
|
var Script = require('../src/script.js')
|
|
|
|
var assert = require('assert')
|
2014-03-25 06:32:43 +01:00
|
|
|
var Address = require('../src/address.js')
|
2014-03-29 15:13:39 +01:00
|
|
|
var Network = require('../src/network.js')
|
2014-04-08 14:00:28 +02:00
|
|
|
var crypto = require('../').crypto
|
2014-03-25 06:32:43 +01:00
|
|
|
var Convert = require('../src/convert.js')
|
2014-03-31 05:47:47 +02:00
|
|
|
var bytesToHex = Convert.bytesToHex
|
|
|
|
var hexToBytes = Convert.hexToBytes
|
2014-03-09 06:43:36 +01:00
|
|
|
|
|
|
|
describe('Script', function() {
|
2014-03-22 14:01:40 +01:00
|
|
|
var p2shScriptPubKey, pubkeyScriptPubkey, addressScriptSig
|
|
|
|
|
|
|
|
beforeEach(function(){
|
|
|
|
p2shScriptPubKey = "a914e8c300c87986efa84c37c0519929019ef86eb5b487"
|
|
|
|
pubkeyScriptPubKey = "76a9145a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a888ac"
|
|
|
|
addressScriptSig = "48304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f30141040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8"
|
2014-04-13 23:32:22 +02:00
|
|
|
|
2014-04-14 06:25:48 +02:00
|
|
|
// txid: 09dd94f2c85262173da87a745a459007bb1eed6eeb6bfa238a0cd91a16cf7790
|
2014-04-13 23:32:22 +02:00
|
|
|
validMultisigScript = '5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae'
|
|
|
|
|
2014-04-14 06:25:48 +02:00
|
|
|
// txid: 5e9be7fb36ee49ce84bee4c8ef38ad0efc0608b78dae1c2c99075297ef527890
|
|
|
|
opreturnScript = '6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474'
|
|
|
|
|
2014-04-13 23:32:22 +02:00
|
|
|
// asm: "0 0 0 OP_CHECKMULTISIG"
|
|
|
|
invalidMultisigScript = '000000ae'
|
|
|
|
|
2014-04-14 06:25:48 +02:00
|
|
|
// txid: a4bfa8ab6435ae5f25dae9d89e4eb67dfa94283ca751f393c1ddc5a837bbc31b
|
|
|
|
nonStandardScript = 'aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087'
|
2014-03-22 14:01:40 +01:00
|
|
|
})
|
|
|
|
|
2014-03-09 06:43:36 +01:00
|
|
|
describe('constructor', function() {
|
|
|
|
it('works for a byte array', function() {
|
|
|
|
assert.ok(new Script([]))
|
|
|
|
})
|
|
|
|
|
2014-03-17 09:13:49 +01:00
|
|
|
it('works when nothing is passed in', function() {
|
|
|
|
assert.ok(new Script())
|
|
|
|
})
|
|
|
|
|
2014-03-09 06:43:36 +01:00
|
|
|
it('throws an error when input is not an array', function() {
|
2014-03-17 09:13:49 +01:00
|
|
|
assert.throws(function(){ new Script({}) })
|
2014-03-09 06:43:36 +01:00
|
|
|
})
|
|
|
|
})
|
2014-03-22 13:28:20 +01:00
|
|
|
|
|
|
|
describe('getOutType', function() {
|
2014-04-14 06:25:48 +02:00
|
|
|
it('supports p2sh', function() {
|
2014-03-22 14:01:40 +01:00
|
|
|
var script = Script.fromHex(p2shScriptPubKey)
|
2014-04-12 00:20:42 +02:00
|
|
|
assert.equal(script.getOutType(), 'scripthash')
|
2014-03-25 06:32:43 +01:00
|
|
|
})
|
|
|
|
|
2014-04-14 06:25:48 +02:00
|
|
|
it('supports pubkeyhash', function() {
|
2014-03-22 14:01:40 +01:00
|
|
|
var script = Script.fromHex(pubkeyScriptPubKey)
|
2014-04-12 00:20:42 +02:00
|
|
|
assert.equal(script.getOutType(), 'pubkeyhash')
|
2014-03-22 13:28:20 +01:00
|
|
|
})
|
2014-04-13 23:32:22 +02:00
|
|
|
|
2014-04-14 06:25:48 +02:00
|
|
|
it('supports multisig', function() {
|
2014-04-13 23:32:22 +02:00
|
|
|
var script = Script.fromHex(validMultisigScript)
|
|
|
|
assert.equal(script.getOutType(), 'multisig')
|
|
|
|
})
|
|
|
|
|
2014-04-14 06:25:48 +02:00
|
|
|
it('supports null_data', function() {
|
|
|
|
var script = Script.fromHex(opreturnScript)
|
|
|
|
assert.equal(script.getOutType(), 'nulldata')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('supports nonstandard script', function() {
|
|
|
|
var script = Script.fromHex(nonStandardScript)
|
2014-04-13 23:32:22 +02:00
|
|
|
assert.equal(script.getOutType(), 'nonstandard')
|
|
|
|
})
|
|
|
|
|
2014-04-14 06:25:48 +02:00
|
|
|
it('identifies invalid multisig script as nonstandard', function() {
|
|
|
|
var script = Script.fromHex(invalidMultisigScript)
|
|
|
|
assert.equal(script.getOutType(), 'nonstandard')
|
2014-04-13 23:32:22 +02:00
|
|
|
})
|
2014-03-22 13:28:20 +01:00
|
|
|
})
|
|
|
|
|
2014-03-22 14:01:40 +01:00
|
|
|
describe('getInType', function() {
|
|
|
|
it('works for address', function() {
|
|
|
|
var script = Script.fromHex(addressScriptSig)
|
2014-04-12 00:20:42 +02:00
|
|
|
assert.equal(script.getInType(), 'pubkeyhash')
|
2014-03-22 14:01:40 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-22 13:30:49 +01:00
|
|
|
describe('getToAddress', function() {
|
2014-03-22 13:28:20 +01:00
|
|
|
it('works for p2sh type output', function() {
|
2014-03-22 14:01:40 +01:00
|
|
|
var script = Script.fromHex(p2shScriptPubKey)
|
|
|
|
assert.equal(script.getToAddress().toString(), '3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8')
|
2014-03-22 13:28:20 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('works for pubkey type output', function() {
|
2014-03-22 14:01:40 +01:00
|
|
|
var script = Script.fromHex(pubkeyScriptPubKey)
|
|
|
|
assert.equal(script.getToAddress().toString(), '19E6FV3m3kEPoJD5Jz6dGKdKwTVvjsWUvu')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getFromAddress', function() {
|
|
|
|
it('works for address type input', function() {
|
|
|
|
var script = Script.fromHex(addressScriptSig)
|
|
|
|
assert.equal(script.getFromAddress().toString(), '1BBjuhF2jHxu7tPinyQGCuaNhEs6f5u59u')
|
2014-03-22 13:28:20 +01:00
|
|
|
})
|
2014-03-25 06:32:43 +01:00
|
|
|
})
|
2014-03-29 15:13:39 +01:00
|
|
|
|
2014-03-25 06:32:43 +01:00
|
|
|
describe('2-of-3 Multi-Signature', function() {
|
2014-03-31 05:47:47 +02:00
|
|
|
var compressedPubKeys = []
|
2014-03-28 03:56:10 +01:00
|
|
|
var numSigs, script, multisig, network
|
2014-03-25 06:32:43 +01:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
compressedPubKeys = ['02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f',
|
2014-03-31 05:47:47 +02:00
|
|
|
'02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f',
|
|
|
|
'036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19']
|
|
|
|
numSigs = 2
|
|
|
|
network = Network.mainnet.p2shVersion
|
2014-03-25 06:32:43 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should create valid multi-sig address', function() {
|
2014-03-29 15:13:39 +01:00
|
|
|
script = Script.createMultiSigOutputScript(numSigs, compressedPubKeys.map(hexToBytes))
|
2014-04-08 14:13:03 +02:00
|
|
|
multisig = crypto.hash160(script.buffer)
|
2014-03-29 15:13:39 +01:00
|
|
|
var multiSigAddress = Address(multisig, network).toString()
|
|
|
|
|
2014-03-25 06:32:43 +01:00
|
|
|
assert.ok(Address.validate(multiSigAddress))
|
2014-03-29 15:13:39 +01:00
|
|
|
assert.equal(Address.getVersion(multiSigAddress), Network.mainnet.p2shVersion)
|
2014-03-25 06:32:43 +01:00
|
|
|
assert.equal(multiSigAddress,'32vYjxBb7pHJJyXgNk8UoK3BdRDxBzny2v')
|
|
|
|
})
|
2014-03-29 15:13:39 +01:00
|
|
|
|
2014-03-28 03:56:10 +01:00
|
|
|
it('should create valid redeemScript', function() {
|
|
|
|
var redeemScript = script.buffer
|
|
|
|
var deserialized = new Script(redeemScript)
|
2014-03-29 15:13:39 +01:00
|
|
|
var numOfSignatures = deserialized.chunks[deserialized.chunks.length - 2] - 80
|
|
|
|
var signaturesRequired = deserialized.chunks[0] - 80
|
2014-03-31 05:47:47 +02:00
|
|
|
var sigs = [
|
|
|
|
bytesToHex(deserialized.chunks[1]),
|
|
|
|
bytesToHex(deserialized.chunks[2]),
|
|
|
|
bytesToHex(deserialized.chunks[3])
|
|
|
|
]
|
2014-03-29 15:13:39 +01:00
|
|
|
|
|
|
|
assert.equal(numOfSignatures, 3)
|
|
|
|
assert.equal(signaturesRequired, 2)
|
|
|
|
assert.equal(sigs[0], '02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f')
|
|
|
|
assert.equal(sigs[1], '02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f')
|
|
|
|
assert.equal(sigs[2], '036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19')
|
2014-04-08 14:13:03 +02:00
|
|
|
assert.equal(Address(crypto.hash160(redeemScript), network).toString(), '32vYjxBb7pHJJyXgNk8UoK3BdRDxBzny2v')
|
2014-03-28 03:56:10 +01:00
|
|
|
})
|
2014-03-25 06:32:43 +01:00
|
|
|
})
|
2014-03-09 06:43:36 +01:00
|
|
|
})
|