forked from DeathwingTheBoss/hivego
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbroadcaster.go
132 lines (108 loc) · 2.81 KB
/
broadcaster.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
package hivego
import (
"encoding/hex"
"fmt"
"github.com/decred/dcrd/dcrec/secp256k1/v2"
)
type HiveTransaction struct {
RefBlockNum uint16 `json:"ref_block_num"`
RefBlockPrefix uint32 `json:"ref_block_prefix"`
Expiration string `json:"expiration"`
Operations []HiveOperation `json:"-"`
OperationsJs [][2]interface{} `json:"operations"`
Extensions []string `json:"extensions"`
Signatures []string `json:"signatures"`
}
func (t *HiveTransaction) GenerateTrxId() (string, error) {
tB, err := SerializeTx(*t)
if err != nil {
return "", err
}
digest := HashTx(tB)
return hex.EncodeToString(digest)[0:40], nil
}
func (t *HiveTransaction) Sign(keyPair KeyPair) (string, error) {
message, err := SerializeTx(*t)
if err != nil {
return "", err
}
digest := HashTxForSig(message)
sig, err := secp256k1.SignCompact(keyPair.PrivateKey, digest, true)
if err != nil {
return "", err
}
return hex.EncodeToString(sig), nil
}
func (t *HiveTransaction) AddSig(sig string) {
t.Signatures = append(t.Signatures, sig)
}
func (t *HiveTransaction) prepareJson() {
var opsContainer [][2]interface{}
for _, op := range t.Operations {
var opContainer [2]interface{}
opContainer[0] = op.OpName()
opContainer[1] = op
opsContainer = append(opsContainer, opContainer)
}
if t.Extensions == nil {
t.Extensions = []string{}
}
t.OperationsJs = opsContainer
}
func (h *HiveRpcNode) Broadcast(ops []HiveOperation, wif *string) (string, error) {
signingData, err := h.GetSigningData()
if err != nil {
return "", err
}
tx := HiveTransaction{
RefBlockNum: signingData.refBlockNum,
RefBlockPrefix: signingData.refBlockPrefix,
Expiration: signingData.expiration,
Operations: ops,
}
message, err := SerializeTx(tx)
if err != nil {
return "", err
}
digest := HashTxForSig(message)
txId, err := tx.GenerateTrxId()
if err != nil {
return "", err
}
sig, err := SignDigest(digest, wif)
if err != nil {
return "", err
}
tx.Signatures = append(tx.Signatures, hex.EncodeToString(sig))
tx.prepareJson()
var params []interface{}
params = append(params, tx)
if !h.NoBroadcast {
q := hrpcQuery{"condenser_api.broadcast_transaction", params}
res, err := h.rpcExec(h.address, q)
if err != nil {
return string(res), err
}
}
return txId, nil
}
func (h *HiveRpcNode) BroadcastRaw(tx HiveTransaction) (string, error) {
if len(tx.Signatures) == 0 {
return "", fmt.Errorf("transaction is not signed")
}
tx.prepareJson()
var params []interface{}
params = append(params, tx)
if !h.NoBroadcast {
q := hrpcQuery{"condenser_api.broadcast_transaction", params}
res, err := h.rpcExec(h.address, q)
if err != nil {
return string(res), err
}
}
txId, err := tx.GenerateTrxId()
if err != nil {
return "", err
}
return txId, nil
}