Skip to content
This repository has been archived by the owner on Jun 14, 2022. It is now read-only.

Some methods of data convert and query

JianweiZheng edited this page Mar 25, 2021 · 5 revisions

convert contract id and token id to each other

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
let contractId = convert.tokenIDToContractID("<token_id>");
// Only NFT need to add token_index as a parameter. The default value of token_index in other tokens is 0, which does not need to be added
let tokenId = convert.contractIDToTokenID("<contract_id>", "token_index");
console.log(contractId);
console.log(tokenId);

get token index from token id (offline)

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
let tokenIndex = convert.getTokenIndex("<token_id>")
console.log(tokenIndex)

get last token index from contract id (online)

// build chain
const vsys = require("@virtualeconomy/js-v-sdk");
const constants = vsys.constants;
const node_address = "https://test.v.systems/api"; // change to your node address
const network_byte = constants.TESTNET_BYTE;
var chain = new vsys.Blockchain(node_address, network_byte);

async function getLastTokenIndex(chain, contract_id) {
    const result = await chain.getLastTokenIndex(contract_id);
    return result;
}
getLastTokenIndex(chain, "<contract_id>");

convert the public key to address

//create account, such as by address
const vsys = require("@virtualeconomy/js-v-sdk");
const constants = vsys.constants;
let acc = new vsys.Account(constants.TESTNET_BYTE);
acc.buildColdWallet('', "<address>");

// Convert public key to address
// network byte: constants.TESTNET_BYTE or constants.MAINNET_BYTE
let address = acc.convertPublicKeyToAddress("<public_key>", "<network_byte>");
console.log(address);

methods for querying variables within a contract

const vsys = require("@virtualeconomy/js-v-sdk");
const constants = vsys.constants;
const node_address = "https://test.v.systems/api"; // change to your node address
const network_byte = constants.TESTNET_BYTE;
let chain = new vsys.Blockchain(node_address, network_byte);

async function getContractData(chain, contract_id, state_index, data_type, data) {
    const result = await chain.getContractData(contract_id, state_index, data_type, data);
    return result;
}

let contractData = getContractData(chain, "<contract_id>", "<state_index>", "<data_type>", "<data>")
console.log(contractData)

calculate the transaction id offline

// build transaction object
const vsys = require("@virtualeconomy/js-v-sdk");
let constants = vsys.constants;
const network_byte = constants.TESTNET_BYTE;
let tra = new vsys.Transaction(network_byte);
tra.buildPaymentTx("<public_key>", "<recipient address>", "<amount>", "<attachment>", "<timestamp>");
// get bytes
let bytes = tra.toBytes();

// get transaction id from tra.buildTransactionId
let transaction_id = tra.buildTransactionId(bytes);

// get transaction id from crypto.buildTransactionId
const crypto = vsys.Crypto;
let transaction_id = crypto.buildTransactionId(bytes);

console.log(transaction_id)

calculate signature offline

// build aacount and transaction object
const vsys = require("@virtualeconomy/js-v-sdk");
var constants = vsys.constants;
const network_byte = constants.TESTNET_BYTE;
let acc = new vsys.Account(network_byte);
acc.buildColdWallet('', "<address>");

var tra = new vsys.Transaction(network_byte);
tra.buildPaymentTx("<public_key>", "<recipient address>", "<amount>", "<attachment>", "<timestamp>");
// get bytes
let bytes = tra.toBytes();

//calculate signature
let signature = acc.getSignature(bytes);
console.log(signature)

convert function data base58 to data entry

// tra: your transaction object, acc: your account object, chain: your blockchain object, build them first!
const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
//get bytes
let bytes = tra.toBytes();

//calculate signature
let signature = acc.getSignature(bytes);
let send_tx = tra.toJsonForSendingTx(signature);

/*such as send_tx is 
{
attachment: "3yZe7d",
contractId: "CEwnu6BJhAVdsbr8DhBUhpzDEuiYwwA2ghd",
fee: 30000000,
feeScale: 100,
functionData: "14uNyP6WGKeMi3mWMu5g6z5v9NXNhLM1iauegC9SDJywVBFH1BV",
functionIndex: 4,
senderPublicKey: "DWKUGdT1HL4a3zrhoeRJd2zfKRPxmknRotdGVFrViK7o",
signature: "26ddXmEpaEB1pG7QaEAnnVrhdAmZNhaVA2noeYM9iL42VzcHdQ3SAd3rsMfqifCvL33to4U3yWKUNcaqiz52X1sm",
timestamp: 1615280855350000000
}
*/

//convert function data base58 to data entry
let parse_function_data = convert.parseFunctionData(send_tx['functionData']);
console.log(parse_function_data)
/*Array (2 items)
0: Object {data: "AUAMpgTZdAVD4kF6J1gLB93vkiPDmaTtuYw", type: 2}
1: Object {data: BigNumber {s: 1, e: 10, c: [10000000000]}, type: 3}
*/

convert boolean to bytes

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
var input_int = false
var arr = convert.booleanToBytes(input_int)
console.log(arr)

convert short to bytes

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
var input_int = 300
var arr = convert.shortToByteArray(input_int)
console.log(arr)

convert int32 to bytes

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
var input_int = 300
var arr = convert.idxToByteArray(input_int)
console.log(arr)

convert long to bytes

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
var input_int = 300
var arr = convert.longToByteArray(input_int)
console.log(arr)

convert string to bytes

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
var input_int = "300"
var arr = convert.stringToByteArray(input_int)
console.log(arr)

convert bytes to short

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
var bytes = [1, 44]
var short = convert.byteArrayToShort(bytes)
console.log(short)

convert bytes to int32

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
var bytes = [0, 0, 1, 44]
var int32 = convert.byteArrayToInt(bytes)
console.log(short)

convert bytes to long

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
var bytes = [0, 0, 0, 0, 0, 0, 1, 44]
var long = convert.byteArrayToLong(bytes)
console.log(long)

convert bytes to string

const vsys = require("@virtualeconomy/js-v-sdk");
const convert = vsys.Convert;
var bytes = [97, 115, 100]
var str = convert.byteArrayToString(bytes)
console.log(str)
Clone this wiki locally