-
Notifications
You must be signed in to change notification settings - Fork 9
/
signer.go
104 lines (86 loc) · 2.41 KB
/
signer.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
package hivego
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"time"
"github.com/decred/base58"
"github.com/decred/dcrd/dcrec/secp256k1/v2"
)
type signingDataFromChain struct {
refBlockNum uint16
refBlockPrefix uint32
expiration string
}
func (h *HiveRpcNode) getSigningData() (signingDataFromChain, error) {
propsB, err := h.GetDynamicGlobalProps()
if err != nil {
return signingDataFromChain{}, err
}
var props globalProps
err = json.Unmarshal(propsB, &props)
if err != nil {
return signingDataFromChain{}, err
}
refBlockNum := uint16(props.HeadBlockNumber & 0xffff)
hbidB, err := hex.DecodeString(props.HeadBlockId)
if err != nil {
return signingDataFromChain{}, err
}
refBlockPrefix := binary.LittleEndian.Uint32(hbidB[4:])
exp, err := time.Parse("2006-01-02T15:04:05", props.Time)
if err != nil {
return signingDataFromChain{}, err
}
exp = exp.Add(30 * time.Second)
expStr := exp.Format("2006-01-02T15:04:05")
signingData := signingDataFromChain{refBlockNum, refBlockPrefix, expStr}
return signingData, nil
}
func hashTxForSig(tx []byte) []byte {
var message bytes.Buffer
message.Write(getHiveChainId())
message.Write(tx)
digest := sha256.New()
digest.Write(message.Bytes())
return digest.Sum(nil)
}
func hashTx(tx []byte) []byte {
var message bytes.Buffer
message.Write(tx)
digest := sha256.New()
digest.Write(message.Bytes())
return digest.Sum(nil)
}
func SignDigest(digest []byte, wif *string) ([]byte, error) {
keyPair, err := KeyPairFromWif(*wif)
if err != nil {
return nil, err
}
return secp256k1.SignCompact(keyPair.PrivateKey, digest, true)
}
func GphBase58CheckDecode(input string) ([]byte, [1]byte, error) {
decoded := base58.Decode(input)
if len(decoded) < 6 {
return nil, [1]byte{0}, errors.New("invalid format: version and/or checksum bytes missing")
}
version := [1]byte{decoded[0]}
dataLen := len(decoded) - 4
decodedChecksum := decoded[dataLen:]
calculatedChecksum := checksum(decoded[:dataLen])
if !bytes.Equal(decodedChecksum, calculatedChecksum[:]) {
return nil, [1]byte{0}, errors.New("checksum error")
}
payload := decoded[1:dataLen]
return payload, version, nil
}
func checksum(input []byte) [4]byte {
var calculatedChecksum [4]byte
intermediateHash := sha256.Sum256(input)
finalHash := sha256.Sum256(intermediateHash[:])
copy(calculatedChecksum[:], finalHash[:])
return calculatedChecksum
}