ECSignature: add toRSBuffer/fromRSBuffer
This commit is contained in:
parent
a301aa8d3c
commit
b94da10e6c
2 changed files with 21 additions and 10 deletions
|
@ -12,24 +12,30 @@ function ECSignature (r, s) {
|
|||
}
|
||||
|
||||
ECSignature.parseCompact = function (buffer) {
|
||||
if (buffer.length !== 65) throw new Error('Invalid signature length')
|
||||
typeforce(types.BufferN(65), buffer)
|
||||
|
||||
var flagByte = buffer.readUInt8(0) - 27
|
||||
if (flagByte !== (flagByte & 7)) throw new Error('Invalid signature parameter')
|
||||
|
||||
var compressed = !!(flagByte & 4)
|
||||
var recoveryParam = flagByte & 3
|
||||
|
||||
var r = BigInteger.fromBuffer(buffer.slice(1, 33))
|
||||
var s = BigInteger.fromBuffer(buffer.slice(33))
|
||||
var signature = ECSignature.fromRSBuffer(buffer.slice(1))
|
||||
|
||||
return {
|
||||
compressed: compressed,
|
||||
i: recoveryParam,
|
||||
signature: new ECSignature(r, s)
|
||||
signature: signature
|
||||
}
|
||||
}
|
||||
|
||||
ECSignature.fromRSBuffer = function (buffer) {
|
||||
typeforce(types.BufferN(64), buffer)
|
||||
|
||||
var r = BigInteger.fromBuffer(buffer.slice(0, 32))
|
||||
var s = BigInteger.fromBuffer(buffer.slice(32, 64))
|
||||
return new ECSignature(r, s)
|
||||
}
|
||||
|
||||
ECSignature.fromDER = function (buffer) {
|
||||
var decode = bip66.decode(buffer)
|
||||
var r = BigInteger.fromDERInteger(decode.r)
|
||||
|
@ -60,9 +66,7 @@ ECSignature.prototype.toCompact = function (i, compressed) {
|
|||
|
||||
var buffer = Buffer.alloc(65)
|
||||
buffer.writeUInt8(i, 0)
|
||||
this.r.toBuffer(32).copy(buffer, 1)
|
||||
this.s.toBuffer(32).copy(buffer, 33)
|
||||
|
||||
this.toRSBuffer(buffer, 1)
|
||||
return buffer
|
||||
}
|
||||
|
||||
|
@ -73,6 +77,13 @@ ECSignature.prototype.toDER = function () {
|
|||
return bip66.encode(r, s)
|
||||
}
|
||||
|
||||
ECSignature.prototype.toRSBuffer = function (buffer, offset) {
|
||||
buffer = buffer || Buffer.alloc(64)
|
||||
this.r.toBuffer(32).copy(buffer, offset)
|
||||
this.s.toBuffer(32).copy(buffer, offset + 32)
|
||||
return buffer
|
||||
}
|
||||
|
||||
ECSignature.prototype.toScriptSignature = function (hashType) {
|
||||
var hashTypeMod = hashType & ~0x80
|
||||
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
||||
|
|
4
test/fixtures/ecsignature.json
vendored
4
test/fixtures/ecsignature.json
vendored
|
@ -120,11 +120,11 @@
|
|||
"hex": "23987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62"
|
||||
},
|
||||
{
|
||||
"exception": "Invalid signature length",
|
||||
"exception": "Expected Buffer\\(Length: 65\\), got Buffer\\(Length: 68\\)",
|
||||
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62000000"
|
||||
},
|
||||
{
|
||||
"exception": "Invalid signature length",
|
||||
"exception": "Expected Buffer\\(Length: 65\\), got Buffer\\(Length: 59\\)",
|
||||
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e9379"
|
||||
}
|
||||
],
|
||||
|
|
Loading…
Add table
Reference in a new issue