-
Notifications
You must be signed in to change notification settings - Fork 35
fix standalone private keys encryption and usage #1959
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -298,33 +298,53 @@ fn create_wallet(chain_config: Arc<ChainConfig>) -> DefaultWallet { | |
| } | ||
|
|
||
| #[track_caller] | ||
| fn create_block<B, P>( | ||
| fn create_block_with_reward_address<B, P>( | ||
| chain_config: &Arc<ChainConfig>, | ||
| wallet: &mut Wallet<B, P>, | ||
| transactions: Vec<SignedTransaction>, | ||
| reward: Amount, | ||
| block_height: u64, | ||
| ) -> (Address<Destination>, Block) | ||
| address: Destination, | ||
| ) -> Block | ||
| where | ||
| B: storage::Backend + 'static, | ||
| P: SignerProvider, | ||
| { | ||
| let address = wallet.get_new_address(DEFAULT_ACCOUNT_INDEX).unwrap().1; | ||
|
|
||
| let block1 = Block::new( | ||
| transactions, | ||
| chain_config.genesis_block_id(), | ||
| chain_config.genesis_block().timestamp(), | ||
| ConsensusData::None, | ||
| BlockReward::new(vec![make_address_output( | ||
| address.clone().into_object(), | ||
| reward, | ||
| )]), | ||
| BlockReward::new(vec![make_address_output(address, reward)]), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| scan_wallet(wallet, BlockHeight::new(block_height), vec![block1.clone()]); | ||
| (address, block1) | ||
| block1 | ||
| } | ||
|
|
||
| #[track_caller] | ||
| fn create_block<B, P>( | ||
| chain_config: &Arc<ChainConfig>, | ||
| wallet: &mut Wallet<B, P>, | ||
| transactions: Vec<SignedTransaction>, | ||
| reward: Amount, | ||
| block_height: u64, | ||
| ) -> (Address<Destination>, Block) | ||
| where | ||
| B: storage::Backend + 'static, | ||
| P: SignerProvider, | ||
| { | ||
| let address = wallet.get_new_address(DEFAULT_ACCOUNT_INDEX).unwrap().1; | ||
| let block = create_block_with_reward_address( | ||
| chain_config, | ||
| wallet, | ||
| transactions, | ||
| reward, | ||
| block_height, | ||
| address.clone().into_object(), | ||
| ); | ||
| (address, block) | ||
| } | ||
|
|
||
| #[track_caller] | ||
|
|
@@ -1251,6 +1271,146 @@ fn locked_wallet_cant_sign_transaction(#[case] seed: Seed) { | |
| .unwrap(); | ||
| } | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[trace] | ||
| #[case(Seed::from_entropy())] | ||
| fn locked_wallet_standalone_keys( | ||
| #[case] seed: Seed, | ||
| #[values(true, false)] insert_before_encrypt: bool, | ||
| #[values(true, false)] change_password: bool, | ||
| ) { | ||
| let mut rng = make_seedable_rng(seed); | ||
| let chain_config = Arc::new(create_mainnet()); | ||
|
|
||
| let mut wallet = create_wallet(chain_config.clone()); | ||
|
|
||
| let coin_balance = get_coin_balance(&wallet); | ||
| assert_eq!(coin_balance, Amount::ZERO); | ||
|
|
||
| let (standalone_sk, standalone_pk) = | ||
| PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr); | ||
| let mut password = Some(gen_random_password(&mut rng)); | ||
|
|
||
| if insert_before_encrypt { | ||
| wallet | ||
| .add_standalone_private_key(DEFAULT_ACCOUNT_INDEX, standalone_sk, None) | ||
| .unwrap(); | ||
| wallet.encrypt_wallet(&password).unwrap(); | ||
| } else { | ||
| wallet.encrypt_wallet(&password).unwrap(); | ||
| wallet | ||
| .add_standalone_private_key(DEFAULT_ACCOUNT_INDEX, standalone_sk, None) | ||
| .unwrap(); | ||
| } | ||
|
|
||
| if change_password { | ||
| password = Some(gen_random_password(&mut rng)); | ||
| wallet.encrypt_wallet(&password).unwrap(); | ||
| } | ||
|
|
||
| let block1_amount = Amount::from_atoms(rng.gen_range(NETWORK_FEE + 1..NETWORK_FEE + 10000)); | ||
|
|
||
| let standalone_destination = if rng.gen::<bool>() { | ||
| Destination::PublicKey(standalone_pk) | ||
| } else { | ||
| Destination::PublicKeyHash((&standalone_pk).into()) | ||
| }; | ||
|
|
||
| if rng.gen::<bool>() { | ||
| // test that wallet will recognise a destination belonging to a standalone key in a block | ||
| // reward | ||
| let _ = create_block_with_reward_address( | ||
| &chain_config, | ||
| &mut wallet, | ||
| vec![], | ||
| block1_amount, | ||
| 0, | ||
| standalone_destination, | ||
| ); | ||
| } else { | ||
| // test that wallet will recognise a destination belonging to a standalone key in a | ||
| // transaction | ||
| let output = make_address_output(standalone_destination, block1_amount); | ||
|
|
||
| let tx = SignedTransaction::new(Transaction::new(0, vec![], vec![output]).unwrap(), vec![]) | ||
| .unwrap(); | ||
|
|
||
| let block1 = Block::new( | ||
| vec![tx.clone()], | ||
| chain_config.genesis_block_id(), | ||
| chain_config.genesis_block().timestamp(), | ||
| ConsensusData::None, | ||
| BlockReward::new(vec![]), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| scan_wallet(&mut wallet, BlockHeight::new(0), vec![block1.clone()]); | ||
|
|
||
| // check the transaction has been added to the wallet | ||
| let tx_data = wallet | ||
| .get_transaction(DEFAULT_ACCOUNT_INDEX, tx.transaction().get_id()) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(tx_data.get_transaction(), tx.transaction()); | ||
| } | ||
|
|
||
| wallet.lock_wallet().unwrap(); | ||
|
|
||
| // check balance is recognising spendable UTXOs belonging to the standalone private key | ||
| let coin_balance = get_coin_balance(&wallet); | ||
| assert_eq!(coin_balance, block1_amount); | ||
|
|
||
| // also check utxos | ||
| let utxos = wallet | ||
| .get_utxos( | ||
| DEFAULT_ACCOUNT_INDEX, | ||
| UtxoType::Transfer | UtxoType::LockThenTransfer | UtxoType::IssueNft, | ||
| UtxoState::Confirmed | UtxoState::Inactive, | ||
| WithLocked::Unlocked, | ||
| ) | ||
| .unwrap(); | ||
| assert_eq!(utxos.len(), 1); | ||
|
|
||
| // try to spend the UTXO belonging to the standalone key | ||
|
|
||
| let new_output = TxOutput::Transfer( | ||
| OutputValue::Coin(Amount::from_atoms( | ||
| rng.gen_range(1..=block1_amount.into_atoms() - NETWORK_FEE), | ||
| )), | ||
| Destination::AnyoneCanSpend, | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| wallet.create_transaction_to_addresses( | ||
| DEFAULT_ACCOUNT_INDEX, | ||
| [new_output.clone()], | ||
| SelectedInputs::Utxos(vec![]), | ||
| BTreeMap::new(), | ||
| FeeRate::from_amount_per_kb(Amount::ZERO), | ||
| FeeRate::from_amount_per_kb(Amount::ZERO), | ||
| TxAdditionalInfo::new(), | ||
| ), | ||
| Err(WalletError::DatabaseError( | ||
| wallet_storage::Error::WalletLocked | ||
| )) | ||
| ); | ||
|
|
||
| // success after unlock | ||
| wallet.unlock_wallet(&password.unwrap()).unwrap(); | ||
| wallet | ||
| .create_transaction_to_addresses( | ||
| DEFAULT_ACCOUNT_INDEX, | ||
| [new_output], | ||
| SelectedInputs::Utxos(vec![]), | ||
| BTreeMap::new(), | ||
| FeeRate::from_amount_per_kb(Amount::ZERO), | ||
| FeeRate::from_amount_per_kb(Amount::ZERO), | ||
| TxAdditionalInfo::new(), | ||
| ) | ||
| .unwrap(); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another scenario worth checking is:
Probably it's better to make it a separate test though. |
||
|
|
||
| #[rstest] | ||
| #[trace] | ||
| #[case(Seed::from_entropy())] | ||
|
|
@@ -5320,59 +5480,6 @@ fn test_not_exhaustion_of_keys(#[case] seed: Seed) { | |
| } | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[trace] | ||
| #[case(Seed::from_entropy())] | ||
| fn test_add_standalone_private_key(#[case] seed: Seed) { | ||
| let mut rng = make_seedable_rng(seed); | ||
| let chain_config = Arc::new(create_regtest()); | ||
|
|
||
| let mut wallet = create_wallet(chain_config.clone()); | ||
|
|
||
| let coin_balance = get_coin_balance(&wallet); | ||
| assert_eq!(coin_balance, Amount::ZERO); | ||
| // generate a random private key unrelated to the wallet and add it | ||
| let (private_key, pub_key) = | ||
| crypto::key::PrivateKey::new_from_rng(&mut rng, KeyKind::Secp256k1Schnorr); | ||
|
|
||
| wallet | ||
| .add_standalone_private_key(DEFAULT_ACCOUNT_INDEX, private_key, None) | ||
| .unwrap(); | ||
|
|
||
| // get the destination address from the new private key and send some coins to it | ||
| let address = | ||
| Address::new(&chain_config, Destination::PublicKeyHash((&pub_key).into())).unwrap(); | ||
|
|
||
| // Generate a new block which sends reward to the new address | ||
| let block1_amount = Amount::from_atoms(rng.gen_range(NETWORK_FEE + 100..NETWORK_FEE + 10000)); | ||
| let output = make_address_output(address.clone().into_object(), block1_amount); | ||
|
|
||
| let tx = | ||
| SignedTransaction::new(Transaction::new(0, vec![], vec![output]).unwrap(), vec![]).unwrap(); | ||
|
|
||
| let block1 = Block::new( | ||
| vec![tx.clone()], | ||
| chain_config.genesis_block_id(), | ||
| chain_config.genesis_block().timestamp(), | ||
| ConsensusData::None, | ||
| BlockReward::new(vec![]), | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| scan_wallet(&mut wallet, BlockHeight::new(0), vec![block1.clone()]); | ||
|
|
||
| // Check amount is still zero | ||
| let coin_balance = get_coin_balance(&wallet); | ||
| assert_eq!(coin_balance, Amount::ZERO); | ||
|
|
||
| // but the transaction has been added to the wallet | ||
| let tx_data = wallet | ||
| .get_transaction(DEFAULT_ACCOUNT_INDEX, tx.transaction().get_id()) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(tx_data.get_transaction(), tx.transaction()); | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[trace] | ||
| #[case(Seed::from_entropy())] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, I guess there is no test that covers this fix. Can you add one?