44//! Platform Payment account balances (DIP-17).
55
66use crate :: account:: account_collection:: PlatformPaymentAccountKey ;
7+ use crate :: managed_account:: address_pool:: KeySource ;
78use crate :: managed_account:: platform_address:: PlatformP2PKHAddress ;
89use crate :: wallet:: managed_wallet_info:: ManagedWalletInfo ;
910
@@ -20,43 +21,55 @@ pub trait WalletPlatformChecker {
2021 /// Set the credit balance for a specific platform address
2122 ///
2223 /// The address must belong to one of the Platform Payment accounts.
24+ /// If `key_source` is provided and the address becomes funded (0 → non-zero),
25+ /// the address will be marked as used and gap limit will be maintained.
2326 /// Returns true if the address was found and the balance was set.
2427 fn set_platform_address_balance (
2528 & mut self ,
2629 address : & PlatformP2PKHAddress ,
2730 credit_balance : u64 ,
31+ key_source : Option < & KeySource > ,
2832 ) -> bool ;
2933
3034 /// Set the credit balance for a specific platform address by account key
3135 ///
3236 /// This is more efficient when you know which account the address belongs to.
37+ /// If `key_source` is provided and the address becomes funded (0 → non-zero),
38+ /// the address will be marked as used and gap limit will be maintained.
3339 /// Returns true if the account was found and the balance was set.
3440 fn set_platform_address_balance_for_account (
3541 & mut self ,
3642 account_key : & PlatformPaymentAccountKey ,
3743 address : PlatformP2PKHAddress ,
3844 credit_balance : u64 ,
45+ key_source : Option < & KeySource > ,
3946 ) -> bool ;
4047
4148 /// Increase the credit balance for a specific platform address
4249 ///
4350 /// The address must belong to one of the Platform Payment accounts.
51+ /// If `key_source` is provided and the address becomes funded (0 → non-zero),
52+ /// the address will be marked as used and gap limit will be maintained.
4453 /// Returns the new balance if successful, None if the address was not found.
4554 fn increase_platform_address_balance (
4655 & mut self ,
4756 address : & PlatformP2PKHAddress ,
4857 amount : u64 ,
58+ key_source : Option < & KeySource > ,
4959 ) -> Option < u64 > ;
5060
5161 /// Increase the credit balance for a specific platform address by account key
5262 ///
5363 /// This is more efficient when you know which account the address belongs to.
64+ /// If `key_source` is provided and the address becomes funded (0 → non-zero),
65+ /// the address will be marked as used and gap limit will be maintained.
5466 /// Returns the new balance if successful, None if the account was not found.
5567 fn increase_platform_address_balance_for_account (
5668 & mut self ,
5769 account_key : & PlatformPaymentAccountKey ,
5870 address : PlatformP2PKHAddress ,
5971 amount : u64 ,
72+ key_source : Option < & KeySource > ,
6073 ) -> Option < u64 > ;
6174
6275 /// Decrease the credit balance for a specific platform address
@@ -93,13 +106,12 @@ impl WalletPlatformChecker for ManagedWalletInfo {
93106 & mut self ,
94107 address : & PlatformP2PKHAddress ,
95108 credit_balance : u64 ,
109+ key_source : Option < & KeySource > ,
96110 ) -> bool {
97111 // Find the account that contains this address
98112 for account in self . accounts . platform_payment_accounts . values_mut ( ) {
99113 if account. contains_platform_address ( address) {
100- // Note: Gap limit maintenance should be done at a higher level
101- // where the Account object is available
102- account. set_address_credit_balance ( * address, credit_balance, None ) ;
114+ account. set_address_credit_balance ( * address, credit_balance, key_source) ;
103115 return true ;
104116 }
105117 }
@@ -111,11 +123,10 @@ impl WalletPlatformChecker for ManagedWalletInfo {
111123 account_key : & PlatformPaymentAccountKey ,
112124 address : PlatformP2PKHAddress ,
113125 credit_balance : u64 ,
126+ key_source : Option < & KeySource > ,
114127 ) -> bool {
115128 if let Some ( account) = self . accounts . platform_payment_accounts . get_mut ( account_key) {
116- // Note: Gap limit maintenance should be done at a higher level
117- // where the Account object is available
118- account. set_address_credit_balance ( address, credit_balance, None ) ;
129+ account. set_address_credit_balance ( address, credit_balance, key_source) ;
119130 true
120131 } else {
121132 false
@@ -126,13 +137,12 @@ impl WalletPlatformChecker for ManagedWalletInfo {
126137 & mut self ,
127138 address : & PlatformP2PKHAddress ,
128139 amount : u64 ,
140+ key_source : Option < & KeySource > ,
129141 ) -> Option < u64 > {
130142 // Find the account that contains this address
131143 for account in self . accounts . platform_payment_accounts . values_mut ( ) {
132144 if account. contains_platform_address ( address) {
133- // Note: Gap limit maintenance should be done at a higher level
134- // where the Account object is available
135- let new_balance = account. add_address_credit_balance ( * address, amount, None ) ;
145+ let new_balance = account. add_address_credit_balance ( * address, amount, key_source) ;
136146 return Some ( new_balance) ;
137147 }
138148 }
@@ -144,11 +154,10 @@ impl WalletPlatformChecker for ManagedWalletInfo {
144154 account_key : & PlatformPaymentAccountKey ,
145155 address : PlatformP2PKHAddress ,
146156 amount : u64 ,
157+ key_source : Option < & KeySource > ,
147158 ) -> Option < u64 > {
148159 if let Some ( account) = self . accounts . platform_payment_accounts . get_mut ( account_key) {
149- // Note: Gap limit maintenance should be done at a higher level
150- // where the Account object is available
151- let new_balance = account. add_address_credit_balance ( address, amount, None ) ;
160+ let new_balance = account. add_address_credit_balance ( address, amount, key_source) ;
152161 Some ( new_balance)
153162 } else {
154163 None
@@ -290,13 +299,13 @@ mod tests {
290299 wallet_info. accounts . platform_payment_accounts . insert ( key, account) ;
291300
292301 // Set balance for existing address
293- let result = wallet_info. set_platform_address_balance ( & addr, 5000 ) ;
302+ let result = wallet_info. set_platform_address_balance ( & addr, 5000 , None ) ;
294303 assert ! ( result) ;
295304 assert_eq ! ( wallet_info. platform_credit_balance( ) , 5000 ) ;
296305
297306 // Try to set balance for non-existent address
298307 let unknown_addr = PlatformP2PKHAddress :: new ( [ 0xFF ; 20 ] ) ;
299- let result = wallet_info. set_platform_address_balance ( & unknown_addr, 1000 ) ;
308+ let result = wallet_info. set_platform_address_balance ( & unknown_addr, 1000 , None ) ;
300309 assert ! ( !result) ;
301310 }
302311
@@ -317,12 +326,12 @@ mod tests {
317326 wallet_info. accounts . platform_payment_accounts . insert ( key, account) ;
318327
319328 // Increase balance
320- let new_balance = wallet_info. increase_platform_address_balance ( & addr, 500 ) ;
329+ let new_balance = wallet_info. increase_platform_address_balance ( & addr, 500 , None ) ;
321330 assert_eq ! ( new_balance, Some ( 1500 ) ) ;
322331 assert_eq ! ( wallet_info. platform_credit_balance( ) , 1500 ) ;
323332
324333 // Increase again
325- let new_balance = wallet_info. increase_platform_address_balance ( & addr, 1000 ) ;
334+ let new_balance = wallet_info. increase_platform_address_balance ( & addr, 1000 , None ) ;
326335 assert_eq ! ( new_balance, Some ( 2500 ) ) ;
327336 assert_eq ! ( wallet_info. platform_credit_balance( ) , 2500 ) ;
328337 }
@@ -394,7 +403,7 @@ mod tests {
394403
395404 // Set balance using account key
396405 let addr = PlatformP2PKHAddress :: new ( [ 0x11 ; 20 ] ) ;
397- let result = wallet_info. set_platform_address_balance_for_account ( & key, addr, 5000 ) ;
406+ let result = wallet_info. set_platform_address_balance_for_account ( & key, addr, 5000 , None ) ;
398407 assert ! ( result) ;
399408 assert_eq ! ( wallet_info. platform_credit_balance( ) , 5000 ) ;
400409
@@ -403,7 +412,8 @@ mod tests {
403412 account : 99 ,
404413 key_class : 0 ,
405414 } ;
406- let result = wallet_info. set_platform_address_balance_for_account ( & bad_key, addr, 1000 ) ;
415+ let result =
416+ wallet_info. set_platform_address_balance_for_account ( & bad_key, addr, 1000 , None ) ;
407417 assert ! ( !result) ;
408418 }
409419}
0 commit comments