add toRSBuffer for less breaking changes

This commit is contained in:
Daniel Cousens 2018-04-13 16:02:45 +10:00
parent c58ada362e
commit c16847434c
3 changed files with 9 additions and 337 deletions

View file

@ -20,6 +20,13 @@ function decode (buffer) {
}
}
function toRSBuffer (signature, buffer, offset) {
buffer = buffer || Buffer.alloc(64)
signature.r.toBuffer(32).copy(buffer, offset)
signature.s.toBuffer(32).copy(buffer, offset + 32)
return buffer
}
function fromRSBuffer (buffer) {
typeforce(types.BufferN(64), buffer)
@ -45,7 +52,8 @@ function encode (signature, hashType) {
}
module.exports = {
fromRSBuffer,
fromRSBuffer: fromRSBuffer,
toRSBuffer: toRSBuffer,
decode: decode,
encode: encode
}

View file

@ -1,122 +0,0 @@
/* global describe, it */
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) {
it('exports ' + f.compact.hex + ' correctly', function () {
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.strictEqual(buffer.toString('hex'), f.compact.hex)
})
})
})
describe('parseCompact', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.compact.hex + ' correctly', function () {
var buffer = Buffer.from(f.compact.hex, 'hex')
var parsed = ECSignature.parseCompact(buffer)
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)
})
})
fixtures.invalid.compact.forEach(function (f) {
it('throws on ' + f.hex, function () {
var buffer = Buffer.from(f.hex, 'hex')
assert.throws(function () {
ECSignature.parseCompact(buffer)
}, new RegExp(f.exception))
})
})
})
describe('toDER', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.DER + ' correctly', function () {
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
var DER = signature.toDER()
assert.strictEqual(DER.toString('hex'), f.DER)
})
})
})
describe('fromDER', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.DER + ' correctly', function () {
var buffer = Buffer.from(f.DER, 'hex')
var signature = ECSignature.fromDER(buffer)
assert.strictEqual(signature.r.toString(), f.signature.r)
assert.strictEqual(signature.s.toString(), f.signature.s)
})
})
fixtures.invalid.DER.forEach(function (f) {
it('throws "' + f.exception + '" for ' + f.hex, function () {
var buffer = Buffer.from(f.hex, 'hex')
assert.throws(function () {
ECSignature.fromDER(buffer)
}, new RegExp(f.exception))
})
})
})
describe('toScriptSignature', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.scriptSignature.hex + ' correctly', function () {
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
var scriptSignature = signature.toScriptSignature(f.scriptSignature.hashType)
assert.strictEqual(scriptSignature.toString('hex'), f.scriptSignature.hex)
})
})
fixtures.invalid.scriptSignature.forEach(function (f) {
it('throws ' + f.exception, function () {
var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
assert.throws(function () {
signature.toScriptSignature(f.hashType)
}, new RegExp(f.exception))
})
})
})
describe('parseScriptSignature', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.scriptSignature.hex + ' correctly', function () {
var buffer = Buffer.from(f.scriptSignature.hex, 'hex')
var parsed = ECSignature.parseScriptSignature(buffer)
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)
})
})
fixtures.invalid.scriptSignature.forEach(function (f) {
it('throws on ' + f.hex, function () {
var buffer = Buffer.from(f.hex, 'hex')
assert.throws(function () {
ECSignature.parseScriptSignature(buffer)
}, new RegExp(f.exception))
})
})
})
})

View file

