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

chore(sui): avoid printing private key #524

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Changes from all commits
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
17 changes: 15 additions & 2 deletions sui/utils/sign-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function getWallet(chain, options) {

switch (options.privateKeyType) {
case 'bech32': {
const decodedKey = decodeSuiPrivateKey(options.privateKey);
const decodedKey = decodePrivateKey(options.privateKey);
const secretKey = decodedKey.secretKey;
keypair = scheme.fromSecretKey(secretKey);
break;
Expand Down Expand Up @@ -116,8 +116,21 @@ async function generateKeypair(options) {
}
}

// Decodes a Sui private key without exposing the secret key when failing
function decodePrivateKey(privateKey) {
if (typeof privateKey !== 'string' || !privateKey) {
throw new Error('Private key must be a non-empty string');
}

try {
return decodeSuiPrivateKey(privateKey);
} catch (e) {
throw new Error('Invalid Sui private key - please verify the format');
}
}

function getRawPrivateKey(keypair) {
return decodeSuiPrivateKey(keypair.getSecretKey()).secretKey;
return decodePrivateKey(keypair.getSecretKey()).secretKey;
}

async function broadcast(client, keypair, tx, actionName) {
Expand Down