/
util.go
318 lines (269 loc) · 7.53 KB
/
util.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package primitives
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"strconv"
"strings"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/btcsuitereleases/btcutil/base58"
)
func CalculateCoinbasePayout(efficiency uint16) uint64 {
// Keep is the percentage of the coinbase kept for the authority
// (Percentage * 100)
keep := 10000 - uint64(efficiency)
// The amount of factoshis in the payout
payout := keep * constants.COINBASE_PAYOUT_AMOUNT
// Put the percentage back into the correct scale
// (10000 == 100%, so divide by 10000)
payout = payout / 10000
return payout
}
func EfficiencyToString(eff uint16) string {
return fmt.Sprintf("%d.%02d", eff/100, eff%100)
}
/*********************************
* Print helpers
********************************/
func AddCommas(v int64) (ret string) {
pos := true
if v < 0 {
pos = false
v = -v
}
finish := func() {
if pos {
return
}
ret = "-" + ret
return
}
defer finish()
for {
nxt := v / 1000
this := v % 1000
switch {
case nxt == 0:
ret = fmt.Sprintf("%d%s", this, ret)
return
default:
ret = fmt.Sprintf(",%03d%s", this, ret)
v = v / 1000
}
}
}
/*********************************
* Marshalling helper functions
*********************************/
func WriteNumber64(out *Buffer, num uint64) {
var buf Buffer
binary.Write(&buf, binary.BigEndian, num)
str := hex.EncodeToString(buf.DeepCopyBytes())
out.WriteString(str)
}
func WriteNumber32(out *Buffer, num uint32) {
var buf Buffer
binary.Write(&buf, binary.BigEndian, num)
str := hex.EncodeToString(buf.DeepCopyBytes())
out.WriteString(str)
}
func WriteNumber16(out *Buffer, num uint16) {
var buf Buffer
binary.Write(&buf, binary.BigEndian, num)
str := hex.EncodeToString(buf.DeepCopyBytes())
out.WriteString(str)
}
func WriteNumber8(out *Buffer, num uint8) {
var buf Buffer
binary.Write(&buf, binary.BigEndian, num)
str := hex.EncodeToString(buf.DeepCopyBytes())
out.WriteString(str)
}
/************************************************
* Helper Functions for User Address handling
************************************************/
// Factoid Address
//
//
// Add a prefix of 0x5fb1 at the start, and the first 4 bytes of a SHA256d to
// the end. Using zeros for the address, this might look like:
//
// 5fb10000000000000000000000000000000000000000000000000000000000000000d48a8e32
//
// A typical Factoid Address:
//
// FA1y5ZGuHSLmf2TqNf6hVMkPiNGyQpQDTFJvDLRkKQaoPo4bmbgu
//
// Entry credits only differ by the prefix of 0x592a and typically look like:
//
// EC3htx3MxKqKTrTMYj4ApWD8T3nYBCQw99veRvH1FLFdjgN6GuNK
//
// More words on this can be found here:
//
// https://github.com/FactomProject/FactomDocs/blob/master/factomDataStructureDetails.md#human-readable-addresses
//
var FactoidPrefix = []byte{0x5f, 0xb1}
var EntryCreditPrefix = []byte{0x59, 0x2a}
var FactoidPrivatePrefix = []byte{0x64, 0x78}
var EntryCreditPrivatePrefix = []byte{0x5d, 0xb6}
// Converts factoshis to floating point factoids
func ConvertDecimalToFloat(v uint64) float64 {
f := float64(v)
f = f / 100000000.0
return f
}
// Converts factoshis to floating point string
func ConvertDecimalToString(v uint64) string {
f := ConvertDecimalToFloat(v)
return fmt.Sprintf("%.8f", f)
}
// Take fixed point data and produce a nice decimal point
// sort of output that users can handle.
func ConvertDecimalToPaddedString(v uint64) string {
tv := v / 100000000
bv := v - (tv * 100000000)
var str string
// Count zeros to lop off
var cnt int
for cnt = 0; cnt < 7; cnt++ {
if (bv/10)*10 != bv {
break
}
bv = bv / 10
}
// Print the proper format string
fstr := fmt.Sprintf(" %s%dv.%s0%vd", "%", 12, "%", 8-cnt)
// Use the format string to print our Factoid balance
str = fmt.Sprintf(fstr, tv, bv)
return str
}
// Convert Decimal point input to FixedPoint (no decimal point)
// output suitable for Factom to chew on.
func ConvertFixedPoint(amt string) (string, error) {
var v int64
var err error
index := strings.Index(amt, ".")
if index == 0 {
amt = "0" + amt
index++
}
if index < 0 {
v, err = strconv.ParseInt(amt, 10, 64)
if err != nil {
return "", err
}
v *= 100000000 // Convert to Factoshis
} else {
tp := amt[:index]
v, err = strconv.ParseInt(tp, 10, 64)
if err != nil {
return "", err
}
v = v * 100000000 // Convert to Factoshis
bp := amt[index+1:]
if len(bp) > 8 {
bp = bp[:8]
}
bpv, err := strconv.ParseInt(bp, 10, 64)
if err != nil {
return "", err
}
for i := 0; i < 8-len(bp); i++ {
bpv *= 10
}
v += bpv
}
return strconv.FormatInt(v, 10), nil
}
// Convert Factoid and Entry Credit addresses to their more user
// friendly and human readable formats.
//
// Creates the binary form. Just needs the conversion to base58
// for display.
func ConvertAddressToUser(prefix []byte, addr interfaces.IAddress) []byte {
dat := make([]byte, 0, 64)
dat = append(dat, prefix...)
dat = append(dat, addr.Bytes()...)
sha256d := Sha(Sha(dat).Bytes()).Bytes()
userd := prefix
userd = append(userd, addr.Bytes()...)
userd = append(userd, sha256d[:4]...)
return userd
}
// Convert Factoid Addresses
func ConvertFctAddressToUserStr(addr interfaces.IAddress) string {
//NOTE: This converts the final hash into user-readable string, NOT the public key!
//In practical terms, you'll need to convert the public key into RCD,
//then hash it before using this function!
userd := ConvertAddressToUser(FactoidPrefix, addr)
return base58.Encode(userd)
}
// Convert Factoid Private Key
func ConvertFctPrivateToUserStr(addr interfaces.IAddress) string {
userd := ConvertAddressToUser(FactoidPrivatePrefix, addr)
return base58.Encode(userd)
}
// Convert Entry Credits
func ConvertECAddressToUserStr(addr interfaces.IAddress) string {
userd := ConvertAddressToUser(EntryCreditPrefix, addr)
return base58.Encode(userd)
}
// Convert Entry Credit Private key
func ConvertECPrivateToUserStr(addr interfaces.IAddress) string {
userd := ConvertAddressToUser(EntryCreditPrivatePrefix, addr)
return base58.Encode(userd)
}
//
// Validates a User representation of a Factom and
// Entry Credit addresses.
//
// Returns false if the length is wrong.
// Returns false if the prefix is wrong.
// Returns false if the checksum is wrong.
//
func validateUserStr(prefix []byte, userFAddr string) bool {
if len(userFAddr) != 52 {
return false
}
v := base58.Decode(userFAddr)
if len(v) < 3 {
return false
}
if bytes.Compare(prefix, v[:2]) != 0 {
return false
}
sha256d := Sha(Sha(v[:34]).Bytes()).Bytes()
if bytes.Compare(sha256d[:4], v[34:]) != 0 {
return false
}
return true
}
// Validate Factoids
func ValidateFUserStr(userFAddr string) bool {
return validateUserStr(FactoidPrefix, userFAddr)
}
// Validate Factoid Private Key
func ValidateFPrivateUserStr(userFAddr string) bool {
return validateUserStr(FactoidPrivatePrefix, userFAddr)
}
// Validate Entry Credits
func ValidateECUserStr(userFAddr string) bool {
return validateUserStr(EntryCreditPrefix, userFAddr)
}
// Validate Entry Credit Private Key
func ValidateECPrivateUserStr(userFAddr string) bool {
return validateUserStr(EntryCreditPrivatePrefix, userFAddr)
}
// Convert a User facing Factoid or Entry Credit address
// or their Private Key representations
// to the regular form. Note validation must be done
// separately!
func ConvertUserStrToAddress(userFAddr string) []byte {
v := base58.Decode(userFAddr)
return v[2:34]
}