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
#![allow(clippy::field_reassign_with_default)]
use cosmwasm_std::{to_binary, Binary, Coin, CosmosMsg, HumanAddr, StdResult};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::types::{AttributeValueType, MarkerAccess, MarkerType, ProvenanceRoute};

// The data format version to pass into provenance for message encoding
static MSG_DATAFMT_VERSION: &str = "2.0.0";

/// Represents a request to encode custom provenance messages.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ProvenanceMsg {
    pub route: ProvenanceRoute,      // The module router key
    pub params: ProvenanceMsgParams, // The module-specific encoder params
    pub version: String,             // The data format version
}

/// A helper method to simplify creating messages.
impl From<ProvenanceMsg> for CosmosMsg<ProvenanceMsg> {
    fn from(msg: ProvenanceMsg) -> CosmosMsg<ProvenanceMsg> {
        CosmosMsg::Custom(msg)
    }
}

/// Input params for custom provenance message encoders.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub enum ProvenanceMsgParams {
    Name(NameMsgParams),
    Attribute(AttributeMsgParams),
    Marker(MarkerMsgParams),
}

/// Input params for creating name module messages.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum NameMsgParams {
    BindName {
        name: String,
        address: HumanAddr,
        restrict: bool,
    },
    DeleteName {
        name: String,
    },
}

/// A helper method to simplify creating messages.
impl From<NameMsgParams> for ProvenanceMsgParams {
    fn from(params: NameMsgParams) -> ProvenanceMsgParams {
        ProvenanceMsgParams::Name(params)
    }
}

// Create a custom cosmos message using name module params.
fn create_name_msg(params: NameMsgParams) -> CosmosMsg<ProvenanceMsg> {
    ProvenanceMsg {
        route: ProvenanceRoute::Name,
        params: params.into(),
        version: String::from(MSG_DATAFMT_VERSION),
    }
    .into()
}

/// Create a message that will bind a restricted name to an address.
pub fn bind_name(name: String, address: HumanAddr) -> CosmosMsg<ProvenanceMsg> {
    create_name_msg(NameMsgParams::BindName {
        name,
        address,
        restrict: true,
    })
}

/// Create a message that will bind a unrestricted name to an address.
pub fn bind_name_unrestricted(name: String, address: HumanAddr) -> CosmosMsg<ProvenanceMsg> {
    create_name_msg(NameMsgParams::BindName {
        name,
        address,
        restrict: false,
    })
}

/// Create a message that will un-bind a name from an address.
pub fn unbind_name(name: String) -> CosmosMsg<ProvenanceMsg> {
    create_name_msg(NameMsgParams::DeleteName { name })
}

/// Input params for creating attribute module messages.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum AttributeMsgParams {
    AddAttribute {
        address: HumanAddr,
        name: String,
        value: Binary,
        value_type: AttributeValueType,
    },
    DeleteAttribute {
        name: String,
        address: HumanAddr,
    },
}

/// A helper method to simplify creating messages.
impl From<AttributeMsgParams> for ProvenanceMsgParams {
    fn from(params: AttributeMsgParams) -> ProvenanceMsgParams {
        ProvenanceMsgParams::Attribute(params)
    }
}

// Create a custom cosmos message using attribute module params.
fn create_attribute_msg(params: AttributeMsgParams) -> CosmosMsg<ProvenanceMsg> {
    ProvenanceMsg {
        route: ProvenanceRoute::Attribute,
        params: params.into(),
        version: String::from(MSG_DATAFMT_VERSION),
    }
    .into()
}

/// Create a message that will add a an attribute to an account.
pub fn add_attribute(
    address: HumanAddr,
    name: String,
    value: Binary,
    value_type: AttributeValueType,
) -> CosmosMsg<ProvenanceMsg> {
    create_attribute_msg(AttributeMsgParams::AddAttribute {
        address,
        name,
        value,
        value_type,
    })
}

/// Create a message that will add a UUID attribute to an account.
pub fn add_uuid_attribute(
    address: HumanAddr,
    name: String,
    uuid: String,
) -> CosmosMsg<ProvenanceMsg> {
    add_attribute(
        address,
        name,
        Binary::from(uuid.as_bytes()),
        AttributeValueType::Uuid,
    )
}

/// Create a message that will add a binary attribute to an account.
pub fn add_binary_attribute(
    address: HumanAddr,
    name: String,
    value: Binary,
) -> CosmosMsg<ProvenanceMsg> {
    add_attribute(address, name, value, AttributeValueType::Bytes)
}

/// Create a message that will add a string attribute to an account.
pub fn add_string_attribute(
    address: HumanAddr,
    name: String,
    value: String,
) -> CosmosMsg<ProvenanceMsg> {
    add_attribute(
        address,
        name,
        Binary::from(value.as_bytes()),
        AttributeValueType::String,
    )
}

/// Create a message that will add a JSON attribute to an account. Serializable types can be passed
/// into this function, but it's up to the user to handle StdResult error case.
pub fn add_json_attribute<T: Serialize + ?Sized>(
    address: HumanAddr,
    name: String,
    data: &T,
) -> StdResult<CosmosMsg<ProvenanceMsg>> {
    // Serialize the value, bailing on error
    let value = to_binary(data)?;
    // Create and return json typed message
    Ok(add_attribute(
        address,
        name,
        value,
        AttributeValueType::Json,
    ))
}

