Skip to content

⭐ CosmosJS - Cosmos JavaScript Library

License

Notifications You must be signed in to change notification settings

bluzelle/cosmosjs

 
 

Repository files navigation

NPM Downloads

Cosmostation logo

CosmosJS - Cosmos JavaScript Library

⭐ Developed / Developing by Cosmostation

A JavasSript Open Source Library for Cosmos Network, IRISnet, Kava, Band Protocol, Starname, Secret Network, Akash Network, and CertiK.

This library supports cosmos address generation and verification. It enables you to create an offline signature functions of different types of transaction messages. It will eventually support all the other blockchains that are based on Tendermint in the future.

⚠️ If you are using under @cosmostation/[email protected]: Be very careful! @cosmostation/[email protected]+ will supports protobuf signing for cosmos-sdk 0.40.0+. You can download it from protobuf-test branch.

License Latest Stable Version NPM Downloads

Installation

In order to fully use this library, you need to run a local or remote full node and set up its rest server, which acts as an intermediary between the front-end and the full-node

NPM (Amino)

npm install @cosmostation/cosmosjs

Yarn (Amino)

yarn add @cosmostation/cosmosjs

Warning

  • If you use a version under 0.9.x, it will be deprecated. You can download protobuf version from the branch of protobuf-test.

Import

NodeJS

const cosmosjs = require("@cosmostation/cosmosjs");

ES6 module

import cosmosjs from "@cosmostation/cosmosjs";

CDN

<script src="https://cdn.jsdelivr.net/npm/@cosmostation/[email protected]/dist/cosmos.min.js"></script>

Usage

  • Cosmos: Generate Cosmos address from mnemonic
const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "cosmoshub-4";
const cosmos = cosmosjs.network(lcdUrl, chainId);

const mnemonic = "..."
cosmos.setPath("m/44'/118'/0'/0/0");
const address = cosmos.getAddress(mnemonic);
const ecpairPriv = cosmos.getECPairPriv(mnemonic);
  • Iris
const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "irishub-1";
const iris = cosmosjs.network(lcdUrl, chainId);
iris.setBech32MainPrefix("iaa");
  • Kava
const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "kava-4";
const kava = cosmosjs.network(lcdUrl, chainId);
kava.setBech32MainPrefix("kava");
  • Band
const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "band-guanyu-mainnet";
const band = cosmosjs.network(lcdUrl, chainId);
band.setBech32MainPrefix("band");
  • Starname
const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "iov-mainnet-2";
const iov = cosmosjs.network(lcdUrl, chainId);
iov.setBech32MainPrefix("star");
  • Secret Network
const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "secret-2";
const scrt = cosmosjs.network(lcdUrl, chainId);
scrt.setBech32MainPrefix("secret");
  • Akash Network
const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "akashnet-1";
const akash = cosmosjs.network(lcdUrl, chainId);
akash.setBech32MainPrefix("akash");
  • Certik
const cosmosjs = require("@cosmostation/cosmosjs");

const chainId = "shentu-1";
const certik = cosmosjs.network(lcdUrl, chainId);
certik.setBech32MainPrefix("certik");

Generate ECPairPriv value that is needed for signing signatures

const ecpairPriv = cosmos.getECPairPriv(mnemonic);

Transfer ATOM to designated address.

  • Make sure to input proper type, account number, and sequence of the cosmos account to generate StdSignMsg. You can get those account information on blockchain
  • Above 0.5.0 version, CosmosJS follows the exact same json format as Cosmos SDK defines.
cosmos.getAccounts(address).then(data => {
	let stdSignMsg = cosmos.newStdMsg({
		msgs: [
			{
				type: "cosmos-sdk/MsgSend",
				value: {
					amount: [
						{
							amount: String(100000),
							denom: "uatom"
						}
					],
					from_address: address,
					to_address: "cosmos18vhdczjut44gpsy804crfhnd5nq003nz0nf20v"
				}
			}
		],
		chain_id: chainId,
		fee: { amount: [ { amount: String(5000), denom: "uatom" } ], gas: String(200000) },
		memo: "",
		account_number: String(data.account.account_number),
		sequence: String(data.account.sequence)
	});

	...
})

Sign transaction by using stdSignMsg and broadcast by using /txs REST API

const signedTx = cosmos.sign(stdSignMsg, ecpairPriv);
cosmos.broadcast(signedTx).then(response => console.log(response));

Cosmostation offers LCD url(https://lcd-cosmos-free.cosmostation.io).

Supporting Message Types (Updating...)

Documentation

This library is simple and easy to use. We don't have any formal documentation yet other than examples. Ask for help if our examples aren't enough to guide you

Contribution

  • Contributions, suggestions, improvements, and feature requests are always welcome

When opening a PR with a minor fix, make sure to add #trivial to the title/description of said PR.

Cosmostation's Services and Community

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Booyoun

💻 🐛 🚧

JayB

💻 📖 🚧

billy rennekamp

💻 🐛

Tony Phuong Nguyen

💻

Tobias Schwarz

💻

Scott Burch

🚧

billy rennekamp

🚧

Pierre Grimaud

🚧

Taariq Levack

🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

About

⭐ CosmosJS - Cosmos JavaScript Library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 90.2%
  • HTML 9.8%