sol_did/legacy/
legacy_did_account.rs1use crate::constants::VM_DEFAULT_FRAGMENT_NAME;
2use crate::state::{DidAccount, VerificationMethodFlags, VerificationMethodType};
3use crate::{Service, VerificationMethod};
4use anchor_lang::prelude::*;
5use anchor_lang::{AccountDeserialize, AccountSerialize, Owner};
6use borsh::BorshDeserialize;
7use num_traits::*;
8use std::str::FromStr;
9
10#[derive(Clone, BorshDeserialize)]
11pub struct LegacyServiceEndpoint {
12 pub id: String,
16 pub endpoint_type: String,
18 pub endpoint: String,
20 pub description: String,
22}
23
24impl LegacyServiceEndpoint {
25 pub fn post_migration_size(&self) -> usize {
26 4 + self.id.len() + 4 + self.endpoint_type.len() + 4 + self.endpoint.len()
27 }
28}
29
30#[derive(Clone, BorshDeserialize)]
31pub struct LegacyVerificationMethod {
32 pub id: String,
36 pub verification_type: String,
38 pub pubkey: Pubkey,
40}
41
42impl LegacyVerificationMethod {
43 pub fn post_migration_size(&self) -> usize {
44 4 + self.id.len() + 2 + 1 + 4 + 32 }
49}
50
51#[derive(Clone, BorshDeserialize)]
52pub struct LegacyDidAccount {
53 pub account_version: u8,
55 pub authority: Pubkey,
58
59 pub version: String,
62 pub controller: Vec<Pubkey>,
64
65 pub verification_method: Vec<LegacyVerificationMethod>,
67 pub authentication: Vec<String>,
68 pub capability_invocation: Vec<String>,
70 pub capability_delegation: Vec<String>,
71 pub key_agreement: Vec<String>,
72 pub assertion_method: Vec<String>,
73 pub service: Vec<LegacyServiceEndpoint>,
75}
76
77impl LegacyDidAccount {
78 pub fn post_migration_size(&self) -> usize {
79 let mut size = DidAccount::initial_size();
80 size += self
81 .verification_method
82 .iter()
83 .fold(0, |accum, item| accum + item.post_migration_size()); size += self
85 .service
86 .iter()
87 .fold(0, |accum, item| accum + item.post_migration_size()); size += self.controller.len() * 32; size += 8; size
91 }
92
93 pub fn migrate(&self, into: &mut DidAccount, bump: u8) -> Result<()> {
94 let default_flags = self.get_flags(&VM_DEFAULT_FRAGMENT_NAME.to_string())
96 | VerificationMethodFlags::OWNERSHIP_PROOF;
97 into.init(bump, &self.authority, default_flags);
98 let migrated = self.migrate_verification_methods();
99 into.set_verification_methods(Vec::new(), migrated)?;
100 let migrated = self.migrate_services();
101 into.set_services(migrated, false)?;
102 into.set_native_controllers(self.controller.clone())?;
103
104 Ok(())
109 }
110
111 fn migrate_verification_methods(&self) -> Vec<VerificationMethod> {
112 self.verification_method
115 .iter()
116 .map(|vm| VerificationMethod {
117 fragment: vm.id.clone(),
118 method_type: VerificationMethodType::Ed25519VerificationKey2018
119 .to_u8()
120 .unwrap(),
121 flags: self.get_flags(&vm.id).bits(),
122 key_data: vm.pubkey.to_bytes().to_vec(),
123 })
124 .collect()
125 }
126
127 fn get_flags(&self, vm_fragment: &String) -> VerificationMethodFlags {
128 let mut flags = VerificationMethodFlags::NONE;
129
130 if self.authentication.contains(vm_fragment) {
131 flags |= VerificationMethodFlags::AUTHENTICATION;
132 }
133 if self.assertion_method.contains(vm_fragment) {
134 flags |= VerificationMethodFlags::ASSERTION;
135 }
136 if self.capability_invocation.contains(vm_fragment) {
137 flags |= VerificationMethodFlags::CAPABILITY_INVOCATION;
138 }
139 if self.capability_delegation.contains(vm_fragment) {
140 flags |= VerificationMethodFlags::CAPABILITY_DELEGATION;
141 }
142 if self.key_agreement.contains(vm_fragment) {
143 flags |= VerificationMethodFlags::KEY_AGREEMENT;
144 }
145
146 if self.capability_invocation.is_empty() && vm_fragment == VM_DEFAULT_FRAGMENT_NAME {
148 flags |= VerificationMethodFlags::CAPABILITY_INVOCATION;
149 }
150
151 flags
152 }
153
154 fn migrate_services(&self) -> Vec<Service> {
155 self.service
156 .iter()
157 .map(|se| {
158 Service {
159 fragment: se.id.clone(),
160 service_type: se.endpoint_type.clone(),
161 service_endpoint: se.endpoint.clone(),
162 }
165 })
166 .collect()
167 }
168}
169
170impl AccountDeserialize for LegacyDidAccount {
171 fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
172 AnchorDeserialize::deserialize(buf).map_err(|_| ErrorCode::AccountDidNotDeserialize.into())
174 }
175}
176
177impl AccountSerialize for LegacyDidAccount {}
178
179impl Owner for LegacyDidAccount {
180 fn owner() -> Pubkey {
181 Pubkey::from_str("idDa4XeCjVwKcprVAo812coUQbovSZ4kDGJf2sPaBnM").unwrap()
183 }
184}