Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(registry): is_community_verified #73

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 75 additions & 20 deletions contracts/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ pub struct Contract {
pub(crate) next_token_ids: LookupMap<IssuerId, TokenId>,
pub(crate) next_issuer_id: IssuerId,

/// tuple of (required issuer for IAH, [required list of classes for IAH])
/// represents mandatory requirements to be verified as human by using is_human and is_human_call methods.
/// tuple of (required issuer, [required list of classes]) that represents mandatory
/// requirements to be verified as human for `is_human` and `is_human_call` methods.
pub(crate) iah_sbts: (AccountId, Vec<ClassId>),
/// Class Set that that represents mandatory requirements to be community verified for using
/// `is_community_verified` and `is_community_verified_call` methods.
pub(crate) community_verified_set: ClassSet,
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
}

// Implement the contract structure
Expand All @@ -65,6 +68,7 @@ impl Contract {
authority: AccountId,
iah_issuer: AccountId,
iah_classes: Vec<ClassId>,
community_verified_set: ClassSet,
authorized_flaggers: Vec<AccountId>,
) -> Self {
require!(
Expand All @@ -90,6 +94,7 @@ impl Contract {
StorageKey::AdminsFlagged,
Some(&authorized_flaggers),
),
community_verified_set,
};
contract._add_sbt_issuer(&iah_issuer);
contract
Expand All @@ -109,6 +114,11 @@ impl Contract {
vec![self.iah_sbts.clone()]
}

/// Returns Community Verified class set.
pub fn community_verified_class_set(&self) -> ClassSet {
self.community_verified_set.clone()
}

#[inline]
fn _is_banned(&self, account: &AccountId) -> bool {
self.banlist.contains(account)
Expand All @@ -123,35 +133,80 @@ impl Contract {
/// Otherwise returns list of SBTs (identifed by issuer and list of token IDs) proving
/// the `account` humanity.
pub fn is_human(&self, account: AccountId) -> SBTs {
self._is_human(&account)
self._is_human(&account, false)
}

fn _is_human(&self, account: &AccountId) -> SBTs {
if self._is_banned(&account) {
return vec![];
fn _is_human(&self, account: &AccountId, no_skip_ban: bool) -> SBTs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need no_skip_ban ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can put conditions in one if

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to not repeat that check in case we are coming from is_community_verified

if no_skip_ban {
if self.flagged.get(&account) == Some(AccountFlag::Blacklisted)
|| self._is_banned(&account)
{
return vec![];
}
}
if let Some(AccountFlag::Blacklisted) = self.flagged.get(&account) {
return vec![];

let proof = self._list_sbts_by_classes(account, &self.iah_sbts.0, &self.iah_sbts.1);
vec![(self.iah_sbts.0.clone(), proof)]
}

/// Returns tuple of community_verified proof and IAH proof.
/// If `is_human` is false, then the `is_human` check is skipped and empty list is
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
/// returned. Otherwise `is_human` is assumed to be required and if the IAH check fails,
/// the community verification is skipped (so two empty lists are returned).
pub fn is_community_verified(&self, account: AccountId, is_human: bool) -> (SBTs, SBTs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need the is human here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid is_human call

self._is_community_verified(&account, is_human)
}

fn _is_community_verified(&self, account: &AccountId, is_human: bool) -> (SBTs, SBTs) {
if self.flagged.get(&account) == Some(AccountFlag::Blacklisted) || self._is_banned(&account)
{
return (vec![], vec![]);
}
let issuer = Some(self.iah_sbts.0.clone());
let mut proof: Vec<TokenId> = Vec::new();
// check if user has tokens from all classes
for cls in &self.iah_sbts.1 {
let iah_proof = if is_human {
let iah_proof = self._is_human(account, true);
// iah is required, but user is not human. Early return.
if iah_proof.is_empty() {
return (vec![], iah_proof);
}
iah_proof
} else {
vec![]
};

let mut proof: SBTs = Vec::new();
for (issuer, classes) in &self.community_verified_set {
let issuer_proof = self._list_sbts_by_classes(account, issuer, classes);
if issuer_proof.is_empty() {
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
return (vec![], iah_proof);
}
proof.push((issuer.clone(), issuer_proof));
}
(proof, iah_proof)
}

fn _list_sbts_by_classes(
&self,
acc: &AccountId,
issuer: &AccountId,
cls: &Vec<ClassId>,
) -> Vec<TokenId> {
let mut cls_sbts: Vec<TokenId> = Vec::new();
for c in cls {
let tokens = self.sbt_tokens_by_owner(
account.clone(),
issuer.clone(),
Some(*cls),
acc.clone(),
Some(issuer.clone()),
Some(*c),
Some(1),
None,
);
// we need to check class, because the query can return a "next" token if a user
// doesn't have the token of requested class.
if !(tokens.len() > 0 && tokens[0].1[0].metadata.class == *cls) {
if !(tokens.len() > 0 && tokens[0].1[0].metadata.class == *c) {
return vec![];
}
proof.push(tokens[0].1[0].token)
cls_sbts.push(tokens[0].1[0].token)
}
vec![(self.iah_sbts.0.clone(), proof)]
cls_sbts
}

//
Expand All @@ -168,7 +223,7 @@ impl Contract {
let issuer = &env::predecessor_account_id();
for ts in &token_spec {
require!(
!self._is_human(&ts.0).is_empty(),
!self._is_human(&ts.0, false).is_empty(),
format!("{} is not a human", &ts.0)
);
}
Expand Down Expand Up @@ -291,7 +346,7 @@ impl Contract {
#[payable]
pub fn is_human_call(&mut self, ctr: AccountId, function: String, payload: String) -> Promise {
let caller = env::predecessor_account_id();
let iah_proof = self._is_human(&caller);
let iah_proof = self._is_human(&caller, false);
require!(!iah_proof.is_empty(), "caller not a human");

let args = IsHumanCallbackArgs {
Expand Down
4 changes: 3 additions & 1 deletion contracts/registry/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ pub struct OldState {
impl Contract {
#[private]
#[init(ignore_state)]
pub fn migrate(authorized_flaggers: Vec<AccountId>) -> Self {
pub fn migrate(community_verified_set: ClassSet, authorized_flaggers: Vec<AccountId>) -> Self {
let old_state: OldState = env::state_read().expect("failed");
// new field in the smart contract :
// + flagged: LookupMap<AccountId, AccountFlag>
// + admins_flagged: LazyOption<Vec<AccountId>>
// + community_verified_set

Self {
authority: old_state.authority.clone(),
Expand All @@ -47,6 +48,7 @@ impl Contract {
StorageKey::AdminsFlagged,
Some(&authorized_flaggers),
),
community_verified_set,
}
}
}
2 changes: 1 addition & 1 deletion contracts/registry/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum StorageKey {
AdminsFlagged,
}

#[derive(BorshSerialize, BorshDeserialize, BorshStorageKey, Serialize, Deserialize)]
#[derive(BorshSerialize, BorshDeserialize, BorshStorageKey, Serialize, Deserialize, PartialEq)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug))]
pub enum AccountFlag {
Expand Down
Loading