以太坊私钥到公钥到地址的计算,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())
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM