-
Notifications
You must be signed in to change notification settings - Fork 19
/
util.go
130 lines (109 loc) · 2.88 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
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom
import (
"bytes"
"crypto/sha256"
"crypto/sha512"
"encoding/binary"
"fmt"
"math"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
const (
// ZeroHash is the string of all 00s
ZeroHash = "0000000000000000000000000000000000000000000000000000000000000000"
OpenNode = "https://api.factomd.net"
)
var (
// RpcConfig sets the default target for the factomd and walletd API servers
RpcConfig = &RPCConfig{
FactomdServer: "localhost:8088",
WalletServer: "localhost:8089",
}
cookieJar http.CookieJar
)
// EntryCost calculates the cost in Entry Credits of adding an Entry to a Chain
// on the Factom protocol.
// The cost is the size of the Entry in Kilobytes excluding the Entry Header
// with any remainder being charged as a whole Kilobyte.
func EntryCost(e *Entry) (int8, error) {
p, err := e.MarshalBinary()
if err != nil {
return 0, err
}
// caulculate the length exluding the header size 35 for Milestone 1
l := len(p) - 35
if l > 10240 {
return 10, fmt.Errorf("Entry cannot be larger than 10KB")
}
// n is the capacity of the entry payment in KB
n := int8(l / 1024)
if r := l % 1024; r > 0 {
n++
}
// The Entry Cost should never be less than one
if n < 1 {
n = 1
}
return n, nil
}
// FactoshiToFactoid converts a uint64 factoshi ammount into a fixed point
// number represented as a string
func FactoshiToFactoid(i uint64) string {
d := i / 1e8
r := i % 1e8
ds := fmt.Sprintf("%d", d)
rs := fmt.Sprintf("%08d", r)
rs = strings.TrimRight(rs, "0")
if len(rs) > 0 {
ds = ds + "."
}
return fmt.Sprintf("%s%s", ds, rs)
}
// FactoidToFactoshi takes a Factoid amount as a string and returns the value in
// factoids
func FactoidToFactoshi(amt string) uint64 {
valid := regexp.MustCompile(`^([0-9]+)?(\.[0-9]+)?$`)
if !valid.MatchString(amt) {
return 0
}
var total uint64 = 0
dot := regexp.MustCompile(`\.`)
pieces := dot.Split(amt, 2)
whole, _ := strconv.Atoi(pieces[0])
total += uint64(whole) * 1e8
if len(pieces) > 1 {
a := regexp.MustCompile(`(0*)([0-9]+)$`)
as := a.FindStringSubmatch(pieces[1])
part, _ := strconv.Atoi(as[0])
power := len(as[1]) + len(as[2])
total += uint64(part * 1e8 / int(math.Pow10(power)))
}
return total
}
// milliTime returns a 6 byte slice representing the unix time in milliseconds
func milliTime() (r []byte) {
buf := new(bytes.Buffer)
t := time.Now().UnixNano()
m := t / 1e6
binary.Write(buf, binary.BigEndian, m)
return buf.Bytes()[2:]
}
// shad Double Sha256 Hash; sha256(sha256(data))
func shad(data []byte) []byte {
h1 := sha256.Sum256(data)
h2 := sha256.Sum256(h1[:])
return h2[:]
}
// sha52 Sha512+Sha256 Hash; sha256(sha512(data)+data)
func sha52(data []byte) []byte {
h1 := sha512.Sum512(data)
h2 := sha256.Sum256(append(h1[:], data...))
return h2[:]
}