Skip to content

Commit

Permalink
wip(feat): panics when genesis hash and network mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
oleonardolima committed Dec 16, 2024
1 parent dbc6a1e commit 2d35535
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ impl Wallet {
let genesis_hash = params
.genesis_hash
.unwrap_or(genesis_block(network).block_hash());

if genesis_hash.ne(&genesis_block(network).block_hash()) {
panic!("unexpected genesis hash check result: genesis_hash (param) mismatch genesis_hash (network)")
}

let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash);

let (descriptor, mut descriptor_keymap) = (params.descriptor)(&secp, network)?;
Expand Down
15 changes: 15 additions & 0 deletions crates/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@ fn test_error_external_and_internal_are_the_same() {
);
}

#[test]
#[should_panic]
fn test_create_genesis_hash_and_network_consistency() {
let (external_desc, internal_desc) = get_test_tr_single_sig_xprv_and_change_desc();
let mainnet_genesis_hash = BlockHash::from_byte_array(ChainHash::BITCOIN.to_bytes());
let _wallet_err = Wallet::create(external_desc, internal_desc)
.genesis_hash(mainnet_genesis_hash)
.network(Network::Regtest)
.create_wallet_no_persist();
// assert!(
// matches!(err, Err(DescriptorError::NetworkMismatch)),
// "unexpected network check result: Regtest (network) is not Mainnet (genesis_hash)"
// )
}

#[test]
fn test_descriptor_checksum() {
let (wallet, _) = get_funded_wallet_wpkh();
Expand Down

0 comments on commit 2d35535

Please sign in to comment.