-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
140 lines (109 loc) Β· 3.69 KB
/
Copy pathmain.go
File metadata and controls
140 lines (109 loc) Β· 3.69 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"crypto/ecdsa"
"flag"
"fmt"
"os"
"path/filepath"
"syscall"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/tyler-smith/go-bip39"
"golang.org/x/term"
)
const (
keystoreDirPerm = 0700
)
func main() {
keystoreOnly := flag.Bool("keystore", false, "Generate as keystore format")
insecure := flag.Bool("insecure", false, "Generate keystore without password (insecure)")
flag.Parse()
fmt.Println("π Generating secure Ethereum wallet...")
fmt.Println()
privateKey, err := crypto.GenerateKey()
if err != nil {
panic(fmt.Sprintf("β Error generating private key: %v", err))
}
privateKeyBytes := crypto.FromECDSA(privateKey)
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
panic("β Error converting public key")
}
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
address := crypto.PubkeyToAddress(*publicKeyECDSA)
if !*keystoreOnly {
mnemonic, err := bip39.NewMnemonic(privateKeyBytes)
if err != nil {
panic(fmt.Sprintf("β Error generating mnemonic phrase: %v", err))
}
fmt.Println("β¨ === Ethereum Wallet Generated ===")
fmt.Println()
fmt.Printf("π Private Key (hex): %s\n", hexutil.Encode(privateKeyBytes))
fmt.Printf("π Public Key (hex): %s\n", hexutil.Encode(publicKeyBytes))
fmt.Printf("π Address: %s\n", address.Hex())
fmt.Println()
fmt.Println("π === BIP39 Mnemonic Phrase ===")
fmt.Println(mnemonic)
fmt.Println()
fmt.Println("β οΈ WARNING: Keep this information secure and never share it!")
} else {
var password string
if *insecure {
password = ""
fmt.Println("β οΈ Insecure mode enabled: keystore will be generated without password!")
fmt.Println()
} else {
fmt.Print("π Enter password for keystore: ")
passwordBytes, err := term.ReadPassword(syscall.Stdin)
if err != nil {
panic(fmt.Sprintf("β Error reading password: %v", err))
}
fmt.Println()
fmt.Print("π Confirm password: ")
passwordConfirm, err := term.ReadPassword(syscall.Stdin)
if err != nil {
panic(fmt.Sprintf("β Error reading password confirmation: %v", err))
}
fmt.Println()
if string(passwordBytes) != string(passwordConfirm) {
panic("β Passwords do not match!")
}
password = string(passwordBytes)
}
keystoreDir := "./keystore"
if err := os.MkdirAll(keystoreDir, os.FileMode(keystoreDirPerm)); err != nil {
panic(fmt.Sprintf("β Error creating keystore directory: %v", err))
}
fmt.Println("πΎ Creating keystore file...")
ks := keystore.NewKeyStore(keystoreDir, keystore.StandardScryptN, keystore.StandardScryptP)
account, err := ks.ImportECDSA(privateKey, password)
if err != nil {
panic(fmt.Sprintf("β Error creating keystore: %v", err))
}
files, err := os.ReadDir(keystoreDir)
if err != nil {
panic(fmt.Sprintf("β Error reading keystore directory: %v", err))
}
var keystoreFile string
for _, file := range files {
if !file.IsDir() {
keystoreFile = filepath.Join(keystoreDir, file.Name())
break
}
}
fmt.Println()
fmt.Println("β
=== Keystore Generated ===")
fmt.Printf("π Keystore file: %s\n", keystoreFile)
fmt.Printf("π Account address: %s\n", account.Address.Hex())
fmt.Println()
if *insecure {
fmt.Println("β οΈ WARNING: Keystore was generated without password (insecure mode)!")
fmt.Println("β οΈ It is extremely vulnerable. Use only for testing!")
} else {
fmt.Println("β οΈ WARNING: Keep this information secure and never share it!")
fmt.Println("β οΈ The keystore file is protected by your password. Don't lose it!")
}
}
}