Kit
Functions

createKeyPairFromPrivateKeyBytes

createKeyPairFromPrivateKeyBytes(bytes, extractable?): Promise<CryptoKeyPair>

Given a private key represented as a 32-byte Uint8Array, creates an Ed25519 public/private key pair for use with other methods in this package that accept CryptoKey objects.

Parameters

ParameterTypeDescription
bytesReadonlyUint8Array32 bytes that represent the private key
extractable?booleanSetting this to true makes it possible to extract the bytes of the private key using the crypto.subtle.exportKey() API. Defaults to false.

Returns

Promise<CryptoKeyPair>

Example

import { createKeyPairFromPrivateKeyBytes } from '@solana/keys';
 
const { privateKey, publicKey } = await createKeyPairFromPrivateKeyBytes(new Uint8Array([...]));

This can be useful when you have a private key but not the corresponding public key or when you need to derive key pairs from seeds. For instance, the following code snippet derives a key pair from the hash of a message.

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 derivedKeypair = await createKeyPairFromPrivateKeyBytes(seed);

On this page