@@ -19,17 +19,15 @@ use pallet_timestamp::Now;
19
19
use parity_scale_codec:: { Decode , Encode } ;
20
20
use scale_info:: { prelude:: string:: String , TypeInfo } ;
21
21
use serde:: { de:: Error , ser:: SerializeStruct , Serializer } ;
22
- use sp_runtime:: { traits:: BadOrigin , DeserializeOwned , RuntimeDebug } ;
22
+ use sp_runtime:: { traits:: BadOrigin , DeserializeOwned } ;
23
23
24
24
pub use jwt_compact:: {
25
25
alg:: { Ed25519 , VerifyingKey } ,
26
26
Claims as StandardClaims , * ,
27
27
} ;
28
28
use serde:: Deserializer ;
29
29
30
- #[ derive(
31
- Clone , Encode , Decode , Eq , PartialEq , Ord , PartialOrd , RuntimeDebug , TypeInfo , Deserialize , Serialize , MaxEncodedLen ,
32
- ) ]
30
+ #[ derive( Clone , Copy , Encode , Decode , Eq , PartialEq , TypeInfo , Deserialize , Serialize , MaxEncodedLen , RuntimeDebug ) ]
33
31
#[ serde( rename_all = "lowercase" ) ]
34
32
pub enum InvestorType {
35
33
Retail ,
@@ -54,8 +52,8 @@ parameter_types! {
54
52
pub const Institutional : InvestorType = InvestorType :: Institutional ;
55
53
}
56
54
57
- #[ derive( Clone , Encode , Decode , Eq , PartialEq , Ord , PartialOrd , RuntimeDebug , TypeInfo , Deserialize ) ]
58
- pub struct SampleClaims < AccountId > {
55
+ #[ derive( Clone , Encode , Decode , Eq , PartialEq , TypeInfo , Deserialize ) ]
56
+ pub struct PolimecPayload < AccountId > {
59
57
#[ serde( rename = "sub" ) ]
60
58
pub subject : AccountId ,
61
59
#[ serde( rename = "iss" ) ]
@@ -75,7 +73,7 @@ impl<T> EnsureOriginWithCredentials<T::RuntimeOrigin> for EnsureInvestor<T>
75
73
where
76
74
T : frame_system:: Config + pallet_timestamp:: Config ,
77
75
{
78
- type Claims = SampleClaims < T :: AccountId > ;
76
+ type Claims = PolimecPayload < T :: AccountId > ;
79
77
type Success = ( T :: AccountId , Did , InvestorType , Cid ) ;
80
78
81
79
fn try_origin (
@@ -86,19 +84,14 @@ where
86
84
let Some ( who) = origin. clone ( ) . into_signer ( ) else { return Err ( origin) } ;
87
85
let Ok ( token) = Self :: verify_token ( token, verifying_key) else { return Err ( origin) } ;
88
86
let claims = token. claims ( ) ;
89
- // Get the current timestamp from the pallet_timestamp. It is in milliseconds.
87
+ // Get current timestamp from pallet_timestamp ( milliseconds)
90
88
let Ok ( now) = Now :: < T > :: get ( ) . try_into ( ) else { return Err ( origin) } ;
91
89
let Some ( date_time) = claims. expiration else { return Err ( origin) } ;
92
90
93
91
let timestamp: u64 = date_time. timestamp_millis ( ) . try_into ( ) . map_err ( |_| origin. clone ( ) ) ?;
94
92
95
93
if claims. custom . subject == who && timestamp >= now {
96
- return Ok ( (
97
- who,
98
- claims. custom . did . clone ( ) ,
99
- claims. custom . investor_type . clone ( ) ,
100
- claims. custom . ipfs_cid . clone ( ) ,
101
- ) ) ;
94
+ return Ok ( ( who, claims. custom . did . clone ( ) , claims. custom . investor_type , claims. custom . ipfs_cid . clone ( ) ) ) ;
102
95
}
103
96
104
97
Err ( origin)
@@ -111,7 +104,7 @@ where
111
104
OuterOrigin : OriginTrait ,
112
105
{
113
106
type Success ;
114
- type Claims : Clone + Encode + Decode + Eq + PartialEq + Ord + PartialOrd + TypeInfo + DeserializeOwned ;
107
+ type Claims : Clone + Encode + Decode + Eq + PartialEq + TypeInfo + DeserializeOwned ;
115
108
116
109
fn try_origin (
117
110
origin : OuterOrigin ,
@@ -142,49 +135,40 @@ where
142
135
D : Deserializer < ' de > ,
143
136
{
144
137
String :: deserialize ( deserializer)
145
- . map ( |string| string. as_bytes ( ) . to_vec ( ) )
146
- . and_then ( |vec| vec . try_into ( ) . map_err ( |_| Error :: custom ( "failed to deserialize " ) ) )
138
+ . map ( |string| string. into_bytes ( ) )
139
+ . and_then ( |vec| BoundedVec :: try_from ( vec ) . map_err ( |_| Error :: custom ( "DID exceeds length limit " ) ) )
147
140
}
148
141
149
142
pub fn from_bounded_cid < ' de , D > ( deserializer : D ) -> Result < Cid , D :: Error >
150
143
where
151
144
D : Deserializer < ' de > ,
152
145
{
153
146
String :: deserialize ( deserializer)
154
- . map ( |string| string. as_bytes ( ) . to_vec ( ) )
155
- . and_then ( |vec| vec . try_into ( ) . map_err ( |_| Error :: custom ( "failed to deserialize " ) ) )
147
+ . map ( |string| string. into_bytes ( ) )
148
+ . and_then ( |vec| BoundedVec :: try_from ( vec ) . map_err ( |_| Error :: custom ( "CID exceeds length limit " ) ) )
156
149
}
157
150
158
- impl < AccountId > Serialize for SampleClaims < AccountId >
151
+ // Key corrected serialization implementation
152
+ impl < AccountId > Serialize for PolimecPayload < AccountId >
159
153
where
160
- AccountId : Serialize , // Ensure AccountId can be serialized
154
+ AccountId : Serialize ,
161
155
{
162
156
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
163
157
where
164
158
S : Serializer ,
165
159
{
166
160
// Define how many fields we are serializing.
167
- let mut state = serializer. serialize_struct ( "SampleClaims " , 5 ) ?;
161
+ let mut state = serializer. serialize_struct ( "PolimecPayload " , 5 ) ?;
168
162
169
163
// Serialize each field.
170
164
// Fields like `subject`, `issuer`, and `investor_type` can be serialized directly.
171
165
state. serialize_field ( "sub" , & self . subject ) ?;
172
166
state. serialize_field ( "iss" , & self . issuer ) ?;
173
- // For the `ipfs_cid_string` field, you'd use your custom logic to convert it to a string or another format suitable for serialization.
174
- // Assuming `cid` is a `BoundedVec<u8, ConstU32<96>>` and you're encoding it as a UTF-8 string.
175
- let ipfs_cid_bytes: scale_info:: prelude:: vec:: Vec < u8 > = self . ipfs_cid . clone ( ) . into ( ) ; // Convert BoundedVec to Vec<u8>
176
- let ipfs_cid_string = String :: from_utf8_lossy ( & ipfs_cid_bytes) ; // Convert Vec<u8> to String
177
- state. serialize_field ( "aud" , & ipfs_cid_string) ?;
178
-
179
167
state. serialize_field ( "investor_type" , & self . investor_type ) ?;
180
-
181
- // For the `did` field, you'd use your custom logic to convert it to a string or another format suitable for serialization.
182
- // Assuming `did` is a `BoundedVec<u8, ConstU32<57>>` and you're encoding it as a UTF-8 string.
183
- let did_bytes: scale_info:: prelude:: vec:: Vec < u8 > = self . did . clone ( ) . into ( ) ; // Convert BoundedVec to Vec<u8>
184
- let did_string = String :: from_utf8_lossy ( & did_bytes) ; // Convert Vec<u8> to String
185
- state. serialize_field ( "did" , & did_string) ?;
186
-
187
- // End the serialization
168
+ // Serialize the `ipfs_cid` and `did` fields as strings.
169
+ state
170
+ . serialize_field ( "aud" , core:: str:: from_utf8 ( & self . ipfs_cid ) . map_err ( |e| serde:: ser:: Error :: custom ( e) ) ?) ?;
171
+ state. serialize_field ( "did" , core:: str:: from_utf8 ( & self . did ) . map_err ( |e| serde:: ser:: Error :: custom ( e) ) ?) ?;
188
172
state. end ( )
189
173
}
190
174
}
0 commit comments