Skip to content

Commit

Permalink
Merge #290
Browse files Browse the repository at this point in the history
290: Evm update r=Eitu33 a=Eitu33



Co-authored-by: Thomas Plisson <[email protected]>
  • Loading branch information
bors[bot] and Eitu33 authored Jul 25, 2023
2 parents a2ad6ea + 67e26cb commit 9fb15c5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
51 changes: 50 additions & 1 deletion src/as_execution/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,56 @@ pub(crate) fn assembly_script_evm_signature_verify(
let public_key = read_buffer(memory, &ctx, public_key)?;
Ok(env
.get_interface()
.verify_evm_signature(&data, &signature, &public_key)? as i32)
.evm_signature_verify(&data, &signature, &public_key)? as i32)
}

/// Get address from public key (EVM)
#[named]
pub(crate) fn assembly_script_evm_get_address_from_pubkey(
mut ctx: FunctionEnvMut<ASEnv>,
public_key: i32,
) -> ABIResult<i32> {
let env = get_env(&ctx)?;
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?;
let memory = get_memory!(env);
let public_key = read_buffer(memory, &ctx, public_key)?;
let address = env
.get_interface()
.evm_get_address_from_pubkey(&public_key)?;
let ptr = pointer_from_bytearray(&env, &mut ctx, &address)?.offset();
Ok(ptr as i32)
}

/// Get public key from signature (EVM)
#[named]
pub(crate) fn assembly_script_evm_get_pubkey_from_signature(
mut ctx: FunctionEnvMut<ASEnv>,
data: i32,
signature: i32,
) -> ABIResult<i32> {
let env = get_env(&ctx)?;
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?;
let memory = get_memory!(env);
let data = read_buffer(memory, &ctx, data)?;
let signature = read_buffer(memory, &ctx, signature)?;
let public_key = env
.get_interface()
.evm_get_pubkey_from_signature(&data, &signature)?;
let ptr = pointer_from_bytearray(&env, &mut ctx, &public_key)?.offset();
Ok(ptr as i32)
}

#[named]
/// Return Ok(1) if the address is a User address, Ok(0) if it is an SC address
pub(crate) fn assembly_script_is_address_eoa(
mut ctx: FunctionEnvMut<ASEnv>,
address: i32,
) -> ABIResult<i32> {
let env = get_env(&ctx)?;
sub_remaining_gas_abi(&env, &mut ctx, function_name!())?;
let memory = get_memory!(env);
let address = read_string(memory, &ctx, address)?;
Ok(env.get_interface().is_address_eoa(&address)? as i32)
}

/// converts a public key to an address
Expand Down
3 changes: 3 additions & 0 deletions src/as_execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ impl ASContext {
"assembly_script_keccak256_hash" => Function::new_typed_with_env(store, &fenv, assembly_script_keccak256_hash),
"assembly_script_signature_verify" => Function::new_typed_with_env(store, &fenv, assembly_script_signature_verify),
"assembly_script_evm_signature_verify" => Function::new_typed_with_env(store, &fenv, assembly_script_evm_signature_verify),
"assembly_script_evm_get_address_from_pubkey" => Function::new_typed_with_env(store, &fenv, assembly_script_evm_get_address_from_pubkey),
"assembly_script_evm_get_pubkey_from_signature" => Function::new_typed_with_env(store, &fenv, assembly_script_evm_get_pubkey_from_signature),
"assembly_script_is_address_eoa" => Function::new_typed_with_env(store, &fenv, assembly_script_is_address_eoa),
"assembly_script_address_from_public_key" => Function::new_typed_with_env(store, &fenv, assembly_script_address_from_public_key),
"assembly_script_validate_address" => Function::new_typed_with_env(store, &fenv, assembly_script_validate_address),
"assembly_script_unsafe_random" => Function::new_typed_with_env(store, &fenv, assembly_script_unsafe_random),
Expand Down
38 changes: 31 additions & 7 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ impl Default for GasCosts {
abi_costs.insert(String::from("assembly_script_set_data_for"), 165);
abi_costs.insert(String::from("assembly_script_signature_verify"), 98);
abi_costs.insert(String::from("assembly_script_evm_signature_verify"), 264);
abi_costs.insert(
String::from("assembly_script_evm_get_address_from_pubkey"),
11,
);
abi_costs.insert(
String::from("assembly_script_evm_get_pubkey_from_signature"),
11,
);
abi_costs.insert(String::from("assembly_script_is_address_eoa"), 11);
abi_costs.insert(String::from("assembly_script_transfer_coins"), 62);
abi_costs.insert(String::from("assembly_script_transfer_coins_for"), 102);
abi_costs.insert(String::from("assembly_script_unsafe_random"), 11);
Expand Down Expand Up @@ -300,32 +309,47 @@ pub trait Interface: Send + Sync + InterfaceClone {
unimplemented!("caller_has_write_access")
}

// Hash data
/// Hash data
fn hash(&self, data: &[u8]) -> Result<[u8; 32]> {
unimplemented!("hash")
}

// Verify signature
/// Verify signature
fn signature_verify(&self, data: &[u8], signature: &str, public_key: &str) -> Result<bool> {
unimplemented!("signature_verify")
}

// Verify EVM signature
fn verify_evm_signature(
/// Verify signature (EVM)
fn evm_signature_verify(
&self,
message: &[u8],
signature: &[u8],
public_key: &[u8],
) -> Result<bool> {
unimplemented!("verify_evm_signature")
unimplemented!("evm_signature_verify")
}

// Convert a public key to an address
/// Get address from public key (EVM)
fn evm_get_address_from_pubkey(&self, public_key: &[u8]) -> Result<Vec<u8>> {
unimplemented!("evm_get_address_from_pubkey")
}

/// Get public key from signature (EVM)
fn evm_get_pubkey_from_signature(&self, hash: &[u8], signature: &[u8]) -> Result<Vec<u8>> {
unimplemented!("evm_get_pubkey_from_signature")
}

/// Return true if the address is a User address, false if it is an SC address
fn is_address_eoa(&self, address: &str) -> Result<bool> {
unimplemented!("is_address_eoa")
}

/// Convert a public key to an address
fn address_from_public_key(&self, public_key: &str) -> Result<String> {
unimplemented!("address_from_public_key")
}

// Validate an address
/// Validate an address
fn validate_address(&self, address: &str) -> Result<bool> {
unimplemented!("validate_address")
}
Expand Down

0 comments on commit 9fb15c5

Please sign in to comment.