Skip to content

Commit

Permalink
Update sign-message.md updated to include v2 example (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
metasal1 authored Nov 21, 2024
1 parent c5e5b90 commit 458d392
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions content/cookbook/wallets/sign-message.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
<Tabs groupId="language" items={['web3.js v2', 'web3.js v1']}> <Tab value="web3.js v2">

```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);
```

</Tab>

<Tab value="web3.js v1">

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";
Expand All @@ -37,3 +63,6 @@ const result = nacl.sign.detached.verify(

console.log(result);
```

</Tab>
</Tabs>

0 comments on commit 458d392

Please sign in to comment.