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