-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
crypto.go
121 lines (105 loc) · 3.43 KB
/
crypto.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
package faker
import (
"strings"
)
// Crypto is a faker struct for generating bitcoin data
type Crypto struct {
Faker *Faker
}
var (
bitcoinMin = 26
bitcoinMax = 35
ethLen = 42
ethPrefix = "0x"
)
// Checks whether the ascii value provided is in the exclusion for bitcoin.
func (Crypto) isInExclusionZone(ascii int) bool {
switch ascii {
// Ascii for uppercase letter "O", uppercase letter "I", lowercase letter "l", and the number "0"
case 48, 73, 79, 108:
return true
}
return false
}
// algorithmRange decides whether to get digit, uppercase, or lowercase. returns the ascii range to do IntBetween on
func (c Crypto) algorithmRange() (int, int) {
dec := c.Faker.IntBetween(0, 2)
if dec == 0 {
// digit
return 48, 57
} else if dec == 1 {
// upper
return 65, 90
}
// lower
return 97, 122
}
// generateBicoinAddress returns a bitcoin address with a given prefix and length
func (c Crypto) generateBicoinAddress(length int, prefix string, f *Faker) string {
address := []string{prefix}
for i := 0; i < length; i++ {
asciiStart, asciiEnd := c.algorithmRange()
val := f.IntBetween(asciiStart, asciiEnd)
if c.isInExclusionZone(val) {
val++
}
address = append(address, string(rune(val)))
}
return strings.Join(address, "")
}
// P2PKHAddress generates a P2PKH bitcoin address.
// Deprecated: Please use BitcoinAddress instead.
func (c Crypto) P2PKHAddress() string {
length := c.Faker.IntBetween(bitcoinMin, bitcoinMax)
// subtract 1 for prefix
return c.generateBicoinAddress(length-1, "1", c.Faker)
}
// P2PKHAddressWithLength generates a P2PKH bitcoin address with specified length.
// Deprecated: Please use BitcoinAddress instead.
func (c Crypto) P2PKHAddressWithLength(length int) string {
return c.generateBicoinAddress(length-1, "1", c.Faker)
}
// P2SHAddress generates a P2SH bitcoin address.
// Deprecated: Please use BitcoinAddress instead.
func (c Crypto) P2SHAddress() string {
length := c.Faker.IntBetween(bitcoinMin, bitcoinMax)
// subtract 1 for prefix
return c.generateBicoinAddress(length-1, "3", c.Faker)
}
// P2SHAddressWithLength generates a P2PKH bitcoin address with specified length.
// Deprecated: Please use BitcoinAddress instead.
func (c Crypto) P2SHAddressWithLength(length int) string {
return c.generateBicoinAddress(length-1, "3", c.Faker)
}
// Bech32Address generates a Bech32 bitcoin address
// Deprecated: Please use BitcoinAddress instead.
func (c Crypto) Bech32Address() string {
length := c.Faker.IntBetween(bitcoinMin, bitcoinMax)
// subtract 1 for prefix
return c.generateBicoinAddress(length-3, "bc1", c.Faker)
}
// Bech32AddressWithLength generates a Bech32 bitcoin address with specified length.
// Deprecated: Please use BitcoinAddress instead.
func (c Crypto) Bech32AddressWithLength(length int) string {
return c.generateBicoinAddress(length-3, "bc1", c.Faker)
}
// BitcoinAddress returns an address of either Bech32, P2PKH, or P2SH type.
func (c Crypto) BitcoinAddress() string {
dec := c.Faker.IntBetween(0, 2)
if dec == 0 {
return c.Bech32Address()
} else if dec == 1 {
return c.P2SHAddress()
}
return c.P2PKHAddress()
}
// EtheriumAddress returns a hexadecimal ethereum address of 42 characters.
func (c Crypto) EtheriumAddress() string {
address := []string{ethPrefix}
for i := 0; i < ethLen-2; i++ {
asciiStart, asciiEnd := c.algorithmRange()
val := c.Faker.IntBetween(asciiStart, asciiEnd)
address = append(address, string(rune(val)))
}
return strings.Join(address, "")
}