-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (59 loc) · 2.06 KB
/
main.go
File metadata and controls
69 lines (59 loc) · 2.06 KB
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
package main
import (
"crypto/ecdsa"
"errors"
"flag"
"fmt"
"strings"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
)
func panicError(err error) {
if err != nil {
panic(err)
}
}
func log(logAllowed bool, msg string) {
if logAllowed {
fmt.Println(msg)
}
}
func main() {
pwd := flag.String("pwd", "", "Password to encrypt keystore file")
dir := flag.String("dir", "./wallets", "Directory to store keystore file")
logAllowed := flag.Bool("logging", false, "Dis/enable logging keys")
mnemonic := flag.String("mnemonic", "", "Use a mnemonic phrase to generate the wallet")
flag.Parse()
// generating privateKey
var privateKey *ecdsa.PrivateKey
if len([]byte(*mnemonic)) > 0 { // using mnemonic phrase
wallet, err := hdwallet.NewFromMnemonic(strings.Trim(*mnemonic, "\""))
panicError(err)
account, err := wallet.Derive(hdwallet.DefaultBaseDerivationPath, false)
panicError(err)
privateKey, err = wallet.PrivateKey(account)
panicError(err)
} else {
var err error
privateKey, err = crypto.GenerateKey() // generating new random privateKey
panicError(err)
}
// generating privateKey
privateKeyBytes := crypto.FromECDSA(privateKey)
log(*logAllowed, "PrivateKey: "+hexutil.Encode(privateKeyBytes)[2:])
// getting publicKey from privateKey
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
panicError(errors.New("fetched publicKey does not have type of ecdsa.PublicKey"))
}
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
log(*logAllowed, "PublicKey: "+hexutil.Encode(publicKeyBytes)[2:])
// generating keystore file from privateKey
ks := keystore.NewKeyStore(*dir, keystore.StandardScryptN, keystore.StandardScryptP)
account, err := ks.ImportECDSA(privateKey, *pwd) // will throw error if one uses same mnemonic phrase again
panicError(err)
log(*logAllowed, "Ethereum Wallet ("+account.Address.Hex()+") has been generated and stored in "+*dir)
}