bitcoinjs-lib/test/bitcoin.core.js

227 lines
7 KiB
JavaScript
Raw Normal View History

2015-02-23 00:36:57 +01:00
/* global describe, it */
2015-02-23 00:36:57 +01:00
var assert = require('assert')
2014-10-15 13:36:10 +02:00
var base58 = require('bs58')
2015-08-20 05:10:03 +02:00
var bitcoin = require('../')
2016-09-14 14:29:12 +02:00
var base58EncodeDecode = require('./fixtures/core/base58_encode_decode.json')
var base58KeysInvalid = require('./fixtures/core/base58_keys_invalid.json')
var base58KeysValid = require('./fixtures/core/base58_keys_valid.json')
var blocksValid = require('./fixtures/core/blocks.json')
var sigCanonical = require('./fixtures/core/sig_canonical.json')
var sigHash = require('./fixtures/core/sighash.json')
var sigNoncanonical = require('./fixtures/core/sig_noncanonical.json')
var txValid = require('./fixtures/core/tx_valid.json')
2015-02-23 00:36:57 +01:00
describe('Bitcoin-core', function () {
2016-09-14 14:29:12 +02:00
// base58EncodeDecode
2015-02-23 00:36:57 +01:00
describe('base58', function () {
2016-09-14 14:29:12 +02:00
base58EncodeDecode.forEach(function (f) {
var fhex = f[0]
var fb58 = f[1]
2015-02-23 00:36:57 +01:00
it('can decode ' + fb58, function () {
var buffer = base58.decode(fb58)
var actual = buffer.toString('hex')
2015-05-07 03:29:20 +02:00
assert.strictEqual(actual, fhex)
})
2015-02-23 00:36:57 +01:00
it('can encode ' + fhex, function () {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.from(fhex, 'hex')
var actual = base58.encode(buffer)
2015-05-07 03:29:20 +02:00
assert.strictEqual(actual, fb58)
})
})
})
2016-09-14 14:29:12 +02:00
// base58KeysValid
2015-08-20 05:12:05 +02:00
describe('address.toBase58Check', function () {
2015-02-23 00:36:57 +01:00
var typeMap = {
'pubkey': 'pubKeyHash',
'script': 'scriptHash'
}
2016-09-14 14:29:12 +02:00
base58KeysValid.forEach(function (f) {
var expected = f[0]
2017-04-19 09:39:16 +02:00
var hash = Buffer.from(f[1], 'hex')
var params = f[2]
if (params.isPrivkey) return
2015-08-20 05:10:03 +02:00
var network = params.isTestnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin
var version = network[typeMap[params.addrType]]
it('can export ' + expected, function () {
2015-08-20 05:10:03 +02:00
assert.strictEqual(bitcoin.address.toBase58Check(hash, version), expected)
})
})
})
2016-09-14 14:29:12 +02:00
// base58KeysInvalid
2015-08-20 05:12:05 +02:00
describe('address.fromBase58Check', function () {
var allowedNetworks = [
2015-08-20 05:10:03 +02:00
bitcoin.networks.bitcoin.pubkeyhash,
bitcoin.networks.bitcoin.scripthash,
bitcoin.networks.testnet.pubkeyhash,
bitcoin.networks.testnet.scripthash
]
2016-09-14 14:29:12 +02:00
base58KeysInvalid.forEach(function (f) {
var string = f[0]
2015-02-23 00:36:57 +01:00
it('throws on ' + string, function () {
assert.throws(function () {
2015-08-20 05:12:05 +02:00
var address = bitcoin.address.fromBase58Check(string)
assert.notEqual(allowedNetworks.indexOf(address.version), -1, 'Invalid network')
2015-08-18 02:20:49 +02:00
}, /(Invalid (checksum|network))|(too (short|long))/)
})
})
})
2016-09-14 14:29:12 +02:00
// base58KeysValid
2015-03-02 06:48:36 +01:00
describe('ECPair', function () {
2016-09-14 14:29:12 +02:00
base58KeysValid.forEach(function (f) {
var string = f[0]
var hex = f[1]
var params = f[2]
if (!params.isPrivkey) return
2015-07-28 08:42:57 +02:00
2015-08-20 05:10:03 +02:00
var network = params.isTestnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin
var keyPair = bitcoin.ECPair.fromWIF(string, network)
2015-07-08 04:33:48 +02:00
it('fromWIF imports ' + string, function () {
2015-05-07 03:29:20 +02:00
assert.strictEqual(keyPair.d.toHex(), hex)
assert.strictEqual(keyPair.compressed, params.isCompressed)
})
2015-07-08 04:33:48 +02:00
it('toWIF exports ' + hex + ' to ' + string, function () {
2015-05-07 03:29:20 +02:00
assert.strictEqual(keyPair.toWIF(), string)
})
})
})
2016-09-14 14:29:12 +02:00
// base58KeysInvalid
2015-07-08 04:33:48 +02:00
describe('ECPair.fromWIF', function () {
var allowedNetworks = [
2015-08-20 05:10:03 +02:00
bitcoin.networks.bitcoin,
bitcoin.networks.testnet
]
2016-09-14 14:29:12 +02:00
base58KeysInvalid.forEach(function (f) {
var string = f[0]
2015-02-23 00:36:57 +01:00
it('throws on ' + string, function () {
assert.throws(function () {
2015-08-20 05:10:03 +02:00
bitcoin.ECPair.fromWIF(string, allowedNetworks)
}, /(Invalid|Unknown) (checksum|compression flag|network version|WIF length)/)
})
})
})
2015-07-08 04:33:48 +02:00
describe('Block.fromHex', function () {
2016-09-14 14:29:12 +02:00
blocksValid.forEach(function (f) {
2015-07-08 04:33:48 +02:00
it('can parse ' + f.id, function () {
2015-08-20 05:10:03 +02:00
var block = bitcoin.Block.fromHex(f.hex)
2015-05-07 03:29:20 +02:00
assert.strictEqual(block.getId(), f.id)
assert.strictEqual(block.transactions.length, f.transactions)
})
})
})
2016-09-14 14:29:12 +02:00
// txValid
2015-07-08 04:33:48 +02:00
describe('Transaction.fromHex', function () {
2016-09-14 14:29:12 +02:00
txValid.forEach(function (f) {
// Objects that are only a single string are ignored
if (f.length === 1) return
var inputs = f[0]
var fhex = f[1]
2015-02-23 00:36:57 +01:00
// var verifyFlags = f[2] // TODO: do we need to test this?
2015-02-23 00:36:57 +01:00
it('can decode ' + fhex, function () {
2015-08-20 05:10:03 +02:00
var transaction = bitcoin.Transaction.fromHex(fhex)
transaction.ins.forEach(function (txIn, i) {
var input = inputs[i]
// reverse because test data is reversed
2017-04-19 09:39:16 +02:00
var prevOutHash = Buffer.from(input[0], 'hex').reverse()
var prevOutIndex = input[1]
assert.deepEqual(txIn.hash, prevOutHash)
// we read UInt32, not Int32
2015-05-07 03:29:20 +02:00
assert.strictEqual(txIn.index & 0xffffffff, prevOutIndex)
})
})
})
})
// sighash
2015-02-23 00:36:57 +01:00
describe('Transaction', function () {
2016-09-14 14:29:12 +02:00
sigHash.forEach(function (f) {
// Objects that are only a single string are ignored
if (f.length === 1) return
var txHex = f[0]
var scriptHex = f[1]
var inIndex = f[2]
var hashType = f[3]
var expectedHash = f[4]
2015-04-28 02:38:10 +02:00
var hashTypes = []
2015-08-20 05:10:03 +02:00
if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_NONE) hashTypes.push('SIGHASH_NONE')
else if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_SINGLE) hashTypes.push('SIGHASH_SINGLE')
2015-04-28 02:38:10 +02:00
else hashTypes.push('SIGHASH_ALL')
2015-08-20 05:10:03 +02:00
if (hashType & bitcoin.Transaction.SIGHASH_ANYONECANPAY) hashTypes.push('SIGHASH_ANYONECANPAY')
2015-04-28 02:38:10 +02:00
var hashTypeName = hashTypes.join(' | ')
it('should hash ' + txHex.slice(0, 40) + '... (' + hashTypeName + ')', function () {
2015-08-20 05:10:03 +02:00
var transaction = bitcoin.Transaction.fromHex(txHex)
2015-05-07 03:29:20 +02:00
assert.strictEqual(transaction.toHex(), txHex)
2017-04-19 09:39:16 +02:00
var script = Buffer.from(scriptHex, 'hex')
2015-08-20 05:31:29 +02:00
var scriptChunks = bitcoin.script.decompile(script)
assert.strictEqual(bitcoin.script.compile(scriptChunks).toString('hex'), scriptHex)
2015-04-28 02:38:10 +02:00
var hash = transaction.hashForSignature(inIndex, script, hashType)
// reverse because test data is reversed
assert.equal(hash.reverse().toString('hex'), expectedHash)
})
})
})
2015-07-08 04:33:48 +02:00
describe('ECSignature.parseScriptSignature', function () {
2016-09-14 14:29:12 +02:00
sigCanonical.forEach(function (hex) {
2017-04-19 09:39:16 +02:00
var buffer = Buffer.from(hex, 'hex')
2015-02-23 00:36:57 +01:00
it('can parse ' + hex, function () {
2015-08-20 05:10:03 +02:00
var parsed = bitcoin.ECSignature.parseScriptSignature(buffer)
var actual = parsed.signature.toScriptSignature(parsed.hashType)
2015-05-07 03:29:20 +02:00
assert.strictEqual(actual.toString('hex'), hex)
})
})
2016-09-14 14:29:12 +02:00
sigNoncanonical.forEach(function (hex, i) {
if (i === 0) return
if (i % 2 !== 0) return
2016-09-14 14:29:12 +02:00
var description = sigNoncanonical[i - 1].slice(0, -1)
2017-04-19 09:39:16 +02:00
var buffer = Buffer.from(hex, 'hex')
2015-02-23 00:36:57 +01:00
it('throws on ' + description, function () {
assert.throws(function () {
2015-08-20 05:10:03 +02:00
bitcoin.ECSignature.parseScriptSignature(buffer)
}, /Expected DER (integer|sequence)|(R|S) value (excessively padded|is negative)|(R|S|DER sequence) length is (zero|too short|too long|invalid)|Invalid hashType/)
})
})
})
})