Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eth async sighash #1186

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/run-container-ci
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
set -e
set -x

CONTAINER=shiftcrypto/firmware_v2:36
CONTAINER=shiftcrypto/firmware_v2:37

if [ "$1" == "pull" ] ; then
docker pull "$CONTAINER"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ RUN rustup target add thumbv7em-none-eabi
RUN rustup component add rustfmt
RUN rustup component add clippy
RUN CARGO_HOME=/opt/cargo cargo install cbindgen --version 0.26.0 --locked
RUN CARGO_HOME=/opt/cargo cargo install bindgen-cli --version 0.69.1 --locked
RUN CARGO_HOME=/opt/cargo cargo install bindgen-cli --version 0.69.4 --locked

COPY tools/prost-build-proto prost-build-proto
RUN CARGO_HOME=/opt/cargo cargo install --path prost-build-proto --locked
Expand Down
12 changes: 5 additions & 7 deletions src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,8 @@ async fn _process(request: &pb::BtcSignInitRequest) -> Result<Response, Error> {
const SIGHASH_ALL: u32 = 0x01;
let sighash = bip143::sighash(&bip143::Args {
version: request.version,
hash_prevouts: Sha256::digest(hash_prevouts).try_into().unwrap(),
hash_sequence: Sha256::digest(hash_sequence).try_into().unwrap(),
hash_prevouts: Sha256::digest(hash_prevouts).into(),
hash_sequence: Sha256::digest(hash_sequence).into(),
outpoint_hash: tx_input.prev_out_hash.as_slice().try_into().unwrap(),
outpoint_index: tx_input.prev_out_index,
sighash_script: &sighash_script(
Expand All @@ -921,7 +921,7 @@ async fn _process(request: &pb::BtcSignInitRequest) -> Result<Response, Error> {
)?,
prevout_value: tx_input.prev_out_value,
sequence: tx_input.sequence,
hash_outputs: Sha256::digest(hash_outputs).try_into().unwrap(),
hash_outputs: Sha256::digest(hash_outputs).into(),
locktime: request.locktime,
sighash_flags: SIGHASH_ALL,
});
Expand Down Expand Up @@ -2187,10 +2187,8 @@ mod tests {
true
})),
ui_confirm_create: Some(Box::new(move |params| unsafe {
match {
UI_COUNTER += 1;
UI_COUNTER
} {
UI_COUNTER += 1;
match UI_COUNTER {
7 => {
assert_eq!(params.title, "High fee");
assert_eq!(params.body, "The fee is 18.1%\nthe send amount.\nProceed?");
Expand Down
749 changes: 420 additions & 329 deletions src/rust/bitbox02-rust/src/hww/api/ethereum/sighash.rs

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,22 @@ fn parse_fee<'a>(request: &Transaction<'_>, params: &'a Params) -> Amount<'a> {
}
}

fn hash_legacy(chain_id: u64, request: &pb::EthSignRequest) -> Result<[u8; 32], Error> {
async fn hash_legacy(chain_id: u64, request: &pb::EthSignRequest) -> Result<[u8; 32], Error> {
let hash = super::sighash::compute_legacy(&super::sighash::ParamsLegacy {
nonce: &request.nonce,
gas_price: &request.gas_price,
gas_limit: &request.gas_limit,
recipient: &request.recipient,
value: &request.value,
data: &request.data,
data: core::cell::RefCell::new(super::sighash::SimpleProducer::new(&request.data)),
chain_id,
})
.await
.map_err(|_| Error::InvalidInput)?;
Ok(hash)
}

fn hash_eip1559(request: &pb::EthSignEip1559Request) -> Result<[u8; 32], Error> {
async fn hash_eip1559(request: &pb::EthSignEip1559Request) -> Result<[u8; 32], Error> {
let hash = super::sighash::compute_eip1559(&super::sighash::ParamsEIP1559 {
chain_id: request.chain_id,
nonce: &request.nonce,
Expand All @@ -183,8 +184,9 @@ fn hash_eip1559(request: &pb::EthSignEip1559Request) -> Result<[u8; 32], Error>
gas_limit: &request.gas_limit,
recipient: &request.recipient,
value: &request.value,
data: &request.data,
data: core::cell::RefCell::new(super::sighash::SimpleProducer::new(&request.data)),
})
.await
.map_err(|_| Error::InvalidInput)?;
Ok(hash)
}
Expand Down Expand Up @@ -359,8 +361,8 @@ pub async fn process(request: &Transaction<'_>) -> Result<Response, Error> {
}

let hash: [u8; 32] = match request {
Transaction::Legacy(legacy) => hash_legacy(params.chain_id, legacy)?,
Transaction::Eip1559(eip1559) => hash_eip1559(eip1559)?,
Transaction::Legacy(legacy) => hash_legacy(params.chain_id, legacy).await?,
Transaction::Eip1559(eip1559) => hash_eip1559(eip1559).await?,
};

let host_nonce = match request.host_nonce_commitment() {
Expand Down
6 changes: 2 additions & 4 deletions src/rust/bitbox02-rust/src/hww/api/ethereum/sign_typed_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,10 +931,8 @@ mod tests {
static mut UI_COUNTER: u32 = 0;
mock(Data {
ui_confirm_create: Some(Box::new(|params| unsafe {
match {
UI_COUNTER += 1;
UI_COUNTER
} {
UI_COUNTER += 1;
match UI_COUNTER {
1 => {
assert_eq!(params.title, "Domain (1/4)");
assert_eq!(params.body, "name: Ether Mail");
Expand Down
2 changes: 1 addition & 1 deletion src/rust/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.74.0
1.76.0
Loading