Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Added crypto extensions #141

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"chain-extensions/signing",
"chain-extensions/dapps-staking",
"chain-extensions/pallet-assets",
"chain-extensions/xvm",
Expand Down Expand Up @@ -152,6 +153,7 @@ pallet-xc-asset-config = { path = "./frame/xc-asset-config", default-features =
dapps-staking-chain-extension-types = { path = "./chain-extensions/types/dapps-staking", default-features = false }
xvm-chain-extension-types = { path = "./chain-extensions/types/xvm", default-features = false }
assets-chain-extension-types = { path = "./chain-extensions/types/assets", default-features = false }
signing-chain-extension-types = { path = "./chain-extensions/types/signing", default-features = false }

pallet-evm-precompile-assets-erc20 = { path = "./precompiles/assets-erc20", default-features = false }
precompile-utils = { path = "./precompiles/utils", default-features = false }
Expand Down
39 changes: 39 additions & 0 deletions chain-extensions/signing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "chain-extension-signing"
version = "1.0.0"
license = "Apache-2.0"
description = "chain extension for Substrate native crypto signing functions"
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 }
signing-chain-extension-types = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"frame-support/std",
"frame-system/std",
"num-traits/std",
"pallet-contracts/std",
"scale-info/std",
"sp-std/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
127 changes: 127 additions & 0 deletions chain-extensions/signing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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 pallet_contracts::chain_extension::{
ChainExtension, Environment, Ext, InitState, RetVal, SysConfig,
};
use parity_scale_codec::Encode;
use signing_chain_extension_types::{Outcome, SigType};
use sp_runtime::{traits::Verify, DispatchError};
use sp_std::marker::PhantomData;
use sp_std::vec::Vec;

enum Func {
Verify,
}

impl TryFrom<u16> for Func {
type Error = DispatchError;

fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
1 => Ok(Func::Verify),
_ => Err(DispatchError::Other(
"CryptoExtension: Unimplemented func_id",
)),
}
}
}

/// Crypto signing chain extension.
pub struct SigningExtension<T>(PhantomData<T>);

impl<T> Default for SigningExtension<T> {
fn default() -> Self {
SigningExtension(PhantomData)
}
}

impl<T> ChainExtension<T> for SigningExtension<T>
where
T: pallet_contracts::Config,
<T as SysConfig>::AccountId: From<[u8; 32]>,
{
fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>
where
E: Ext<T = T>,
{
let func_id = env.func_id().try_into()?;
let mut env = env.buf_in_buf_out();

match func_id {
Func::Verify => {
let (sig_type, signature, msg, pubkey): (SigType, Vec<u8>, Vec<u8>, Vec<u8>) =
env.read_as_unbounded(env.in_len())?;

let result = match sig_type {
SigType::Sr25519 => {
// 64 bytes
let sig = match sp_core::sr25519::Signature::try_from(signature.as_slice())
{
Ok(sig) => sig,
Err(_) => {
return Ok(RetVal::Converging(Outcome::InvalidSignature as u32))
}
};
// 32 bytes
let pubkey = match sp_core::sr25519::Public::try_from(pubkey.as_slice()) {
Ok(pubkey) => pubkey,
Err(_) => return Ok(RetVal::Converging(Outcome::InvalidPubkey as u32)),
};
sig.verify(msg.as_slice(), &pubkey)
}
SigType::Ed25519 => {
// 64 bytes
let sig = match sp_core::ed25519::Signature::try_from(signature.as_slice())
{
Ok(sig) => sig,
Err(_) => {
return Ok(RetVal::Converging(Outcome::InvalidSignature as u32))
}
};
// 32 bytes
let pubkey = match sp_core::ed25519::Public::try_from(pubkey.as_slice()) {
Ok(pubkey) => pubkey,
Err(_) => return Ok(RetVal::Converging(Outcome::InvalidPubkey as u32)),
};
sig.verify(msg.as_slice(), &pubkey)
}
SigType::Ecdsa => {
// 65 bytes
let sig = match sp_core::ecdsa::Signature::try_from(signature.as_slice()) {
Ok(sig) => sig,
Err(_) => {
return Ok(RetVal::Converging(Outcome::InvalidSignature as u32))
}
};
// 33 bytes
let pubkey = match sp_core::ecdsa::Public::try_from(pubkey.as_slice()) {
Ok(pubkey) => pubkey,
Err(_) => return Ok(RetVal::Converging(Outcome::InvalidPubkey as u32)),
};
sig.verify(msg.as_slice(), &pubkey)
}
};
env.write(&result.encode(), false, None)?;
}
}

Ok(RetVal::Converging(Outcome::Success as u32))
}
}
26 changes: 26 additions & 0 deletions chain-extensions/types/signing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "signing-chain-extension-types"
version = "1.1.0"
license = "Apache-2.0"
description = "Types definitions for signing chain-extension"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
frame-support = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"frame-support/std",
"scale-info/std",
"sp-core/std",
"sp-runtime/std",
]
40 changes: 40 additions & 0 deletions chain-extensions/types/signing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 frame_support::pallet_prelude::MaxEncodedLen;
use parity_scale_codec::{Decode, Encode};

#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, Debug)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum Outcome {
/// Success
Success = 0,
/// Invalid signature
InvalidSignature = 1,
/// Invalid pubkey
InvalidPubkey = 2,
}

#[derive(Encode, Decode, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum SigType {
Sr25519,
Ed25519,
Ecdsa,
}