-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcredentials.rs
174 lines (161 loc) · 6.22 KB
/
credentials.rs
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
// Polimec Blockchain – https://www.polimec.org/
// Copyright (C) Polimec 2022. All rights reserved.
// The Polimec Blockchain is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Polimec Blockchain is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::*;
use frame_support::{
assert_ok, dispatch::GetDispatchInfo, pallet_prelude::TransactionSource, traits::tokens::currency::VestingSchedule,
};
use pallet_asset_tx_payment::Val;
use pallet_skip_feeless_payment::Intermediate;
use polimec_common::credentials::{Did, InvestorType};
use polimec_common_test_utils::{get_fake_jwt, get_mock_jwt_with_cid, get_test_jwt};
use polimec_runtime::PLMC;
use sp_runtime::{
generic::Era,
traits::{TransactionExtension, TxBaseImplication},
AccountId32, DispatchError,
};
use tests::defaults::*;
#[test]
fn test_jwt_for_create() {
let project = default_project_metadata(ISSUER.into());
PolimecNet::execute_with(|| {
let issuer = AccountId32::from(ISSUER);
assert_ok!(PolimecBalances::force_set_balance(PolimecOrigin::root(), issuer.into(), 10_000 * PLMC));
let retail_jwt = get_test_jwt(PolimecAccountId::from(ISSUER), InvestorType::Retail);
assert_noop!(
PolimecFunding::create_project(PolimecOrigin::signed(ISSUER.into()), retail_jwt, project.clone()),
pallet_funding::Error::<PolimecRuntime>::WrongInvestorType
);
let inst_jwt = get_test_jwt(PolimecAccountId::from(ISSUER), InvestorType::Institutional);
assert_ok!(PolimecFunding::create_project(PolimecOrigin::signed(ISSUER.into()), inst_jwt, project.clone()));
});
}
#[test]
fn test_jwt_verification() {
let project = default_project_metadata(ISSUER.into());
PolimecNet::execute_with(|| {
let issuer = AccountId32::from(ISSUER);
assert_ok!(PolimecBalances::force_set_balance(PolimecOrigin::root(), issuer.into(), 1000 * PLMC));
// This JWT tokens is signed with a private key that is not the one set in the Pallet Funding configuration in the real runtime.
let inst_jwt = get_fake_jwt(PolimecAccountId::from(ISSUER), InvestorType::Institutional);
assert_noop!(
PolimecFunding::create_project(PolimecOrigin::signed(ISSUER.into()), inst_jwt, project.clone()),
DispatchError::BadOrigin
);
});
}
macros::generate_accounts!(EMPTY_ACCOUNT);
#[test]
fn dispenser_signed_extensions_pass_for_new_account() {
PolimecNet::execute_with(|| {
let who = PolimecAccountId::from(EMPTY_ACCOUNT);
assert_eq!(PolimecBalances::free_balance(who.clone()), 0);
let jwt = get_test_jwt(who.clone(), InvestorType::Retail);
let free_call = PolimecCall::Dispenser(pallet_dispenser::Call::dispense { jwt: jwt.clone() });
let paid_call = PolimecCall::System(frame_system::Call::remark { remark: vec![69, 69] });
let extra: polimec_runtime::TxExtension = (
frame_system::CheckNonZeroSender::<PolimecRuntime>::new(),
frame_system::CheckSpecVersion::<PolimecRuntime>::new(),
frame_system::CheckTxVersion::<PolimecRuntime>::new(),
frame_system::CheckGenesis::<PolimecRuntime>::new(),
frame_system::CheckEra::<PolimecRuntime>::from(Era::mortal(0u64, 0u64)),
pallet_dispenser::extensions::CheckNonce::<PolimecRuntime>::from(0),
frame_system::CheckWeight::<PolimecRuntime>::new(),
pallet_skip_feeless_payment::SkipCheckIfFeeless::<
PolimecRuntime,
pallet_asset_tx_payment::ChargeAssetTxPayment<PolimecRuntime>,
>::from(pallet_asset_tx_payment::ChargeAssetTxPayment::<PolimecRuntime>::from(0u64.into(), None)),
frame_metadata_hash_extension::CheckMetadataHash::<PolimecRuntime>::new_with_custom_hash([0u8; 32]),
);
// `InitialPayment` struct from pallet_asset_tx_payment doesn't implement Debug and PartialEq to compare to a specific Error or use assert_ok!
assert!(extra
.validate(
polimec_runtime::RuntimeOrigin::signed(who.clone()),
&paid_call,
&paid_call.get_dispatch_info(),
0,
extra.implicit().unwrap(),
&TxBaseImplication(()),
TransactionSource::External
)
.is_err());
assert!(extra
.clone()
.prepare(
(
(),
(),
(),
(),
(),
pallet_dispenser::extensions::Val::CheckNonce((who.clone(), 0)),
0,
Intermediate::Apply(Val::Charge { tip: 0, who: who.clone(), fee: 0 }),
(),
),
&polimec_runtime::RuntimeOrigin::signed(who.clone()),
&paid_call,
&paid_call.get_dispatch_info(),
0
)
.is_err());
let val = extra
.validate(
polimec_runtime::RuntimeOrigin::signed(who.clone()),
&free_call,
&free_call.get_dispatch_info(),
0,
extra.implicit().unwrap(),
&TxBaseImplication(()),
TransactionSource::External,
)
.unwrap();
assert!(extra
.prepare(val.1, &polimec_runtime::RuntimeOrigin::signed(who), &free_call, &free_call.get_dispatch_info(), 0)
.is_ok());
});
}
#[test]
fn dispenser_works_with_runtime_values() {
PolimecNet::execute_with(|| {
let who = PolimecAccountId::from(EMPTY_ACCOUNT);
let did = "kilt:did:tz:tz1K7fCz9QJtXv3J8Ud3Zvz7eQ6";
let bytes_did = did.as_bytes().to_vec();
let bounded_did: Did = bytes_did.try_into().unwrap();
let jwt = get_mock_jwt_with_cid(
who.clone(),
InvestorType::Retail,
bounded_did,
polimec_runtime::DispenserWhitelistedPolicy::get(),
);
PolimecBalances::force_set_balance(
PolimecOrigin::root(),
PolimecDispenser::dispense_account().into(),
1000 * PLMC,
)
.unwrap();
assert_ok!(PolimecDispenser::dispense(PolimecOrigin::signed(who.clone()), jwt));
assert_eq!(PolimecBalances::free_balance(&who), 700 * PLMC);
assert_eq!(
PolimecBalances::usable_balance(who.clone()),
<PolimecRuntime as pallet_dispenser::Config>::FreeDispenseAmount::get()
);
assert_eq!(
PolimecVesting::vesting_balance(&who),
Some(
<PolimecRuntime as pallet_dispenser::Config>::InitialDispenseAmount::get() -
<PolimecRuntime as pallet_dispenser::Config>::FreeDispenseAmount::get()
)
);
})
}