Skip to content

Commit

Permalink
zkp verify
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Nov 18, 2024
1 parent 45065b2 commit ade97e7
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
159 changes: 159 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ jsonrpsee = { version = "0.22", features = ["client-core", "macros", "server"] }
sc-rpc = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }
sp-blockchain = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }
hex-literal = { version = " 0.4.1" }

risc0-zkvm = { version = "0.19.1", default-features = false }
serde_json = { version = "1", default-features = false, features = ["alloc"] }
## New end

parity-scale-codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [
Expand Down
4 changes: 4 additions & 0 deletions custom-pallets/anonymous-account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ frame-system = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
risc0-zkvm = { workspace = true, default-features = false }
serde_json = { workspace = true, default-features = false }


[dev-dependencies]
Expand All @@ -34,6 +36,8 @@ std = [
"frame-support/std",
"frame-system/std",
"scale-info/std",
"risc0-zkvm/std",
"serde_json/std",
]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime"]
22 changes: 22 additions & 0 deletions custom-pallets/anonymous-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub use weights::*;
pub type DepartmentId = u64;

use frame_support::pallet_prelude::DispatchError;
use risc0_zkvm::Receipt;
use scale_info::prelude::string::String;
use sp_std::collections::btree_set::BTreeSet;

#[frame_support::pallet(dev_mode)]
Expand All @@ -37,6 +39,9 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Type representing the weight of this pallet
type WeightInfo: WeightInfo;

#[pallet::constant]
type AnonymousAccountImageId: Get<[u32; 8]>;
}

// The pallet's runtime storage items.
Expand Down Expand Up @@ -87,6 +92,7 @@ pub mod pallet {
hash: [u8; 32],
account_id: T::AccountId,
},
ProofVerified,
}

// Errors inform users that something went wrong.
Expand All @@ -101,6 +107,7 @@ pub mod pallet {
NoAccounts,
IncompleteSlice,
HashNotFound,
ProofNotVerified,
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
Expand Down Expand Up @@ -176,6 +183,21 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight(0)]
pub fn verify_proof(origin: OriginFor<T>, receipt_bytes: Vec<u8>) -> DispatchResult {
let who = ensure_signed(origin)?;
let image_id = T::AnonymousAccountImageId::get();
let receipt_json: String = Decode::decode(&mut &receipt_bytes[..]).unwrap();
let receipt: Receipt = serde_json::from_str(&receipt_json).unwrap();
let (output, password_hash): ([u8; 32], [u8; 32]) = receipt.journal.decode().unwrap();

receipt.verify(image_id).map_err(|_| Error::<T>::ProofNotVerified)?;

Self::deposit_event(Event::ProofVerified);
Ok(())
}

#[pallet::call_index(50)]
#[pallet::weight(0)]
pub fn calculate_hash(origin: OriginFor<T>) -> DispatchResult {
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ parameter_types! {
pub BlockLength: frame_system::limits::BlockLength = frame_system::limits::BlockLength
::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
pub const SS58Prefix: u8 = 42;
pub const AnonymousAccountImageId: [u32; 8] = [1512492500, 2753161227, 4049970770, 2674496521, 3333553514, 2059402670, 1701049823, 2725882521];
}

/// The default types are being injected by [`derive_impl`](`frame_support::derive_impl`) from
Expand Down Expand Up @@ -302,6 +303,7 @@ impl pallet_departments::Config for Runtime {
impl pallet_anonymous_account::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_anonymous_account::weights::SubstrateWeight<Runtime>;
type AnonymousAccountImageId = AnonymousAccountImageId;
}

impl pallet_department_funding::Config for Runtime {
Expand Down

0 comments on commit ade97e7

Please sign in to comment.