Skip to content

Commit

Permalink
feat: Sui supports the secp256k1/secp256r1 algorithms (#2476)
Browse files Browse the repository at this point in the history
* feat: support secp256k1/secp256r1 for sui

* fix coderabbitai review
  • Loading branch information
lispking authored Jan 18, 2025
1 parent d30b341 commit beb0bc1
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions packages/plugin-sui/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
import type { IAgentRuntime } from "@elizaos/core";
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
import { Secp256k1Keypair } from "@mysten/sui/keypairs/secp256k1";
import { Secp256r1Keypair } from "@mysten/sui/keypairs/secp256r1";

const parseAccount = (runtime: IAgentRuntime): Ed25519Keypair => {
const parseAccount = (
runtime: IAgentRuntime
): Ed25519Keypair | Secp256k1Keypair | Secp256r1Keypair => {
const privateKey = runtime.getSetting("SUI_PRIVATE_KEY");
if (!privateKey) {
throw new Error("SUI_PRIVATE_KEY is not set");
} else if (privateKey.startsWith("suiprivkey")) {
return Ed25519Keypair.fromSecretKey(privateKey);
return loadFromSecretKey(privateKey);
} else {
return Ed25519Keypair.deriveKeypairFromSeed(privateKey);
return loadFromMnemonics(privateKey);
}
};

const loadFromSecretKey = (privateKey: string) => {
const keypairClasses = [Ed25519Keypair, Secp256k1Keypair, Secp256r1Keypair];
for (const KeypairClass of keypairClasses) {
try {
return KeypairClass.fromSecretKey(privateKey);
} catch {
continue;
}
}
throw new Error("Failed to initialize keypair from secret key");
};

const loadFromMnemonics = (mnemonics: string) => {
const keypairMethods = [
{ Class: Ed25519Keypair, method: "deriveKeypairFromSeed" },
{ Class: Secp256k1Keypair, method: "deriveKeypair" },
{ Class: Secp256r1Keypair, method: "deriveKeypair" },
];
for (const { Class, method } of keypairMethods) {
try {
return Class[method](mnemonics);
} catch {
continue;
}
}
throw new Error("Failed to derive keypair from mnemonics");
};

export { parseAccount };

0 comments on commit beb0bc1

Please sign in to comment.