Skip to content

Commit 85e2193

Browse files
committed
feat(sdk): migrate signMessage to ox
1 parent 17488b8 commit 85e2193

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import * as ox__Hex from "ox/Hex";
2+
import * as ox__PersonalMessage from "ox/PersonalMessage";
3+
import * as ox__Secp256k1 from "ox/Secp256k1";
4+
import * as ox__Signature from "ox/Signature";
15
import type { Account } from "../../wallets/interfaces/wallet.js";
26
import type { Hex } from "../encoding/hex.js";
3-
import { hashMessage } from "../hashing/hashMessage.js";
47
import type { Prettify } from "../type-utils.js";
5-
import { sign } from "./sign.js";
6-
import { signatureToHex } from "./signature-to-hex.js";
78

89
type Message = Prettify<
910
| string
@@ -59,9 +60,14 @@ export function signMessage(
5960
options: SignMessageOptions | { message: Message; account: Account },
6061
): Hex | Promise<Hex> {
6162
if ("privateKey" in options) {
62-
const { message, privateKey } = options;
63-
const signature = sign({ hash: hashMessage(message), privateKey });
64-
return signatureToHex(signature);
63+
const payload = ox__PersonalMessage.getSignPayload(
64+
typeof options.message === "object"
65+
? options.message.raw
66+
: ox__Hex.fromString(options.message),
67+
);
68+
return ox__Signature.toHex(
69+
ox__Secp256k1.sign({ payload, privateKey: options.privateKey }),
70+
);
6571
}
6672
if ("account" in options) {
6773
const { message, account } = options;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { secp256k1 } from "@noble/curves/secp256k1";
2-
import type { Signature } from "viem";
32
import { type Hex, hexToBigInt } from "../encoding/hex.js";
43

4+
// We can't migrate this to Ox without breaking changes
5+
56
/**
67
* Converts a signature to a hex string.
78
* @param signature The signature to convert.
@@ -25,7 +26,12 @@ import { type Hex, hexToBigInt } from "../encoding/hex.js";
2526
* ```
2627
* @utils
2728
*/
28-
export function signatureToHex(signature: Signature): Hex {
29+
export function signatureToHex(signature: {
30+
r: Hex;
31+
s: Hex;
32+
v?: bigint;
33+
yParity?: number;
34+
}): Hex {
2935
const { r, s, v, yParity } = signature;
3036
const yParity_ = (() => {
3137
if (yParity === 0 || yParity === 1) return yParity;

0 commit comments

Comments
 (0)