forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
171 lines (146 loc) · 5.65 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
// 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"
bin "github.com/dfuse-io/binary"
)
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"`
}
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"`
}
func (m *Message) AccountMetaList() (out []*AccountMeta) {
return m.AccountMetaList()
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.
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.
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 TransactionFromData(in []byte) (*Transaction, error) {
var out *Transaction
decoder := bin.NewDecoder(in)
err := decoder.Decode(&out)
if err != nil {
return nil, err
}
return out, nil
}
func MustTransactionFromData(in []byte) *Transaction {
out, err := TransactionFromData(in)
if err != nil {
panic(err)
}
return out
}