Skip to content

Commit

Permalink
feat: support secp256k1/secp256r1 for sui
Browse files Browse the repository at this point in the history
  • Loading branch information
lispking committed Jan 18, 2025
1 parent 87793af commit 9b92792
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions packages/plugin-sui/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
import { 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 loadSecretKey(privateKey);
} else {
return Ed25519Keypair.deriveKeypairFromSeed(privateKey);
return loadFromMnemonics(privateKey);
}
};

const loadSecretKey = (privateKey: string) => {
try {
return Ed25519Keypair.fromSecretKey(privateKey);
} catch {
try {
return Secp256k1Keypair.fromSecretKey(privateKey);
} catch {
return Secp256r1Keypair.fromSecretKey(privateKey);
}
}
};

const loadFromMnemonics = (mnemonics: string) => {
try {
return Ed25519Keypair.deriveKeypairFromSeed(mnemonics);
} catch {
try {
return Secp256k1Keypair.deriveKeypair(mnemonics);
} catch {
return Secp256r1Keypair.deriveKeypair(mnemonics);
}
}
};

Expand Down

0 comments on commit 9b92792

Please sign in to comment.