Skip to content

Instantly share code, notes, and snippets.

@airvin
Created September 22, 2020 14:18
Show Gist options
  • Save airvin/f9ea089ed5e9658f962ac8e77aedff6d to your computer and use it in GitHub Desktop.
Save airvin/f9ea089ed5e9658f962ac8e77aedff6d to your computer and use it in GitHub Desktop.
Hyperledger Fabric chaincode function that returns the set of results from the GetHistoryForKey stub function.
async GetHistoryForKey(ctx, key) {
console.log(`Get history for key ${key}`);
let iterator = await ctx.stub.getHistoryForKey(key);
let allResults = [];
let res = await iterator.next();
while (!res.done) {
if (res.value && res.value.value.toString()) {
let jsonRes = {};
console.log(res.value.value.toString("utf8"));
jsonRes.txId = res.value.tx_id;
jsonRes.timestamp = res.value.timestamp;
jsonRes.isDelete = res.value.isDelete;
try {
jsonRes.value = JSON.parse(res.value.value.toString("utf8"));
} catch (err) {
console.log(err);
jsonRes.value = res.value.value.toString("utf8");
}
allResults.push(jsonRes);
}
res = await iterator.next();
}
iterator.close();
return JSON.stringify(allResults);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment