File tree Expand file tree Collapse file tree 6 files changed +84
-0
lines changed
Expand file tree Collapse file tree 6 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99## Changed
1010- Restrict which ` cfg ` attributes can be used ‒ [ #2313 ] ( https://github.com/use-ink/ink/pull/2313 )
1111
12+ ## Added
13+ - Support for ` caller_is_root ` - [ #2332 ] (https://github.com/use-ink/ink/pull/2332 )
14+
1215## Fixed
1316- [ E2E] Have port parsing handle comma-separated list ‒ [ #2336 ] ( https://github.com/use-ink/ink/pull/2336 )
1417
Original file line number Diff line number Diff line change @@ -729,6 +729,26 @@ where
729729 } )
730730}
731731
732+ /// Checks whether the caller of the current contract is root.
733+ ///
734+ /// Note that only the origin of the call stack can be root. Hence this function returning
735+ /// `true` implies that the contract is being called by the origin.
736+ ///
737+ /// A return value of `true` indicates that this contract is being called by a root
738+ /// origin, and `false` indicates that the caller is a signed origin.
739+ ///
740+ /// # Errors
741+ ///
742+ /// If the returned value cannot be properly decoded.
743+ pub fn caller_is_root < E > ( ) -> bool
744+ where
745+ E : Environment ,
746+ {
747+ <EnvInstance as OnInstance >:: on_instance ( |instance| {
748+ TypedEnvBackend :: caller_is_root :: < E > ( instance)
749+ } )
750+ }
751+
732752/// Replace the contract code at the specified address with new code.
733753///
734754/// # Note
Original file line number Diff line number Diff line change @@ -413,6 +413,15 @@ pub trait TypedEnvBackend: EnvBackend {
413413 where
414414 E : Environment ;
415415
416+ /// Checks whether the caller of the current contract is root.
417+ ///
418+ /// # Note
419+ ///
420+ /// For more details visit: [`caller_is_root`][`crate::caller_is_root`]
421+ fn caller_is_root < E > ( & mut self ) -> bool
422+ where
423+ E : Environment ;
424+
416425 /// Retrieves the code hash of the contract at the given `account` id.
417426 ///
418427 /// # Note
Original file line number Diff line number Diff line change @@ -567,6 +567,13 @@ impl TypedEnvBackend for EnvInstance {
567567 unimplemented ! ( "off-chain environment does not support cross-contract calls" )
568568 }
569569
570+ fn caller_is_root < E > ( & mut self ) -> bool
571+ where
572+ E : Environment ,
573+ {
574+ unimplemented ! ( "off-chain environment does not support `caller_is_root`" )
575+ }
576+
570577 fn code_hash < E > ( & mut self , _account : & E :: AccountId ) -> Result < E :: Hash >
571578 where
572579 E : Environment ,
Original file line number Diff line number Diff line change @@ -676,6 +676,20 @@ impl TypedEnvBackend for EnvInstance {
676676 ext:: caller_is_origin ( )
677677 }
678678
679+ fn caller_is_root < E > ( & mut self ) -> bool
680+ where
681+ E : Environment ,
682+ {
683+ // `ext::caller_is_root()` currently returns `u32`.
684+ // See https://github.com/paritytech/polkadot-sdk/issues/6767 for more details.
685+ let ret = ext:: caller_is_root ( ) ;
686+ match ret {
687+ 0u32 => false ,
688+ 1u32 => true ,
689+ _ => panic ! ( "Invalid value for bool conversion: {}" , ret) ,
690+ }
691+ }
692+
679693 fn code_hash < E > ( & mut self , account_id : & E :: AccountId ) -> Result < E :: Hash >
680694 where
681695 E : Environment ,
Original file line number Diff line number Diff line change @@ -1178,6 +1178,37 @@ where
11781178 ink_env:: caller_is_origin :: < E > ( )
11791179 }
11801180
1181+ /// Checks whether the caller of the current contract is root.
1182+ ///
1183+ /// # Example
1184+ ///
1185+ /// ```
1186+ /// # #[ink::contract]
1187+ /// # pub mod my_contract {
1188+ /// # #[ink(storage)]
1189+ /// # pub struct MyContract { }
1190+ /// #
1191+ /// # impl MyContract {
1192+ /// # #[ink(constructor)]
1193+ /// # pub fn new() -> Self {
1194+ /// # Self {}
1195+ /// # }
1196+ /// #
1197+ /// #[ink(message)]
1198+ /// pub fn caller_is_root(&mut self) -> bool {
1199+ /// self.env().caller_is_root()
1200+ /// }
1201+ /// # }
1202+ /// # }
1203+ /// ```
1204+ ///
1205+ /// # Note
1206+ ///
1207+ /// For more details visit: [`ink_env::caller_is_root`]
1208+ pub fn caller_is_root ( self ) -> bool {
1209+ ink_env:: caller_is_root :: < E > ( )
1210+ }
1211+
11811212 /// Returns the code hash of the contract at the given `account` id.
11821213 ///
11831214 /// # Example
You can’t perform that action at this time.
0 commit comments