2

I want to generate a bip32 private root key based on bip39 seed. I found an article with python code, copied it to myself and executed it, getting a valid private root key (checked here). Then I wrote a similar script on golang, inserted the seed from the python example into it, and after executing it, I got the wrong root key. The set of bytes, which is then encoded by base58 to obtain the root key, coincided in both scripts, but the result of base58 encoding was different.

Python example:

...
print(all_bytes.hex())
root_key = base58.b58encode_check(all_bytes).decode('utf8')
print(root_key)

Python output:

0488ade400000000000000000060499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689004b03d6fc340455b363f51020ad3ecca4f0850280cf436c70c727923f6db46c3e
xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U

Golang example:

fmt.Println(hex.EncodeToString(buffer.Bytes()))
rootKey := base58.CheckEncode(buffer.Bytes(), 0)
fmt.Println(rootKey)

Golang output:

0488ade400000000000000000060499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689004b03d6fc340455b363f51020ad3ecca4f0850280cf436c70c727923f6db46c3e
1xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsUrGUX

As you can see, the bytes that are used for encoding are the same here and there, but the result is different. I tried using another golang module to generate a base58 string, but it generates an error for this seed :/ Maybe someone has encountered something similar and knows how to generate the correct base58 strings in golang

1 Answer 1

0

Your problem might come from the base58.CheckEncode function, try using a different Golang library like this one github.com/btcsuite/btcutil/base58 and below is how to use it in your code

package main

import (
    "bytes"
    "encoding/hex"
    "fmt"

    "github.com/btcsuite/btcutil/base58"
)

func main() {
    allBytes := []byte{...} // Your bytes here

    fmt.Println(hex.EncodeToString(allBytes))
    rootKey := base58.Encode(allBytes)
    fmt.Println(rootKey)
}
1
  • I tried this module, but the result on a similar set of bytes was quite strange: 9Xoy2u8vevhdokmLKtGXvGcDTHpSQV2DdjbpRe9a7vRGmByeQbE1YvZDpA8ZNfNNdqiyZ3ycaZ8E6pJeseYjvSgvZCMgLRb82bMrmr6G3P
    – Mod diller
    Apr 16, 2023 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.