Skip to main content

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

npm i @locuschain/lib

Usage

LocusLib.function(param1, param2, ... )

  • function: Function Name
  • param: Parameter
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

KeyTypeDescription
LibraryVersionintLibrary Version
KeyStoreVersionintKeystore Version
KeyStoreVersionMinintMinimum Compatible Keystore Version
GoBuildVersionstringGolang Version used to build Library

createAccountAndKeystore

Creates two separate keystore files for master key and normal key.

Input

createAccountAndKeystore( params )

KeyTypeDescription
params.passwordMasterstringMaster Key Password
params.passwordNormalstringNormal Key Password
params.algoMasterstringMaster Key Algorithm
params.algoNormalstringNormal Key Algorithm

Output

KeyTypeDescription
-stringMaster and Normal Keystore String

createNormalKey

Generates a new normal key for the given address.

Input

createNormalKey( params )

KeyTypeDescription
params.addrStrstringAddress
params.keyAlgostringNormal Key Algorithm

Output

KeyTypeDescription
-stringNormal Key String

createMasterKeystore

Creates a master keystore for the given address.

Input

createMasterKeystore( params )

KeyTypeDescription
params.addrStrstringAddress
params.passwordstringMaster Key Password
params.mskStrstringMaster Secret Key
params.mpkStrstringMaster Public Key

Output

KeyTypeDescription
-stringMaster Keystore String

createNormalKeystore

Creates a normal keystore for the given address.

Input

createNormalKeystore( params )

KeyTypeDescription
params.addrStrstringAddress
params.passwordstringNormal Key Password
params.nskStrstringNormal Secret Key
params.npkStrstringNormal Public Key
params.mpkStrstringMaster Public Key
params.keySignstringKey Signature

Output

KeyTypeDescription
-stringNormal Keystore String

loadMasterKeystore

Returns the master key from the master keystore.

Input

loadMasterKeystore( params )

KeyTypeDescription
params.passStrstringMaster Key Password
params.ksJsonstringMaster Keystore String

Output

KeyTypeDescription
-stringMaster Key String

loadNormalKeystore

Returns the normal key from the normal keystore.

Input

loadNormalKeystore ( params )

KeyTypeDescription
params.passStrstringNormal Key Password
params.ksJsonstringNormal Keystore String

Output

KeyTypeDescription
-stringNormal Key String

convertToData

Decodes various values to default encoding (base64).

Input

convertToData( value, valueType )

KeyTypeDescription
valuestring
valueTypeintvalueType : 0(raw string), 1(hex), 2(crc16-base32), 3(base64)

Output

KeyTypeDescription
-string

convertDataTo

Converts default encoded (base64) values to various types.

Input

convertDataTo( value, retType )

KeyTypeDescription
valuestring
retTypeintvalueType : 0(raw string), 1(hex), 2(crc16-base32), 3(base64)

Output

KeyTypeDescription
-stringResult

convertStringToData

Decodes string values to default encoding (base64).

Input

convertStringToData( raw )

KeyTypeDescription
rawstring

Output

KeyTypeDescription
-stringResult

convertDataToString

Decodes default encoded (base64) values to string values.

Input

convertDataToString ( str )

KeyTypeDescription
strstring

Output

KeyTypeDescription
-stringResult

convertAddressToHex

Decodes base32 encoded address to hex value.

Input

convertAddressToHex( addrEncoded )

KeyTypeDescription
addrEncodedstring

Output

KeyTypeDescription
-stringResult

convertHexToAddress

Encodes hex value to base32 encoded address.

Input

convertHexToAddress( hexString )

KeyTypeDescription
hexStringstring

Output

KeyTypeDescription
-stringResult

convertAddressToData

Decodes base32 encoded address to default encoding.

Input

convertAddressToData( addrEncoded )

KeyTypeDescription
addrEncodedstring

Output

KeyTypeDescription
-stringResult

convertDataToAddress

Encodes default encoded value to base32 encoded address.

Input

convertDataToAddress( str )

KeyTypeDescription
strstring

Output

KeyTypeDescription
-stringResult

convertBase32ToHex

Decodes base32 encoded value to hex value.

Input

convertBase32ToHex( encoded )

KeyTypeDescription
encodedstring

Output

