Skip to content

CosmosJS - Cosmos JavaScript Library

License

Notifications You must be signed in to change notification settings

channprj/cosmosjs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cosmostation logo

CosmosJS - Cosmos JavaScript Library

⭐ Developed / Developing by Cosmostation

A JavasSript Open Source Library for Cosmos Network

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, such as IRISnet

MIT NPM

WARNING: CosmosJS is under ACTIVE DEVELOPMENT and should be treated as alpha version. We will remove this warning when we have a release that is stable, secure, and ready to be used in production

Installation

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

NPM

npm install @cosmostation/cosmosjs

Yarn

yarn add @cosmostation/cosmosjs

Browser Distribution

CosmosJS supports browserify.

Import

NodeJS

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

Browser

<script src='js/cosmosjs-bundle.js'></script>

Usage

Generate Cosmos address from mnemonic

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

const chainId = "chain-id";
const cosmos = cosmosjs.network(chainId)

const mnemonic = "..."
const address = cosmos.getAddress(mnemonic);
const ecpairPriv = cosmos.getECPairPriv(mnemonic);

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
cosmos.getAccounts(address).then(data => {
	let stdSignMsg = cosmos.NewStdMsg({
		type: "cosmos-sdk/MsgSend",
		from_address: address,
		to_address: "cosmos18vhdczjut44gpsy804crfhnd5nq003nz0nf20v",
		amountDenom: "uatom",
		amount: 100000,
		feeDenom: "uatom",
		fee: 5000,
		gas: 200000,
		memo: "",
		account_number: data.value.account_number,
		sequence: data.value.sequence
	});

	...
})

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

  • API Call Limit: 10 per second
const signedTx = cosmos.sign(stdSignMsg, ecpairPriv);
cosmos.broadcast(signedTx).then(response => console.log(response));

Supporting Message Types (Updating...)

  • MsgSend
let stdSignMsg = cosmos.NewStdMsg({
	type: "cosmos-sdk/MsgSend",
	from_address: address,
	to_address: "cosmos18vhdczjut44gpsy804crfhnd5nq003nz0nf20v",
	amountDenom: "uatom",
	amount: 1000000,
	feeDenom: "uatom",
	fee: 5000,
	gas: 200000,
	memo: "",
	account_number: data.value.account_number,
	sequence: data.value.sequence
});
  • MsgDelegate
stdSignMsg = cosmos.NewStdMsg({
	type: "cosmos-sdk/MsgDelegate",
	delegator_address: address,
	validator_address: "cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn",
	amountDenom: "uatom",
	amount: 1000000,
	feeDenom: "uatom",
	fee: 5000,
	gas: 200000,
	memo: "",
	account_number: data.value.account_number,
	sequence: data.value.sequence
});
  • MsgUndelegate
stdSignMsg = cosmos.NewStdMsg({
	type: "cosmos-sdk/MsgUndelegate",
	delegator_address: address,
	validator_address: "cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn",
	amountDenom: "uatom",
	amount: 1000000,
	feeDenom: "uatom",
	fee: 5000,
	gas: 200000,
	memo: "",
	account_number: data.value.account_number,
	sequence: data.value.sequence
});
  • MsgWithdrawDelegationReward
stdSignMsg = cosmos.NewStdMsg({
	type: "cosmos-sdk/MsgWithdrawDelegationReward",
	delegator_address: address,
	validator_address: "cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn",
	feeDenom: "uatom",
	fee: 5000,
	gas: 200000,
	memo: "",
	account_number: data.value.account_number,
	sequence: data.value.sequence
});
  • MsgSubmitProposal
stdSignMsg = cosmos.NewStdMsg({
	type: "cosmos-sdk/MsgSubmitProposal",
	title: "Activate the Community Pool",
	description: "Enable governance to spend funds from the community pool. Full proposal: https://ipfs.io/ipfs/QmNsVCsyRmEiep8rTQLxVNdMHm2uiZkmaSHCR6S72Y1sL1",
	initialDepositDenom: "uatom",
	initialDepositAmount: 1000000,
	proposal_type: "Text",
	proposer: address,
	feeDenom: "uatom",
	fee: 5000,
	gas: 200000,
	memo: "",
	account_number: data.value.account_number,
	sequence: data.value.sequence
});
  • MsgDeposit
stdSignMsg = cosmos.NewStdMsg({
	type: "cosmos-sdk/MsgDeposit",
	depositor: address,
	proposal_id: 1,
	amountDenom: "uatom",
	amount: 1000000,
	feeDenom: "uatom",
	fee: 5000,
	gas: 200000,
	memo: "",
	account_number: data.value.account_number,
	sequence: data.value.sequence
});
  • MsgVote
stdSignMsg = cosmos.NewStdMsg({
	type: "cosmos-sdk/MsgVote",
	voter: address,
	proposal_id: 1,
	option: "Yes",	// Yes, No, NowithVeto, Abstain
	feeDenom: "uatom",
	fee: 5000,
	gas: 200000,
	memo: "",
	account_number: data.value.account_number,
	sequence: data.value.sequence
});
  • MsgBeginRedelegate
stdSignMsg = cosmos.NewStdMsg({
	type: "cosmos-sdk/MsgBeginRedelegate",
	delegator_address: address,
	validator_src_address: "cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn",
	validator_dst_address: "cosmosvaloper1ec3p6a75mqwkv33zt543n6cnxqwun37rr5xlqv",
	amountDenom: "uatom",
	amount: 1000000,
	feeDenom: "uatom",
	fee: 5000,
	gas: 200000,
	memo: "",
	account_number: data.value.account_number,
	sequence: data.value.sequence
});

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 are welcome
  • Any suggestions or improvements are welcome

Cosmostation's Services and Community

About

CosmosJS - Cosmos JavaScript Library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%