|
| 1 | +/* |
| 2 | + * Copyright IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +package com.weaver.corda.app.interop.contracts |
| 8 | + |
| 9 | +import com.weaver.corda.app.interop.states.AssetPledgeState |
| 10 | +import com.weaver.corda.app.interop.states.AssetClaimStatusState |
| 11 | +import com.weaver.corda.app.interop.states.NetworkIdState |
| 12 | +import net.corda.core.contracts.CommandData |
| 13 | +import net.corda.core.contracts.Contract |
| 14 | +import net.corda.core.contracts.requireSingleCommand |
| 15 | +import net.corda.core.contracts.requireThat |
| 16 | +import net.corda.core.contracts.StaticPointer |
| 17 | +import net.corda.core.transactions.LedgerTransaction |
| 18 | +import java.time.Instant |
| 19 | +import java.util.* |
| 20 | + |
| 21 | +/** |
| 22 | + * AssetTransferContract defines the rules for managing a [AssetPledgeState]. |
| 23 | + * |
| 24 | + */ |
| 25 | +class AssetTransferContract : Contract { |
| 26 | + companion object { |
| 27 | + // Used to identify our contract when building a transaction. |
| 28 | + const val ID = "com.weaver.corda.app.interop.contracts.AssetTransferContract" |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * A transaction is valid if the verify() function of the contract for any of the transaction's |
| 33 | + * input and output states does not throw an exception. |
| 34 | + */ |
| 35 | + override fun verify(tx: LedgerTransaction) { |
| 36 | + val command = tx.commands.requireSingleCommand<Commands>() |
| 37 | + when (command.value) { |
| 38 | + is Commands.Pledge -> requireThat { |
| 39 | + "There should be one input state." using (tx.inputs.size == 1) |
| 40 | + "There should be one output state." using (tx.outputs.size == 1) |
| 41 | + "The output state should be of type AssetPledgeState." using (tx.outputs[0].data is AssetPledgeState) |
| 42 | + |
| 43 | + // Get the Asset Pledge State |
| 44 | + val pledgeState = tx.outputs[0].data as AssetPledgeState |
| 45 | + |
| 46 | + // Check if output belong to this contract |
| 47 | + "Output state should belong to this contract" using (tx.outputs[0].contract.equals(ID)) |
| 48 | + |
| 49 | + // Check if timeout is beyond current time |
| 50 | + val expiryTime = Instant.ofEpochSecond(pledgeState.expiryTimeSecs) |
| 51 | + "AssetPledgeState.expiryTimeSecs is after current time." using (expiryTime.isAfter(Instant.now())) |
| 52 | + |
| 53 | + // Check if the asset owner is the locker |
| 54 | + val inputState = tx.inputs[0].state.data |
| 55 | + "Locker must be the owner of pledged asset" using inputState.participants.containsAll(listOf(pledgeState.locker)) |
| 56 | + |
| 57 | + // Check if asset consumed (input state) is the same used in pledge |
| 58 | + val assetPointer = StaticPointer(tx.inputs[0].ref, tx.inputs[0].state.data.javaClass) |
| 59 | + "Asset consumed should be the one used in pledge." using (assetPointer.equals(pledgeState.assetStatePointer)) |
| 60 | + |
| 61 | + // Check if the locker is a signer |
| 62 | + val requiredSigners = pledgeState.participants.map { it.owningKey } |
| 63 | + "The required signers of the transaction must include the locker." using (command.signers.containsAll(requiredSigners)) |
| 64 | + |
| 65 | + val inReferences = tx.referenceInputRefsOfType<NetworkIdState>() |
| 66 | + "There should be a single reference input network id." using (inReferences.size == 1) |
| 67 | + |
| 68 | + val validNetworkIdState = inReferences.get(0).state.data |
| 69 | + "AssetPledgeState.localNetwokId must match with the networkId of current network." using (pledgeState.localNetworkId.equals(validNetworkIdState.networkId)) |
| 70 | + } |
| 71 | + is Commands.ClaimRemoteAsset -> requireThat { |
| 72 | + "There should be no input state." using (tx.inputs.size == 0) |
| 73 | + "There should be two output states." using (tx.outputs.size == 2) |
| 74 | + "One of the output states should be of type AssetClaimStatusState." using (tx.outputsOfType<AssetClaimStatusState>().size == 1) |
| 75 | + |
| 76 | + // Check if output state [AssetClaimStatusState] belongs to this contract |
| 77 | + "Output state should belong to this contract" using (tx.outputs[1].contract.equals(ID)) |
| 78 | + |
| 79 | + // Get the input asset pledge state |
| 80 | + val claimState = tx.outputs[1].data as AssetClaimStatusState |
| 81 | + |
| 82 | + val inReferences = tx.referenceInputRefsOfType<NetworkIdState>() |
| 83 | + "There should be a single reference input network id." using (inReferences.size == 1) |
| 84 | + val validNetworkIdState = inReferences.get(0).state.data |
| 85 | + |
| 86 | + "AssetClaimStatusState.localNetwokID must match with the networkId of current network." using (claimState.localNetworkID.equals(validNetworkIdState.networkId)) |
| 87 | + |
| 88 | + // Check if timeWindow <= expiryTime |
| 89 | + val untilTime = tx.timeWindow!!.untilTime!! |
| 90 | + val expiryTime = Instant.ofEpochSecond(claimState.expiryTimeSecs) |
| 91 | + "TimeWindow to claim pledged remote asset should be before expiry time." using |
| 92 | + (untilTime.isBefore(expiryTime) || untilTime.equals(expiryTime)) |
| 93 | + |
| 94 | + // Check if the owner of the asset in the importing network after asset claim is the recipient |
| 95 | + val outputAssetState = tx.outputs[0].data |
| 96 | + "Recipient must be the owner of claimed asset." using (outputAssetState.participants.containsAll(listOf(claimState.recipient))) |
| 97 | + |
| 98 | + // Check if the recipient is a signer |
| 99 | + val requiredSigners = claimState.participants.map { it.owningKey } |
| 100 | + "The required signers of the transaction must include the recipient." using (command.signers.containsAll(requiredSigners)) |
| 101 | + } |
| 102 | + is Commands.ReclaimPledgedAsset -> requireThat { |
| 103 | + "There should be one input state." using (tx.inputs.size == 1) |
| 104 | + "The input state should be of type AssetPledgeState." using (tx.inputs[0].state.data is AssetPledgeState) |
| 105 | + "There should be one output state." using (tx.outputs.size == 1) |
| 106 | + |
| 107 | + // Get the input asset pledge state |
| 108 | + val pledgeState = tx.inputs[0].state.data as AssetPledgeState |
| 109 | + |
| 110 | + // Check if timeWindow > expiryTime |
| 111 | + val fromTime = tx.timeWindow!!.fromTime!! |
| 112 | + "TimeWindow for reclaim pledged asset should be after expiry time." using (fromTime.isAfter(Instant.ofEpochSecond(pledgeState.expiryTimeSecs))) |
| 113 | + |
| 114 | + // Check if the asset owner is the locker |
| 115 | + val outputState = tx.outputs[0].data |
| 116 | + "Locker must be the owner of pledged asset" using outputState.participants.containsAll(listOf(pledgeState.locker)) |
| 117 | + |
| 118 | + // Verify if locker is the signer |
| 119 | + val requiredSigners = listOf(pledgeState.locker.owningKey) |
| 120 | + "The required signers of the transaction must include the locker." using (command.signers.containsAll(requiredSigners)) |
| 121 | + |
| 122 | + val inReferences = tx.referenceInputRefsOfType<NetworkIdState>() |
| 123 | + "There should be a single reference input network id." using (inReferences.size == 1) |
| 124 | + val validNetworkIdState = inReferences.get(0).state.data |
| 125 | + |
| 126 | + "LocalNetwokId must match with the networkId of current network." using (pledgeState.localNetworkId.equals(validNetworkIdState.networkId)) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Commands are used to indicate the intent of a transaction. |
| 133 | + * Commands for [AssetTransferContract] are: |
| 134 | + * - Pledge |
| 135 | + * - ReclaimPledgedAsset |
| 136 | + * - ClaimRemoteAsset |
| 137 | + */ |
| 138 | + interface Commands : CommandData { |
| 139 | + class Pledge : Commands |
| 140 | + class ReclaimPledgedAsset : Commands |
| 141 | + class ClaimRemoteAsset : Commands |
| 142 | + } |
| 143 | +} |
0 commit comments