Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: signing raw messages in ethers adapter #3366

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-cherries-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix sign raw message through ethers 5/6 adapters
14 changes: 14 additions & 0 deletions packages/thirdweb/src/adapters/ethers5.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, test } from "vitest";
import { ANVIL_CHAIN } from "../../test/src/chains.js";
import { TEST_CLIENT } from "../../test/src/test-clients.js";
import { ANVIL_PKEY_A, TEST_ACCOUNT_B } from "../../test/src/test-wallets.js";
import { randomBytesBuffer } from "../utils/random.js";
import { privateKeyToAccount } from "../wallets/private-key.js";
import { toEthersSigner } from "./ethers5.js";

Expand Down Expand Up @@ -35,6 +36,19 @@ describe("ethers5 adapter", () => {
expect(sig).toBe(expectedSig);
});

test("should sign raw message", async () => {
const signer = await toEthersSigner(
ethers5,
TEST_CLIENT,
account,
ANVIL_CHAIN,
);
const bytes = randomBytesBuffer(32);
const expectedSig = await account.signMessage({ message: { raw: bytes } });
const sig = await signer.signMessage(bytes);
expect(sig).toBe(expectedSig);
});

test("should sign typed data", async () => {
const signer = await toEthersSigner(
ethers5,
Expand Down
5 changes: 2 additions & 3 deletions packages/thirdweb/src/adapters/ethers5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { type ThirdwebContract, getContract } from "../contract/contract.js";
import { sendTransaction } from "../transaction/actions/send-transaction.js";
import { waitForReceipt } from "../transaction/actions/wait-for-tx-receipt.js";
import { prepareTransaction } from "../transaction/prepare-transaction.js";
import { toHex, uint8ArrayToHex } from "../utils/encoding/hex.js";
import { toHex } from "../utils/encoding/hex.js";
import type { Account } from "../wallets/interfaces/wallet.js";

type Ethers5 = typeof ethers5;
Expand Down Expand Up @@ -306,8 +306,7 @@ export async function toEthersSigner(
throw new Error("Account not found");
}
return account.signMessage({
message:
typeof message === "string" ? message : uint8ArrayToHex(message),
message: typeof message === "string" ? message : { raw: message },
});
}
/**
Expand Down
16 changes: 10 additions & 6 deletions packages/thirdweb/src/adapters/ethers6.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, test } from "vitest";
import { ANVIL_CHAIN } from "../../test/src/chains.js";
import { TEST_CLIENT } from "../../test/src/test-clients.js";
import { ANVIL_PKEY_A, TEST_ACCOUNT_B } from "../../test/src/test-wallets.js";
import { randomBytesBuffer } from "../utils/random.js";
import { privateKeyToAccount } from "../wallets/private-key.js";
import { toEthersSigner } from "./ethers6.js";

Expand All @@ -24,17 +25,20 @@ describe("toEthersSigner", () => {
});

test("should sign message", async () => {
const signer = await toEthersSigner(
ethers6,
TEST_CLIENT,
account,
ANVIL_CHAIN,
);
const signer = toEthersSigner(ethers6, TEST_CLIENT, account, ANVIL_CHAIN);
const expectedSig = await account.signMessage({ message: "hello world" });
const sig = await signer.signMessage("hello world");
expect(sig).toBe(expectedSig);
});

test("should sign raw message", async () => {
const signer = toEthersSigner(ethers6, TEST_CLIENT, account, ANVIL_CHAIN);
const bytes = randomBytesBuffer(32);
const expectedSig = await account.signMessage({ message: { raw: bytes } });
const sig = await signer.signMessage(bytes);
expect(sig).toBe(expectedSig);
});

test("should sign typed data", async () => {
const signer = await toEthersSigner(
ethers6,
Expand Down
5 changes: 2 additions & 3 deletions packages/thirdweb/src/adapters/ethers6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ThirdwebClient } from "../client/client.js";
import { type ThirdwebContract, getContract } from "../contract/contract.js";
import { toSerializableTransaction } from "../transaction/actions/to-serializable-transaction.js";
import type { PreparedTransaction } from "../transaction/prepare-transaction.js";
import { toHex, uint8ArrayToHex } from "../utils/encoding/hex.js";
import { toHex } from "../utils/encoding/hex.js";
import { resolvePromisedValue } from "../utils/promise/resolve-promised-value.js";
import type { Account } from "../wallets/interfaces/wallet.js";
import { normalizeChainId } from "../wallets/utils/normalizeChainId.js";
Expand Down Expand Up @@ -376,8 +376,7 @@ export function toEthersSigner(
throw new Error("Account not found");
}
return account.signMessage({
message:
typeof message === "string" ? message : uint8ArrayToHex(message),
message: typeof message === "string" ? message : { raw: message },
});
}

Expand Down
26 changes: 26 additions & 0 deletions packages/thirdweb/src/adapters/viem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { typedData } from "~test/typed-data.js";

import { ANVIL_CHAIN, FORKED_ETHEREUM_CHAIN } from "../../test/src/chains.js";
import { TEST_CLIENT } from "../../test/src/test-clients.js";
import { randomBytesBuffer } from "../utils/random.js";
import { privateKeyToAccount } from "../wallets/private-key.js";
import { toViemContract, viemAdapter } from "./viem.js";

Expand All @@ -35,6 +36,31 @@ describe("walletClient.toViem", () => {
expect(walletClient.signMessage).toBeDefined();
});

test("should sign message", async () => {
if (!walletClient.account) {
throw new Error("Account not found");
}
const expectedSig = await account.signMessage({ message: "hello world" });
const sig = await walletClient.signMessage({
account: walletClient.account,
message: "hello world",
});
expect(sig).toBe(expectedSig);
});

test("should sign raw message", async () => {
if (!walletClient.account) {
throw new Error("Account not found");
}
const bytes = randomBytesBuffer(32);
const expectedSig = await account.signMessage({ message: { raw: bytes } });
const sig = await walletClient.signMessage({
account: walletClient.account,
message: { raw: bytes },
});
expect(sig).toBe(expectedSig);
});

test("should sign typed data", async () => {
expect(walletClient.signTypedData).toBeDefined();

Expand Down
Loading