Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Add createKeyPairSignerFromPrivateKeyBytes helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Aug 2, 2024
1 parent cf1091d commit 5ef7999
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/signers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,36 @@ import { generateKeyPairSigner } from '@solana/signers';
const myKeyPairSigner = await generateKeyPairSigner();
```

#### `createKeyPairSignerFromBytes()`

A convenience function that creates a new KeyPair from a 64-bytes `Uint8Array` secret key and immediately creates a `KeyPairSigner` from it.

```ts
import fs from 'fs';
import { createKeyPairFromBytes } from '@solana/keys';

// Get bytes from local keypair file.
const keypairFile = fs.readFileSync('~/.config/solana/id.json');
const keypairBytes = new Uint8Array(JSON.parse(keypairFile.toString()));

// Create a KeyPairSigner from the bytes.
const { privateKey, publicKey } = await createKeyPairSignerFromBytes(keypairBytes);
```

#### `createKeyPairSignerFromPrivateKeyBytes()`

A convenience function that creates a new KeyPair from a 32-bytes `Uint8Array` private key and immediately creates a `KeyPairSigner` from it.

```ts
import { getUtf8Encoder } from '@solana/codecs-strings';
import { createKeyPairFromPrivateKeyBytes } from '@solana/keys';

const message = getUtf8Encoder().encode('Hello, World!');
const seed = new Uint8Array(await crypto.subtle.digest('SHA-256', message));

const derivedSigner = await createKeyPairSignerFromPrivateKeyBytes(seed);
```

#### `isKeyPairSigner()`

A type guard that returns `true` if the provided value is a `KeyPairSigner`.
Expand Down
10 changes: 9 additions & 1 deletion packages/signers/src/keypair-signer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, getAddressFromPublicKey } from '@solana/addresses';
import { ReadonlyUint8Array } from '@solana/codecs-core';
import { SOLANA_ERROR__SIGNER__EXPECTED_KEY_PAIR_SIGNER, SolanaError } from '@solana/errors';
import { createKeyPairFromBytes, generateKeyPair, signBytes } from '@solana/keys';
import { createKeyPairFromBytes, createKeyPairFromPrivateKeyBytes, generateKeyPair, signBytes } from '@solana/keys';
import { partiallySignTransaction } from '@solana/transactions';

import { isMessagePartialSigner, MessagePartialSigner } from './message-partial-signer';
Expand Down Expand Up @@ -73,3 +73,11 @@ export async function createKeyPairSignerFromBytes(
): Promise<KeyPairSigner> {
return await createSignerFromKeyPair(await createKeyPairFromBytes(bytes, extractable));
}

/** Creates a signer capable of signing messages and transactions using the 32 bytes of a private key. */
export async function createKeyPairSignerFromPrivateKeyBytes(
bytes: ReadonlyUint8Array,
extractable?: boolean,
): Promise<KeyPairSigner> {
return await createSignerFromKeyPair(await createKeyPairFromPrivateKeyBytes(bytes, extractable));
}

0 comments on commit 5ef7999

Please sign in to comment.