forked from PlatONnetwork/PlatON-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genkeypair.go
54 lines (50 loc) · 1.28 KB
/
genkeypair.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
package main
import (
"github.com/PlatONnetwork/PlatON-Go/cmd/utils"
"github.com/PlatONnetwork/PlatON-Go/crypto"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"gopkg.in/urfave/cli.v1"
)
type outputGenkeypair struct {
Address string
PrivateKey string
PublicKey string
}
var commandGenkeypair = cli.Command{
Name: "genkeypair",
Usage: "generate new private key pair",
ArgsUsage: "[ ]",
Description: `
Generate a new private key pair.
.
`,
Flags: []cli.Flag{
jsonFlag,
},
Action: func(ctx *cli.Context) error {
// Check if keyfile path given and make sure it doesn't already exist.
var privateKey *ecdsa.PrivateKey
var err error
// generate random.
privateKey, err = crypto.GenerateKey()
if err != nil {
utils.Fatalf("Failed to generate random private key: %v", err)
}
// Output some information.
out := outputGenkeypair{
Address: crypto.PubkeyToAddress(privateKey.PublicKey).Hex(),
PublicKey: hex.EncodeToString(crypto.FromECDSAPub(&privateKey.PublicKey)[1:]),
PrivateKey: hex.EncodeToString(crypto.FromECDSA(privateKey)),
}
if ctx.Bool(jsonFlag.Name) {
mustPrintJSON(out)
} else {
fmt.Println("Address : ", out.Address)
fmt.Println("PrivateKey: ", out.PrivateKey)
fmt.Println("PublicKey : ", out.PublicKey)
}
return nil
},
}