Previous: Steem-lib Guide [Part1]
This section is about transaction construction, signing and broadcast.
Set a privatekey(WIF) as signingkey for an account.
var MY_ACCOUNT = "my-account-name";
var MY_KEY = "wif*****";
remote.set_key(MY_ACCOUNT, MY_KEY)
this will cache the key into the client for later use of signing. The first argument (MY_ACCOUNT) is a label for the privatekey, could be arbitrary string but is convenient/intuitive by using just account name.
remote.set_auth('my-account-name', 'my-dummy-password');
this method will generate all keys from username + password, and cache them into the client.
// create a tx with MY_ACCOUNT as signer
var tx = remote.transaction(MY_ACCOUNT);
tx.add_operation('transfer', {
from: MY_ACCOUNT,
to: "the-recipient-account",
amount: "1.000 STEEM",
memo: "payment for xxx"
});
// to manually set expiration, e.g. 30s.
tx.expiration = Math.ceil(Date.now()/1000 + 30);
For the full list of supported operations, have a look into operation.js
optional steps:
There are two methods to accomplish this, both doing the similar jobs:
tx.broadcast() will callback immediately when the txn got into a head-block, or if there's an error.
// show immediate result whether the tx is included in a head block
tx.broadcast(function(err, res){
// immedate result from broadcast_api.
console.log(err, res)
})
tx.submit() will not callback until the txn was seen in an irreversible-block.
tx.submit(function(err, res){
if (err) return console.log('Error:', err);
if (res) console.log('Success & Final');
});
// broadcast result is still availabe by listening to 'submitted' event.
tx.once('submitted', function(err, res){
// head-block result
console.log(err || res)
})
Check the examples folder of the repository.
Next: Steem-lib Guide [Part3]