Skip to content

Commit b94da10

Browse files
committed
ECSignature: add toRSBuffer/fromRSBuffer
1 parent a301aa8 commit b94da10

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/ecsignature.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,30 @@ function ECSignature (r, s) {
1212
}
1313

1414
ECSignature.parseCompact = function (buffer) {
15-
if (buffer.length !== 65) throw new Error('Invalid signature length')
15+
typeforce(types.BufferN(65), buffer)
1616

1717
var flagByte = buffer.readUInt8(0) - 27
1818
if (flagByte !== (flagByte & 7)) throw new Error('Invalid signature parameter')
1919

2020
var compressed = !!(flagByte & 4)
2121
var recoveryParam = flagByte & 3
22-
23-
var r = BigInteger.fromBuffer(buffer.slice(1, 33))
24-
var s = BigInteger.fromBuffer(buffer.slice(33))
22+
var signature = ECSignature.fromRSBuffer(buffer.slice(1))
2523

2624
return {
2725
compressed: compressed,
2826
i: recoveryParam,
29-
signature: new ECSignature(r, s)
27+
signature: signature
3028
}
3129
}
3230

31+
ECSignature.fromRSBuffer = function (buffer) {
32+
typeforce(types.BufferN(64), buffer)
33+
34+
var r = BigInteger.fromBuffer(buffer.slice(0, 32))
35+
var s = BigInteger.fromBuffer(buffer.slice(32, 64))
36+
return new ECSignature(r, s)
37+
}
38+
3339
ECSignature.fromDER = function (buffer) {
3440
var decode = bip66.decode(buffer)
3541
var r = BigInteger.fromDERInteger(decode.r)
@@ -60,9 +66,7 @@ ECSignature.prototype.toCompact = function (i, compressed) {
6066

6167
var buffer = Buffer.alloc(65)
6268
buffer.writeUInt8(i, 0)
63-
this.r.toBuffer(32).copy(buffer, 1)
64-
this.s.toBuffer(32).copy(buffer, 33)
65-
69+
this.toRSBuffer(buffer, 1)
6670
return buffer
6771
}
6872

@@ -73,6 +77,13 @@ ECSignature.prototype.toDER = function () {
7377
return bip66.encode(r, s)
7478
}
7579

80+
ECSignature.prototype.toRSBuffer = function (buffer, offset) {
81+
buffer = buffer || Buffer.alloc(64)
82+
this.r.toBuffer(32).copy(buffer, offset)
83+
this.s.toBuffer(32).copy(buffer, offset + 32)
84+
return buffer
85+
}
86+
7687
ECSignature.prototype.toScriptSignature = function (hashType) {
7788
var hashTypeMod = hashType & ~0x80
7889
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)

test/fixtures/ecsignature.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@
120120
"hex": "23987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62"
121121
},
122122
{
123-
"exception": "Invalid signature length",
123+
"exception": "Expected Buffer\\(Length: 65\\), got Buffer\\(Length: 68\\)",
124124
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62000000"
125125
},
126126
{
127-
"exception": "Invalid signature length",
127+
"exception": "Expected Buffer\\(Length: 65\\), got Buffer\\(Length: 59\\)",
128128
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e9379"
129129
}
130130
],

0 commit comments

Comments
 (0)