forked from OneKeyHQ/libsol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstruction.c
More file actions
121 lines (113 loc) · 3.76 KB
/
Copy pathinstruction.c
File metadata and controls
121 lines (113 loc) · 3.76 KB
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
#include "instruction.h"
#include "serum_assert_owner_instruction.h"
#include "spl_memo_instruction.h"
#include "spl_token_instruction.h"
#include "stake_instruction.h"
#include "system_instruction.h"
#include "util.h"
#include <string.h>
enum ProgramId instruction_program_id(
const Instruction* instruction,
const MessageHeader* header
) {
const Pubkey* program_id = &header->pubkeys[instruction->program_id_index];
if (memcmp(program_id, &system_program_id, PUBKEY_SIZE) == 0) {
return ProgramIdSystem;
} else if (memcmp(program_id, &stake_program_id, PUBKEY_SIZE) == 0) {
return ProgramIdStake;
} else if (memcmp(program_id, &vote_program_id, PUBKEY_SIZE) == 0) {
return ProgramIdVote;
} else if (memcmp(program_id, &spl_token_program_id, PUBKEY_SIZE) == 0) {
return ProgramIdSplToken;
} else if (memcmp(
program_id,
&spl_associated_token_account_program_id,
PUBKEY_SIZE
) == 0) {
return ProgramIdSplAssociatedTokenAccount;
} else if (
memcmp(program_id, &serum_assert_owner_program_id, PUBKEY_SIZE) == 0
) {
return ProgramIdSerumAssertOwner;
} else if (memcmp(program_id, &spl_memo_program_id, PUBKEY_SIZE) == 0) {
return ProgramIdSplMemo;
}
return ProgramIdUnknown;
}
int instruction_validate(
const Instruction* instruction,
const MessageHeader* header
) {
BAIL_IF(
instruction->program_id_index >= header->pubkeys_header.pubkeys_length
);
for (size_t i = 0; i < instruction->accounts_length; i++) {
BAIL_IF(
instruction->accounts[i] >= header->pubkeys_header.pubkeys_length
);
}
return 0;
}
bool instruction_info_matches_brief(
const InstructionInfo* info,
const InstructionBrief* brief
) {
if (brief->program_id == info->kind) {
switch (brief->program_id) {
case ProgramIdSerumAssertOwner: return true;
case ProgramIdSplAssociatedTokenAccount: return true;
case ProgramIdSplMemo: return true;
case ProgramIdSplToken: return (brief->spl_token == info->spl_token.kind);
case ProgramIdStake: return (brief->stake == info->stake.kind);
case ProgramIdSystem: return (brief->system == info->system.kind);
case ProgramIdVote: return (brief->vote == info->vote.kind);
case ProgramIdUnknown: break;
}
}
return false;
}
bool instruction_infos_match_briefs(
InstructionInfo* const * infos,
const InstructionBrief* briefs,
size_t len
) {
size_t i;
for (i = 0; i < len; i++) {
if (!instruction_info_matches_brief(infos[i], &briefs[i])) {
break;
}
}
return (i == len);
}
void instruction_accounts_iterator_init(
InstructionAccountsIterator* it,
const MessageHeader* header,
const Instruction* instruction
) {
it->message_header_pubkeys = header->pubkeys;
it->instruction_accounts_length = instruction->accounts_length;
it->instruction_accounts = instruction->accounts;
it->current_instruction_account = 0;
}
int instruction_accounts_iterator_next(
InstructionAccountsIterator* it,
const Pubkey** next_account
) {
if (it->current_instruction_account < it->instruction_accounts_length) {
size_t pubkeys_index =
it->instruction_accounts[it->current_instruction_account++];
if (next_account) {
*next_account = &it->message_header_pubkeys[pubkeys_index];
}
return 0;
}
return 1;
}
size_t instruction_accounts_iterator_remaining(
const InstructionAccountsIterator* it
) {
if (it->current_instruction_account < it->instruction_accounts_length) {
return it->instruction_accounts_length - it->current_instruction_account;
}
return 0;
}