-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqubic.go
229 lines (181 loc) · 6.24 KB
/
qubic.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package qubic
import (
"context"
"encoding/binary"
"github.com/0xluk/go-qubic/data/identity"
"github.com/0xluk/go-qubic/data/tick"
"github.com/0xluk/go-qubic/data/tx"
"github.com/0xluk/go-qubic/foundation/tcp"
"github.com/cloudflare/circl/xof/k12"
"github.com/pkg/errors"
)
type Client struct {
Qc *tcp.QubicConnection
}
func NewClient(ctx context.Context, nodeIP, nodePort string) (*Client, error) {
qc, err := tcp.NewQubicConnection(ctx, nodeIP, nodePort)
if err != nil {
return nil, errors.Wrap(err, "creating qubic connection")
}
return &Client{Qc: qc}, nil
}
func GetIdentity(ctx context.Context, qc *tcp.QubicConnection, id string) (identity.GetIdentityResponse, error) {
type requestPacket struct {
PublicKey [32]byte
}
request := requestPacket{PublicKey: getPublicKeyFromIdentity(id)}
var result identity.GetIdentityResponse
err := tcp.SendGenericRequest(ctx, qc, identity.RequestBalanceType, identity.RespondBalanceType, request, &result)
if err != nil {
return identity.GetIdentityResponse{}, errors.Wrap(err, "sending req to node")
}
return result, nil
}
func GetTickInfo(ctx context.Context, qc *tcp.QubicConnection) (tick.CurrentTickInfo, error) {
var result tick.CurrentTickInfo
err := tcp.SendGenericRequest(ctx, qc, tick.REQUEST_CURRENT_TICK_INFO, tick.RESPOND_CURRENT_TICK_INFO, nil, &result)
if err != nil {
return tick.CurrentTickInfo{}, errors.Wrap(err, "sending req to node")
}
return result, nil
}
func GetTxStatus(ctx context.Context, qc *tcp.QubicConnection, tick uint32, digest [32]byte, sig [64]byte) (tx.ResponseTxStatus, error) {
request := tx.RequestTxStatus{
Tick: tick,
Digest: digest,
Signature: sig,
}
var result tx.ResponseTxStatus
err := tcp.SendGenericRequest(ctx, qc, tx.REQUEST_TX_STATUS, tx.RESPONSE_TX_STATUS, request, &result)
if err != nil {
return tx.ResponseTxStatus{}, errors.Wrap(err, "sending generic req")
}
return result, nil
}
func GetTickTransactions(ctx context.Context, qc *tcp.QubicConnection, tickNumber uint32) ([]tick.Transaction, error) {
tickData, err := GetTickData(ctx, qc, tickNumber)
var nrTx int
for _, digest := range tickData.TransactionDigests {
if digest == [32]byte{} {
continue
}
nrTx++
}
requestTickTransactions := tick.RequestTickTransactions{Tick: tickNumber}
for i := 0; i < (nrTx+7)/8; i++ {
requestTickTransactions.TransactionFlags[i] = 0
}
for i := (nrTx + 7) / 8; i < tick.NUMBER_OF_TRANSACTIONS_PER_TICK/8; i++ {
requestTickTransactions.TransactionFlags[i] = 1
}
txs, err := tcp.SendGetTransactionsRequest(ctx, qc, tick.REQUEST_TICK_TRANSACTIONS, tick.BROADCAST_TRANSACTION, requestTickTransactions, nrTx)
if err != nil {
return nil, errors.Wrap(err, "sending transaction req")
}
transactions := make([]tick.Transaction, 0, len(txs))
for _, txData := range txs {
hash, err := getHashFromTxData(txData)
if err != nil {
return nil, errors.Wrapf(err, "getting hash from tx data: %+v", txData)
}
transactions = append(transactions, tick.Transaction{Data: txData, Hash: hash})
}
return transactions, nil
}
func GetTickData(ctx context.Context, qc *tcp.QubicConnection, tickNumber uint32) (tick.TickData, error) {
tickInfo, err := GetTickInfo(ctx, qc)
if err != nil {
return tick.TickData{}, errors.Wrap(err, "getting tick info")
}
if tickInfo.Tick < tickNumber {
return tick.TickData{}, errors.Errorf("Requested tick %d is in the future. Latest tick is: %d", tickNumber, tickInfo.Tick)
}
request := tick.RequestTickData{Tick: tickNumber}
var result tick.TickData
err = tcp.SendGenericRequest(ctx, qc, tick.REQUEST_TICK_DATA, tick.BROADCAST_FUTURE_TICK_DATA, request, &result)
if err != nil {
return tick.TickData{}, errors.Wrap(err, "sending req to node")
}
return result, nil
}
func SendRawTransaction(ctx context.Context, qc *tcp.QubicConnection, rawTx []byte) error {
err := tcp.SendTransaction(ctx, qc, tick.BROADCAST_TRANSACTION, 0, rawTx, nil)
if err != nil {
return errors.Wrap(err, "sending req")
}
return nil
}
func (c Client) Close() error {
if c.Qc != nil {
return c.Qc.Close()
}
return nil
}
func getPublicKeyFromIdentity(identity string) [32]byte {
publicKeyBuffer := make([]byte, 32)
for i := 0; i < 4; i++ {
value := uint64(0)
for j := 13; j >= 0; j-- {
if identity[i*14+j] < 'A' || identity[i*14+j] > 'Z' {
return [32]byte{} // Error condition: invalid character in identity
}
value = value*26 + uint64(identity[i*14+j]-'A')
}
// Copy the 8-byte value into publicKeyBuffer
for k := 0; k < 8; k++ {
publicKeyBuffer[i*8+k] = byte(value >> (k * 8))
}
}
var pubKey [32]byte
copy(pubKey[:], publicKeyBuffer[:32])
return pubKey
}
func getHashFromTxData(txData tick.TransactionData) (tick.TransactionHash, error) {
txDataMarshalledBytes, err := txData.MarshallBinary()
if err != nil {
return tick.TransactionHash{}, errors.Wrap(err, "marshalling")
}
h := k12.NewDraft10([]byte{})
_, err = h.Write(txDataMarshalledBytes)
if err != nil {
return tick.TransactionHash{}, errors.Wrap(err, "writing msg to k12")
}
var digest [32]byte
_, err = h.Read(digest[:])
if err != nil {
return tick.TransactionHash{}, errors.Wrap(err, "reading hash from k12")
}
hash, err := getTxHashFromDigestFromPubKey(digest)
if err != nil {
return tick.TransactionHash{}, errors.Wrap(err, "getting id from pubkey")
}
return hash, err
}
func getTxHashFromDigestFromPubKey(digest [32]byte) ([60]byte, error) {
var hash [60]byte
for i := 0; i < 4; i++ {
var publicKeyFragment = binary.LittleEndian.Uint64(digest[i*8 : (i+1)*8])
for j := 0; j < 14; j++ {
hash[i*14+j] = byte((publicKeyFragment % 26) + 'a')
publicKeyFragment /= 26
}
}
h := k12.NewDraft10([]byte{})
_, err := h.Write(hash[:])
if err != nil {
return [60]byte{}, errors.Wrap(err, "writing msg to k12")
}
var identityBytesChecksum [3]byte
_, err = h.Read(identityBytesChecksum[:])
if err != nil {
return [60]byte{}, errors.Wrap(err, "reading hash from k12")
}
var identityBytesChecksumInt uint64
identityBytesChecksumInt = uint64(identityBytesChecksum[0]) | (uint64(identityBytesChecksum[1]) << 8) | (uint64(identityBytesChecksum[2]) << 16)
identityBytesChecksumInt &= 0x3FFFF
for i := 0; i < 4; i++ {
hash[56+i] = byte((identityBytesChecksumInt % 26) + 'a')
identityBytesChecksumInt /= 26
}
return hash, nil
}