Skip to content

Instantly share code, notes, and snippets.

@ls0f
Created October 21, 2022 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ls0f/2c7101ae846f530540083cc0b3831089 to your computer and use it in GitHub Desktop.
Save ls0f/2c7101ae846f530540083cc0b3831089 to your computer and use it in GitHub Desktop.
generate a eth wallet
package main
import (
"bytes"
"flag"
"fmt"
"log"
"crypto/ecdsa"
"crypto/hmac"
"crypto/sha256"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"golang.org/x/crypto/sha3"
)
var (
token string
mark string
)
func main() {
flag.StringVar(&token, "token", "", "token,keep secret")
flag.StringVar(&mark, "mark", "", "mark(代号,用来生成不同的私钥)")
flag.Parse()
mac := hmac.New(sha256.New, []byte(token))
mac.Write([]byte(mark))
sign := mac.Sum(nil)
//GenerateKey 需要40个字节的随机数,所以再把签名再做一次hash当做随机数
buf := bytes.NewBuffer(sign)
mac.Write(sign)
buf.Write(mac.Sum(nil))
// 把hmac当做随机数,生成私钥
private, err := ecdsa.GenerateKey(crypto.S256(), buf)
if err != nil {
log.Fatal(err)
}
privateKeyBytes := crypto.FromECDSA(private)
fmt.Println(hexutil.Encode(privateKeyBytes)[2:])
publicKey := private.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
// fmt.Println(hexutil.Encode(publicKeyBytes)[4:])
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
fmt.Println(address)
hash := sha3.NewLegacyKeccak256()
hash.Write(publicKeyBytes[1:])
fmt.Println(hexutil.Encode(hash.Sum(nil)[12:]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment