Skip to content

Commit

Permalink
feat: bring return type for get_signature function
Browse files Browse the repository at this point in the history
denbite committed Oct 14, 2024
1 parent bc82b4b commit 135684c
Showing 4 changed files with 60 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -6,3 +6,4 @@ pub const ONE_MINUTE_NANOS: Duration = 60_000_000_000;
pub const MIN_GAS_FOR_GET_SIGNATURE: Gas = Gas::from_tgas(260);

pub const GAS_FOR_PROMISE: Gas = Gas::from_tgas(5);
pub const GAS_FOR_CALLBACK: Gas = Gas::from_tgas(5);
23 changes: 20 additions & 3 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use ethers_core::types::transaction::eip2930::AccessList;
use ethers_core::types::Eip1559TransactionRequest;
use ethers_core::types::{Bytes, Eip1559TransactionRequest};
use ethers_core::utils::keccak256;
use near_sdk::serde_json::json;
use near_sdk::{env, require, AccountId, Gas, NearToken, Promise, StorageUsage};

use crate::constants::GAS_FOR_PROMISE;
use crate::constants::{GAS_FOR_CALLBACK, GAS_FOR_PROMISE};
use crate::primitives::{BaseEip1559TransactionPayload, OtherEip1559TransactionPayload, Request};
use crate::Contract;

pub fn create_derivation_path(seed_number: u32) -> String {
format!("{}-{}", env::predecessor_account_id(), seed_number)
@@ -63,11 +64,16 @@ fn create_eip1559_tx(
}
}

fn build_tx_payload(tx: Eip1559TransactionRequest) -> [u8; 32] {
pub fn tx_to_vec(tx: Eip1559TransactionRequest) -> Vec<u8> {
// byte "2" stands for EIP-1559 Type
let mut vec = vec![2u8];
vec.extend(tx.rlp().to_vec());

vec
}

fn build_tx_payload(tx: Eip1559TransactionRequest) -> [u8; 32] {
let vec = tx_to_vec(tx);
keccak256(vec)
}

@@ -100,11 +106,22 @@ pub fn create_sign_promise(account_id: AccountId, args: Vec<u8>) -> Promise {
.unwrap()
// some Gas will be used to create Promise itself
.checked_sub(GAS_FOR_PROMISE)
.unwrap()
// some Gas will be allocated for callback
.checked_sub(GAS_FOR_CALLBACK)
.unwrap();

Promise::new(account_id).function_call(function, args, deposit, gas)
}

pub fn create_on_sign_callback_promise(tx: Eip1559TransactionRequest) -> Promise {
let vec = tx_to_vec(tx);

Contract::ext(env::current_account_id())
.with_static_gas(GAS_FOR_CALLBACK)
.on_get_signature(Bytes::from(vec.clone()).to_string())
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
49 changes: 33 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -5,15 +5,20 @@ mod primitives;
use constants::{MIN_GAS_FOR_GET_SIGNATURE, ONE_MINUTE_NANOS};
use helpers::{
assert_deposit, assert_gas, calculate_deposit_for_used_storage, create_derivation_path,
create_sign_promise, create_tx_and_args_for_sign, refund_unused_deposit,
create_on_sign_callback_promise, create_sign_promise, create_tx_and_args_for_sign,
refund_unused_deposit,
};
use near_sdk::serde_json;
use near_sdk::{
env::{self, block_timestamp},
near, require,
store::LookupMap,
AccountId, NearToken, PanicOnDefault, Promise,
AccountId, NearToken, PanicOnDefault, Promise, PromiseResult,
};
use primitives::{
GetSignatureResponse, InputRequest, OtherEip1559TransactionPayload, Request, RequestId,
StorageKey,
};
use primitives::{InputRequest, OtherEip1559TransactionPayload, Request, RequestId, StorageKey};

// Define the contract structure
#[derive(PanicOnDefault)]
@@ -82,8 +87,31 @@ impl Contract {
"ERR_FORBIDDEN"
);

let (_, args) = create_tx_and_args_for_sign(request.clone(), other_payload);
create_sign_promise(self.mpc_contract_id.clone(), args)
let (tx, args) = create_tx_and_args_for_sign(request.clone(), other_payload);

let sign_promise = create_sign_promise(self.mpc_contract_id.clone(), args);
let callback_promise = create_on_sign_callback_promise(tx);

sign_promise.then(callback_promise)
}

#[private]
pub fn on_get_signature(&mut self, tx_hex: String) -> GetSignatureResponse {
// https://docs.rs/near-sdk/latest/near_sdk/env/fn.promise_results_count.html
assert_eq!(env::promise_results_count(), 1, "ERR_TOO_MANY_RESULTS");

let signature_json = match env::promise_result(0) {
PromiseResult::Successful(data) => {
serde_json::from_slice::<serde_json::Value>(data.as_slice())
.expect("Couldn't deserialize signature!")
}
_ => env::panic_str("Signature couldn't be decoded from Promise response!"),
};

GetSignatureResponse {
tx: tx_hex,
signature: signature_json,
}
}
}

@@ -278,17 +306,6 @@ mod tests {
contract.register_signature_request(input_request.clone());
}

#[test]
fn test_get_signature() {
let (mut contract, _) = setup();

let input_request = input_request();
let request_id = contract.register_signature_request(input_request.clone());

let other_payload = other_payload();
contract.get_signature(request_id, other_payload);
}

#[should_panic = "ERR_NOT_FOUND"]
#[test]
fn test_get_signature_panics_on_unexisted_request() {
6 changes: 6 additions & 0 deletions src/primitives.rs
Original file line number Diff line number Diff line change
@@ -157,6 +157,12 @@ impl From<OtherEip1559TransactionPayload> for Eip1559TransactionRequest {
}
}

#[near_sdk::near(serializers = [json])]
pub struct GetSignatureResponse {
pub tx: String,
pub signature: near_sdk::serde_json::Value,
}

#[cfg(test)]
mod tests {
use ethers_core::{

0 comments on commit 135684c

Please sign in to comment.