2018-07-23 09:45:01 +02:00
|
|
|
const { describe, it } = require('mocha')
|
2018-06-25 08:25:12 +02:00
|
|
|
const assert = require('assert')
|
2019-01-04 10:33:02 +01:00
|
|
|
const bscriptSig = require('../src/script').signature
|
2018-06-25 08:25:12 +02:00
|
|
|
const Buffer = require('safe-buffer').Buffer
|
|
|
|
const fixtures = require('./fixtures/signature.json')
|
2016-10-13 14:45:08 +02:00
|
|
|
|
|
|
|
describe('Script Signatures', function () {
|
|
|
|
function fromRaw (signature) {
|
2018-05-15 02:07:11 +02:00
|
|
|
return Buffer.concat([
|
|
|
|
Buffer.from(signature.r, 'hex'),
|
|
|
|
Buffer.from(signature.s, 'hex')
|
|
|
|
], 64)
|
2016-10-13 14:45:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function toRaw (signature) {
|
|
|
|
return {
|
2018-05-15 02:07:11 +02:00
|
|
|
r: signature.slice(0, 32).toString('hex'),
|
|
|
|
s: signature.slice(32, 64).toString('hex')
|
2016-10-13 14:45:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('encode', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('encodes ' + f.hex, function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const buffer = bscriptSig.encode(fromRaw(f.raw), f.hashType)
|
2016-10-13 14:45:08 +02:00
|
|
|
|
|
|
|
assert.strictEqual(buffer.toString('hex'), f.hex)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.invalid.forEach(function (f) {
|
|
|
|
if (!f.raw) return
|
|
|
|
|
|
|
|
it('throws ' + f.exception, function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const signature = fromRaw(f.raw)
|
2016-10-13 14:45:08 +02:00
|
|
|
|
|
|
|
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-06-25 08:37:45 +02:00
|
|
|
const 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-06-25 08:37:45 +02:00
|
|
|
const buffer = Buffer.from(f.hex, 'hex')
|
2016-10-13 14:45:08 +02:00
|
|
|
|
|
|
|
assert.throws(function () {
|
|
|
|
bscriptSig.decode(buffer)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|