/// Create a message that will remove all attributes with the given name from an account.
pub fn delete_attributes(name: String, address: HumanAddr) -> CosmosMsg<ProvenanceMsg> {
    create_attribute_msg(AttributeMsgParams::DeleteAttribute { name, address })
}

/// Input params for creating marker module messages.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum MarkerMsgParams {
    CreateMarker {
        coin: Coin,
        marker_type: MarkerType,
    },
    GrantMarkerAccess {
        denom: String,
        address: HumanAddr,
        permissions: Vec<MarkerAccess>,
    },
    RevokeMarkerAccess {
        denom: String,
        address: HumanAddr,
    },
    FinalizeMarker {
        denom: String,
    },
    ActivateMarker {
        denom: String,
    },
    CancelMarker {
        denom: String,
    },
    DestroyMarker {
        denom: String,
    },
    MintMarkerSupply {
        coin: Coin,
    },
    BurnMarkerSupply {
        coin: Coin,
    },
    WithdrawMarkerCoins {
        coin: Coin,
        recipient: HumanAddr,
    },
    TransferMarkerCoins {
        coin: Coin,
        to: HumanAddr,
        from: HumanAddr,
    },
}

/// A helper method to simplify creating messages.
impl From<MarkerMsgParams> for ProvenanceMsgParams {
    fn from(params: MarkerMsgParams) -> ProvenanceMsgParams {
        ProvenanceMsgParams::Marker(params)
    }
}

// Create a custom cosmos message using marker module params.
fn create_marker_msg(params: MarkerMsgParams) -> CosmosMsg<ProvenanceMsg> {
    ProvenanceMsg {
        route: ProvenanceRoute::Marker,
        params: params.into(),
        version: String::from(MSG_DATAFMT_VERSION),
    }
    .into()
}

/// Create a message that will propose a new marker with the default type.
pub fn create_marker(coin: Coin) -> CosmosMsg<ProvenanceMsg> {
    create_marker_with_type(coin, MarkerType::Coin)
}

/// Create a message that will propose a new marker with the type set to restricted.
pub fn create_restricted_marker(coin: Coin) -> CosmosMsg<ProvenanceMsg> {
    create_marker_with_type(coin, MarkerType::Restricted)
}

// Create a message that will propose a new marker with a given type.
fn create_marker_with_type(coin: Coin, marker_type: MarkerType) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::CreateMarker { coin, marker_type })
}

/// Create a message that will grant permissions to a marker.
pub fn grant_marker_access(
    denom: String,
    address: HumanAddr,
    permissions: Vec<MarkerAccess>,
) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::GrantMarkerAccess {
        denom,
        address,
        permissions,
    })
}

/// Create a message that will grant all available permissions to a marker.
pub fn grant_marker_access_all(denom: String, address: HumanAddr) -> CosmosMsg<ProvenanceMsg> {
    grant_marker_access(
        denom,
        address,
        vec![
            MarkerAccess::Admin,
            MarkerAccess::Burn,
            MarkerAccess::Delete,
            MarkerAccess::Deposit,
            MarkerAccess::Mint,
            MarkerAccess::Transfer,
            MarkerAccess::Withdraw,
        ],
    )
}

/// Create a message that will grant supply permissions to a marker.
pub fn grant_marker_access_supply(denom: String, address: HumanAddr) -> CosmosMsg<ProvenanceMsg> {
    grant_marker_access(denom, address, vec![MarkerAccess::Burn, MarkerAccess::Mint])
}

/// Create a message that will grant asset permissions to a marker.
pub fn grant_marker_access_asset(denom: String, address: HumanAddr) -> CosmosMsg<ProvenanceMsg> {
    grant_marker_access(
        denom,
        address,
        vec![MarkerAccess::Deposit, MarkerAccess::Withdraw],
    )
}

/// Create a message that will revoke marker permissions.
pub fn revoke_marker_access(denom: String, address: HumanAddr) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::RevokeMarkerAccess { denom, address })
}

/// Create a message that will finalize a proposed marker.
pub fn finalize_marker(denom: String) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::FinalizeMarker { denom })
}

/// Create a message that will activate a finalized marker.
pub fn activate_marker(denom: String) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::ActivateMarker { denom })
}

/// Create a message that will cancel a marker.
pub fn cancel_marker(denom: String) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::CancelMarker { denom })
}

/// Create a message that will destroy a marker.
pub fn destroy_marker(denom: String) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::DestroyMarker { denom })
}

/// Create a message that will mint marker coins.
pub fn mint_marker_supply(coin: Coin) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::MintMarkerSupply { coin })
}

/// Create a message that will burn marker coins.
pub fn burn_marker_supply(coin: Coin) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::BurnMarkerSupply { coin })
}

/// Create a message that will transfer marker coins to a recipient account.
pub fn withdraw_marker_coins(coin: Coin, recipient: HumanAddr) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::WithdrawMarkerCoins { coin, recipient })
}

/// Create a message that will transfer a marker amount from one account to another.
pub fn transfer_marker_coins(
    coin: Coin,
    to: HumanAddr,
    from: HumanAddr,
) -> CosmosMsg<ProvenanceMsg> {
    create_marker_msg(MarkerMsgParams::TransferMarkerCoins { coin, to, from })
}