I’m developing a dapp on the Internet Computer and want to implement MetaMask login functionality similar to OpenChat’s simple one-button approach. I need help with:
Current Goal
I need to connect MetaMask and obtain an identity that can be used with the Agent:
const agent = new HttpAgent({
identity: fetchIdentity(MetaMaskToken),
host,
});
Where my backend can catch that in caller()
What I’ve Tried
Looked at ic-swi example but found it overly complex, it requires user to preform two steps and I want them to do it in just one click
Tried to navigate OpenChat’s codebase but it’s quite large and difficult to extract just the MetaMask integration part
What I Need
A simpler implementation with a single button for MetaMask connection
The correct way to get the identity from MetaMask that works with HttpAgent
Ensure that caller() returns the MetaMask ID when the user is authenticated
?
Has anyone implemented a clean solution for this or can point me to relevant code examples? Ideally looking for something more straightforward than the existing examples.
It seems that the one most widely used in the community is the SIWE developed by Astrox.
I once developed an application that allowed users to log in using MetaMask. The flow was simple: the frontend would prompt MetaMask to sign a message (which could be customized), and after signing just once, users could log into the IC directly.
This mechanism uses a Delegation Identity flow similar to Internet Identity. It’s a bit complex to explain, but not too difficult to implement or use.
I didn’t package it as a public infrastructure library at the time, but I can show you how to implement the functionality you’re looking for—you’ll just need to adapt the code I provide. If you’re interested, I’d be happy to walk you through it.
the identity remains in the frontend so the user can manage it, what I’d do is encrypt it with an user password for extra security, but you can even add more 2FAs methods like passkeys or Google Auth
try {
// Generate the identity from seed phrase and index
const { identity: newIdentity } = deriveAddressFromSeedPhrase(
this.seedPhrase,
addressIndex
);
// Update the global identity
identity = newIdentity;
this.currentAddressIndex = addressIndex;
// Save state
this.saveStateToLocalStorage();
return true;
} catch (error) {
console.error('Error switching address:', error);
return false;
}
}
so you just let the “handleLoginFlow” method to take care of everything else, I did that with chatGPT but Claude, DeepSeek and Gemini 2.5 should do it.
Just prompt to use the copy pasted code based on your requirements, but give it full context that is another project and you want only the part of the auth.js that handles the identities
I wouldn’t use ICP Ninja for that, also a standalone index.html for a full dapp won’t get you far
How about you create an opensource Github repo for testing purposes, use a JS framework like react/vue/svelte and then you should be good make it work.
Also don’t hurt yourself writing code manually, AI is here to stay just learn the frameworks so you can make better decisions.
Cursor is now free for students and is very powerful or you can get a 15-day trial, Caffeine AI is on its way so you can get started and prepare for it.