KeyTypeDescription
-stringResult

convertHexToBase32

Encodes hex value to base32 encoded value.

Input

convertHexToBase32( hexString )

KeyTypeDescription
hexStringstring

Output

KeyTypeDescription
-stringResult

convertHexToData

Decodes hex encoded value to default encoding (base64).

Input

convertHexToData( encoded )

KeyTypeDescription
encodedstring

Output

KeyTypeDescription
-stringResult

convertDataToHex

Encodes default encoded (base64) value to hex encoded value.

Input

convertDataToHex( str )

KeyTypeDescription
strstring

Output

KeyTypeDescription
-stringResult

convertBase32ToData

Decodes base32 encoded value to default encoding (base64). convertBase32ToData( encoded )

Input

KeyTypeDescription
encodedstring

Output

KeyTypeDescription
-stringResult

convertDataToBase32

Encodes default encoded (base64) value to base32 encoded value.

Input

convertDataToBase32( str )

KeyTypeDescription
strstring

Output

KeyTypeDescription
-stringResult

signByMasterKey

Generates a signature using master secret key and message.

Input

signByMasterKey ( params )

KeyTypeDescription
params.mskstringMaster Secret Key
params.messagestringMessage to sign

Output

KeyTypeDescription
-stringResult

sign

Generates a signature using normal secret key and message.

Input

sign( params )

KeyTypeDescription
params.skstringNormal Secret Key
params.messagestringMessage to sign

Output

KeyTypeDescription
-stringResult

verify

Returns the result of verifying signature using message and normal public key.

Input

verify( params )

KeyTypeDescription
params.pkstringNormal Public Key
params.messagestringMessage to verify
params.signstringSignature to verify

Output

KeyTypeDescription
-stringResult

verifyTx

Verifies all types of transactions generated by RPC.

Input

verifyTx( jsonTx )

KeyTypeDescription
jsonTxstring

Output

KeyTypeDescription
-stringResult

compileCoreScript

CompileCoreScript compiles core script source code to byte code.

Input

compileCoreScript( params )

KeyTypeDescription
params.codestring

Output

KeyTypeDescription
-stringResult

disCompileCoreScript

DisCompileCoreScript decompiles core script byte code. DisCompileCoreScript does not detect syntax errors.

Input

disCompileCoreScript( params )

KeyTypeDescription
params.codestring

Output

KeyTypeDescription
-stringResult

testCoreScript

TestCoreScript tests ScriptProvide and ScriptAccept with TxDataProvide and TxDataAccept.

Input

testCoreScript( params )

KeyTypeDescription
params.scriptProvidestringProvide Script Code
params.scriptAcceptstringAccept Script Code
params.txDataProvidetypes.ParamTxDataParameters for Provide Script
params.txDataAccepttypes.ParamTxDataParameters for Accept Script

types.ParamTxData

KeyTypeDescription
rounduint32
heightint32
args[]string

Output

KeyTypeDescription
-stringResult

getDefFromCoreScript

GetDefFromCoreScript retrieves definitions from byte code.

Input

getDefFromCoreScript ( params )

KeyTypeDescription
params.codestring

Output

KeyTypeDescription
-stringResult

encodeTxNumber

EncodeTxNumber encodes number type data to []byte.

Input

encodeTxNumber ( params )

KeyTypeDescription
params.numberstring
params.typeint

Output

KeyTypeDescription
-stringResult

encodeTxCurrency

EncodeTxCurrency encodes currency to []byte.

Input

encodeTxCurrency( params )

KeyTypeDescription
params.currencystring

Output

KeyTypeDescription
-stringResult

hash

Hash hashes data according to hash type.

Input

hash ( params )

KeyTypeDescription
params.datastring
params.typeint

Output

KeyTypeDescription
-stringResult

verifyMerkleProof

VerifyMerkleProof verifies Proof using Hash and GoalHash.

Input

verifyMerkleProof ( params )

KeyTypeDescription
params.proofstring
params.hashstring
params.goalHashstring

Output

KeyTypeDescription
boolResult

calculateTxLinkHash

Calculates TxLink hash.

Input

calculateTxLinkHash( params )

KeyTypeDescription
params.txstringTx string

Output

KeyTypeDescription
-stringResult

decodeTxs

