From 6bd5ec919ac8dd5b08447da51089922ccf769856 Mon Sep 17 00:00:00 2001 From: metasal <54984459+metasal1@users.noreply.github.com> Date: Thu, 21 Nov 2024 12:40:20 +1100 Subject: [PATCH] Update sign-message.md updated to include v2 example --- content/cookbook/wallets/sign-message.md | 41 ++++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/content/cookbook/wallets/sign-message.md b/content/cookbook/wallets/sign-message.md index d491d7a97..fe6488b23 100644 --- a/content/cookbook/wallets/sign-message.md +++ b/content/cookbook/wallets/sign-message.md @@ -4,14 +4,40 @@ sidebarSortOrder: 6 description: "Learn how to sign messages on Solana." --- -The primary function of a keypair is to sign messages and enable verification of -the signature. Verification of a signature allows the recipient to be sure that -the data was signed by the owner of a specific private key. +The primary function of a keypair is to sign messages, transactions and enable +verification of the signature. Verification of a signature allows the recipient +to be sure that the data was signed by the owner of a specific private key. -To do so, we can use the [TweetNaCl](https://www.npmjs.com/package/tweetnacl) -crypto library: + -```typescript filename="sign-message.ts" +```typescript +import { + generateKeyPair, + signBytes, + verifySignature, + getUtf8Encoder, + getBase58Decoder, +} from "@solana/web3.js"; + +const keys = await generateKeyPair(); +const message = getUtf8Encoder().encode("Hello, World!"); +const signedBytes = await signBytes(keys.privateKey, message); + +const decoded = getBase58Decoder().decode(signedBytes); +console.log("Signature:", decoded); + +const verified = await verifySignature(keys.publicKey, signedBytes, message); +console.log("Verified:", verified); +``` + + + + + +In Solana Web3.js v1, we can use the +[TweetNaCl](https://www.npmjs.com/package/tweetnacl) crypto library: + +```typescript import { Keypair } from "@solana/web3.js"; import nacl from "tweetnacl"; import { decodeUTF8 } from "tweetnacl-util"; @@ -37,3 +63,6 @@ const result = nacl.sign.detached.verify( console.log(result); ``` + + +