Skip to content

Commit ed06dab

Browse files
authored
Merge branch 'develop' into develop
2 parents 274ce4a + beb0bc1 commit ed06dab

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

packages/plugin-sui/src/utils.ts

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
import type { IAgentRuntime } from "@elizaos/core";
22
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
3+
import { Secp256k1Keypair } from "@mysten/sui/keypairs/secp256k1";
4+
import { Secp256r1Keypair } from "@mysten/sui/keypairs/secp256r1";
35

4-
const parseAccount = (runtime: IAgentRuntime): Ed25519Keypair => {
6+
const parseAccount = (
7+
runtime: IAgentRuntime
8+
): Ed25519Keypair | Secp256k1Keypair | Secp256r1Keypair => {
59
const privateKey = runtime.getSetting("SUI_PRIVATE_KEY");
610
if (!privateKey) {
711
throw new Error("SUI_PRIVATE_KEY is not set");
812
} else if (privateKey.startsWith("suiprivkey")) {
9-
return Ed25519Keypair.fromSecretKey(privateKey);
13+
return loadFromSecretKey(privateKey);
1014
} else {
11-
return Ed25519Keypair.deriveKeypairFromSeed(privateKey);
15+
return loadFromMnemonics(privateKey);
1216
}
1317
};
1418

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+
1547
export { parseAccount };

0 commit comments

Comments
 (0)