/
wallet.go
149 lines (140 loc) · 3.34 KB
/
wallet.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
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
141
142
143
144
145
146
147
148
149
package utils
import (
"crypto/ecdsa"
"fmt"
"log"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"golang.org/x/crypto/sha3"
)
// Create a pair of private key and address.
func CreateKey() (privateKey string, address string) {
private, err := crypto.GenerateKey()
if err != nil {
log.Fatal(err)
}
privateKeyBytes := crypto.FromECDSA(private)
privateKey = hexutil.Encode(privateKeyBytes)[2:]
publicKey := private.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
address = crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
return
}
// From private key to address.
func Private2Address(key string) string {
private, err := crypto.HexToECDSA(key)
if err != nil {
return ""
}
publicKey := private.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return ""
}
return crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
}
// Return checksummed ethereum address.
func ChecksumEthereumAddress(addr string) string {
hex := strings.ToLower(addr)[2:]
d := sha3.NewLegacyKeccak256()
d.Write([]byte(hex))
hash := d.Sum(nil)
checksumed := "0x"
for i, b := range hex {
c := string(b)
if b < '0' || b > '9' {
if hash[i/2]&byte(128-i%2*120) != 0 {
c = string(b - 32)
}
}
checksumed += c
}
return checksumed
}
type findAddr struct {
Private string
Address string
}
// Find an address contain the target string.
func FindAddressContain(times int, targets ...string) []findAddr {
for _, target := range targets {
_, err := strconv.ParseInt(target, 16, 64)
// not hex
if err != nil {
return nil
}
}
var found []findAddr
var tried int = 0
for len(found) < times {
priv, addr := CreateKey()
if ContainAnyOfStrs(addr, targets) {
found = append(found, findAddr{Private: priv, Address: addr})
}
tried += 1
if tried > 10000000 {
fmt.Println("already tried 10 million times, the target maybe too long")
return found
}
}
return found
}
// Find an address start with the target string.
func FindAddressStart(times int, targets ...string) []findAddr {
for _, target := range targets {
_, err := strconv.ParseInt(target, 16, 64)
// not hex
if err != nil {
return nil
}
}
var found []findAddr
var tried int = 0
for len(found) < times {
priv, addr := CreateKey()
for _, target := range targets {
if strings.EqualFold(target, addr[2:len(target)+2]) {
found = append(found, findAddr{Private: priv, Address: addr})
continue
}
}
tried += 1
if tried > 10000000 {
fmt.Println("already tried 10 million times, the target maybe too long")
return found
}
}
return found
}
// Find an address end with the target string.
func FindAddressEnd(times int, targets ...string) []findAddr {
for _, target := range targets {
_, err := strconv.ParseInt(target, 16, 64)
// not hex
if err != nil {
return nil
}
}
var found []findAddr
var tried int = 0
for len(found) < times {
priv, addr := CreateKey()
for _, target := range targets {
if strings.EqualFold(target, addr[42-len(target):]) {
found = append(found, findAddr{Private: priv, Address: addr})
continue
}
}
tried += 1
if tried > 10000000 {
fmt.Println("already tried 10 million times, the target maybe too long")
return found
}
}
return found
}