-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Sui supports the secp256k1/secp256r1 algorithms (#2476)
* feat: support secp256k1/secp256r1 for sui * fix coderabbitai review
- Loading branch information
Showing
1 changed file
with
35 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |