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')
|
|
|
|
|
|
|
|
describe('ECSignature', function() {
|
|
|
|
describe('toCompact', function() {
|
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-06-15 09:20:45 +02:00
|
|
|
it('exports ' + f.compact.hex + ' correctly', function() {
|
2014-06-15 08:44:52 +02:00
|
|
|
var signature = new ECSignature(
|
|
|
|
new BigInteger(f.signature.r),
|
|
|
|
new BigInteger(f.signature.s)
|
|
|
|
)
|
|
|
|
|
|
|
|
var buffer = signature.toCompact(f.compact.i, f.compact.compressed)
|
|
|
|
assert.equal(buffer.toString('hex'), f.compact.hex)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-16 16:26:16 +02:00
|
|
|
describe('parseCompact', function() {
|
2014-06-15 08:44:52 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
2014-06-15 09:20:45 +02:00
|
|
|
it('imports ' + f.compact.hex + ' correctly', function() {
|
2014-06-15 08:44:52 +02:00
|
|
|
var buffer = new Buffer(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
|
|
|
|
|
|
|
assert.equal(parsed.compressed, f.compact.compressed)
|
|
|
|
assert.equal(parsed.i, f.compact.i)
|
|
|
|
assert.equal(parsed.signature.r.toString(), f.signature.r)
|
|
|
|
assert.equal(parsed.signature.s.toString(), f.signature.s)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.compact.forEach(function(f) {
|
|
|
|
it('throws on ' + f.hex, function() {
|
|
|
|
var buffer = new Buffer(f.hex, 'hex')
|
|
|
|
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('toDER', function() {
|
2014-06-15 09:20:45 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('exports ' + f.DER + ' correctly', function() {
|
2014-06-15 08:44:52 +02:00
|
|
|
var signature = new ECSignature(
|
|
|
|
new BigInteger(f.signature.r),
|
|
|
|
new BigInteger(f.signature.s)
|
|
|
|
)
|
|
|
|
|
|
|
|
var DER = signature.toDER()
|
|
|
|
assert.equal(DER.toString('hex'), f.DER)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('fromDER', function() {
|
2014-06-15 09:20:45 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('imports ' + f.DER + ' correctly', function() {
|
2014-06-15 08:44:52 +02:00
|
|
|
var buffer = new Buffer(f.DER, 'hex')
|
|
|
|
var signature = ECSignature.fromDER(buffer)
|
|
|
|
|
|
|
|
assert.equal(signature.r.toString(), f.signature.r)
|
|
|
|
assert.equal(signature.s.toString(), f.signature.s)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.DER.forEach(function(f) {
|
|
|
|
it('throws on ' + f.hex, function() {
|
|
|
|
var buffer = new Buffer(f.hex, 'hex')
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
ECSignature.fromDER(buffer)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-06-15 09:15:17 +02:00
|
|
|
|
|
|
|
describe('toScriptSignature', function() {
|
2014-06-15 09:20:45 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('exports ' + f.scriptSignature.hex + ' correctly', function() {
|
2014-06-15 09:15:17 +02:00
|
|
|
var signature = new ECSignature(
|
|
|
|
new BigInteger(f.signature.r),
|
|
|
|
new BigInteger(f.signature.s)
|
|
|
|
)
|
|
|
|
|
|
|
|
var scriptSignature = signature.toScriptSignature(f.scriptSignature.hashType)
|
|
|
|
assert.equal(scriptSignature.toString('hex'), f.scriptSignature.hex)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-16 16:26:16 +02:00
|
|
|
describe('parseScriptSignature', function() {
|
2014-06-15 09:20:45 +02:00
|
|
|
fixtures.valid.forEach(function(f) {
|
|
|
|
it('imports ' + f.scriptSignature.hex + ' correctly', function() {
|
2014-06-15 09:15:17 +02:00
|
|
|
var buffer = new Buffer(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
|
|
|
|
|
|
|
assert.equal(parsed.signature.r.toString(), f.signature.r)
|
|
|
|
assert.equal(parsed.signature.s.toString(), f.signature.s)
|
|
|
|
assert.equal(parsed.hashType, f.scriptSignature.hashType)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.DER.forEach(function(f) {
|
|
|
|
it('throws on ' + f.hex, function() {
|
|
|
|
var buffer = new Buffer(f.hex + '01', 'hex')
|
|
|
|
|
|
|
|
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
|
|
|
})
|