apl-token-metadata 0.6.3

On-chain program for Arch Token Metadata
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//! Program state processor

use {
    crate::{
        error::MetadataError,
        find_attributes_pda_with_program, find_metadata_pda_with_program,
        instruction::MetadataInstruction,
        state::{
            TokenMetadata, TokenMetadataAttributes, DESCRIPTION_MAX_LEN, IMAGE_MAX_LEN,
            MAX_ATTRIBUTES, MAX_KEY_LENGTH, MAX_VALUE_LENGTH, NAME_MAX_LEN, SYMBOL_MAX_LEN,
        },
        ATTRIBUTES_SEED, METADATA_SEED,
    },
    apl_token::{self, state::Mint},
    arch_program::{
        account::{next_account_info, AccountInfo},
        entrypoint::ProgramResult,
        msg,
        program::invoke_signed,
        program_error::ProgramError,
        program_option::COption,
        program_pack::{IsInitialized, Pack},
        pubkey::Pubkey,
        rent::minimum_rent,
        system_instruction::create_account,
    },
};

/// Program state handler.
pub struct Processor {}

impl Processor {
    /// Process a single instruction
    pub fn process(
        program_id: &Pubkey,
        accounts: &[AccountInfo],
        instruction_data: &[u8],
    ) -> ProgramResult {
        let instruction = MetadataInstruction::unpack(instruction_data)?;

        match instruction {
            MetadataInstruction::CreateMetadata {
                name,
                symbol,
                image,
                description,
                immutable,
            } => Self::process_create_metadata(
                program_id,
                accounts,
                name,
                symbol,
                image,
                description,
                immutable,
            ),
            MetadataInstruction::UpdateMetadata {
                name,
                symbol,
                image,
                description,
            } => Self::process_update_metadata(accounts, name, symbol, image, description),
            MetadataInstruction::CreateAttributes { data } => {
                Self::process_create_attributes(program_id, accounts, data)
            }
            MetadataInstruction::ReplaceAttributes { data } => {
                Self::process_replace_attributes(program_id, accounts, data)
            }

            MetadataInstruction::TransferAuthority { new_authority } => {
                Self::process_transfer_authority(accounts, new_authority)
            }

            MetadataInstruction::MakeImmutable => Self::process_make_immutable(accounts),
        }
    }

    fn process_create_metadata(
        program_id: &Pubkey,
        accounts: &[AccountInfo],
        name: String,
        symbol: String,
        image: String,
        description: String,
        immutable: bool,
    ) -> ProgramResult {
        let account_info_iter = &mut accounts.iter();
        let payer_info = next_account_info(account_info_iter)?; // [writable, signer]
        let system_program_info = next_account_info(account_info_iter)?; // []
        let mint_info = next_account_info(account_info_iter)?; // []
        let metadata_info = next_account_info(account_info_iter)?; // [writable]
        let mint_authority_info = next_account_info(account_info_iter)?; // [signer]

        // Token mint must be owned by the token program
        if mint_info.owner != &apl_token::id() {
            msg!(
                "Mint is not owned by the token program expected {:?}, got {:?}",
                apl_token::id(),
                mint_info.owner
            );
            return Err(ProgramError::IncorrectProgramId);
        }

        // Deserialize mint and enforce authority policy
        let mint = Mint::unpack(&mint_info.data.borrow()) //
            .map_err(|_| ProgramError::InvalidAccountData)?;

        if !mint.is_initialized() {
            msg!("Mint is not initialized");
            return Err(ProgramError::UninitializedAccount);
        }

        // Check signer against mint authority, with freeze authority fallback
        let matched_signer: Option<Pubkey> = match mint.mint_authority {
            COption::Some(mint_auth) => {
                if cmp_pubkeys(&mint_auth, mint_authority_info.key) {
                    Some(mint_auth)
                } else {
                    msg!("Mint authority does not match expected authority");
                    return Err(MetadataError::InvalidAuthority.into());
                }
            }
            COption::None => match mint.freeze_authority {
                COption::Some(freeze_auth) => {
                    if cmp_pubkeys(&freeze_auth, mint_authority_info.key) {
                        Some(freeze_auth)
                    } else {
                        msg!("Freeze authority does not match expected authority");
                        return Err(MetadataError::InvalidAuthority.into());
                    }
                }
                COption::None => {
                    msg!("Mint has no mint or freeze authority");
                    return Err(MetadataError::InvalidAuthority.into());
                }
            },
        };

        // Validate mint authority is signer
        if !mint_authority_info.is_signer {
            msg!("Mint authority is not a signer");
            return Err(MetadataError::InvalidAuthority.into());
        }

        // Validate the metadata PDA address
        let (expected_md_pda, md_bump) = find_metadata_pda_with_program(program_id, mint_info.key);
        if !cmp_pubkeys(&expected_md_pda, metadata_info.key) {
            msg!("Metadata PDA does not match expected PDA");
            return Err(ProgramError::InvalidSeeds);
        }

        // Validate field sizes
        if name.len() > NAME_MAX_LEN
            || symbol.len() > SYMBOL_MAX_LEN
            || image.len() > IMAGE_MAX_LEN
            || description.len() > DESCRIPTION_MAX_LEN
        {
            msg!(
                "Metadata field size is too long: name={}/{}, symbol={}/{}, image={}/{}, description={}/{}",
                name.len(),
                NAME_MAX_LEN,
                symbol.len(),
                SYMBOL_MAX_LEN,
                image.len(),
                IMAGE_MAX_LEN,
                description.len(),
                DESCRIPTION_MAX_LEN,
            );
            return Err(MetadataError::StringTooLong.into());
        }

        // If not owned by this program, create metadata PDA via CPI using PDA seeds
        if metadata_info.owner != program_id {
            // Require correct system program id
            if *system_program_info.key != Pubkey::system_program() {
                msg!("System program id does not match expected system program id");
                return Err(ProgramError::IncorrectProgramId);
            }

            if !payer_info.is_signer {
                msg!("Payer is not a signer");
                return Err(ProgramError::MissingRequiredSignature);
            }

            let space = TokenMetadata::LEN as u64;
            let lamports = minimum_rent(TokenMetadata::LEN);

            invoke_signed(
                &create_account(
                    payer_info.key,
                    metadata_info.key,
                    lamports,
                    space,
                    program_id,
                ),
                &[
                    payer_info.clone(),
                    metadata_info.clone(),
                    system_program_info.clone(),
                ],
                &[&[
                    METADATA_SEED, //
                    mint_info.key.as_ref(),
                    &[md_bump],
                ]],
            )?;
        }

        // Ensure not already initialized (zero-initialized account will have first byte == 0)
        {
            let data_ref = metadata_info.data.borrow();
            if !data_ref.is_empty() && data_ref[0] != 0 {
                return Err(MetadataError::MetadataAlreadyExists.into());
            }
        }

        // Write metadata
        let metadata = TokenMetadata {
            is_initialized: true,
            mint: *mint_info.key,
            name,
            symbol,
            image,
            description,
            update_authority: if immutable { None } else { matched_signer },
        };

        metadata.pack_into_slice(&mut metadata_info.data.borrow_mut());

        Ok(())
    }

