-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtypes.go
131 lines (115 loc) · 3.87 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
// 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/streamingfast/binary"
)
type Transaction struct {
Signatures []Signature `json:"signatures"`
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 uint8) (PublicKey, error) {
return t.Message.ResolveProgramIDIndex(programIDIndex)
}
type Message struct {
Header MessageHeader `json:"header"`
AccountKeys []PublicKey `json:"accountKeys"`
RecentBlockhash PublicKey/* TODO: change to Hash */ `json:"recentBlockhash"`
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 uint8) (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 {
NumRequiredSignatures uint8 `json:"numRequiredSignatures"`
NumReadonlySignedAccounts uint8 `json:"numReadonlySignedAccounts"`
NumReadonlyUnsignedAccounts uint8 `json:"numReadonlyUnsignedAccounts"`
}
type CompiledInstruction struct {
ProgramIDIndex uint8 `json:"programIdIndex"`
AccountCount bin.Varuint16 `json:"-" bin:"sizeof=Accounts"`
Accounts []uint8 `json:"accounts"`
DataLength bin.Varuint16 `json:"-" bin:"sizeof=Data"`
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
}