无法使用Truffle/Ganache私钥在Golang中创建公共地址



Truffle/Ganache和Golang Fans,

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-snapped用于从专用十六进制密钥字符串创建公共地址

//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: %sn", hexKey, address)

不幸的是,输出是:

From private key: 9a89a9fa5d10fad0f746ab21e62634d6c472c93f1b5f93259844c1dfd6512d30 address is: 0x0116559E22fb75B5b641b41b313fA030D122Aa1d

期望值:golang代码的输出密钥应该和truffle/ganache的公共地址相同。

哪里出了问题:它不是同一个地址。0xebbf26c04840d7ec79f54e0580028d92afa3d63c!=0x0116559E22fb75B5b641b41b313fA030D122Aa1d

奇怪:当在truffle/ganache上使用golang中生成的ECDSA密钥进行一些事务时,它是有效的。所以签名是正确编码的。

问题:你看到哪里不对劲了吗?truffle/ganache中的十六进制地址是否以不同方式生成?

Thx。

在跌跌撞撞了将近2天后,我发现了这个问题。

上面的代码不是将私钥hex从以太坊转换为golang中的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: %sn", hexKey, address)

最新更新