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
use std::collections::HashMap;

use {anchor_lang::prelude::*, hpl_utils::Default};

/// User State Account
#[account]
pub struct User {
    pub bump: u8,
    pub primary_wallet: Pubkey,
    pub secondary_wallets: Vec<Pubkey>,
    pub username: String,
    pub name: String,
    pub bio: String,
    pub pfp: String,
}

impl Default for User {
    const LEN: usize = 160 + 8; // base size + 8 align

    fn set_defaults(&mut self) {
        self.bump = 0;
        self.primary_wallet = Pubkey::default();
        self.secondary_wallets = vec![];
        self.username = String::from("");
        self.name = String::from("");
        self.bio = String::from("");
        self.pfp = String::from("");
    }
}

#[account]
pub struct WalletResolver {
    pub bump: u8,
    pub user: Pubkey,
    pub wallet: Pubkey,
}

impl Default for WalletResolver {
    const LEN: usize = 65 + 8; // base size + 8 align

    fn set_defaults(&mut self) {
        self.bump = 0;
        self.user = Pubkey::default();
        self.wallet = Pubkey::default();
    }
}

/// User Profile
#[account]
pub struct Profile {
    pub bump: u8,
    pub project: Pubkey,
    pub user: Pubkey,
    pub identity: ProfileIdentity,
    pub data: HashMap<String, ProfileData>,
}

impl Default for Profile {
    const LEN: usize = 96 + 10 + 8; // base size + 8 align

    fn set_defaults(&mut self) {
        self.bump = 0;
        self.project = Pubkey::default();
        self.user = Pubkey::default();
        self.identity = ProfileIdentity::Main;
        self.data = HashMap::new();
    }
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, PartialEq)]
pub enum ProfileIdentity {
    Main,
    Wallet { key: Pubkey },
    Value { value: String },
}

impl ProfileIdentity {
    pub fn to_bytes(&self) -> Vec<u8> {
        match self {
            ProfileIdentity::Main => {
                let value = String::from("main");
                value.as_bytes().to_vec()
            }
            ProfileIdentity::Wallet { key } => key.as_ref().to_vec(),
            ProfileIdentity::Value { value } => value.as_bytes().to_vec(),
        }
    }

    pub fn to_string(&self) -> String {
        match self {
            ProfileIdentity::Main => String::from("main"),
            ProfileIdentity::Wallet { key } => key.to_string(),
            ProfileIdentity::Value { value } => value.clone(),
        }
    }
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, PartialEq)]
pub enum ProfileData {
    SingleValue { value: String },
    MultiValue { value: Vec<String> },
    Entity { tree: Pubkey },
}
impl ProfileData {
    pub const LEN: usize = 8 + 40;
}