Skip to content

Commit e3cda43

Browse files
committed
refactor(sdk): migrate more functions to ox
1 parent ec1f2cc commit e3cda43

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { secp256k1 } from "@noble/curves/secp256k1";
2-
import type { Signature } from "viem";
1+
import * as ox__Secp256k1 from "ox/Secp256k1";
32

43
import { type Hex, toHex } from "../encoding/hex.js";
54

@@ -28,12 +27,12 @@ export type SignOptions = {
2827
* ```
2928
* @utils
3029
*/
31-
export function sign({ hash, privateKey }: SignOptions): Signature {
32-
const { r, s, recovery } = secp256k1.sign(hash.slice(2), privateKey.slice(2));
30+
export function sign({ hash, privateKey }: SignOptions) {
31+
const { r, s, yParity } = ox__Secp256k1.sign({ payload: hash, privateKey });
3332
return {
3433
r: toHex(r, { size: 32 }),
3534
s: toHex(s, { size: 32 }),
36-
v: recovery ? 28n : 27n,
37-
yParity: recovery,
35+
v: yParity === 1 ? 28n : 27n,
36+
yParity,
3837
};
3938
}

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

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

45
// We can't migrate this to Ox without breaking changes
56

@@ -32,14 +33,9 @@ export function signatureToHex(signature: {
3233
v?: bigint;
3334
yParity?: number;
3435
}): Hex {
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"}`;
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+
});
4541
}

0 commit comments

Comments
 (0)