feat: add Transaction setters and separate random tx signing (#2951)

This commit is contained in:
Dan Cline
2023-06-02 05:37:21 -04:00
committed by GitHub
parent 43f421d0b6
commit 6353792a84
2 changed files with 32 additions and 0 deletions

View File

@ -72,6 +72,11 @@ pub fn random_signed_tx() -> TransactionSigned {
let secp = Secp256k1::new();
let key_pair = KeyPair::new(&secp, &mut rand::thread_rng());
let tx = random_tx();
sign_tx_with_key_pair(key_pair, tx)
}
/// Signs the [Transaction] with the given key pair.
pub fn sign_tx_with_key_pair(key_pair: KeyPair, tx: Transaction) -> TransactionSigned {
let signature =
sign_message(H256::from_slice(&key_pair.secret_bytes()[..]), tx.signature_hash()).unwrap();
TransactionSigned::from_transaction_and_signature(tx, signature)

View File

@ -224,6 +224,33 @@ impl Transaction {
}
}
}
/// This sets the transaction's nonce.
pub fn set_nonce(&mut self, nonce: u64) {
match self {
Transaction::Legacy(tx) => tx.nonce = nonce,
Transaction::Eip2930(tx) => tx.nonce = nonce,
Transaction::Eip1559(tx) => tx.nonce = nonce,
}
}
/// This sets the transaction's value.
pub fn set_value(&mut self, value: u128) {
match self {
Transaction::Legacy(tx) => tx.value = value,
Transaction::Eip2930(tx) => tx.value = value,
Transaction::Eip1559(tx) => tx.value = value,
}
}
/// This sets the transaction's input field.
pub fn set_input(&mut self, input: Bytes) {
match self {
Transaction::Legacy(tx) => tx.input = input,
Transaction::Eip2930(tx) => tx.input = input,
Transaction::Eip1559(tx) => tx.input = input,
}
}
}
impl Compact for Transaction {