Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Sui supports the secp256k1/secp256r1 algorithms #2476

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix coderabbitai review
lispking committed Jan 18, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit c0543b58585d4ffa8ca27bd4268a2cbd35c03aed
28 changes: 16 additions & 12 deletions packages/plugin-sui/src/utils.ts
Original file line number Diff line number Diff line change
@@ -10,34 +10,38 @@ const parseAccount = (
if (!privateKey) {
throw new Error("SUI_PRIVATE_KEY is not set");
} else if (privateKey.startsWith("suiprivkey")) {
return loadSecretKey(privateKey);
return loadFromSecretKey(privateKey);
} else {
return loadFromMnemonics(privateKey);
}
};

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

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

export { parseAccount };