@@ -32,6 +32,10 @@ use ink_prelude::{
32
32
} ;
33
33
34
34
pub use bytes:: AsSolBytes ;
35
+ use primitive_types:: {
36
+ H160 ,
37
+ H256 ,
38
+ } ;
35
39
pub use types:: {
36
40
SolTypeDecode ,
37
41
SolTypeEncode ,
@@ -233,15 +237,14 @@ impl_refs_encode! {
233
237
[ ] Box <T >,
234
238
}
235
239
236
- // AccountId
240
+ // AccountId <-> bytes32
237
241
impl SolDecode for AccountId {
238
242
type SolType = AsSolBytes < [ u8 ; 32 ] > ;
239
243
240
244
fn from_sol_type ( value : Self :: SolType ) -> Self {
241
245
AccountId ( value. 0 )
242
246
}
243
247
}
244
-
245
248
impl SolEncode for AccountId {
246
249
type SolType = AsSolBytes < [ u8 ; 32 ] > ;
247
250
@@ -260,15 +263,14 @@ impl SolEncode for AccountId {
260
263
}
261
264
}
262
265
263
- // Hash
266
+ // Hash <-> bytes32
264
267
impl SolDecode for Hash {
265
268
type SolType = AsSolBytes < [ u8 ; 32 ] > ;
266
269
267
270
fn from_sol_type ( value : Self :: SolType ) -> Self {
268
271
Hash :: from ( value. 0 )
269
272
}
270
273
}
271
-
272
274
impl SolEncode for Hash {
273
275
type SolType = AsSolBytes < [ u8 ; 32 ] > ;
274
276
@@ -286,3 +288,58 @@ impl SolEncode for Hash {
286
288
Cow :: Owned ( AsSolBytes :: < [ u8 ; 32 ] > ( ( * self ) . into ( ) ) )
287
289
}
288
290
}
291
+
292
+ // H256 <-> bytes32
293
+ impl SolDecode for H256 {
294
+ type SolType = AsSolBytes < [ u8 ; 32 ] > ;
295
+
296
+ fn from_sol_type ( value : Self :: SolType ) -> Self {
297
+ H256 ( value. 0 )
298
+ }
299
+ }
300
+ impl SolEncode for H256 {
301
+ type SolType = AsSolBytes < [ u8 ; 32 ] > ;
302
+
303
+ fn encode ( & self ) -> Vec < u8 > {
304
+ // Override for better performance.
305
+ sol_data:: FixedBytes :: abi_encode ( & self . 0 )
306
+ }
307
+
308
+ fn to_sol_type ( & self ) -> Cow < Self :: SolType > {
309
+ // NOTE: Not actually used for encoding because of `encode` override above (for
310
+ // better performance).
311
+ // Arbitrary newtype wrappers can achieve similar performance (without overriding
312
+ // `encode`) by using `AsSolBytes<[u8; 32]>` as the inner type and returning
313
+ // `Cow::Borrowed(&self.0)`.
314
+ Cow :: Owned ( AsSolBytes ( self . 0 ) )
315
+ }
316
+ }
317
+
318
+ // H160 <-> bytes20
319
+ // TODO: (@davidsemakula) Evaluate if it's worth removing to mapping, changing it
320
+ // `address` before v6 release. Rationale: while this mapping is technically correct, it
321
+ // may be confusing for ink! devs, or just needless increase the cognitive load.
322
+ impl SolDecode for H160 {
323
+ type SolType = AsSolBytes < [ u8 ; 20 ] > ;
324
+
325
+ fn from_sol_type ( value : Self :: SolType ) -> Self {
326
+ H160 ( value. 0 )
327
+ }
328
+ }
329
+ impl SolEncode for H160 {
330
+ type SolType = AsSolBytes < [ u8 ; 20 ] > ;
331
+
332
+ fn encode ( & self ) -> Vec < u8 > {
333
+ // Override for better performance.
334
+ sol_data:: FixedBytes :: abi_encode ( & self . 0 )
335
+ }
336
+
337
+ fn to_sol_type ( & self ) -> Cow < Self :: SolType > {
338
+ // NOTE: Not actually used for encoding because of `encode` override above (for
339
+ // better performance).
340
+ // Arbitrary newtype wrappers can achieve similar performance (without overriding
341
+ // `encode`) by using `AsSolBytes<[u8; 32]>` as the inner type and returning
342
+ // `Cow::Borrowed(&self.0)`.
343
+ Cow :: Owned ( AsSolBytes ( self . 0 ) )
344
+ }
345
+ }
0 commit comments