Skip to content

Instantly share code, notes, and snippets.

@podefr
Created March 6, 2024 15:10
Show Gist options
  • Save podefr/6eff327d6dcb2adb53ced5cc302837dd to your computer and use it in GitHub Desktop.
Save podefr/6eff327d6dcb2adb53ced5cc302837dd to your computer and use it in GitHub Desktop.
Verify Message signed by Account Abstraction / Smart Account [DFNS + ALCHEMY]
import "dotenv/config";
import { DfnsWallet } from "@dfns/lib-viem";
import { DfnsApiClient } from "@dfns/sdk";
import { AsymmetricKeySigner } from "@dfns/sdk-keysigner";
import { LocalAccountSigner, polygonMumbai } from "@alchemy/aa-core";
import { createModularAccountAlchemyClient } from "@alchemy/aa-alchemy";
import { generatePrivateKey } from "viem/accounts" ;
import { createPublicClient, http } from "viem";
/**
* Trying to sign a message with DFNS + Alchemy AA
* and verify it with viem verifyMessage https://viem.sh/docs/actions/public/verifyMessage.html
*/
const keySigner = new AsymmetricKeySigner({
credId: process.env.DFNS_CRED_ID,
privateKey: process.env.DFNS_PEM,
appOrigin: process.env.DFNS_APP_ORIGIN
});
const dfnsClient = new DfnsApiClient({
baseUrl: process.env.DFNS_API_URL,
appId: process.env.DFNS_APP_ID,
authToken: process.env.DFNS_JWT_KEY,
signer: keySigner
});
const dfnsWallet = await DfnsWallet.init({
walletId: "wa-5ju0r-lhhbc-90uo2jlinjgve8o3",
dfnsClient
});
const alchemyProvider = await createModularAccountAlchemyClient({
apiKey: process.env.ALCHEMY_API_KEY,
chain: polygonMumbai,
signer: new LocalAccountSigner(dfnsWallet)
});
const publicClient = createPublicClient({
chain: polygonMumbai,
transport: http()
});
const message = "not random message";
const dfnsSignature = await alchemyProvider.signMessage({ message });
// is false / not valid
console.log(await publicClient.verifyMessage({
address: alchemyProvider.getAddress(),
message,
signature: dfnsSignature
}));
/**
* Signing the message with an Alchemy's LocalAccountSigner and a private key
* and verifying it with viem verifyMessage https://viem.sh/docs/actions/public/verifyMessage.html
*/
const signer = LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey());
const signature = await signer.signMessage(message);
// is true / valid
const valid = await publicClient.verifyMessage({
address: await signer.getAddress(),
message,
signature
});
console.log(valid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment