如何在Hyperledger Composer的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
historyDatabaseenabled in the peer configuration—without this,getHistoryForKeywill 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.ApproveAssetwith 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

