npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

decentjs-lib

v0.0.1

Published

Pure JavaScript Decent library for node.js and browsers.

Downloads

1

Readme

decentJS (decentjs-lib)

Pure JavaScript decent crypto library for node.js and browsers. Can be used to construct, sign and broadcast transactions in JavaScript.

npm version npm downloads

Setup

This library can be obtained through npm:

npm install decentjs-lib

DISCLAIMER: This is a work in progress and most likely there will be bugs. Please file issues if you encounter any problems.

The source code uses ES6 syntax but the npm library is transpiled to regular ES5 and can be used without Babel.

Tests

There's a quite extensive suite of tests that can be run using npm run test. These tests cover many different use cases and can be used as a reference point.

Usage

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

API setup

When constructing a decent transaction and also when verifying the password/private key of an account, some data from the blockchain is required. Because of this, decentjs-lib includes a dependency on steem-rpc, which is a websocket API library for connecting to API servers. Before attempting to broadcast a transaction you will need to initialise this library. By default it will connect to a public API server provided by DecentGO, but you can change this in the options if you prefer to use your own server.

To initialise the API library, use the following code:

var options = {};
var {Client} = require('steem-rpc');
var Api = Client.get(options, true);
Api.initPromise.then(response => {
    console.log("Api ready:", response);
})

In a browser the syntax is slightly different:

var options = {};
var Client = window.decentJS.decentRPC.Client;
var Api = Client.get(options, true);
Api.initPromise.then(response => {
    console.log("Api ready:", response);
})

More details on the possible options can be found in the README of steem-rpc.

Once the init promise has been resolved, the API connection is ready and you can start using the transaction builder.

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 decentd node with the network_broadcast_api enabled.

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

// First generate the private key using the Login class
var { TransactionBuilder, Login } = require("decentjs-lib");
var login = new Login();
login.setRoles(["posting"]);
var loginSuccess = login.checkKeys({
    accountName: "myacccount",
    password: "mypassword",
    auths: {
        posting: [["STMpostingAuthKey", 1]]
    }}
);

if (!loginSuccess) {
    throw new Error("The password or account name was incorrect");
}    
// Then create the transaction and sign it without broadcasting
var 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 of process_transaction is broadcast. Setting it to false will simply construct the transaction and serialize it, without broadcasting it. If you want it to broadcast immediately, set it to true.

Operation types

For a list of possible operation types with their required and optional inputs, see this file: operations.js.

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 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.

ECC

The ECC library contains all the crypto functions for private and public keys as well as transaction creation/signing.

Private keys

As a quick example, here's how to generate a new private key from a seed (a brainkey for example):

var {PrivateKey, key} = require("decentjs-lib");

let seed = "THIS IS A TERRIBLE BRAINKEY SEED WORD SEQUENCE";
let pkey = PrivateKey.fromSeed( key.normalize_brainKey(seed) );

console.log("\nPrivate key:", pkey.toWif());
console.log("Public key :", pkey.toPublicKey().toString(), "\n");