-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathid.go
68 lines (55 loc) · 1.26 KB
/
id.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
package Stratum
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
)
// A stratum session id is assigned by the mining pool to a miner and it
// is included in the coinbase script of the block that is produced.
// we also use ID for job ids.
type ID uint32
func encodeBigEndian(id uint32) string {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(id))
return hex.EncodeToString(b)
}
func decodeBigEndian(s string) (uint32, error) {
b, err := hex.DecodeString(s)
if err != nil {
return 0, err
}
if len(b) != 4 {
return 0, errors.New("Invalid format")
}
var x uint32
binary.Read(bytes.NewBuffer(b), binary.BigEndian, &x)
return x, nil
}
func encodeLittleEndian(id uint32) string {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, uint32(id))
return hex.EncodeToString(b)
}
func decodeLittleEndian(s string) (uint32, error) {
b, err := hex.DecodeString(s)
if err != nil {
return 0, err
}
if len(b) != 4 {
return 0, errors.New("Invalid format")
}
var x uint32
binary.Read(bytes.NewBuffer(b), binary.LittleEndian, &x)
return x, nil
}
func encodeID(id ID) string {
return encodeBigEndian(uint32(id))
}
func decodeID(s string) (ID, error) {
x, err := decodeBigEndian(s)
if err != nil {
return 0, err
}
return ID(x), nil
}