-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx.go
168 lines (131 loc) · 3.69 KB
/
tx.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package alsdk
import (
"encoding/json"
"errors"
"fmt"
)
type StreamID string
type StreamIDWrapper map[StreamID]interface{}
type DataWrapper map[string]interface{}
type TransactionHandler interface {
Sign(Key KeyHandler, ID StreamID) error
SetInput(ID StreamID, Input map[string]interface{})
SetOutput(ID StreamID, Output map[string]interface{})
SetReadonly(ID StreamID, ReadOnly map[string]interface{})
GetTransaction() Transaction
}
type Transaction struct {
Territoriality string `json:"$territoriality,omitempty"`
Transaction TxBody `json:"$tx"`
SelfSign bool `json:"$selfsign"`
Signature StreamIDWrapper `json:"$sigs"`
}
type TxBody struct {
Namespace string `json:"$namespace"`
Contract string `json:"$contract"`
Entry string `json:"$entry,omitempty"`
Input StreamIDWrapper `json:"$i"`
Output StreamIDWrapper `json:"$o,omitempty"`
ReadOnly StreamIDWrapper `json:"$t,omitempty"`
}
type TransactionOpts struct {
StreamID StreamID
OutputStreamID StreamID
ReadOnlyStreamID StreamID
Key KeyHandler
Namespace string
Contract string
Entry string
SelfSign bool
Territoriality string
Input DataWrapper
Output DataWrapper
ReadOnly DataWrapper
}
var (
ErrNoStreamID error = errors.New("stream id not given")
)
func (t Transaction) GetTransaction() Transaction {
return t
}
// Sign - Sign a transaction
func (t *Transaction) Sign(key KeyHandler, id StreamID) error {
sigWrap := make(StreamIDWrapper)
toSign, err := json.Marshal(t.Transaction)
if err != nil {
return fmt.Errorf("marshaling transaction to JSON failed: %v", err)
}
signature, _, err := key.Sign(toSign)
if err != nil {
return err
}
sigWrap[id] = signature
t.Signature = sigWrap
return nil
}
// SetInput - Set the input data of the transaction
func (t *Transaction) SetInput(id StreamID, d map[string]interface{}) {
t.Transaction.Input[id] = d
}
// SetOutput - Set the output data of the transaction
func (t *Transaction) SetOutput(id StreamID, d map[string]interface{}) {
t.Transaction.Output[id] = d
}
// SetReadonly - Set the readonly data of a transaction
func (t *Transaction) SetReadonly(id StreamID, d map[string]interface{}) {
t.Transaction.ReadOnly[id] = d
}
// BuildTransaction - Pass all required data and create a transaction from it
func BuildTransaction(o TransactionOpts) (Tx TransactionHandler, Hash []byte, TXError error) {
if o.StreamID == "" {
return &Transaction{}, []byte{}, ErrNoStreamID
}
t := Transaction{}
txBody := TxBody{
Namespace: o.Namespace,
Contract: o.Contract,
}
if o.Entry != "" {
txBody.Entry = o.Entry
}
if o.SelfSign {
t.SelfSign = true
}
if o.Territoriality != "" {
t.Territoriality = o.Territoriality
}
if o.Input != nil {
txBody.Input = make(StreamIDWrapper)
txBody.Input[o.StreamID] = o.Input
}
if o.Output != nil {
txBody.Output = make(StreamIDWrapper)
// If OutputStreamID not provided assume StreamID
outStreamId := o.StreamID
if o.OutputStreamID != "" {
outStreamId = o.OutputStreamID
}
txBody.Output[outStreamId] = o.Output
}
if o.ReadOnly != nil {
txBody.ReadOnly = make(StreamIDWrapper)
readStreamId := o.StreamID
if o.ReadOnlyStreamID != "" {
readStreamId = o.ReadOnlyStreamID
}
txBody.ReadOnly[readStreamId] = o.ReadOnly
}
t.Transaction = txBody
toSign, err := json.Marshal(t.Transaction)
if err != nil {
return &Transaction{}, []byte{}, nil
}
sig, hash, err := o.Key.Sign(toSign)
if err != nil {
return &Transaction{}, []byte{}, nil
}
sigWrap := make(StreamIDWrapper)
sigWrap[o.StreamID] = sig
t.Signature = sigWrap
return &t, hash, nil
}