Skip to content

Commit

Permalink
Merge branch 'update'
Browse files Browse the repository at this point in the history
  • Loading branch information
benma committed Nov 3, 2023
2 parents 0b92aca + 3a24e98 commit ed6242b
Show file tree
Hide file tree
Showing 36 changed files with 522 additions and 430 deletions.
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:34
CONTAINER=shiftcrypto/firmware_v2:35

if [ "$1" == "pull" ] ; then
docker pull "$CONTAINER"
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | CARGO_HOME=/opt/
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.25.0 --locked
RUN CARGO_HOME=/opt/cargo cargo install bindgen-cli --version 0.66.1 --locked
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.0 --locked

COPY tools/prost-build-proto prost-build-proto
RUN CARGO_HOME=/opt/cargo cargo install --path prost-build-proto --locked
Expand Down
10 changes: 5 additions & 5 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/rust/bitbox02-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bitcoin = { version = "0.30.0", default-features = false, features = ["no-std"],

[dependencies.prost]
# keep version in sync with tools/prost-build/Cargo.toml.
version = "0.11.5"
version = "0.12.1"
default-features = false
features = ["prost-derive"]

Expand Down
19 changes: 5 additions & 14 deletions src/rust/bitbox02-rust/src/hww/api/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use pb::BtcCoin;
use pb::BtcScriptConfig;

use alloc::string::String;
use core::convert::TryFrom;
use core::convert::TryInto;

/// Like `hww::next_request`, but for Bitcoin requests/responses.
Expand Down Expand Up @@ -193,8 +194,7 @@ pub async fn address_multisig(
display: bool,
) -> Result<Response, Error> {
let coin_params = params::get(coin);
let script_type =
MultisigScriptType::from_i32(multisig.script_type).ok_or(Error::InvalidInput)?;
let script_type = MultisigScriptType::try_from(multisig.script_type)?;
keypath::validate_address_multisig(keypath, coin_params.bip44_coin, script_type)
.or(Err(Error::InvalidInput))?;
let account_keypath = &keypath[..keypath.len() - 2];
Expand Down Expand Up @@ -266,27 +266,18 @@ async fn address_policy(

/// Handle a Bitcoin xpub/address protobuf api call.
pub async fn process_pub(request: &pb::BtcPubRequest) -> Result<Response, Error> {
let coin = match BtcCoin::from_i32(request.coin) {
Some(coin) => coin,
None => return Err(Error::InvalidInput),
};
let coin = BtcCoin::try_from(request.coin)?;
coin_enabled(coin)?;
match request.output {
None => Err(Error::InvalidInput),
Some(Output::XpubType(xpub_type)) => {
let xpub_type = match XPubType::from_i32(xpub_type) {
Some(xpub_type) => xpub_type,
None => return Err(Error::InvalidInput),
};
let xpub_type = XPubType::try_from(xpub_type)?;
xpub(coin, xpub_type, &request.keypath, request.display).await
}
Some(Output::ScriptConfig(BtcScriptConfig {
config: Some(Config::SimpleType(simple_type)),
})) => {
let simple_type = match SimpleType::from_i32(simple_type) {
Some(simple_type) => simple_type,
None => return Err(Error::InvalidInput),
};
let simple_type = SimpleType::try_from(simple_type)?;
address_simple(coin, simple_type, &request.keypath, request.display).await
}
Some(Output::ScriptConfig(BtcScriptConfig {
Expand Down
7 changes: 3 additions & 4 deletions src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::xpubcache::Bip32XpubCache;

use alloc::string::String;
use alloc::vec::Vec;
use core::convert::TryFrom;

use bech32::{ToBase32, Variant};

Expand Down Expand Up @@ -126,8 +127,7 @@ impl Payload {
// Note that the witness script has an additional varint prefix.

let script_type =
pb::btc_script_config::multisig::ScriptType::from_i32(multisig.script_type)
.ok_or(Error::InvalidInput)?;
pb::btc_script_config::multisig::ScriptType::try_from(multisig.script_type)?;
let script = multisig::pkscript(multisig, keypath_change, keypath_address)?;
let payload_p2wsh = Payload {
data: Sha256::digest(&script).to_vec(),
Expand Down Expand Up @@ -182,8 +182,7 @@ impl Payload {
}),
..
} => {
let simple_type = pb::btc_script_config::SimpleType::from_i32(*simple_type)
.ok_or(Error::InvalidInput)?;
let simple_type = pb::btc_script_config::SimpleType::try_from(*simple_type)?;
Self::from_simple(xpub_cache, params, simple_type, keypath)
}
pb::BtcScriptConfigWithKeypath {
Expand Down
9 changes: 5 additions & 4 deletions src/rust/bitbox02-rust/src/hww/api/bitcoin/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::workflow::confirm;

use alloc::string::String;
use alloc::vec::Vec;
use core::convert::TryFrom;
use core::convert::TryInto;

use sha2::{Digest, Sha256};
Expand Down Expand Up @@ -61,7 +62,7 @@ pub fn get_hash(
}
{
// 2. script config type
let byte: u8 = match ScriptType::from_i32(multisig.script_type).ok_or(())? {
let byte: u8 = match ScriptType::try_from(multisig.script_type).map_err(|_| ())? {
ScriptType::P2wsh => 0x00,
ScriptType::P2wshP2sh => 0x01,
};
Expand Down Expand Up @@ -180,14 +181,14 @@ pub async fn confirm_extended(
xpub_type: XPubType,
keypath: &[u32],
) -> Result<(), Error> {
let script_type = ScriptType::from_i32(multisig.script_type).ok_or(Error::InvalidInput)?;
let script_type = ScriptType::try_from(multisig.script_type)?;

confirm(title, params, name, multisig).await?;
confirm::confirm(&confirm::Params {
title,
body: &format!(
"{}\nat\n{}",
match ScriptType::from_i32(multisig.script_type).ok_or(Error::InvalidInput)? {
match ScriptType::try_from(multisig.script_type)? {
ScriptType::P2wsh => "p2wsh",
ScriptType::P2wshP2sh => "p2wsh-p2sh",
},
Expand Down Expand Up @@ -265,7 +266,7 @@ pub fn validate(multisig: &Multisig, keypath: &[u32], expected_coin: u32) -> Res
super::keypath::validate_account_multisig(
keypath,
expected_coin,
ScriptType::from_i32(multisig.script_type).ok_or(Error::InvalidInput)?,
ScriptType::try_from(multisig.script_type)?,
)
.or(Err(Error::InvalidInput))?;

Expand Down
12 changes: 7 additions & 5 deletions src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use pb::btc_response::Response;
use pb::btc_script_config::Config;
use pb::BtcCoin;

use core::convert::TryFrom;

use super::multisig::SortXpubs;

use crate::workflow::{confirm, status, trinary_input_string};
Expand All @@ -39,7 +41,7 @@ pub fn process_is_script_config_registered(
}),
keypath,
}) => {
let coin = BtcCoin::from_i32(*coin).ok_or(Error::InvalidInput)?;
let coin = BtcCoin::try_from(*coin)?;
Ok(Response::IsScriptConfigRegistered(
pb::BtcIsScriptConfigRegisteredResponse {
is_registered: super::multisig::get_name(coin, multisig, keypath)?.is_some(),
Expand All @@ -54,7 +56,7 @@ pub fn process_is_script_config_registered(
}),
..
}) => {
let coin = BtcCoin::from_i32(*coin).ok_or(Error::InvalidInput)?;
let coin = BtcCoin::try_from(*coin)?;
Ok(Response::IsScriptConfigRegistered(
pb::BtcIsScriptConfigRegisteredResponse {
is_registered: super::policies::get_name(coin, policy)?.is_some(),
Expand Down Expand Up @@ -112,11 +114,11 @@ pub async fn process_register_script_config(
}),
keypath,
}) => {
let coin = BtcCoin::from_i32(*coin).ok_or(Error::InvalidInput)?;
let coin = BtcCoin::try_from(*coin)?;
let coin_params = params::get(coin);
let name = get_name(request).await?;
super::multisig::validate(multisig, keypath, coin_params.bip44_coin)?;
let xpub_type = XPubType::from_i32(request.xpub_type).ok_or(Error::InvalidInput)?;
let xpub_type = XPubType::try_from(request.xpub_type)?;
super::multisig::confirm_extended(
title,
coin_params,
Expand Down Expand Up @@ -146,7 +148,7 @@ pub async fn process_register_script_config(
}),
..
}) => {
let coin = BtcCoin::from_i32(*coin).ok_or(Error::InvalidInput)?;
let coin = BtcCoin::try_from(*coin)?;
let coin_params = params::get(coin);
let name = get_name(request).await?;
super::policies::parse(policy)?.validate(coin)?;
Expand Down
8 changes: 3 additions & 5 deletions src/rust/bitbox02-rust/src/hww/api/bitcoin/signmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use alloc::vec::Vec;
use core::convert::TryFrom;
use core::convert::TryInto;

use sha2::{Digest, Sha256};
Expand All @@ -36,7 +37,7 @@ const MAX_MESSAGE_SIZE: usize = 1024;
/// The result contains a 65 byte signature. The first 64 bytes are the secp256k1 signature in
/// compact format (R and S values), and the last byte is the recoverable id (recid).
pub async fn process(request: &pb::BtcSignMessageRequest) -> Result<Response, Error> {
let coin = BtcCoin::from_i32(request.coin).ok_or(Error::InvalidInput)?;
let coin = BtcCoin::try_from(request.coin)?;
if coin != BtcCoin::Btc {
return Err(Error::InvalidInput);
}
Expand All @@ -47,10 +48,7 @@ pub async fn process(request: &pb::BtcSignMessageRequest) -> Result<Response, Er
config: Some(Config::SimpleType(simple_type)),
}),
keypath,
}) => (
keypath,
SimpleType::from_i32(*simple_type).ok_or(Error::InvalidInput)?,
),
}) => (keypath, SimpleType::try_from(*simple_type)?),
_ => return Err(Error::InvalidInput),
};
if simple_type == SimpleType::P2tr {
Expand Down
Loading

0 comments on commit ed6242b

Please sign in to comment.