diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index da625db33..994a48cd4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,7 +38,6 @@ set(DBB-FIRMWARE-SOURCES ${CMAKE_SOURCE_DIR}/src/workflow/blocking.c ${CMAKE_SOURCE_DIR}/src/workflow/idle_workflow.c ${CMAKE_SOURCE_DIR}/src/workflow/orientation_screen.c - ${CMAKE_SOURCE_DIR}/src/apps/btc/btc_common.c ${CMAKE_SOURCE_DIR}/src/queue.c ${CMAKE_SOURCE_DIR}/src/usb/usb_processing.c ) @@ -389,8 +388,6 @@ add_custom_target(rust-bindgen --rustified-enum simple_type_t --rustified-enum multisig_script_type_t --rustified-enum output_type_t - --allowlist-function btc_common_pkscript_from_payload - --allowlist-function btc_common_pkscript_from_multisig --allowlist-var MAX_VARINT_SIZE --allowlist-var MAX_PK_SCRIPT_SIZE --allowlist-function reboot diff --git a/src/apps/btc/btc_common.c b/src/apps/btc/btc_common.c deleted file mode 100644 index ce3e638ae..000000000 --- a/src/apps/btc/btc_common.c +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright 2019 Shift Cryptosecurity AG -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - -#include "btc_common.h" - -#include -#include -#include -#include -#include -#include - -bool btc_common_pkscript_from_multisig( - const multisig_t* multisig, - uint32_t keypath_change, - uint32_t keypath_address, - uint8_t* script_out, - size_t* script_out_size) -{ - uint8_t pubkeys[MULTISIG_P2WSH_MAX_SIGNERS * EC_PUBLIC_KEY_LEN]; - - for (size_t index = 0; index < multisig->xpubs_count; index++) { - struct ext_key xpub = {0}; - if (bip32_key_unserialize(multisig->xpubs[index], sizeof(multisig->xpubs[index]), &xpub) != - WALLY_OK) { - return false; - } - struct ext_key derived_cosigner_xpub = {0}; - const uint32_t keypath[2] = {keypath_change, keypath_address}; - if (bip32_key_from_parent_path( - &xpub, keypath, 2, BIP32_FLAG_KEY_PUBLIC, &derived_cosigner_xpub) != WALLY_OK) { - return false; - } - memcpy( - &pubkeys[index * EC_PUBLIC_KEY_LEN], derived_cosigner_xpub.pub_key, EC_PUBLIC_KEY_LEN); - } - - size_t written; - if (wally_scriptpubkey_multisig_from_bytes( - pubkeys, - multisig->xpubs_count * EC_PUBLIC_KEY_LEN, - multisig->threshold, - WALLY_SCRIPT_MULTISIG_SORTED, - script_out, - *script_out_size, - &written) != WALLY_OK) { - return false; - } - if (written > *script_out_size) { - // Double check since the function above sets written to script_len if the buffer was too - // short. - return false; - } - *script_out_size = written; - - return true; -} diff --git a/src/apps/btc/btc_common.h b/src/apps/btc/btc_common.h deleted file mode 100644 index e814b6f54..000000000 --- a/src/apps/btc/btc_common.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2019 Shift Cryptosecurity AG -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef _APPS_BTC_COMMON_H -#define _APPS_BTC_COMMON_H - -#include -#include -#include - -#include -#include - -#include -#include -#include - -#define MULTISIG_P2WSH_MAX_SIGNERS 15 - -typedef struct { - size_t xpubs_count; - uint8_t xpubs[MULTISIG_P2WSH_MAX_SIGNERS][BIP32_SERIALIZED_LEN]; - uint32_t threshold; -} multisig_t; - -// see https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer -#define MAX_VARINT_SIZE (9) -// current expected max pk script size is a m-of-15 multisig. 700 is also enough for m-of-20, which -// is technically possible to extend to if needed. -#define MAX_PK_SCRIPT_SIZE (700) - -/** - * Creates a n-of-m multisig script based on OP_CHECKMULTISIG. 0 Result { - Ok(bitbox02::app_btc::Multisig { - xpubs_count: multisig.xpubs.len() as _, - xpubs: { - let mut xpubs = [[0u8; 78]; MAX_SIGNERS]; - for (i, xpub) in multisig.xpubs.iter().enumerate() { - xpubs[i] = bip32::Xpub::from(xpub) - .serialize(Some(bip32::XPubType::Xpub)) - .or(Err(Error::InvalidInput))? - .try_into() - .or(Err(Error::Generic))?; - } - xpubs - }, - threshold: multisig.threshold, - }) -} - pub enum SortXpubs { No, Yes, @@ -311,11 +292,35 @@ pub fn pkscript( keypath_change: u32, keypath_address: u32, ) -> Result, Error> { - Ok(bitbox02::app_btc::pkscript_from_multisig( - &convert_multisig(multisig)?, - keypath_change, - keypath_address, - )?) + if multisig.xpubs.len() < 2 || multisig.xpubs.len() > MAX_SIGNERS { + return Err(Error::InvalidInput); + } + if multisig.threshold == 0 || multisig.threshold > multisig.xpubs.len() as _ { + return Err(Error::InvalidInput); + } + let mut pubkeys: Vec> = multisig + .xpubs + .iter() + .map(|xpub| -> Result, ()> { + Ok(bip32::Xpub::from(xpub) + .derive(&[keypath_change, keypath_address])? + .public_key() + .to_vec()) + }) + .collect::>()?; + pubkeys.sort(); + + let mut script_builder = bitcoin::script::Builder::new().push_int(multisig.threshold as _); + for pk in pubkeys.iter() { + let pk: &bitcoin::script::PushBytes = + pk.as_slice().try_into().map_err(|_| Error::Generic)?; + script_builder = script_builder.push_slice(pk); + } + script_builder = script_builder + .push_int(pubkeys.len() as _) + .push_opcode(bitcoin::opcodes::all::OP_CHECKMULTISIG); + + Ok(script_builder.into_bytes()) } #[cfg(test)] diff --git a/src/rust/bitbox02-sys/wrapper.h b/src/rust/bitbox02-sys/wrapper.h index 02fb1c790..dda3edfcf 100644 --- a/src/rust/bitbox02-sys/wrapper.h +++ b/src/rust/bitbox02-sys/wrapper.h @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include #include diff --git a/src/rust/bitbox02/src/app_btc.rs b/src/rust/bitbox02/src/app_btc.rs deleted file mode 100644 index e6cf53ad2..000000000 --- a/src/rust/bitbox02/src/app_btc.rs +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2020 Shift Crypto AG -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -extern crate alloc; -use alloc::vec::Vec; - -pub use bitbox02_sys::multisig_t as Multisig; - -pub fn pkscript_from_multisig( - multisig: &Multisig, - keypath_change: u32, - keypath_address: u32, -) -> Result, ()> { - let mut out = [0u8; bitbox02_sys::MAX_PK_SCRIPT_SIZE as usize]; - let mut out_len: usize = out.len() as _; - match unsafe { - bitbox02_sys::btc_common_pkscript_from_multisig( - multisig, - keypath_change, - keypath_address, - out.as_mut_ptr(), - &mut out_len, - ) - } { - true => Ok(out[..out_len].to_vec()), - false => Err(()), - } -} diff --git a/src/rust/bitbox02/src/lib.rs b/src/rust/bitbox02/src/lib.rs index 6259f5998..c05903549 100644 --- a/src/rust/bitbox02/src/lib.rs +++ b/src/rust/bitbox02/src/lib.rs @@ -30,8 +30,6 @@ extern crate lazy_static; #[cfg(feature = "testing")] pub mod testing; -#[cfg(any(feature = "app-bitcoin", feature = "app-litecoin"))] -pub mod app_btc; #[cfg(feature = "app-ethereum")] pub mod app_eth; pub mod bip32; diff --git a/test/unit-test/test_keystore_functional.c b/test/unit-test/test_keystore_functional.c index 993a83403..9de48edc4 100644 --- a/test/unit-test/test_keystore_functional.c +++ b/test/unit-test/test_keystore_functional.c @@ -19,7 +19,6 @@ #include -#include #include #include #include