btcsuite / btcutil

Provides bitcoin-specific convenience functions and types
479 stars 409 forks source link

Derived Addresses not matching bip-44 #112

Closed themihai closed 6 years ago

themihai commented 6 years ago

I'm trying to generate bip-44 addresses. The address looks valid but when I use the same seed and path (m/49'/0'/0'/0/0) with different wallets they generate a different set of addresses which makes me believe I'm doing something wrong. E.g. The same mnemonic/seed used with https://iancoleman.io/bip39/ returns m/44'/0'/0'/0/0 | 17s47s1bFG6qG8bqPxwkCpnDHWEc3z2KFn as first address while my code returns 15oRMZN7rFEjp2EiRyoMXJ5GiLXQkGj8yq

package main

import (
    "fmt"
    "github.com/btcsuite/btcd/chaincfg"
    "github.com/btcsuite/btcutil/hdkeychain"
    bip39 "github.com/tyler-smith/go-bip39"
)

func main() {
    const mnemonic = "attract immense unknown voice prosper result since soul toast talent much need"
    seed := bip39.NewSeed(mnemonic, "")
    // Generate a new master node using the seed.
    master, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
    if err != nil {
        panic(err)
        return
    }

    // m/49'
    purpose, err := master.Child(49 + hdkeychain.HardenedKeyStart)
    if err != nil {
        panic(err)
    }

    // m/49'/0'
    coinType, err := purpose.Child(0 + hdkeychain.HardenedKeyStart)
    if err != nil {
        panic(err)
    }

    // m/49'/0'/0'
    acct0, err := coinType.Child(0 + hdkeychain.HardenedKeyStart)
    if err != nil {
        panic(err)
    }

    // m/49'/0'/0'/0
    acct0External, err := acct0.Child(0)
    if err != nil {
        panic(err)
    }

    acct0ExternalPub, err := acct0External.Neuter()
    if err != nil {
        panic(err)
    }

    // m/49'/0'/0'/0/0
    acct0External0Pub, err := acct0ExternalPub.Child(0)
    if err != nil {
        panic(err)
    }
    addr, err := acct0External0Pub.Address(&chaincfg.MainNetParams)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s", addr)
    return

}
themihai commented 6 years ago

I was using bip-49 prefix thus the reason why the addresses were not matching.