crypto for cosmos in react-native
npm install cosmos-cryptoJavaScript 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
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
~~~
``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 contractjs`
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.$3
js`
/**
* @privatekey The msg you should encrypto/decrypto
* @pwd The encrypto password
* /
const encryptoPrivatekey = Crypto.encrypto(privatekey,pwd)
const decryptoPrivatekey = Crypto.decrypto(privatekey,pwd)
js
let stdTx = Builder.buildAndSignTx('your request', 'your privateKey');
let postTx = stdTx.GetData();
let hash = stdTx.Hash();
``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