-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unified account chain extension
- Loading branch information
1 parent
7364f63
commit 47b75ef
Showing
12 changed files
with
248 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
name = "unified-accounts-chain-extension-types" | ||
version = "0.1.0" | ||
description = "Types definitions for contracts using Unified Accounts chain-extension." | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
num_enum = { workspace = true } | ||
parity-scale-codec = { workspace = true } | ||
scale-info = { workspace = true } | ||
|
||
#substarte | ||
sp-core = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"num_enum/std", | ||
"parity-scale-codec/std", | ||
"scale-info/std", | ||
"sp-core/std", | ||
"sp-runtime/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// This file is part of Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use num_enum::{IntoPrimitive, TryFromPrimitive}; | ||
use parity_scale_codec::{Decode, Encode}; | ||
|
||
#[repr(u16)] | ||
#[derive(TryFromPrimitive, IntoPrimitive, Decode, Encode)] | ||
pub enum Command { | ||
/// Get the mapped Evm address if any | ||
GetEvmAddress = 0, | ||
/// Get the mapped Evm address if any otheriwse default associated Evm address | ||
GetEvmAddressOrDefault = 1, | ||
/// Get the mapped Native address if any | ||
GetNativeAddress = 2, | ||
/// Get the mapped Native address if any otheriwse default associated Native address | ||
GetNativeAddressOrDefault = 3, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
[package] | ||
name = "pallet-chain-extension-unified-accounts" | ||
version = "0.1.0" | ||
description = "Chain extension for AU" | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
frame-support = { workspace = true } | ||
frame-system = { workspace = true } | ||
log = { workspace = true } | ||
num-traits = { workspace = true } | ||
pallet-contracts = { workspace = true } | ||
pallet-contracts-primitives = { workspace = true } | ||
parity-scale-codec = { workspace = true } | ||
scale-info = { workspace = true } | ||
sp-core = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
sp-std = { workspace = true } | ||
|
||
# Astar | ||
astar-primitives = { workspace = true } | ||
pallet-unified-accounts = { workspace = true } | ||
unified-accounts-chain-extension-types = { workspace = true } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"parity-scale-codec/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"num-traits/std", | ||
"pallet-contracts/std", | ||
"pallet-contracts-primitives/std", | ||
"scale-info/std", | ||
"sp-std/std", | ||
"sp-core/std", | ||
"sp-runtime/std", | ||
# Astar | ||
"astar-primitives/std", | ||
"pallet-unified-accounts/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// This file is part of Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use astar_primitives::evm::{EvmAddress, UnifiedAddressMapper}; | ||
use core::marker::PhantomData; | ||
use sp_runtime::DispatchError; | ||
|
||
use frame_support::{traits::Get, DefaultNoBound}; | ||
use pallet_contracts::chain_extension::{ | ||
ChainExtension, Environment, Ext, InitState, Result as DispatchResult, RetVal, | ||
}; | ||
use parity_scale_codec::Encode; | ||
pub use unified_accounts_chain_extension_types::Command::{self, *}; | ||
|
||
#[derive(DefaultNoBound)] | ||
pub struct UnifiedAccountsExtension<T, UA>(PhantomData<(T, UA)>); | ||
|
||
impl<T, UA> ChainExtension<T> for UnifiedAccountsExtension<T, UA> | ||
where | ||
T: pallet_contracts::Config + pallet_unified_accounts::Config, | ||
UA: UnifiedAddressMapper<T::AccountId>, | ||
{ | ||
fn call<E>(&mut self, env: Environment<E, InitState>) -> DispatchResult<RetVal> | ||
where | ||
E: Ext<T = T>, | ||
{ | ||
let mut env = env.buf_in_buf_out(); | ||
match env.func_id().try_into().map_err(|_| { | ||
DispatchError::Other("Unsupported func id in Unified Accounts Chain Extension") | ||
})? { | ||
GetEvmAddress => { | ||
let account_id: T::AccountId = env.read_as()?; | ||
|
||
let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1); | ||
env.charge_weight(base_weight)?; | ||
// write to buffer | ||
UA::to_h160(&account_id).using_encoded(|r| env.write(r, false, None))?; | ||
} | ||
GetEvmAddressOrDefault => { | ||
let account_id: T::AccountId = env.read_as()?; | ||
|
||
let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1); | ||
env.charge_weight(base_weight)?; | ||
// write to buffer | ||
UA::to_h160_or_default(&account_id).using_encoded(|r| env.write(r, false, None))?; | ||
} | ||
GetNativeAddress => { | ||
let evm_address: EvmAddress = env.read_as()?; | ||
|
||
let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1); | ||
env.charge_weight(base_weight)?; | ||
// write to buffer | ||
UA::to_account_id(&evm_address).using_encoded(|r| env.write(r, false, None))?; | ||
} | ||
GetNativeAddressOrDefault => { | ||
let evm_address: EvmAddress = env.read_as()?; | ||
|
||
let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1); | ||
env.charge_weight(base_weight)?; | ||
// write to buffer | ||
UA::to_account_id_or_default(&evm_address) | ||
.using_encoded(|r| env.write(r, false, None))?; | ||
} | ||
}; | ||
Ok(RetVal::Converging(0)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters