-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprogram.go
280 lines (243 loc) · 8.49 KB
/
program.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
package system
import (
"bytes"
"crypto/ed25519"
"encoding/binary"
"github.com/pkg/errors"
"github.com/code-payments/code-server/pkg/solana"
)
var ProgramKey [32]byte
const (
commandCreateAccount uint32 = iota
// nolint:varcheck,deadcode,unused
commandAssign
// nolint:varcheck,deadcode,unused
commandTransfer
// nolint:varcheck,deadcode,unused
commandCreateAccountWithSeed
// nolint:varcheck,deadcode,unused
commandAdvanceNonceAccount
// nolint:varcheck,deadcode,unused
commandWithdrawNonceAccount
// nolint:varcheck,deadcode,unused
commandInitializeNonceAccount
// nolint:varcheck,deadcode,unused
commandAuthorizeNonceAccount
// nolint:varcheck,deadcode,unused
commandAllocate
// nolint:varcheck,deadcode,unused
commandAllocateWithSeed
// nolint:varcheck,deadcode,unused
commandAssignWithSeed
// nolint:varcheck,deadcode,unused
commandTransferWithSeed
)
// Reference: https://github.com/solana-labs/solana/blob/f02a78d8fff2dd7297dc6ce6eb5a68a3002f5359/sdk/src/system_instruction.rs#L58-L72
func CreateAccount(funder, address, owner ed25519.PublicKey, lamports, size uint64) solana.Instruction {
// # Account references
// 0. [WRITE, SIGNER] Funding account
// 1. [WRITE, SIGNER] New account
//
// CreateAccount {
// // Number of lamports to transfer to the new account
// lamports: u64,
// // Number of bytes of memory to allocate
// space: u64,
//
// //Address of program that will own the new account
// owner: Pubkey,
// }
//
data := make([]byte, 4+2*8+32)
binary.LittleEndian.PutUint32(data, commandCreateAccount)
binary.LittleEndian.PutUint64(data[4:], lamports)
binary.LittleEndian.PutUint64(data[4+8:], size)
copy(data[4+2*8:], owner)
return solana.NewInstruction(
ProgramKey[:],
data,
solana.NewAccountMeta(funder, true),
solana.NewAccountMeta(address, true),
)
}
type DecompiledCreateAccount struct {
Funder ed25519.PublicKey
Address ed25519.PublicKey
Lamports uint64
Size uint64
Owner ed25519.PublicKey
}
func DecompileCreateAccount(m solana.Message, index int) (*DecompiledCreateAccount, error) {
if index >= len(m.Instructions) {
return nil, errors.Errorf("instruction doesn't exist at %d", index)
}
var prefix [4]byte
binary.LittleEndian.PutUint32(prefix[:], commandCreateAccount)
i := m.Instructions[index]
if !bytes.Equal(m.Accounts[i.ProgramIndex], ProgramKey[:]) {
return nil, solana.ErrIncorrectProgram
}
if !bytes.HasPrefix(i.Data, prefix[:]) {
return nil, solana.ErrIncorrectInstruction
}
if len(i.Accounts) != 2 {
return nil, errors.Errorf("invalid number of accounts: %d", len(i.Accounts))
}
if len(i.Data) != 52 {
return nil, errors.Errorf("invalid instruction data size: %d", len(i.Data))
}
v := &DecompiledCreateAccount{
Funder: m.Accounts[i.Accounts[0]],
Address: m.Accounts[i.Accounts[1]],
}
v.Lamports = binary.LittleEndian.Uint64(i.Data[4:])
v.Size = binary.LittleEndian.Uint64(i.Data[4+8:])
v.Owner = make(ed25519.PublicKey, ed25519.PublicKeySize)
copy(v.Owner, i.Data[4+2*8:])
return v, nil
}
// Reference: https://github.com/solana-labs/solana/blob/f02a78d8fff2dd7297dc6ce6eb5a68a3002f5359/sdk/src/system_instruction.rs#L113-L119
func AdvanceNonce(nonce, authority ed25519.PublicKey) solana.Instruction {
/// # Account references
/// 0. [WRITE, SIGNER] Nonce account
/// 1. [] RecentBlockhashes sysvar
/// 2. [SIGNER] Nonce authority
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data, commandAdvanceNonceAccount)
return solana.NewInstruction(
ProgramKey[:],
data,
solana.NewAccountMeta(nonce, false),
solana.NewReadonlyAccountMeta(RecentBlockhashesSysVar, false),
solana.NewReadonlyAccountMeta(authority, true),
)
}
type DecompiledAdvanceNonce struct {
Nonce ed25519.PublicKey
Authority ed25519.PublicKey
}
func DecompileAdvanceNonce(m solana.Message, index int) (*DecompiledAdvanceNonce, error) {
if index >= len(m.Instructions) {
return nil, errors.Errorf("instruction doesn't exist at %d", index)
}
var prefix [4]byte
binary.LittleEndian.PutUint32(prefix[:], commandAdvanceNonceAccount)
i := m.Instructions[index]
if !bytes.Equal(m.Accounts[i.ProgramIndex], ProgramKey[:]) {
return nil, solana.ErrIncorrectProgram
}
if !bytes.Equal(i.Data, prefix[:]) {
return nil, solana.ErrIncorrectInstruction
}
if len(i.Accounts) != 3 {
return nil, errors.Errorf("invalid number of accounts: %d", len(i.Accounts))
}
if !bytes.Equal(RecentBlockhashesSysVar, m.Accounts[i.Accounts[1]]) {
return nil, errors.Errorf("invalid RecentBlockhashesSysVar")
}
return &DecompiledAdvanceNonce{
Nonce: m.Accounts[i.Accounts[0]],
Authority: m.Accounts[i.Accounts[2]],
}, nil
}
// GetNonceValueFromAccount returns the nonce value of a nonce account.
//
// Layout references:
// https://github.com/solana-labs/solana/blob/d7b9aca87b0327266cde4f0116113a4203642130/web3.js/src/nonce-account.js#L16-L22
// https://github.com/solana-labs/solana/blob/a4956844bdd081e7b90508066c579f29be306ce7/sdk/program/src/nonce/state/current.rs#L26
func GetNonceValueFromAccount(info solana.AccountInfo) (val solana.Blockhash, err error) {
if len(info.Data) != 80 {
return val, errors.Errorf("invalid nonce account size: %d", len(info.Data))
}
if !bytes.Equal(info.Owner, ProgramKey[:]) {
return val, errors.Errorf("invalid nonce account (not owned by sys program)")
}
// (4) u32: version
// (4) u32: size
// (32) pubKey: authority
// (32) pubkey: blockhash/value
start := 4 + 4 + ed25519.PublicKeySize
copy(val[:], info.Data[start:start+ed25519.PublicKeySize])
return val, nil
}
// WithdrawNonce returns an instruction to withdraw funds from a nonce account
//
// The `uint64` parameter is the lamports to withdraw, which must leave the
// account balance above the rent exempt reserve or at zero.
//
// Reference: https://github.com/solana-labs/solana/blob/f02a78d8fff2dd7297dc6ce6eb5a68a3002f5359/sdk/src/system_instruction.rs#L131
func WithdrawNonce(nonce, auth, receipient ed25519.PublicKey, lamports uint64) solana.Instruction {
/// Withdraw funds from a nonce account
///
/// # Account references
/// 0. [WRITE] Nonce account
/// 1. [WRITE] Recipient account
/// 2. [] RecentBlockhashes sysvar
/// 3. [] Rent sysvar
/// 4. [SIGNER] Nonce authority
///
/// The `u64` parameter is the lamports to withdraw, which must leave the
/// account balance above the rent exempt reserve or at zero.
data := make([]byte, 4+8)
binary.LittleEndian.PutUint32(data[:], commandWithdrawNonceAccount)
binary.LittleEndian.PutUint64(data[4:], lamports)
return solana.NewInstruction(
ProgramKey[:],
data,
solana.NewAccountMeta(nonce, true),
solana.NewAccountMeta(receipient, true),
solana.NewReadonlyAccountMeta(RecentBlockhashesSysVar, false),
solana.NewReadonlyAccountMeta(RentSysVar, false),
solana.NewReadonlyAccountMeta(auth, true),
)
}
type DecompiledWithdrawNonce struct {
Nonce ed25519.PublicKey
Auth ed25519.PublicKey
Recipient ed25519.PublicKey
Amount uint64
}
func DecompileWithdrawNonce() {
}
// InitializeNonce returns an instruction to change the state of an Uninitalized nonce account to Initialized, setting the nonce value
//
// The `Pubkey` parameter specifies the entity authorized to execute nonce
// instruction on the account
//
// No signatures are required to execute this instruction, enabling derived
// nonce account addresses
//
// Reference: https://github.com/solana-labs/solana/blob/f02a78d8fff2dd7297dc6ce6eb5a68a3002f5359/sdk/src/system_instruction.rs#L146
func InitializeNonce(nonce, auth ed25519.PublicKey) solana.Instruction {
/// # Account references
/// 0. [WRITE] Nonce account
/// 1. [] RecentBlockhashes sysvar
/// 2. [] Rent sysvar
data := make([]byte, 4+32)
binary.LittleEndian.PutUint32(data, commandInitializeNonceAccount)
copy(data[4:], auth[:32])
return solana.NewInstruction(
ProgramKey[:],
data,
solana.NewAccountMeta(nonce, false),
solana.NewReadonlyAccountMeta(RecentBlockhashesSysVar, false),
solana.NewReadonlyAccountMeta(RentSysVar, false),
)
}
func DecompileInitializeNonce() {
}
// AuthorizeNonce returns an instruction to change the entity authorized to execute nonce instructions on the account
//
// The `Pubkey` parameter identifies the entity to authorize
func AuthorizeNonce(nonce ed25519.PublicKey) solana.Instruction {
/// # Account references
/// 0. [WRITE, SIGNER] Nonce account
data := make([]byte, 4+ed25519.PublicKeySize)
binary.LittleEndian.PutUint32(data[:], commandAuthorizeNonceAccount)
copy(data[4:], nonce[:])
return solana.NewInstruction(
ProgramKey[:],
data,
solana.NewAccountMeta(nonce, true),
)
}