From a1b5f6830dd3ba38f7c9f139f065e8d65c94b88a Mon Sep 17 00:00:00 2001 From: Ajiths10 Date: Wed, 19 Jun 2024 15:21:29 +0530 Subject: [PATCH] GetHistoryForKey added to the chaincode-typescript Signed-off-by: Ajiths10 --- .../chaincode-typescript/src/assetTransfer.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts b/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts index d3ed927a..df7d0c5e 100644 --- a/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts +++ b/asset-transfer-basic/chaincode-typescript/src/assetTransfer.ts @@ -170,4 +170,34 @@ export class AssetTransferContract extends Contract { return JSON.stringify(allResults); } + // GetHistoryForKey returns all assets history in the world state. + @Transaction(false) + @Returns("string") + public async GetHistoryForKey(ctx: Context, id: string): Promise { + const results: string[] = []; + try { + const historyIterator = await ctx.stub.getHistoryForKey(id); + let result = await historyIterator.next(); + if (result.done) { + const errorMessage = `Asset ${id} does not exist`; + console.log(errorMessage); + throw new Error(errorMessage); + } + + while (!result.done) { + const iteratorValue = Buffer.from( + result.value.value.toString() + ).toString("utf8"); + results.push(iteratorValue); + console.log(iteratorValue); + result = await historyIterator.next(); + } + + await historyIterator.close(); + } catch (error) { + console.log(error); + } + return JSON.stringify(results); + } + }