Skip to content

Instantly share code, notes, and snippets.

@DooblyNoobly
Last active January 14, 2024 00:57
Show Gist options
  • Save DooblyNoobly/b6811924e500fd0e4b05b48ae6cd44d9 to your computer and use it in GitHub Desktop.
Save DooblyNoobly/b6811924e500fd0e4b05b48ae6cd44d9 to your computer and use it in GitHub Desktop.
Derive Terra Wallet Address (bech32) from Secp256k1 compressed Terra Public key in dotnet core
using Ecdsa.Secp;
using Org.BouncyCastle.Crypto.Digests;
using Nano.Bech32;
var TerraPublicKeyBase64 = "Your Base64 PubKey Here!";
//Create the pub key object (Secp256k1 compressed)
var secp256k1 = new Secp256k1();
byte[] compressed = secp256k1.CompressKey(Convert.FromBase64String(TerraPublicKeyBase64));
//SHA256
var sha = new Sha256Digest();
sha.BlockUpdate(compressed, 0, compressed.Length);
var shaHash2 = new byte[sha.GetDigestSize()];
sha.DoFinal(shaHash2, 0);
//RIPEMD160
var hasher = new RipeMD160Digest();
hasher.BlockUpdate(shaHash2, 0, shaHash2.Length);
var ripeResult = new byte[hasher.GetDigestSize()];
hasher.DoFinal(ripeResult, 0);
//BECH32
string bech32Address = Bech32Encoder.Encode("terra", ripeResult);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment