Skip to content

AergoLite﹢Node.js

Bernardo Ramos edited this page Nov 19, 2020 · 8 revisions

On Node.js we use the better-sqlite3-aergolite wrapper

It is based on better-sqlite3

Installation

npm install better-sqlite3-aergolite

Usage

Here is a sample script:

const options = { verbose: console.log };
const uri = 'file:test.db?blockchain=on&discovery=local:4329&password=test&admin=Am...';
const db = require('better-sqlite3-aergolite')(uri, options);

res = db.prepare('PRAGMA blockchain_status').get();
console.log(res);

const timer = setInterval(function(){
  res = db.prepare('PRAGMA db_is_ready').get();
  if (res.db_is_ready) {
    clearInterval(timer);
    onDbReady();
  }
}, 1000);

function onDbReady() {

  console.log('OK. this node is part of the blockchain network');

  setTimeout(function(){
    console.log('closing...');
    db.close();
  }, 2000);

}

Signing a transaction from the blockchain admin:

db.function('sign_transaction', function(data){
  console.log("txn to be signed:", data)
  var signature = sign(data, privkey)
  return pubkey.toString('hex') + ':' + signature.toString('hex')
});

Receiving a notification of a processed transaction:

db.function('transaction_notification', function(nonce, status){
  console.log("transaction " + nonce.toString() + ": " + status)
  return null
});

Receiving notification of local db update:

db.function('update_notification', function(unused){
  console.log("update received")
  return null
});

Troubleshooting

If there is no pre-compiled binary for your platform it is possible to compile from the sources:

  1. Install AergoLite on your computer

  2. Build the amalgamation

make amalgamation
  1. Install the wrapper using the amalgamation
npm install better-sqlite3-aergolite --build-from-source --sqlite3=/path/to/aergolite/amalgamation/
Clone this wiki locally