2.1.0 • Published 4 years ago

wicc-wallet-lib v2.1.0

Weekly downloads
7
License
MIT
Repository
github
Last release
4 years ago

wicc-wallet-utils-js v2.0

Official JavaScript library that provides WICC Offline Wallet capabilities

Installation

CDN

<script src="https://sw91.net/devops/wicc-wallet-utils/wicc-wallet-lib-2.0.0.js"></script>

NPM

npm install wicc-wallet-lib

Usage

const { Wallet, WalletManager, WaykiTransaction, BaasClient } = require("wicc-wallet-lib")

Key Classes

Wallet —— Constructor of a blockchain wallet

Demo

const privKeyWIF = "Y6J4aK6Wcs4A3Ex4HXdfjJ6ZsHpNZfjaS4B9w7xqEnmFEYMqQd13"
const wallet = new Wallet(privKeyWIF) 
//{privateKey: 'Y6J4aK6Wcs4A3Ex4HXdfjJ6ZsHpNZfjaS4B9w7xqEnmFEYMqQd13', address: 'wLKf2NqwtHk3BfzK5wMDfbKYN1SC3weyR4' }

Instance Methods

  • signMessage

    Message signature method

const msg = "wicc test"
const signTx = wallet.signMessage(msg) 
  • publicKeyAsHex

    Get wallet public key

const pubKey = wallet.publicKeyAsHex()

WalletManager —— Constructor of wallet management

Demo

const networkType = "testnet"
var walletManager = new WalletManager(networkType)

Instance Methods

  • randomMnemonicCodes

    Generate a mnemonic randomly

const lang = "ENGLISH" // or CHINESE
const mnemonics = walletManager.randomMnemonicCodes(lang) 
//mnemonics = "clinic dose kingdom fetch away industry squirrel cheese purchase mean slide mixed"
  • switchMnemonicCodes

    Switching from mnemonics in the current language to mnemonics in another language, both can generate the same wallet

var mnemonics1 = walletManager.randomMnemonicCodes("ENGLISH") 
var wallet1 = walletManager.importWalletFromMnemonic(mnemonics)

var mnemonics2 = walletManager.switchMnemonicCodes(mnemonics1, "CHINESE") 
var wallet2 = walletManager.importWalletFromMnemonic(mnemonics2)

wallet1.address === wallet2.address
  • createWallet

    Create a blockchain wallet from valid mnemonics, returns a instance object of Wallet

var wallet = walletManager.createWallet(mnemonics)
  • importWalletFromMnemonic

    Import an existing wallet from its mnemonics

var wallet = walletManager.importWalletFromMnemonic(mnemonics)
  • importWalletFromPrivateKey

    Import an existing wallet from its private key

var wallet = walletManager.importWalletFromPrivateKey(privateKeyWIF)

WaykiTransaction —— Constructor of transaction signature

var wallet = new Wallet(privKeyWIF) 
var txParams = {
  nTxType: 15, // transaction type
  nValidHeight: 34400,    // create height
  srcRegId: '0-1',    // sender's regId
  appId: "24555-1",  // app regId
  feeSymbol: "WICC",
  coinSymbol: "WUSD",
  fees: 1000000,         // fees pay for miner
  amount: 8,              // amount of WICC to be sent to the app account
  vContract: "f018"      // contract method, hex format string
};
var transaction = new WaykiTransaction(txParams, wallet)

Instance Methods

  • genRawTx
var rawTx = transaction.genRawTx()

List of all transactions

Transaction typenTxTypeDemo
ACCOUNT_REGISTER_TX2test-registeraccounttx.js
BCOIN_TRANSFER_TX3test-commontx.js
LCONTRACT_INVOKE_TX4test-callcontracttx.js
LCONTRACT_DEPLOY_TX5test-registercontracttx.js
DELEGATE_VOTE_TX6test-delegatetx.js
ASSET_ISSUE_TX9test-assetcreatetx.js
ASSET_UPDATE_TX10test-assetupdatetx.js
UCOIN_TRANSFER_TX11test-ucointransfertx.js
UCOIN_CONTRACT_INVOKE_TX15test-ucontractinvoketx.js
CDP_STAKE_TX21test-cdpstaketx.js
CDP_REDEEMP_TX22test-cdpredeemtx.js
CDP_LIQUIDATE_TX23test-cdpliquidatetx.js
DEX_LIMIT_BUY_ORDER_TX84test-dexbuylimitordertx.js
DEX_LIMIT_SELL_ORDER_TX85test-dexselllimitordertx.js
DEX_MARKET_BUY_ORDER_TX86test-dexbuymarketordertx.js
DEX_MARKET_SELL_ORDER_TX87test-dexsellmarketordertx.js
DEX_CANCEL_ORDER_TX88test-dexcancelordertx.js

BaasClient —— Contains some of Baas (Blockchain as a Service) API; HTTP requerts

Demo

var baasClient = new BaasClient(BaasUrl)
BaasNetworkBaasUrlRemarks
TestNetworkhttps://baas-test.wiccdev.org/v2/apidocumentation
ProdNetworkhttps://baas.wiccdev.org/v2/apidocumentation

Instance Methods

  • getAccountInfo

    Get information of a specific wallet address, returns a Promise

const address = "whiRBzMprDzY5wq3oPsHvAnyDV8ggYNaZE"
const response = baasClient.getAccountInfo(address)
response.then(data => {
    console.log(data)
})
  • getBlockCount

    Get the current block height, returns a Promise

const response = baasClient.getBlockCount()
response.then(data => {
    console.log(data)
})
  • sendRawTx

    Broadcast transaction signature data to blockchain, returns a Promise

const rawTx = "0301818c760200011476c6077b5679c8ef47f4243ca25537d5c3f7fad883e1ac009f80e7eeef000d74657374207472616e73666572473045022100e2853b6dfb8a892659de4a43181f8a02e983cca319def2457ea7b0a14c8966ea0220733ce86e172bff8aba750d104b6f39b737c897df7c33413142bd13b2415ce2f2"
const response = baasClient.sendRawTx(rawTx)
response.then(data => {
    console.log(data)
})
  • decodeRawTx

    Get the original transaction detail based on the signature data, returns a Promise

const rawTx = "0301818c760200011476c6077b5679c8ef47f4243ca25537d5c3f7fad883e1ac009f80e7eeef000d74657374207472616e73666572473045022100e2853b6dfb8a892659de4a43181f8a02e983cca319def2457ea7b0a14c8966ea0220733ce86e172bff8aba750d104b6f39b737c897df7c33413142bd13b2415ce2f2"
const response = baasClient.decodeRawTx(rawTx)
response.then(data => {
    console.log(data)
})

Development & Tests

$ git clone https://github.com/WaykiChain/wicc-wallet-utils-js.git
$ cd wicc-wallet-utils-js
$ npm install

Build (you will get a UMD module):

  • Execution Command
$ npm run build 
  • Output
dist/wicc-wallet-lib-2.0.js

Examples

Run all the Demos:

$ npm run test

Reference Projects

bitpay/bitcore-lib

Contact us

To get community assistance and ask for help with implementation questions

License

Code released under the MIT license.

© 2017-present WaykiChain, Inc.