Skip to content

Commit 42bc075

Browse files
committed
bitcoin: pre-process script configs
By converting the script configs into a parsed/validated form, we gain the following advantages: - for policies, we only parse/validate it once per tx, instead of also twice per input (compute pk_script, compute sighash_script), and twice for each change output (to check if it is really change and to compute the payload), making policy transactions much more efficient - the match patterns are much simpler as it removes SimpleType parsing and all the protobuf nesting and unreachable `_ => ...` patterns - we will be able to check if the script config is a Taproot policy in `is_taproot()` without parsing it once more once we add Taproot policies
1 parent 34bc408 commit 42bc075

File tree

4 files changed

+133
-133
lines changed

4 files changed

+133
-133
lines changed

src/rust/bitbox02-rust/src/hww/api/bitcoin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod payment_request;
2525
mod policies;
2626
mod registration;
2727
mod script;
28+
mod script_configs;
2829
pub mod signmsg;
2930
pub mod signtx;
3031

src/rust/bitbox02-rust/src/hww/api/bitcoin/common.rs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use pb::btc_script_config::SimpleType;
2424
pub use pb::btc_sign_init_request::FormatUnit;
2525
pub use pb::{BtcCoin, BtcOutputType};
2626

27+
use super::script_configs::{ValidatedScriptConfig, ValidatedScriptConfigWithKeypath};
2728
use super::{multisig, params::Params, script};
2829

2930
use sha2::{Digest, Sha256};
@@ -169,39 +170,19 @@ impl Payload {
169170
xpub_cache: &mut Bip32XpubCache,
170171
params: &Params,
171172
keypath: &[u32],
172-
script_config_account: &pb::BtcScriptConfigWithKeypath,
173+
script_config_account: &ValidatedScriptConfigWithKeypath,
173174
) -> Result<Self, Error> {
174-
match script_config_account {
175-
pb::BtcScriptConfigWithKeypath {
176-
script_config:
177-
Some(pb::BtcScriptConfig {
178-
config: Some(pb::btc_script_config::Config::SimpleType(simple_type)),
179-
}),
180-
..
181-
} => {
182-
let simple_type = pb::btc_script_config::SimpleType::try_from(*simple_type)?;
183-
Self::from_simple(xpub_cache, params, simple_type, keypath)
175+
match &script_config_account.config {
176+
ValidatedScriptConfig::SimpleType(simple_type) => {
177+
Self::from_simple(xpub_cache, params, *simple_type, keypath)
184178
}
185-
pb::BtcScriptConfigWithKeypath {
186-
script_config:
187-
Some(pb::BtcScriptConfig {
188-
config: Some(pb::btc_script_config::Config::Multisig(multisig)),
189-
}),
190-
..
191-
} => Self::from_multisig(
179+
ValidatedScriptConfig::Multisig(multisig) => Self::from_multisig(
192180
params,
193181
multisig,
194182
keypath[keypath.len() - 2],
195183
keypath[keypath.len() - 1],
196184
),
197-
pb::BtcScriptConfigWithKeypath {
198-
script_config:
199-
Some(pb::BtcScriptConfig {
200-
config: Some(pb::btc_script_config::Config::Policy(policy)),
201-
}),
202-
..
203-
} => Self::from_policy(&super::policies::parse(policy)?, keypath),
204-
_ => Err(Error::InvalidInput),
185+
ValidatedScriptConfig::Policy(policy) => Self::from_policy(policy, keypath),
205186
}
206187
}
207188

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use super::pb;
16+
use pb::btc_script_config::{Multisig, SimpleType};
17+
18+
use super::policies::ParsedPolicy;
19+
20+
/// Parsed and validated form of `pb::BtcScriptConfig`.
21+
pub enum ValidatedScriptConfig<'a> {
22+
SimpleType(SimpleType),
23+
Multisig(&'a Multisig),
24+
Policy(ParsedPolicy<'a>),
25+
}
26+
27+
/// Parsed and validated form of `pb::BtcScriptConfigWithKeypath`.
28+
pub struct ValidatedScriptConfigWithKeypath<'a> {
29+
pub keypath: &'a [u32],
30+
pub config: ValidatedScriptConfig<'a>,
31+
}

0 commit comments

Comments
 (0)