@@ -51,6 +51,7 @@ import org.bitcoinj.core.Context
5151import org.bitcoinj.core.ECKey
5252import org.bitcoinj.core.InsufficientMoneyException
5353import org.bitcoinj.core.Transaction
54+ import org.bitcoinj.evolution.AssetLockTransaction
5455import org.bitcoinj.utils.ExchangeRate
5556import org.bitcoinj.wallet.AuthenticationKeyChain
5657import org.bitcoinj.wallet.SendRequest
@@ -261,7 +262,8 @@ class SendCoinsViewModel @Inject constructor(
261262 editedAmount : Coin ,
262263 exchangeRate : ExchangeRate ? ,
263264 checkBalance : Boolean ,
264- key : ECKey
265+ key : ECKey ,
266+ emptyWallet : Boolean
265267 ): Transaction {
266268 _state .value = State .SENDING
267269 if (! isAssetLock) {
@@ -270,7 +272,7 @@ class SendCoinsViewModel @Inject constructor(
270272 val finalPaymentIntent = basePaymentIntent.mergeWithEditedValues(editedAmount, null )
271273
272274 val transaction = try {
273- val finalSendRequest = sendCoinsTaskRunner.createAssetLockSendRequest(
275+ var finalSendRequest = sendCoinsTaskRunner.createAssetLockSendRequest(
274276 basePaymentIntent.mayEditAmount(),
275277 finalPaymentIntent,
276278 true ,
@@ -280,6 +282,25 @@ class SendCoinsViewModel @Inject constructor(
280282 finalSendRequest.memo = basePaymentIntent.memo
281283 finalSendRequest.exchangeRate = exchangeRate
282284
285+ if (emptyWallet) {
286+ sendCoinsTaskRunner.signSendRequest(finalSendRequest)
287+ wallet.completeTx(finalSendRequest)
288+
289+ // make sure that the asset lock payload matches the OP_RETURN output
290+ val outputValue = finalSendRequest.tx.outputs.first().value
291+ val assetLockedValue = (finalSendRequest.tx as AssetLockTransaction ).assetLockPayload.creditOutputs.first().value
292+ if (assetLockedValue != outputValue) {
293+ val newRequest = SendRequest .assetLock(wallet.params, key, outputValue, true )
294+ newRequest.coinSelector = finalSendRequest.coinSelector
295+ newRequest.returnChange = finalSendRequest.returnChange
296+ newRequest.aesKey = finalSendRequest.aesKey
297+ finalSendRequest = newRequest
298+ } else {
299+ // this shouldn't happen
300+ error(" The asset lock value is the same as the output though emptying the wallet" )
301+ }
302+ }
303+
283304 sendCoinsTaskRunner.sendCoins(finalSendRequest, checkBalanceConditions = checkBalance)
284305 } catch (ex: Exception ) {
285306 _state .value = State .FAILED
@@ -534,7 +555,7 @@ class SendCoinsViewModel @Inject constructor(
534555 }
535556 }
536557
537- fun getNextTopupKey (): ECKey {
558+ fun getNextKey (): ECKey {
538559 val authGroup = wallet.getKeyChainExtension(
539560 AuthenticationGroupExtension .EXTENSION_ID
540561 ) as AuthenticationGroupExtension
0 commit comments