Skip to main content

idn_contracts/ext/
chain_ext.rs

1/*
2 * Copyright 2025 by Ideal Labs, LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use ink::env::Environment;
18
19#[ink::chain_extension(extension = 42)]
20pub trait RandExtension {
21	type ErrorCode = RandomReadErr;
22	// 1101 = chain extension func id on the target runtime (IDN)
23	#[ink(function = 1101)]
24	fn fetch_random(subject: [u8; 32]) -> [u8; 32];
25}
26
27#[derive(Debug, Copy, Clone, PartialEq, Eq)]
28#[ink::scale_derive(Encode, Decode, TypeInfo)]
29pub enum RandomReadErr {
30	FailGetRandomSource,
31}
32
33impl ink::env::chain_extension::FromStatusCode for RandomReadErr {
34	fn from_status_code(status_code: u32) -> Result<(), Self> {
35		match status_code {
36			0 => Ok(()),
37			1 => Err(Self::FailGetRandomSource),
38			_ => panic!("encountered unknown status code"),
39		}
40	}
41}
42
43#[derive(Debug, Clone, PartialEq, Eq)]
44#[ink::scale_derive(TypeInfo)]
45pub enum IDNEnvironment {}
46
47impl Environment for IDNEnvironment {
48	const MAX_EVENT_TOPICS: usize = <ink::env::DefaultEnvironment as Environment>::MAX_EVENT_TOPICS;
49
50	type AccountId = <ink::env::DefaultEnvironment as Environment>::AccountId;
51	type Balance = <ink::env::DefaultEnvironment as Environment>::Balance;
52	type Hash = <ink::env::DefaultEnvironment as Environment>::Hash;
53	type BlockNumber = <ink::env::DefaultEnvironment as Environment>::BlockNumber;
54	type Timestamp = <ink::env::DefaultEnvironment as Environment>::Timestamp;
55
56	type ChainExtension = RandExtension;
57}