Skip to content

Commit

Permalink
feat: add support for eth_signTransaction (#12500)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma authored Nov 13, 2024
1 parent 03f3646 commit bf44c97
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
14 changes: 7 additions & 7 deletions crates/rpc/rpc-builder/tests/it/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,13 @@ where
.await
.unwrap();
EthApiClient::<Transaction, Block, Receipt>::syncing(client).await.unwrap();
EthApiClient::<Transaction, Block, Receipt>::send_transaction(client, transaction_request)
EthApiClient::<Transaction, Block, Receipt>::send_transaction(
client,
transaction_request.clone(),
)
.await
.unwrap_err();
EthApiClient::<Transaction, Block, Receipt>::sign_transaction(client, transaction_request)
.await
.unwrap_err();
EthApiClient::<Transaction, Block, Receipt>::hashrate(client).await.unwrap();
Expand Down Expand Up @@ -318,12 +324,6 @@ where
.err()
.unwrap()
));
assert!(is_unimplemented(
EthApiClient::<Transaction, Block, Receipt>::sign_transaction(client, call_request.clone())
.await
.err()
.unwrap()
));
}

async fn test_basic_debug_calls<C>(client: &C)
Expand Down
5 changes: 3 additions & 2 deletions crates/rpc/rpc-eth-api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,9 @@ where
}

/// Handler for: `eth_signTransaction`
async fn sign_transaction(&self, _transaction: TransactionRequest) -> RpcResult<Bytes> {
Err(internal_rpc_err("unimplemented"))
async fn sign_transaction(&self, request: TransactionRequest) -> RpcResult<Bytes> {
trace!(target: "rpc::eth", ?request, "Serving eth_signTransaction");
Ok(EthTransactions::sign_transaction(self, request).await?)
}

/// Handler for: `eth_signTypedData`
Expand Down
30 changes: 20 additions & 10 deletions crates/rpc/rpc-eth-api/src/helpers/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,10 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
txn: TransactionRequest,
) -> impl Future<Output = Result<TransactionSigned, Self::Error>> + Send {
async move {
let signers: Vec<_> = self.signers().read().iter().cloned().collect();
for signer in signers {
if signer.is_signer_for(from) {
return match signer.sign_transaction(txn, from).await {
Ok(tx) => Ok(tx),
Err(e) => Err(e.into_eth_err()),
}
}
}
Err(EthApiError::InvalidTransactionSignature.into())
self.find_signer(from)?
.sign_transaction(txn, from)
.await
.map_err(Self::Error::from_eth_err)
}
}

Expand All @@ -430,6 +424,22 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
}
}

/// Signs a transaction request using the given account in request
/// Returns the EIP-2718 encoded signed transaction.
fn sign_transaction(
&self,
request: TransactionRequest,
) -> impl Future<Output = Result<Bytes, Self::Error>> + Send {
async move {
let from = match request.from {
Some(from) => from,
None => return Err(SignError::NoAccount.into_eth_err()),
};

Ok(self.sign_request(&from, request).await?.encoded_2718().into())
}
}

/// Encodes and signs the typed data according EIP-712. Payload must implement Eip712 trait.
fn sign_typed_data(&self, data: &TypedData, account: Address) -> Result<Bytes, Self::Error> {
Ok(self
Expand Down

0 comments on commit bf44c97

Please sign in to comment.