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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use borsh::{maybestd::io::Error as BorshError, BorshDeserialize, BorshSerialize};
use mpl_utils::{create_or_allocate_account_raw, token::get_mint_authority};
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program_option::COption, pubkey::Pubkey,
};
use super::{compression::is_decompression, *};
use crate::{
assertions::{
assert_mint_authority_matches_mint, assert_owned_by,
collection::assert_collection_update_is_valid, metadata::assert_data_valid,
uses::assert_valid_use,
},
state::{
Collection, CollectionDetails, Data, DataV2, Key, Metadata, ProgrammableConfig,
TokenStandard, Uses, EDITION, MAX_METADATA_LEN, PREFIX,
},
};
pub const SEED_AUTHORITY: Pubkey = Pubkey::new_from_array([
0x92, 0x17, 0x2c, 0xc4, 0x72, 0x5d, 0xc0, 0x41, 0xf9, 0xdd, 0x8c, 0x51, 0x52, 0x60, 0x04, 0x26,
0x00, 0x93, 0xa3, 0x0b, 0x02, 0x73, 0xdc, 0xfa, 0x74, 0x92, 0x17, 0xfc, 0x94, 0xa2, 0x40, 0x49,
]);
pub struct CreateMetadataAccountsLogicArgs<'a> {
pub metadata_account_info: &'a AccountInfo<'a>,
pub mint_info: &'a AccountInfo<'a>,
pub mint_authority_info: &'a AccountInfo<'a>,
pub payer_account_info: &'a AccountInfo<'a>,
pub update_authority_info: &'a AccountInfo<'a>,
pub system_account_info: &'a AccountInfo<'a>,
}
pub fn process_create_metadata_accounts_logic(
program_id: &Pubkey,
accounts: CreateMetadataAccountsLogicArgs,
data: DataV2,
allow_direct_creator_writes: bool,
mut is_mutable: bool,
is_edition: bool,
add_token_standard: bool,
collection_details: Option<CollectionDetails>,
) -> ProgramResult {
let CreateMetadataAccountsLogicArgs {
metadata_account_info,
mint_info,
mint_authority_info,
payer_account_info,
update_authority_info,
system_account_info,
} = accounts;
let mut update_authority_key = *update_authority_info.key;
let existing_mint_authority = get_mint_authority(mint_info)?;
assert_mint_authority_matches_mint(&existing_mint_authority, mint_authority_info).or_else(
|e| {
if mint_authority_info.key == &SEED_AUTHORITY && mint_authority_info.is_signer {
if let COption::Some(auth) = existing_mint_authority {
update_authority_key = auth;
is_mutable = true;
}
Ok(())
} else {
Err(e)
}
},
)?;
assert_owned_by(mint_info, &spl_token::id())?;
let metadata_seeds = &[
PREFIX.as_bytes(),
program_id.as_ref(),
mint_info.key.as_ref(),
];
let (metadata_key, metadata_bump_seed) =
Pubkey::find_program_address(metadata_seeds, program_id);
let metadata_authority_signer_seeds = &[
PREFIX.as_bytes(),
program_id.as_ref(),
mint_info.key.as_ref(),
&[metadata_bump_seed],
];
if metadata_account_info.key != &metadata_key {
return Err(MetadataError::InvalidMetadataKey.into());
}
create_or_allocate_account_raw(
*program_id,
metadata_account_info,
system_account_info,
payer_account_info,
MAX_METADATA_LEN,
metadata_authority_signer_seeds,
)?;
let mut metadata = Metadata::from_account_info(metadata_account_info)?;
let compatible_data = data.to_v1();
let allow_direct_creator_writes =
allow_direct_creator_writes || is_decompression(mint_info, mint_authority_info);
assert_data_valid(
&compatible_data,
&update_authority_key,
&metadata,
allow_direct_creator_writes,
update_authority_info.is_signer,
)?;
let mint_decimals = get_mint_decimals(mint_info)?;
metadata.mint = *mint_info.key;
metadata.key = Key::MetadataV1;
metadata.data = data.to_v1();
metadata.is_mutable = is_mutable;
metadata.update_authority = update_authority_key;
assert_valid_use(&data.uses, &None)?;
metadata.uses = data.uses;
assert_collection_update_is_valid(is_edition, &None, &data.collection)?;
metadata.collection = data.collection;
if let Some(details) = collection_details {
match details {
CollectionDetails::V1 { size: _size } => {
metadata.collection_details = Some(CollectionDetails::V1 { size: 0 });
}
}
} else {
metadata.collection_details = None;
}
if add_token_standard {
let token_standard = if is_edition {
TokenStandard::NonFungibleEdition
} else if mint_decimals == 0 {
TokenStandard::FungibleAsset
} else {
TokenStandard::Fungible
};
metadata.token_standard = Some(token_standard);
} else {
metadata.token_standard = None;
}
puff_out_data_fields(&mut metadata);
let edition_seeds = &[
PREFIX.as_bytes(),
program_id.as_ref(),
metadata.mint.as_ref(),
EDITION.as_bytes(),
];
let (_, edition_bump_seed) = Pubkey::find_program_address(edition_seeds, program_id);
metadata.edition_nonce = Some(edition_bump_seed);
metadata.save(&mut metadata_account_info.data.borrow_mut())?;
Ok(())
}
pub fn meta_deser_unchecked(buf: &mut &[u8]) -> Result<Metadata, BorshError> {
let key: Key = BorshDeserialize::deserialize(buf)?;
let update_authority: Pubkey = BorshDeserialize::deserialize(buf)?;
let mint: Pubkey = BorshDeserialize::deserialize(buf)?;
let data: Data = BorshDeserialize::deserialize(buf)?;
let primary_sale_happened: bool = BorshDeserialize::deserialize(buf)?;
let is_mutable: bool = BorshDeserialize::deserialize(buf)?;
let edition_nonce: Option<u8> = BorshDeserialize::deserialize(buf)?;
let token_standard_res: Result<Option<TokenStandard>, BorshError> =
BorshDeserialize::deserialize(buf);
let collection_res: Result<Option<Collection>, BorshError> = BorshDeserialize::deserialize(buf);
let uses_res: Result<Option<Uses>, BorshError> = BorshDeserialize::deserialize(buf);
let collection_details_res: Result<Option<CollectionDetails>, BorshError> =
BorshDeserialize::deserialize(buf);
let programmable_config_res: Result<Option<ProgrammableConfig>, BorshError> =
BorshDeserialize::deserialize(buf);
let (token_standard, collection, uses) = match (token_standard_res, collection_res, uses_res) {
(Ok(token_standard_res), Ok(collection_res), Ok(uses_res)) => {
(token_standard_res, collection_res, uses_res)
}
_ => (None, None, None),
};
let collection_details = match collection_details_res {
Ok(details) => details,
Err(_) => None,
};
let programmable_config = programmable_config_res.unwrap_or(None);
let metadata = Metadata {
key,
update_authority,
mint,
data,
primary_sale_happened,
is_mutable,
edition_nonce,
token_standard,
collection,
uses,
collection_details,
programmable_config,
};
Ok(metadata)
}
pub fn clean_write_metadata(
metadata: &mut Metadata,
metadata_account_info: &AccountInfo,
) -> ProgramResult {
let mut metadata_account_info_data = metadata_account_info.try_borrow_mut_data()?;
metadata_account_info_data[0..].fill(0);
metadata.serialize(&mut *metadata_account_info_data)?;
Ok(())
}
#[cfg(test)]
pub mod tests {
use solana_program::pubkey;
use super::*;
pub use crate::{state::Creator, utils::puff_out_data_fields};
pub fn pesky_data() -> &'static [u8] {
&[
4, 12, 25, 250, 103, 242, 3, 129, 143, 173, 110, 204, 157, 11, 1, 247, 211, 138, 199,
219, 79, 142, 183, 195, 96, 206, 63, 208, 102, 152, 127, 62, 43, 181, 253, 142, 126,
95, 96, 46, 202, 26, 76, 133, 228, 219, 191, 64, 186, 139, 115, 88, 216, 76, 125, 144,
12, 216, 198, 54, 196, 128, 102, 191, 96, 32, 0, 0, 0, 80, 101, 115, 107, 121, 32, 80,
101, 110, 103, 117, 105, 110, 115, 32, 35, 56, 48, 54, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 10, 0, 0, 0, 78, 79, 79, 84, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0, 104, 116, 116,
112, 115, 58, 47, 47, 97, 114, 119, 101, 97, 118, 101, 46, 110, 101, 116, 47, 72, 122,
79, 110, 102, 78, 77, 87, 81, 66, 72, 84, 57, 118, 48, 68, 87, 56, 69, 114, 57, 89, 70,
119, 100, 105, 71, 74, 88, 52, 45, 117, 75, 57, 82, 83, 89, 65, 82, 56, 102, 120, 69,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 244, 1, 1, 3, 0, 0, 0,
135, 35, 134, 27, 83, 153, 173, 73, 166, 213, 73, 13, 254, 1, 156, 113, 34, 24, 205,
42, 233, 242, 137, 173, 173, 195, 214, 108, 110, 42, 89, 229, 1, 0, 12, 25, 250, 103,
242, 3, 129, 143, 173, 110, 204, 157, 11, 1, 247, 211, 138, 199, 219, 79, 142, 183,
195, 96, 206, 63, 208, 102, 152, 127, 62, 43, 1, 40, 12, 63, 245, 233, 144, 127, 205,
69, 77, 225, 56, 60, 107, 184, 84, 240, 194, 136, 55, 121, 217, 128, 246, 223, 140, 64,
40, 122, 145, 17, 203, 60, 0, 60, 1, 1, 1, 255, 149, 248, 123, 137, 230, 77, 203, 8,
124, 145, 63, 132, 220, 224, 64, 60, 253, 17, 33, 18, 81, 80, 186, 15, 248, 247, 249,
243, 1, 20, 26, 244, 47, 94, 35, 232, 64, 68, 124, 40, 100, 36, 93, 190, 82, 38, 36,
149, 248, 56, 72, 95, 68, 50, 157, 1, 155, 95, 113, 49, 247, 176, 1, 20, 1, 1, 1, 255,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
]
}
pub fn expected_pesky_metadata() -> Metadata {
let creators = vec![
Creator {
address: pubkey!("A6XTVFiwGVsG6b6LsvQTGnV5LH3Pfa3qW3TGz8RjToLp"),
verified: true,
share: 0,
},
Creator {
address: pubkey!("pEsKYABNARLiDFYrjbjHDieD5h6gHrvYf9Vru62NX9k"),
verified: true,
share: 40,
},
Creator {
address: pubkey!("ppTeamTpw1cbC8ybJpppbnoL7xXD9froJNFb5uvoPvb"),
verified: false,
share: 60,
},
];
let data = Data {
name: "Pesky Penguins #8060".to_string(),
symbol: "NOOT".to_string(),
uri: "https://arweave.net/HzOnfNMWQBHT9v0DW8Er9YFwdiGJX4-uK9RSYAR8fxE".to_string(),
seller_fee_basis_points: 500,
creators: Some(creators),
};
let mut metadata = Metadata {
key: Key::MetadataV1,
update_authority: pubkey!("pEsKYABNARLiDFYrjbjHDieD5h6gHrvYf9Vru62NX9k"),
mint: pubkey!("DFR3KjTso6PFCyUtq48a2aPZQpMMoaFgtbdxtaLxF2TR"),
data,
primary_sale_happened: true,
is_mutable: true,
edition_nonce: Some(255),
token_standard: None,
collection: None,
uses: None,
collection_details: None,
programmable_config: None,
};
puff_out_data_fields(&mut metadata);
metadata
}
#[test]
fn deserialize_corrupted_metadata() {
let mut buf = pesky_data();
let metadata = meta_deser_unchecked(&mut buf).unwrap();
let expected_metadata = expected_pesky_metadata();
assert_eq!(metadata, expected_metadata);
}
}