github.com/aergoio/aergo@v1.3.1/tools/pkgen/pkgen.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"github.com/aergoio/aergo/types"
    10  	"os"
    11  
    12  	"github.com/libp2p/go-libp2p-core/crypto"
    13  )
    14  
    15  func main() {
    16  	argsWithoutProg := os.Args[1:]
    17  	if 0 == len(argsWithoutProg) {
    18  		panic("Usage: pkgen <fileprefix>")
    19  	}
    20  	prefix := argsWithoutProg[0]
    21  	priv, pub, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
    22  	pid, _ := types.IDFromPublicKey(pub)
    23  
    24  	pkFile := prefix + ".key"
    25  	pubFile := prefix + ".pub"
    26  	idFile := prefix + ".id"
    27  
    28  	pkf, err := os.Create(pkFile)
    29  	if err != nil {
    30  		panic("fail to create key file: " + pkFile)
    31  	}
    32  
    33  	pkBytes, err := priv.Bytes()
    34  	if err != nil {
    35  		panic("wrong key <fileprefix>")
    36  	}
    37  	pkf.Write(pkBytes)
    38  	pkf.Sync()
    39  
    40  	pubf, err := os.Create(pubFile)
    41  	if err != nil {
    42  		panic("fail to create pub file: " + pubFile)
    43  	}
    44  
    45  	pubBytes, err := pub.Bytes()
    46  	if err != nil {
    47  		panic("wrong key <fileprefix>")
    48  	}
    49  	pubf.Write(pubBytes)
    50  	pubf.Sync()
    51  
    52  	idf, err := os.Create(idFile)
    53  	idBytes := []byte(types.IDB58Encode(pid))
    54  	if err != nil {
    55  		panic("wrong key <fileprefix>")
    56  	}
    57  	idf.Write(idBytes)
    58  	idf.Sync()
    59  
    60  	fmt.Println("Done!")
    61  }