    fn process_update_metadata(
        accounts: &[AccountInfo],
        name: Option<String>,
        symbol: Option<String>,
        image: Option<String>,
        description: Option<String>,
    ) -> ProgramResult {
        let account_info_iter = &mut accounts.iter();
        let metadata_info = next_account_info(account_info_iter)?; // [writable]
        let update_authority_info = next_account_info(account_info_iter)?; // [signer]

        if !update_authority_info.is_signer {
            msg!("Update authority is not a signer");
            return Err(ProgramError::MissingRequiredSignature);
        }

        // Load existing metadata
        let mut metadata = TokenMetadata::unpack(&metadata_info.data.borrow())
            .map_err(|_| ProgramError::InvalidAccountData)?;

        if !metadata.is_initialized() {
            msg!("Metadata not initialized");
            return Err(ProgramError::UninitializedAccount);
        }

        // Enforce update authority (immutable if None)
        match metadata.update_authority {
            Some(current_auth) => {
                if !cmp_pubkeys(&current_auth, update_authority_info.key) {
                    msg!("Update authority does not match");
                    return Err(MetadataError::InvalidAuthority.into());
                }
            }
            None => {
                msg!("Metadata is immutable");
                return Err(MetadataError::InvalidAuthority.into());
            }
        }

        // Validate and apply optional fields
        if let Some(ref n) = name {
            if n.len() > NAME_MAX_LEN {
                return Err(MetadataError::StringTooLong.into());
            }
        }
        if let Some(ref s) = symbol {
            if s.len() > SYMBOL_MAX_LEN {
                return Err(MetadataError::StringTooLong.into());
            }
        }
        if let Some(ref i) = image {
            if i.len() > IMAGE_MAX_LEN {
                return Err(MetadataError::StringTooLong.into());
            }
        }
        if let Some(ref d) = description {
            if d.len() > DESCRIPTION_MAX_LEN {
                return Err(MetadataError::StringTooLong.into());
            }
        }

        if let Some(n) = name {
            metadata.name = n;
        }
        if let Some(s) = symbol {
            metadata.symbol = s;
        }
        if let Some(i) = image {
            metadata.image = i;
        }
        if let Some(d) = description {
            metadata.description = d;
        }

        metadata.pack_into_slice(&mut metadata_info.data.borrow_mut());
        Ok(())
    }

