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

Commit

Permalink
Allow creating crypto key/keypair from ReadonlyUint8Array (#2707)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcintyre94 authored May 11, 2024
1 parent 47bf06d commit cb49bfa
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/stale-turtles-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@solana/signers": patch
"@solana/keys": patch
---

Allow creating keypairs and keys from ReadonlyUint8Array
6 changes: 6 additions & 0 deletions packages/keys/src/__typetests__/key-pair-typetests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ReadonlyUint8Array } from '@solana/codecs-core';

import { createKeyPairFromBytes } from '../key-pair';

createKeyPairFromBytes(new Uint8Array()) satisfies Promise<CryptoKeyPair>;
createKeyPairFromBytes(new Uint8Array() as ReadonlyUint8Array) satisfies Promise<CryptoKeyPair>;
6 changes: 6 additions & 0 deletions packages/keys/src/__typetests__/private-key-typetests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ReadonlyUint8Array } from '@solana/codecs-core';

import { createPrivateKeyFromBytes } from '../private-key';

createPrivateKeyFromBytes(new Uint8Array()) satisfies Promise<CryptoKey>;
createPrivateKeyFromBytes(new Uint8Array() as ReadonlyUint8Array) satisfies Promise<CryptoKey>;
3 changes: 2 additions & 1 deletion packages/keys/src/key-pair.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertKeyGenerationIsAvailable, assertPRNGIsAvailable } from '@solana/assertions';
import { ReadonlyUint8Array } from '@solana/codecs-core';
import {
SOLANA_ERROR__KEYS__INVALID_KEY_PAIR_BYTE_LENGTH,
SOLANA_ERROR__KEYS__PUBLIC_KEY_MUST_MATCH_PRIVATE_KEY,
Expand All @@ -18,7 +19,7 @@ export async function generateKeyPair(): Promise<CryptoKeyPair> {
return keyPair as CryptoKeyPair;
}

export async function createKeyPairFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKeyPair> {
export async function createKeyPairFromBytes(bytes: ReadonlyUint8Array, extractable?: boolean): Promise<CryptoKeyPair> {
assertPRNGIsAvailable();

if (bytes.byteLength !== 64) {
Expand Down
5 changes: 3 additions & 2 deletions packages/keys/src/private-key.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ReadonlyUint8Array } from '@solana/codecs-core';
import { SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, SolanaError } from '@solana/errors';

function addPkcs8Header(bytes: Uint8Array): Uint8Array {
function addPkcs8Header(bytes: ReadonlyUint8Array): ReadonlyUint8Array {
// prettier-ignore
return new Uint8Array([
/**
Expand Down Expand Up @@ -37,7 +38,7 @@ function addPkcs8Header(bytes: Uint8Array): Uint8Array {
]);
}

export async function createPrivateKeyFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<CryptoKey> {
export async function createPrivateKeyFromBytes(bytes: ReadonlyUint8Array, extractable?: boolean): Promise<CryptoKey> {
const actualLength = bytes.byteLength;
if (actualLength !== 32) {
throw new SolanaError(SOLANA_ERROR__KEYS__INVALID_PRIVATE_KEY_BYTE_LENGTH, {
Expand Down
6 changes: 5 additions & 1 deletion packages/signers/src/keypair-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SOLANA_ERROR__SIGNER__EXPECTED_KEY_PAIR_SIGNER, SolanaError } from '@so
import { createKeyPairFromBytes, generateKeyPair, signBytes } from '@solana/keys';
import { partiallySignTransaction } from '@solana/transactions';

import { ReadonlyUint8Array } from '../../codecs-core/dist/types';
import { isMessagePartialSigner, MessagePartialSigner } from './message-partial-signer';
import { isTransactionPartialSigner, TransactionPartialSigner } from './transaction-partial-signer';

Expand Down Expand Up @@ -66,6 +67,9 @@ export async function generateKeyPairSigner(): Promise<KeyPairSigner> {
}

/** Creates a signer capable of signing messages and transactions using the 64 bytes of a KeyPair. */
export async function createKeyPairSignerFromBytes(bytes: Uint8Array, extractable?: boolean): Promise<KeyPairSigner> {
export async function createKeyPairSignerFromBytes(
bytes: ReadonlyUint8Array,
extractable?: boolean,
): Promise<KeyPairSigner> {
return await createSignerFromKeyPair(await createKeyPairFromBytes(bytes, extractable));
}

0 comments on commit cb49bfa

Please sign in to comment.