generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblockchain.go
126 lines (115 loc) · 3.79 KB
/
blockchain.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
package nownodes
import (
"encoding/hex"
"strings"
)
// Blockchain is the supported blockchain networks
type Blockchain string
// Supported blockchains
const (
BCH Blockchain = blockchainBCH // BitcoinCash: https://bch.info/
BSV Blockchain = blockchainBSV // BitCoin: https://bitcoinsv.com
BTC Blockchain = blockchainBTC // BitCore: https://bitcoin.org
BTCTestnet Blockchain = blockchainBTCTestnet // BitCore Testnet: https://bitcoin.org
BTG Blockchain = blockchainBTG // BitGold: https://bitcoingold.org/
DASH Blockchain = blockchainDASH // Dash: https://www.dash.org/
DOGE Blockchain = blockchainDOGE // DogeCoin: https://dogecoin.com/
LTC Blockchain = blockchainLTC // LiteCoin: https://litecoin.org/
ETH Blockchain = blockchainETH // Ethereum: https://ethereum.org/
)
const (
period = "."
)
// String is the string version of the blockchain
func (n Blockchain) String() string {
return string(n)
}
// BlockBookURL is the url for the block book API
func (n Blockchain) BlockBookURL() string {
switch n {
case BCH:
return blockchainBCH + period + nowNodesURL
case BSV:
return blockchainBSV + period + nowNodesURL
case BTC:
return blockchainBTC + period + nowNodesURL
case BTCTestnet:
return blockchainBTCTestnet + period + nowNodesURL
case BTG:
return blockchainBTG + period + nowNodesURL
case DASH:
return blockchainDASH + period + nowNodesURL
case DOGE:
return blockchainDOGE + period + nowNodesURL
case LTC:
return blockchainLTC + period + nowNodesURL
case ETH:
return blockchainETH + period + nowNodesURL
default:
return ""
}
}
// NodeAPIURL is the url for the Node API
func (n Blockchain) NodeAPIURL() string {
return n.BlockBookURL() // Right now it's the same urls
}
// ValidateTxID will do basic validations on the tx id string
func (n Blockchain) ValidateTxID(txID string) bool {
switch n {
case BCH, BSV, BTC, BTCTestnet, BTG, DASH, DOGE, LTC:
return len(txID) == bitcoinTransactionLength
case ETH:
return len(txID) == ethereumTransactionLength
default:
return false
}
}
// ValidateTxHex will do basic validations on the tx hex string
func (n Blockchain) ValidateTxHex(txHex string) bool {
switch n {
case BCH, BSV, BTC, BTCTestnet, BTG, DASH, DOGE, LTC:
if b, err := hex.DecodeString(
txHex,
); err != nil || len(b) == 0 {
return false
}
return true
case ETH:
return true // NOT IMPLEMENTED
default:
return false
}
}
// ValidateAddress will do basic validations on the address
func (n Blockchain) ValidateAddress(address string) bool {
switch n {
case BCH:
// note: validate that it's a LTC address (prefix)
withoutPrefix := strings.ReplaceAll(address, bitcoinCashPrefix, "")
return len(withoutPrefix) >= bitcoinMinAddressLength && len(withoutPrefix) <= bitcoinCashMaxAddressLength
case BSV:
return len(address) >= bitcoinMinAddressLength && len(address) <= bitcoinMaxAddressLength
case BTC, BTCTestnet, BTG, DOGE:
return len(address) >= bitcoinMinAddressLength && len(address) <= bitcoinMaxAddressLength
case DASH:
// note: validate that it's a DASH address (prefix)
return len(address) >= bitcoinMinAddressLength && len(address) <= bitcoinMaxAddressLength
case LTC:
// note: validate that it's a LTC address (prefix)
return len(address) >= bitcoinMinAddressLength && len(address) <= liteCoinMaxAddressLength
case ETH:
// note: validate that it's a ETH address
return len(address) >= bitcoinMinAddressLength && len(address) <= ethereumMaxAddressLength
default:
return false
}
}
// isBlockchainSupported will return true if the blockchain was found in the list
func isBlockchainSupported(list []Blockchain, blockchain Blockchain) bool {
for _, chain := range list {
if chain == blockchain {
return true
}
}
return false
}