Edit This Page

API

Interact with the chainscript.io API using Javascript.

Install

To install and save in your current project, use:

$ npm install --save chainscript

Basic usage

Creating a new script

var Chainscript = require('chainscript');

// You pass the initial script
new Chainscript({body: {content: {name: 'My Document'}}})
  // Add a snapshot command
  .snapshot()
  // Add a notarize command
  .notarize()
  // Add a send mail command
  .email('test@email.address')
  // Run the script (returns a promise)
  .run()
  .then(function(cs) {
    console.log(cs.toJSON());
  })
  .catch(function(err) {
    console.error(err.message);
  });

Working with a previously snapshotted script

var Chainscript = require('chainscript');

Chainscript.load('chainscript:document:3940c155-d17d-421a-b34e-8bf5a458299e')
  .then(function(cs) {
    console.log(cs.toJSON());
    // You can add commands to the loaded script and run the script
    return cs
      .email('test@email.address')
      .run();
  }).then(function(cs) {
    // New script executed with added commands
    console.log(cs.toJSON());
  })
  .catch(function(err) {
    console.error(err.message);
  });

Reference

Chainscript

new Chainscript(script, immutable = false)

Creates a new Chainscript from a JSON object.

If immutable is true, THE INSTANCE IS IMMUTABLE. It is never modified after initialization. Adding commands to a script returns a new instance.

Chainscript.load(uuid, immutable = false)

Loads an existing script. Returns a promise that resolves with an instance of Chainscript.

Chainscript#get(path)

Returns the value at specified path, or undefined if the path doesn't exist.

Ex:

var value = new Chainscript({body: {content: {name: 'My Document'}}})
  .get('body.content.name'));

console.log(value); // My Document

Chainscript#set(path, value)

Sets the value of a key at the specified path. Returns a new instance of Chainscript if immutable, otherwise returns the instance.

Chainscript#snapshot()

Adds a snapshot command to a script. Returns a new instance of Chainscript if immutable, otherwise returns the instance.

Chainscript#update(updates)

Adds an update command to a script. Returns a new instance of Chainscript if immutable, otherwise returns the instance.

Chainscript#notarize()

Adds a notarize command to a script. Returns a new instance of Chainscript if immutable, otherwise returns the instance.

Chainscript#email(to, [subject])

Adds a send_email command to a script. Returns a new instance of Chainscript if immutable, otherwise returns the instance.

Chainscript#change(fn)

Adds an update command to a script that applies granular updates to the content. Returns a new instance of Chainscript if immutable, otherwise returns the instance.

Ex:

new Chainscript({body: {content: {name: 'My Document', val: true}}})
  .change(function(content) {
    delete content.val;
    content.name += ' V2';
    content.meta = {
      author: 'Stephan Florquin',
      time: Date.now()
    };
  })
  .run()
  .then(function(cs) {
    console.log(cs.get('body.content'));
  })
  .catch(function(err) {
    console.error(err.message);
  });

Chainscript#delta(content)

Adds an update command to a script that applies the necessary changes to update the current content to the given content. Returns a new instance of Chainscript if immutable, otherwise returns the instance.

Ex:

var content = {
  name: 'My Document'
};

var cs = new Chainscript({body: {content: content}});

content.name = 'My Document V2';
content.meta = {
  author: 'Stephan Florquin',
  time: Date.now()
};

cs.delta(content).run().then(function() {
  console.log(cs.get('body.content'));
});

Chainscript#sign(wif)

Adds a sign_content command to the script. The private key must be in WIF format. Returns a new instance of Chainscript if immutable, otherwise returns the instance.

Chainscript#toJSON()

Returns the script as a JSON object.

Chainscript#run()

Runs the Chainscript. Returns a promise that resolves with a new instance of Chainscript if immutable, otherwise with the instance.

Chainscript#clone()

Clones Chainscript. Returns a new instance of Chainscript.

Mutable extensions

This only applies when immutable is false (the default value).

You may change the script directly via Chainscript#script. If you change the body content directly, an update command will be issued if needed before other commands when you call run.

var cs = new Chainscript({body: {content: {name: 'My Document'}}});

cs.script.body.content.name += ' V2';
cs.script.body.content.meta = {
  author: 'Stephan Florquin',
  time: Date.now()
};

script
  .run()
  .then(function() {
    console.log(script.toJSON());
  });