Skip to content

Instantly share code, notes, and snippets.

@tlehman
Created March 22, 2023 02:12
Show Gist options
  • Save tlehman/2c84f47d35c9992d6a159cf3e32e5c6d to your computer and use it in GitHub Desktop.
Save tlehman/2c84f47d35c9992d6a159cf3e32e5c6d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/ripemd.h>
#include <bitcoin/bitcoin.h>
#define BIP39_ENTROPY_LEN 32
#define BIP39_MNEMONIC_LEN 12
void bip39_to_address(const char *mnemonic, const char *password)
{
// Decode BIP39 mnemonic and validate checksum
uint8_t entropy[BIP39_ENTROPY_LEN];
if (!bip39_mnemonic_to_entropy(mnemonic, entropy, BIP39_ENTROPY_LEN) ||
!bip39_mnemonic_validate_checksum(mnemonic))
{
printf("Invalid BIP39 mnemonic or checksum\n");
return;
}
// Generate HD wallet from BIP39 seed
hd_private_t *hd_root = hd_new();
hd_generate_from_seed(entropy, BIP39_ENTROPY_LEN, password, hd_root);
// Derive public key from HD wallet
bc_public_key_t *public_key = bc_public_key_new();
hd_to_public_key(hd_root, public_key);
// Generate Bitcoin address from public key
bc_address_t *address = bc_address_new();
bc_public_key_to_address(public_key, BC_ADDRESS_P2PKH, address);
printf("Bitcoin address: %s\n", bc_address_string(address));
// Free memory
hd_free(hd_root);
bc_public_key_free(public_key);
bc_address_free(address);
}
int main()
{
// Example BIP39 seed phrase and password
const char *mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const char *password = "";
// Convert BIP39 seed phrase to Bitcoin address
bip39_to_address(mnemonic, password);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment