[ANN] SteemJS (steemjs-lib)

in #steem8 years ago

Need more Javascript

Since we still don't have an open-source Javascript library for constructing and signing transactions, I've spent some time over the last week to build one, starting from graphenejs-lib. The result is now out on NPM and Github and can be used to quickly and easily create bots using Node.js, or to start building custom trading solutions or posting platforms. The library can be installed with npm or cloned from github:

npm install steemjs-lib

https://github.com/svk31/steemjs-lib

Crypto tests screenshot

I will be releasing some specific use cases in the near future (who wants a "vote-as" bot?), and I might also use this to integrate actual trading in my Live market page. For now however the easiest way to figure out how to use it is to look at the tests, which can be found here.

Usage

Three sub-libraries are included: ECC, Chain and Serializer. Generally only the ECC and Chain libraries need to be used directly.

Chain

The Chain library contains utility functions related to the chain state, as well as a transaction builder and a login class.

Transaction builder

The transaction builder can be used to construct any transaction, sign it, and broadcast it. To broadcast a transaction you need to be connected to a steemd node with the network_broadcast_api enabled.

For an example of how to create transaction, see below:

let login = new Login();
let tr = new TransactionBuilder();
tr.add_type_operation("vote", {
    voter: "myaccount,
    author: "seshadga",
    permlink: "bitcoin-price-sustainability-looks-on-track",
    weight: 100
});

tr.process_transaction(login, null, false);

The third argument is process_transaction is broadcast. Setting it to false will simply construct the transaction and serialize it, without broadcasting it.

Login

The Chain library includes the Login class that can be used to "log in" using an account name and a corresponding password or private key. Logging in here simply means verifying that the private key or password provided can be used to generate the private key for that account. The verification checks the public keys of the given account.

The password used on https://steemit.com is compatible with this library. To run the Login tests, copy config.example.js and create a config.js. In this file you must provide two accounts, one with a password and one with a private key. The corresponding public keys can be found on https://steemd.com.

The Login class uses the following format to generate private keys from account names and passwords:

keySeed = accountName + role + password

Where role can be one of active, owner, posting, memo.

Using this seed, private keys are generated for either the default roles active, owner, posting, memo, or as specified. A minimum password length of 12 characters is enforced, but an even longer password is recommended. Three methods are provided:

generateKeys(account, password, [roles])
fromPrivKey(accountName, privateKey, [roles])
checkKeys({accountName, password, privateKey, auths})
signTransaction(tr)
getRoles()
setRoles([roles])

The auths object should contain the auth arrays from the account object. An example is this:

{
    active: [
        ["GPH5Abm5dCdy3hJ1C5ckXkqUH2Me7dXqi9Y7yjn9ACaiSJ9h8r8mL", 1]
    ]
}

If checkKeys is successful, you can use signTransaction to sign a TransactionBuilder transaction using the private keys for that account.

Sort:  

This is great!

I'm researching the different ways of programmatically interacting with the steem blockchain and will add this to my list of things to study. I'm wondering how this compares with an existing steemjs at https://github.com/pharesim/steemjs

I do prefer js code to python since I have done a lot of work with it. Do you think one language is better than the other, or just a matter of preference?

Thanks for your work on this, and for sharing it!

Thanks.

I have a websocket rpc library available also that can be compared to pharesim's steemjs. This new library steemjs-lib provides completely different functionality as it lets you construct and sign transactions without needing the cli_wallet. You need a running steemd to broadcast transactions, but that could be a public one like xeroc's wss://this.piston.rocks.

Thanks for your excellent answer! I'm not there yet on a complete understanding, but I am getting there!

Grate!
Wonderful Javascript world!

some js love is good for us, thanks @svk !

perfectly and check that you have gathered here, too)

Awesome! I'm gonna give this a go later tonight with some silly experiments.

JavaScript never fails to make a quick and interesting entrance to any scene.