-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (58 loc) · 1.48 KB
/
main.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
62
63
64
65
66
67
68
69
70
package main
import (
"crypto/ecdsa"
"errors"
"log"
"os"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/flashbots/builder-tools/common"
cli "github.com/urfave/cli/v2" // imports as package "cli"
)
// var flags []cli.Flag = []cli.Flag{
// &cli.BoolFlag{
// Name: "log-json",
// Value: false,
// Usage: "log in JSON format",
// },
// &cli.BoolFlag{
// Name: "log-debug",
// Value: false,
// Usage: "log debug messages",
// },
// }
func main() {
app := &cli.App{
Name: "ecdsa-gen",
Usage: "Create ECDSA keypair",
// Flags: flags,
Action: runCli,
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func runCli(cCtx *cli.Context) error {
log := common.SetupLogger(&common.LoggingOpts{})
ecdsaPrivkeyHex, ecdsaAddressHex, err := genECDSA()
if err != nil {
return err
}
log.Info("ECDSA keypair generated", "private", ecdsaPrivkeyHex, "address", ecdsaAddressHex)
return nil
}
func genECDSA() (privkey, address string, err error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
log.Fatal(err)
}
privateKeyBytes := crypto.FromECDSA(privateKey)
privkeyHex := hexutil.Encode(privateKeyBytes)[2:]
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return "", "", errors.New("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
addressHex := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
return privkeyHex, addressHex, nil
}