-
Notifications
You must be signed in to change notification settings - Fork 0
/
txn.go
executable file
·173 lines (139 loc) · 4.28 KB
/
txn.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
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"github.com/herumi/bls-eth-go-binary/bls"
)
const (
TxnRaw_LONG = 0
TxnRaw_SHORT = 1
)
const TxnRaw_ADJUST_SRC_ID = 1000000
const TxnRaw_ADJUST_DST_ID = 1000000
const TxnRaw_ADJUST_AMOUNT = 1000000
const BlockVerMT_NUM_AGG_SIGNITURES = 8
type TxnRaw struct {
src_id int64
src_nonce int64
amount int64
fee int64 // compression? ...
//union? ...
dst_pubKey BLSPubKey
dst_id int64
dst_type uint8
}
func (txn *TxnRaw) InitTxnRaw(src_id int64, src_nonce int64, amount int64, fee int64, dst_type uint8) {
txn.src_id = src_id + TxnRaw_ADJUST_SRC_ID
txn.src_nonce = src_nonce
txn.amount = amount + TxnRaw_ADJUST_AMOUNT
txn.fee = fee
txn.dst_type = dst_type
}
func (txn *TxnRaw) InitTxnRawLong(src_id int64, src_nonce int64, amount int64, fee int64, dst_pubKey *BLSPubKey) {
txn.InitTxnRaw(src_id, src_nonce, amount, fee, TxnRaw_LONG)
txn.dst_pubKey = *dst_pubKey
}
func (txn *TxnRaw) InitTxnRawShort(src_id int64, src_nonce int64, amount int64, fee int64, dst_id int64) {
txn.InitTxnRaw(src_id, src_nonce, amount, fee, TxnRaw_SHORT)
txn.dst_id = dst_id + TxnRaw_ADJUST_DST_ID
}
func (txn *TxnRaw) InitTxnFromBuffer(buff *TBuffer, readPubKey bool, needSign bool) ([]byte, *bls.PublicKey, *bls.Sign, error) {
var pk bls.PublicKey
if readPubKey {
pubKey := BLSPubKey{}
err := buff.ReadSBlob(pubKey.arr[:], int64(len(pubKey.arr)))
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
err = pubKey.Export(&pk)
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() PubKey export failed: %w", err)
}
}
pos_backup := buff.pos
var err error
txn.src_id, err = buff.ReadNumber()
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
txn.src_nonce, err = buff.ReadNumber()
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
txn.amount, err = buff.ReadNumber()
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
txn.fee, err = buff.ReadNumber()
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
txn.dst_type, err = buff.ReadByte()
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
txn.src_id -= TxnRaw_ADJUST_SRC_ID
txn.amount -= TxnRaw_ADJUST_AMOUNT
if txn.dst_type == 0 {
err := buff.ReadSBlob(txn.dst_pubKey.arr[:], int64(len(txn.dst_pubKey.arr)))
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
} else {
txn.dst_id, err = buff.ReadNumber()
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
txn.dst_id -= TxnRaw_ADJUST_DST_ID
}
ret := buff.data[pos_backup:buff.pos]
var sig bls.Sign
if needSign {
var s BLSSign
err := buff.ReadSBlob(s.arr[:], int64(len(s.arr)))
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
err = s.Export(&sig)
if err != nil {
return nil, nil, nil, fmt.Errorf("InitTxnFromBuffer() failed: %w", err)
}
}
return ret, &pk, &sig, nil
}
func (txn *TxnRaw) ExportBuffer(pubKey *BLSPubKey, key *bls.SecretKey, buff *TBuffer) error {
buff.Clear()
if pubKey != nil {
buff.WriteSBlob(pubKey.arr[:])
}
buff.WriteNumber(txn.src_id)
buff.WriteNumber(txn.src_nonce)
buff.WriteNumber(txn.amount)
buff.WriteNumber(txn.fee)
buff.WriteUint8(txn.dst_type)
if txn.dst_type == 0 {
buff.WriteSBlob(txn.dst_pubKey.arr[:])
} else {
buff.WriteNumber(txn.dst_id)
}
{
h, err := buff.sha256(len(pubKey.arr))
if err != nil {
return fmt.Errorf("ExportBuffer() failed: %w", err)
}
s := NewBLSSign(key.SignByte(h)) // SLOW
buff.WriteSBlob(s.arr[:])
}
return nil
}