forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
346 lines (299 loc) · 10.3 KB
/
types.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2021 github.com/gagliardetto
// This file has been modified by github.com/gagliardetto
//
// Copyright 2020 dfuse Platform Inc.
//
// 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 solana
import (
"fmt"
"time"
bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/solana-go/text"
"github.com/gagliardetto/treeout"
)
type Transaction struct {
// A list of base-58 encoded signatures applied to the transaction.
// The list is always of length `message.header.numRequiredSignatures` and not empty.
// The signature at index `i` corresponds to the public key at index
// `i` in `message.account_keys`. The first one is used as the transaction id.
Signatures []Signature `json:"signatures"`
// Defines the content of the transaction.
Message Message `json:"message"`
}
var _ bin.EncoderDecoder = &Transaction{}
func (t *Transaction) TouchAccount(account PublicKey) bool { return t.Message.TouchAccount(account) }
func (t *Transaction) IsSigner(account PublicKey) bool { return t.Message.IsSigner(account) }
func (t *Transaction) IsWritable(account PublicKey) bool { return t.Message.IsWritable(account) }
func (t *Transaction) AccountMetaList() (out []*AccountMeta) { return t.Message.AccountMetaList() }
func (t *Transaction) ResolveProgramIDIndex(programIDIndex uint16) (PublicKey, error) {
return t.Message.ResolveProgramIDIndex(programIDIndex)
}
type Message struct {
// List of base-58 encoded public keys used by the transaction,
// including by the instructions and for signatures.
// The first `message.header.numRequiredSignatures` public keys must sign the transaction.
AccountKeys []PublicKey `json:"accountKeys"`
// Details the account types and signatures required by the transaction.
Header MessageHeader `json:"header"`
// A base-58 encoded hash of a recent block in the ledger used to
// prevent transaction duplication and to give transactions lifetimes.
RecentBlockhash Hash `json:"recentBlockhash"`
// List of program instructions that will be executed in sequence
// and committed in one atomic transaction if all succeed.
Instructions []CompiledInstruction `json:"instructions"`
}
var _ bin.EncoderDecoder = &Message{}
func (mx *Message) EncodeToTree(txTree treeout.Branches) {
txTree.Child(text.Sf("RecentBlockhash: %s", mx.RecentBlockhash))
txTree.Child(fmt.Sprintf("AccountKeys[len=%v]", len(mx.AccountKeys))).ParentFunc(func(accountKeysBranch treeout.Branches) {
for _, key := range mx.AccountKeys {
accountKeysBranch.Child(text.ColorizeBG(key.String()))
}
})
txTree.Child("Header").ParentFunc(func(message treeout.Branches) {
mx.Header.EncodeToTree(message)
})
}
func (header *MessageHeader) EncodeToTree(mxBranch treeout.Branches) {
mxBranch.Child(text.Sf("NumRequiredSignatures: %v", header.NumRequiredSignatures))
mxBranch.Child(text.Sf("NumReadonlySignedAccounts: %v", header.NumReadonlySignedAccounts))
mxBranch.Child(text.Sf("NumReadonlyUnsignedAccounts: %v", header.NumReadonlyUnsignedAccounts))
}
func (mx *Message) MarshalBinary() ([]byte, error) {
buf := []byte{
mx.Header.NumRequiredSignatures,
mx.Header.NumReadonlySignedAccounts,
mx.Header.NumReadonlyUnsignedAccounts,
}
bin.EncodeCompactU16Length(&buf, len(mx.AccountKeys))
for _, key := range mx.AccountKeys {
buf = append(buf, key[:]...)
}
buf = append(buf, mx.RecentBlockhash[:]...)
bin.EncodeCompactU16Length(&buf, len(mx.Instructions))
for _, instruction := range mx.Instructions {
buf = append(buf, byte(instruction.ProgramIDIndex))
bin.EncodeCompactU16Length(&buf, len(instruction.Accounts))
for _, accountIdx := range instruction.Accounts {
buf = append(buf, byte(accountIdx))
}
bin.EncodeCompactU16Length(&buf, len(instruction.Data))
buf = append(buf, instruction.Data...)
}
return buf, nil
}
func (mx *Message) MarshalWithEncoder(encoder *bin.Encoder) error {
out, err := mx.MarshalBinary()
if err != nil {
return err
}
return encoder.WriteBytes(out, false)
}
func (mx *Message) UnmarshalWithDecoder(decoder *bin.Decoder) (err error) {
{
mx.Header.NumRequiredSignatures, err = decoder.ReadUint8()
if err != nil {
return err
}
mx.Header.NumReadonlySignedAccounts, err = decoder.ReadUint8()
if err != nil {
return err
}
mx.Header.NumReadonlyUnsignedAccounts, err = decoder.ReadUint8()
if err != nil {
return err
}
}
{
numAccountKeys, err := bin.DecodeCompactU16LengthFromByteReader(decoder)
if err != nil {
return err
}
for i := 0; i < numAccountKeys; i++ {
pubkeyBytes, err := decoder.ReadNBytes(32)
if err != nil {
return err
}
var sig PublicKey
copy(sig[:], pubkeyBytes)
mx.AccountKeys = append(mx.AccountKeys, sig)
}
}
{
recentBlockhashBytes, err := decoder.ReadNBytes(32)
if err != nil {
return err
}
var recentBlockhash Hash
copy(recentBlockhash[:], recentBlockhashBytes)
mx.RecentBlockhash = recentBlockhash
}
{
numInstructions, err := bin.DecodeCompactU16LengthFromByteReader(decoder)
if err != nil {
return err
}
for i := 0; i < numInstructions; i++ {
programIDIndex, err := decoder.ReadUint8()
if err != nil {
return err
}
var compInst CompiledInstruction
compInst.ProgramIDIndex = uint16(programIDIndex)
{
numAccounts, err := bin.DecodeCompactU16LengthFromByteReader(decoder)
if err != nil {
return err
}
compInst.AccountCount = bin.Varuint16(numAccounts)
for i := 0; i < numAccounts; i++ {
accountIndex, err := decoder.ReadUint8()
if err != nil {
return err
}
compInst.Accounts = append(compInst.Accounts, uint16(accountIndex))
}
}
{
dataLen, err := bin.DecodeCompactU16LengthFromByteReader(decoder)
if err != nil {
return err
}
dataBytes, err := decoder.ReadNBytes(dataLen)
if err != nil {
return err
}
compInst.DataLength = bin.Varuint16(dataLen)
compInst.Data = Base58(dataBytes)
}
mx.Instructions = append(mx.Instructions, compInst)
}
}
return nil
}
func (m *Message) AccountMetaList() (out []*AccountMeta) {
for _, a := range m.AccountKeys {
out = append(out, &AccountMeta{
PublicKey: a,
IsSigner: m.IsSigner(a),
IsWritable: m.IsWritable(a),
})
}
return out
}
func (m *Message) ResolveProgramIDIndex(programIDIndex uint16) (PublicKey, error) {
if int(programIDIndex) < len(m.AccountKeys) {
return m.AccountKeys[programIDIndex], nil
}
return PublicKey{}, fmt.Errorf("programID index not found %d", programIDIndex)
}
func (m *Message) TouchAccount(account PublicKey) bool {
for _, a := range m.AccountKeys {
if a.Equals(account) {
return true
}
}
return false
}
func (m *Message) IsSigner(account PublicKey) bool {
for idx, acc := range m.AccountKeys {
if acc.Equals(account) {
return idx < int(m.Header.NumRequiredSignatures)
}
}
return false
}
func (m *Message) IsWritable(account PublicKey) bool {
index := 0
found := false
for idx, acc := range m.AccountKeys {
if acc.Equals(account) {
found = true
index = idx
}
}
if !found {
return false
}
h := m.Header
return (index < int(h.NumRequiredSignatures-h.NumReadonlySignedAccounts)) ||
((index >= int(h.NumRequiredSignatures)) && (index < len(m.AccountKeys)-int(h.NumReadonlyUnsignedAccounts)))
}
func (m *Message) signerKeys() []PublicKey {
return m.AccountKeys[0:m.Header.NumRequiredSignatures]
}
type MessageHeader struct {
// The total number of signatures required to make the transaction valid.
// The signatures must match the first `numRequiredSignatures` of `message.account_keys`.
NumRequiredSignatures uint8 `json:"numRequiredSignatures"`
// The last numReadonlySignedAccounts of the signed keys are read-only accounts.
// Programs may process multiple transactions that load read-only accounts within
// a single PoH entry, but are not permitted to credit or debit lamports or modify
// account data.
// Transactions targeting the same read-write account are evaluated sequentially.
NumReadonlySignedAccounts uint8 `json:"numReadonlySignedAccounts"`
// The last `numReadonlyUnsignedAccounts` of the unsigned keys are read-only accounts.
NumReadonlyUnsignedAccounts uint8 `json:"numReadonlyUnsignedAccounts"`
}
type CompiledInstruction struct {
// Index into the message.accountKeys array indicating the program account that executes this instruction.
// NOTE: it is actually a uint8, but using a uint16 because uint8 is treated as a byte everywhere,
// and that can be an issue.
ProgramIDIndex uint16 `json:"programIdIndex"`
AccountCount bin.Varuint16 `json:"-" bin:"sizeof=Accounts"`
DataLength bin.Varuint16 `json:"-" bin:"sizeof=Data"`
// List of ordered indices into the message.accountKeys array indicating which accounts to pass to the program.
// NOTE: it is actually a []uint8, but using a uint16 because []uint8 is treated as a []byte everywhere,
// and that can be an issue.
Accounts []uint16 `json:"accounts"`
// The program input data encoded in a base-58 string.
Data Base58 `json:"data"`
}
func (ci *CompiledInstruction) ResolveInstructionAccounts(message *Message) (out []*AccountMeta) {
metas := message.AccountMetaList()
for _, acct := range ci.Accounts {
out = append(out, metas[acct])
}
return
}
func TransactionFromDecoder(decoder *bin.Decoder) (*Transaction, error) {
var out *Transaction
err := decoder.Decode(&out)
if err != nil {
return nil, err
}
return out, nil
}
func MustTransactionFromDecoder(decoder *bin.Decoder) *Transaction {
out, err := TransactionFromDecoder(decoder)
if err != nil {
panic(err)
}
return out
}
// Unix timestamp (seconds since the Unix epoch)
type UnixTimeSeconds int64
func (res UnixTimeSeconds) Time() time.Time {
return time.Unix(int64(res), 0)
}
func (res UnixTimeSeconds) String() string {
return res.Time().String()
}
type DurationSeconds int64
func (res DurationSeconds) Duration() time.Duration {
return time.Duration(res) * time.Second
}
func (res DurationSeconds) String() string {
return res.Duration().String()
}