Skip to content

Commit

Permalink
docs: add metamask verify arbitrary example
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronCQL committed Jan 4, 2024
1 parent f767aed commit 7eff51e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/verify-signatures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"dependencies": {
"@keplr-wallet/cosmos": "^0.11.63",
"cosmes": "link:../..",
"secp256k1": "^5.0.0"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions examples/verify-signatures/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions examples/verify-signatures/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { verifyADR36Amino } from "@keplr-wallet/cosmos";
import { recoverPubKeyFromEthSignature } from "cosmes/codec";
import { createHash } from "node:crypto";
import { ecdsaVerify } from "secp256k1";

Expand Down Expand Up @@ -38,22 +39,46 @@ function verifyArbitraryStation(
);
}

/**
* Returns true iff the signature is valid for the given data and public key.
* Works for Metamask.
*/
function verifyArbitraryMetamask(
data: string,
pubKey: string,
signature: string
) {
const recoveredPubKey = recoverPubKeyFromEthSignature(
Buffer.from(data, "utf8"),
Buffer.from(signature, "base64")
);
return pubKey === Buffer.from(recoveredPubKey).toString("base64");
}

console.log(
verifyArbitraryKeplr(
// TODO: fill in chain prefix (eg. "osmo")
"",
// TODO: fill in address (eg. "osmo1...")
"",
// TODO: change data if necessary
"Hi from Coinhall! This is a test message just to prove that the wallet is working.",
"Hi from CosmeES! This is a test message just to prove that the wallet is working.",
// TODO: fill in base64 pubKey
"",
// TODO: fill in base64 signature
""
),
verifyArbitraryStation(
// TODO: change data if necessary
"Hi from Coinhall! This is a test message just to prove that the wallet is working.",
"Hi from CosmeES! This is a test message just to prove that the wallet is working.",
// TODO: fill in base64 pubKey
"",
// TODO: fill in base64 signature
""
),
verifyArbitraryMetamask(
// TODO: change data if necessary
"Hi from CosmeES! This is a test message just to prove that the wallet is working.",
// TODO: fill in base64 pubKey
"",
// TODO: fill in base64 signature
Expand Down

0 comments on commit 7eff51e

Please sign in to comment.