forked from VIZ-Blockchain/viz-go-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trx.go
108 lines (89 loc) · 2.5 KB
/
trx.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
package viz
import (
"time"
"github.com/VIZ-Blockchain/viz-go-lib/api"
"github.com/VIZ-Blockchain/viz-go-lib/operations"
"github.com/VIZ-Blockchain/viz-go-lib/transactions"
"github.com/VIZ-Blockchain/viz-go-lib/types"
)
//SendTrx generates and sends an array of transactions to VIZ.
func (client *Client) SendTrx(username string, strx []operations.Operation) (*types.OperationResponse, error) {
var bresp types.OperationResponse
// Getting the necessary parameters
props, err := client.API.GetDynamicGlobalProperties()
if err != nil {
return nil, err
}
// Creating a Transaction
refBlockPrefix, err := transactions.RefBlockPrefix(props.HeadBlockID)
if err != nil {
return nil, err
}
tx := transactions.NewSignedTransaction(&operations.Transaction{
RefBlockNum: transactions.RefBlockNum(props.HeadBlockNumber),
RefBlockPrefix: refBlockPrefix,
})
// Adding Operations to a Transaction
for _, val := range strx {
tx.PushOperation(val)
}
expTime := time.Now().Add(59 * time.Minute).UTC()
tm := types.Time{
Time: &expTime,
}
tx.Expiration = &tm
// Obtain the key required for signing
privKeys, err := client.SigningKeys(strx[0])
if err != nil {
return nil, err
}
// Sign the transaction
if err := tx.Sign(privKeys, client.Config.ChainID); err != nil {
return nil, err
}
// Sending a transaction
var resp *api.BroadcastResponse
if client.asyncProtocol {
err = client.API.BroadcastTransaction(tx.Transaction)
} else {
resp, err = client.API.BroadcastTransactionSynchronous(tx.Transaction)
}
bresp.JSONTrx, _ = JSONTrxString(tx)
if err != nil {
return &bresp, err
}
if resp != nil && !client.asyncProtocol {
bresp.ID = resp.ID
bresp.BlockNum = resp.BlockNum
bresp.TrxNum = resp.TrxNum
bresp.Expired = resp.Expired
return &bresp, nil
}
return &bresp, nil
}
func (client *Client) GetTrx(strx []operations.Operation) (*operations.Transaction, error) {
// Getting the necessary parameters
props, err := client.API.GetDynamicGlobalProperties()
if err != nil {
return nil, err
}
// Creating a Transaction
refBlockPrefix, err := transactions.RefBlockPrefix(props.HeadBlockID)
if err != nil {
return nil, err
}
tx := &operations.Transaction{
RefBlockNum: transactions.RefBlockNum(props.HeadBlockNumber),
RefBlockPrefix: refBlockPrefix,
}
// Adding Operations to a Transaction
for _, val := range strx {
tx.PushOperation(val)
}
expTime := time.Now().Add(59 * time.Minute).UTC()
tm := types.Time{
Time: &expTime,
}
tx.Expiration = &tm
return tx, nil
}