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
use {
borsh::{BorshDeserialize, BorshSerialize},
solana_program::{
instruction::{AccountMeta, Instruction},
program_error::ProgramError,
pubkey::Pubkey,
system_program,
},
};
/// Instructions supported by the generic Name Registry program
#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)]
pub enum NameRegistryInstruction {
/// Create an empty name record
///
/// The address of the name record (account #1) is a program-derived address
/// with the following seeds to ensure uniqueness:
/// * SHA256(HASH_PREFIX, `Create::name`)
/// * Account class (account #3)
/// * Parent name record address (account #4)
///
/// If this is a child record, the parent record's owner must approve by
/// signing (account #5)
///
/// Accounts expected by this instruction:
/// 0. `[]` System program
/// 1. `[writeable, signer]` Funding account (must be a system account)
/// 2. `[writeable]` Name record to be created (program-derived address)
/// 3. `[]` Account owner (written into `NameRecordHeader::owner`)
/// 4. `[signer]` Account class (written into `NameRecordHeader::class`).
/// If `Pubkey::default()` then the `signer` bit is not required
/// 5. `[]` Parent name record (written into
/// `NameRecordHeader::parent_name). `Pubkey::default()` is equivalent
/// to no existing parent.
/// 6. `[signer]` Owner of the parent name record. Optional but needed if
/// parent name different than default.
Create {
/// SHA256 of the (HASH_PREFIX + Name) of the record to create, hashing
/// is done off-chain
hashed_name: Vec<u8>,
/// Number of lamports to fund the name record with
lamports: u64,
/// Number of bytes of memory to allocate in addition to the
/// `NameRecordHeader`
space: u32,
},
/// Update the data in a name record
///
/// Accounts expected by this instruction:
/// * If account class is `Pubkey::default()`:
/// 0. `[writeable]` Name record to be updated
/// 1. `[signer]` Account owner
///
/// * If account class is not `Pubkey::default()`:
/// 0. `[writeable]` Name record to be updated
/// 1. `[signer]` Account class
///
/// * If the signer is the parent name account owner
/// 0. `[writeable]` Name record to be updated
/// 1. `[signer]` Parent name account owner
/// 2. `[]` Parent name record
Update { offset: u32, data: Vec<u8> },
/// Transfer ownership of a name record
///
/// Accounts expected by this instruction:
///
/// * If account class is `Pubkey::default()`:
/// 0. `[writeable]` Name record to be transferred
/// 1. `[signer]` Account owner
///
/// * If account class is not `Pubkey::default()`:
/// 0. `[writeable]` Name record to be transferred
/// 1. `[signer]` Account owner
/// 2. `[signer]` Account class
///
/// * If the signer is the parent name account owner
/// 0. `[writeable]` Name record to be transferred
/// 1. `[signer]` Account owner
/// 2. `[signer]` Account class
/// 3. `[]` Parent name record
Transfer { new_owner: Pubkey },
/// Delete a name record.
///
/// Any lamports remaining in the name record will be transferred to the
/// refund account (#2)
///
/// Accounts expected by this instruction:
/// 0. `[writeable]` Name record to be deleted
/// 1. `[signer]` Account owner
/// 2. `[writeable]` Refund account
Delete,
/// Realloc the data of a name record.
///
/// The space change cannot be more than `MAX_PERMITTED_DATA_LENGTH` greater
/// than current `space`.
///
/// Accounts expected by this instruction:
/// 0. `[]` System program
/// 1. `[writeable, signer]` Payer account (will be refunded if new
/// `space` is less than current `space`)
/// 2. `[writeable]` Name record to be reallocated
/// 3. `[signer]` Account owner
Realloc {
/// New total number of bytes in addition to the `NameRecordHeader`.
/// There are no checks on the existing data; it will be truncated if
/// the new space is less than the current space.
space: u32,
},
}
#[allow(clippy::too_many_arguments)]
pub fn create(
name_service_program_id: Pubkey,
instruction_data: NameRegistryInstruction,
name_account_key: Pubkey,
payer_key: Pubkey,
name_owner: Pubkey,
name_class_opt: Option<Pubkey>,
name_parent_opt: Option<Pubkey>,
name_parent_owner_opt: Option<Pubkey>,
) -> Result<Instruction, ProgramError> {
let data = borsh::to_vec(&instruction_data).unwrap();
let mut accounts = vec![
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new(payer_key, true),
AccountMeta::new(name_account_key, false),
AccountMeta::new_readonly(name_owner, false),
];
if let Some(name_class) = name_class_opt {
accounts.push(AccountMeta::new_readonly(name_class, true));
} else {
accounts.push(AccountMeta::new_readonly(Pubkey::default(), false));
}
if let Some(name_parent) = name_parent_opt {
accounts.push(AccountMeta::new_readonly(name_parent, false));
} else {
accounts.push(AccountMeta::new_readonly(Pubkey::default(), false));
}
if let Some(key) = name_parent_owner_opt {
accounts.push(AccountMeta::new_readonly(key, true));
}
Ok(Instruction {
program_id: name_service_program_id,
accounts,
data,
})
}
pub fn update(
name_service_program_id: Pubkey,
offset: u32,
data: Vec<u8>,
name_account_key: Pubkey,
name_update_signer: Pubkey,
name_parent: Option<Pubkey>,
) -> Result<Instruction, ProgramError> {
let instruction_data = NameRegistryInstruction::Update { offset, data };
let data = borsh::to_vec(&instruction_data).unwrap();
let mut accounts = vec![
AccountMeta::new(name_account_key, false),
AccountMeta::new_readonly(name_update_signer, true),
];
if let Some(name_parent_key) = name_parent {
accounts.push(AccountMeta::new(name_parent_key, false))
}
Ok(Instruction {
program_id: name_service_program_id,
accounts,
data,
})
}
pub fn transfer(
name_service_program_id: Pubkey,
new_owner: Pubkey,
name_account_key: Pubkey,
name_owner_key: Pubkey,
name_class_opt: Option<Pubkey>,
) -> Result<Instruction, ProgramError> {
let instruction_data = NameRegistryInstruction::Transfer { new_owner };
let data = borsh::to_vec(&instruction_data).unwrap();
let mut accounts = vec![
AccountMeta::new(name_account_key, false),
AccountMeta::new_readonly(name_owner_key, true),
];
if let Some(key) = name_class_opt {
accounts.push(AccountMeta::new_readonly(key, true));
}
Ok(Instruction {
program_id: name_service_program_id,
accounts,
data,
})
}
pub fn delete(
name_service_program_id: Pubkey,
name_account_key: Pubkey,
name_owner_key: Pubkey,
refund_target: Pubkey,
) -> Result<Instruction, ProgramError> {
let instruction_data = NameRegistryInstruction::Delete;
let data = borsh::to_vec(&instruction_data).unwrap();
let accounts = vec![
AccountMeta::new(name_account_key, false),
AccountMeta::new_readonly(name_owner_key, true),
AccountMeta::new(refund_target, false),
];
Ok(Instruction {
program_id: name_service_program_id,
accounts,
data,
})
}
pub fn realloc(
name_service_program_id: Pubkey,
payer_key: Pubkey,
name_account_key: Pubkey,
name_owner_key: Pubkey,
space: u32,
) -> Result<Instruction, ProgramError> {
let instruction_data = NameRegistryInstruction::Realloc { space };
let data = borsh::to_vec(&instruction_data).unwrap();
let accounts = vec![
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new(payer_key, true),
AccountMeta::new(name_account_key, false),
AccountMeta::new_readonly(name_owner_key, true),
];
Ok(Instruction {
program_id: name_service_program_id,
accounts,
data,
})
}