# cosmos-lib

> Lib to generate private/public keys, addresses for Cosmos

Latest version **1.1.0** (published 2019-12-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install cosmos-lib
pnpm add cosmos-lib
yarn add cosmos-lib
bun add cosmos-lib
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2019-12-18 |
| First published | 2019-06-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 12.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Maintainers | boris_wings |
| Keywords | crypto, cosmos-sdk, cosmos, bech32, address, bitcoin |

## Links

- npm: https://www.npmjs.com/package/cosmos-lib
- Repository: https://github.com/WingsDao/cosmos-lib
- Issues: https://github.com/WingsDao/cosmos-lib/issues
- npm.io page: https://npm.io/package/cosmos-lib

## Dependencies (5)

- [bip32](https://npm.io/package/bip32.md) ^2.0.3
- [bip39](https://npm.io/package/bip39.md) ^3.0.2
- [bech32](https://npm.io/package/bech32.md) ^1.1.3
- [ripemd160](https://npm.io/package/ripemd160.md) ^2.0.2
- [secp256k1](https://npm.io/package/secp256k1.md) ^3.7.1

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2019-12-18
- 1.0.4 — 2019-06-28

## README

# Cosmos Lib

JS implementation of [Cosmos](https://github.com/cosmos/cosmos-sdk) cryptography and addresses logic.

Features:
* Generate private/public key from mnemonic and path.
* Sign/verify bytes/JSON with private/public key.s
* Convert Cosmos bech32 address to bytes32 (solidity capability).

# Installation

Install dependency:

    yarn add cosmos-lib

And use in code:

    const cosmos = require('cosmos-lib');

# Usage

Generate private/public key from mnemonic:

```js
const MNEMONIC = 'cluster unveil differ bright define prosper hunt warrior fetch rough host fringe worry mention gospel enlist elder laugh segment funny avoid regular market fortune';

const keys = cosmos.crypto.getKeysFromMnemonic(MNEMONIC);
console.log(keys.publicKey, keys.privateKey);
```

Sign bytes/JSON:

```js
const bytes = crypto.randomBytes(12);
const json  = {
    data: crypto.randomBytes(128)
};

let signature = cosmos.crypto.sign(bytes, keys.privateKey);
let verify    = cosmos.crypto.verify(bytes, signature, keys.publicKey);

if (!verify) {
    throw 'Cant verify signature';
}

signature = cosmos.crypto.signJson(json, keys.privateKey);
verify = cosmos.crypto.verifyJson(json, signature, keys.publicKey);

if (!verify) {
    throw 'Cant verify signature';
}
```

Get address from public key:

```js
const prefix = 'mywallet';
const address = cosmos.address.getAddress(keys.publicKey, prefix); // prefix by default is 'cosmos'.
console.log(address);
```

Get bytes32 from address:
```js
bytes32 = cosmos.address.getBytes32(address);
console.log(bytes32.toString('hex'));
```

More examples could be found in [tests](/test).

# Docs

To generate docs:

    yarn global add jsdoc
    yarn run docs

Then server them from `out` directory.

# Tests

To run tests:

    git clone git@github.com:WingsDao/cosmos-lib.git
    cd cosmos-lib/
    yarn install
    yarn test

# LICENSE

MIT.

---
_Source: https://npm.io/package/cosmos-lib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
