-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
executable file
·98 lines (81 loc) · 2.61 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#![cfg_attr(not(feature = "std"), no_std, no_main)]
use ink::{env::Environment, prelude::vec::Vec};
use scale::{Decode, Encode};
type DefaultAccountId = <ink::env::DefaultEnvironment as Environment>::AccountId;
type DefaultBalance = <ink::env::DefaultEnvironment as Environment>::Balance;
#[derive(PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum Error {
PalletVerificationFailed,
ContractVerificationFailed,
}
#[derive(PartialEq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo, Debug))]
pub struct ProofData {
slice: Vec<u32>,
image_id: [u32; 8],
}
impl From<scale::Error> for Error {
fn from(_: scale::Error) -> Self {
panic!("encountered unexpected invalid SCALE encoding")
}
}
impl ink::env::chain_extension::FromStatusCode for Error {
fn from_status_code(status_code: u32) -> core::result::Result<(), Self> {
match status_code {
0 => Ok(()),
1 => Err(Error::PalletVerificationFailed),
_ => panic!("encountered unknown status code"),
}
}
}
#[ink::chain_extension]
pub trait VerifierExtension {
type ErrorCode = Error;
#[ink(extension = 1)]
fn verify(proof_data: ProofData) -> Result<(), Error>;
}
/// An environment using default ink environment types, with PSP-22 extension included
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum CustomEnvironment {}
impl Environment for CustomEnvironment {
const MAX_EVENT_TOPICS: usize = <ink::env::DefaultEnvironment as Environment>::MAX_EVENT_TOPICS;
type AccountId = DefaultAccountId;
type Balance = DefaultBalance;
type Hash = <ink::env::DefaultEnvironment as Environment>::Hash;
type Timestamp = <ink::env::DefaultEnvironment as Environment>::Timestamp;
type BlockNumber = <ink::env::DefaultEnvironment as Environment>::BlockNumber;
type ChainExtension = crate::VerifierExtension;
}
#[ink::contract(env = crate::CustomEnvironment)]
mod factors_verifier {
use crate::Error;
use crate::Vec;
#[ink(storage)]
pub struct FactorsVerifier {
verified: bool,
image_id: [u32; 8],
}
impl FactorsVerifier {
#[ink(constructor)]
pub fn new(image_id: [u32; 8]) -> Self {
Self { verified: false, image_id }
}
#[ink(message)]
pub fn verify_via_ext(&mut self, bytes: Vec<u32>) -> Result<(), Error> {
let proof_data = crate::ProofData { slice: bytes, image_id: self.image_id };
let res = self.env().extension().verify(proof_data);
if res.is_ok() {
self.verified = true;
Ok(())
} else {
Err(Error::PalletVerificationFailed)
}
}
#[ink(message)]
pub fn get_status(&self) -> bool {
self.verified
}
}
}