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
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{to_vec, Addr};

pub const MAX_TEXT_LENGTH: u32 = 512;

pub type TokenId = String;

#[cw_serde]
pub struct NFT {
    pub collection: Addr,
    pub token_id: TokenId,
}

impl NFT {
    pub fn into_json_string(self: &NFT) -> String {
        String::from_utf8(to_vec(&self).unwrap_or_default()).unwrap_or_default()
    }
}

#[cw_serde]
pub struct TextRecord {
    pub name: String,           // "twitter"
    pub value: String,          // "shan3v"
    pub verified: Option<bool>, // can only be set by oracle
}

impl TextRecord {
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            verified: None,
        }
    }

    pub fn into_json_string(self: &TextRecord) -> String {
        String::from_utf8(to_vec(&self).unwrap_or_default()).unwrap_or_default()
    }
}

/// Note that the address mapped to the name is stored in `token_uri`.
#[cw_serde]
#[derive(Default)]
pub struct Metadata {
    pub image_nft: Option<NFT>,
    pub records: Vec<TextRecord>,
}

impl Metadata {
    // Yes this is rather ugly. It is used to convert metadata into a JSON
    // string for use in emitting events. Events can only be strings, so
    // serializing it into a JSON string allows the indexer to parse it
    // and represent it as a type. Note that we have to use the CosmWasm fork
    // of serde_json to avoid floats.
    pub fn into_json_string(self: &Metadata) -> String {
        String::from_utf8(to_vec(&self).unwrap_or_default()).unwrap_or_default()
    }
}

#[cw_serde]
pub enum SgNameExecuteMsg {
    /// Set name marketplace contract address
    SetNameMarketplace { address: String },
    /// Set an address for name reverse lookup
    /// Can be an EOA or a contract address
    AssociateAddress {
        name: String,
        address: Option<String>,
    },
    /// Update image
    UpdateImageNft { name: String, nft: Option<NFT> },
    /// Update Metadata
    UpdateMetadata {
        name: String,
        metadata: Option<Metadata>,
    },
    /// Add text record ex: twitter handle, discord name, etc
    AddTextRecord { name: String, record: TextRecord },
    /// Remove text record ex: twitter handle, discord name, etc
    RemoveTextRecord { name: String, record_name: String },
    /// Update text record ex: twitter handle, discord name, etc
    UpdateTextRecord { name: String, record: TextRecord },
    /// Verify a text record (via oracle)
    VerifyTextRecord {
        name: String,
        record_name: String,
        result: bool,
    },
    /// Update the reset the verification oracle
    UpdateVerifier { verifier: Option<String> },
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum SgNameQueryMsg {
    /// `address` can be any Bech32 encoded address. It will be
    /// converted to a stars address for internal mapping.
    #[returns(String)]
    Name { address: String },
    #[returns(Addr)]
    NameMarketplace {},
    #[returns(String)]
    AssociatedAddress { name: String },
    #[returns(Option<NFT>)]
    ImageNFT { name: String },
    #[returns(Vec<TextRecord>)]
    TextRecords { name: String },
    #[returns(bool)]
    IsTwitterVerified { name: String },
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn encode_nft() {
        let nft = NFT {
            collection: Addr::unchecked("stars1y54exmx84cqtasvjnskf9f63djuuj68p2th570"),
            token_id: "1".to_string(),
        };
        let json = nft.into_json_string();
        assert_eq!(
            json,
            r#"{"collection":"stars1y54exmx84cqtasvjnskf9f63djuuj68p2th570","token_id":"1"}"#
        );
    }

    #[test]
    fn encode_text_record() {
        let mut record = TextRecord::new("twitter", "shan3v");
        let json = record.into_json_string();
        assert_eq!(
            json,
            r#"{"name":"twitter","value":"shan3v","verified":null}"#
        );

        record.verified = Some(true);

        let json = record.into_json_string();
        assert_eq!(
            json,
            r#"{"name":"twitter","value":"shan3v","verified":true}"#
        );

        record.verified = Some(false);

        let json = record.into_json_string();
        assert_eq!(
            json,
            r#"{"name":"twitter","value":"shan3v","verified":false}"#
        );
    }

    #[test]
    fn encode_metadata() {
        let image_nft = Some(NFT {
            collection: Addr::unchecked("stars1y54exmx84cqtasvjnskf9f63djuuj68p2th570"),
            token_id: "1".to_string(),
        });
        let record_1 = TextRecord::new("twitter", "shan3v");
        let mut record_2 = TextRecord::new("discord", "shan3v");
        record_2.verified = Some(true);
        let records = vec![record_1, record_2];
        let metadata = Metadata { image_nft, records };

        let json = metadata.into_json_string();
        assert_eq!(
            json,
            r#"{"image_nft":{"collection":"stars1y54exmx84cqtasvjnskf9f63djuuj68p2th570","token_id":"1"},"records":[{"name":"twitter","value":"shan3v","verified":null},{"name":"discord","value":"shan3v","verified":true}]}"#,
        );
    }
}