forked from iotaledger/iota.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
210 lines (184 loc) · 9.08 KB
/
transaction.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
MIT License
Copyright (c) 2016 Sascha Hanse
Copyright (c) 2017 Shinya Yagyu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package giota
import (
"encoding/json"
"errors"
"time"
)
// Transaction contains all info needed for an iota transaction
type Transaction struct {
SignatureMessageFragment Trytes
Address Address
Value int64 `json:",string"`
ObsoleteTag Trytes
Timestamp time.Time `json:",string"`
CurrentIndex int64 `json:",string"`
LastIndex int64 `json:",string"`
Bundle Trytes
TrunkTransaction Trytes
BranchTransaction Trytes
Tag Trytes
AttachmentTimestamp Trytes
AttachmentTimestampLowerBound Trytes
AttachmentTimestampUpperBound Trytes
Nonce Trytes
}
// errors for tx
var (
ErrInvalidTransactionType = errors.New("invalid transaction type")
ErrInvalidTransactionHash = errors.New("invalid transaction hash")
ErrInvalidTransaction = errors.New("malformed transaction")
)
// Trinary sizes and offsets of a transaction
const (
SignatureMessageFragmentTrinaryOffset = 0
SignatureMessageFragmentTrinarySize = 6561
AddressTrinaryOffset = SignatureMessageFragmentTrinaryOffset + SignatureMessageFragmentTrinarySize
AddressTrinarySize = 243
ValueTrinaryOffset = AddressTrinaryOffset + AddressTrinarySize
ValueTrinarySize = 81
ObsoleteTagTrinaryOffset = ValueTrinaryOffset + ValueTrinarySize
ObsoleteTagTrinarySize = 81
TimestampTrinaryOffset = ObsoleteTagTrinaryOffset + ObsoleteTagTrinarySize
TimestampTrinarySize = 27
CurrentIndexTrinaryOffset = TimestampTrinaryOffset + TimestampTrinarySize
CurrentIndexTrinarySize = 27
LastIndexTrinaryOffset = CurrentIndexTrinaryOffset + CurrentIndexTrinarySize
LastIndexTrinarySize = 27
BundleTrinaryOffset = LastIndexTrinaryOffset + LastIndexTrinarySize
BundleTrinarySize = 243
TrunkTransactionTrinaryOffset = BundleTrinaryOffset + BundleTrinarySize
TrunkTransactionTrinarySize = 243
BranchTransactionTrinaryOffset = TrunkTransactionTrinaryOffset + TrunkTransactionTrinarySize
BranchTransactionTrinarySize = 243
TagTrinaryOffset = BranchTransactionTrinaryOffset + BranchTransactionTrinarySize
TagTrinarySize = 81
AttachmentTimestampTrinaryOffset = TagTrinaryOffset + TagTrinarySize
AttachmentTimestampTrinarySize = 27
AttachmentTimestampLowerBoundTrinaryOffset = AttachmentTimestampTrinaryOffset + AttachmentTimestampTrinarySize
AttachmentTimestampLowerBoundTrinarySize = 27
AttachmentTimestampUpperBoundTrinaryOffset = AttachmentTimestampLowerBoundTrinaryOffset + AttachmentTimestampLowerBoundTrinarySize
AttachmentTimestampUpperBoundTrinarySize = 27
NonceTrinaryOffset = AttachmentTimestampUpperBoundTrinaryOffset + AttachmentTimestampUpperBoundTrinarySize
NonceTrinarySize = 81
transactionTrinarySize = SignatureMessageFragmentTrinarySize + AddressTrinarySize +
ValueTrinarySize + ObsoleteTagTrinarySize + TimestampTrinarySize +
CurrentIndexTrinarySize + LastIndexTrinarySize + BundleTrinarySize +
TrunkTransactionTrinarySize + BranchTransactionTrinarySize +
TagTrinarySize + AttachmentTimestampTrinarySize +
AttachmentTimestampLowerBoundTrinarySize + AttachmentTimestampUpperBoundTrinarySize +
NonceTrinarySize
)
// NewTransaction makes tx from trits.
func NewTransaction(trytes Trytes) (*Transaction, error) {
t := Transaction{}
if err := checkTx(trytes); err != nil {
return nil, err
}
err := t.parser(trytes.Trits())
if err != nil {
return nil, err
}
return &t, nil
}
func checkTx(trytes Trytes) error {
err := trytes.IsValid()
switch {
case err != nil:
return errors.New("invalid transaction " + err.Error())
case len(trytes) != transactionTrinarySize/3:
return errors.New("invalid trits counts in transaction")
case trytes[2279:2295] != "9999999999999999":
return errors.New("invalid value in transaction")
default:
return nil
}
}
func (t *Transaction) parser(trits Trits) error {
var err error
t.SignatureMessageFragment = trits[SignatureMessageFragmentTrinaryOffset:SignatureMessageFragmentTrinarySize].Trytes()
t.Address, err = trits[AddressTrinaryOffset : AddressTrinaryOffset+AddressTrinarySize].Trytes().ToAddress()
if err != nil {
return err
}
t.Value = trits[ValueTrinaryOffset : ValueTrinaryOffset+ValueTrinarySize].Int()
t.ObsoleteTag = trits[ObsoleteTagTrinaryOffset : ObsoleteTagTrinaryOffset+ObsoleteTagTrinarySize].Trytes()
timestamp := trits[TimestampTrinaryOffset : TimestampTrinaryOffset+TimestampTrinarySize].Int()
t.Timestamp = time.Unix(timestamp, 0)
t.CurrentIndex = trits[CurrentIndexTrinaryOffset : CurrentIndexTrinaryOffset+CurrentIndexTrinarySize].Int()
t.LastIndex = trits[LastIndexTrinaryOffset : LastIndexTrinaryOffset+LastIndexTrinarySize].Int()
t.Bundle = trits[BundleTrinaryOffset : BundleTrinaryOffset+BundleTrinarySize].Trytes()
t.TrunkTransaction = trits[TrunkTransactionTrinaryOffset : TrunkTransactionTrinaryOffset+TrunkTransactionTrinarySize].Trytes()
t.BranchTransaction = trits[BranchTransactionTrinaryOffset : BranchTransactionTrinaryOffset+BranchTransactionTrinarySize].Trytes()
t.Tag = trits[TagTrinaryOffset : TagTrinaryOffset+TagTrinarySize].Trytes()
t.AttachmentTimestamp = trits[AttachmentTimestampTrinaryOffset : AttachmentTimestampTrinaryOffset+AttachmentTimestampTrinarySize].Trytes()
t.AttachmentTimestampLowerBound = trits[AttachmentTimestampLowerBoundTrinaryOffset : AttachmentTimestampLowerBoundTrinaryOffset+AttachmentTimestampLowerBoundTrinarySize].Trytes()
t.AttachmentTimestampUpperBound = trits[AttachmentTimestampUpperBoundTrinaryOffset : AttachmentTimestampUpperBoundTrinaryOffset+AttachmentTimestampUpperBoundTrinarySize].Trytes()
t.Nonce = trits[NonceTrinaryOffset : NonceTrinaryOffset+NonceTrinarySize].Trytes()
return nil
}
// Trytes converts the transaction to Trytes.
func (t *Transaction) Trytes() Trytes {
tr := make(Trits, transactionTrinarySize)
copy(tr, t.SignatureMessageFragment.Trits())
copy(tr[AddressTrinaryOffset:], Trytes(t.Address).Trits())
copy(tr[ValueTrinaryOffset:], Int2Trits(t.Value, ValueTrinarySize))
copy(tr[ObsoleteTagTrinaryOffset:], t.ObsoleteTag.Trits())
copy(tr[TimestampTrinaryOffset:], Int2Trits(t.Timestamp.Unix(), TimestampTrinarySize))
copy(tr[CurrentIndexTrinaryOffset:], Int2Trits(t.CurrentIndex, CurrentIndexTrinarySize))
copy(tr[LastIndexTrinaryOffset:], Int2Trits(t.LastIndex, LastIndexTrinarySize))
copy(tr[BundleTrinaryOffset:], t.Bundle.Trits())
copy(tr[TrunkTransactionTrinaryOffset:], t.TrunkTransaction.Trits())
copy(tr[BranchTransactionTrinaryOffset:], t.BranchTransaction.Trits())
copy(tr[TagTrinaryOffset:], t.Tag.Trits())
copy(tr[AttachmentTimestampTrinaryOffset:], t.AttachmentTimestamp.Trits())
copy(tr[AttachmentTimestampLowerBoundTrinaryOffset:], t.AttachmentTimestampLowerBound.Trits())
copy(tr[AttachmentTimestampUpperBoundTrinaryOffset:], t.AttachmentTimestampUpperBound.Trits())
copy(tr[NonceTrinaryOffset:], t.Nonce.Trits())
return tr.Trytes()
}
// HasValidNonce checks if the transaction has the valid MinWeightMagnitude.
// In order to check the MWM we count trailing 0's of the curlp hash of a
// transaction.
func (t *Transaction) HasValidNonce(mwm int64) bool {
return t.Hash().Trits().TrailingZeros() >= mwm
}
// Hash returns the hash of the transaction.
func (t *Transaction) Hash() Trytes {
return t.Trytes().Hash()
}
// UnmarshalJSON makes transaction struct from json.
func (t *Transaction) UnmarshalJSON(b []byte) error {
var s Trytes
var err error
if err = json.Unmarshal(b, &s); err != nil {
return err
}
if err = checkTx(s); err != nil {
return err
}
return t.parser(s.Trits())
}
// MarshalJSON makes trytes ([]byte) from a transaction.
func (t *Transaction) MarshalJSON() ([]byte, error) {
return []byte(`"` + t.Trytes() + `"`), nil
}