Skip to content

Commit

Permalink
Feat: allow xcc calls to perform any possible NEAR call (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
birchmd authored Oct 2, 2022
1 parent fd5d880 commit bd0ae7e
Show file tree
Hide file tree
Showing 12 changed files with 836 additions and 92 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ exclude = [
"etc/tests/ft-receiver",
"etc/tests/benchmark-contract",
"etc/tests/self-contained-5bEgfRQ",
"etc/tests/fibonacci",
"etc/xcc-router",
]
1 change: 1 addition & 0 deletions engine-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ sha2 = { version = "0.10.2", default-features = false }
std = ["aurora-engine-types/std", "borsh/std", "sha3/std", "sha2/std"]
contract = []
log = []
all-promise-actions = []
mainnet = []
testnet = []
132 changes: 125 additions & 7 deletions engine-sdk/src/near_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::io::StorageIntermediate;
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::parameters::{
NearPublicKey, PromiseAction, PromiseBatchAction, PromiseCreateArgs,
};
use aurora_engine_types::types::PromiseResult;
use aurora_engine_types::H256;

Expand All @@ -18,6 +20,16 @@ const CUSTODIAN_ADDRESS: &[u8] = &[
132, 168, 43, 179, 156, 131, 152, 157, 93, 192, 126, 19, 16, 40, 25, 35, 210, 84, 77, 194,
];

macro_rules! unsafe_feature_gated {
($feature_name:literal, $code:block) => {
if cfg!(feature = $feature_name) {
unsafe { $code }
} else {
unimplemented!("Not implemented without feature {}", $feature_name)
}
};
}

/// Wrapper type for indices in NEAR's register API.
pub struct RegisterIndex(u64);

Expand Down Expand Up @@ -346,7 +358,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 @@ -373,6 +385,78 @@ impl crate::promise::PromiseHandler for Runtime {
gas.as_u64(),
)
},
PromiseAction::Stake { amount, public_key } => {
unsafe_feature_gated!("all-promise-actions", {
let amount = amount.as_u128();
let pk: RawPublicKey = public_key.into();
let pk_bytes = pk.as_bytes();
exports::promise_batch_action_stake(
id,
&amount as *const u128 as _,
pk_bytes.len() as _,
pk_bytes.as_ptr() as _,
)
});
}
PromiseAction::AddFullAccessKey { public_key, nonce } => {
unsafe_feature_gated!("all-promise-actions", {
let pk: RawPublicKey = public_key.into();
let pk_bytes = pk.as_bytes();
exports::promise_batch_action_add_key_with_full_access(
id,
pk_bytes.len() as _,
pk_bytes.as_ptr() as _,
*nonce,
)
});
}
PromiseAction::AddFunctionCallKey {
public_key,
nonce,
allowance,
receiver_id,
function_names,
} => {
unsafe_feature_gated!("all-promise-actions", {
let pk: RawPublicKey = public_key.into();
let pk_bytes = pk.as_bytes();
let allowance = allowance.as_u128();
let receiver_id = receiver_id.as_bytes();
let function_names = function_names.as_bytes();
exports::promise_batch_action_add_key_with_function_call(
id,
pk_bytes.len() as _,
pk_bytes.as_ptr() as _,
*nonce,
&allowance as *const u128 as _,
receiver_id.len() as _,
receiver_id.as_ptr() as _,
function_names.len() as _,
function_names.as_ptr() as _,
)
});
}
PromiseAction::DeleteKey { public_key } => {
unsafe_feature_gated!("all-promise-actions", {
let pk: RawPublicKey = public_key.into();
let pk_bytes = pk.as_bytes();
exports::promise_batch_action_delete_key(
id,
pk_bytes.len() as _,
pk_bytes.as_ptr() as _,
)
});
}
PromiseAction::DeleteAccount { beneficiary_id } => {
unsafe_feature_gated!("all-promise-actions", {
let beneficiary_id = beneficiary_id.as_bytes();
exports::promise_batch_action_delete_key(
id,
beneficiary_id.len() as _,
beneficiary_id.as_ptr() as _,
)
});
}
}
}

Expand All @@ -390,6 +474,40 @@ impl crate::promise::PromiseHandler for Runtime {
}
}

/// Similar to NearPublicKey, except the first byte includes
/// the curve identifier.
enum RawPublicKey {
Ed25519([u8; 33]),
Secp256k1([u8; 65]),
}

impl RawPublicKey {
fn as_bytes(&self) -> &[u8] {
match self {
Self::Ed25519(bytes) => bytes,
Self::Secp256k1(bytes) => bytes,
}
}
}

impl<'a> From<&'a NearPublicKey> for RawPublicKey {
fn from(key: &'a NearPublicKey) -> Self {
match key {
NearPublicKey::Ed25519(bytes) => {
let mut buf = [0u8; 33];
buf[1..33].copy_from_slice(bytes);
Self::Ed25519(buf)
}
NearPublicKey::Secp256k1(bytes) => {
let mut buf = [0u8; 65];
buf[0] = 0x01;
buf[1..65].copy_from_slice(bytes);
Self::Secp256k1(buf)
}
}
}
}

/// Some host functions are not usable in NEAR view calls.
/// This struct puts in default values for those calls instead.
pub struct ViewEnv;
Expand Down Expand Up @@ -539,19 +657,19 @@ pub(crate) mod exports {
gas: u64,
);
pub(crate) fn promise_batch_action_transfer(promise_index: u64, amount_ptr: u64);
fn promise_batch_action_stake(
pub(crate) fn promise_batch_action_stake(
promise_index: u64,
amount_ptr: u64,
public_key_len: u64,
public_key_ptr: u64,
);
fn promise_batch_action_add_key_with_full_access(
pub(crate) fn promise_batch_action_add_key_with_full_access(
promise_index: u64,
public_key_len: u64,
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 All @@ -562,12 +680,12 @@ pub(crate) mod exports {
method_names_len: u64,
method_names_ptr: u64,
);
fn promise_batch_action_delete_key(
pub(crate) fn promise_batch_action_delete_key(
promise_index: u64,
public_key_len: u64,
public_key_ptr: u64,
);
fn promise_batch_action_delete_account(
pub(crate) fn promise_batch_action_delete_account(
promise_index: u64,
beneficiary_id_len: u64,
beneficiary_id_ptr: u64,
Expand Down
Loading

0 comments on commit bd0ae7e

Please sign in to comment.