# cosmos-crypto

> crypto for cosmos in react-native

Latest version **0.4.2** (published 2020-05-11) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.2 |
| Published | 2020-05-11 |
| First published | 2019-08-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Unpacked size | 916.4 KB |
| Known vulnerabilities | 0 (+12 in 2 direct dependencies) |
| Install scripts | no |
| Author | meyun |
| Maintainers | meyun |
| Keywords | crypto, cosmos, react-native |

## Links

- npm: https://www.npmjs.com/package/cosmos-crypto
- npm.io page: https://npm.io/package/cosmos-crypto

## Dependencies (12)

- [bip39](https://npm.io/package/bip39.md) 2.5.0
- [bech32](https://npm.io/package/bech32.md) ^1.1.3
- [buffer](https://npm.io/package/buffer.md) ^5.4.0
- [sha256](https://npm.io/package/sha256.md) ^0.2.0
- [base64-js](https://npm.io/package/base64-js.md) ^1.3.1
- [cosmos-bn](https://npm.io/package/cosmos-bn.md) 0.0.2
- [crypto-js](https://npm.io/package/crypto-js.md) ^3.1.9-1
- [ripemd160](https://npm.io/package/ripemd160.md) ^2.0.2
- [secp256k1](https://npm.io/package/secp256k1.md) ^3.7.1
- [protobufjs](https://npm.io/package/protobufjs.md) ^6.8.8
- [create-hmac](https://npm.io/package/create-hmac.md) ^1.1.7
- [react-native-crypto](https://npm.io/package/react-native-crypto.md) ^2.2.0

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

- 0.4.2 (latest) — 2020-05-11
- 0.4.1 — 2020-04-29
- 0.4.0 — 2019-12-19
- 0.3.9 — 2019-12-18
- 0.3.8 — 2019-12-16
- 0.3.7 — 2019-12-07
- 0.3.6 — 2019-12-05
- 0.3.5 — 2019-11-26
- 0.3.4 — 2019-11-22
- 0.3.3 — 2019-11-22
- 0.3.2 — 2019-11-22
- 0.3.1 — 2019-11-21
- 0.3.0 — 2019-09-20
- 0.2.9 — 2019-09-09
- 0.2.8 — 2019-09-09
- … 27 more at https://npm.io/package/cosmos-crypto/versions

## README

# cosmos-crypto

JavaScript crypto library supporting cosmos light client in react-native app(also can use in brower).
It mainly provides account generation, transaction construction and offline signing functions of cosmos. You can install this library in the following ways

## 1.install 

npm install -s cosmos-crypto

then config env(only in react-native)
~~~js
npm i --save react-native-crypto
//install peer deps 
npm i --save react-native-randombytes
react-native link react-native-randombytes
//install latest rn-nodeify 
npm i --save-dev tradle/rn-nodeify
//install node core shims and recursively hack package.json files 
//in ./node_modules to add/update the "browser"/"react-native" field with relevant mappings 
./node_modules/.bin/rn-nodeify --hack --install
//next 
rn-nodeify will create a shim.js in the project root directory
// index.ios.js or index.android.js
// make sure you use `import` and not require!  
import './shim.js'
import crypto from 'crypto'
// ...the rest of your code
~~~ 
## Useage

### import package

```js
import {Crypto,Builder} from 'cosmos-crypto';
```


##### generate account
```js
let account = Crypto.create('your language');
// account: {"address":"faa1e4y8urzgjd82447ydlw9tszsm2lxfwdr5hxj4a","phrase":"carbon when squeeze ginger rather science taxi disagree safe season mango teach trust open baby immune nephew youth nothing afraid sick prefer daughter throw","privateKey":"436EB1ACE1D9D8F4EA519D050FF16ADD4B9CAF3D6D0917411857318259022EFF","publicKey":"fap1addwnpepqw36efnhzgurxaq3mxsgf4fjm280dehh20w03u3726arm0deagne5u254g2"}
```
The create method has a parameter:language,Used to specify the generated mnemonic language.The default is 'english'.You have the following options to choose from:
- chinese_simplified
- japanese
- spanish
- english

We recommend choosing 'english'

##### recovery account
via mnemonic
```js
let account = Crypto.recover('your seed','your language');

```
or via privateKey

```js
let account = Crypto.import('your privateKey');
```
##### translate contract
```js
const contractHexString = Crypto.splice("contract",[param1,param2])
```
The difference between the above two is that the latter does not return the mnemonic of the account.
### Encrypto/Decrypto privatekey
```js
/**
 *  @privatekey The msg you should encrypto/decrypto
 *  @pwd The encrypto password 
 * /
const encryptoPrivatekey = Crypto.encrypto(privatekey,pwd)   
const decryptoPrivatekey = Crypto.decrypto(privatekey,pwd)   
```

### Transaction
Construct a transaction and sign
```js
let stdTx = Builder.buildAndSignTx('your request', 'your privateKey');
let postTx = stdTx.GetData();
let hash = stdTx.Hash();
```
*buildAndSignTx* has two parameters: 
- request : transaction content,specific instructions are as follows:
    - chain_id : blockchain's chain_id(example:fuxi/irishub/gaia-13001 etc.)
    - from : transaction originator address(example:faa1ljemm0yznz58qxxs8xyak7fashcfxf5lssn6jm)
    - account_number : you can get it from lcd's 'auth/accounts/'
    - sequence : you can get it from lcd's 'auth/accounts/'
    - fees : transaction fee
    - gas : gas limit
    - memo : transaction description
    - type : transaction type,support the following values
        - transfer
        - delegate
        - unbond
        - redelegate
        - withdrawDelegationRewardsAll
        - withdrawDelegationReward
        - sendToContractReal
        - msgTokenReq
        - paGame
        - endGame
        - sGameSession
        - sGame
        - queryGame
        - rGame
        - seed
        - cContractReal
    - msg : message content  
~~~js
    msg:{
          ToAddress:{
            value:'contract139rv6v6p7ewqv85wlqtfvk23x5aufxl8r0qaxc',  //type of contract address
            type:'contract'
          },
          FromAddress:{
            value:test.address,      //type of user address
            type:'address'
          },
          Amount:'1',
          Sequence:sequence,
          Data:{
            value:'ecc2a9a0',     //type of base64
            type:'base64'
          },
          GasLimit:'4294967295',
          GasPrice:'0',
          GameName:'twentyOne',
          GameType:'classical',
          ChipRange:'1',
          ContractKey:'ToAddress'   //指定合约读取的key    当部署或调用合约时需指定合约地址的key
    }
~~~
- privateKey : your privateKey

*GetData* will return the constructed and signed transaction,you can call lcd's 'tx/broadcast'(cosmos:/txs) to send the transaction.

*Hash* is used to calculate the hash of the transaction,prevent server response from being unavailable due to timeout. You can use hash to confirm if the transaction was successful.
[contact me](wks19921023@163.com 'contact me')

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