From a63f84cda771e44bfb97d4dabd93f15504488802 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Fri, 30 Dec 2022 16:32:17 -0500 Subject: [PATCH] Allow non-bytes32 values for r and s in Signature. --- src.ts/crypto/signature.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src.ts/crypto/signature.ts b/src.ts/crypto/signature.ts index b6f94df4c..183f67d0e 100644 --- a/src.ts/crypto/signature.ts +++ b/src.ts/crypto/signature.ts @@ -1,7 +1,7 @@ import { ZeroHash } from "../constants/index.js"; import { concat, dataLength, getBigInt, getBytes, getNumber, hexlify, - isHexString, + toBeArray, isHexString, zeroPadValue, assertArgument, assertPrivate } from "../utils/index.js"; @@ -42,6 +42,10 @@ export type SignatureLike = Signature | string | { yParityAndS?: string; }; +function toUint256(value: BigNumberish): string { + return zeroPadValue(toBeArray(value), 32); +} + /** * A Signature @TODO */ @@ -291,16 +295,13 @@ export class Signature { if (sig instanceof Signature) { return sig.clone(); } // Get r - const r = sig.r; - assertError(r != null, "missing r"); - assertError(isHexString(r, 32), "invalid r"); + const _r = sig.r; + assertError(_r != null, "missing r"); + const r = toUint256(_r); // Get s; by any means necessary (we check consistency below) const s = (function(s?: string, yParityAndS?: string) { - if (s != null) { - assertError(isHexString(s, 32), "invalid s"); - return s; - } + if (s != null) { return toUint256(s); } if (yParityAndS != null) { assertError(isHexString(yParityAndS, 32), "invalid yParityAndS");