    fn process_create_attributes(
        program_id: &Pubkey,
        accounts: &[AccountInfo],
        data: Vec<(String, String)>,
    ) -> ProgramResult {
        let account_info_iter = &mut accounts.iter();
        let payer_info = next_account_info(account_info_iter)?; // [writable, signer]
        let system_program_info = next_account_info(account_info_iter)?; // []
        let mint_info = next_account_info(account_info_iter)?; // []
        let attributes_info = next_account_info(account_info_iter)?; // [writable]
        let update_authority_info = next_account_info(account_info_iter)?; // [signer]
        let metadata_info = next_account_info(account_info_iter)?; // [] (readonly)

        if !payer_info.is_signer || !update_authority_info.is_signer {
            return Err(ProgramError::MissingRequiredSignature);
        }

        // Validate attribute PDA address using this program_id
        let (expected_attrs_pda, attrs_bump) =
            find_attributes_pda_with_program(program_id, mint_info.key);
        if !cmp_pubkeys(&expected_attrs_pda, attributes_info.key) {
            msg!("Attributes PDA does not match expected PDA");
            return Err(ProgramError::InvalidSeeds);
        }

        // Ensure metadata exists and authority matches
        let metadata = TokenMetadata::unpack(&metadata_info.data.borrow())
            .map_err(|_| ProgramError::InvalidAccountData)?;
        if !metadata.is_initialized() || !cmp_pubkeys(&metadata.mint, mint_info.key) {
            msg!("Metadata not initialized or mint mismatch");
            return Err(ProgramError::InvalidAccountData);
        }

        match metadata.update_authority {
            Some(current_auth) => {
                if !cmp_pubkeys(&current_auth, update_authority_info.key) {
                    msg!("Update authority does not match");
                    return Err(MetadataError::InvalidAuthority.into());
                }
            }
            None => {
                msg!("Metadata is immutable");
                return Err(MetadataError::InvalidAuthority.into());
            }
        }

        // Validate vector sizes and elements
        if data.len() > MAX_ATTRIBUTES {
            msg!("Too many attributes: {} > {}", data.len(), MAX_ATTRIBUTES);
            return Err(MetadataError::TooManyAttributes.into());
        }
        for (k, v) in &data {
            if k.is_empty() || v.is_empty() {
                msg!("Attribute key and value must be non-empty");
                return Err(MetadataError::InvalidInstructionData.into());
            }
            if k.len() > MAX_KEY_LENGTH || v.len() > MAX_VALUE_LENGTH {
                msg!(
                    "Attribute key or value is too long: key={}/{}, value={}/{}",
                    k.len(),
                    MAX_KEY_LENGTH,
                    v.len(),
                    MAX_VALUE_LENGTH,
                );
                return Err(MetadataError::StringTooLong.into());
            }
        }

        // Allocate full max size so future replacements never need reallocation
        let required_space: u64 = TokenMetadataAttributes::LEN as u64;

        if attributes_info.owner != program_id {
            if *system_program_info.key != Pubkey::system_program() {
                msg!("System program id does not match expected system program id");
                return Err(ProgramError::IncorrectProgramId);
            }
            if !payer_info.is_signer {
                msg!("Payer is not a signer");
                return Err(ProgramError::MissingRequiredSignature);
            }
            let lamports = minimum_rent(TokenMetadataAttributes::LEN);

            invoke_signed(
                &create_account(
                    payer_info.key,
                    attributes_info.key,
                    lamports,
                    required_space,
                    program_id,
                ),
                &[
                    payer_info.clone(),
                    attributes_info.clone(),
                    system_program_info.clone(),
                ],
                &[&[
                    ATTRIBUTES_SEED, //
                    mint_info.key.as_ref(),
                    &[attrs_bump],
                ]],
            )?;
        } else {
            let curr_len = attributes_info.data.borrow().len() as u64;
            if curr_len != required_space {
                msg!(
                    "Attributes account size mismatch: curr={} required={}",
                    curr_len,
                    required_space
                );
                return Err(ProgramError::InvalidAccountData);
            }
        }

        // Ensure not already initialized
        {
            let data_ref = attributes_info.data.borrow();
            if !data_ref.is_empty() && data_ref[0] != 0 {
                return Err(MetadataError::MetadataAlreadyExists.into());
            }
        }

        let attrs = TokenMetadataAttributes {
            is_initialized: true,
            mint: *mint_info.key,
            data,
        };
        attrs.pack_into_slice(&mut attributes_info.data.borrow_mut());
        Ok(())
    }

