Skip to content

Commit

Permalink
Merge pull request #141 from metaplex-foundation/feat/kp-from-files
Browse files Browse the repository at this point in the history
Add ability to use solana config or keypair files
  • Loading branch information
blockiosaurus authored Aug 25, 2024
2 parents fe38c68 + 8c8aa5f commit 7b1813f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/spicy-islands-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@metaplex-foundation/umi-eddsa-web3js": patch
"@metaplex-foundation/umi": patch
---

Add ability to use solana config or keypair files
3 changes: 2 additions & 1 deletion packages/umi-eddsa-web3js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"dependencies": {
"@metaplex-foundation/umi-web3js-adapters": "workspace:^",
"@soceanfi/solana-cli-config": "0.2.0",
"@noble/curves": "^1.0.0"
},
"peerDependencies": {
Expand Down Expand Up @@ -63,4 +64,4 @@
}
}
}
}
}
12 changes: 12 additions & 0 deletions packages/umi-eddsa-web3js/src/createWeb3JsEddsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import {
toWeb3JsPublicKey,
} from '@metaplex-foundation/umi-web3js-adapters';
import { ed25519 } from '@noble/curves/ed25519';
import { SolanaCliConfig } from '@soceanfi/solana-cli-config';
import {
Keypair as Web3JsKeypair,
PublicKey as Web3JsPublicKey,
} from '@solana/web3.js';
import { readFileSync } from 'fs';

export function createWeb3JsEddsa(): EddsaInterface {
const generateKeypair = (): Keypair =>
Expand All @@ -28,6 +30,14 @@ export function createWeb3JsEddsa(): EddsaInterface {
const createKeypairFromSeed = (seed: Uint8Array): Keypair =>
fromWeb3JsKeypair(Web3JsKeypair.fromSeed(seed));

const createKeypairFromFile = (path: string): Keypair =>
createKeypairFromSecretKey(
new Uint8Array(JSON.parse(readFileSync(path).toString()))
);

const createKeypairFromSolanaConfig = (): Keypair =>
fromWeb3JsKeypair(SolanaCliConfig.load().loadKeypair());

const isOnCurve = (input: PublicKeyInput): boolean =>
Web3JsPublicKey.isOnCurve(toWeb3JsPublicKey(publicKey(input)));

Expand All @@ -52,6 +62,8 @@ export function createWeb3JsEddsa(): EddsaInterface {
generateKeypair,
createKeypairFromSecretKey,
createKeypairFromSeed,
createKeypairFromFile,
createKeypairFromSolanaConfig,
isOnCurve,
findPda,
sign,
Expand Down
12 changes: 12 additions & 0 deletions packages/umi-eddsa-web3js/test/Web3JsEddsa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ test('it can sign and verify messages', async (t) => {
// Then we expect the signature to be valid.
t.true(verified);
});

test('it can create a keypair from a file', async (t) => {
// Given a keypair file.
const eddsa = createWeb3JsEddsa();
const keypair = eddsa.createKeypairFromFile('test/test.json');

// Then we expect the signature to be valid.
t.true(
keypair.publicKey.toString() ==
'HUaeN9AVCpTU6QvEmE48wAttKmQEh6RsRd5cWaXqkhVk'
);
});
6 changes: 6 additions & 0 deletions packages/umi-eddsa-web3js/test/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
52, 164, 75, 60, 77, 252, 108, 145, 74, 181, 35, 32, 89, 22, 231, 157, 245,
70, 255, 49, 225, 78, 203, 55, 121, 240, 77, 246, 224, 78, 24, 79, 244, 203,
182, 220, 132, 185, 2, 89, 57, 34, 151, 38, 211, 134, 214, 168, 177, 118, 159,
131, 163, 161, 198, 114, 45, 178, 239, 183, 160, 72, 46, 91
]
6 changes: 6 additions & 0 deletions packages/umi/src/EddsaInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface EddsaInterface {
createKeypairFromSecretKey: (secretKey: Uint8Array) => Keypair;
/** Restores a keypair from a seed. */
createKeypairFromSeed: (seed: Uint8Array) => Keypair;
/** Restores a keypair from a file. */
createKeypairFromFile: (path: string) => Keypair;
/** Restore a keypair from the solana config file. */
createKeypairFromSolanaConfig: (path: string) => Keypair;
/** Whether the given public key is on the EdDSA elliptic curve. */
isOnCurve: (publicKey: PublicKey) => boolean;
/** Finds a Program-Derived Address from the given programId and seeds. */
Expand All @@ -41,6 +45,8 @@ export function createNullEddsa(): EddsaInterface {
generateKeypair: errorHandler,
createKeypairFromSecretKey: errorHandler,
createKeypairFromSeed: errorHandler,
createKeypairFromFile: errorHandler,
createKeypairFromSolanaConfig: errorHandler,
isOnCurve: errorHandler,
findPda: errorHandler,
sign: errorHandler,
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7b1813f

Please sign in to comment.