2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-06-15 08:44:52 +02:00
|
|
|
var assert = require('assert')
|
|
|
|
|
|
|
|
var BigInteger = require('bigi')
|
|
|
|
var ECSignature = require('../src/ecsignature')
|
|
|
|
|
|
|
|
var fixtures = require('./fixtures/ecsignature.json')
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('ECSignature', function () {
|
|
|
|
describe('toCompact', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('exports ' + f.compact.hex + ' correctly', function () {
|
2015-03-02 03:31:03 +01:00
|
|
|
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
|
2014-06-15 08:44:52 +02:00
|
|
|
|
|
|
|
var buffer = signature.toCompact(f.compact.i, f.compact.compressed)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(buffer.toString('hex'), f.compact.hex)
|
2014-06-15 08:44:52 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('parseCompact', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.compact.hex + ' correctly', function () {
|
2017-04-19 09:39:16 +02:00
|
|
|
var buffer = Buffer.from(f.compact.hex, 'hex')
|
2014-06-16 16:26:16 +02:00
|
|
|
var parsed = ECSignature.parseCompact(buffer)
|
2014-06-15 08:44:52 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(parsed.compressed, f.compact.compressed)
|
|
|
|
assert.strictEqual(parsed.i, f.compact.i)
|
|
|
|
assert.strictEqual(parsed.signature.r.toString(), f.signature.r)
|
|
|
|
assert.strictEqual(parsed.signature.s.toString(), f.signature.s)
|
2014-06-15 08:44:52 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.compact.forEach(function (f) {
|
|
|
|
it('throws on ' + f.hex, function () {
|
2017-04-19 09:39:16 +02:00
|
|
|
var buffer = Buffer.from(f.hex, 'hex')
|
2014-06-15 08:44:52 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-06-16 16:26:16 +02:00
|
|
|
ECSignature.parseCompact(buffer)
|
2014-06-15 08:44:52 +02:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('toDER', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('exports ' + f.DER + ' correctly', function () {
|
2015-03-02 03:31:03 +01:00
|
|
|
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
|
2014-06-15 08:44:52 +02:00
|
|
|
|
|
|
|
var DER = signature.toDER()
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(DER.toString('hex'), f.DER)
|
2014-06-15 08:44:52 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('fromDER', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.DER + ' correctly', function () {
|
2017-04-19 09:39:16 +02:00
|
|
|
var buffer = Buffer.from(f.DER, 'hex')
|
2014-06-15 08:44:52 +02:00
|
|
|
var signature = ECSignature.fromDER(buffer)
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(signature.r.toString(), f.signature.r)
|
|
|
|
assert.strictEqual(signature.s.toString(), f.signature.s)
|
2014-06-15 08:44:52 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.DER.forEach(function (f) {
|
2015-07-07 09:42:01 +02:00
|
|
|
it('throws "' + f.exception + '" for ' + f.hex, function () {
|
2017-04-19 09:39:16 +02:00
|
|
|
var buffer = Buffer.from(f.hex, 'hex')
|
2014-06-15 08:44:52 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-06-15 08:44:52 +02:00
|
|
|
ECSignature.fromDER(buffer)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-06-15 09:15:17 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('toScriptSignature', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('exports ' + f.scriptSignature.hex + ' correctly', function () {
|
2015-03-02 03:31:03 +01:00
|
|
|
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
|
2014-06-15 09:15:17 +02:00
|
|
|
|
|
|
|
var scriptSignature = signature.toScriptSignature(f.scriptSignature.hashType)
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(scriptSignature.toString('hex'), f.scriptSignature.hex)
|
2014-06-15 09:15:17 +02:00
|
|
|
})
|
|
|
|
})
|
2014-11-28 01:35:11 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.scriptSignature.forEach(function (f) {
|
|
|
|
it('throws ' + f.exception, function () {
|
2015-03-02 03:31:03 +01:00
|
|
|
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
|
2014-11-28 01:35:11 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-11-28 01:35:11 +01:00
|
|
|
signature.toScriptSignature(f.hashType)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-06-15 09:15:17 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('parseScriptSignature', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.scriptSignature.hex + ' correctly', function () {
|
2017-04-19 09:39:16 +02:00
|
|
|
var buffer = Buffer.from(f.scriptSignature.hex, 'hex')
|
2014-06-16 16:26:16 +02:00
|
|
|
var parsed = ECSignature.parseScriptSignature(buffer)
|
2014-06-15 09:15:17 +02:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(parsed.signature.r.toString(), f.signature.r)
|
|
|
|
assert.strictEqual(parsed.signature.s.toString(), f.signature.s)
|
|
|
|
assert.strictEqual(parsed.hashType, f.scriptSignature.hashType)
|
2014-06-15 09:15:17 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.scriptSignature.forEach(function (f) {
|
|
|
|
it('throws on ' + f.hex, function () {
|
2017-04-19 09:39:16 +02:00
|
|
|
var buffer = Buffer.from(f.hex, 'hex')
|
2014-06-15 09:15:17 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-06-16 16:26:16 +02:00
|
|
|
ECSignature.parseScriptSignature(buffer)
|
2014-06-15 09:15:17 +02:00
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-06-15 08:44:52 +02:00
|
|
|
})
|