-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
49 lines (42 loc) · 1.07 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
package monerorpc
import (
"crypto/rand"
"encoding/hex"
"fmt"
"strconv"
)
// NewPaymentID64 generates a 64 bit payment ID
func NewPaymentID64() string {
buf := make([]byte, 8)
_, _ = rand.Read(buf)
return hex.EncodeToString(buf)
}
// NewPaymentID256 generates a 256 bit payment ID (hex encoded).
func NewPaymentID256() string {
buf := make([]byte, 32)
_, _ = rand.Read(buf)
return hex.EncodeToString(buf)
}
// XMRToDecimal converts a raw atomic XMR balance to a more
// human readable format.
func XMRToDecimal(xmr uint64) string {
str0 := fmt.Sprintf("%013d", xmr)
l := len(str0)
return str0[:l-12] + "." + str0[l-12:]
}
// XMRToFloat64 converts raw atomic XMR to a float64
func XMRToFloat64(xmr uint64) float64 {
return float64(xmr) / 1e12
}
// Float64ToXMR converts a float64 to a raw atomic XMR
func Float64ToXMR(xmr float64) uint64 {
return uint64(xmr * 1e12)
}
// StringToXMR converts a string to a raw atomic XMR
func StringToXMR(xmr string) (uint64, error) {
f, err := strconv.ParseFloat(xmr, 64)
if err != nil {
return 0, err
}
return uint64(f * 1e12), nil
}