crypto: Add Secp256k1Signature.toFixedLength method

This commit is contained in:
willclarktech 2020-09-10 11:51:39 +02:00
parent 78fb56856c
commit 4fe034e994
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
3 changed files with 14 additions and 0 deletions

View File

@ -76,6 +76,15 @@ describe("Secp256k1Signature", () => {
).toThrowError(/unsigned integer s must be encoded as unpadded big endian./i);
});
it("can be encoded as fixed length", () => {
const signature = new Secp256k1Signature(new Uint8Array([0x22, 0x33]), new Uint8Array([0xaa]));
expect(signature.toFixedLength()).toEqual(
fromHex(
"000000000000000000000000000000000000000000000000000000000000223300000000000000000000000000000000000000000000000000000000000000aa",
),
);
});
it("can encode to DER", () => {
// Signature 3045022100f25b86e1d8a11d72475b3ed273b0781c7d7f6f9e1dae0dd5d3ee9b84f3fab891022063d9c4e1391de077244583e9a6e3d8e8e1f236a3bf5963735353b93b1a3ba935
// decoded by http://asn1-playground.oss.com/

View File

@ -119,6 +119,10 @@ export class Secp256k1Signature {
}
}
public toFixedLength(): Uint8Array {
return new Uint8Array([...this.r(32), ...this.s(32)]);
}
public toDer(): Uint8Array {
// DER supports negative integers but our data is unsigned. Thus we need to prepend
// a leading 0 byte when the higest bit is set to differentiate nagative values

View File

@ -12,6 +12,7 @@ export declare class Secp256k1Signature {
constructor(r: Uint8Array, s: Uint8Array);
r(length?: number): Uint8Array;
s(length?: number): Uint8Array;
toFixedLength(): Uint8Array;
toDer(): Uint8Array;
}
/**