Skip to content

Toolkit of wallet to manage multiple chains & support multiple keystores for each chain

License

Notifications You must be signed in to change notification settings

JCCDex/jcc_wallet

Repository files navigation

jcc_wallet

npm build Coverage Status npm downloads PRs Welcome

Preface

jcc_wallet is a wallet toolkit of JCCDex. At present, it can handle the creation and verification of jingtum, moac, ethereum, stm, call & bizain wallet, import and export wallet with keystore file, and set password for wallet.

jcc_wallet是井畅交换平台的钱包处理工具,目前可以处理jingtum, moac, ethereum, stm, call以及bizain钱包的创建和校验,以 keystore 形式导入和导出钱包,设置钱包交易密码。

jcc_wallet Support running in browsers

井畅应用交流群: 557524730

JCCDex Tech support QQ group ID: 557524730

Installtion

npm install jcc_wallet

CDN

jcc_wallet as a global variable.

<script src="https://unpkg.com/jcc_wallet/dist/jcc-wallet.min.js"></script>

Table of Contents

Structure of Jingchang Wallet

For more see IJingchangWalletModel.

API of JingchangWallet

Support multiple wallet keystore for each type.

Usage

const JingchangWallet = require("jcc_wallet").JingchangWallet;
// import { JingchangWallet } from 'jcc_wallet'

constructor

/**
 * Creates an instance of JingchangWallet.
 * @param {IJingchangWalletModel} wallet
 * @param {boolean} [multiple=false] if the value is true, support save multiple wallet keystore
 * for each type, otherwise only support one.
 * @param {boolean} [samePassword=true] if the value is true, use the default swt keystore's password
 * which be generated in the beginning as password for other type.
 * @memberof JingchangWallet
 */

generate

/**
 * create a jingchang wallet
 *
 * @static
 * @param {string} password password for keystore
 * @param {string} [secret] swtc chain's secret
 * @returns {Promise<IJingchangWalletModel>} resolve jingchang wallet if success.
 * @memberof JingchangWallet
 */

isValid

/**
 * check jingchang wallet is valid or not
 *
 * @static
 * @param {*} wallet
 * @returns {boolean} return true if valid.
 * @memberof JingchangWallet
 */

get

/**
 * get jingchang wallet from local storage
 *
 * @static
 * @returns {(IJingchangWalletModel | null)} return jingchang wallet or null.
 * @memberof JingchangWallet
 */

clear

/**
 * clear jingchang wallet from local storage.
 *
 * @static
 * @memberof JingchangWallet
 */

save

/**
 * save jingchang wallet to local storage.
 *
 * @static
 * @param {IJingchangWalletModel} wallet
 * @memberof JingchangWallet
 */

deriveKeyPair

/**
 * derive key pair with secret
 *
 * @static
 * @param {string} secret
 * @param {string} [chain="swt"]
 * @returns {IKeyPair} for privateKey, it's length should be 64 when call `decryptWithPrivateKey`, but the origin derived
 * privateKey's length is 66 that contains prefix `00` for `secp256k1` or `ED` for `ed25519`, so removed it.
 * @memberof JingchangWallet
 */

encryptWithPublicKey

/**
 * encrypt data with public key
 *
 * @static
 * @param {string} message
 * @param {string} publicKey
 * @returns {Promise<IEncrypt>}
 * @memberof JingchangWallet
 */

decryptWithPrivateKey

/**
 * decrypt data with private key
 *
 * @static
 * @param {IEncrypt} message
 * @param {string} privateKey the privateKey's length should be 64
 * @returns {Promise<string>}
 * @memberof JingchangWallet
 */

getWallets

/**
 * get wallets from jingchang wallet.
 *
 * @static
 * @param {IJingchangWalletModel} jcWallet
 * @returns {Array<IKeystoreModel>} return wallets if valid, otherwise return empty array.
 * @memberof JingchangWallet
 */

setJingchangWallet

/**
 * set property of _jingchangWallet
 *
 * @param {IJingchangWalletModel} wallet
 * @memberof JingchangWallet
 */

getAddress

/**
 * get default wallet's keystore address for each type
 *
 * @param {string} [type="swt"]
 * @returns {Promise<string>} resolve address if success
 * @memberof JingchangWallet
 */

