|
1 | 1 | import type { IAgentRuntime } from "@elizaos/core";
|
2 | 2 | import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
|
| 3 | +import { Secp256k1Keypair } from "@mysten/sui/keypairs/secp256k1"; |
| 4 | +import { Secp256r1Keypair } from "@mysten/sui/keypairs/secp256r1"; |
3 | 5 |
|
4 |
| -const parseAccount = (runtime: IAgentRuntime): Ed25519Keypair => { |
| 6 | +const parseAccount = ( |
| 7 | + runtime: IAgentRuntime |
| 8 | +): Ed25519Keypair | Secp256k1Keypair | Secp256r1Keypair => { |
5 | 9 | const privateKey = runtime.getSetting("SUI_PRIVATE_KEY");
|
6 | 10 | if (!privateKey) {
|
7 | 11 | throw new Error("SUI_PRIVATE_KEY is not set");
|
8 | 12 | } else if (privateKey.startsWith("suiprivkey")) {
|
9 |
| - return Ed25519Keypair.fromSecretKey(privateKey); |
| 13 | + return loadFromSecretKey(privateKey); |
10 | 14 | } else {
|
11 |
| - return Ed25519Keypair.deriveKeypairFromSeed(privateKey); |
| 15 | + return loadFromMnemonics(privateKey); |
12 | 16 | }
|
13 | 17 | };
|
14 | 18 |
|
| 19 | +const loadFromSecretKey = (privateKey: string) => { |
| 20 | + const keypairClasses = [Ed25519Keypair, Secp256k1Keypair, Secp256r1Keypair]; |
| 21 | + for (const KeypairClass of keypairClasses) { |
| 22 | + try { |
| 23 | + return KeypairClass.fromSecretKey(privateKey); |
| 24 | + } catch { |
| 25 | + continue; |
| 26 | + } |
| 27 | + } |
| 28 | + throw new Error("Failed to initialize keypair from secret key"); |
| 29 | +}; |
| 30 | + |
| 31 | +const loadFromMnemonics = (mnemonics: string) => { |
| 32 | + const keypairMethods = [ |
| 33 | + { Class: Ed25519Keypair, method: "deriveKeypairFromSeed" }, |
| 34 | + { Class: Secp256k1Keypair, method: "deriveKeypair" }, |
| 35 | + { Class: Secp256r1Keypair, method: "deriveKeypair" }, |
| 36 | + ]; |
| 37 | + for (const { Class, method } of keypairMethods) { |
| 38 | + try { |
| 39 | + return Class[method](mnemonics); |
| 40 | + } catch { |
| 41 | + continue; |
| 42 | + } |
| 43 | + } |
| 44 | + throw new Error("Failed to derive keypair from mnemonics"); |
| 45 | +}; |
| 46 | + |
15 | 47 | export { parseAccount };
|
0 commit comments