You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Hyperledger Composer的logic.js中访问资产交易历史?

Fetching Asset Transaction History in Hyperledger Composer's logic.js

Hey there! I’ve worked with Hyperledger Composer quite a bit, so let’s walk through exactly how to fetch an asset’s transaction history in your logic.js to verify that approval from your authority.

Key Background

Hyperledger Composer doesn’t have a direct high-level API for transaction history, but we can leverage the native Fabric chaincode API (via tx.stub) to call getHistoryForKey—this lets us pull all historical records tied to an asset.

Step-by-Step Implementation

Here’s a complete example transaction function that checks if an asset has been approved by your authority:

/**
 * Verify if an asset has received approval from the authority
 * @param {org.yournamespace.CheckAssetApproval} tx - Transaction to trigger approval check
 * @transaction
 */
async function checkAssetApproval(tx) {
    // 1. Get access to the native Fabric chaincode API
    const nativeStub = tx.stub;
    
    // 2. Get the asset's unique identifier (Composer's internal key)
    const assetUniqueKey = tx.targetAsset.getIdentifier();
    
    // 3. Fetch the asset's transaction history
    const historyIterator = await nativeStub.getHistoryForKey(assetUniqueKey);
    
    let isApproved = false;
    const approvalRecords = [];
    
    // 4. Iterate through the history to find approval transactions
    while (true) {
        const historyRecord = await historyIterator.next();
        
        // Skip empty records
        if (!historyRecord.value || !historyRecord.value.value.toString()) {
            if (historyRecord.done) break;
            continue;
        }
        
        // Parse the transaction data from the record
        const txData = JSON.parse(historyRecord.value.value.toString('utf8'));
        
        // 5. Check if this is your authority's approval transaction
        // Replace with your actual transaction class and authority validation logic
        if (txData.$class === 'org.yournamespace.ApproveAsset' && txData.approvingAuthority.role === 'AUTHORITY') {
            isApproved = true;
            approvalRecords.push({
                transactionId: historyRecord.value.txId,
                approvalTime: historyRecord.value.timestamp,
                details: txData
            });
        }
        
        // Exit loop when we've processed all records
        if (historyRecord.done) {
            await historyIterator.close();
            break;
        }
    }
    
    // 6. Set results on the transaction (or emit an event)
    tx.approvalStatus = isApproved;
    tx.approvalHistory = approvalRecords;
    
    // Update the transaction registry to store results
    const txRegistry = await getTransactionRegistry('org.yournamespace.CheckAssetApproval');
    await txRegistry.update(tx);
    
    // Optional: Emit an event to notify external systems
    const approvalEvent = getFactory().newEvent('org.yournamespace', 'AssetApprovalChecked');
    approvalEvent.asset = tx.targetAsset;
    approvalEvent.isApproved = isApproved;
    emit(approvalEvent);
}

Critical Notes to Keep in Mind

  • Enable History Database: Make sure your Fabric network has the historyDatabase enabled in the peer configuration—without this, getHistoryForKey will return empty results.
  • ACL Permissions: Update your Composer ACL file to allow the participant executing this transaction to access asset history. For example:
    rule AllowApprovalCheck {
        description: "Allow authority checkers to access asset history"
        participant: "org.yournamespace.ApprovalChecker"
        operation: READ
        resource: "org.yournamespace.*"
    }
    
  • Transaction Class Matching: Replace org.yournamespace.ApproveAsset with the actual fully qualified name of your approval transaction, and adjust the authority validation logic to match your participant structure.
  • Iterator Cleanup: Always close the history iterator once you’re done to avoid resource leaks.

This approach will let you reliably check an asset’s transaction history directly within your smart contract logic.

内容的提问来源于stack exchange,提问作者Rohit Khatri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:54:36