go-ethereum Create Wallet

杨宏儒
2023-12-01
package main

import (
	ethereum_crypto "github.com/ethereum/go-ethereum/crypto"
	"fmt"
	"github.com/ethereum/go-ethereum/common/hexutil"
	"crypto/ecdsa"
)

func main()  {
	privateKey, err := ethereum_crypto.GenerateKey()
	if err != nil {
		fmt.Print(err)
		return
	}
	privateKeyBytes := ethereum_crypto.FromECDSA(privateKey)
	privateKeyHex := hexutil.Encode(privateKeyBytes)
	fmt.Println(privateKeyHex)

	_publicKey := privateKey.Public()
	publicKey := _publicKey.(*ecdsa.PublicKey)
	publicKeyBytes := ethereum_crypto.FromECDSAPub(publicKey)
	publicKeyHex := hexutil.Encode(publicKeyBytes)
	fmt.Println(publicKeyHex)

	address := ethereum_crypto.PubkeyToAddress(*publicKey).Hex()
	fmt.Print(address)
}

 

 类似资料: