Locus Library
Provides a library containing functions necessary when checking the Locus Chain to the service. The following features are included:
- Generate or load keys.
- Convert data types.
- Verify transactions.
- Sign with private keys.
Installation
- Javascript
npm i @locuschain/lib
Usage
LocusLib.function(param1, param2, ... )
- function: Function Name
- param: Parameter
- Javascript
import { getLibraryVersions } from '@locuschain/lib/utils';
const result = getLibraryVersions()
console.log(result)
Functions
getLibraryVersions
GetLibraryVersion returns the version of this wasm library.
Input
getLibraryVersions( )
[None]
Output
| Key | Type | Description |
|---|---|---|
| LibraryVersion | int | Library Version |
| KeyStoreVersion | int | Keystore Version |
| KeyStoreVersionMin | int | Minimum Compatible Keystore Version |
| GoBuildVersion | string | Golang Version used to build Library |
createAccountAndKeystore
Creates two separate keystore files for master key and normal key.
Input
createAccountAndKeystore( params )
| Key | Type | Description |
|---|---|---|
| params.passwordMaster | string | Master Key Password |
| params.passwordNormal | string | Normal Key Password |
| params.algoMaster | string | Master Key Algorithm |
| params.algoNormal | string | Normal Key Algorithm |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Master and Normal Keystore String |
createNormalKey
Generates a new normal key for the given address.
Input
createNormalKey( params )
| Key | Type | Description |
|---|---|---|
| params.addrStr | string | Address |
| params.keyAlgo | string | Normal Key Algorithm |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Normal Key String |
createMasterKeystore
Creates a master keystore for the given address.
Input
createMasterKeystore( params )
| Key | Type | Description |
|---|---|---|
| params.addrStr | string | Address |
| params.password | string | Master Key Password |
| params.mskStr | string | Master Secret Key |
| params.mpkStr | string | Master Public Key |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Master Keystore String |
createNormalKeystore
Creates a normal keystore for the given address.
Input
createNormalKeystore( params )
| Key | Type | Description |
|---|---|---|
| params.addrStr | string | Address |
| params.password | string | Normal Key Password |
| params.nskStr | string | Normal Secret Key |
| params.npkStr | string | Normal Public Key |
| params.mpkStr | string | Master Public Key |
| params.keySign | string | Key Signature |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Normal Keystore String |
loadMasterKeystore
Returns the master key from the master keystore.
Input
loadMasterKeystore( params )
| Key | Type | Description |
|---|---|---|
| params.passStr | string | Master Key Password |
| params.ksJson | string | Master Keystore String |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Master Key String |
loadNormalKeystore
Returns the normal key from the normal keystore.
Input
loadNormalKeystore ( params )
| Key | Type | Description |
|---|---|---|
| params.passStr | string | Normal Key Password |
| params.ksJson | string | Normal Keystore String |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Normal Key String |
convertToData
Decodes various values to default encoding (base64).
Input
convertToData( value, valueType )
| Key | Type | Description |
|---|---|---|
| value | string | |
| valueType | int | valueType : 0(raw string), 1(hex), 2(crc16-base32), 3(base64) |
Output
| Key | Type | Description |
|---|---|---|
| - | string |
convertDataTo
Converts default encoded (base64) values to various types.
Input
convertDataTo( value, retType )
| Key | Type | Description |
|---|---|---|
| value | string | |
| retType | int | valueType : 0(raw string), 1(hex), 2(crc16-base32), 3(base64) |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertStringToData
Decodes string values to default encoding (base64).
Input
convertStringToData( raw )
| Key | Type | Description |
|---|---|---|
| raw | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertDataToString
Decodes default encoded (base64) values to string values.
Input
convertDataToString ( str )
| Key | Type | Description |
|---|---|---|
| str | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertAddressToHex
Decodes base32 encoded address to hex value.
Input
convertAddressToHex( addrEncoded )
| Key | Type | Description |
|---|---|---|
| addrEncoded | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertHexToAddress
Encodes hex value to base32 encoded address.
Input
convertHexToAddress( hexString )
| Key | Type | Description |
|---|---|---|
| hexString | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertAddressToData
Decodes base32 encoded address to default encoding.
Input
convertAddressToData( addrEncoded )
| Key | Type | Description |
|---|---|---|
| addrEncoded | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertDataToAddress
Encodes default encoded value to base32 encoded address.
Input
convertDataToAddress( str )
| Key | Type | Description |
|---|---|---|
| str | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertBase32ToHex
Decodes base32 encoded value to hex value.
Input
convertBase32ToHex( encoded )
| Key | Type | Description |
|---|---|---|
| encoded | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertHexToBase32
Encodes hex value to base32 encoded value.
Input
convertHexToBase32( hexString )
| Key | Type | Description |
|---|---|---|
| hexString | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertHexToData
Decodes hex encoded value to default encoding (base64).
Input
convertHexToData( encoded )
| Key | Type | Description |
|---|---|---|
| encoded | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertDataToHex
Encodes default encoded (base64) value to hex encoded value.
Input
convertDataToHex( str )
| Key | Type | Description |
|---|---|---|
| str | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertBase32ToData
Decodes base32 encoded value to default encoding (base64). convertBase32ToData( encoded )
Input
| Key | Type | Description |
|---|---|---|
| encoded | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertDataToBase32
Encodes default encoded (base64) value to base32 encoded value.
Input
convertDataToBase32( str )
| Key | Type | Description |
|---|---|---|
| str | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
signByMasterKey
Generates a signature using master secret key and message.
Input
signByMasterKey ( params )
| Key | Type | Description |
|---|---|---|
| params.msk | string | Master Secret Key |
| params.message | string | Message to sign |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
sign
Generates a signature using normal secret key and message.
Input
sign( params )
| Key | Type | Description |
|---|---|---|
| params.sk | string | Normal Secret Key |
| params.message | string | Message to sign |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
verify
Returns the result of verifying signature using message and normal public key.
Input
verify( params )
| Key | Type | Description |
|---|---|---|
| params.pk | string | Normal Public Key |
| params.message | string | Message to verify |
| params.sign | string | Signature to verify |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
verifyTx
Verifies all types of transactions generated by RPC.
Input
verifyTx( jsonTx )
| Key | Type | Description |
|---|---|---|
| jsonTx | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
compileCoreScript
CompileCoreScript compiles core script source code to byte code.
Input
compileCoreScript( params )
| Key | Type | Description |
|---|---|---|
| params.code | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
disCompileCoreScript
DisCompileCoreScript decompiles core script byte code. DisCompileCoreScript does not detect syntax errors.
Input
disCompileCoreScript( params )
| Key | Type | Description |
|---|---|---|
| params.code | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
testCoreScript
TestCoreScript tests ScriptProvide and ScriptAccept with TxDataProvide and TxDataAccept.
Input
testCoreScript( params )
| Key | Type | Description |
|---|---|---|
| params.scriptProvide | string | Provide Script Code |
| params.scriptAccept | string | Accept Script Code |
| params.txDataProvide | types.ParamTxData | Parameters for Provide Script |
| params.txDataAccept | types.ParamTxData | Parameters for Accept Script |
types.ParamTxData
| Key | Type | Description |
|---|---|---|
| round | uint32 | |
| height | int32 | |
| args | []string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
getDefFromCoreScript
GetDefFromCoreScript retrieves definitions from byte code.
Input
getDefFromCoreScript ( params )
| Key | Type | Description |
|---|---|---|
| params.code | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
encodeTxNumber
EncodeTxNumber encodes number type data to []byte.
Input
encodeTxNumber ( params )
| Key | Type | Description |
|---|---|---|
| params.number | string | |
| params.type | int |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
encodeTxCurrency
EncodeTxCurrency encodes currency to []byte.
Input
encodeTxCurrency( params )
| Key | Type | Description |
|---|---|---|
| params.currency | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
hash
Hash hashes data according to hash type.
Input
hash ( params )
| Key | Type | Description |
|---|---|---|
| params.data | string | |
| params.type | int |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
verifyMerkleProof
VerifyMerkleProof verifies Proof using Hash and GoalHash.
Input
verifyMerkleProof ( params )
| Key | Type | Description |
|---|---|---|
| params.proof | string | |
| params.hash | string | |
| params.goalHash | string |
Output
| Key | Type | Description |
|---|---|---|
| bool | Result |
calculateTxLinkHash
Calculates TxLink hash.
Input
calculateTxLinkHash( params )
| Key | Type | Description |
|---|---|---|
| params.tx | string | Tx string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
decodeTxs
DecodeTxs decodes base64 encoded transactions downloaded from node.
Input
decodeTxs ( params )
| Key | Type | Description |
|---|---|---|
| params.txs | []string | Array of Tx strings |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
gzipAndEncode
Compresses string and then encodes to base64.
Input
gzipAndEncode ( str )
| Key | Type | Description |
|---|---|---|
| str | string |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
convertCurrency
Converts currency value from one unit to another.
Input
convertCurrency( params )
| Key | Type | Description |
|---|---|---|
| params.v | string | Value |
| params.fromUnit | number | From Unit |
| params.toUnit | number | To Unit |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
makeCurrency
Creates currency value from x and unit.
Input
makeCurrency( params )
| Key | Type | Description |
|---|---|---|
| params.x | string | Value |
| params.unit | number | Unit |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
getHomeShard
Returns home shard ID for the given account.
Input
getHomeShard( params )
| Key | Type | Description |
|---|---|---|
| params.account | string | Account |
| params.totalShard | number | Total Shard Count |
Output
| Key | Type | Description |
|---|---|---|
| - | number | Result |
generateMnemonic
Generates mnemonic phrase with specified entropy bit size.
Input
generateMnemonic( params )
| Key | Type | Description |
|---|---|---|
| params.entropyBit | number | Entropy Bits |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
generateMnemonicBySeed
Generates mnemonic phrase from seed.
Input
generateMnemonicBySeed( params )
| Key | Type | Description |
|---|---|---|
| params.seed | string | Seed |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
deriveKeysFromMnemonic
Derives keys from mnemonic phrase.
Input
deriveKeysFromMnemonic( params )
| Key | Type | Description |
|---|---|---|
| params.mnemonic | string | Mnemonic |
| params.path | string | Path |
| params.atu | number | atu |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
isGrantConsumingTx
Checks if transaction type consumes grant.
Input
isGrantConsumingTx( txTypeStr )
| Key | Type | Description |
|---|---|---|
| txTypeStr | string | Transaction Type String |
Output
| Key | Type | Description |
|---|---|---|
| - | GrantConsumeResult | Result |
verifyByMasterKey
Verifies signature using master public key.
Input
verifyByMasterKey( params )
| Key | Type | Description |
|---|---|---|
| params.pk | string | Public Key |
| params.message | string | Message to verify |
| params.sign | string | Signature |
Output
| Key | Type | Description |
|---|---|---|
| - | string | Result |
checkAddressType
Checks what address type the address is.
Input
checkAddressType( address )
| Key | Type | Description |
|---|---|---|
| address | string | Address |
Output
| Key | Type | Description |
|---|---|---|
| - | number | Result |
isAccount
Checks if address type is account address.
Input
isAccount( addressType )
| Key | Type | Description |
|---|---|---|
| addressType | number | Address Type (Refer to AddressClass type) |
Output
| Key | Type | Description |
|---|---|---|
| - | boolean | Result |
isContract
Checks if address type is contract address.
Input
isContract( addressType )
| Key | Type | Description |
|---|---|---|
| addressType | number | Address Type (Refer to AddressClass type) |
Output
| Key | Type | Description |
|---|---|---|
| - | boolean | Result |
isToken
Checks if address type is token address.
Input
isToken( addressType )
| Key | Type | Description |
|---|---|---|
| addressType | number | Address Type (Refer to AddressClass type) |
Output
| Key | Type | Description |
|---|---|---|
| - | boolean | Result |
isSystemToken
Checks if address type is system token address.
Input
isSystemToken( addressType )
| Key | Type | Description |
|---|---|---|
| addressType | number | Address Type (Refer to AddressClass type) |
Output
| Key | Type | Description |
|---|---|---|
| - | boolean | Result |
loadLocusWasm
Loads Locus WASM module.
Input
loadLocusWasm( ) [None]
Output
| Key | Type | Description |
|---|---|---|
| - | Promise<void> | Result |
loadLocusWasmSync
Loads Locus WASM module synchronously.
Input
loadLocusWasmSync( ) [None]
Output
| Key | Type | Description |
|---|---|---|
| - | void | Result |
verifyTxParams
Verifies transaction parameters.
Input
verifyTxParams( rpcResult, params, options )
| Key | Type | Description |
|---|---|---|
| rpcResult | types.generatedTx | Tx generated by node |
| params | object | Parameters to verify |
| options | Options | Options |
types.generatedTx
| Key | Type | Description |
|---|---|---|
| hash | string | Transaction Hash |
| tx | any | Transaction |
types.Options
| Key | Type | Description |
|---|---|---|
| targetTxHash | string|false |
Output
| Key | Type | Description |
|---|---|---|
| result | boolean | string | Verification result (true/false) or error message |
| reason | string | Reason for failure |
| field | string | (Option) Failed field name |
| txHash | string | (Option) Transaction Hash |