@ -1,214 +0,0 @@
{
"valid": [
{
"compact": {
"hex": "1f33a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c96f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa54342262",
"compressed": true,
"i": 0
},
"scriptSignature": {
"hex": "3044022033a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c902206f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa5434226201",
"hashType": 1
},
"DER": "3044022033a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c902206f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa54342262",
"signature": {
"r": "23362334225185207751494092901091441011938859014081160902781146257181456271561",
"s": "50433721247292933944369538617440297985091596895097604618403996029256432099938"
}
},
{
"compact": {
"hex": "1b54c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed07082304410efa6b2943111b6a4e0aaa7b7db55a07e9861d1fb3cb1f421044a5",
"compressed": false,
"i": 0
},
"DER": "3044022054c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed022007082304410efa6b2943111b6a4e0aaa7b7db55a07e9861d1fb3cb1f421044a5",
"scriptSignature": {
"hex": "3044022054c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed022007082304410efa6b2943111b6a4e0aaa7b7db55a07e9861d1fb3cb1f421044a502",
"hashType": 2
},
"signature": {
"r": "38341707918488238920692284707283974715538935465589664377561695343399725051885",
"s": "3180566392414476763164587487324397066658063772201694230600609996154610926757"
}
},
{
"compact": {
"hex": "1fff466a9f1b7b273e2f4c3ffe032eb2e814121ed18ef84665d0f515360dab3dd06fc95f5132e5ecfdc8e5e6e616cc77151455d46ed48f5589b7db7771a332b283",
"compressed": true,
"i": 0
},
"scriptSignature": {
"hex": "3045022100ff466a9f1b7b273e2f4c3ffe032eb2e814121ed18ef84665d0f515360dab3dd002206fc95f5132e5ecfdc8e5e6e616cc77151455d46ed48f5589b7db7771a332b28303",
"hashType": 3
},
"DER": "3045022100ff466a9f1b7b273e2f4c3ffe032eb2e814121ed18ef84665d0f515360dab3dd002206fc95f5132e5ecfdc8e5e6e616cc77151455d46ed48f5589b7db7771a332b283",
"signature": {
"r": "115464191557905790016094131873849783294273568009648050793030031933291767741904",
"s": "50562520307781850052192542766631199590053690478900449960232079510155113443971"
}
},
{
"compact": {
"hex": "1cc0dafec8251f1d5010289d210232220b03202cba34ec11fec58b3e93a85b91d375afdc06b7d6322a590955bf264e7aaa155847f614d80078a90292fe205064d3",
"compressed": false,
"i": 1
},
"scriptSignature": {
"hex": "3045022100c0dafec8251f1d5010289d210232220b03202cba34ec11fec58b3e93a85b91d3022075afdc06b7d6322a590955bf264e7aaa155847f614d80078a90292fe205064d381",
"hashType": 129
},
"DER": "3045022100c0dafec8251f1d5010289d210232220b03202cba34ec11fec58b3e93a85b91d3022075afdc06b7d6322a590955bf264e7aaa155847f614d80078a90292fe205064d3",
"signature": {
"r": "87230998027579607140680851455601772643840468630989315269459846730712163783123",
"s": "53231320085894623106179381504478252331065330583563809963303318469380290929875"
}
},
{
"compact": {
"hex": "1f7186363571d65e084e7f02b0b77c3ec44fb1b257dee26274c38c928986fea45d0de0b38e06807e46bda1f1e293f4f6323e854c86d58abdd00c46c16441085df6",
"compressed": true,
"i": 0
},
"scriptSignature": {
"hex": "304402207186363571d65e084e7f02b0b77c3ec44fb1b257dee26274c38c928986fea45d02200de0b38e06807e46bda1f1e293f4f6323e854c86d58abdd00c46c16441085df682",
"hashType": 130
},
"DER": "304402207186363571d65e084e7f02b0b77c3ec44fb1b257dee26274c38c928986fea45d02200de0b38e06807e46bda1f1e293f4f6323e854c86d58abdd00c46c16441085df6",
"signature": {
"r": "51348483531757779992459563033975330355971795607481991320287437101831125115997",
"s": "6277080015686056199074771961940657638578000617958603212944619747099038735862"
}
},
{
"compact": {
"hex": "1cfbfe5076a15860ba8ed00e75e9bd22e05d230f02a936b653eb55b61c99dda4870e68880ebb0050fe4312b1b1eb0899e1b82da89baa5b895f612619edf34cbd37",
"compressed": false,
"i": 1
},
"scriptSignature": {
"hex": "3045022100fbfe5076a15860ba8ed00e75e9bd22e05d230f02a936b653eb55b61c99dda48702200e68880ebb0050fe4312b1b1eb0899e1b82da89baa5b895f612619edf34cbd3783",
"hashType": 131
},
"DER": "3045022100fbfe5076a15860ba8ed00e75e9bd22e05d230f02a936b653eb55b61c99dda48702200e68880ebb0050fe4312b1b1eb0899e1b82da89baa5b895f612619edf34cbd37",
"signature": {
"r": "113979859486826658566290715281614250298918272782414232881639314569529560769671",
"s": "6517071009538626957379450615706485096874328019806177698938278220732027419959"
}
},
{
"compact": {
"hex": "20cde1302d83f8dd835d89aef803c74a119f561fbaef3eb9129e45f30de86abbf906ce643f5049ee1f27890467b77a6a8e11ec4661cc38cd8badf90115fbd03cef",
"compressed": true,
"i": 1
},
"scriptSignature": {
"hex": "3045022100cde1302d83f8dd835d89aef803c74a119f561fbaef3eb9129e45f30de86abbf9022006ce643f5049ee1f27890467b77a6a8e11ec4661cc38cd8badf90115fbd03cef81",
"hashType": 129
},
"DER": "3045022100cde1302d83f8dd835d89aef803c74a119f561fbaef3eb9129e45f30de86abbf9022006ce643f5049ee1f27890467b77a6a8e11ec4661cc38cd8badf90115fbd03cef",
"signature": {
"r": "93122007060065279508564838030979550535085999589142852106617159184757394422777",
"s": "3078539468410661027472930027406594684630312677495124015420811882501887769839"
}
}
],
"invalid": {
"compact": [
{
"exception": "Invalid signature parameter",
"hex": "23987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62"
},
{
"exception": "Expected Buffer\\(Length: 65\\), got Buffer\\(Length: 68\\)",
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62000000"
},
{
"exception": "Expected Buffer\\(Length: 65\\), got Buffer\\(Length: 59\\)",
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e9379"
}
],
"DER": [
{
"exception": "DER sequence length is too short",
"hex": "ffffffffffffff"
},
{
"exception": "DER sequence length is too long",
"hex": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
},
{
"exception": "Expected DER sequence",
"hex": "00ffff0400ffffff020400ffffff"
},
{
"exception": "DER sequence length is invalid",
"hex": "30ff020400ffffff020400ffffff"
},
{
"exception": "DER sequence length is invalid",
"hex": "300c030400ffffff030400ffffff0000"
},
{
"exception": "Expected DER integer",
"hex": "300cff0400ffffff020400ffffff"
},
{
"exception": "Expected DER integer \\(2\\)",
"hex": "300c020200ffffff020400ffffff"
},
{
"exception": "R length is zero",
"hex": "30080200020400ffffff"
},
{
"exception": "S length is zero",
"hex": "3008020400ffffff0200"
},
{
"exception": "R length is too long",
"hex": "300c02dd00ffffff020400ffffff"
},
{
"exception": "S length is invalid",
"hex": "300c020400ffffff02dd00ffffff"
},
{
"exception": "R value is negative",
"hex": "300c020480000000020400ffffff"
},
{
"exception": "S value is negative",
"hex": "300c020400ffffff020480000000"
},
{
"exception": "R value excessively padded",
"hex": "300c02040000ffff020400ffffff"
},
{
"exception": "S value excessively padded",
"hex": "300c020400ffffff02040000ffff"
}
],
"scriptSignature": [
{
"exception": "Invalid hashType 7",
"hashType": 7,
"hex": "3044022033a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c902206f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa5434226207",
"signature": {
"r": "23362334225185207751494092901091441011938859014081160902781146257181456271561",
"s": "50433721247292933944369538617440297985091596895097604618403996029256432099938"
}
},
{
"exception": "Invalid hashType 140",
"hashType": 140,
"hex": "3044022033a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c902206f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa543422628c",
"signature": {
"r": "23362334225185207751494092901091441011938859014081160902781146257181456271561",
"s": "50433721247292933944369538617440297985091596895097604618403996029256432099938"
}
}
]
}
}