-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
199 lines (166 loc) · 4.87 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package utils
import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"os"
"path"
"runtime"
"strings"
"github.com/mit-dci/go-bverify/bitcoin/chainhash"
"github.com/mit-dci/go-bverify/bitcoin/bech32"
"github.com/mit-dci/go-bverify/bitcoin/txscript"
"github.com/mit-dci/go-bverify/bitcoin/wire"
"github.com/mit-dci/go-bverify/bitcoin/btcutil"
"github.com/mit-dci/go-bverify/crypto/btcec"
"github.com/mit-dci/go-bverify/logging"
)
const APP_NAME = "B_Verify"
var overrideClientDataDir string
var maidenHash []byte
// CloneByteSlice clones a byte slice and returns the clone
func CloneByteSlice(b []byte) []byte {
clone := make([]byte, len(b))
copy(clone[:], b[:])
return clone
}
// Max returns the larger of x or y.
func Max(x, y int) int {
if x < y {
return y
}
return x
}
// Min returns the smaller of x or y.
func Min(x, y int) int {
if x > y {
return y
}
return x
}
// GetBit gets the bit at index in a byte array. byte array: byte[0]|| byte[1] ||
// byte[2] || byte[3] index [0...7] [8...15] [16...23] [24...31]
func GetBit(b []byte, idx uint) bool {
bitIdx := uint(idx % 8)
byteIdx := (idx - bitIdx) / 8
return (b[byteIdx] & (1 << (7 - bitIdx))) > 0
}
func DataDirectory() string {
if runtime.GOOS == "windows" {
return path.Join(os.Getenv("APPDATA"), APP_NAME)
} else if runtime.GOOS == "darwin" {
return path.Join(os.Getenv("HOME"), "Library", "Application Support", APP_NAME)
} else if runtime.GOOS == "linux" {
return path.Join(os.Getenv("HOME"), fmt.Sprintf(".%s", strings.ToLower(APP_NAME)))
}
return "."
}
func SetOverrideClientDataDirectory(dataDir string) {
overrideClientDataDir = dataDir
}
func ClientDataDirectory() string {
if overrideClientDataDir != "" {
return overrideClientDataDir
}
if runtime.GOOS == "windows" {
return path.Join(os.Getenv("APPDATA"), APP_NAME+" Client")
} else if runtime.GOOS == "darwin" {
return path.Join(os.Getenv("HOME"), "Library", "Application Support", APP_NAME+" Client")
} else if runtime.GOOS == "linux" {
return path.Join(os.Getenv("HOME"), fmt.Sprintf(".%s", strings.ToLower(APP_NAME+"_client")))
}
return "."
}
func KeyHashFromPkScript(pkscript []byte) []byte {
// match p2wpkh
if len(pkscript) == 22 && pkscript[0] == 0x00 && pkscript[1] == 0x14 {
return pkscript[2:]
}
// match p2wsh
if len(pkscript) == 34 && pkscript[0] == 0x00 && pkscript[1] == 0x20 {
return pkscript[2:]
}
return nil
}
func KeyHashFromPubKey(pk *btcec.PublicKey) [20]byte {
pkh := [20]byte{}
copy(pkh[:], btcutil.Hash160(pk.SerializeCompressed()))
return pkh
}
func PrintTx(tx *wire.MsgTx) {
var buf bytes.Buffer
tx.Serialize(&buf)
logging.Debugf("TX: %x\n", buf.Bytes())
}
func DirectWPKHScriptFromPKH(pkh [20]byte) []byte {
builder := txscript.NewScriptBuilder()
builder.AddOp(txscript.OP_0).AddData(pkh[:])
b, _ := builder.Script()
return b
}
func DirectWSHScriptFromSH(sh [32]byte) []byte {
builder := txscript.NewScriptBuilder()
builder.AddOp(txscript.OP_0).AddData(sh[:])
b, _ := builder.Script()
return b
}
func MaidenHash() []byte {
return maidenHash
}
func DirectWSHScriptFromAddress(adr string) ([]byte, error) {
var scriptHash [32]byte
decoded, err := bech32.SegWitAddressDecode(adr)
if err != nil {
return []byte{}, err
}
copy(scriptHash[:], decoded[2:]) // skip version and pushdata byte returned by SegWitAddressDecode
return DirectWSHScriptFromSH(scriptHash), nil
}
func DirectWPKHScriptFromAddress(adr string) ([]byte, error) {
var pubkeyHash [20]byte
decoded, err := bech32.SegWitAddressDecode(adr)
if err != nil {
return []byte{}, err
}
copy(pubkeyHash[:], decoded[2:]) // skip version and pushdata byte returned by SegWitAddressDecode
return DirectWPKHScriptFromPKH(pubkeyHash), nil
}
func NextPowerOfTwo(n uint64) (e uint) {
for ; (1 << e) < n; e++ {
}
return
}
// HashToBig converts a chainhash.Hash into a big.Int that can be used to
// perform math comparisons.
func HashToBig(hash *chainhash.Hash) *big.Int {
// A Hash is in little-endian, but the big package wants the bytes in
// big-endian, so reverse them.
buf := *hash
blen := len(buf)
for i := 0; i < blen/2; i++ {
buf[i], buf[blen-1-i] = buf[blen-1-i], buf[i]
}
return new(big.Int).SetBytes(buf[:])
}
func GetEnvOrDefault(evar string, def string) string {
val := os.Getenv(evar)
if val == "" {
return def
}
return val
}
func init() {
// The maidenHash is a fixed result of the following log statements being added to the
// server upon first startup:
//
// srv.RegisterLogID([32]byte{}, [33]byte{})
// logHash := fastsha256.Sum256([]byte("Maiden commitment for b_verify"))
// srv.RegisterLogStatement([32]byte{}, 0, logHash[:])
// srv.Commit()
//
// Every commitment chain of b_verify servers will start with this hash - which
// makes it very easy to recognize there were no prior commitments
maidenHash, _ = hex.DecodeString("523e59cfc5235b915dc89de188d87449453b083a8b7d97c1ee64d875da403361")
overrideClientDataDir = ""
}