Skip to content

Commit 642c25e

Browse files
authored
registry: Split registry creation for native and cyra (#620)
Signed-off-by: Shreevatsa N <vatsa@dhiway.com>
1 parent dbc18da commit 642c25e

File tree

3 files changed

+142
-68
lines changed

3 files changed

+142
-68
lines changed

pallets/registry/src/lib.rs

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use cord_uri::{EntryTypeOf, EventStamp, Identifier, RegistryIdentifierCheck, Ss5
3232
use frame_support::dispatch::DispatchResult;
3333
use frame_support::BoundedVec;
3434
use frame_system::pallet_prelude::BlockNumberFor;
35+
use frame_support::traits::ConstU32;
3536
use frame_system::WeightInfo;
3637
use pallet_profile::ProfileIdOf;
3738

@@ -50,6 +51,9 @@ pub type RegistryIdentifierOf = Ss58Identifier;
5051
pub type HashOf<T> = <T as frame_system::Config>::Hash;
5152
pub(crate) type CordAccountOf<T> = <T as frame_system::Config>::AccountId;
5253

54+
pub type DocIdOf = BoundedVec<u8, ConstU32<64>>;
55+
pub type DocNodeIdOf = BoundedVec<u8, ConstU32<64>>;
56+
5357
#[frame_support::pallet]
5458
pub mod pallet {
5559
use super::*;
@@ -90,7 +94,7 @@ pub mod pallet {
9094
_,
9195
Blake2_128Concat,
9296
RegistryIdentifierOf,
93-
RegistryDetails<HashOf<T>, Status>,
97+
RegistryDetails<HashOf<T>, Status, DocIdOf, DocNodeIdOf>,
9498
OptionQuery,
9599
>;
96100

@@ -124,6 +128,11 @@ pub mod pallet {
124128
creator: CordAccountOf<T>,
125129
profile_id: ProfileIdOf
126130
},
131+
RegistryStoreCreated {
132+
registry: RegistryIdentifierOf,
133+
creator: CordAccountOf<T>,
134+
profile_id: ProfileIdOf
135+
},
127136
RegistryUpdated {
128137
registry: RegistryIdentifierOf,
129138
authority: CordAccountOf<T>,
@@ -274,18 +283,14 @@ pub mod pallet {
274283

275284
/// Creates a new registry.
276285
///
277-
/// Initializes a new registry with the provided transaction hash and optional metadata
278-
/// (document ID, author, node ID). The creator must have a valid profile. The registry is
279-
/// created via the `registry::create_registry` function, assigned a unique SS58 identifier,
280-
/// and marked as active.
286+
/// Initializes a new registry with the provided transaction hash and a optional blob.
287+
/// The creator must have a valid profile. The registry is created via the `registry::create_registry`
288+
/// function, assigned a unique SS58 identifier, and marked as active.
281289
///
282290
/// # Arguments
283291
/// * `origin` - The signed account creating the registry.
284292
/// * `tx_hash` - The hash of the registry’s content.
285293
/// * `_blob` - Optional data associated with the registry.
286-
/// * `doc_id` - Optional document identifier.
287-
/// * `doc_author_id` - Optional account ID of the document author.
288-
/// * `doc_node_id` - Optional document node identifier.
289294
///
290295
/// # Errors
291296
/// * `InvalidIdentifierLength` - If the generated registry ID is invalid.
@@ -302,9 +307,6 @@ pub mod pallet {
302307
origin: OriginFor<T>,
303308
tx_hash: HashOf<T>,
304309
_blob: Option<RegistryBlobOf<T>>,
305-
doc_id: Option<Vec<u8>>,
306-
doc_author_id: Option<CordAccountOf<T>>,
307-
doc_node_id: Option<Vec<u8>>,
308310
) -> DispatchResult {
309311
let creator = ensure_signed(origin)?;
310312

@@ -313,16 +315,69 @@ pub mod pallet {
313315
)
314316
.map_err(<pallet_profile::Error<T>>::from)?;
315317

316-
let doc_author_profile_id = if let Some(author) = doc_author_id.clone() {
317-
Some(
318-
pallet_profile::Pallet::<T>::get_profile_id(&author)
319-
.map_err(<pallet_profile::Error<T>>::from)?
320-
)
321-
} else {
322-
None
323-
};
324-
325318
let registry_id = registry::create_registry::<T>(
319+
tx_hash,
320+
profile_id.clone(),
321+
)?;
322+
323+
Self::deposit_event(
324+
Event::RegistryCreated {
325+
registry: registry_id,
326+
creator, profile_id,
327+
}
328+
);
329+
330+
Ok(())
331+
}
332+
333+
334+
/// Creates a new registry store with cyra based credentials.
335+
///
336+
/// Initializes a new registry store with the provided transaction hash and optional metadata
337+
/// (document ID, author, node ID). The creator must have a valid profile. The registry is
338+
/// created via the `registry::create_registry_store` function, assigned a unique SS58 identifier,
339+
/// and marked as active.
340+
///
341+
/// The key difference between create and create-store is that create-store is to map the registry
342+
/// data present in Cyra into Cord.
343+
///
344+
/// # Arguments
345+
/// * `origin` - The signed account creating the registry.
346+
/// * `tx_hash` - The hash of the registry’s content.
347+
/// * `_blob` - Data associated with the registry.
348+
/// * `doc_id` - Document identifier.
349+
/// * `doc_author_id` - Account ID of the document author.
350+
/// * `doc_node_id` - Document node identifier.
351+
///
352+
/// # Errors
353+
/// * `InvalidIdentifierLength` - If the generated registry ID is invalid.
354+
/// * `RegistryAlreadyExists` - If a registry with the same ID exists.
355+
/// * `pallet_profile::Error` - If the creator’s or author’s profile is invalid.
356+
///
357+
/// # Events
358+
/// * `RegistryStoreCreated` - Emitted with `registry`, `creator`, `profile_id`.
359+
///
360+
/// ```
361+
#[pallet::call_index(3)]
362+
#[pallet::weight({10_000})]
363+
pub fn create_store(
364+
origin: OriginFor<T>,
365+
tx_hash: HashOf<T>,
366+
doc_id: Vec<u8>,
367+
doc_author_id: CordAccountOf<T>,
368+
doc_node_id: Vec<u8>,
369+
) -> DispatchResult {
370+
let creator = ensure_signed(origin)?;
371+
372+
let profile_id = pallet_profile::Pallet::<T>::get_profile_id(
373+
&creator
374+
)
375+
.map_err(<pallet_profile::Error<T>>::from)?;
376+
377+
let doc_author_profile_id = pallet_profile::Pallet::<T>::get_profile_id(&doc_author_id)
378+
.map_err(<pallet_profile::Error<T>>::from)?;
379+
380+
let registry_id = registry::create_registry_store::<T>(
326381
tx_hash,
327382
doc_id,
328383
doc_author_profile_id,
@@ -331,7 +386,7 @@ pub mod pallet {
331386
)?;
332387

333388
Self::deposit_event(
334-
Event::RegistryCreated {
389+
Event::RegistryStoreCreated {
335390
registry: registry_id,
336391
creator, profile_id,
337392
}
@@ -360,7 +415,7 @@ pub mod pallet {
360415
/// * `RegistryArchived` - Emitted with `registry`, `authority`, `authority_profile_id`.
361416
///
362417
/// ```
363-
#[pallet::call_index(3)]
418+
#[pallet::call_index(4)]
364419
#[pallet::weight({10_000})]
365420
pub fn archive(
366421
origin: OriginFor<T>,
@@ -404,7 +459,7 @@ pub mod pallet {
404459
/// * `RegistryRestored` - Emitted with `registry`, `authority`, `authority_profile_id`.
405460
///
406461
/// ```
407-
#[pallet::call_index(4)]
462+
#[pallet::call_index(5)]
408463
#[pallet::weight({10_000})]
409464
pub fn restore(
410465
origin: OriginFor<T>,
@@ -448,7 +503,7 @@ pub mod pallet {
448503
/// * `RegistryUpdated` - Emitted with `registry`, `authority` (new author), `authority_profile_id`.
449504
///
450505
/// ```
451-
#[pallet::call_index(5)]
506+
#[pallet::call_index(6)]
452507
#[pallet::weight({10_000})]
453508
pub fn update_author(
454509
origin: OriginFor<T>,
@@ -502,7 +557,7 @@ pub mod pallet {
502557
/// * `RegistryUpdated` - Emitted with `registry`, `authority` (new creator), `authority_profile_id`.
503558
///
504559
/// ```
505-
#[pallet::call_index(6)]
560+
#[pallet::call_index(7)]
506561
#[pallet::weight({10_000})]
507562
pub fn update_creator(
508563
origin: OriginFor<T>,
@@ -556,7 +611,7 @@ pub mod pallet {
556611
/// * `RegistryUpdated` - Emitted with `registry`, `authority`, `authority_profile_id`.
557612
///
558613
/// ```
559-
#[pallet::call_index(7)]
614+
#[pallet::call_index(8)]
560615
#[pallet::weight({10_000})]
561616
pub fn update_registry_hash(
562617
origin: OriginFor<T>,

pallets/registry/src/registry.rs

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,15 @@ use crate::{
2828
RegistryDetails, RegistryIdentifierOf, Status,
2929
};
3030

31-
pub fn push_option(buf: &mut Vec<u8>, field: Option<&[u8]>) {
32-
if let Some(bytes) = field {
33-
buf.push(1u8);
34-
buf.extend_from_slice(bytes);
35-
} else {
36-
buf.push(0u8);
37-
}
38-
}
3931

4032
/// Create a new registry.
4133
pub fn create_registry<T: crate::Config>(
4234
tx_hash: HashOf<T>,
43-
doc_id: Option<Vec<u8>>,
44-
doc_author_profile_id: Option<ProfileIdOf>,
45-
doc_node_id: Option<Vec<u8>>,
4635
profile_id: ProfileIdOf,
4736
) -> Result<RegistryIdentifierOf, sp_runtime::DispatchError> {
48-
let bounded_doc_id = doc_id
49-
.map(|v| v.try_into())
50-
.transpose()
51-
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
52-
let bounded_doc_node_id = doc_node_id
53-
.map(|v| v.try_into())
54-
.transpose()
55-
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
56-
let encoded_doc_author = doc_author_profile_id.as_ref().map(|author| author.encode());
5737

5838
let mut data = Vec::with_capacity(256);
5939
data.extend_from_slice(tx_hash.as_ref());
60-
push_option(
61-
&mut data,
62-
bounded_doc_id.as_ref().map(|v: &BoundedVec<u8, ConstU32<64>>| v.as_slice()),
63-
);
64-
push_option(&mut data, encoded_doc_author.as_ref().map(|v| v.as_slice()));
65-
push_option(
66-
&mut data,
67-
bounded_doc_node_id
68-
.as_ref()
69-
.map(|v: &BoundedVec<u8, ConstU32<64>>| v.as_slice()),
70-
);
7140
data.extend_from_slice(&profile_id.encode());
7241

7342
let digest = T::Hashing::hash(&data);
@@ -82,23 +51,73 @@ pub fn create_registry<T: crate::Config>(
8251
let details = RegistryDetails {
8352
creator: profile_id.clone(),
8453
tx_hash,
85-
doc_id: bounded_doc_id,
86-
doc_author_profile_id: doc_author_profile_id.clone(),
87-
doc_node_id: bounded_doc_node_id,
54+
doc_id: None,
55+
doc_author_profile_id: None,
56+
doc_node_id: None,
8857
status: Status::Active,
8958
};
9059

9160
Pallet::<T>::record_activity(&registry_id, b"RegistryCreated")?;
9261
Registries::<T>::insert(&registry_id, details);
9362
Delegates::<T>::insert(&registry_id, &profile_id, Permissions::all());
63+
64+
Ok(registry_id)
65+
}
9466

95-
if let Some(author_profile_id) = doc_author_profile_id {
96-
Delegates::<T>::insert(&registry_id, &author_profile_id, Permissions::ENTRY);
97-
}
67+
68+
/// Create a new registry store.
69+
pub fn create_registry_store<T: crate::Config>(
70+
tx_hash: HashOf<T>,
71+
doc_id: Vec<u8>,
72+
doc_author_profile_id: ProfileIdOf,
73+
doc_node_id: Vec<u8>,
74+
profile_id: ProfileIdOf,
75+
) -> Result<RegistryIdentifierOf, sp_runtime::DispatchError> {
76+
let bounded_doc_id: DocIdOf = doc_id
77+
.try_into()
78+
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
79+
let bounded_doc_node_id: DocNodeIdOf = doc_node_id
80+
.try_into()
81+
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
82+
83+
let mut data = Vec::with_capacity(256);
84+
data.extend_from_slice(tx_hash.as_ref());
85+
86+
data.extend_from_slice(&bounded_doc_id.encode());
87+
data.extend_from_slice(&bounded_doc_node_id.encode());
88+
89+
data.extend_from_slice(&doc_author_profile_id.encode());
90+
data.extend_from_slice(&profile_id.encode());
91+
92+
let digest = T::Hashing::hash(&data);
93+
let pallet_name = <crate::pallet::Pallet<T> as frame_support::traits::PalletInfoAccess>::name();
94+
95+
let registry_id =
96+
<cord_uri::Pallet<T> as Identifier>::build(&(digest).encode()[..], pallet_name)
97+
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
98+
99+
ensure!(!Registries::<T>::contains_key(&registry_id), Error::<T>::RegistryAlreadyExists);
100+
101+
let details = RegistryDetails {
102+
creator: profile_id.clone(),
103+
tx_hash,
104+
doc_id: Some(bounded_doc_id),
105+
doc_author_profile_id: Some(doc_author_profile_id.clone()),
106+
doc_node_id: Some(bounded_doc_node_id),
107+
status: Status::Active,
108+
};
109+
110+
Pallet::<T>::record_activity(&registry_id, b"RegistryStoreCreated")?;
111+
Registries::<T>::insert(&registry_id, details);
112+
Delegates::<T>::insert(&registry_id, &profile_id, Permissions::all());
113+
114+
/* Add the doc_author as a delegate for having permission to create entry */
115+
Delegates::<T>::insert(&registry_id, &doc_author_profile_id, Permissions::ENTRY);
98116

99117
Ok(registry_id)
100118
}
101119

120+
102121
/// Update a existing registry.
103122
pub fn update_registry_hash<T: crate::Config>(
104123
registry_id: &RegistryIdentifierOf,
@@ -162,6 +181,7 @@ pub fn restore_registry<T: crate::Config>(
162181
Ok(())
163182
}
164183

184+
165185
/// Update the document author for a registry.
166186
pub fn update_registry_author<T: crate::Config>(
167187
registry_id: &RegistryIdentifierOf,
@@ -192,6 +212,7 @@ pub fn update_registry_author<T: crate::Config>(
192212
Ok(())
193213
}
194214

215+
195216
/// Update registry creator
196217
pub fn update_registry_creator<T: crate::Config>(
197218
registry_id: &RegistryIdentifierOf,

pallets/registry/src/types.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
use super::*;
2020
use bitflags::bitflags;
2121
use codec::{Decode, Encode, MaxEncodedLen};
22-
use frame_support::traits::ConstU32;
23-
use frame_support::BoundedVec;
2422
use scale_info::TypeInfo;
2523
use sp_runtime::RuntimeDebug;
2624

@@ -89,17 +87,17 @@ pub enum Status {
8987
}
9088

9189
#[derive(Encode, Decode, Clone, MaxEncodedLen, RuntimeDebug, PartialEq, Eq, TypeInfo)]
92-
pub struct RegistryDetails<Hash, Status> {
90+
pub struct RegistryDetails<Hash, Status, DocIdOf, DocNodeIdOf> {
9391
/// The identity of the account (profile) that created/ owns the registry.
9492
pub creator: ProfileIdOf,
9593
/// The transaction hash associated with the document.
9694
pub tx_hash: Hash,
9795
/// Optionally, the document identifier as a bounded vector.
98-
pub doc_id: Option<BoundedVec<u8, ConstU32<64>>>,
96+
pub doc_id: Option<DocIdOf>,
9997
/// Optionally, the identity account (profile) that created (authored) the document.
10098
pub doc_author_profile_id: Option<ProfileIdOf>,
10199
/// Optionally, the node identifier as a bounded vector.
102-
pub doc_node_id: Option<BoundedVec<u8, ConstU32<64>>>,
100+
pub doc_node_id: Option<DocNodeIdOf>,
103101
/// The status of the registry entry.
104102
pub status: Status,
105103
}

0 commit comments

Comments
 (0)