forked from perlin-network/wavelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
204 lines (152 loc) · 5.25 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
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
// Copyright (c) 2019 Perlin
//
// 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 wavelet
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/perlin-network/noise/edwards25519"
"github.com/perlin-network/noise/skademlia"
"github.com/perlin-network/wavelet/sys"
"github.com/pkg/errors"
"golang.org/x/crypto/blake2b"
"io"
)
type Transaction struct {
Sender AccountID // Transaction sender.
Nonce uint64
Block uint64
Tag sys.Tag
Payload []byte
Signature Signature
ID TransactionID // BLAKE2b(*).
}
func NewTransaction(sender *skademlia.Keypair, nonce, block uint64, tag sys.Tag, payload []byte) Transaction {
var nonceBuf [8]byte
binary.BigEndian.PutUint64(nonceBuf[:], nonce)
var blockBuf [8]byte
binary.BigEndian.PutUint64(blockBuf[:], block)
signature := edwards25519.Sign(
sender.PrivateKey(), append(nonceBuf[:], append(blockBuf[:], append([]byte{byte(tag)}, payload...)...)...),
)
return NewSignedTransaction(sender.PublicKey(), nonce, block, tag, payload, signature)
}
func NewSignedTransaction(
sender edwards25519.PublicKey, nonce, block uint64, tag sys.Tag, payload []byte, signature edwards25519.Signature,
) Transaction {
tx := Transaction{Sender: sender, Nonce: nonce, Block: block, Tag: tag, Payload: payload, Signature: signature}
tx.ID = blake2b.Sum256(tx.Marshal())
return tx
}
func (tx Transaction) Marshal() []byte {
w := bytes.NewBuffer(make([]byte, 0, 32+8+8+1+4+len(tx.Payload)+64))
w.Write(tx.Sender[:])
var buf [8]byte
binary.BigEndian.PutUint64(buf[:8], tx.Nonce)
w.Write(buf[:8])
binary.BigEndian.PutUint64(buf[:8], tx.Block)
w.Write(buf[:8])
w.WriteByte(byte(tx.Tag))
binary.BigEndian.PutUint32(buf[:4], uint32(len(tx.Payload)))
w.Write(buf[:4])
w.Write(tx.Payload)
w.Write(tx.Signature[:])
return w.Bytes()
}
func UnmarshalTransaction(r io.Reader) (t Transaction, err error) {
if _, err = io.ReadFull(r, t.Sender[:]); err != nil {
err = errors.Wrap(err, "failed to decode transaction sender")
return
}
var buf [8]byte
if _, err = io.ReadFull(r, buf[:8]); err != nil {
err = errors.Wrap(err, "failed to read nonce")
return
}
t.Nonce = binary.BigEndian.Uint64(buf[:8])
if _, err = io.ReadFull(r, buf[:8]); err != nil {
err = errors.Wrap(err, "failed to read block index")
return
}
t.Block = binary.BigEndian.Uint64(buf[:8])
if _, err = io.ReadFull(r, buf[:1]); err != nil {
err = errors.Wrap(err, "failed to read tag")
return
}
t.Tag = sys.Tag(buf[0])
if t.Tag < sys.TagTransfer || t.Tag > sys.TagBatch {
err = errors.Wrapf(err, "got an unknown tag %d", t.Tag)
return
}
if _, err = io.ReadFull(r, buf[:4]); err != nil {
err = errors.Wrap(err, "could not read transaction payload length")
return
}
t.Payload = make([]byte, binary.BigEndian.Uint32(buf[:4]))
if _, err = io.ReadFull(r, t.Payload); err != nil {
err = errors.Wrap(err, "could not read transaction payload")
return
}
if _, err = io.ReadFull(r, t.Signature[:]); err != nil {
err = errors.Wrap(err, "failed to decode signature")
return
}
t.ID = blake2b.Sum256(t.Marshal())
return t, nil
}
func (tx Transaction) ComputeIndex(id BlockID) []byte {
idx := blake2b.Sum256(append(tx.ID[:], id[:]...))
return idx[:]
}
func (tx Transaction) Fee() uint64 {
fee := uint64(sys.TransactionFeeMultiplier * float64(len(tx.Payload)))
if fee < sys.DefaultTransactionFee {
return sys.DefaultTransactionFee
}
return fee
}
// LogicalUnits counts the total number of atomic logical units of changes
// the specified tx comprises of.
func (tx Transaction) LogicalUnits() int {
if tx.Tag != sys.TagBatch {
return 1
}
var buf [1]byte
if _, err := io.ReadFull(bytes.NewReader(tx.Payload), buf[:1]); err != nil {
return 1
}
return int(buf[0])
}
func (tx Transaction) String() string {
return fmt.Sprintf("Transaction{ID: %x}", tx.ID)
}
func (tx Transaction) VerifySignature() bool {
var (
nonceBuf [8]byte
blockBuf [8]byte
)
binary.BigEndian.PutUint64(nonceBuf[:], tx.Nonce)
binary.BigEndian.PutUint64(blockBuf[:], tx.Block)
message := make([]byte, 0, 8+8+1+len(tx.Payload))
message = append(message, nonceBuf[:]...)
message = append(message, blockBuf[:]...)
message = append(message, byte(tx.Tag))
message = append(message, tx.Payload...)
return edwards25519.Verify(tx.Sender, message, tx.Signature)
}