Skip to content

Commit 014fbf1

Browse files
committed
add tests
1 parent 36de96f commit 014fbf1

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

packages/thirdweb/src/auth/verify-hash.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ export async function verifyHash({
125125
try {
126126
const result = await eth_call(rpcRequest, verificationData);
127127
return hexToBool(result);
128-
} catch (err) {
129-
console.error("Error verifying ERC-6492 signature", err);
128+
} catch (_err) {
130129
// Some chains do not support the eth_call simulation and will fail, so we fall back to regular EIP1271 validation
131130
const validEip1271 = await verifyEip1271Signature({
132131
hash,

packages/thirdweb/src/auth/verify-signature.test.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import * as ox__Bytes from "ox/Bytes";
12
import { describe, expect, it, test } from "vitest";
23
import { FORKED_ETHEREUM_CHAIN } from "../../test/src/chains.js";
34
import { TEST_CLIENT } from "../../test/src/test-clients.js";
45
import { TEST_ACCOUNT_A } from "../../test/src/test-wallets.js";
5-
import { mainnet } from "../chains/chain-definitions/ethereum.js";
6+
import { ethereum, mainnet } from "../chains/chain-definitions/ethereum.js";
67
import { sepolia } from "../chains/chain-definitions/sepolia.js";
7-
import { verifyEOASignature, verifySignature } from "./verify-signature.js";
8+
import { smartWallet } from "../wallets/smart/smart-wallet.js";
9+
import {
10+
verifyContractWalletSignature,
11+
verifyEOASignature,
12+
verifySignature,
13+
} from "./verify-signature.js";
814

915
describe("verifyEOASignature", () => {
1016
test("should return true for a valid signature", async () => {
@@ -98,3 +104,43 @@ describe.runIf(process.env.TW_SECRET_KEY)(
98104
});
99105
},
100106
);
107+
108+
describe.runIf(process.env.TW_SECRET_KEY)(
109+
"verifyContractWalletSignature",
110+
async () => {
111+
const message = "Hakuna matata";
112+
const wallet = smartWallet({
113+
chain: ethereum,
114+
gasless: true,
115+
});
116+
const smartAccount = await wallet.connect({
117+
client: TEST_CLIENT,
118+
personalAccount: TEST_ACCOUNT_A,
119+
});
120+
121+
test("should verify a smart account signature", async () => {
122+
const rawSignature = await smartAccount.signMessage({ message });
123+
const result = await verifyContractWalletSignature({
124+
signature: rawSignature,
125+
message,
126+
address: smartAccount.address,
127+
chain: ethereum,
128+
client: TEST_CLIENT,
129+
});
130+
expect(result).toBe(true);
131+
});
132+
133+
test("should verify a smart account signature as bytes", async () => {
134+
const rawSignature = await smartAccount.signMessage({ message });
135+
const bytesSignature = ox__Bytes.fromHex(rawSignature);
136+
const result = await verifyContractWalletSignature({
137+
signature: bytesSignature,
138+
message,
139+
address: smartAccount.address,
140+
chain: ethereum,
141+
client: TEST_CLIENT,
142+
});
143+
expect(result).toBe(true);
144+
});
145+
},
146+
);

packages/thirdweb/src/auth/verify-signature.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as ox__Bytes from "ox/Bytes";
2-
import * as ox__Hex from "ox/Hex";
32
import * as ox__Secp256k1 from "ox/Secp256k1";
43
import * as ox__Signature from "ox/Signature";
54
import type { Chain } from "../chains/types.js";
@@ -21,7 +20,7 @@ type Message = Prettify<
2120
*/
2221
export type VerifyEOASignatureParams = {
2322
message: string | Message;
24-
signature: string | Uint8Array | ox__Signature.Signature;
23+
signature: string | Uint8Array;
2524
address: string;
2625
};
2726

@@ -116,32 +115,14 @@ export async function verifyContractWalletSignature({
116115

117116
const parsedSignature = (() => {
118117
if (ox__Bytes.validate(signature)) {
119-
const sig = ox__Signature.fromBytes(signature);
120-
return {
121-
r: ox__Hex.fromNumber(sig.r),
122-
s: ox__Hex.fromNumber(sig.s),
123-
yParity: sig.yParity,
124-
};
125-
} else if (typeof signature === "object") {
126-
return {
127-
r: ox__Hex.fromNumber(signature.r),
128-
s: ox__Hex.fromNumber(signature.s),
129-
yParity: signature.yParity,
130-
};
118+
return ox__Bytes.toHex(signature);
131119
}
132120
return signature;
133121
})();
134122

135123
return verifyHash({
136124
hash: messageHash,
137-
signature:
138-
typeof parsedSignature === "string"
139-
? parsedSignature
140-
: {
141-
r: ox__Hex.toBigInt(parsedSignature.r),
142-
s: ox__Hex.toBigInt(parsedSignature.s),
143-
yParity: parsedSignature.yParity,
144-
},
125+
signature: parsedSignature,
145126
address,
146127
client,
147128
chain,

0 commit comments

Comments
 (0)