API

Settings

Numerous settings are available when initialising the core API via Lisk-JS. Please see the examples below.

The provision of settings is optional, therefore initialising without any arguments var LSK = lisk.api(); yields the defaults.

Example 1 Random node, testnet with port 7000

                                        
var options = {
    ssl: false, // Default false. Set to true to enable the https instead of http protocol.
    node: '', // Default randomNode. Insert a node without http or https protocol. Use ssl option in order to set http or https.
    randomPeer: true, // Default true. Lisk-js automatically connects to a random peer to get lisk blockchain information. Set to false to disable this behaviour.
    testnet: true, // Default false. Set to true to use the testnet. Set to false to use the mainnet.
    port: '7000', // Default 8000. Enter the port as the protocol http(s)://node:port - can be any string.
    bannedPeers: [], // Default empty. Array of peers that should not be connected to. Without http(s) or port.
};

var LSK = lisk.api(options);
                                        
                                    

Example 2 Restricted to localhost, port 8000, no SSL

                                        
var options = {
    ssl: false, // default false
    node: 'localhost', // write node without http:// or port
    randomPeer: false, // default true
    testnet: false, // default false
    port: '8000', // default 8000
    bannedPeers: [], // default empty

};

var LSK = lisk.api(options);
                                        
                                    

API Functions

getAccount

Example Get single account by address

                                        
lisk.api().getAccount(accountAddress, callback);
                                        
                                    

listActiveDelegates

Example Get the first 5 active delegates

                                        
lisk.api().listActiveDelegates(5, callback);
                                        
                                    

listStandbyDelegates

Example Get the first 6 standby delegates

                                        
lisk.api().listStandbyDelegates(6, callback);
                                        
                                    

searchDelegateByUsername

Example Search for delegates by username

                                        
lisk.api().searchDelegateByUsername('username', callback);
                                        
                                    

getBlock

Example Get a single block by id

                                        
lisk.api().getBlock(blockId, callback);
                                        
                                    

listBlocks

Example Get a list of blocks

                                        
lisk.api().listBlocks(amount, callback);
                                        
                                    

listForgedBlocks

Example List the forged blocks using a given public key

                                        
lisk.api().listForgedBlocks(publicKey, callback);
                                        
                                    

getTransaction

Example Get a single transaction by id

                                        
lisk.api().getTransaction(transactionId, callback);
                                        
                                    

listTransactions

Example List transactions for a given address

                                        
lisk.api().listTransactions(address, limit, offset, callback);
                                        
                                    

Address: The Lisk address for recipient and sender of the transactions.

Limit: The number of transactions to retrieve. Minimum: 1, Maximum: 1000.

Offset: The number of records to offset the query from, e.g. a given offset of 10 excludes the first 10 records from the yielded results.

listVotes

Example List votes placed by a given address

                                        
lisk.api().listVotes(address, callback);
                                        
                                    

listVoters

Example List votes received by a given address

                                        
lisk.api().listVoters(publicKey, callback);
                                        
                                    

listMultisignatureTransactions

Example Lists all pending multisignature transactions

                                        
lisk.api().listMultisignatureTransactions(callback);
                                        
                                    

getMultisignatureTransaction

Example Lists a multisignature transaction by a given transaction ID

                                        
lisk.api().getMultisignatureTransaction('transactionID', callback);
                                        
                                    

Network Functions

sendRequest

sendRequest gives full access to the Lisk core API. It accepts any valid endpoint and parameters.

Example GET - Retreive the current block height

                                        

var liskBlockheight = lisk.api().sendRequest('blocks/getHeight', function (data) {
    var str = JSON.stringify(data);
    document.getElementById('output').innerHTML = str;
});
                                        
                                    

Example POST - Open a new account

                                        
var liskNewAccount = lisk.api().sendRequest('accounts/open', { secret: 'my secret passphrase' }, function (data) {
    var str = JSON.stringify(data);
    document.getElementById('output').innerHTML = str;
});
                                        
                                    

The provided secret is intercepted and never transmitted over the network.

getNethash

Example Get a unique hash for the currently selected network

                                        
var LSK = lisk.api().getNethash();
                                        
                                    
Returning
                                        
{
    'Content-Type': 'application/json',
    'nethash': 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511',
    'broadhash': 'ed14889723f24ecc54871d058d98ce91ff2f973192075c0155ba2b7b70ad2511',
    'os': 'lisk-js-api',
    'version': '1.0.0',
    'minVersion': '>=0.5.0',
    'port': this.port
}
                                        
                                    

setNode

Example Set the currently selected node

                                        
var LSK = lisk.api().setNode('mynode.com');
                                        
                                    

Use LSK.setNode(); without any arguments to automatically choose a new random node.

listPeers

Example Lists the peers configured for each network.

                                        
var LSK = lisk.api().listPeers();
                                        
                                    

Yields an object containing arrays of network categorised peers:

