Asset Creation through Transaction in Hyperledger

2020-07-25 23:25发布

问题:

While creating any asset or participant need to check some condition Like (IF..THEN..ELSE) on some field.

Is it Possible to create Asset or Participant through transaction?

回答1:

Yes it is possible.

I did the same thing in my network, creating assets with a transaction and applying whatever rules you need.

transactions are run from your logic.js file in lib.

assume you have an asset myAsset in org.myAssets namespace

asset myAsset identified by assetId
{
  o String assetId
  o String someData
  //other fields as required
}

you now want a transaction which will create an asset

your cto transaction looks like this:

namespace org.transactions
import org.myAssets.*

transaction MyAssetCreate
{
  o myAsset anAsset
}

you can't have a reference to the asset here since you won't have an asset yet.

in your lib/logic.js you have something like:

/**
 * creates an asset
 * @param {org.transactions.MyAssetCreate} myAssetCreate 
 * @transaction
 */
async function MyAssetCreate(myAssetCreate) {
    return getAssetRegistry('org.myAssets.myAsset')
    .then(function(result) {
        var factory = getFactory();
        var newAsset = factory.newResource(
        'org.myAssets', 
        'myAsset', 
        myAssetCreate.anAsset.assetId); 
        newAsset.someData = myAssetCreate.anAsset.someData
        //continue with property assignments and any logic you have
        //when you are done and everything looks good you can continue
        return result.add(newAsset);
     });

now you can invoke MyAssetCreate and you will get your asset in the right registry. Of course, if you do this then you need to make sure you don't allow assets to be created via the standard asset endpoint.

I myself plan to not expose any asset endpoints at all and only allow changes via transactions.

Check the code for typos etc, I took this from my running network, replacing my type names so it's possible I mistyped something.



标签: hyperledger