From 1ae6a1e885e0168a46a53628696c159b935fbcbe Mon Sep 17 00:00:00 2001 From: Cory Dickson Date: Tue, 23 Apr 2024 16:24:23 -0400 Subject: [PATCH 1/3] Add type storage --- src/lib.rs | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8bbe7b6..669d2ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,10 +10,12 @@ use near_sdk::{ // Represents the content being stored into the storage map #[derive(BorshSerialize, BorshDeserialize, Clone, Debug)] #[borsh(crate = "near_sdk::borsh")] + pub struct Manifest { pub version: String, pub cid: String, - pub content_type: String + pub content_type: String, + pub types: Vec } // An attestation for a given manifest @@ -30,13 +32,16 @@ pub struct Attestation { enum PrefixKeys { Package, Manifest, - Attestation + Attestation, + Types, + TypeList } pub type PackageName = String; pub type Namespace = Vec; pub type Releases = LookupMap>; pub type Attestations = Vec; +pub type Types = Vec; #[near_bindgen] #[derive(BorshDeserialize, BorshSerialize)] @@ -46,13 +51,17 @@ pub struct Contract { pub packages: LookupMap, // A signer can submit an attestation for a particular package already in the registry pub attestations: LookupMap>, + pub compiled_types: LookupMap, + pub type_list: LookupMap, } impl Default for Contract { fn default() -> Self { Self { packages: LookupMap::new(PrefixKeys::Package), - attestations: LookupMap::new(PrefixKeys::Attestation) + attestations: LookupMap::new(PrefixKeys::Attestation), + compiled_types: LookupMap::new(PrefixKeys::Types), + type_list: LookupMap::new(PrefixKeys::TypeList) } } } @@ -105,13 +114,16 @@ impl Contract { content_type: String, // The IPFS content id that contains the package manifest cid: String, + // A list of named types in the package + mut types: Vec, // If a contract is calling this function the reference key can be the contract account if true or the signers account when false is_contract: bool, ) { let manifest = Manifest { version, content_type, - cid + cid, + types: types.clone() }; let mut author = near_sdk::env::signer_account_id(); @@ -141,6 +153,18 @@ impl Contract { versions.push(manifest); manifests.insert(&package_name, &versions); + + let namespace = Self::generate_key(author, package_name); + + if !self.compiled_types.contains_key(&namespace) { + self.compiled_types.insert(&namespace, &types); + } + + else { + let mut compiled_types = self.compiled_types.get(&namespace).unwrap(); + compiled_types.append(&mut types) + } + } // Retrieves the last manifest for a particular package @@ -325,6 +349,7 @@ mod tests { version.clone(), content_type.clone(), cid.clone(), + Vec::new(), false ); assert_eq!( @@ -348,6 +373,7 @@ mod tests { version.clone(), content_type.clone(), cid.clone(), + Vec::new(), false ); @@ -356,6 +382,7 @@ mod tests { version.clone(), content_type.clone(), cid.clone(), + Vec::new(), false ); @@ -364,6 +391,7 @@ mod tests { "0.0.2".to_string(), content_type.clone(), cid.clone(), + Vec::new(), false ); @@ -398,6 +426,7 @@ mod tests { version.clone(), content_type.clone(), cid.clone(), + Vec::new(), false ); @@ -432,7 +461,8 @@ mod tests { version.clone(), content_type.clone(), cid.clone(), - false + Vec::new(), + false, ); From 2729f3829db8a569b691f18a40234151c7fc37e9 Mon Sep 17 00:00:00 2001 From: Cory Dickson Date: Sun, 28 Apr 2024 22:10:12 -0400 Subject: [PATCH 2/3] Add attest to type --- src/lib.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 669d2ee..3c785e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; use near_sdk::env::log_str; use near_sdk::serde::{Serialize, Deserialize}; -use near_sdk::collections::LookupMap; +use near_sdk::collections::{LookupMap, UnorderedMap, Vector }; use near_sdk::near_bindgen; use near_sdk::{ AccountId, BorshStorageKey, PublicKey, require @@ -52,7 +52,7 @@ pub struct Contract { // A signer can submit an attestation for a particular package already in the registry pub attestations: LookupMap>, pub compiled_types: LookupMap, - pub type_list: LookupMap, + pub type_list: UnorderedMap, } impl Default for Contract { @@ -61,7 +61,7 @@ impl Default for Contract { packages: LookupMap::new(PrefixKeys::Package), attestations: LookupMap::new(PrefixKeys::Attestation), compiled_types: LookupMap::new(PrefixKeys::Types), - type_list: LookupMap::new(PrefixKeys::TypeList) + type_list: UnorderedMap::new(PrefixKeys::TypeList) } } } @@ -319,6 +319,23 @@ impl Contract { return at[index].clone(); } + + pub fn attest_to_type( + &mut self, + // An account ID of the author who published the manifest + author: AccountId, + // Name of a type in a package + type_name: String, + ) { + if self.attestations.contains_key(&author) { + let count: u8 = match self.type_list.get(&type_name) { + Some(v) => v + 1u8, + None => 1u8, + }; + + self.type_list.insert(&type_name, &count); + } + } } #[cfg(all(test, not(target_arch = "wasm32")))] From 32be801b6357e5152860b4386fd9b644a9c9dd6e Mon Sep 17 00:00:00 2001 From: Cory Dickson Date: Sun, 28 Apr 2024 22:11:52 -0400 Subject: [PATCH 3/3] Remove unused import --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3c785e6..ff6d9e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; use near_sdk::env::log_str; use near_sdk::serde::{Serialize, Deserialize}; -use near_sdk::collections::{LookupMap, UnorderedMap, Vector }; +use near_sdk::collections::{LookupMap, UnorderedMap}; use near_sdk::near_bindgen; use near_sdk::{ AccountId, BorshStorageKey, PublicKey, require