-
Notifications
You must be signed in to change notification settings - Fork 1
/
address.go
137 lines (120 loc) · 3.83 KB
/
address.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
// Copyright gotools (https://github.com/zerjioang/gotools)
// SPDX-License-Identifier: GNU GPL v3
package eth
import (
"regexp"
"github.com/zerjioang/gotools/lib/eth/fixtures/common"
"github.com/zerjioang/gotools/util/str"
"github.com/zerjioang/gotools/lib/eth/fixtures"
)
const (
// length of an ethereum address
AddressLengthBytes = 20
// length of ethereum address hexadecimal encoded
AddressLengthHexEncoded = AddressLengthBytes * 2
// length of ethereum address hexadecimal encoded and prefixed with '0x'
AddressLengthHexEncodedWithPrefix = 2 + AddressLengthHexEncoded
)
// an ethereum address is represented as 20 bytes
// in 1Gb (1000000000 bytes), 50.000.000 addresses can be stored
// as individual addresses.
// using a database or radix tree, b-tree or similar this space can be reduced
var (
addressRegex = regexp.MustCompile("^0x[0-9a-fA-F]{40}$")
)
// converts an string ethereum address to eth address
// addr: 0x71c7656ec7ab88b098defb751b7401b5f6d8976f
func ConvertAddress(addr string) fixtures.Address {
return fixtures.HexToAddress(addr)
}
// check if an address is syntactically valid or not
// example address: 0x71c7656ec7ab88b098defb751b7401b5f6d8976f
func IsValidAddress(addr string) bool {
return addressRegex.MatchString(addr)
}
// check if an address is syntactically valid or not
// example address: 0x71c7656ec7ab88b098defb751b7401b5f6d8976f
func IsValidAddressLow(addr string) bool {
if len(addr) == 42 {
raw := str.UnsafeBytes(addr)
// for bound checks speed up
_ = raw[41]
// check adress begin (0x)
x := raw[1]
zero := raw[0]
if zero == '0' && (x == 'x' || x == 'X') {
//check address body
for i := 2; i < 40; i++ {
c := raw[i]
if !((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9')) {
return false
}
}
return true
}
}
return false
}
// check if the input string represents a valid block number
// allowed block numbers are:
// The following options are possible for the defaultBlock parameter:
// HEX String - an integer block number
// String "earliest" for the earliest/genesis block
// String "latest" - for the latest mined block
// String "pending" - for the pending state/transactions
func IsValidBlockNumber(blkStr string) bool {
if blkStr == "" {
return false
} else if blkStr == "earliest" {
return true
} else if blkStr == "latest" {
return true
} else if blkStr == "pending" {
return true
} else {
//validate input is valid hex string
return common.IsOxPrefixedHex(blkStr)
}
}
// a block hash is valid only if:
// starts with 0x
// is hexadecimal string of 32 bytes encoded data (64 bytes as hex string) + 0x prefix
// example: 0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238
func IsValidBlockHash(blockHash string) bool {
if len(blockHash) == 66 {
return common.IsOxPrefixedHex(blockHash)
}
return false
}
// IsZeroAddress validate if it's a 0 address
func IsZeroAddress(addr string) bool {
if len(addr) == AddressLengthHexEncodedWithPrefix {
return addr == "0x0000000000000000000000000000000000000000"
}
return false
}
// check whether given byte value is hexadecimal or not
func IsXdigit(c byte) bool {
return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9')
}
func IsValidHexSignature(signature string) bool {
if signature != "" && len(signature) == 130 {
return common.IsHex(signature)
}
return false
}
// hex encoded private key example: cdfa05e62455fae56b1fea15607691975db23b6bef5342f9f50505769529d
// hex encoded private key example: 0xcdfa05e62455fae56b1fea15607691975db23b6bef5342f9f50505769529d
func IsValidHexPrivateKey(pkey string) bool {
if pkey != "" {
switch len(pkey) {
case 61:
// private key does not contain 0x prefix
return common.IsHex(pkey)
case 63:
// private key contain 0x prefix
return common.IsOxPrefixedHex(pkey)
}
}
return false
}