getWalletWithType

/**
 * get default wallet keystore with type
 *
 * @param {string} [type="swt"]
 * @returns {Promise<IKeystoreModel>} resolve default wallet keystore if success.
 * @memberof JingchangWallet
 */

getWalletWithAddress

/**
 * get wallet keystore with address
 *
 * @param {string} address
 * @returns {Promise<IKeystoreModel>} resolve wallet keystore if success.
 * @memberof JingchangWallet
 */

hasDefault

/**
 * check if has default wallet for each type
 *
 * @param {string} [type="swt"]
 * @returns {boolean} return true if has default.
 * @memberof JingchangWallet
 */

getSecretWithType

/**
 * get the default wallet keystore's secret with type.
 *
 * @param {string} password
 * @param {string} [type="swt"]
 * @returns {Promise<string>} resolve secret if success.
 * @memberof JingchangWallet
 */

getSecretWithAddress

/**
 * get the wallet keystore's secret with address.
 *
 * @param {string} password
 * @param {string} address
 * @returns {Promise<string>} resolve secret if success.
 * @memberof JingchangWallet
 */

changeWholePassword

/**
 * change the whole jingchang wallet password, if you set property of _samePassword is false,
 * will throw an error
 *
 * @param {string} oldPassword
 * @param {string} newPassword
 * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
 * @memberof JingchangWallet
 */

changePasswordWithAddress

/**
 * change the keystore password with address, if you set the property of _samePassword is true,
 * will throw an error
 *
 * @param {string} address
 * @param {string} oldPassword
 * @param {string} newPassword
 * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
 * @memberof JingchangWallet
 */

removeWalletWithType

/**
 * remove default wallet keystore of the given type
 * @param {string} [type="swt"]
 * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
 * @memberof JingchangWallet
 */

removeWalletWithAddress

/**
 * remove wallet keystore of the given address
 *
 * @param {string} address
 * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
 * @memberof JingchangWallet
 */

setDefaultWallet

/**
 * set defalut wallet keystore for each type
 *
 * @param {string} address
 * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
 * @memberof JingchangWallet
 */

importSecret

/**
 * import secret
 *
 * @param {string} secret
 * @param {string} password
 * @param {string} type
 * @param {(secret: string) => string} retriveSecret
 * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
 * @memberof JingchangWallet
 */

findWallet

/**
 * find wallet keystore according to filter function
 *
 * @protected
 * @param {(wallet: IKeystoreModel) => boolean} filter
 * @returns {IKeystoreModel} return wallet keystore if existent, otherwise throw `keystore is invalid`
 * if the jingchang wallet is invalid or throw `wallet is empty` if the wallet isn't existent
 * @memberof JingchangWallet
 */

getEncryptData

/**
 * encrypt data
 *
 * @protected
 * @param {string} password
 * @param {IKeypairsModel} keypairs
 * @returns {IKeystoreModel}
 * @memberof JingchangWallet
 */

saveWallet

/**
 * save wallet keystore to jingchang wallet
 *
 * @private
 * @param {string} password
 * @param {IKeypairsModel} keypairs
 * @returns {Promise<IJingchangWalletModel>} resolve new jingchang wallet if success
 * @memberof JingchangWallet
 */

HD Wallet

jcc wallet support BIP32/39/44 standard.

more detail see test/hd.spec.js test case.

// normally hd maniuplate
IHDWallet hd = HDWallet.generate({ language: "chinese_simplified" });

// derive bsc wallet
hd.deriveWallet({ chain: BIP44Chain.BSC, account: 0, index: 0 })
hd.getAddress();

// don't worry about address/secret format, hd object have chain type attribute, check it by native chain sdk
hd.validAddress("your address, like jxxxxx or 0x1234");
hd.validSecret("your secret");

// default encode utf-8
hd.hash("message text");

// sign by hd private key automatically
hd.sign("message text");

// verify message signature is sign address signed or not
hd.verify("message text", signature, signAddress);
// or if you don't passby address, you can passby keypair
hd.verify("message test", signature, null, keypair)

Deprecated API

Deprecated api will be removed after 2020.1.1, please update asap.

For more see deprecatedAPI.