Skip to content

Commit

Permalink
Implement signature of arbitrary data for Account
Browse files Browse the repository at this point in the history
  • Loading branch information
m-lord-renkse committed Oct 15, 2024
1 parent c4aea8e commit 0f1daad
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ethcontract/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub use self::gas_price::GasPrice;
pub use self::send::TransactionResult;
use crate::errors::ExecutionError;
use crate::secret::{Password, PrivateKey};
use primitive_types::H256;
use web3::api::Web3;
use web3::signing::Signature;
use web3::types::{AccessList, Address, Bytes, CallRequest, TransactionCondition, U256};
use web3::Transport;

Expand Down Expand Up @@ -44,6 +46,35 @@ impl Account {
Account::Kms(kms, _) => kms.public_address(),
}
}

/// Signs the given data
pub async fn sign<T: Transport>(&self, web3: Web3<T>, data: [u8; 32]) -> Option<Signature> {
match self {
Account::Local(_, _) => None,
Account::Locked(account, password, _) => {
let signature = web3
.personal()
.sign(Bytes::from(data), *account, password)
.await
.ok()?;
Some(Signature {
v: signature[0].into(), // The first byte is V
r: H256::from_slice(&signature[1..33]), // Next 32 bytes are R
s: H256::from_slice(&signature[33..65]), // Last 32 bytes are S
})
}
Account::Offline(key, _) => {
let signature = web3.accounts().sign(data, key);
Some(Signature {
v: signature.v.into(),
r: signature.r,
s: signature.s,
})
}
#[cfg(feature = "aws-kms")]
Account::Kms(account, _) => account.sign(data).await.ok(),
}
}
}

/// The condition on which a transaction's `SendFuture` gets resolved.
Expand Down

0 comments on commit 0f1daad

Please sign in to comment.