Libraries description
Table of Contents
Address Lib
The address-lib.js
file contains implementation the operations with address representations.
The Power addresses API is explained in detail here.
It exposes following methods:
Method | Code | Description and Usage |
---|---|---|
encodeAddress | encodeAddress(address) | Use this method to parse binary address (like [128, 1, 64, 0, 3, 0, 0, 92] ). The parsing result is an object like the following: {block: 3, checksum: 46, group: 10, scope: "public", txt: "AA100000005033174046", wallet: 92} |
parseTextAddress | decodeAddress(textAddress) | Use this method to parse the text representation of the address (like AA100000005033174046 ). The parsing result is an Uint8Array like [128, 1, 64, 0, 3, 0, 0, 92] |
hexToAddress | hexToAddress(hex) | Use this method to convert the hexadecimal representation of the address (like 800140000300005C ) to binary representation (like [128, 1, 64, 0, 3, 0, 0, 92] ) |
textAddressToHex | textAddressToHex(address) | Use this method to convert the address text representation (like AA100000005033174046 ) to its hexadecimal representation |
isTextAddressValid | isTextAddressValid(textAddress) | This method returns true if provided as an argument address in the text representation is valid, and false — if not |
hexToTextAddress | hexToTextAddress(hexAddress) | Use this method to convert the hexadecimal address representation (like 800140000300005C ) to its text representation (like AA100000005033174046 ) |
Transactions Lib
The transactions-lib.js
file contains implementation of the operations with transactions.
Transaction API is described in detail here.
It exposes following methods:
Method | Code | Description and usage | Arguments accepted | Examples |
---|---|---|---|---|
composeSimpleTransferTX | composeSimpleTransferTX(feeSettings, wif, from, to, token, amount, message, seq) | Use this method to compose, wrap and sign a general purpose transaction (token transfer transaction). Method returns packed and signed transaction, presented in base64 . ATTENTION: The result of this method call with the same arguments may be different since timestamps are used in the transaction | feeSettings — object that contains information regarding fee settings in a chain. It may be empty if the chain does not require a feewif — wallet private key in WIF format;from — text representation of source wallet address;to — text representation of receiving wallet address;token — token name to transfer;amount — amount to transfer;message — message to be included into transaction. May be empty;seq — sequence number for transaction | tpSdk.transactionsLib.composeSimpleTransferTX({}, 'L2vedH1QQaKfes8EkiWnfc1Atx45WMVuXDMVC1nutKAwMSSQQgEg', 'AA100000005033263927', 'AA100000005033174046', 'FTT', 1, '', 2) produces transaction like g6Rib2R5xDiHoWsQoXTPAAABaFHKEeOhZsQIgAFAAAMAA9+idG/ECIABQAADAABcoXMCoXCRkwCjRlRUAaFlgKNzaWeRxGv/RjBEAiAl/YU0097qrBD//YTML5Mqyw0LWdVqQ8L8RYZsTdWhWgIgAiAKohVrASJ0NrQ/BMKFTphJaA5CNK3PyTY6n478aCsCIQOUYecsNUYiEPiwbHJdrWsaZzVTFURFwbcUDqVQiBT5gaN2ZXIC |
composeSCMethodCallTX | composeSCMethodCallTX(address, sc, toCall, wif, feeSettings) | Use this method to compose, wrap and sign an SC method call transaction. Method returns packed and signed transaction, presented in base64 | address — text representation of source wallet address;sc — smart contract address;toCall — array that contains method name and parameters. Format: ['method', [parameters]] ;wif — wallet private key in WIF format;feeSettings — object that contains information regarding fee settings in a chain. It may be empty if the chain does not require a fee; | |
composeStoreTX | composeStoreTX(address, patches, wif, feeSettings) | Use this method to compose, wrap and sign a storage transaction. Method returns packed and signed transaction, presented in base64 | address — text representation of source wallet address;patches — array of patches like {p:[path], v:value, f:'set'} ;toCall — array that contains method name and parameters. Format: ['method', [parameters]] ;wif — wallet private key in WIF format;feeSettings — object that contains information regarding fee settings in a chain. It may be empty if the chain does not require a fee; | |
composeRegisterTX | async composeRegisterTX(wif, referrer) | Use this method to compose and sign registration transaction. The method returns a promise that will return packed and signed transaction, presented in base64 upon resolve.ATTENTION: The result of this method call with the same arguments may be different since timestamps are used in transaction | wif — newly generated wallet private key in WIF format;referrer — text representation of the wallet address that will be used as a referrer of a newly created wallet (Optional) | await tpSdk.transactionsLib.composeRegisterTX('L2vedH1QQaKfes8EkiWnfc1Atx45WMVuXDMVC1nutKAwMSSQQgEg') produces transaction like g6Rib2R5xDyEoWsRoXTPAAABaFHQt6mlbm9uY2XNAfGhaMQg5cQq5McyGwdywxhoHrUeCWmQgqdnUUf/uCUVqkeWyK+jc2lnkcRs/0cwRQIhALQ6z/biF10pPrMfMlPTlbHFiRQDsACgfVpf6hEIrayPAiAQjaQcZmgB4h+g+RdkR/lAIaFrE3gaV9aQ3RiyD7cGOgIhA5Rh5yw1RiIQ+LBscl2taxpnNVMVREXBtxQOpVCIFPmBo3ZlcgI= |
calculateFee | calculateFee(feeSettings, from, to, token, amount, message, seq) | Use this method to calculate the amount of fee required to complete the transaction. Method returns array like [1, "SK", 100] , where:- 1 — fee signature;- "SK" — fee currency;- 100 — fee ammount | feeSettings — object that contains information regarding fee settings in a chain. It can be empty if the chain does not require fee;from — text representation of source wallet address;to — text representation of receiving wallet address;token — token name to transfer;amount — amount to transfer;message — message to be included into transaction. Can be empty.seq — sequence number for transaction | tpSdk.transactionsLib.calculateFee({feeCur: 'SK', fee: 100, baseEx: 100, kb: 500}, 'AA100000005033263927', 'AA100000005033174046', 'FTT', 1, '', 2) produces an array of [1, "SK", 100] |
packAndSignTX | packAndSignTX(tx, wif) | Use this method to pack and sign existing transaction. Method returns packed and signed transaction, presented in base64 | tx — object that contains transaction;wif — wallet private key in WIF format. | tx = { k: 16, t: 1547575208280, f: [128, 1, 64, 0, 1, 0, 7, 20], to: [128, 1, 64, 0, 3, 0, 3, 40], s: 2, p: [[0, 'FTT', 2]] } tpSdk.transactionsLib.packAndSignTX(tx, 'L2vedH1QQaKfes8EkiWnfc1Atx45WMVuXDMVC1nutKAwMSSQQgEg') produces the following transaction: g6Rib2R5xDWGoWsQoXTPAAABaFKrqVihZpjMgAFAAAEABxSidG+YzIABQAADAAMooXMCoXCRkwCjRlRUAqNzaWeRxGv/RjBEAiB4YgIlerGNYhPNCCCWkJF1yWfjs499lfAwU0+oGgLjpQIgORgauyggsblOzp90Odtgsm+kPstlwSSAV9NBLEQVpyoCIQOUYecsNUYiEPiwbHJdrWsaZzVTFURFwbcUDqVQiBT5gaN2ZXIC |
prepareTXFromSC | prepareTXFromSC(feeSettings, address, seq, scData, gasToken = 'SK', gasValue = 5000) | Use this method to complete transaction generated by smart contract. Transactions generated by smart contracts lack source address, sequence number, timestamp and fee and gas amounts. Method returns an object containing new transaction | feeSettings — object that contains information regarding fee settings in a chain. It can be empty if the chain does not require a fee;address — text representation of source wallet address;seq — sequence number for transaction;scData — partial transaction, returned by smart contract;gasToken — name of the token used for Gas (default is SK);gasValue — maximum amount to be spent on Gas (default is 5000 ) | feeSettings = {feeCur: "SK", fee: 100, baseEx: 100, kb: 500};<br/>scData = { k: 16, p: [[0, 'SK', 10]], c: ["save", [1]] }; tpSdk.transactionsLib.prepareTXFromSC(feeSettings, 'AA100000005033263927', 2, scData) produces the following transaction: { k: 16, p: [[3, 'SK', 5000], [0, 'SK', 10], [1, "SK", 100]], c: ["save", [1]], f: [128, 1, 64, 0, 3, 0, 3, 223], s: 2, t: 1547578028384, } |
decodeTx | decodeTx(tx) | Use this method to decode transaction. It accepts transactions in both base64 and binary formats | N/A | tpSdk.transactionsLib.decodeTx('g6Rib2R5xDWGoWsQoXTPAAABaFKrqVihZpjMgAFAAAEABxSidG+YzIABQAADAAMooXMCoXCRkwCjRlRUAqNzaWeRxGv/RjBEAiB4YgIlerGNYhPNCCCWkJF1yWfjs499lfAwU0+oGgLjpQIgORgauyggsblOzp90Odtgsm+kPstlwSSAV9NBLEQVpyoCIQOUYecsNUYiEPiwbHJdrWsaZzVTFURFwbcUDqVQiBT5gaN2ZXIC') produces { body: { f: [128, 1, 64, 0, 1, 0, 7, 20], k: 16, p: [[0, "FTT", 2]] s: 2, t: 1547575208280, to: (8) [128, 1, 64, 0, 3, 0, 3, 40], } sig: <Uint8Array (107) 255, 70, 48, 68, 2, 32, 120, 98, 2, 37, 122, 177, 141, 98, 19, 205 ...>, ver: 2} |
listValidTxSignatures | listValidTxSignatures(tx) | Use this method to validate transaction signatures. It accepts transactions in both base64 and binary formats. Method returns an object containing list of bsig with valid signatures (validSignatures field) and count of invalid signatures (invalidSignaturesCount field) | tpSdk.transactionsLib.listValidTxSignatures('g6Rib2R5xDWGoWsQoXTPAAABaFKrqVihZpjMgAFAAAEABxSidG+YzIABQAADAAMooXMCoXCRkwCjRlRUAqNzaWeRxGv/RjBEAiB4YgIlerGNYhPNCCCWkJF1yWfjs499lfAwU0+oGgLjpQIgORgauyggsblOzp90Odtgsm+kPstlwSSAV9NBLEQVpyoCIQOUYecsNUYiEPiwbHJdrWsaZzVTFURFwbcUDqVQiBT5gaN2ZXIC') produces { invalidSignaturesCount: 0 validSignatures: <Uint8Array (107) 255, 70, 48, 68, 2, 32, 120, 98, 2, 37, 122, 177, 141, 98, 19, 205 ...>} | |
extractTaggedDataFromBSig | extractTaggedDataFromBSig(tag, bsig) | Use this method to extract tagged data from bsig binary. Method returns binary representation of the tagged data | tag — target data tag. Value should lie between 0 and 255;bsig — bsig binary that contains tagged data. | bsig = new Uint8Array([1, 5, 1, 2, 3, 4, 5, 2, 3, 3, 2, 1]); tpSdk.transactionsLib.extractTaggedDataFromBSig(2, bsig); produces <Uint8Array (3) 3, 2, 1> |
Note
feeSettings
object has the following fields:
feeCur
— name of the token that is used for fee;fee
— minimal fee value;baseEx
— number of bytes allowed for minimal fee;kb
— cost of each Kb above the minimum.
Each chain has its own fee settings. Chain settings are available via nodes HTTP API.
Network Lib
The network-lib.js
file contains implementation of the operations with blockchain.
It exposes following methods:
Method | Code | Description and usage | Arguments accepted |
---|---|---|---|
sendTxAndWaitForResponse | sendTxAndWaitForResponse(tx, chain, timeout) | Use this method to send transaction to selected chain. Method returns a Promise that resolves to information about created transaction or error information | tx — packed and signed transaction in base64;chain — chain number;timeout — timeout in seconds. Defaults to 120. |
getFeeSettings | async getFeeSettings(chain) | Use this method to load fee settings from selected chain. Method asynchronously returns an object containing chain's fee settings | chain — chain number |
getBlock | async getBlock(chain, hash = 'last') | Use this method to load given block from selected chain. Method asynchronously returns an object containing block | chain — chain number;hash — hash of a block to get; |
getWallet | async getWallet(chain, address) | Use this method to load given wallet information from selected chain. Method asynchronously returns an object containing wallet infomation | chain - chain number;address - address of a wallet to get |
addChain | async addChain(number, nodes) | Use this method to add new chain to then list. Method does not return a value | number — chain numbernodes — array containing data about chain nodes. Each array element should have the following format: {address: < address>, nodeId: < id>} , where:- address is a node URL (like http://51.15.248.43:49841 or http://c103n1.thepower.io:49841)- nodeId is string identifying the node; |
scLoader Lib
The sc_loader.js
file contains implementation of the instantiation of smart contract.
Method | Code | Description and usage | Arguments accepted |
---|---|---|---|
instantiateSC | async instantiateSC(address, chain) | Use this method to instantiate remote SC. This method returns an object with instantiated smart contract. The returned object exposes executeMethod | address — text representation of smart contract address;chain — chain number |
loadScLocal | async loadScLocal(code, state = {}, balance = {}) | Use this method to locally load smart contract from. This method returns an object with an instantiated smart contract. Returned object exposes executeMethod | code — typed array (Uint8Array ), containing the smart contract code;state — object containing smart contract state. balance — object containing smart contract balance. |
executeMethod | Use this method to execute method of a smart contract. This method asynchronously executes specified smart contract method | method — method nameparams — array with method parameters |
What's next?
Visit our Hello World apps section to learn how to work with our