Skip to content

Commit

Permalink
migrate did to alloy -> part 20
Browse files Browse the repository at this point in the history
  • Loading branch information
ufoscout committed Nov 25, 2024
1 parent c9b4f4c commit f0bd12e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
20 changes: 0 additions & 20 deletions src/did/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,6 @@ impl From<alloy::primitives::PrimitiveSignature> for Signature {
}
}

// impl From<Signature> for EthersSignature {
// fn from(value: Signature) -> Self {
// Self {
// r: value.r.into(),
// s: value.s.into(),
// v: value.v.into(),
// }
// }
// }

// impl From<EthersSignature> for Signature {
// fn from(value: EthersSignature) -> Self {
// Self {
// r: value.r.into(),
// s: value.s.into(),
// v: value.v.into(),
// }
// }
// }

impl Signature {
/// Upper limit for signature S field.
/// See comment to `Signature::check_malleability()` for more details.
Expand Down
67 changes: 43 additions & 24 deletions src/eth-signer-alloy/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::borrow::Cow;

use alloy::consensus::SignableTransaction;
use alloy::network::{TransactionBuilder as AlloyTransactionBuilder, TxSignerSync};
use alloy::rpc::types::{Transaction as AlloyRpcTransaction, TransactionRequest};
use alloy::signers::k256::ecdsa::SigningKey;
use did::error::EvmError;
use did::hash::H160;
use did::integer::U256;
use did::transaction::Signature as DidSignature;
use did::Transaction;
use did::transaction::{Signature as DidSignature, Transaction as DidTransaction};

use crate::LocalWallet;

Expand Down Expand Up @@ -37,19 +39,19 @@ pub struct TransactionBuilder<'a, 'b> {

impl<'a, 'b> TransactionBuilder<'a, 'b> {
/// Creates a new transaction with the expected hash
pub fn calculate_hash_and_build(self) -> Result<Transaction, EvmError> {
pub fn calculate_hash_and_build(self) -> Result<DidTransaction, EvmError> {
// NOTE: we intentionally do not set chain id here since chain ID shouldn't be present in
// legacy transaction RLP encoding
let mut transaction = ethers_core::types::Transaction {
from: self.from.0,
to: self.to.map(Into::into),
nonce: self.nonce.0,
value: self.value.0,
gas: self.gas.0,
gas_price: self.gas_price.map(Into::into),
input: self.input.into(),

let mut transaction = TransactionRequest {
to: self.to.map(|to| to.0.into()),
gas_price: self.gas_price.map(|price| price.0.to()),
..Default::default()
};
}.with_from(self.from.0)
.with_nonce(self.nonce.0.to())
.with_value(self.value.0)
.with_gas_limit(self.gas.0.to())
.with_input(self.input.into());

match self.signature {
SigningMethod::None => {}
Expand All @@ -60,18 +62,35 @@ impl<'a, 'b> TransactionBuilder<'a, 'b> {
}
SigningMethod::SigningKey(key) => {
let wallet =
Wallet::new_with_signer(Cow::Borrowed(key), transaction.from, self.chain_id);

// NOTE: we can avoid cloning input here by re-implementing code that calculates
// transaction signature hash
let typed_tx: TypedTransaction = (&transaction).into();
let signature = wallet
.sign_transaction_sync(&typed_tx)
.map_err(|e| EvmError::TransactionSignature(e.to_string()))?;

transaction.r = signature.r;
transaction.s = signature.s;
transaction.v = signature.v.into();
LocalWallet::new_with_credential(*key, self.from.0, Some(self.chain_id));

let mut tx = transaction.build_consensus_tx().unwrap();
let mut tx = tx.legacy().cloned().unwrap();
let signature = wallet.sign_transaction_sync(&mut tx).unwrap();

let WHAT_ABOUT_CHAIN_ID = false;

let signed = tx.into_signed(signature);
let transaction: AlloyRpcTransaction = AlloyRpcTransaction{
inner: signed.into(),
from: self.from.0,
block_hash: None,
block_number: None,
transaction_index: None,
effective_gas_price: None,
};


// // NOTE: we can avoid cloning input here by re-implementing code that calculates
// // transaction signature hash
// let typed_tx: TypedTransaction = (&transaction).into();
// let signature = wallet
// .sign_transaction_sync(&typed_tx)
// .map_err(|e| EvmError::TransactionSignature(e.to_string()))?;

// transaction.r = signature.r;
// transaction.s = signature.s;
// transaction.v = signature.v.into();
}
}

Expand Down

0 comments on commit f0bd12e

Please sign in to comment.