cosmos-crypto v0.4.2
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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago