'Cannot create public address in Golang from Truffle/Ganache private Key

Truffle/Ganache and Golang Fans,

Truffle/Ganache creates some addresses and account addresses. Output is:


Accounts:
(0) 0xebbf26c04840d7ec79f54e0580028d92afa3d63c
(1) 0x94d4ce2201fe947171a5d5cb148fba01f3d28252
(2) 0x90d7b082368ffc3238cf50c72921c6b9440c017b
(3) 0x25c7798dd837012abd67dd17064700f6aee41d00
(4) 0xbe9382ffad32f6ca2dba469c61e56ce4f15edfa4
(5) 0x6fe07b7ca1472593de5d782c1a8d56f4de7ab9e7
(6) 0xdcb879f5e870729c4d8abe3cc5646f9142a703e6
(7) 0xe188c5f2e9c527a0c51731fde6e9246ca9a13b01
(8) 0xef3d54ccf594e7c5d46d581a4eb95d352f587c41
(9) 0xab23eee383161e398c5e8a4a3038a7e4e31ee060

Private Keys:
(0) 9a89a9fa5d10fad0f746ab21e62634d6c472c93f1b5f93259844c1dfd6512d30
(1) a76dbde7b840c5046f86e2561e1376d5ebb71f35a34e934091c6be2ee3b3ca3a
(2) 4916567b69cb351d409f7f31372258081f77771ff5d89409e3452c50eb600db4
(3) 2d79d2c870240db5101ea1da2e07f206722139dfb44a7f9a22820b4373a618e7
(4) 1aa2f30b5be480bcbd7593ea8343eace968777061d6a51f702378b85f1b847ae
(5) 939d9e3ea19c305b25bf1e1ebc5dc41a5fd1293c9ffc29921bfa456ef0ff2e43
(6) 556993943d0ad470ce747d8b34336c5f0e0bcc6413519858ae44bbc460ddaf0b
(7) 1060c1246343c0863447aa8942c2a4a82bdce14aa238af7736844cf87172fd58
(8) 1742574de65bdf5f533b0ec26a17bbf24b499138e587f01fb65587a5b3b5c211
(9) 38e2db130d2b8f2c0888a64205358b3806434433ebcb8eb3c024ded3656e5943

This golang sniped is used to create the public address from the private hex key string

    //hex input key
    hexKey := "9a89a9fa5d10fad0f746ab21e62634d6c472c93f1b5f93259844c1dfd6512d30"
    //generate private ECDSA Key
    privateKey := new(ecdsa.PrivateKey)
    privateKey.D, _ = new(big.Int).SetString(hexKey, 16) //first private key from Ganache
    privateKey.PublicKey.Curve = elliptic.P256()
    privateKey.PublicKey.X, privateKey.PublicKey.Y = privateKey.PublicKey.Curve.ScalarBaseMult(privateKey.D.Bytes())

    //extract public address from private key
    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
    }
    address := crypto.PubkeyToAddress(*publicKeyECDSA)

    fmt.Printf("From private key: %s address is: %s\n", hexKey, address)

Unfortunately the output is:

From private key: 9a89a9fa5d10fad0f746ab21e62634d6c472c93f1b5f93259844c1dfd6512d30 address is: 0x0116559E22fb75B5b641b41b313fA030D122Aa1d

Expectation: Output key of golang code should be same as public address from truffle/ganache.

What is wrong: It is not the same address. 0xebbf26c04840d7ec79f54e0580028d92afa3d63c != 0x0116559E22fb75B5b641b41b313fA030D122Aa1d

Strange: When doing some transactions with the generated ECDSA Key in golang on truffle/ganache it works. So signature is encoded correctly.

Question: Did you see what is wrong? Is the hex address inside truffle/ganache generated differently?

Thx.



Solution 1:[1]

After stumbling around for almost 2 days I found the issue.

The above code is not the correct way to convert private key hex from ethereum to an ecdsa key in golang.

The correct way:

First the string should be added with a "0x" at the beginning:

"0x9a89a9fa5d10fad0f746ab21e62634d6c472c93f1b5f93259844c1dfd6512d30"

Next it is needed to convert the string into a byte array:

privateKeyBin, _ := hexutil.Decode(hexKey)

And finally use the byte array to decode to ECDSA with the help of the crypto library:

privateKeyBin, _ := hexutil.Decode(hexKey)

Complete code:

    //hex input key
    hexKey := "0x9a89a9fa5d10fad0f746ab21e62634d6c472c93f1b5f93259844c1dfd6512d30"
    //generate private ECDSA Key
    privateKeyBin, err := hexutil.Decode(hexKey)
    if err != nil {
        log.Fatal(err)
    }
    privateKey, err := crypto.ToECDSA(privateKeyBin)
    if err != nil {
        log.Fatal(err)
    }

    //extract public address from private key
    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
    }
    address := crypto.PubkeyToAddress(*publicKeyECDSA)

    fmt.Printf("From private key: %s address is: %s\n", hexKey, address)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David