I'm trying to connect and execute statements to Amazon QLDB using Node.js. In order to achieve this, I need to complete the final step which is calculating the commit digest. I have no idea how to do this.
I've thoroughly researched the QLDB Sessions javascript API here: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/QLDBSession.html but there was nothing out there about calculating the commit digest. I happened to find another question on stack overflow here: How to get/compute CommitDigest when committing a transaction in AWS QLDB?. I tried understanding the comments and messing around with the ion-hash-js library to see if I can create a commit digest, but each time I ran the code I kept getting errors. The OP did not provide any feedback on what worked/didn't work, so I'm stuck here.
const AWS = require('aws-sdk');
const qldb = new AWS.QLDB({apiVersion: '2019-01-02', region: 'us-east-1'});
var qldbSession = new AWS.QLDBSession({apiVersion: '2019-07-11', region: 'us-east-1'});
const ionHashJS = require("ion-hash-js/dist/commonjs/es5/src/IonHash");
const ionJs = require('ion-js')
async function execute(){
let sessionToken;
let transactionId;
let digest;
// ** Start Session **
await qldbSession.sendCommand({
StartSession: {
LedgerName: 'Vehicle-Registration'
}
}).promise().then(data => {
sessionToken = data.StartSession.SessionToken
})
// ** Start Transaction **
await qldbSession.sendCommand({
StartTransaction: {},
SessionToken: sessionToken
}).promise().then(data => {
transactionId = data.StartTransaction.TransactionId
})
// ** Insert Document **
await qldbSession.sendCommand({
ExecuteStatement: {
TransactionId: transactionId,
Statement: `CREATE TABLE Vehicle`
},
SessionToken: sessionToken
}).promise().then(data => {
console.log(data)
})
// ** Get Ledger Digest **
await qldb.getDigest({
Name: 'Vehicle-Registration'
}).promise().then(data => {
digest = data.Digest
}).catch(err => console.log(err))
// ** Commit Transaction **
await qldbSession.sendCommand({
CommitTransaction: {
TransactionId: transactionId,
CommitDigest: digest // <-- How to compute?
},
SessionToken: sessionToken}).promise().then(data => {console.log(data)}).catch(err => console.log(err))
}
execute();
I keep receiving a "Digests don't match" error