Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Submit access for external accounts #582

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
9 changes: 5 additions & 4 deletions engine-sdk/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::{OneYoctoAttachError, PrivateCallError};
use crate::prelude::{NearGas, H256};
use aurora_engine_types::account_id::AccountId;
use aurora_engine_types::types::Balance;

pub const DEFAULT_PREPAID_GAS: NearGas = NearGas::new(300_000_000_000_000);

Expand Down Expand Up @@ -42,7 +43,7 @@ pub trait Env {
/// Timestamp (in ns) of the current block.
fn block_timestamp(&self) -> Timestamp;
/// Amount of NEAR attached to current call
fn attached_deposit(&self) -> u128;
fn attached_deposit(&self) -> Balance;
/// Random seed generated for the current block
fn random_seed(&self) -> H256;
/// Prepaid NEAR Gas
Expand All @@ -57,7 +58,7 @@ pub trait Env {
}

fn assert_one_yocto(&self) -> Result<(), OneYoctoAttachError> {
if self.attached_deposit() == 1 {
if self.attached_deposit() == Balance::new(1) {
Ok(())
} else {
Err(OneYoctoAttachError)
Expand All @@ -74,7 +75,7 @@ pub struct Fixed {
pub predecessor_account_id: AccountId,
pub block_height: u64,
pub block_timestamp: Timestamp,
pub attached_deposit: u128,
pub attached_deposit: Balance,
pub random_seed: H256,
pub prepaid_gas: NearGas,
}
Expand All @@ -100,7 +101,7 @@ impl Env for Fixed {
self.block_timestamp
}

fn attached_deposit(&self) -> u128 {
fn attached_deposit(&self) -> Balance {
self.attached_deposit
}

Expand Down
35 changes: 28 additions & 7 deletions engine-sdk/src/near_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::prelude::NearGas;
use crate::promise::PromiseId;
use aurora_engine_types::account_id::AccountId;
use aurora_engine_types::parameters::{PromiseAction, PromiseBatchAction, PromiseCreateArgs};
use aurora_engine_types::types::PromiseResult;
use aurora_engine_types::types::{Balance, PromiseResult};
use aurora_engine_types::H256;

#[cfg(all(feature = "mainnet", not(feature = "testnet")))]
Expand Down Expand Up @@ -237,11 +237,11 @@ impl crate::env::Env for Runtime {
crate::env::Timestamp::new(ns)
}

fn attached_deposit(&self) -> u128 {
fn attached_deposit(&self) -> Balance {
unsafe {
let data = [0u8; core::mem::size_of::<u128>()];
exports::attached_deposit(data.as_ptr() as u64);
u128::from_le_bytes(data)
Balance::new(u128::from_le_bytes(data))
}
}

Expand Down Expand Up @@ -343,7 +343,7 @@ impl crate::promise::PromiseHandler for Runtime {
let amount = amount.as_u128();
exports::promise_batch_action_transfer(id, &amount as *const u128 as _);
},
PromiseAction::DeployConotract { code } => unsafe {
PromiseAction::DeployContract { code } => unsafe {
let code = code.as_slice();
exports::promise_batch_action_deploy_contract(
id,
Expand All @@ -370,6 +370,27 @@ impl crate::promise::PromiseHandler for Runtime {
gas.as_u64(),
)
},
PromiseAction::AddAccessKey {
public_key,
allowance,
receiver_id,
function_names,
} => unsafe {
let public_key = public_key.as_bytes();
let receiver_id = receiver_id.as_bytes();
let function_names = function_names.as_bytes();
exports::promise_batch_action_add_key_with_function_call(
id,
public_key.len() as _,
public_key.as_ptr() as _,
0, // redundant value
&allowance.as_u128() as *const u128 as _,
receiver_id.len() as _,
receiver_id.as_ptr() as _,
function_names.len() as _,
function_names.as_ptr() as _,
)
},
}
}

Expand Down Expand Up @@ -416,8 +437,8 @@ impl crate::env::Env for ViewEnv {
crate::env::Timestamp::new(ns)
}

fn attached_deposit(&self) -> u128 {
1
fn attached_deposit(&self) -> Balance {
Balance::new(1)
}

fn random_seed(&self) -> H256 {
Expand Down Expand Up @@ -548,7 +569,7 @@ pub(crate) mod exports {
public_key_ptr: u64,
nonce: u64,
);
fn promise_batch_action_add_key_with_function_call(
pub(crate) fn promise_batch_action_add_key_with_function_call(
promise_index: u64,
public_key_len: u64,
public_key_ptr: u64,
Expand Down
8 changes: 7 additions & 1 deletion engine-types/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum PromiseAction {
Transfer {
amount: Yocto,
},
DeployConotract {
DeployContract {
code: Vec<u8>,
},
FunctionCall {
Expand All @@ -41,6 +41,12 @@ pub enum PromiseAction {
attached_yocto: Yocto,
gas: NearGas,
},
AddAccessKey {
public_key: PublicKey,
allowance: Balance,
receiver_id: AccountId,
function_names: String,
},
}

#[must_use]
Expand Down
2 changes: 2 additions & 0 deletions engine-types/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub mod address;
pub mod balance;
pub mod fee;
pub mod gas;
pub mod public_key;
pub mod wei;

pub use address::*;
pub use balance::*;
pub use fee::*;
pub use gas::*;
pub use public_key::*;
pub use wei::*;

pub type RawU256 = [u8; 32];
Expand Down
10 changes: 10 additions & 0 deletions engine-types/src/types/public_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use borsh::{BorshDeserialize, BorshSerialize};

#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq, BorshDeserialize, BorshSerialize, Hash)]
pub struct PublicKey(Vec<u8>);

impl PublicKey {
pub fn as_bytes(&self) -> &[u8] {
self.0.as_slice()
}
}
26 changes: 22 additions & 4 deletions engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ mod contract {
use crate::fungible_token::FungibleTokenMetadata;
use crate::json::parse_json;
use crate::parameters::{
self, CallArgs, DeployErc20TokenArgs, GetErc20FromNep141CallArgs, GetStorageAtArgs,
InitCallArgs, IsUsedProofCallArgs, NEP141FtOnTransferArgs, NewCallArgs,
self, AddSubmitKeyArgs, CallArgs, DeployErc20TokenArgs, GetErc20FromNep141CallArgs,
GetStorageAtArgs, InitCallArgs, IsUsedProofCallArgs, NEP141FtOnTransferArgs, NewCallArgs,
PauseEthConnectorCallArgs, ResolveTransferCallArgs, SetContractDataCallArgs,
StorageDepositCallArgs, StorageWithdrawCallArgs, TransferCallCallArgs, ViewCallArgs,
};
Expand All @@ -88,7 +88,8 @@ mod contract {
};
use crate::prelude::storage::{bytes_to_key, KeyPrefix};
use crate::prelude::{
sdk, u256_to_arr, Address, PromiseResult, ToString, Yocto, ERR_FAILED_PARSE, H256,
sdk, u256_to_arr, Address, PromiseAction, PromiseBatchAction, PromiseResult, ToString,
Yocto, ERR_FAILED_PARSE, H256,
};
use aurora_engine_sdk::env::Env;
use aurora_engine_sdk::io::{StorageIntermediate, IO};
Expand Down Expand Up @@ -369,6 +370,23 @@ mod contract {
}
}

#[no_mangle]
pub extern "C" fn add_submit_key() {
let io = Runtime;
// Public key is passed in
let args: AddSubmitKeyArgs = io.read_input_borsh().sdk_unwrap();
let allowance = io.attached_deposit();
let receiver_id = io.current_account_id(); // TODO deploy and double chech this.
let action = PromiseAction::AddAccessKey {
public_key: args.public_key,
allowance,
receiver_id,
function_names: "submit".to_string(),
};
// io.promise_create_call();
todo!()
}

///
/// NONMUTATIVE METHODS
///
Expand All @@ -392,7 +410,7 @@ mod contract {
.map(|state| state.chain_id)
.sdk_unwrap();
let block_hash =
crate::engine::compute_block_hash(chain_id, block_height, account_id.as_bytes());
engine::compute_block_hash(chain_id, block_height, account_id.as_bytes());
io.return_output(block_hash.as_bytes())
}

Expand Down
8 changes: 7 additions & 1 deletion engine/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::prelude::{
WeiU256,
};
use crate::proof::Proof;
use aurora_engine_types::types::{Fee, NEP141Wei, Yocto};
use aurora_engine_types::types::{Fee, NEP141Wei, PublicKey, Yocto};
use evm::backend::Log;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -180,6 +180,12 @@ pub struct DeployErc20TokenArgs {
pub nep141: AccountId,
}

/// Borsh-encoded parameters for the `add_submit_key` function.
#[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct AddSubmitKeyArgs {
pub public_key: PublicKey,
}

/// Borsh-encoded parameters for `get_erc20_from_nep141` function.
pub type GetErc20FromNep141CallArgs = DeployErc20TokenArgs;

Expand Down