mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 07:25:10 +00:00
GetHistoryForKey added to the chaincode-typescript
This commit is contained in:
parent
179bc96846
commit
cd9fa373ad
1 changed files with 29 additions and 0 deletions
|
|
@ -170,4 +170,33 @@ 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<string> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue