我非常努力地将字符串格式的以太坊私钥 BIP44 转换为可以 (*ecdsa.PrivateKey) 被其余代码使用的类型。
import (
"crypto/x509"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
const (
privateKey2 string = "0xbacd06016aea4280e14efd7182ba18cd98bf11701943d3d47d76b04bb7baad19"
)
func main() {
_, err = x509.ParsePKCS8PrivateKey(firstKey)
if err != nil {
fmt.Println("Cannot parse private key")
}
}
答案 0 :(得分:1)
这是如何将私钥转换为以太坊地址 您需要导入一个额外的包“crypto/ecdsa”,并从私钥中删除“0x”。
privateKey, err := crypto.HexToECDSA(privateKey2)
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)