-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpda.rs
More file actions
186 lines (168 loc) · 6.42 KB
/
pda.rs
File metadata and controls
186 lines (168 loc) · 6.42 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
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
use std::net::Ipv4Addr;
use solana_program::pubkey::Pubkey;
use crate::{
seeds::{
SEED_ACCESS_PASS, SEED_CONFIG, SEED_CONTRIBUTOR, SEED_DEVICE, SEED_DEVICE_TUNNEL_BLOCK,
SEED_DZ_PREFIX_BLOCK, SEED_EXCHANGE, SEED_GLOBALSTATE, SEED_INDEX, SEED_LINK,
SEED_LINK_IDS, SEED_LOCATION, SEED_MULTICASTGROUP_BLOCK, SEED_MULTICAST_GROUP,
SEED_MULTICAST_PUBLISHER_BLOCK, SEED_PERMISSION, SEED_PREFIX, SEED_PROGRAM_CONFIG,
SEED_SEGMENT_ROUTING_IDS, SEED_TENANT, SEED_TUNNEL_IDS, SEED_USER, SEED_USER_TUNNEL_BLOCK,
SEED_VRF_IDS,
},
state::user::UserType,
};
pub fn get_globalstate_pda(program_id: &Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[SEED_PREFIX, SEED_GLOBALSTATE], program_id)
}
pub fn get_globalconfig_pda(program_id: &Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[SEED_PREFIX, SEED_CONFIG], program_id)
}
pub fn get_program_config_pda(program_id: &Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[SEED_PREFIX, SEED_PROGRAM_CONFIG], program_id)
}
pub fn get_location_pda(program_id: &Pubkey, index: u128) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[SEED_PREFIX, SEED_LOCATION, &index.to_le_bytes()],
program_id,
)
}
pub fn get_exchange_pda(program_id: &Pubkey, index: u128) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[SEED_PREFIX, SEED_EXCHANGE, &index.to_le_bytes()],
program_id,
)
}
pub fn get_device_pda(program_id: &Pubkey, index: u128) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[SEED_PREFIX, SEED_DEVICE, &index.to_le_bytes()],
program_id,
)
}
pub fn get_link_pda(program_id: &Pubkey, index: u128) -> (Pubkey, u8) {
Pubkey::find_program_address(&[SEED_PREFIX, SEED_LINK, &index.to_le_bytes()], program_id)
}
pub fn get_user_old_pda(program_id: &Pubkey, index: u128) -> (Pubkey, u8) {
Pubkey::find_program_address(&[SEED_PREFIX, SEED_USER, &index.to_le_bytes()], program_id)
}
pub fn get_user_pda(program_id: &Pubkey, ip: &Ipv4Addr, user_type: UserType) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[SEED_PREFIX, SEED_USER, &ip.octets(), &[user_type as u8]],
program_id,
)
}
pub fn get_multicastgroup_pda(program_id: &Pubkey, index: u128) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[SEED_PREFIX, SEED_MULTICAST_GROUP, &index.to_le_bytes()],
program_id,
)
}
pub fn get_contributor_pda(program_id: &Pubkey, index: u128) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[SEED_PREFIX, SEED_CONTRIBUTOR, &index.to_le_bytes()],
program_id,
)
}
pub fn get_tenant_pda(program_id: &Pubkey, code: &str) -> (Pubkey, u8) {
Pubkey::find_program_address(&[SEED_PREFIX, SEED_TENANT, code.as_bytes()], program_id)
}
pub fn get_permission_pda(program_id: &Pubkey, user_payer: &Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[SEED_PREFIX, SEED_PERMISSION, user_payer.as_ref()],
program_id,
)
}
pub fn get_accesspass_pda(
program_id: &Pubkey,
client_ip: &Ipv4Addr,
user_payer: &Pubkey,
) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[
SEED_PREFIX,
SEED_ACCESS_PASS,
&client_ip.octets(),
&user_payer.to_bytes(),
],
program_id,
)
}
pub fn get_index_pda(program_id: &Pubkey, entity_seed: &[u8], key: &str) -> (Pubkey, u8) {
let lowercase_key = key.to_ascii_lowercase();
Pubkey::find_program_address(
&[
SEED_PREFIX,
SEED_INDEX,
entity_seed,
lowercase_key.as_bytes(),
],
program_id,
)
}
pub fn get_resource_extension_pda(
program_id: &Pubkey,
resource_type: crate::resource::ResourceType,
) -> (Pubkey, u8, &'static [u8]) {
match resource_type {
crate::resource::ResourceType::DeviceTunnelBlock => {
let (pda, bump_seed) =
Pubkey::find_program_address(&[SEED_PREFIX, SEED_DEVICE_TUNNEL_BLOCK], program_id);
(pda, bump_seed, SEED_DEVICE_TUNNEL_BLOCK)
}
crate::resource::ResourceType::UserTunnelBlock => {
let (pda, bump_seed) =
Pubkey::find_program_address(&[SEED_PREFIX, SEED_USER_TUNNEL_BLOCK], program_id);
(pda, bump_seed, SEED_USER_TUNNEL_BLOCK)
}
crate::resource::ResourceType::MulticastGroupBlock => {
let (pda, bump_seed) =
Pubkey::find_program_address(&[SEED_PREFIX, SEED_MULTICASTGROUP_BLOCK], program_id);
(pda, bump_seed, SEED_MULTICASTGROUP_BLOCK)
}
crate::resource::ResourceType::MulticastPublisherBlock => {
let (pda, bump_seed) = Pubkey::find_program_address(
&[SEED_PREFIX, SEED_MULTICAST_PUBLISHER_BLOCK],
program_id,
);
(pda, bump_seed, SEED_MULTICAST_PUBLISHER_BLOCK)
}
crate::resource::ResourceType::DzPrefixBlock(ref associated_pk, index) => {
let (pda, bump_seed) = Pubkey::find_program_address(
&[
SEED_PREFIX,
SEED_DZ_PREFIX_BLOCK,
&associated_pk.to_bytes(),
&index.to_le_bytes(),
],
program_id,
);
(pda, bump_seed, SEED_DZ_PREFIX_BLOCK)
}
crate::resource::ResourceType::TunnelIds(ref associated_pk, index) => {
let (pda, bump_seed) = Pubkey::find_program_address(
&[
SEED_PREFIX,
SEED_TUNNEL_IDS,
&associated_pk.to_bytes(),
&index.to_le_bytes(),
],
program_id,
);
(pda, bump_seed, SEED_TUNNEL_IDS)
}
crate::resource::ResourceType::LinkIds => {
let (pda, bump_seed) =
Pubkey::find_program_address(&[SEED_PREFIX, SEED_LINK_IDS], program_id);
(pda, bump_seed, SEED_LINK_IDS)
}
crate::resource::ResourceType::SegmentRoutingIds => {
let (pda, bump_seed) =
Pubkey::find_program_address(&[SEED_PREFIX, SEED_SEGMENT_ROUTING_IDS], program_id);
(pda, bump_seed, SEED_SEGMENT_ROUTING_IDS)
}
crate::resource::ResourceType::VrfIds => {
let (pda, bump_seed) =
Pubkey::find_program_address(&[SEED_PREFIX, SEED_VRF_IDS], program_id);
(pda, bump_seed, SEED_VRF_IDS)
}
}
}