Skip to content

Instantly share code, notes, and snippets.

@tranduybau
Last active August 7, 2025 11:13
Show Gist options
  • Select an option

  • Save tranduybau/dbcd40b55be423c2a6547e73c89af28d to your computer and use it in GitHub Desktop.

Select an option

Save tranduybau/dbcd40b55be423c2a6547e73c89af28d to your computer and use it in GitHub Desktop.
Get cosmos wallet address from private key
try {
const prefix = bech32Prefix || this.getChainPrefix();
// Remove '0x' prefix if present
const cleanPrivateKey = privateKey.startsWith('0x') ? privateKey.slice(2) : privateKey;
// Create wallet using DirectSecp256k1Wallet
const wallet = await DirectSecp256k1Wallet.fromKey(
fromHex(cleanPrivateKey),
prefix
);
// Get accounts from wallet
const accounts = await wallet.getAccounts();
if (accounts.length === 0) {
throw new Error('No accounts found in wallet');
}
return accounts[0].address;
} catch (error: any) {
console.error('Error generating STOC address with DirectSecp256k1Wallet:', error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment