Skip to content

Commit eb46456

Browse files
saraswatpuneetJoeCap08055aramikmmattheworris
authored
Provider Context: Minimal Profile for Provider and Applications (#2549)
# Details [Design Doc](https://github.com/frequency-chain/frequency/blob/main/designdocs/provider_contexts.md) Provider Context allows provider to now register a minimal profile `ProviderRegistryEntry` and `ApplicationContext` for provider's default profile and various applications providers want to register. Providers which want to register multiple applications under single provider delegation but distributed across multiple app. Following issues were resolved as part of this work with following salient points: 1. Migration existing `ProviderToRegistryEntry` to host new `ProviderRegistryEntry` payload 2. Introduce new state `ProviderToApplicationRegistry` and `ApprovedLogos` storage maps. 3. CID's are computed in deterministic way = `base(cid_v1(sha_256(logo_bytes)))` where logo_bytes are .png image bytes. 4. ApprovedLogos ensure it matches CID and logoBytes but recomputing and checking on chain. Issues complete as part of `Provider Context` work - [x] #2523 - [x] #2524 - [x] #2525 - [x] #2528 - [x] #2526 - [x] #2577 - [x] #2578 Closes #2578 Closes #2577 # Discussion items - [x] Do we feature flag than base filter out extrinsics @JoeCap08055 comment [here](#2587 (comment)) # Checklist - [x] Updated Pallet Readme? - [x] Updated js/api-augment for Custom RPC APIs? - [x] Design doc(s) updated? - [x] Unit Tests added? - [x] e2e Tests added? - [x] Benchmarks added? - [x] Spec version incremented? --------- Co-authored-by: Joe Caputo <[email protected]> Co-authored-by: Aramik <[email protected]> Co-authored-by: Matthew Orris <[email protected]>
1 parent a53a905 commit eb46456

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+4748
-461
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,6 @@ jobs:
457457
- name: Extract and List Downloaded Binaries
458458
run: |
459459
download_dir=${{steps.download-binaries.outputs.download-path}}
460-
mv "${download_dir}"/artifacts*/* "${download_dir}"
461460
echo "Download dir: $download_dir"
462461
echo "Downloaded binaries: $(ls -l $download_dir)"
463462
- name: Move Binary to Target Dir
@@ -600,7 +599,6 @@ jobs:
600599
- name: Extract and List Downloaded Binaries
601600
run: |
602601
download_dir=${{steps.download-binaries.outputs.download-path}}
603-
mv "${download_dir}"/artifacts*/* "${download_dir}"
604602
echo "Download dir: $download_dir"
605603
echo "Downloaded binaries: $(ls -l $download_dir)"
606604
- name: Move Binary to Target Dir
@@ -664,7 +662,6 @@ jobs:
664662
- name: Rename and Move Binaries
665663
run: |
666664
download_dir=${{steps.download-binaries.outputs.download-path}}
667-
mv "${download_dir}"/artifacts*/* "${download_dir}"
668665
echo "Download dir: $download_dir"
669666
echo "Downloaded binaries: $(ls -l $download_dir)"
670667
mkdir -p ${{env.BIN_DIR}}

.github/workflows/verify-pr-commit.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,6 @@ jobs:
463463
- name: Extract and List Downloaded Binaries
464464
run: |
465465
download_dir=${{steps.download-binaries.outputs.download-path}}
466-
mv "${download_dir}"/artifacts*/* "${download_dir}"
467466
echo "Download dir: $download_dir"
468467
echo "Downloaded binaries: $(ls -l $download_dir)"
469468
- name: Set Binary Permissions
@@ -741,7 +740,6 @@ jobs:
741740
- name: List Downloaded Binaries
742741
run: |
743742
download_dir=${{steps.download-binaries.outputs.download-path}}
744-
mv "${download_dir}"/artifacts*/* "${download_dir}"
745743
echo "Download dir: $download_dir"
746744
echo "Downloaded binaries: $(ls -l $download_dir)"
747745
- name: Set Binary Permissions
@@ -859,7 +857,6 @@ jobs:
859857
- name: List Downloaded Binaries
860858
run: |
861859
download_dir=${{steps.download-binaries.outputs.download-path}}
862-
mv "${download_dir}"/artifacts*/* "${download_dir}"
863860
echo "Download dir: $download_dir"
864861
echo "Downloaded binaries: $(ls -l $download_dir)"
865862
- name: Download api-augment tarball

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ apache-avro = { version = "0.19.0", default-features = false }
2020
rand = "0.9.0"
2121
parking_lot = "0.12.1"
2222
lazy_static = { version = "1.5", features = ["spin_no_std"] }
23+
multibase = { version = "0.9", default-features = false }
24+
cid = { version = "0.11", default-features = false, features = ["alloc"] }
2325

2426
# substrate wasm
2527
parity-scale-codec = { version = "3.7.4", default-features = false }

common/primitives/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ sp-runtime-interface = { workspace = true }
3030
libsecp256k1 = { workspace = true, features = ["hmac"] }
3131
log = "0.4.22"
3232
lazy_static = { workspace = true }
33+
cid = { workspace = true }
3334

3435
[features]
3536
default = ['std']

common/primitives/src/cid.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use cid::Cid;
2+
use sp_io::hashing::sha2_256;
3+
use sp_runtime::Vec;
4+
5+
/// Multihash type for wrapping digests (support up to 64-byte digests)
6+
pub type Multihash = cid::multihash::Multihash<64>;
7+
8+
/// SHA2-256 multihash code
9+
const SHA2_256: u64 = 0x12;
10+
11+
/// Raw codec for CIDv1 (0x55)
12+
const RAW: u64 = 0x55;
13+
14+
/// Computes a CIDv1 (RAW + SHA2-256 multihash)
15+
pub fn compute_cid_v1(bytes: &[u8]) -> Option<Vec<u8>> {
16+
let digest = sha2_256(bytes);
17+
let mh = Multihash::wrap(SHA2_256, &digest).ok()?;
18+
let cid = Cid::new_v1(RAW, mh);
19+
Some(cid.to_bytes())
20+
}

common/primitives/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ pub mod benchmarks;
4444

4545
/// Signature support for ethereum
4646
pub mod signatures;
47+
48+
/// IPFS CIDv1 related utilities
49+
pub mod cid;

common/primitives/src/msa.rs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::fmt::Debug;
12
use frame_support::{dispatch::DispatchResult, traits::Get, BoundedBTreeMap, BoundedVec};
23
use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, EncodeLike, Error, MaxEncodedLen};
34
use scale_info::TypeInfo;
@@ -12,6 +13,13 @@ use alloc::vec::Vec;
1213

1314
pub use crate::schema::SchemaId;
1415

16+
/// ApplicationIndex type
17+
pub type ApplicationIndex = u16;
18+
19+
/// ApplicationContext is type of ProviderRegistryEntry
20+
pub type ApplicationContext<NameSize, LangSize, CidSize, MaxLocaleCount> =
21+
ProviderRegistryEntry<NameSize, LangSize, CidSize, MaxLocaleCount>;
22+
1523
/// Message Source Id or msaId is the unique identifier for Message Source Accounts
1624
pub type MessageSourceId = u64;
1725

@@ -202,14 +210,72 @@ impl From<ProviderId> for MessageSourceId {
202210

203211
/// This is the metadata associated with a provider. As of now it is just a
204212
/// name, but it will likely be expanded in the future
205-
#[derive(MaxEncodedLen, TypeInfo, Debug, Clone, Decode, Encode, PartialEq, Eq)]
206-
#[scale_info(skip_type_params(T))]
207-
pub struct ProviderRegistryEntry<T>
213+
/// Generic over size constraints to be used in common types.
214+
#[derive(
215+
MaxEncodedLen, TypeInfo, Clone, Debug, Decode, DecodeWithMemTracking, Encode, PartialEq, Eq,
216+
)]
217+
#[scale_info(skip_type_params(NameSize, LangSize, CidSize, MaxLocaleCount))]
218+
#[codec(mel_bound(
219+
NameSize: Get<u32> + Debug + PartialEq + Eq,
220+
LangSize: Get<u32> + Debug + PartialEq + Eq,
221+
CidSize: Get<u32> + Debug + PartialEq + Eq,
222+
MaxLocaleCount: Get<u32> + Debug + PartialEq + Eq,
223+
))]
224+
pub struct ProviderRegistryEntry<
225+
NameSize: Get<u32> + Debug + PartialEq + Eq,
226+
LangSize: Get<u32> + Debug + PartialEq + Eq,
227+
CidSize: Get<u32> + Debug + PartialEq + Eq,
228+
MaxLocaleCount: Get<u32> + Debug + PartialEq + Eq,
229+
> {
230+
/// Default name (display name) of the provider or application.
231+
pub default_name: BoundedVec<u8, NameSize>,
232+
233+
/// Localized names keyed by BCP 47 language code (e.g., "en-US").
234+
pub localized_names:
235+
BoundedBTreeMap<BoundedVec<u8, LangSize>, BoundedVec<u8, NameSize>, MaxLocaleCount>,
236+
237+
/// Default logo (PNG 250x100) content-addressed CID (e.g., IPFS hash).
238+
pub default_logo_250_100_png_cid: BoundedVec<u8, CidSize>,
239+
240+
/// Localized logo CIDs keyed by BCP 47 language code.
241+
pub localized_logo_250_100_png_cids:
242+
BoundedBTreeMap<BoundedVec<u8, LangSize>, BoundedVec<u8, CidSize>, MaxLocaleCount>,
243+
}
244+
245+
impl<NameSize, LangSize, CidSize, MaxLocaleCount> Default
246+
for ProviderRegistryEntry<NameSize, LangSize, CidSize, MaxLocaleCount>
208247
where
209-
T: Get<u32>,
248+
NameSize: Get<u32> + Debug + PartialEq + Eq,
249+
LangSize: Get<u32> + Debug + PartialEq + Eq,
250+
CidSize: Get<u32> + Debug + PartialEq + Eq,
251+
MaxLocaleCount: Get<u32> + Debug + PartialEq + Eq,
210252
{
211-
/// The provider's name
212-
pub provider_name: BoundedVec<u8, T>,
253+
fn default() -> Self {
254+
Self {
255+
default_name: BoundedVec::default(),
256+
localized_names: BoundedBTreeMap::default(),
257+
default_logo_250_100_png_cid: BoundedVec::default(),
258+
localized_logo_250_100_png_cids: BoundedBTreeMap::default(),
259+
}
260+
}
261+
}
262+
263+
/// RPC Response for getting ProviderApplicationContext
264+
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
265+
#[derive(TypeInfo, Debug, Clone, Decode, Encode, PartialEq, Default)]
266+
pub struct ProviderApplicationContext {
267+
/// The default name associated with this entry
268+
pub default_name: Vec<u8>,
269+
/// The provider associated with the `key`
270+
pub provider_id: ProviderId,
271+
/// The default Application/Provider logo
272+
pub default_logo_250_100_png_bytes: Option<Vec<u8>>,
273+
/// The optional application id
274+
pub application_id: Option<ApplicationIndex>,
275+
/// The optional localized name
276+
pub localized_name: Option<Vec<u8>>,
277+
/// The optional localized logo bytes
278+
pub localized_logo_250_100_png_bytes: Option<Vec<u8>>,
213279
}
214280

215281
/// The pointer value for the Signature Registry

common/primitives/src/utils.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ pub fn wrap_binary_data(data: Vec<u8>) -> Vec<u8> {
159159
encapsuled
160160
}
161161

162+
/// This is useful for testing and benchmarks and should not be used for any
163+
/// cryptographically secure operation
164+
#[cfg(any(test, feature = "runtime-benchmarks"))]
165+
pub struct XorRng {
166+
state: u64,
167+
}
168+
169+
#[cfg(any(test, feature = "runtime-benchmarks"))]
170+
impl XorRng {
171+
/// Creates Rnd from the seed
172+
pub fn new(seed: u64) -> Self {
173+
Self { state: if seed == 0 { 1 } else { seed } }
174+
}
175+
176+
/// Generates the next u8
177+
pub fn gen_u8(&mut self) -> u8 {
178+
// XorShift64
179+
self.state ^= self.state << 13;
180+
self.state ^= self.state >> 7;
181+
self.state ^= self.state << 17;
182+
self.state as u8
183+
}
184+
}
185+
162186
#[cfg(test)]
163187
mod tests {
164188
use super::*;

designdocs/provider_contexts.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Proposed are the following changes:
216216

217217
To support the new structure, a storage migration will be required to:
218218

219-
- Migrate existing `ProviderToRegistryEntry` to the new format with default values for localized names and logos.
219+
- Migrate existing `ProviderToRegistryEntryV2` to the new format with default values for localized names and logos.
220220

221221
### **Runtime RPC Changes** <a id='rpc'></a>
222222

@@ -326,7 +326,7 @@ The Wallet would then:
326326
1. Verify the SIWF Signed Request.
327327
2. Lookup the Provider via the `publicKey` in the SIWF Signed Request.
328328
3. If any, fetch the `ApplicationIndex` from Frequency.
329-
4. If present, fetch the `ProviderToApplicationRegistryEntry` for that (ProviderId, ApplicationId) pair. Otherwise, fetch the default `ProviderToRegistryEntry` for the ProviderId
329+
4. If present, fetch the `ProviderToApplicationRegistryEntry` for that (ProviderId, ApplicationId) pair. Otherwise, fetch the default `ProviderToRegistryEntryV2` for the ProviderId
330330
5. Display the information to the user to help them know who they are authorizing.
331331
6. Allow the user to continue the login process.
332332

0 commit comments

Comments
 (0)