2016-10-13 14:45:08 +02:00
|
|
|
/* global describe, it */
|
|
|
|
|
|
|
|
var assert = require('assert')
|
|
|
|
var bscriptSig = require('../src/script').signature
|
|
|
|
var BigInteger = require('bigi')
|
2018-04-13 08:06:41 +02:00
|
|
|
var Buffer = require('safe-buffer').Buffer
|
2016-10-13 14:45:08 +02:00
|
|
|
var fixtures = require('./fixtures/signature.json')
|
|
|
|
|
|
|
|
describe('Script Signatures', function () {
|
|
|
|
function fromRaw (signature) {
|
|
|
|
return {
|
|
|
|
r: new BigInteger(signature.r),
|
|
|
|
s: new BigInteger(signature.s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toRaw (signature) {
|
|
|
|
return {
|
|
|
|
r: signature.r.toString(),
|
|
|
|
s: signature.s.toString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('encode', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('encodes ' + f.hex, function () {
|
|
|
|
var buffer = bscriptSig.encode(fromRaw(f.raw), f.hashType)
|
|
|
|
|
|
|
|
assert.strictEqual(buffer.toString('hex'), f.hex)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.forEach(function (f) {
|
|
|
|
if (!f.raw) return
|
|
|
|
|
|
|
|
it('throws ' + f.exception, function () {
|
|
|
|
var signature = fromRaw(f.raw)
|
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
bscriptSig.encode(signature, f.hashType)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('decode', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('decodes ' + f.hex, function () {
|
2018-04-13 08:06:41 +02:00
|
|
|
var decode = bscriptSig.decode(Buffer.from(f.hex, 'hex'))
|
2016-10-13 14:45:08 +02:00
|
|
|
|
|
|
|
assert.deepEqual(toRaw(decode.signature), f.raw)
|
|
|
|
assert.strictEqual(decode.hashType, f.hashType)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.forEach(function (f) {
|
|
|
|
it('throws on ' + f.hex, function () {
|
2018-04-13 08:06:41 +02:00
|
|
|
var buffer = Buffer.from(f.hex, 'hex')
|
2016-10-13 14:45:08 +02:00
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
bscriptSig.decode(buffer)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|