@@ -298,34 +298,29 @@ fn create_wallet(chain_config: Arc<ChainConfig>) -> DefaultWallet {
298298}
299299
300300#[ track_caller]
301- fn create_block_with_address_reward < B , P > (
301+ fn create_block_with_reward_address < B , P > (
302302 chain_config : & Arc < ChainConfig > ,
303303 wallet : & mut Wallet < B , P > ,
304304 transactions : Vec < SignedTransaction > ,
305305 reward : Amount ,
306306 block_height : u64 ,
307307 address : Destination ,
308- ) -> ( Address < Destination > , Block )
308+ ) -> Block
309309where
310310 B : storage:: Backend + ' static ,
311311 P : SignerProvider ,
312312{
313- let address = Address :: new ( chain_config, address) . unwrap ( ) ;
314-
315313 let block1 = Block :: new (
316314 transactions,
317315 chain_config. genesis_block_id ( ) ,
318316 chain_config. genesis_block ( ) . timestamp ( ) ,
319317 ConsensusData :: None ,
320- BlockReward :: new ( vec ! [ make_address_output(
321- address. clone( ) . into_object( ) ,
322- reward,
323- ) ] ) ,
318+ BlockReward :: new ( vec ! [ make_address_output( address, reward) ] ) ,
324319 )
325320 . unwrap ( ) ;
326321
327322 scan_wallet ( wallet, BlockHeight :: new ( block_height) , vec ! [ block1. clone( ) ] ) ;
328- ( address , block1)
323+ block1
329324}
330325
331326#[ track_caller]
@@ -341,14 +336,15 @@ where
341336 P : SignerProvider ,
342337{
343338 let address = wallet. get_new_address ( DEFAULT_ACCOUNT_INDEX ) . unwrap ( ) . 1 ;
344- create_block_with_address_reward (
339+ let block = create_block_with_reward_address (
345340 chain_config,
346341 wallet,
347342 transactions,
348343 reward,
349344 block_height,
350- address. into_object ( ) ,
351- )
345+ address. clone ( ) . into_object ( ) ,
346+ ) ;
347+ ( address, block)
352348}
353349
354350#[ track_caller]
@@ -1279,7 +1275,11 @@ fn locked_wallet_cant_sign_transaction(#[case] seed: Seed) {
12791275#[ rstest]
12801276#[ trace]
12811277#[ case( Seed :: from_entropy( ) ) ]
1282- fn locked_wallet_standalone_keys ( #[ case] seed : Seed ) {
1278+ fn locked_wallet_standalone_keys (
1279+ #[ case] seed : Seed ,
1280+ #[ values( true , false ) ] insert_before_encrypt : bool ,
1281+ #[ values( true , false ) ] change_password : bool ,
1282+ ) {
12831283 let mut rng = make_seedable_rng ( seed) ;
12841284 let chain_config = Arc :: new ( create_mainnet ( ) ) ;
12851285
@@ -1290,28 +1290,90 @@ fn locked_wallet_standalone_keys(#[case] seed: Seed) {
12901290
12911291 let ( standalone_sk, standalone_pk) =
12921292 PrivateKey :: new_from_rng ( & mut rng, KeyKind :: Secp256k1Schnorr ) ;
1293- wallet
1294- . add_standalone_private_key ( DEFAULT_ACCOUNT_INDEX , standalone_sk, None )
1295- . unwrap ( ) ;
1293+ let mut password = Some ( gen_random_password ( & mut rng) ) ;
1294+
1295+ if insert_before_encrypt {
1296+ wallet
1297+ . add_standalone_private_key ( DEFAULT_ACCOUNT_INDEX , standalone_sk, None )
1298+ . unwrap ( ) ;
1299+ wallet. encrypt_wallet ( & password) . unwrap ( ) ;
1300+ } else {
1301+ wallet. encrypt_wallet ( & password) . unwrap ( ) ;
1302+ wallet
1303+ . add_standalone_private_key ( DEFAULT_ACCOUNT_INDEX , standalone_sk, None )
1304+ . unwrap ( ) ;
1305+ }
1306+
1307+ if change_password {
1308+ password = Some ( gen_random_password ( & mut rng) ) ;
1309+ wallet. encrypt_wallet ( & password) . unwrap ( ) ;
1310+ }
12961311
1297- // Generate a new block which sends reward to the wallet
12981312 let block1_amount = Amount :: from_atoms ( rng. gen_range ( NETWORK_FEE + 1 ..NETWORK_FEE + 10000 ) ) ;
1299- let _ = create_block_with_address_reward (
1300- & chain_config,
1301- & mut wallet,
1302- vec ! [ ] ,
1303- block1_amount,
1304- 0 ,
1305- Destination :: PublicKey ( standalone_pk) ,
1306- ) ;
13071313
1308- let password = Some ( gen_random_password ( & mut rng) ) ;
1309- wallet. encrypt_wallet ( & password) . unwrap ( ) ;
1314+ let standalone_destination = if rng. gen :: < bool > ( ) {
1315+ Destination :: PublicKey ( standalone_pk)
1316+ } else {
1317+ Destination :: PublicKeyHash ( ( & standalone_pk) . into ( ) )
1318+ } ;
1319+
1320+ if rng. gen :: < bool > ( ) {
1321+ // test that wallet will recognise a destination belonging to a standalone key in a block
1322+ // reward
1323+ let _ = create_block_with_reward_address (
1324+ & chain_config,
1325+ & mut wallet,
1326+ vec ! [ ] ,
1327+ block1_amount,
1328+ 0 ,
1329+ standalone_destination,
1330+ ) ;
1331+ } else {
1332+ // test that wallet will recognise a destination belonging to a standalone key in a
1333+ // transaction
1334+ let output = make_address_output ( standalone_destination, block1_amount) ;
1335+
1336+ let tx = SignedTransaction :: new ( Transaction :: new ( 0 , vec ! [ ] , vec ! [ output] ) . unwrap ( ) , vec ! [ ] )
1337+ . unwrap ( ) ;
1338+
1339+ let block1 = Block :: new (
1340+ vec ! [ tx. clone( ) ] ,
1341+ chain_config. genesis_block_id ( ) ,
1342+ chain_config. genesis_block ( ) . timestamp ( ) ,
1343+ ConsensusData :: None ,
1344+ BlockReward :: new ( vec ! [ ] ) ,
1345+ )
1346+ . unwrap ( ) ;
1347+
1348+ scan_wallet ( & mut wallet, BlockHeight :: new ( 0 ) , vec ! [ block1. clone( ) ] ) ;
1349+
1350+ // check the transaction has been added to the wallet
1351+ let tx_data = wallet
1352+ . get_transaction ( DEFAULT_ACCOUNT_INDEX , tx. transaction ( ) . get_id ( ) )
1353+ . unwrap ( ) ;
1354+
1355+ assert_eq ! ( tx_data. get_transaction( ) , tx. transaction( ) ) ;
1356+ }
1357+
13101358 wallet. lock_wallet ( ) . unwrap ( ) ;
13111359
1360+ // check balance is recognising spendable UTXOs belonging to the standalone private key
13121361 let coin_balance = get_coin_balance ( & wallet) ;
13131362 assert_eq ! ( coin_balance, block1_amount) ;
13141363
1364+ // also check utxos
1365+ let utxos = wallet
1366+ . get_utxos (
1367+ DEFAULT_ACCOUNT_INDEX ,
1368+ UtxoType :: Transfer | UtxoType :: LockThenTransfer | UtxoType :: IssueNft ,
1369+ UtxoState :: Confirmed | UtxoState :: Inactive ,
1370+ WithLocked :: Unlocked ,
1371+ )
1372+ . unwrap ( ) ;
1373+ assert_eq ! ( utxos. len( ) , 1 ) ;
1374+
1375+ // try to spend the UTXO belonging to the standalone key
1376+
13151377 let new_output = TxOutput :: Transfer (
13161378 OutputValue :: Coin ( Amount :: from_atoms (
13171379 rng. gen_range ( 1 ..=block1_amount. into_atoms ( ) - NETWORK_FEE ) ,
@@ -1336,49 +1398,17 @@ fn locked_wallet_standalone_keys(#[case] seed: Seed) {
13361398
13371399 // success after unlock
13381400 wallet. unlock_wallet ( & password. unwrap ( ) ) . unwrap ( ) ;
1339- if rng. gen :: < bool > ( ) {
1340- wallet
1341- . create_transaction_to_addresses (
1342- DEFAULT_ACCOUNT_INDEX ,
1343- [ new_output] ,
1344- SelectedInputs :: Utxos ( vec ! [ ] ) ,
1345- BTreeMap :: new ( ) ,
1346- FeeRate :: from_amount_per_kb ( Amount :: ZERO ) ,
1347- FeeRate :: from_amount_per_kb ( Amount :: ZERO ) ,
1348- TxAdditionalInfo :: new ( ) ,
1349- )
1350- . unwrap ( ) ;
1351- } else {
1352- // check if we remove the password it should fail to lock
1353- wallet. encrypt_wallet ( & None ) . unwrap ( ) ;
1354-
1355- let err = wallet. lock_wallet ( ) . unwrap_err ( ) ;
1356- assert_eq ! (
1357- err,
1358- WalletError :: DatabaseError ( wallet_storage:: Error :: WalletLockedWithoutAPassword )
1359- ) ;
1360-
1361- // check that the kdf challenge has been deleted
1362- assert ! ( wallet
1363- . db
1364- . transaction_ro( )
1365- . unwrap( )
1366- . get_encryption_key_kdf_challenge( )
1367- . unwrap( )
1368- . is_none( ) ) ;
1369-
1370- wallet
1371- . create_transaction_to_addresses (
1372- DEFAULT_ACCOUNT_INDEX ,
1373- [ new_output] ,
1374- SelectedInputs :: Utxos ( vec ! [ ] ) ,
1375- BTreeMap :: new ( ) ,
1376- FeeRate :: from_amount_per_kb ( Amount :: ZERO ) ,
1377- FeeRate :: from_amount_per_kb ( Amount :: ZERO ) ,
1378- TxAdditionalInfo :: new ( ) ,
1379- )
1380- . unwrap ( ) ;
1381- }
1401+ wallet
1402+ . create_transaction_to_addresses (
1403+ DEFAULT_ACCOUNT_INDEX ,
1404+ [ new_output] ,
1405+ SelectedInputs :: Utxos ( vec ! [ ] ) ,
1406+ BTreeMap :: new ( ) ,
1407+ FeeRate :: from_amount_per_kb ( Amount :: ZERO ) ,
1408+ FeeRate :: from_amount_per_kb ( Amount :: ZERO ) ,
1409+ TxAdditionalInfo :: new ( ) ,
1410+ )
1411+ . unwrap ( ) ;
13821412}
13831413
13841414#[ rstest]
@@ -5450,59 +5480,6 @@ fn test_not_exhaustion_of_keys(#[case] seed: Seed) {
54505480 }
54515481}
54525482
5453- #[ rstest]
5454- #[ trace]
5455- #[ case( Seed :: from_entropy( ) ) ]
5456- fn test_add_standalone_private_key ( #[ case] seed : Seed ) {
5457- let mut rng = make_seedable_rng ( seed) ;
5458- let chain_config = Arc :: new ( create_regtest ( ) ) ;
5459-
5460- let mut wallet = create_wallet ( chain_config. clone ( ) ) ;
5461-
5462- let coin_balance = get_coin_balance ( & wallet) ;
5463- assert_eq ! ( coin_balance, Amount :: ZERO ) ;
5464- // generate a random private key unrelated to the wallet and add it
5465- let ( private_key, pub_key) =
5466- crypto:: key:: PrivateKey :: new_from_rng ( & mut rng, KeyKind :: Secp256k1Schnorr ) ;
5467-
5468- wallet
5469- . add_standalone_private_key ( DEFAULT_ACCOUNT_INDEX , private_key, None )
5470- . unwrap ( ) ;
5471-
5472- // get the destination address from the new private key and send some coins to it
5473- let address =
5474- Address :: new ( & chain_config, Destination :: PublicKeyHash ( ( & pub_key) . into ( ) ) ) . unwrap ( ) ;
5475-
5476- // Generate a new block which sends reward to the new address
5477- let block1_amount = Amount :: from_atoms ( rng. gen_range ( NETWORK_FEE + 100 ..NETWORK_FEE + 10000 ) ) ;
5478- let output = make_address_output ( address. clone ( ) . into_object ( ) , block1_amount) ;
5479-
5480- let tx =
5481- SignedTransaction :: new ( Transaction :: new ( 0 , vec ! [ ] , vec ! [ output] ) . unwrap ( ) , vec ! [ ] ) . unwrap ( ) ;
5482-
5483- let block1 = Block :: new (
5484- vec ! [ tx. clone( ) ] ,
5485- chain_config. genesis_block_id ( ) ,
5486- chain_config. genesis_block ( ) . timestamp ( ) ,
5487- ConsensusData :: None ,
5488- BlockReward :: new ( vec ! [ ] ) ,
5489- )
5490- . unwrap ( ) ;
5491-
5492- scan_wallet ( & mut wallet, BlockHeight :: new ( 0 ) , vec ! [ block1. clone( ) ] ) ;
5493-
5494- // Check amount is still zero
5495- let coin_balance = get_coin_balance ( & wallet) ;
5496- assert_eq ! ( coin_balance, block1_amount) ;
5497-
5498- // but the transaction has been added to the wallet
5499- let tx_data = wallet
5500- . get_transaction ( DEFAULT_ACCOUNT_INDEX , tx. transaction ( ) . get_id ( ) )
5501- . unwrap ( ) ;
5502-
5503- assert_eq ! ( tx_data. get_transaction( ) , tx. transaction( ) ) ;
5504- }
5505-
55065483#[ rstest]
55075484#[ trace]
55085485#[ case( Seed :: from_entropy( ) ) ]
0 commit comments