Skip to content

Commit 39d4348

Browse files
committed
ox fixes
1 parent bbe12f5 commit 39d4348

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

packages/thirdweb/src/utils/signatures/sign-message.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import type { Prettify } from "../type-utils.js";
99
type Message = Prettify<
1010
| string
1111
| {
12-
raw: Hex | Uint8Array;
13-
}
12+
raw: Hex | Uint8Array;
13+
}
1414
>;
1515
export type SignMessageOptions = {
1616
message: Message;
@@ -66,7 +66,10 @@ export function signMessage(
6666
: ox__Hex.fromString(options.message),
6767
);
6868

69-
const signature = ox__Secp256k1.sign({ payload, privateKey: options.privateKey }),
69+
const signature = ox__Secp256k1.sign({
70+
payload,
71+
privateKey: options.privateKey,
72+
});
7073
return ox__Signature.toHex(signature);
7174
}
7275
if ("account" in options) {

packages/thirdweb/src/utils/signatures/signature-to-hex.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import * as ox__Hex from "ox/Hex";
2-
import * as ox__Signature from "ox/Signature";
3-
import type { Hex } from "../encoding/hex.js";
1+
import { secp256k1 } from "@noble/curves/secp256k1";
2+
import { type Hex, hexToBigInt } from "../encoding/hex.js";
43

5-
// We can't migrate this to Ox without breaking changes
4+
// We can't migrate this to Ox without breaking changes since Ox does not support pre-EIP1559 signatures
65

76
/**
87
* Converts a signature to a hex string.
@@ -33,9 +32,14 @@ export function signatureToHex(signature: {
3332
v?: bigint;
3433
yParity?: number;
3534
}): Hex {
36-
return ox__Signature.toHex({
37-
r: ox__Hex.toBigInt(signature.r),
38-
s: ox__Hex.toBigInt(signature.s),
39-
yParity: (signature.yParity ?? signature.v === 28n) ? 1 : 0,
40-
});
35+
const { r, s, v, yParity } = signature;
36+
const yParity_ = (() => {
37+
if (yParity === 0 || yParity === 1) return yParity;
38+
if (v && (v === 27n || v === 28n || v >= 35n)) return v % 2n === 0n ? 1 : 0;
39+
throw new Error("Invalid `v` or `yParity` value");
40+
})();
41+
return `0x${new secp256k1.Signature(
42+
hexToBigInt(r),
43+
hexToBigInt(s),
44+
).toCompactHex()}${yParity_ === 0 ? "1b" : "1c"}`;
4145
}

0 commit comments

Comments
 (0)