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
use ink::env::Environment;

use codec::{Decode, Encode};

#[cfg(feature = "std")]
use scale_info::TypeInfo;

use alloc::vec::Vec;

use crate::{Encoded, IdentityId, AccountId};

#[ink::chain_extension]
#[derive(Clone, Copy)]
pub trait PolymeshRuntime {
  type ErrorCode = PolymeshRuntimeErr;

  #[ink(extension = 0x00_00_00_01)]
  fn call_runtime(call: Encoded);

  #[ink(extension = 0x00_00_00_02)]
  fn read_storage(key: Encoded) -> Option<Vec<u8>>;

  #[ink(extension = 0x00_00_00_03)]
  fn get_spec_version() -> u32;

  #[ink(extension = 0x00_00_00_04)]
  fn get_transaction_version() -> u32;

  #[ink(extension = 0x00_00_00_05)]
  fn get_key_did(key: AccountId) -> Option<IdentityId>;

  #[ink(extension = 0x00_00_00_10)]
  fn twox_64(data: Encoded) -> [u8; 8];

  #[ink(extension = 0x00_00_00_11)]
  fn twox_128(data: Encoded) -> [u8; 16];

  #[ink(extension = 0x00_00_00_12)]
  fn twox_256(data: Encoded) -> [u8; 32];
}

pub type PolymeshRuntimeInstance = <PolymeshRuntime as ink::ChainExtensionInstance>::Instance;

pub fn new_instance() -> PolymeshRuntimeInstance {
  <PolymeshRuntime as ink::ChainExtensionInstance>::instantiate()
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub enum PolymeshRuntimeErr {
  Unknown,
}

impl ink::env::chain_extension::FromStatusCode for PolymeshRuntimeErr {
  fn from_status_code(status_code: u32) -> Result<(), Self> {
    match status_code {
      0 => Ok(()),
      1 => Err(Self::Unknown),
      _ => panic!("encountered unknown status code"),
    }
  }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PolymeshEnvironment {}

impl Environment for PolymeshEnvironment {
  const MAX_EVENT_TOPICS: usize = <ink::env::DefaultEnvironment as Environment>::MAX_EVENT_TOPICS;

  type AccountId = <ink::env::DefaultEnvironment as Environment>::AccountId;
  type Balance = <ink::env::DefaultEnvironment as Environment>::Balance;
  type Hash = <ink::env::DefaultEnvironment as Environment>::Hash;
  type BlockNumber = <ink::env::DefaultEnvironment as Environment>::BlockNumber;
  type Timestamp = <ink::env::DefaultEnvironment as Environment>::Timestamp;

  type ChainExtension = PolymeshRuntime;
}