Hyperledger composer Access Control,允许在事务中创建

时间:2018-06-01 12:15:35

标签: hyperledger hyperledger-composer

以下是我要实施的内容:

我想允许用户(参与者的类型)创建资产,但仅在事务中执行,而在此事务之外,我想拒绝所有用户创建资产的权限。

我尝试使用函数:

在规则.acl文件中使用条件来解决它
rule UserCanCreateAssetOnlyInTransaction {
    description: "Deny all participants create access to all userWallets if not in transaction"
    participant(p): "com.example.User"
    operation: CREATE
    resource(r): "com.example.UserAsset"
    condition:(isInTransactionF())
    action: ALLOW 
}

然后在事务中我创建变量var isInTransaction = true;,并在logic.js文件中添加:

/**
@returns {boolean} boolean true/false
*/
function isInTransactionF(){
    if(isInTransaction){
      return true; 
    }else{
      return false;
    }   
}

它不起作用,当我调用创建访问应该工作的唯一事务时,它表示用户没有创建访问权来提交此事务。 我想我做错了什么,有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

在你的功能中实现你想要的 - 你会说:

/**
@returns {boolean} boolean true/false
*/
function isInTransactionF() {
    var isInTransaction = true ;  // Boolean
    if(isInTransaction) {
    // if( Boolean(isInTransaction)) { // alternative
      return true; 
    } else{
      return false;
    }   
}

您当前的ACL将有效。

我可以调用console.log来查看返回的结果

console.log("The return result is " + isInTransactionF() );`  // true

限制参与者仅通过某个事务类创建资产 - 该规则看起来像(即资产只能通过此类创建 - 隐含地,如果没有其他资产创建规则,它应该在其他地方被拒绝):

rule CreateAssetThruTxn {
    description: "sample""
    participant(p): "com.example.User"
    operation: CREATE
    resource(r): "com.example.UserAsset"
    transaction(tx): "com.example.AssetCreate"
    condition:(true)
    action: ALLOW 
}

如果您的ACL失败,那么您需要查看其他ACL规则可能允许通过其他方式创建此资产,但我提供的规则将是控制该规则的常用方法(根据您提供的信息)问题)