Last active
August 7, 2025 11:13
-
-
Save tranduybau/dbcd40b55be423c2a6547e73c89af28d to your computer and use it in GitHub Desktop.
Get cosmos wallet address from private key
This file contains hidden or 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
| 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