-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added multiple keypairs/signers functions (#44)
* chore: add assertions * feat: generate extractable keypairs * fix: type import * feat: saving keypairs to files * feat: added load leys from env * feat: saving keys to env files * docs: document in readme * chore: changeset * docs: readme and comment
- Loading branch information
1 parent
b9491e4
commit e18fc1b
Showing
11 changed files
with
427 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"gill": minor | ||
--- | ||
|
||
added functions for generating extractable keypairs, saving keypairs to files, and loading/saving keypairs to env variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,7 @@ test-ledger/ | |
.ghpages-deploy | ||
|
||
# Codegenerated TypeDoc API | ||
docs/ | ||
docs/ | ||
|
||
.env | ||
.env.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { assertKeyExporterIsAvailable, assertKeyGenerationIsAvailable } from "@solana/assertions"; | ||
import { createKeyPairFromBytes } from "@solana/keys"; | ||
import { | ||
type KeyPairSigner, | ||
createSignerFromKeyPair, | ||
type createKeyPairSignerFromBytes, | ||
} from "@solana/signers"; | ||
|
||
export function assertKeyPairIsExtractable( | ||
keyPair: CryptoKeyPair, | ||
): asserts keyPair is ExtractableCryptoKeyPair { | ||
assertKeyExporterIsAvailable(); | ||
|
||
if (!keyPair.privateKey) { | ||
throw new Error("Keypair is missing private key"); | ||
} | ||
|
||
if (!keyPair.publicKey) { | ||
throw new Error("Keypair is missing public key"); | ||
} | ||
|
||
if (!keyPair.privateKey.extractable) { | ||
throw new Error("Private key is not extractable"); | ||
} | ||
} | ||
|
||
type Extractable = { "~extractable": true }; | ||
|
||
type ExtractableCryptoKeyPair = CryptoKeyPair & Extractable; | ||
type ExtractableKeyPairSigner = KeyPairSigner & Extractable; | ||
|
||
/** | ||
* Generates an extractable Ed25519 `CryptoKeyPair` capable of signing messages and transactions | ||
* */ | ||
export async function generateExtractableKeyPair(): Promise<ExtractableCryptoKeyPair> { | ||
await assertKeyGenerationIsAvailable(); | ||
return crypto.subtle.generateKey( | ||
/* algorithm */ "Ed25519", // Native implementation status: https://github.com/WICG/webcrypto-secure-curves/issues/20 | ||
/* extractable */ true, | ||
/* allowed uses */ ["sign", "verify"], | ||
) as Promise<ExtractableCryptoKeyPair>; | ||
} | ||
|
||
/** | ||
* Generates an extractable signer capable of signing messages and transactions using a Crypto KeyPair. | ||
* */ | ||
export async function generateExtractableKeyPairSigner(): Promise<ExtractableKeyPairSigner> { | ||
return createSignerFromKeyPair( | ||
await generateExtractableKeyPair(), | ||
) as Promise<ExtractableKeyPairSigner>; | ||
} | ||
|
||
/** | ||
* Extracts the raw key material from an extractable Ed25519 CryptoKeyPair. | ||
* | ||
* @remarks | ||
* - Requires a keypair generated with extractable=true. See {@link generateExtractableKeyPair}. | ||
* - The extracted bytes can be used to reconstruct the `CryptoKeyPair` with {@link createKeyPairFromBytes}. | ||
* | ||
* @param keypair An extractable Ed25519 `CryptoKeyPair` | ||
* @returns Raw key bytes as `Uint8Array` | ||
*/ | ||
export async function extractBytesFromKeyPair( | ||
keypair: ExtractableCryptoKeyPair | CryptoKeyPair, | ||
): Promise<Uint8Array> { | ||
assertKeyPairIsExtractable(keypair); | ||
|
||
const [publicKeyBytes, privateKeyJwk] = await Promise.all([ | ||
crypto.subtle.exportKey("raw", keypair.publicKey), | ||
crypto.subtle.exportKey("jwk", keypair.privateKey), | ||
]); | ||
|
||
if (!privateKeyJwk.d) throw new Error("Failed to get private key bytes"); | ||
|
||
return new Uint8Array([ | ||
...Buffer.from(privateKeyJwk.d, "base64"), | ||
...new Uint8Array(publicKeyBytes), | ||
]); | ||
} | ||
|
||
/** | ||
* Extracts the raw key material from an extractable Ed25519 KeyPairSigner. | ||
* | ||
* @remarks | ||
* - Requires a keypair generated with extractable=true. See {@link generateExtractableKeyPairSigner}. | ||
* - The extracted bytes can be used to reconstruct the `CryptoKeyPair` with {@link createKeyPairSignerFromBytes}. | ||
* | ||
* @param keypairSigner An extractable Ed25519 `KeyPairSigner` | ||
* @returns Raw key bytes as `Uint8Array` | ||
*/ | ||
export async function extractBytesFromKeyPairSigner( | ||
keypairSigner: ExtractableKeyPairSigner | KeyPairSigner, | ||
): Promise<Uint8Array> { | ||
return extractBytesFromKeyPair(keypairSigner.keyPair); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./const"; | ||
export * from "./load-keypair"; | ||
export * from "./save-keypair"; |
Oops, something went wrong.