Sample code for 30+ languages & platforms
PowerBuilder

BIP39 Compute Binary Seed from Mnemonic

See more Encryption Examples

Creates a binary seed from a mnemonic. Uses the PBKDF2 function with a mnemonic sentence (in UTF-8 NFKD) used as the password and the string "mnemonic" + passphrase (again in UTF-8 NFKD) used as the salt. The iteration count is set to 2048 and HMAC-SHA512 is used as the pseudo-random function. The length of the derived key is 512 bits (= 64 bytes).

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
oleobject loo_Crypt
string ls_Mnemonic
string ls_Passphrase
string ls_ExpectedSeed
string ls_ExpectedMasterKey
oleobject loo_BdSalt
string ls_ComputedSeed
oleobject loo_BdSeed
string ls_HmacSha512_hex
oleobject loo_BdHmac
oleobject loo_BdXprv
oleobject loo_BdHash
string ls_SecondHash
string ls_ComputedMasterKey

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Test with the test vectors at https://github.com/trezor/python-mnemonic/blob/master/vectors.json

// This is the 2nd test vector..
ls_Mnemonic = "legal winner thank year wave sausage worth useful legal winner thank yellow"
ls_Passphrase = "TREZOR"
ls_ExpectedSeed = "2e8905819b8723fe2c1d161860e5ee1830318dbf49a83bd451cfb8440c28bd6fa457fe1296106559a3c80937a1c1069be3a3a5bd381ee6260e8d9739fce1f607"
ls_ExpectedMasterKey = "xprv9s21ZrQH143K2gA81bYFHqU68xz1cX2APaSq5tt6MFSLeXnCKV1RVUJt9FWNTbrrryem4ZckN8k4Ls1H6nwdvDTvnV7zEXs2HgPezuVccsq"

// The mnemonic sentence (in UTF-8 NFKD) used as the password.
// The string "mnemonic" + passphrase (again in UTF-8 NFKD) used as the salt.
// The iteration count is set to 2048 and HMAC-SHA512 is used as the pseudo-random function.
// The length of the derived key is 512 bits (= 64 bytes). 

// We want the computed seed to be lowercase hex, therefore our salt must also be hex.
// The seed is the keyword "mnemonic" + passphrase (in this case is "TREZOR") converted to hex.
loo_BdSalt = create oleobject
li_rc = loo_BdSalt.ConnectToNewObject("Chilkat.BinData")

loo_BdSalt.AppendString("mnemonic","utf-8")
loo_BdSalt.AppendString(ls_Passphrase,"utf-8")

ls_ComputedSeed = loo_Crypt.Pbkdf2(ls_Mnemonic,"utf-8","sha512",loo_BdSalt.GetEncoded("hex_lower"),2048,512,"hex_lower")

Write-Debug "Expected: " + ls_ExpectedSeed
Write-Debug "Computed: " + ls_ComputedSeed

// To compute the hd_master_key, duplicate this Python code:

//     def to_hd_master_key(seed: bytes, testnet: bool = False) -> str:
//         if len(seed) != 64:
//             raise ValueError("Provided seed should have length of 64")
// 
//         # Compute HMAC-SHA512 of seed
//         seed = hmac.new(b"Bitcoin seed", seed, digestmod=hashlib.sha512).digest()
// 
//         # Serialization format can be found at: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Serialization_format
//         xprv = b"\x04\x88\xad\xe4"  # Version for private mainnet
//         if testnet:
//             xprv = b"\x04\x35\x83\x94"  # Version for private testnet
//         xprv += b"\x00" * 9  # Depth, parent fingerprint, and child number
//         xprv += seed[32:]  # Chain code
//         xprv += b"\x00" + seed[:32]  # Master key
// 
//         # Double hash using SHA256
//         hashed_xprv = hashlib.sha256(xprv).digest()
//         hashed_xprv = hashlib.sha256(hashed_xprv).digest()
// 
//         # Append 4 bytes of checksum
//         xprv += hashed_xprv[:4]
// 
//         # Return base58
//         return b58encode(xprv)

// First compute the HMAC-SHA512 of the computedSeed
loo_BdSeed = create oleobject
li_rc = loo_BdSeed.ConnectToNewObject("Chilkat.BinData")

loo_BdSeed.AppendEncoded(ls_ComputedSeed,"hex_lower")
loo_Crypt.EncodingMode = "hex_lower"
loo_Crypt.HashAlgorithm = "sha512"
loo_Crypt.SetMacKeyString("Bitcoin seed")
ls_HmacSha512_hex = loo_Crypt.MacBdENC(loo_BdSeed)

loo_BdHmac = create oleobject
li_rc = loo_BdHmac.ConnectToNewObject("Chilkat.BinData")

loo_BdHmac.AppendEncoded(ls_HmacSha512_hex,"hex_lower")

loo_BdXprv = create oleobject
li_rc = loo_BdXprv.ConnectToNewObject("Chilkat.BinData")

loo_BdXprv.AppendEncoded("0488ade4","hex_lower")
loo_BdXprv.AppendEncoded("000000000000000000","hex_lower")
loo_BdXprv.AppendEncoded(loo_BdHmac.GetEncodedChunk(32,32,"hex_lower"),"hex_lower")
loo_BdXprv.AppendByte(0)
loo_BdXprv.AppendEncoded(loo_BdHmac.GetEncodedChunk(0,32,"hex_lower"),"hex_lower")

// Double hash using SHA256
loo_Crypt.EncodingMode = "hex_lower"
loo_Crypt.HashAlgorithm = "sha256"

loo_BdHash = create oleobject
li_rc = loo_BdHash.ConnectToNewObject("Chilkat.BinData")

loo_BdHash.AppendEncoded(loo_Crypt.HashBdENC(loo_BdXprv),"hex_lower")
ls_SecondHash = loo_Crypt.HashBdENC(loo_BdHash)
loo_BdHash.Clear()
loo_BdHash.AppendEncoded(ls_SecondHash,"hex_lower")

// Append the 1st 4 bytes of the bdHash to bdXprv.
loo_BdXprv.AppendEncoded(loo_BdHash.GetEncodedChunk(0,4,"hex_lower"),"hex_lower")

// Base58 encode bdXprv
ls_ComputedMasterKey = loo_BdXprv.GetEncoded("base58")

Write-Debug "Expected Master Key: " + ls_ExpectedMasterKey
Write-Debug "Computed Master Key: " + ls_ComputedMasterKey


destroy loo_Crypt
destroy loo_BdSalt
destroy loo_BdSeed
destroy loo_BdHmac
destroy loo_BdXprv
destroy loo_BdHash