无法通过松露/ganache私钥在Golang中创建公共地址

发布于 2025-01-18 20:37:29 字数 2662 浏览 4 评论 0原文

松露/Ganache和Golang粉丝,

松露/Ganache创建了一些地址和帐户地址。输出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

此golang狙击用于从私有六角键字符串中创建公共地址,

    //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)

但不幸的是,输出为:

From private key: 9a89a9fa5d10fad0f746ab21e62634d6c472c93f1b5f93259844c1dfd6512d30 address is: 0x0116559E22fb75B5b641b41b313fA030D122Aa1d

期望: Golang代码的输出密钥应与Truffle/Ganache中的公共地址相同。

怎么了: 它不是相同的地址。 0xebbf26C04840D79F54E0580028D92AFA3D63C!= =

0x011659E222222FB75B5B5B5B641B41B41B41B41B31B313FA0303FA030D122AA1D12AA1D 当在Truffle/Ganache上使用Golang生成的ECDSA密钥进行一些交易时,它可以正常工作。因此签名正确编码。

问题: 你看到怎么了吗?松露/Ganache内部的十六进制地址是否产生不同?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝咒 2025-01-25 20:37:29

绊倒了将近两天后,我发现了这个问题。

上面的代码上面的 不是将私有密钥 从以太坊转换为Golang的ECDSA密钥的正确方法。

正确的方法:

首先应在开始时添加字符串:

"0x9a89a9fa5d10fad0f746ab21e62634d6c472c93f1b5f93259844c1dfd6512d30"

接下来需要将字符串转换为字节数组:

privateKeyBin, _ := hexutil.Decode(hexKey)

最后使用字节数组在加密库库的帮助下解码到ECDA:

privateKeyBin, _ := hexutil.Decode(hexKey)

完成代码:

    //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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文