Skip to content

Commit d4b7f8a

Browse files
Add Sophon Labs account integration for EIP-6963 support
1 parent 8bb2b0a commit d4b7f8a

File tree

9 files changed

+919
-40
lines changed

9 files changed

+919
-40
lines changed

apps/playground-web/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"dependencies": {
3+
"@sophon-labs/account-core": "^1.3.7",
4+
"@sophon-labs/account-eip6963": "^1.3.7",
35
"@abstract-foundation/agw-react": "^1.6.4",
46
"@hookform/resolvers": "^3.9.1",
57
"@radix-ui/react-checkbox": "^1.3.2",

apps/playground-web/src/app/providers.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { ThemeProviderProps } from "next-themes";
55
import { ThemeProvider as NextThemesProvider } from "next-themes";
66
import type * as React from "react";
77

8+
import "@sophon-labs/account-eip6963/mainnet";
9+
810
const queryClient = new QueryClient();
911

1012
export const Providers: React.FC<React.PropsWithChildren> = ({ children }) => {

apps/playground-web/src/app/wallets/auth/server/actions/auth.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export async function generatePayload(options: GenerateLoginPayloadParams) {
2424
}
2525

2626
export async function login(payload: VerifyLoginPayloadParams) {
27+
console.log("payload", payload);
2728
const verifiedPayload = await thirdwebAuth.verifyPayload(payload);
29+
console.log("verifiedPayload", verifiedPayload);
2830
if (verifiedPayload.valid) {
2931
const jwt = await thirdwebAuth.generateJWT({
3032
payload: verifiedPayload.payload,

apps/playground-web/src/components/auth/auth-button.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
logout,
1010
} from "@/app/wallets/auth/server/actions/auth";
1111
import { THIRDWEB_CLIENT } from "@/lib/client";
12+
import { defineChain } from "thirdweb";
1213

1314
export function AuthButton() {
1415
return (
@@ -17,10 +18,11 @@ export function AuthButton() {
1718
doLogin: (params) => login(params),
1819
doLogout: () => logout(),
1920
getLoginPayload: ({ address }) =>
20-
generatePayload({ address, chainId: 84532 }),
21+
generatePayload({ address, chainId: 50104 }),
2122
isLoggedIn: (address) => isLoggedIn(address),
2223
}}
2324
client={THIRDWEB_CLIENT}
25+
chain={defineChain(50104)}
2426
wallets={[
2527
inAppWallet({
2628
auth: {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export async function verifyHash({
8686
}),
8787
);
8888

89+
console.log("isDeployed", isDeployed);
90+
8991
if (isDeployed) {
9092
const validEip1271 = await verifyEip1271Signature({
9193
contract: getContract({
@@ -195,6 +197,13 @@ export async function verifyEip1271Signature({
195197
hash,
196198
signature,
197199
});
200+
console.log("result", {
201+
hash,
202+
address: contract.address,
203+
chain: contract.chain.id,
204+
signature,
205+
result,
206+
});
198207
return result === EIP_1271_MAGIC_VALUE;
199208
} catch (err) {
200209
console.error("Error verifying EIP-1271 signature", err);

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import * as ox__Secp256k1 from "ox/Secp256k1";
33
import * as ox__Signature from "ox/Signature";
44
import type { Chain } from "../chains/types.js";
55
import type { ThirdwebClient } from "../client/client.js";
6-
import { type Hex, isHex } from "../utils/encoding/hex.js";
6+
import { type Hex, isHex, stringToHex } from "../utils/encoding/hex.js";
77
import { hashMessage } from "../utils/hashing/hashMessage.js";
88
import type { Prettify } from "../utils/type-utils.js";
99
import { verifyHash } from "./verify-hash.js";
10+
import { recoverAddress, verifyMessage } from "viem";
1011

1112
type Message = Prettify<
1213
| string
@@ -48,11 +49,27 @@ export async function verifyEOASignature(options: VerifyEOASignatureParams) {
4849
return false;
4950
}
5051

52+
const viewm = await verifyMessage({
53+
message: options.message,
54+
signature: options.signature,
55+
address: options.address,
56+
});
57+
58+
const viemRecoveredAddress = await recoverAddress({
59+
hash: messageHash,
60+
signature: options.signature,
61+
});
62+
63+
console.log("viewm", viewm);
64+
console.log("viemRecoveredAddress", viemRecoveredAddress);
65+
5166
const recoveredAddress = ox__Secp256k1.recoverAddress({
5267
payload: messageHash,
5368
signature: ox__Signature.fromHex(options.signature),
5469
});
5570

71+
console.log("recoveredAddress", recoveredAddress);
72+
5673
if (recoveredAddress.toLowerCase() === options.address.toLowerCase()) {
5774
return true;
5875
}
@@ -111,7 +128,8 @@ export async function verifyContractWalletSignature({
111128
client,
112129
accountFactory,
113130
}: VerifyContractWalletSignatureParams) {
114-
const messageHash = hashMessage(message);
131+
// FIXME undo stringToHex, but this is what fixes sophon
132+
const messageHash = hashMessage(stringToHex(message as string));
115133

116134
const parsedSignature = (() => {
117135
if (ox__Bytes.validate(signature)) {
@@ -167,6 +185,7 @@ export async function verifySignature(options: VerifySignatureParams) {
167185
// no-op, we skip to contract signature check
168186
}
169187
if (isVerifyContractWalletSignatureParams(options)) {
188+
console.log("isVerifyContractWalletSignatureParams");
170189
try {
171190
return await verifyContractWalletSignature(options);
172191
} catch (err) {

packages/thirdweb/src/utils/hashing/hashMessage.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Hex } from "../encoding/hex.js";
33
import { stringToBytes, toBytes } from "../encoding/to-bytes.js";
44
import type { SignableMessage } from "../types.js";
55
import { keccak256 } from "./keccak256.js";
6+
import { hashMessage as viemHashMessage } from "viem";
67

78
const presignMessagePrefix = "\x19Ethereum Signed Message:\n";
89
type To = "hex" | "bytes";
@@ -27,6 +28,10 @@ export function hashMessage<TTo extends To = "hex">(
2728
message: SignableMessage,
2829
to_?: TTo,
2930
): HashMessage<TTo> {
31+
console.log("hashMessage", {
32+
message,
33+
to_,
34+
});
3035
const messageBytes = (() => {
3136
if (typeof message === "string") {
3237
return stringToBytes(message);
@@ -39,5 +44,12 @@ export function hashMessage<TTo extends To = "hex">(
3944
const prefixBytes = stringToBytes(
4045
`${presignMessagePrefix}${messageBytes.length}`,
4146
);
42-
return keccak256(ox__Bytes.concat(prefixBytes, messageBytes), to_);
47+
const ours = keccak256(ox__Bytes.concat(prefixBytes, messageBytes), to_);
48+
49+
const viem = viemHashMessage(message, to_);
50+
51+
console.log("ours", ours);
52+
console.log("viem", viem);
53+
54+
return ours;
4355
}

packages/thirdweb/src/wallets/injected/index.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import {
55
serializeTypedData,
66
stringify,
77
validateTypedData,
8+
verifyMessage,
9+
hashMessage,
810
} from "viem";
911
import { isInsufficientFundsError } from "../../analytics/track/helpers.js";
1012
import {
1113
trackInsufficientFundsError,
1214
trackTransaction,
1315
} from "../../analytics/track/transaction.js";
1416
import type { Chain } from "../../chains/types.js";
15-
import { getCachedChain, getChainMetadata } from "../../chains/utils.js";
17+
import { defineChain, getCachedChain, getChainMetadata } from "../../chains/utils.js";
1618
import type { ThirdwebClient } from "../../client/client.js";
1719
import { getAddress } from "../../utils/address.js";
1820
import {
@@ -34,6 +36,8 @@ import { normalizeChainId } from "../utils/normalizeChainId.js";
3436
import type { WalletEmitter } from "../wallet-emitter.js";
3537
import type { WalletId } from "../wallet-types.js";
3638
import { injectedProvider } from "./mipdStore.js";
39+
import { verifyEip1271Signature } from "../../auth/verify-hash.js";
40+
import { getContract } from "../../contract/contract.js";
3741

3842
// TODO: save the provider in data
3943
export function getInjectedProvider(walletId: WalletId) {
@@ -250,6 +254,11 @@ function createAccount({
250254
throw new Error("Provider not setup");
251255
}
252256

257+
console.log("signMessage", {
258+
message,
259+
address: account.address,
260+
});
261+
253262
const messageToSign = (() => {
254263
if (typeof message === "string") {
255264
return stringToHex(message);
@@ -260,10 +269,32 @@ function createAccount({
260269
return message.raw;
261270
})();
262271

263-
return await provider.request({
272+
const signature = await provider.request({
264273
method: "personal_sign",
265274
params: [messageToSign, getAddress(account.address)],
266275
});
276+
console.log("signature", {
277+
messageToSign,
278+
address: account.address,
279+
signature,
280+
});
281+
const isValid = await verifyEip1271Signature({
282+
contract: getContract({
283+
address: account.address,
284+
chain: defineChain(50104),
285+
client: client,
286+
}),
287+
hash: hashMessage(stringToHex(message as string)),
288+
signature: signature as Hex,
289+
});
290+
console.log("isValid 1271", isValid);
291+
const viemValidSignature = await verifyMessage({
292+
message,
293+
signature,
294+
address: account.address,
295+
});
296+
console.log("valid signature from viem", viemValidSignature);
297+
return signature as Hex;
267298
},
268299
async signTypedData(typedData) {
269300
if (!provider || !account.address) {

0 commit comments

Comments
 (0)