@@ -126,6 +126,10 @@ impl WalletPlatformChecker for ManagedWalletInfo {
126126 key_source : Option < & KeySource > ,
127127 ) -> bool {
128128 if let Some ( account) = self . accounts . platform_payment_accounts . get_mut ( account_key) {
129+ // Verify the address belongs to this account before modifying
130+ if !account. contains_platform_address ( & address) {
131+ return false ;
132+ }
129133 account. set_address_credit_balance ( address, credit_balance, key_source) ;
130134 true
131135 } else {
@@ -157,6 +161,10 @@ impl WalletPlatformChecker for ManagedWalletInfo {
157161 key_source : Option < & KeySource > ,
158162 ) -> Option < u64 > {
159163 if let Some ( account) = self . accounts . platform_payment_accounts . get_mut ( account_key) {
164+ // Verify the address belongs to this account before modifying
165+ if !account. contains_platform_address ( & address) {
166+ return None ;
167+ }
160168 let new_balance = account. add_address_credit_balance ( address, amount, key_source) ;
161169 Some ( new_balance)
162170 } else {
@@ -231,7 +239,7 @@ mod tests {
231239
232240 // Create and add a platform account
233241 let pool = create_test_pool ( ) ;
234- let mut account = ManagedPlatformAccount :: new ( 0 , 0 , Network :: Testnet , pool, false ) ;
242+ let mut account = ManagedPlatformAccount :: new ( 0 , 0 , pool, false ) ;
235243
236244 // Add some balance
237245 let addr = PlatformP2PKHAddress :: new ( [ 0x11 ; 20 ] ) ;
@@ -253,13 +261,13 @@ mod tests {
253261
254262 // Create first platform account
255263 let pool1 = create_test_pool ( ) ;
256- let mut account1 = ManagedPlatformAccount :: new ( 0 , 0 , Network :: Testnet , pool1, false ) ;
264+ let mut account1 = ManagedPlatformAccount :: new ( 0 , 0 , pool1, false ) ;
257265 let addr1 = PlatformP2PKHAddress :: new ( [ 0x11 ; 20 ] ) ;
258266 account1. set_address_credit_balance ( addr1, 3000 , None ) ;
259267
260268 // Create second platform account
261269 let pool2 = create_test_pool ( ) ;
262- let mut account2 = ManagedPlatformAccount :: new ( 1 , 0 , Network :: Testnet , pool2, false ) ;
270+ let mut account2 = ManagedPlatformAccount :: new ( 1 , 0 , pool2, false ) ;
263271 let addr2 = PlatformP2PKHAddress :: new ( [ 0x22 ; 20 ] ) ;
264272 account2. set_address_credit_balance ( addr2, 2000 , None ) ;
265273
@@ -288,7 +296,7 @@ mod tests {
288296
289297 // Create platform account
290298 let pool = create_test_pool ( ) ;
291- let mut account = ManagedPlatformAccount :: new ( 0 , 0 , Network :: Testnet , pool, false ) ;
299+ let mut account = ManagedPlatformAccount :: new ( 0 , 0 , pool, false ) ;
292300 let addr = PlatformP2PKHAddress :: new ( [ 0x11 ; 20 ] ) ;
293301 account. set_address_credit_balance ( addr, 1000 , None ) ;
294302
@@ -315,7 +323,7 @@ mod tests {
315323
316324 // Create platform account
317325 let pool = create_test_pool ( ) ;
318- let mut account = ManagedPlatformAccount :: new ( 0 , 0 , Network :: Testnet , pool, false ) ;
326+ let mut account = ManagedPlatformAccount :: new ( 0 , 0 , pool, false ) ;
319327 let addr = PlatformP2PKHAddress :: new ( [ 0x11 ; 20 ] ) ;
320328 account. set_address_credit_balance ( addr, 1000 , None ) ;
321329
@@ -342,7 +350,7 @@ mod tests {
342350
343351 // Create platform account
344352 let pool = create_test_pool ( ) ;
345- let mut account = ManagedPlatformAccount :: new ( 0 , 0 , Network :: Testnet , pool, false ) ;
353+ let mut account = ManagedPlatformAccount :: new ( 0 , 0 , pool, false ) ;
346354 let addr = PlatformP2PKHAddress :: new ( [ 0x11 ; 20 ] ) ;
347355 account. set_address_credit_balance ( addr, 5000 , None ) ;
348356
@@ -367,7 +375,7 @@ mod tests {
367375
368376 // Create platform account
369377 let pool = create_test_pool ( ) ;
370- let mut account = ManagedPlatformAccount :: new ( 0 , 0 , Network :: Testnet , pool, false ) ;
378+ let mut account = ManagedPlatformAccount :: new ( 0 , 0 , pool, false ) ;
371379 let addr = PlatformP2PKHAddress :: new ( [ 0x11 ; 20 ] ) ;
372380 account. set_address_credit_balance ( addr, 3000 , None ) ;
373381
@@ -391,22 +399,31 @@ mod tests {
391399 fn test_set_platform_address_balance_for_account ( ) {
392400 let mut wallet_info = create_test_wallet_info ( ) ;
393401
394- // Create platform account
402+ // Create platform account with an address already having a balance
395403 let pool = create_test_pool ( ) ;
396- let account = ManagedPlatformAccount :: new ( 0 , 0 , Network :: Testnet , pool, false ) ;
404+ let mut account = ManagedPlatformAccount :: new ( 0 , 0 , pool, false ) ;
405+ let addr = PlatformP2PKHAddress :: new ( [ 0x11 ; 20 ] ) ;
406+ // First set initial balance so the address is known to the account
407+ account. set_address_credit_balance ( addr, 1000 , None ) ;
397408
398409 let key = PlatformPaymentAccountKey {
399410 account : 0 ,
400411 key_class : 0 ,
401412 } ;
402413 wallet_info. accounts . platform_payment_accounts . insert ( key, account) ;
403414
404- // Set balance using account key
405- let addr = PlatformP2PKHAddress :: new ( [ 0x11 ; 20 ] ) ;
415+ // Update balance using account key - should succeed since address is known
406416 let result = wallet_info. set_platform_address_balance_for_account ( & key, addr, 5000 , None ) ;
407417 assert ! ( result) ;
408418 assert_eq ! ( wallet_info. platform_credit_balance( ) , 5000 ) ;
409419
420+ // Try with address not belonging to the account - should fail
421+ let unknown_addr = PlatformP2PKHAddress :: new ( [ 0xFF ; 20 ] ) ;
422+ let result =
423+ wallet_info. set_platform_address_balance_for_account ( & key, unknown_addr, 2000 , None ) ;
424+ assert ! ( !result) ;
425+ assert_eq ! ( wallet_info. platform_credit_balance( ) , 5000 ) ; // Balance unchanged
426+
410427 // Try with non-existent account
411428 let bad_key = PlatformPaymentAccountKey {
412429 account : 99 ,
0 commit comments