{
    official: [...],
    ssl: [...],
    testnet: [...],
    localhost: [...]

}
                                

setSSL

Example Enable the SSL connecton protocol

                                        
var LSK = lisk.api().setSSL(true); // or false
                                        
                                    

setTestnet

Example Toggle between the testnet and mainnet

                                        
var LSK = lisk.api().setTestnet(true); // or false
                                        
                                    

Crypto

getKeys

Example Get a public and private keypair from a given secret

                                        
var keys = lisk.crypto.getKeys('secret');
                                        
                                    
Returning
                                        
{
    publicKey: '5d036a858ce89f844491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09',
    privateKey: '2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a2…44491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09'
}
                                        
                                    

getAddress

Example Get the LSK address from a given public key

                                        
var keys = lisk.crypto.getAddress('5d036a858ce89f844491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09');
                                        
                                    
Returning
                                        
18160565574430594874L
                                        
                                    

getBytes

Example Get the number of bytes for a transaction object

                                        
var transactionBytes = lisk.crypto.getBytes(transactionObject);
                                        
                                    

getHash

Example Get the hash of a transaction object

                                        
var transactionHash = lisk.crypto.getHash(transactionObject);
                                        
                                    

getId

Example Get the unique identifier of a transaction object

                                        
var transactionId = lisk.crypto.getId(transactionObject);
                                        
                                    

getFee

Example Get the fees for a transaction object

                                        
var transactionFee = lisk.crypto.getFee(transactionObject);
                                        
                                    

sign

Example Sign a transaction object using a given keypair

                                        
var transactionSignature = lisk.crypto.sign(transactionObject, keypair);
                                        
                                    

secondSign

Example Add a second signature to a transaction object using a given keypair

                                        
var transactionSignature = lisk.crypto.secondSign(transactionObject, keypair);
                                        
                                    

multiSign

Example Add a multisignature to a transaction object using a given keypair

                                        
var transactionSignature = lisk.crypto.multiSign(transactionObject, keypair);
                                        
                                    

getKeys

Example Get a public and private keypair from a given secret

                                        
var keypair = lisk.crypto.getKeys(secret);
                                        
                                    

verify

Example Verify the signature of transaction object

                                        
var verification = lisk.crypto.verify(transactionObject);
                                        
                                    

verifySecondSignature

Example Verify the second signature of a transaction object

                                        
var verification = lisk.crypto.verifySecondSignature(transactionObject);
                                        
                                    

fixedPoint

Example Convert a given floating point LSK amount to an integer representation

                                        
var bigAmount = lisk.crypto.fixedPoint(amount);
                                        
                                    

bufferToHex

Example Convert a buffer to a hex string

                                        
var hexString = lisk.crypto.bufferToHex(buffer);
                                        
                                    

hexToBuffer

Example Convert a hex string to buffer

                                        
var buffer = lisk.crypto.hexToBuffer(hexString);
                                        
                                    

useFirstEightBufferEntriesReversed

Example Cut the first 8 bytes of a given buffer and reverses them

Used to calculate unique identifiers.

                                        
var rawId = lisk.crypto.useFirstEightBufferEntriesReversed(buffer);
                                        
                                    

signMessageWithSecret

Example Signs a message (not encrypted) using a plain message and a secret

                                        
var messageSignature = lisk.crypto.signMessageWithSecret(message, secret);
                                        
                                    

printSignedMessage

Example Formats the given plain message, signed message and public key

                                        
var printSignedMessage = lisk.crypto.printSignedMessage(message, signedMessage, publicKey);
                                        
                                    
                                        
-----BEGIN LISK SIGNED MESSAGE-----
<message>
-----BEGIN SIGNATURE-----
<publicKey>
<signature>
-----END LISK SIGNED MESSAGE-----
                                        
                                    

In order maintain formatting it is advised to wrap the resulting output within a pre tag.

signAndPrintMessage

Example Signs a message (not encrypted) using a plain message and a secret, then prints it

Combines the signMessageWithSecret and printSignedMessage functions.

                                        
var printSignedMessage = lisk.crypto.printSignedMessage(message, secret);
                                        
                                    

verifyMessageWithPublicKey

Example Verify a signed message using a given public key

Returns null if invalid, or the original message if valid.

                                        
var verifySignedMessage = lisk.crypto.verifyMessageWithPublicKey(signedMessage, publicKey);
                                        
                                    

encryptMessageWithSecret

Example Encrypts a message with the given secret and recipient PublicKey

Where message is a UTF-8 message, secret a passphrase and recipientPublicKey the publicKey in hex.

                                        
var encrypted = lisk.crypto.encryptMessageWithSecret(message, secret, recipientPublicKey);
                                        
                                    
Returning
                                        
{
    encryptedMessage: encryptedMessage(hex),
    nonce: nonce(hex)
}
                                        
                                    

