以太坊私钥到公钥到地址的计算,golang

借助geth官方提供的函数可以很简单的实现代码如下

package main

import (
    "crypto/ecdsa"
    "encoding/hex"
    "fmt"

    "github.com/ethereum/go-ethereum/crypto"
)

func main() {
    priKeyHash := "796c823671b118258b53ef6056fd1f9fc96d125600f348f75f397b2000267fe8"
    // 创建私钥对象,上面私钥没有钱哦
    priKey, err := crypto.HexToECDSA(priKeyHash)
    if err != nil {
        panic(err)
    }
    priKeyBytes := crypto.FromECDSA(priKey)
    fmt.Printf("私钥为: %s\n", hex.EncodeToString(priKeyBytes))

    pubKey := priKey.Public().(*ecdsa.PublicKey)
    // 获取公钥并去除头部0x04
    pubKeyBytes := crypto.FromECDSAPub(pubKey)[1:]
    fmt.Printf("公钥为: %s\n", hex.EncodeToString(pubKeyBytes))

    // 获取地址
    addr := crypto.PubkeyToAddress(*pubKey)
    fmt.Printf("地址为: %s\n", addr.Hex())
}

 

posted @ 2021-01-06 22:24  鸡毛巾  阅读(1182)  评论(0编辑  收藏  举报