DecodeTxs decodes base64 encoded transactions downloaded from node.

Input

decodeTxs ( params )

KeyTypeDescription
params.txs[]stringArray of Tx strings

Output

KeyTypeDescription
-stringResult

gzipAndEncode

Compresses string and then encodes to base64.

Input

gzipAndEncode ( str )

KeyTypeDescription
strstring

Output

KeyTypeDescription
-stringResult

convertCurrency

Converts currency value from one unit to another.

Input

convertCurrency( params )

KeyTypeDescription
params.vstringValue
params.fromUnitnumberFrom Unit
params.toUnitnumberTo Unit

Output

KeyTypeDescription
-stringResult

makeCurrency

Creates currency value from x and unit.

Input

makeCurrency( params )

KeyTypeDescription
params.xstringValue
params.unitnumberUnit

Output

KeyTypeDescription
-stringResult

getHomeShard

Returns home shard ID for the given account.

Input

getHomeShard( params )

KeyTypeDescription
params.accountstringAccount
params.totalShardnumberTotal Shard Count

Output

KeyTypeDescription
-numberResult

generateMnemonic

Generates mnemonic phrase with specified entropy bit size.

Input

generateMnemonic( params )

KeyTypeDescription
params.entropyBitnumberEntropy Bits

Output

KeyTypeDescription
-stringResult

generateMnemonicBySeed

Generates mnemonic phrase from seed.

Input

generateMnemonicBySeed( params )

KeyTypeDescription
params.seedstringSeed

Output

KeyTypeDescription
-stringResult

deriveKeysFromMnemonic

Derives keys from mnemonic phrase.

Input

deriveKeysFromMnemonic( params )

KeyTypeDescription
params.mnemonicstringMnemonic
params.pathstringPath
params.atunumberatu

Output

KeyTypeDescription
-stringResult

isGrantConsumingTx

Checks if transaction type consumes grant.

Input

isGrantConsumingTx( txTypeStr )

KeyTypeDescription
txTypeStrstringTransaction Type String

Output

KeyTypeDescription
-GrantConsumeResultResult

verifyByMasterKey

Verifies signature using master public key.

Input

verifyByMasterKey( params )

KeyTypeDescription
params.pkstringPublic Key
params.messagestringMessage to verify
params.signstringSignature

Output

KeyTypeDescription
-stringResult

checkAddressType

Checks what address type the address is.

Input

checkAddressType( address )

KeyTypeDescription
addressstringAddress

Output

KeyTypeDescription
-numberResult

isAccount

Checks if address type is account address.

Input

isAccount( addressType )

KeyTypeDescription
addressTypenumberAddress Type (Refer to AddressClass type)

Output

KeyTypeDescription
-booleanResult

isContract

Checks if address type is contract address.

Input

isContract( addressType )

KeyTypeDescription
addressTypenumberAddress Type (Refer to AddressClass type)

Output

KeyTypeDescription
-booleanResult

isToken

Checks if address type is token address.

Input

isToken( addressType )

KeyTypeDescription
addressTypenumberAddress Type (Refer to AddressClass type)

Output

KeyTypeDescription
-booleanResult

isSystemToken

Checks if address type is system token address.

Input

isSystemToken( addressType )

KeyTypeDescription
addressTypenumberAddress Type (Refer to AddressClass type)

Output

KeyTypeDescription
-booleanResult

loadLocusWasm

Loads Locus WASM module.

Input

loadLocusWasm( ) [None]

Output

KeyTypeDescription
-Promise<void>Result

loadLocusWasmSync

Loads Locus WASM module synchronously.

Input

loadLocusWasmSync( ) [None]

Output

KeyTypeDescription
-voidResult

verifyTxParams

Verifies transaction parameters.

Input

verifyTxParams( rpcResult, params, options )

KeyTypeDescription
rpcResulttypes.generatedTxTx generated by node
paramsobjectParameters to verify
optionsOptionsOptions

types.generatedTx

KeyTypeDescription
hashstringTransaction Hash
txanyTransaction

types.Options

KeyTypeDescription
targetTxHashstring|false

Output

KeyTypeDescription
resultboolean | stringVerification result (true/false) or error message
reasonstringReason for failure
fieldstring(Option) Failed field name
txHashstring(Option) Transaction Hash