Returns an object with the encrypted message and a nonce. Both are necessary in order to decrypt the message.

decryptMessageWithSecret

Example Decrypts a message with secret, senderPublicKey and the nonce of the message

Where encrypted message is the hex format of the encrypted message, nonce also hex. The secret as the passphrase of the recipient and the sender publicKey in hex.

                                        
var encrypted = lisk.crypto.decryptMessageWithSecret(encryptedMessage, nonce, secret, senderPublicKey);
                                        
                                    
Returning
                                        
Original Message
                                        
                                    

If successfull, returns the plain text of the senders message.

getPrivateAndPublicKeyFromSecret

Example Get a hex representation of a keypair from a given secret

                                        
var keypair = lisk.crypto.getPrivateAndPublicKeyFromSecret(secret);
                                        
                                    
Returning
                                        
{
    publicKey: publicKey(hex),
    privateKey: privateKey(hex)
}
                                        
                                    

getRawPrivateAndPublicKeyFromSecret

Example Get a raw byte representation of a keypair from a given secret

                                        
var keypairBytes = lisk.crypto.getRawPrivateAndPublicKeyFromSecret(secret);
                                        
                                    
Returning
                                        
{
    publicKey: publicKey(bytes),
    privateKey: privateKey(bytes)
}
                                        
                                    

getAddressFromPublicKey

Example Get the address from a given public key

                                        
var address = lisk.crypto.getAddressFromPublicKey(publicKeyHex);
                                        
                                    

getSha256Hash

Example Get the sha256 hash from a given string

                                        
var sha256Hash = lisk.crypto.getSha256Hash(string);
                                        
                                    

Transactions

Create Transaction

Example Create a signed "balance transfer" transaction object

                                        
var amount      = 1000 * Math.pow(10, 8); // 100000000000
var transaction = lisk.transaction.createTransaction('1859190791819301L', amount, 'passphrase', 'secondPassphrase');
                                        
                                    
Returning
                                        
{
    type: 0, // Transaction type. 0 = Normal transaction.
    amount: 100000000000, // The amount to send expressed as an integer value.
    asset: {}, // Transaction asset, dependent on tx type.
    fee: 100000000, // 0.1 LSK expressed as an integer value.
    id: '500224999259823996', // Transaction ID.
    recipientId: '1859190791819301L', // Recipient ID.
    senderPublicKey: '56e106a1d4a53dbe22cac52fefd8fc4123cfb4ee482f8f25a4fc72eb459b38a5', // Sender's public key.
    signSignature: '03fdd33bed30270b97e77ada44764cc8628f6ad3bbd84718571695262a5a18baa37bd76a62dd25bc21beacd61eaf2c63af0cf34edb0d191d225f4974cd3aa509', // Sender's second passphrase signature.
    signature: '9419ca3cf11ed2e3fa4c63bc9a4dc18b5001648e74522bc0f22bda46a188e462da4785e5c71a43cfc0486af08d447b9340ba8b93258c4c7f50798060fff2d709', // Transaction signature.
    timestamp: 27953413 // Based on UTC time of genesis since epoch.
}
                                        
                                    

Create Vote

Example Create a signed "delegate vote" transaction object

                                        
var transaction = lisk.vote.createVote('secret', ['+58199578191950019299181920120128129'], 'secondSecret');
                                        
                                    

Create Dapp

Example Create a signed "dapp registration" transaction object

                                        
var options = {
    category: 0,
    name: 'Lisk Guestbook',
    description: 'The official Lisk guestbook',
    tags: 'guestbook message sidechain',
    type: 0,
    link: 'https://github.com/MaxKK/guestbookDapp/archive/master.zip',
    icon: 'https://raw.githubusercontent.com/MaxKK/guestbookDapp/master/icon.png'
};

var transaction = lisk.dapp.createDapp('secret', 'secondSecret', options);
                                        
                                    

Create Delegate

Example Create a signed "delegate registration" transaction object

                                        
var transaction = lisk.delegate.createDelegate('secret', 'username', 'secondSecret');
                                        
                                    

Create Second Signature

Example Create a signed "second signature registration" transaction object

                                        
var transaction = lisk.signature.createTransaction('secret', 'secondSecret');
                                        
                                    

Create Multisignature Account

Example Create a signed "multisignature account" transaction object

                                        
var secret = 'myprivatesecret';
var secondSecret = '';
var keysgroup = ['+publicKey1', '+publicKey2']; // Add publicKeys for multisignature.
var lifetime = 20; // Time in hours until transactions will be invalid.
var min = 2; // Minimum signatures so that a transaction is valid.
var transaction = lisk.multisignature.createMultisignature(secret, secondSecret, keysgroup, lifetime, min);
                                        
                                    

Sign Multisignature Transaction

Example Sign a multisignature transaction object

                                        
var signature = lisk.multisignature.signTransaction(transaction, secret);