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
use crate::state::{DidAccount, Secp256k1RawSignature};
use crate::{Service, VerificationMethod};
use anchor_lang::prelude::*;

pub fn update(
    ctx: Context<Update>,
    update_arg: UpdateArg,
    eth_signature: Option<Secp256k1RawSignature>,
) -> Result<()> {
    // Move the business logic DidAccount struct.
    let data = &mut ctx.accounts.did_data;
    if eth_signature.is_some() {
        data.nonce += 1;
    }

    data.set_services(update_arg.services, false)?;
    data.set_verification_methods(Vec::new(), update_arg.verification_methods)?;
    data.set_native_controllers(update_arg.native_controllers)?;
    data.set_other_controllers(update_arg.other_controllers)?;

    Ok(())
}

#[derive(Accounts)]
#[instruction(update_arg: UpdateArg, eth_signature: Option<Secp256k1RawSignature>)]
pub struct Update<'info> {
    #[account(
        mut,
        seeds = [b"did-account", did_data.initial_verification_method.key_data.as_ref()],
        bump = did_data.bump,
        constraint = did_data.find_authority_constraint(&authority.key(), &update_arg.try_to_vec().unwrap(), eth_signature.as_ref(), None).is_some()
    )]
    pub did_data: Account<'info, DidAccount>,
    pub authority: Signer<'info>,
}

/// Argument
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct UpdateArg {
    /// All verification methods
    pub verification_methods: Vec<VerificationMethod>,
    /// Services
    pub services: Vec<Service>,
    /// Controller (native) - did:sol:<controller>
    pub native_controllers: Vec<Pubkey>,
    /// Controller (others) - all others
    pub other_controllers: Vec<String>,
}