Skip to content

Instantly share code, notes, and snippets.

@motopig
Created December 5, 2019 05:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save motopig/c680f53897429fd15f5b3ca9aa6f6ed2 to your computer and use it in GitHub Desktop.
Save motopig/c680f53897429fd15f5b3ca9aa6f6ed2 to your computer and use it in GitHub Desktop.
tron address generate
package main
import (
"crypto/ecdsa"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"github.com/mr-tron/base58"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
func main() {
privateKey, err := crypto.GenerateKey()
if err != nil {
log.Fatal(err)
}
privateKeyBytes := crypto.FromECDSA(privateKey)
fmt.Println("privateKey:", hexutil.Encode(privateKeyBytes)[2:])
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
fmt.Println("publicKey:", hexutil.Encode(publicKeyBytes)[2:])
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
address = "41" + address[2:]
fmt.Println("address hex: ", address)
addb, _ := hex.DecodeString(address)
hash1 := s256(s256(addb))
secret := hash1[:4]
for _, v := range secret {
addb = append(addb, v)
}
fmt.Println("address base58: ", base58.Encode(addb))
}
func s256(s []byte) []byte {
h := sha256.New()
h.Write(s)
bs := h.Sum(nil)
return bs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment