@aztec/foundation 2.1.0-rc.29 → 2.1.0-rc.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -97,6 +97,8 @@ export declare function maxBy<T>(arr: T[], fn: (x: T) => number | bigint): T | u
97
97
  export declare function sum(arr: number[]): number;
98
98
  /** Computes the median of a numeric array. Returns undefined if array is empty. */
99
99
  export declare function median(arr: number[]): number | undefined;
100
+ /** Computes the median of a bigint array. Returns undefined if array is empty. */
101
+ export declare function median(arr: bigint[]): bigint | undefined;
100
102
  /** Computes the mean of a numeric array. Returns undefined if the array is empty. */
101
103
  export declare function mean(values: number[]): number | undefined;
102
104
  /** Computes the variance of a numeric array. Returns undefined if there are less than 2 points. */
@@ -1 +1 @@
1
- {"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../../src/collection/array.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAEnD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EAC7C,GAAG,EAAE,CAAC,EAAE,EACR,IAAI,EAAE,CAAC,EACP,MAAM,EAAE,CAAC,EACT,QAAQ,SAAqC,GAC5C,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAMb;AAED,8EAA8E;AAC9E,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,GAAG,CAAC,EAAE,CAGrF;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAM5F;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,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AAE,CAAC,CAWpF"}
@@ -141,15 +141,24 @@
141
141
  /** Computes the sum of a numeric array. */ export function sum(arr) {
142
142
  return arr.reduce((a, b)=>a + b, 0);
143
143
  }
144
- /** Computes the median of a numeric array. Returns undefined if array is empty. */ export function median(arr) {
144
+ export function median(arr) {
145
145
  if (arr.length === 0) {
146
146
  return undefined;
147
147
  }
148
+ // Handle number array
149
+ if (typeof arr[0] === 'number') {
150
+ const sorted = [
151
+ ...arr
152
+ ].sort((a, b)=>a - b);
153
+ const mid = Math.floor(sorted.length / 2);
154
+ return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
155
+ }
156
+ // Handle bigint array
148
157
  const sorted = [
149
158
  ...arr
150
- ].sort((a, b)=>a - b);
159
+ ].sort((a, b)=>a < b ? -1 : a > b ? 1 : 0);
151
160
  const mid = Math.floor(sorted.length / 2);
152
- return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
161
+ return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2n;
153
162
  }
154
163
  /** Computes the mean of a numeric array. Returns undefined if the array is empty. */ export function mean(values) {
155
164
  if (values.length === 0) {
@@ -1,6 +1,24 @@
1
1
  import { Buffer32 } from '../../buffer/buffer32.js';
2
2
  import { EthAddress } from '../../eth-address/index.js';
3
3
  import { Signature } from '../../eth-signature/eth_signature.js';
4
+ /** Signature recovery options */
5
+ type RecoveryOpts = {
6
+ /**
7
+ * Whether to allow s-values in the high half of the curve (s >= CURVE.n/2).
8
+ * These are discouraged by EIP2 to prevent signature malleability, and outright
9
+ * rejected in OpenZeppelin's ECDSA recover, which we use in our Rollup contract.
10
+ */
11
+ allowMalleable?: boolean;
12
+ /**
13
+ * Whether to allow an y-parity 0-1 bit instead of the standard v value 27-28.
14
+ */
15
+ allowYParityAsV?: boolean;
16
+ };
17
+ export declare class Secp256k1Error extends Error {
18
+ constructor(message: string, opts?: {
19
+ cause: unknown;
20
+ });
21
+ }
4
22
  export declare function makeEthSignDigest(message: Buffer32): Buffer32;
5
23
  /**
6
24
  * Converts a private key to a public key.
@@ -18,17 +36,19 @@ export declare function addressFromPrivateKey(privateKey: Buffer): EthAddress;
18
36
  * Recovers an address from a hash and a signature.
19
37
  * @param hash - The hash to recover the address from.
20
38
  * @param signature - The signature to recover the address from.
39
+ * @param opts - Recovery options.
21
40
  * @returns The address.
22
- * @throws Error if signature recovery fails.
41
+ * @throws Error if signature recovery fails or if signature is malleable and allowMalleable is false.
23
42
  */
24
- export declare function recoverAddress(hash: Buffer32, signature: Signature): EthAddress;
43
+ export declare function recoverAddress(hash: Buffer32, signature: Signature, opts?: RecoveryOpts): EthAddress;
25
44
  /**
26
45
  * Safely attempts to recover an address from a hash and a signature.
27
46
  * @param hash - The hash to recover the address from.
28
47
  * @param signature - The signature to recover the address from.
48
+ * @param opts - Recovery options.
29
49
  * @returns The address if recovery succeeds, undefined otherwise.
30
50
  */
31
- export declare function tryRecoverAddress(hash: Buffer32, signature: Signature): EthAddress | undefined;
51
+ export declare function tryRecoverAddress(hash: Buffer32, signature: Signature, opts?: RecoveryOpts): EthAddress | undefined;
32
52
  /**
33
53
  * @attribution - viem
34
54
  * Converts a yParityOrV value to a recovery bit.
@@ -43,11 +63,29 @@ export declare function toRecoveryBit(yParityOrV: number): 0 | 1;
43
63
  * @returns The signature.
44
64
  */
45
65
  export declare function signMessage(message: Buffer32, privateKey: Buffer): Signature;
66
+ /**
67
+ * Flips an ECDSA signature.
68
+ * If the signature has a low s-value (s < CURVE.n/2), it flips it to high s-value (CURVE.n - s) and vice versa.
69
+ * Also flips the v value accordingly (27 <-> 28, or 0 <-> 1).
70
+ * This is useful for testing signature malleability handling.
71
+ * @param signature - The signature to flip.
72
+ * @returns A new signature with flipped s-value and v-value.
73
+ */
74
+ export declare function flipSignature(signature: Signature): Signature;
75
+ /**
76
+ * Normalizes an ECDSA signature.
77
+ * If the signature has a high s-value (s >= CURVE.n/2), it flips it to low s-value (CURVE.n - s), and flips v accordingly.
78
+ * If the signature uses a recovery bit of 0/1, it is converted to a v-value 27/28 for ecrecover.
79
+ * @remarks This does not handle post EIP155 tx signatures which embed the chain id in v. Use it only for feeding into ECRECOVER precompiles.
80
+ * @param signature - The signature to normalize.
81
+ */
82
+ export declare function normalizeSignature(signature: Signature): Signature;
46
83
  /**
47
84
  * Recovers a public key from a hash and a signature.
48
85
  * @param hash - The hash to recover the public key from.
49
86
  * @param signature - The signature to recover the public key from.
50
87
  * @returns The public key.
51
88
  */
52
- export declare function recoverPublicKey(hash: Buffer32, signature: Signature): Buffer;
89
+ export declare function recoverPublicKey(hash: Buffer32, signature: Signature, opts?: RecoveryOpts): Buffer;
90
+ export {};
53
91
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/crypto/secp256k1-signer/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAMjE,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,QAAQ,GAAG,QAAQ,CAG7D;AAYD;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAElE;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU,CAGpE;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,GAAG,UAAU,CAS/E;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,GAAG,UAAU,GAAG,SAAS,CAO9F;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC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AU,GAAG,SAAS,CAOnH;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,UAAU,EAAE,MAAM,SAW/C;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,aAGhE;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,CAM7D;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,CAUlE;AA6BD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,GAAE,YAAiB,GAAG,MAAM,CAYtG"}
@@ -4,6 +4,12 @@ import { EthAddress } from '../../eth-address/index.js';
4
4
  import { Signature } from '../../eth-signature/eth_signature.js';
5
5
  import { keccak256 } from '../keccak/index.js';
6
6
  const ETH_SIGN_PREFIX = '\x19Ethereum Signed Message:\n32';
7
+ export class Secp256k1Error extends Error {
8
+ constructor(message, opts){
9
+ super(message, opts);
10
+ this.name = 'Secp256k1Error';
11
+ }
12
+ }
7
13
  // We just hash the message to make it easier to work with in the smart contract.
8
14
  export function makeEthSignDigest(message) {
9
15
  const prefix = Buffer.from(ETH_SIGN_PREFIX);
@@ -39,24 +45,28 @@ export function makeEthSignDigest(message) {
39
45
  * Recovers an address from a hash and a signature.
40
46
  * @param hash - The hash to recover the address from.
41
47
  * @param signature - The signature to recover the address from.
48
+ * @param opts - Recovery options.
42
49
  * @returns The address.
43
- * @throws Error if signature recovery fails.
44
- */ export function recoverAddress(hash, signature) {
50
+ * @throws Error if signature recovery fails or if signature is malleable and allowMalleable is false.
51
+ */ export function recoverAddress(hash, signature, opts) {
45
52
  try {
46
- const publicKey = recoverPublicKey(hash, signature);
53
+ const publicKey = recoverPublicKey(hash, signature, opts);
47
54
  return publicKeyToAddress(publicKey);
48
55
  } catch (err) {
49
- throw new Error(`Error recovering Ethereum address from hash ${hash.toString()} and signature ${signature.toString()}: ${err}`);
56
+ throw new Secp256k1Error(`Error recovering Ethereum address from hash ${hash.toString()} and signature ${signature.toString()}`, {
57
+ cause: err
58
+ });
50
59
  }
51
60
  }
52
61
  /**
53
62
  * Safely attempts to recover an address from a hash and a signature.
54
63
  * @param hash - The hash to recover the address from.
55
64
  * @param signature - The signature to recover the address from.
65
+ * @param opts - Recovery options.
56
66
  * @returns The address if recovery succeeds, undefined otherwise.
57
- */ export function tryRecoverAddress(hash, signature) {
67
+ */ export function tryRecoverAddress(hash, signature, opts) {
58
68
  try {
59
- const publicKey = recoverPublicKey(hash, signature);
69
+ const publicKey = recoverPublicKey(hash, signature, opts);
60
70
  return publicKeyToAddress(publicKey);
61
71
  } catch {
62
72
  return undefined;
@@ -77,7 +87,7 @@ export function makeEthSignDigest(message) {
77
87
  if (yParityOrV === 28) {
78
88
  return 1;
79
89
  }
80
- throw new Error('Invalid yParityOrV value');
90
+ throw new Secp256k1Error(`Invalid yParityOrV value ${yParityOrV}`);
81
91
  }
82
92
  /**
83
93
  * Signs a message using ecdsa over the secp256k1 curve.
@@ -88,15 +98,73 @@ export function makeEthSignDigest(message) {
88
98
  const { r, s, recovery } = secp256k1.sign(message.buffer, privateKey);
89
99
  return new Signature(Buffer32.fromBigInt(r), Buffer32.fromBigInt(s), recovery ? 28 : 27);
90
100
  }
101
+ /**
102
+ * Flips an ECDSA signature.
103
+ * If the signature has a low s-value (s < CURVE.n/2), it flips it to high s-value (CURVE.n - s) and vice versa.
104
+ * Also flips the v value accordingly (27 <-> 28, or 0 <-> 1).
105
+ * This is useful for testing signature malleability handling.
106
+ * @param signature - The signature to flip.
107
+ * @returns A new signature with flipped s-value and v-value.
108
+ */ export function flipSignature(signature) {
109
+ const { r, s, v } = signature;
110
+ const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt());
111
+ const flippedS = secp256k1.CURVE.n - sig.s;
112
+ return new Signature(r, Buffer32.fromBigInt(flippedS), flipV(v));
113
+ }
114
+ /**
115
+ * Normalizes an ECDSA signature.
116
+ * If the signature has a high s-value (s >= CURVE.n/2), it flips it to low s-value (CURVE.n - s), and flips v accordingly.
117
+ * If the signature uses a recovery bit of 0/1, it is converted to a v-value 27/28 for ecrecover.
118
+ * @remarks This does not handle post EIP155 tx signatures which embed the chain id in v. Use it only for feeding into ECRECOVER precompiles.
119
+ * @param signature - The signature to normalize.
120
+ */ export function normalizeSignature(signature) {
121
+ const { r, s, v } = signature;
122
+ const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt());
123
+ if (sig.hasHighS()) {
124
+ const newV = flipV(v);
125
+ const newS = sig.normalizeS().s;
126
+ return new Signature(r, Buffer32.fromBigInt(newS), toVFromYParityOrV(newV));
127
+ }
128
+ return new Signature(r, s, toVFromYParityOrV(v));
129
+ }
130
+ /** Converts a yParityOrV value to a pre-EIP155 v-value 27-28. */ function toVFromYParityOrV(yParityOrV) {
131
+ if (yParityOrV === 0 || yParityOrV === 1) {
132
+ return yParityOrV + 27;
133
+ } else if (yParityOrV === 27 || yParityOrV === 28) {
134
+ return yParityOrV;
135
+ } else {
136
+ throw new Secp256k1Error(`Invalid yParityOrV value ${yParityOrV}`);
137
+ }
138
+ }
139
+ /** Flips the recovery bit or v-value */ function flipV(v) {
140
+ switch(v){
141
+ case 27:
142
+ return 28;
143
+ case 28:
144
+ return 27;
145
+ case 0:
146
+ return 1;
147
+ case 1:
148
+ return 0;
149
+ default:
150
+ throw new Secp256k1Error(`Invalid v value ${v}`);
151
+ }
152
+ }
91
153
  /**
92
154
  * Recovers a public key from a hash and a signature.
93
155
  * @param hash - The hash to recover the public key from.
94
156
  * @param signature - The signature to recover the public key from.
95
157
  * @returns The public key.
96
- */ export function recoverPublicKey(hash, signature) {
158
+ */ export function recoverPublicKey(hash, signature, opts = {}) {
97
159
  const { r, s, v } = signature;
160
+ if (!opts.allowYParityAsV && v !== 27 && v !== 28) {
161
+ throw new Secp256k1Error(`Invalid v value ${v} (expected 27 or 28)`);
162
+ }
98
163
  const recoveryBit = toRecoveryBit(v);
99
164
  const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt()).addRecoveryBit(recoveryBit);
165
+ if (!opts.allowMalleable && sig.hasHighS()) {
166
+ throw new Secp256k1Error('Signature has high s-value (malleable signature)');
167
+ }
100
168
  const publicKey = sig.recoverPublicKey(hash.buffer).toHex(false);
101
169
  return Buffer.from(publicKey, 'hex');
102
170
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/foundation",
3
- "version": "2.1.0-rc.29",
3
+ "version": "2.1.0-rc.30",
4
4
  "type": "module",
5
5
  "main": "./dest/index.js",
6
6
  "types": "./dest/index.d.ts",
@@ -103,7 +103,7 @@
103
103
  "testEnvironment": "../../foundation/src/jest/env.mjs"
104
104
  },
105
105
  "dependencies": {
106
- "@aztec/bb.js": "2.1.0-rc.29",
106
+ "@aztec/bb.js": "2.1.0-rc.30",
107
107
  "@koa/cors": "^5.0.0",
108
108
  "@noble/curves": "=1.7.0",
109
109
  "@noble/hashes": "^1.6.1",
@@ -178,13 +178,25 @@ export function sum(arr: number[]): number {
178
178
  }
179
179
 
180
180
  /** Computes the median of a numeric array. Returns undefined if array is empty. */
181
- export function median(arr: number[]) {
181
+ export function median(arr: number[]): number | undefined;
182
+ /** Computes the median of a bigint array. Returns undefined if array is empty. */
183
+ export function median(arr: bigint[]): bigint | undefined;
184
+ export function median(arr: number[] | bigint[]): number | bigint | undefined {
182
185
  if (arr.length === 0) {
183
186
  return undefined;
184
187
  }
185
- const sorted = [...arr].sort((a, b) => a - b);
188
+
189
+ // Handle number array
190
+ if (typeof arr[0] === 'number') {
191
+ const sorted = [...(arr as number[])].sort((a, b) => a - b);
192
+ const mid = Math.floor(sorted.length / 2);
193
+ return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
194
+ }
195
+
196
+ // Handle bigint array
197
+ const sorted = [...(arr as bigint[])].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
186
198
  const mid = Math.floor(sorted.length / 2);
187
- return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
199
+ return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2n;
188
200
  }
189
201
 
190
202
  /** Computes the mean of a numeric array. Returns undefined if the array is empty. */
@@ -7,6 +7,27 @@ import { keccak256 } from '../keccak/index.js';
7
7
 
8
8
  const ETH_SIGN_PREFIX = '\x19Ethereum Signed Message:\n32';
9
9
 
10
+ /** Signature recovery options */
11
+ type RecoveryOpts = {
12
+ /**
13
+ * Whether to allow s-values in the high half of the curve (s >= CURVE.n/2).
14
+ * These are discouraged by EIP2 to prevent signature malleability, and outright
15
+ * rejected in OpenZeppelin's ECDSA recover, which we use in our Rollup contract.
16
+ */
17
+ allowMalleable?: boolean;
18
+ /**
19
+ * Whether to allow an y-parity 0-1 bit instead of the standard v value 27-28.
20
+ */
21
+ allowYParityAsV?: boolean;
22
+ };
23
+
24
+ export class Secp256k1Error extends Error {
25
+ constructor(message: string, opts?: { cause: unknown }) {
26
+ super(message, opts);
27
+ this.name = 'Secp256k1Error';
28
+ }
29
+ }
30
+
10
31
  // We just hash the message to make it easier to work with in the smart contract.
11
32
  export function makeEthSignDigest(message: Buffer32): Buffer32 {
12
33
  const prefix = Buffer.from(ETH_SIGN_PREFIX);
@@ -46,16 +67,18 @@ export function addressFromPrivateKey(privateKey: Buffer): EthAddress {
46
67
  * Recovers an address from a hash and a signature.
47
68
  * @param hash - The hash to recover the address from.
48
69
  * @param signature - The signature to recover the address from.
70
+ * @param opts - Recovery options.
49
71
  * @returns The address.
50
- * @throws Error if signature recovery fails.
72
+ * @throws Error if signature recovery fails or if signature is malleable and allowMalleable is false.
51
73
  */
52
- export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress {
74
+ export function recoverAddress(hash: Buffer32, signature: Signature, opts?: RecoveryOpts): EthAddress {
53
75
  try {
54
- const publicKey = recoverPublicKey(hash, signature);
76
+ const publicKey = recoverPublicKey(hash, signature, opts);
55
77
  return publicKeyToAddress(publicKey);
56
- } catch (err) {
57
- throw new Error(
58
- `Error recovering Ethereum address from hash ${hash.toString()} and signature ${signature.toString()}: ${err}`,
78
+ } catch (err: unknown) {
79
+ throw new Secp256k1Error(
80
+ `Error recovering Ethereum address from hash ${hash.toString()} and signature ${signature.toString()}`,
81
+ { cause: err },
59
82
  );
60
83
  }
61
84
  }
@@ -64,11 +87,12 @@ export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress
64
87
  * Safely attempts to recover an address from a hash and a signature.
65
88
  * @param hash - The hash to recover the address from.
66
89
  * @param signature - The signature to recover the address from.
90
+ * @param opts - Recovery options.
67
91
  * @returns The address if recovery succeeds, undefined otherwise.
68
92
  */
69
- export function tryRecoverAddress(hash: Buffer32, signature: Signature): EthAddress | undefined {
93
+ export function tryRecoverAddress(hash: Buffer32, signature: Signature, opts?: RecoveryOpts): EthAddress | undefined {
70
94
  try {
71
- const publicKey = recoverPublicKey(hash, signature);
95
+ const publicKey = recoverPublicKey(hash, signature, opts);
72
96
  return publicKeyToAddress(publicKey);
73
97
  } catch {
74
98
  return undefined;
@@ -91,7 +115,7 @@ export function toRecoveryBit(yParityOrV: number) {
91
115
  if (yParityOrV === 28) {
92
116
  return 1;
93
117
  }
94
- throw new Error('Invalid yParityOrV value');
118
+ throw new Secp256k1Error(`Invalid yParityOrV value ${yParityOrV}`);
95
119
  }
96
120
 
97
121
  /**
@@ -105,16 +129,84 @@ export function signMessage(message: Buffer32, privateKey: Buffer) {
105
129
  return new Signature(Buffer32.fromBigInt(r), Buffer32.fromBigInt(s), recovery ? 28 : 27);
106
130
  }
107
131
 
132
+ /**
133
+ * Flips an ECDSA signature.
134
+ * If the signature has a low s-value (s < CURVE.n/2), it flips it to high s-value (CURVE.n - s) and vice versa.
135
+ * Also flips the v value accordingly (27 <-> 28, or 0 <-> 1).
136
+ * This is useful for testing signature malleability handling.
137
+ * @param signature - The signature to flip.
138
+ * @returns A new signature with flipped s-value and v-value.
139
+ */
140
+ export function flipSignature(signature: Signature): Signature {
141
+ const { r, s, v } = signature;
142
+ const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt());
143
+ const flippedS = secp256k1.CURVE.n - sig.s;
144
+
145
+ return new Signature(r, Buffer32.fromBigInt(flippedS), flipV(v));
146
+ }
147
+
148
+ /**
149
+ * Normalizes an ECDSA signature.
150
+ * If the signature has a high s-value (s >= CURVE.n/2), it flips it to low s-value (CURVE.n - s), and flips v accordingly.
151
+ * If the signature uses a recovery bit of 0/1, it is converted to a v-value 27/28 for ecrecover.
152
+ * @remarks This does not handle post EIP155 tx signatures which embed the chain id in v. Use it only for feeding into ECRECOVER precompiles.
153
+ * @param signature - The signature to normalize.
154
+ */
155
+ export function normalizeSignature(signature: Signature): Signature {
156
+ const { r, s, v } = signature;
157
+ const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt());
158
+ if (sig.hasHighS()) {
159
+ const newV = flipV(v);
160
+ const newS = sig.normalizeS().s;
161
+ return new Signature(r, Buffer32.fromBigInt(newS), toVFromYParityOrV(newV));
162
+ }
163
+
164
+ return new Signature(r, s, toVFromYParityOrV(v));
165
+ }
166
+
167
+ /** Converts a yParityOrV value to a pre-EIP155 v-value 27-28. */
168
+ function toVFromYParityOrV(yParityOrV: number): number {
169
+ if (yParityOrV === 0 || yParityOrV === 1) {
170
+ return yParityOrV + 27;
171
+ } else if (yParityOrV === 27 || yParityOrV === 28) {
172
+ return yParityOrV;
173
+ } else {
174
+ throw new Secp256k1Error(`Invalid yParityOrV value ${yParityOrV}`);
175
+ }
176
+ }
177
+
178
+ /** Flips the recovery bit or v-value */
179
+ function flipV(v: number): number {
180
+ switch (v) {
181
+ case 27:
182
+ return 28;
183
+ case 28:
184
+ return 27;
185
+ case 0:
186
+ return 1;
187
+ case 1:
188
+ return 0;
189
+ default:
190
+ throw new Secp256k1Error(`Invalid v value ${v}`);
191
+ }
192
+ }
193
+
108
194
  /**
109
195
  * Recovers a public key from a hash and a signature.
110
196
  * @param hash - The hash to recover the public key from.
111
197
  * @param signature - The signature to recover the public key from.
112
198
  * @returns The public key.
113
199
  */
114
- export function recoverPublicKey(hash: Buffer32, signature: Signature): Buffer {
200
+ export function recoverPublicKey(hash: Buffer32, signature: Signature, opts: RecoveryOpts = {}): Buffer {
115
201
  const { r, s, v } = signature;
202
+ if (!opts.allowYParityAsV && v !== 27 && v !== 28) {
203
+ throw new Secp256k1Error(`Invalid v value ${v} (expected 27 or 28)`);
204
+ }
116
205
  const recoveryBit = toRecoveryBit(v);
117
206
  const sig = new secp256k1.Signature(r.toBigInt(), s.toBigInt()).addRecoveryBit(recoveryBit);
207
+ if (!opts.allowMalleable && sig.hasHighS()) {
208
+ throw new Secp256k1Error('Signature has high s-value (malleable signature)');
209
+ }
118
210
  const publicKey = sig.recoverPublicKey(hash.buffer).toHex(false);
119
211
  return Buffer.from(publicKey, 'hex');
120
212
  }