Skip to content

Commit

Permalink
Update restore-keypair.md to include v2 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
metasal1 authored Nov 21, 2024
1 parent c5e5b90 commit 8cf170e
Showing 1 changed file with 57 additions and 17 deletions.
74 changes: 57 additions & 17 deletions content/cookbook/wallets/restore-keypair.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,68 @@ 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/web3.js";

const keypairBytes = 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, ]);

const keypair = await createKeyPairFromBytes(keypairBytes);

````
</Tab>

<Tab value="web3.js v1">
```typescript
import { Keypair } from "@solana/web3.js";
const keypair = Keypair.fromSecretKey(
Uint8Array.from([
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,
]),
);
```
const keypairBytes = Uint8Array.from([ 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,]),
const keypair = Keypair.fromSecretKey( keypairBytes );
````
## From base58 String
</Tab>
</Tabs>
```typescript filename="restore-keypair-from-base58.ts
## From Base58 String
<Tabs groupId="language" items={['web3.js v2', 'web3.js v1']}>
<Tab value="web3.js v2">
```typescript
import { createKeyPairFromBytes, getBase58Codec } from "@solana/web3.js";

const keypairBase58 =
"5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG";
const keypairBytes = getBase58Codec().decode(keypairBase58); const keypair =
await createKeyPairFromBytes(keypairBytes);

````
</Tab>

<Tab value="web3.js v1">
```typescript
import { Keypair } from "@solana/web3.js";
import * as bs58 from "bs58";
const keypair = Keypair.fromSecretKey(
bs58.decode(
"5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG",
),
);
const keypairBase58 = "5MaiiCavjCmn9Hs1o3eznqDEhRwxo7pXiAYez7keQUviUkauRiTMD8DrESdrNjN8zd9mTmVhRvBJeg5vhyvgrAhG";
const keypairBytes = bs58.decode(keypairBase58);
const keypair = Keypair.fromSecretKey( keypairBytes );
````
</Tab>
</Tabs>
```
```

0 comments on commit 8cf170e

Please sign in to comment.