    fn process_replace_attributes(
        program_id: &Pubkey,
        accounts: &[AccountInfo],
        data: Vec<(String, String)>,
    ) -> ProgramResult {
        let account_info_iter = &mut accounts.iter();
        let attributes_info = next_account_info(account_info_iter)?; // [writable]
        let update_authority_info = next_account_info(account_info_iter)?; // [signer]
        let metadata_info = next_account_info(account_info_iter)?; // [] (readonly)

        if !update_authority_info.is_signer {
            return Err(ProgramError::MissingRequiredSignature);
        }

        // Validate metadata and authority
        let metadata = TokenMetadata::unpack(&metadata_info.data.borrow())
            .map_err(|_| ProgramError::InvalidAccountData)?;
        if !metadata.is_initialized() {
            return Err(ProgramError::UninitializedAccount);
        }
        match metadata.update_authority {
            Some(current_auth) => {
                if !cmp_pubkeys(&current_auth, update_authority_info.key) {
                    return Err(MetadataError::InvalidAuthority.into());
                }
            }
            None => return Err(MetadataError::InvalidAuthority.into()),
        }

        // Validate PDA
        let (expected_attrs_pda, _bump) =
            find_attributes_pda_with_program(program_id, &metadata.mint);
        if !cmp_pubkeys(&expected_attrs_pda, attributes_info.key) {
            return Err(ProgramError::InvalidSeeds);
        }

        // Ensure attributes exist
        let mut attrs = TokenMetadataAttributes::unpack(&attributes_info.data.borrow())
            .map_err(|_| ProgramError::InvalidAccountData)?;
        if !attrs.is_initialized() {
            return Err(ProgramError::UninitializedAccount);
        }

        // Validate sizes
        if data.len() > MAX_ATTRIBUTES {
            return Err(MetadataError::TooManyAttributes.into());
        }
        for (k, v) in &data {
            if k.is_empty() || v.is_empty() {
                return Err(MetadataError::InvalidInstructionData.into());
            }
            if k.len() > MAX_KEY_LENGTH || v.len() > MAX_VALUE_LENGTH {
                return Err(MetadataError::StringTooLong.into());
            }
        }

        // Replace vector
        attrs.data = data;
        attrs.pack_into_slice(&mut attributes_info.data.borrow_mut());
        Ok(())
    }

    fn process_transfer_authority(
        accounts: &[AccountInfo],
        new_authority: Pubkey,
    ) -> ProgramResult {
        let account_info_iter = &mut accounts.iter();
        let metadata_info = next_account_info(account_info_iter)?; // [writable]
        let current_authority_info = next_account_info(account_info_iter)?; // [signer]

        if !current_authority_info.is_signer {
            msg!("Current authority is not a signer");
            return Err(ProgramError::MissingRequiredSignature);
        }

        let mut metadata = TokenMetadata::unpack(&metadata_info.data.borrow())
            .map_err(|_| ProgramError::InvalidAccountData)?;
        if !metadata.is_initialized() {
            msg!("Metadata not initialized");
            return Err(ProgramError::UninitializedAccount);
        }

        match metadata.update_authority {
            Some(current_auth) => {
                if !cmp_pubkeys(&current_auth, current_authority_info.key) {
                    msg!("Signer is not current update authority");
                    return Err(MetadataError::InvalidAuthority.into());
                }
            }
            None => {
                msg!("Metadata is immutable; cannot transfer authority");
                return Err(MetadataError::InvalidAuthority.into());
            }
        }

        metadata.update_authority = Some(new_authority);
        metadata.pack_into_slice(&mut metadata_info.data.borrow_mut());
        Ok(())
    }

    fn process_make_immutable(accounts: &[AccountInfo]) -> ProgramResult {
        let account_info_iter = &mut accounts.iter();
        let metadata_info = next_account_info(account_info_iter)?; // [writable]
        let current_authority_info = next_account_info(account_info_iter)?; // [signer]

        if !current_authority_info.is_signer {
            msg!("Current authority is not a signer");
            return Err(ProgramError::MissingRequiredSignature);
        }

        let mut metadata = TokenMetadata::unpack(&metadata_info.data.borrow())
            .map_err(|_| ProgramError::InvalidAccountData)?;
        if !metadata.is_initialized() {
            msg!("Metadata not initialized");
            return Err(ProgramError::UninitializedAccount);
        }

        match metadata.update_authority {
            Some(current_auth) => {
                if !cmp_pubkeys(&current_auth, current_authority_info.key) {
                    msg!("Signer is not current update authority");
                    return Err(MetadataError::InvalidAuthority.into());
                }
            }
            None => {
                msg!("Metadata already immutable");
                return Err(MetadataError::InvalidAuthority.into());
            }
        }

        metadata.update_authority = None;
        metadata.pack_into_slice(&mut metadata_info.data.borrow_mut());
        Ok(())
    }
}

/// Checks two pubkeys for equality using a cheap memcmp
fn cmp_pubkeys(a: &Pubkey, b: &Pubkey) -> bool {
    arch_program::program_memory::sol_memcmp(a.as_ref(), b.as_ref(), 32) == 0
}