Function: createKeyPairFromBytes()
ts
function createKeyPairFromBytes(
bytes,
extractable?
): Promise<CryptoKeyPair>;Given a 64-byte Uint8Array secret key, creates an Ed25519 public/private key pair for use with other methods in this package that accept CryptoKey objects.
Parameters
| Parameter | Type | Description |
|---|---|---|
bytes | ReadonlyUint8Array | 64 bytes, the first 32 of which represent the private key and the last 32 of which represent its associated public key |
extractable? | boolean | Setting 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
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 CryptoKeyPair from the bytes.
const { privateKey, publicKey } = await createKeyPairFromBytes(keypairBytes);