/sciencepark

Primary LanguageJavaScriptMIT LicenseMIT

Smart City Hackathon

Datum helper with example

Please use the datumHelper.js to help you get sense of how to generate, recover, and transfer DAT to your accounts.

Please note the following:

  1. All balances are returned in Wei not DAT.
  2. You can't perform concurrent set/remove, this is related to Blockchain not Datum since you have to manage transaction nonce.

How to get help

  1. ask all your questions @ our gitter_lobby
  2. check out our official example folder our SDK repo
  3. Check our getting Started

Create New Datum Object

To Create Datum Identity you need to perform two steps:

  1. Create new datum Object
  const tmpDatObj = new Datum();
  1. Create Identity
const id = await Datum.createIdentity("password", accounts);

password is used to encrypt your keystore, accounts is the number of accounts you need to generate. Usually the value is 0 unless you want to generate number of accounts under the same seed.

  1. initialize your datum object with Identity KeyStore
tmpDatObj.initialize({ identity: id.keystore });

Note that keystore must be stringified.

Full example

async function createDatumIdentity(password, accounts = 0) {
  const tmpDatObj = new Datum();
  const id = await Datum.createIdentity(password, accounts);
  //Save Identity is a function that save seed into a file to later recovery
  saveIdentity(id.seed);
  tmpDatObj.initialize({ identity: id.keystore });
  tmpDatObj.identity.storePassword(password);
  return tmpDatObj;
}

storePassword function allow you to perform operations without providing password every time

Recover Datum Object From SEED

To recover datum object from seed keyword

  1. Create empty Identity object.
const tmpId = new Datum().Identity();
  1. Recover Identity from seed keyword
await tmpId.recover(seed, password);

password is used to encrypt the keystore information in datum object

  1. Create empty Datum object
const tmpDatum = new Datum();
  1. initialize datum object with recovered Identity
tmpDatum.initialize({
  identity: JSON.stringify(tmpId.keystore),
});

Notice that keystore is stringified

Full example

async function recoverDatumObj(seed, password) {
  const tmpId = new Datum().Identity();
  await tmpId.recover(seed, password);
  const tmpDatum = new Datum();
  tmpDatum.initialize({
    identity: JSON.stringify(tmpId.keystore),
  });
  tmpDatum.identity.storePassword(password);
  return tmpDatum;
}