cosmos-crypto
crypto for cosmos in react-native
Last updated 3 months ago by meyun .
ISC · Original npm · Tarball · package.json
$ cnpm install cosmos-crypto 
SYNC missed versions from official npm registry.

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)

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

import {Crypto,Builder} from 'cosmos-crypto';
generate account
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

let account = Crypto.recover('your seed','your language');

or via privateKey

let account = Crypto.import('your privateKey');
translate contract
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

/**
 *  @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

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

Current Tags

  • 0.4.2                                ...           latest (3 months ago)

42 Versions

  • 0.4.2                                ...           3 months ago
  • 0.4.1                                ...           3 months ago
  • 0.4.0                                ...           8 months ago
  • 0.3.9                                ...           8 months ago
  • 0.3.8                                ...           8 months ago
  • 0.3.7                                ...           8 months ago
  • 0.3.6                                ...           8 months ago
  • 0.3.5                                ...           8 months ago
  • 0.3.4                                ...           9 months ago
  • 0.3.3                                ...           9 months ago
  • 0.3.2                                ...           9 months ago
  • 0.3.1                                ...           9 months ago
  • 0.3.0                                ...           a year ago
  • 0.2.9                                ...           a year ago
  • 0.2.8                                ...           a year ago
  • 0.2.7                                ...           a year ago
  • 0.2.6                                ...           a year ago
  • 0.2.5                                ...           a year ago
  • 0.2.4                                ...           a year ago
  • 0.2.3                                ...           a year ago
  • 0.2.2                                ...           a year ago
  • 0.2.1                                ...           a year ago
  • 0.2.0                                ...           a year ago
  • 0.1.9                                ...           a year ago
  • 0.1.8                                ...           a year ago
  • 0.1.7                                ...           a year ago
  • 0.1.6                                ...           a year ago
  • 0.1.5                                ...           a year ago
  • 0.1.4                                ...           a year ago
  • 0.1.3                                ...           a year ago
  • 0.1.2                                ...           a year ago
  • 0.1.1                                ...           a year ago
  • 0.1.0                                ...           a year ago
  • 0.0.9                                ...           a year ago
  • 0.0.8                                ...           a year ago
  • 0.0.7                                ...           a year ago
  • 0.0.6                                ...           a year ago
  • 0.0.5                                ...           a year ago
  • 0.0.4                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           a year ago
  • 0.0.1                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (12)
Dev Dependencies (14)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |