2022.1.3 • Published 2 years ago

@crichain/kit.js v2022.1.3

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

@crichain/kit.js


Install

$ npm install @crichain/kit.js

Super simple to use

Setting up the network,@crichain/kit.js support network has testnet and prodnet

const brew=require('@crichain/kit.js');
var rp = require('request-promise')
//set prodnet network
brew.config.server_base='http://rc.domain.com/fbs';
brew.config.net_type='prodnet'
brew.config.rpc_provider = rp;

Create keystore.json、keypair

const brew=require('@crichain/kit.js');
const fs=require('fs');
var kp = brew.KeyPair.genRandomKey();
/**
* create keystore
* @param {*} kp
* @param {*} password
*/
var json = brew.keystore.exportJSON(kp,"000000");
let fd = fs.openSync('keystore.json', 'w+');
fs.writeSync(fd,JSON.stringify(json));
console.log('keypair',kp);

Get balance and nonce

const brew=require('@crichain/kit.js');
/**
* get balance and nonce
* @param {*} address
* @result {
        "retCode": 1,
        "address": "0x46f1f188bca9c555464ab41daecffaa0405f177c",
        "nonce": 0,//nonce
        "balance": "0x21e19e0c9bab2400000",//16进制余额
        "status": 0
    } 
*/
brew.rpc.getBalance('8f3aa4f0f35ff81ba487f91f6b980c0ba2562245').then(function(result){
    console.log(result.account.balance)
    console.log(result.account.nonce)
})

Get block info by hash

const brew=require('@crichain/kit.js');
/**
* get block info by hash
* @param {*} blockhash
*/
brew.rpc.getBlockByHash('03a15d84e6e29d2affab1ddc680f0aefc20586bd73ea3d81dcf6505924cfb86c').then(function(result){
    console.log(result)
})

Get block info by height

const brew=require('@crichain/kit.js');
/**
* get block info by height
* @param {*} height
*/
brew.rpc.getBlockByNumber('1518059').then(function(result){
    console.log(result)
})

Get the current height of the BREW blockchain

const brew=require('@crichain/kit.js');
brew.rpc.getBlockByMax().then(function(result){
    console.log(result)
})

Get transaction information through transaction hash

const brew=require('@crichain/kit.js');
/**
* get transaction information
* @param {*} transactionhash
*/
brew.rpc.getTransaction('f9bea09140e8e2eb2956976c3373418e2a935d821732d86bce33117d17314088').then(function(result){
    console.log(result)
})

Send transfer,support balance transfer,token transfer, crypto token transfer 。

const brew=require('@crichain/kit.js');
/**
* get keystore by prikey
* @param {*} prikey
*/
var kp = brew.KeyPair.genFromPrikey(
  '89611e9ed751b2bb0f2a84d1b364bd6ef97a512a7ad0b1b50241168ff3add985')
  
/**
* transfer
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
* @param {*} exdata "hexstring"
* @param {*} args [{"address":"066c03fcc3048863f72b051530e5a212fb9233f6","amount":""}]
* @result {*}
 *  {
 *      "retCode": 1, //1=成功 0=失败
 *      "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
 *  }
*/
kp.nonce=10;
//remove prefix
kp.hexAddress=brew.rpc.removePrefix(kp.hexAddress);
var from={keypair:kp};
var args=[{"address":brew.rpc.removePrefix("066c03fcc3048863f72b051530e5a212fb9233f6"),"amount":1}]
var exdata=""
brew.rpc.transfer(from,extdata,args).then(function(result){
    console.log(result)
}).catch(function(error){
    console.log(error);
})

sign transfer

/**
 * sign transfer normal
 * @param {*} from {"keypair":{"address":"","privateKey":"",nonce:10}}
 * @param {*} exdata
 * @param {*} args [{"address":"066c03fcc3048863f72b051530e5a212fb9233f6","amount":""}]
 */
//only get sign
var sign = brew.rpc.signTransfer(from,extdata,args);

Create a token transaction

/**
* create token
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"", "nonce": 0}}
* @param {*} exdata "hexstring"
* @param {*} args {"tos":["",""], "values":["",""],"name":"","symbol":"","decimals":18}
* @result {*}
 *  {
 *      "retCode": 1, //1=成功 0=失败
 *      "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
 *  }
*/
brew.rpc.createToken(from,exdata args).then(function(result){
    console.log(result)
}).catch(function(error){
    console.log(error);
})

sign create token

/**
* create token
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"", "nonce": 0}}
* @param {*} exdata "hexstring"
* @param {*} args {"tos":["",""], "values":["",""],"name":"","symbol":"","decimals":18} 
* @result {"tx":tx}
*/
//only get sign
var sign = brew.rpc.signCreateToken(from,extdata,args);

Transfer token transaction

/**
 * transfer token 
 * @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
 * @param {*} token
 * @param {*} args {"tos":["",""], "values":["",""]} 
 * @result {*}
 *  {
 *      "retCode": 1, //1=成功 0=失败
 *      "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
 *  }
 */
brew.rpc.transferToken(from,token,args).then(function(result){
    console.log(result)
}).catch(function(error){
    console.log(error);
})

sign transfer token

/**
 * transfer token 
 * @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
 * @param {*} token
 * @param {*} args {"tos":["",""], "values":["",""]} 
 * @result {"tx":tx}
 */
var sign = brew.rpc.signTransferToken(from,extdata,args);

Create contract

/**
* create contract
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
* @param {*} exdata "hexstring"
* @param {*} args {"data":"hexstring"}
* @result {*} 
 *  {
 *      "retCode": 1, //1=成功 0=失败
 *      "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
 *      "contractHash":""//合约地址 ,其他交易无此参数
 *  }
*/
brew.rpc.createContract(from,exdata,args).then(function(result){
    console.log(result)
}).catch(function(error){
    console.log(error);
})

sign Create contract

/**
* create contract
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
* @param {*} exdata "hexstring"
* @param {*} args {"data":"hexstring"}
* @result {"tx":tx}
*/
var sign = brew.rpc.signCreateContract(from,extdata,args);

Call contract

/**
 * call contract
 * @param {*} from 
 * @param {*} exdata
 * @param {*} args {"contract":"", "data":"hexstring", "amount":""}
 * @result {*}
 *  {
 *      "retCode": 1, //1=成功 0=失败
 *      "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
 *  }
*/

brew.rpc.callContract(from,exdata,args).then(function(result){
    console.log(result)
}).catch(function(error){
    console.log(error);
})

sign Call contract

/**
 * call contract
 * @param {*} from 
 * @param {*} exdata
 * @param {*} args {"contract":"", "data":"hexstring", "amount":""}
 * @result {"tx":tx}
*/
var sign = brew.rpc.signCallContract(from,extdata,args);

License

MIT