英文:
Cannot create public address in Golang from Truffle/Ganache private Key
问题
Truffle/Ganache和Golang的粉丝们,
Truffle/Ganache创建了一些地址和账户地址。输出结果如下:
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的公共地址相同。
问题所在:
它们不是相同的地址。
0xebbf26c04840d7ec79f54e0580028d92afa3d63c != 0x0116559E22fb75B5b641b41b313fA030D122Aa1d
奇怪的是:
在truffle/ganache上使用生成的ECDSA密钥进行一些交易时,它是有效的。因此,签名被正确编码。
问题:
你看出了什么问题吗?truffle/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.
答案1
得分: 1
在研究了将以太坊的私钥十六进制转换为Golang中的ECDSA密钥的正确方法后,我终于找到了问题所在。
上述代码不是将以太坊的私钥十六进制转换为ECDSA密钥的正确方法。
正确的方法是:
首先,在字符串的开头添加"0x":
"0x9a89a9fa5d10fad0f746ab21e62634d6c472c93f1b5f93259844c1dfd6512d30"
接下来,需要将字符串转换为字节数组:
privateKeyBin, _ := hexutil.Decode(hexKey)
最后,使用字节数组和加密库将其解码为ECDSA:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论