Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update restore-keypair.md to include web3.js v2 examples #623

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions content/cookbook/wallets/restore-keypair.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,30 @@ secret to test out your dApp.

## From Bytes

```typescript filename="restore-keypair-from-bytes.ts"
<Tabs groupId="language" items={['web3.js v2', 'web3.js v1']}>

<Tab value="web3.js v2">

```typescript
import { createKeyPairFromBytes } from "@solana/keys";

const keypair = await createKeyPairFromBytes(
new Uint8Array([
174, 47, 154, 16, 202, 193, 206, 113, 199, 190, 53, 133, 169, 175, 31, 56,
222, 53, 138, 189, 224, 216, 117, 173, 10, 149, 53, 45, 73, 251, 237, 246,
15, 185, 186, 82, 177, 240, 148, 69, 241, 227, 167, 80, 141, 89, 240, 121,
121, 35, 172, 247, 68, 251, 226, 218, 48, 63, 176, 109, 168, 89, 238, 135,
]),
);

console.log(keypair);
```

</Tab>

<Tab value="web3.js v1">

```typescript
import { Keypair } from "@solana/web3.js";

const keypair = Keypair.fromSecretKey(
Expand All @@ -22,9 +45,34 @@ const keypair = Keypair.fromSecretKey(
);
```

</Tab>

</Tabs>

## From base58 String

```typescript filename="restore-keypair-from-base58.ts
<Tabs groupId="language" items={['web3.js v2 from base58', 'web3.js v1 from base58']}>

<Tab value="web3.js v2 from base58">

```typescript
import { createKeyPairFromBytes } from "@solana/keys";
import bs58 from "bs58";

const keypair = await createKeyPairFromBytes(
bs58.decode(
"5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG",
),
);

console.log(keypair);
```

</Tab>

<Tab value="web3.js v1 from base58">

```typescript
import { Keypair } from "@solana/web3.js";
import * as bs58 from "bs58";

Expand All @@ -34,3 +82,7 @@ const keypair = Keypair.fromSecretKey(
),
);
```

</Tab>

</Tabs>
Loading