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
use core::ops::{Add, Div, Mul, Sub};
use alloc::string::ToString;
use alloc::{borrow::ToOwned, string::String};
use casper_contract::contract_api::runtime::get_key;
use casper_contract::{
contract_api::{
runtime::{get_call_stack, revert},
storage,
},
unwrap_or_revert::UnwrapOrRevert,
};
use casper_types::U512;
use casper_types::{
system::CallStackElement, ApiError, ContractPackageHash, PublicKey, URef,
};
use ed25519_dalek::{ed25519::signature::Signature, Verifier};
use crate::constants::RUNTIME_FEE;
use crate::ndpc_types::{ApprovedNFT, NFTHolder, PublishRequest, NftMetadata};
use crate::{
constants::NAMED_KEY_RATIO_VERIFIER,
ndpc_types::{self, U64list},
Error,
};
pub fn get_named_key_by_name(dict_name: &str) -> casper_types::URef {
casper_contract::contract_api::runtime::get_key(dict_name)
.unwrap_or_revert()
.into_uref()
.unwrap_or_revert()
}
pub fn get_ratio_verifier() -> casper_types::PublicKey {
let ratio_verifier = casper_contract::contract_api::runtime::get_key(NAMED_KEY_RATIO_VERIFIER)
.unwrap_or_revert()
.into_uref()
.unwrap_or_revert();
storage::read::<casper_types::PublicKey>(ratio_verifier)
.unwrap_or_revert()
.unwrap_or_revert()
}
pub fn verify_signature(public_key: PublicKey, signature: String, message: String) -> bool {
let mut owned_string = "Casper Message:\n".to_owned();
owned_string.push_str(&message);
match public_key {
casper_types::PublicKey::Ed25519(x) => {
let sig = ed25519_dalek::Signature::from_bytes(
base16::decode(signature.as_str()).unwrap().as_slice(),
)
.unwrap();
x.verify(owned_string.as_bytes(), &sig).is_ok()
}
casper_types::PublicKey::Secp256k1(_x) => {
revert(Error::TransferFailed);
}
_ => {
panic!("Invalid ratio verifier type");
}
}
}
pub fn get_holders_cnt(holders_cnt_uref: casper_types::URef) -> u64 {
storage::read(holders_cnt_uref)
.unwrap_or_revert()
.unwrap_or_revert()
}
pub fn get_holder_ids(owners_dict_uref: casper_types::URef, owner: &str) -> Option<U64list> {
storage::dictionary_get(owners_dict_uref, owner).unwrap_or_revert()
}
pub fn contract_package_hash() -> ContractPackageHash {
let call_stacks = get_call_stack();
let last_entry = call_stacks.last().unwrap_or_revert();
let package_hash: Option<ContractPackageHash> = match last_entry {
CallStackElement::StoredContract {
contract_package_hash,
contract_hash: _,
} => Some(*contract_package_hash),
_ => None,
};
package_hash.unwrap_or_revert()
}
pub fn get_nft_metadata(token_id: String, metadatas_dict: URef) -> ndpc_types::NftMetadata {
storage::dictionary_get::<NftMetadata>(metadatas_dict, token_id.as_str()).unwrap_or_revert_with(ApiError::from(Error
::MetadataDoesentExist)).unwrap_or_revert_with(ApiError::from(Error
::MetadataDoesentExist))
}
pub fn get_holder_by_id(holders_dict: URef, holder_id: u64) -> NFTHolder {
storage::dictionary_get::<ndpc_types::NFTHolder>(holders_dict, holder_id.to_string().as_str())
.unwrap_or_revert_with(ApiError::from(Error::HolderDoesentExist))
.unwrap_or_revert_with(ApiError::from(Error::HolderDoesentExist))
}
pub fn get_request_by_id(requests_dict: URef, request_id: u64) -> PublishRequest {
storage::dictionary_get::<PublishRequest>(requests_dict, request_id.to_string().as_str())
.unwrap_or_revert_with(ApiError::from(Error::RequestDoesntExist))
.unwrap_or_revert_with(ApiError::from(Error::RequestDoesntExist))
}
pub fn get_approved_holder_by_id(approved_dict: URef, approved_id: u64) -> ApprovedNFT {
storage::dictionary_get::<ApprovedNFT>(approved_dict, approved_id.to_string().as_str())
.unwrap_or_revert_with(ApiError::from(Error::ApprovedHolderDoesentExist))
.unwrap_or_revert_with(ApiError::from(Error::ApprovedHolderDoesentExist))
}
pub fn get_droplinked_account() -> PublicKey {
get_ratio_verifier()
}
pub struct PaymentDetails {
pub droplinked: U512,
pub recipient: U512,
}
pub fn calculate_payment(
product_price: U512,
shipping_price: U512,
tax_price: U512,
fee: u64,
) -> PaymentDetails {
let droplinked_part = product_price.mul(fee).div(10000u64);
let recipient_part = shipping_price
.add(product_price.sub(droplinked_part))
.add(shipping_price)
.add(tax_price);
PaymentDetails {
droplinked: droplinked_part,
recipient: recipient_part,
}
}
pub(crate) fn get_fee() -> u64 {
let fee_uref = get_key(RUNTIME_FEE)
.unwrap_or_revert_with(Error::KeyNotFound)
.into_uref()
.unwrap_or_revert_with(Error::KeyNotUref);
storage::read::<u64>(fee_uref)
.unwrap_or_revert_with(Error::FeeNotFound)
.unwrap_or_revert_with(Error::FeeNotFound)
}