Skip to content

Commit

Permalink
Add type storage (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
corydickson authored Apr 23, 2024
1 parent 556d68f commit 16a1030
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
}

// An attestation for a given manifest
Expand All @@ -30,13 +32,16 @@ pub struct Attestation {
enum PrefixKeys {
Package,
Manifest,
Attestation
Attestation,
Types,
TypeList
}

pub type PackageName = String;
pub type Namespace = Vec<u8>;
pub type Releases = LookupMap<PackageName, Vec<Manifest>>;
pub type Attestations = Vec<Attestation>;
pub type Types = Vec<String>;

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
Expand All @@ -46,13 +51,17 @@ pub struct Contract {
pub packages: LookupMap<AccountId, Releases>,
// A signer can submit an attestation for a particular package already in the registry
pub attestations: LookupMap<AccountId, LookupMap<Namespace, Attestations>>,
pub compiled_types: LookupMap<Namespace, Types>,
pub type_list: LookupMap<String, u8>,
}

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)
}
}
}
Expand Down Expand Up @@ -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<String>,
// 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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -325,6 +349,7 @@ mod tests {
version.clone(),
content_type.clone(),
cid.clone(),
Vec::new(),
false
);
assert_eq!(
Expand All @@ -348,6 +373,7 @@ mod tests {
version.clone(),
content_type.clone(),
cid.clone(),
Vec::new(),
false
);

Expand All @@ -356,6 +382,7 @@ mod tests {
version.clone(),
content_type.clone(),
cid.clone(),
Vec::new(),
false
);

Expand All @@ -364,6 +391,7 @@ mod tests {
"0.0.2".to_string(),
content_type.clone(),
cid.clone(),
Vec::new(),
false
);

Expand Down Expand Up @@ -398,6 +426,7 @@ mod tests {
version.clone(),
content_type.clone(),
cid.clone(),
Vec::new(),
false
);

Expand Down Expand Up @@ -432,7 +461,8 @@ mod tests {
version.clone(),
content_type.clone(),
cid.clone(),
false
Vec::new(),
false,
);


Expand Down

0 comments on commit 16a1030

Please sign in to comment.