This repository was archived by the owner on Mar 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutils.go
136 lines (122 loc) · 2.68 KB
/
utils.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
package utils
import (
"fmt"
"net"
"github.com/btcsuite/btcd/chaincfg"
)
// PanicOnError panics if err is not nil
func PanicOnError(err error) {
if err != nil {
panic(err)
}
}
func Max(num uint32, nums ...uint32) uint32 {
r := num
for _, v := range nums {
if v > r {
r = v
}
}
return r
}
type Network string
type BackendName string
const (
Mainnet Network = "mainnet"
Testnet Network = "testnet"
Electrum BackendName = "electrum"
Btcd BackendName = "btcd"
)
// ChainConfig returns a given chaincfg.Params for a given Network
func (n Network) ChainConfig() *chaincfg.Params {
switch n {
case Mainnet:
return &chaincfg.MainNetParams
case Testnet:
return &chaincfg.TestNet3Params
default:
panic("unreachable")
}
}
// prefixes come from BIP32
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format
func XpubToNetwork(xpub string) Network {
prefix := xpub[0:4]
switch prefix {
case "xpub":
return Mainnet
case "tpub":
return Testnet
default:
panic(fmt.Sprintf("unknown prefix: %s", xpub))
}
}
func AddressToNetwork(addr string) Network {
switch addr[0] {
case 'm':
return Testnet // pubkey hash
case 'n':
return Testnet // pubkey hash
case '2':
return Testnet //script hash
case '1':
return Mainnet // pubkey hash
case '3':
return Mainnet // script hash
default:
panic(fmt.Sprintf("unknown prefix: %s", addr))
}
}
func GenesisBlock(network Network) string {
switch network {
case Mainnet:
return "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
case Testnet:
return "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"
default:
panic("unreachable")
}
}
func VerifyMandN(m int, n int) error {
if m <= 0 {
return fmt.Errorf("m has to be positive (got %d)", m)
}
if m > n {
return fmt.Errorf("m cannot be larger than n (%d > %d)", m, n)
}
if n > 20 {
return fmt.Errorf("n cannot be greater than 20 (got %d)", n)
}
return nil
}
// Picks a default server for electrum or localhost for btcd
// Returns a pair of hostname:port (or pseudo-port for electrum)
func GetDefaultServer(network Network, backend BackendName, addr string) (string, string) {
if addr != "" {
host, port, err := net.SplitHostPort(addr)
PanicOnError(err)
return host, port
}
switch backend {
case Electrum:
switch network {
case "mainnet":
return "electrum.petrkr.net", "s50002"
case "testnet":
return "testnet.hsmiths.com", "s53012"
default:
panic("unreachable")
}
case Btcd:
switch network {
case "mainnet":
return "localhost", "8334"
case "testnet":
return "localhost", "18334"
default:
panic("unreachable")
}
default:
panic("unreachable")
}
}