Skip to content

Commit 814dd27

Browse files
done
1 parent a4fc76d commit 814dd27

6 files changed

Lines changed: 111 additions & 256 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"title": "User Information after Logging In with NextJS",
3-
"description": "Example of retrieving and displaying user data after authentication with Immutable Passport in a NextJS application",
4-
"keywords": ["Immutable", "SDK", "Passport", "NextJS", "User Info", "Linked Addresses", "Tokens"],
5-
"tech_stack": ["NextJS", "TypeScript", "React"],
3+
"description": "Example app demonstrating how to access user profile data, linked addresses, auth tokens, and link external wallets after authentication with Immutable Passport in a Next.js application.",
4+
"keywords": ["Immutable", "SDK", "Passport", "NextJS", "Authentication", "User Profile", "Linked Addresses", "External Wallet"],
5+
"tech_stack": ["Next.js", "TypeScript", "React Query"],
66
"product": "Passport",
77
"programming_language": "TypeScript"
88
}

examples/passport/logged-in-user-with-nextjs/src/app/link-external-wallet/page.tsx

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export default function LinkExternalWallet() {
104104
// Generate a nonce for the signature
105105
const nonce = generateNonce();
106106
// Ensure addresses are in the correct format - lowercase 0x-prefixed
107-
const formattedExternalWalletAddress = externalWalletAddress.toLowerCase() as `0x${string}`;
108-
const formattedPassportAddress = accountAddress.toLowerCase() as `0x${string}`;
107+
const metamaskAddress = externalWalletAddress.toLowerCase() as `0x${string}`;
108+
const passportAddress = accountAddress.toLowerCase() as `0x${string}`;
109109

110110
const dataToSign = {
111111
types: {
@@ -136,62 +136,52 @@ export default function LinkExternalWallet() {
136136
},
137137
primaryType: "LinkWallet",
138138
domain: {
139-
chainId: 1,
139+
chainId: 1, // Must be set to 1 for Ethereum Mainnet
140140
},
141141
message: {
142-
walletAddress: formattedExternalWalletAddress,
143-
immutablePassportAddress: formattedPassportAddress,
142+
walletAddress: metamaskAddress,
143+
immutablePassportAddress: passportAddress,
144144
condition: "I agree to link this wallet to my Immutable Passport account.",
145145
nonce
146146
}
147147
}
148148

149-
console.log('dataToSign', dataToSign);
150-
151149
if (typeof window === 'undefined' || !window.ethereum) {
152150
throw new Error('MetaMask not installed');
153151
}
154152

155-
// Sign the message using window.ethereum directly
156-
const signature = await window.ethereum.request({
157-
method: 'eth_signTypedData_v4',
158-
params: [formattedExternalWalletAddress, JSON.stringify(dataToSign)]
159-
});
160-
161-
console.log('signature', signature);
153+
let signature: string;
162154

163-
// Import and use our signature validation function
164-
const { validateSignatureComprehensive } = await import('../utils/validateEIP712Signature');
165-
166-
const isValid = await validateSignatureComprehensive(
167-
formattedExternalWalletAddress, // the signer address
168-
dataToSign, // the payload
169-
signature,
170-
window.ethereum // pass the provider for contract wallet validation if needed
171-
);
155+
try {
156+
// Metamask must be connected to Ethereum Mainnet
157+
await window.ethereum.request({
158+
method: 'wallet_switchEthereumChain',
159+
params: [{ chainId: '0x1' }], // chainId must be in hexadecimal format
160+
});
172161

173-
console.log('isValid', isValid);
162+
// Now request the signature
163+
signature = await window.ethereum.request({
164+
method: 'eth_signTypedData_v4',
165+
params: [metamaskAddress, JSON.stringify(dataToSign)]
166+
});
174167

175-
if (!isValid) {
176-
throw new Error('Invalid signature');
168+
} catch (error) {
169+
console.error('Error signing message:', error);
170+
setLinkingError('Failed to sign message');
171+
setLinkingStatus('Make sure you have MetaMask installed and connected to Ethereum Mainnet');
172+
setIsLinking(false);
173+
return
177174
}
178-
175+
179176
setLinkingStatus('Linking wallet...');
180177

181-
console.log('linkExternalWallet type:', "External");
182-
console.log('linkExternalWallet walletAddress:', formattedExternalWalletAddress);
183-
console.log('linkExternalWallet signature:', signature);
184-
console.log('linkExternalWallet nonce:', nonce);
185-
186178
// Call the linkExternalWallet method to link the wallet
187179
const result = await passportInstance.linkExternalWallet({
188180
type: "External",
189-
walletAddress: formattedExternalWalletAddress,
181+
walletAddress: metamaskAddress,
190182
signature,
191183
nonce
192184
});
193-
194-
console.log('result', result);
195185

196186
const linkedAddresses = await passportInstance.getLinkedAddresses();
197187
setLinkedAddresses(linkedAddresses);
@@ -266,12 +256,7 @@ export default function LinkExternalWallet() {
266256
<Table.Cell><b>External Wallet</b></Table.Cell>
267257
<Table.Cell>{externalWalletAddress || 'Not connected'}</Table.Cell>
268258
</Table.Row>
269-
{linkingStatus && (
270-
<Table.Row>
271-
<Table.Cell><b>Status</b></Table.Cell>
272-
<Table.Cell>{linkingStatus}</Table.Cell>
273-
</Table.Row>
274-
)}
259+
275260
{linkedAddresses.length > 0 && (
276261
<Table.Row>
277262
<Table.Cell><b>Linked Addresses</b></Table.Cell>
@@ -284,6 +269,12 @@ export default function LinkExternalWallet() {
284269
<Table.Cell>{linkingError}</Table.Cell>
285270
</Table.Row>
286271
)}
272+
{linkingStatus && (
273+
<Table.Row>
274+
<Table.Cell><b>Message</b></Table.Cell>
275+
<Table.Cell>{linkingStatus}</Table.Cell>
276+
</Table.Row>
277+
)}
287278
</Table.Body>
288279
</Table>
289280

examples/passport/logged-in-user-with-nextjs/src/app/utils/isValidSignature.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

examples/passport/logged-in-user-with-nextjs/src/app/utils/validateEIP712Signature.ts

Lines changed: 0 additions & 142 deletions
This file was deleted.

examples/passport/logged-in-user-with-nextjs/tests/base.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ test.describe("sub-pages navigation", () => {
5555
test("Check Link External Wallet", async ({ page }) => {
5656
await page.click("text=Link External Wallet");
5757
await expect(page.getByRole("heading", { name: "Link External Wallet" })).toBeVisible();
58-
await expect(page.getByRole("button", { name: /Login|Logged In/ })).toBeVisible();
58+
await expect(page.getByRole("button", { name: "Login with Passport" })).toBeVisible();
5959
await expect(page.getByRole("row", { name: /Is Logged In/ })).toBeVisible();
6060
await expect(page.getByRole("row", { name: /Account Address/ })).toBeVisible();
61-
await expect(page.getByRole("button", { name: "Connect Wallet" })).toBeVisible();
61+
await expect(page.getByRole("row", { name: /External Wallet/ })).toBeVisible();
6262
await expect(page.getByRole("link", { name: "Return to Examples" })).toBeVisible();
6363
});
6464
});

0 commit comments

Comments
 (0)