-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathencoding.go
164 lines (133 loc) · 4.06 KB
/
encoding.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
package solana
import (
"bytes"
"crypto/ed25519"
"io"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
"github.com/code-payments/code-server/pkg/solana/shortvec"
)
func (s TransactionSignature) ToBase58() string {
return base58.Encode(s.Signature[:])
}
func (t Transaction) Marshal() []byte {
b := bytes.NewBuffer(nil)
// Signatures
_, _ = shortvec.EncodeLen(b, len(t.Signatures))
for _, s := range t.Signatures {
_, _ = b.Write(s[:])
}
// Message
_, _ = b.Write(t.Message.Marshal())
return b.Bytes()
}
func (t *Transaction) Unmarshal(b []byte) error {
buf := bytes.NewBuffer(b)
sigLen, err := shortvec.DecodeLen(buf)
if err != nil {
return errors.Wrap(err, "failed to read signature length")
}
t.Signatures = make([]Signature, sigLen)
for i := 0; i < sigLen; i++ {
if _, err = io.ReadFull(buf, t.Signatures[i][:]); err != nil {
return errors.Wrapf(err, "failed to read signature at %d", i)
}
}
return (&t.Message).Unmarshal(buf.Bytes())
}
func (m Message) Marshal() []byte {
b := bytes.NewBuffer(nil)
// Header
_ = b.WriteByte(m.Header.NumSignatures)
_ = b.WriteByte(m.Header.NumReadonlySigned)
_ = b.WriteByte(m.Header.NumReadOnly)
// Accounts
_, _ = shortvec.EncodeLen(b, len(m.Accounts))
for _, a := range m.Accounts {
_, _ = b.Write(a)
}
// Recent Blockhash
_, _ = b.Write(m.RecentBlockhash[:])
// Instructions
_, _ = shortvec.EncodeLen(b, len(m.Instructions))
for _, i := range m.Instructions {
_ = b.WriteByte(i.ProgramIndex)
// Accounts
_, _ = shortvec.EncodeLen(b, len(i.Accounts))
_, _ = b.Write(i.Accounts)
// Data
_, _ = shortvec.EncodeLen(b, len(i.Data))
_, _ = b.Write(i.Data)
}
return b.Bytes()
}
func (m *Message) Unmarshal(b []byte) (err error) {
buf := bytes.NewBuffer(b)
// Header
if m.Header.NumSignatures, err = buf.ReadByte(); err != nil {
return errors.Wrap(err, "failed to read num signatures")
}
if m.Header.NumReadonlySigned, err = buf.ReadByte(); err != nil {
return errors.Wrap(err, "failed to read num readonly signatures")
}
if m.Header.NumReadOnly, err = buf.ReadByte(); err != nil {
return errors.Wrap(err, "failed to read num readonly")
}
// Accounts
accountLen, err := shortvec.DecodeLen(buf)
if err != nil {
return errors.Wrap(err, "failed to read account len")
}
m.Accounts = make([]ed25519.PublicKey, accountLen)
for i := 0; i < accountLen; i++ {
m.Accounts[i] = make([]byte, ed25519.PublicKeySize)
if _, err = io.ReadFull(buf, m.Accounts[i]); err != nil {
return errors.Wrapf(err, "failed to read account at index %d", i)
}
}
// Recent block hash
if _, err = io.ReadFull(buf, m.RecentBlockhash[:]); err != nil {
return errors.Wrap(err, "failed to read recent block hash")
}
// Instructions
instructionLen, err := shortvec.DecodeLen(buf)
if err != nil {
return errors.Wrap(err, "failed to read instruction len")
}
m.Instructions = make([]CompiledInstruction, instructionLen)
for i := 0; i < instructionLen; i++ {
var c CompiledInstruction
// Program Index
if c.ProgramIndex, err = buf.ReadByte(); err != nil {
return errors.Wrapf(err, "failed to read instruction[%d] program index", i)
}
if int(c.ProgramIndex) >= len(m.Accounts) {
return errors.Errorf("program index out of range: %d:%d", i, c.ProgramIndex)
}
// Account Indexes
accountLen, err = shortvec.DecodeLen(buf)
if err != nil {
return errors.Wrapf(err, "failed to read instruction[%d] account len", i)
}
c.Accounts = make([]byte, accountLen)
if _, err = io.ReadFull(buf, c.Accounts); err != nil {
return errors.Wrapf(err, "failed to read instruction[%d] accounts", i)
}
for _, index := range c.Accounts {
if int(index) >= len(m.Accounts) {
return errors.Errorf("account index out of range: %d:%d", i, index)
}
}
// Data
dataLen, err := shortvec.DecodeLen(buf)
if err != nil {
return errors.Wrapf(err, "failed to read instruction[%d] data len", i)
}
c.Data = make([]byte, dataLen)
if _, err = io.ReadFull(buf, c.Data); err != nil {
return errors.Wrapf(err, "failed to read instruction[%d] data", i)
}
m.Instructions[i] = c
}
return nil
}