virtualeconomy / js-v-sdk

[DEPRECATED and please use https://github.com/virtualeconomy/js-vsys instead] Java Script library for V Systems
https://www.npmjs.com/package/@virtualeconomy/js-v-sdk
MIT License
10 stars 5 forks source link

how to get transaction id #41

Closed meiseayoung closed 3 years ago

meiseayoung commented 3 years ago

how to get transaction id ?

SheldonYS commented 3 years ago

There are 2 ways to get transaction ID (online and offline).

Method 1 (get transaction ID online):

You can send the transaction to node, the transaction ID will appear in response.

// tx: your transaction object, acc: your account object, chain: your blockchain object, build them first!
const node_address = "https://test.v.systems/api"; // change to your node address

async function sendPaymentTx(tx) {
    const result = await acc.sendTransaction(chain, tx);
    console.log(result);
}

// Create Transaction Object
let public_key = acc.getPublicKey();
let timestamp = Date.now() * 1e6;
tx.buildPaymentTx(public_key, "<recipient address>", "<amount>", "<attachment>", timestamp);

// Get bytes
let bytes = tx.toBytes();

// Get signature
let signature = acc.getSignature(bytes);

// Get json for sending tx
let send_tx = tx.toJsonForSendingTx(signature);

// Send transaction
sendPaymentTx(send_tx);

You will get response like this:

{
  "type": 2,
  "id": "EoNQyNouEKg8pDcEEPY2dJL9FMQx61YFk1Sn5EJN8H7K",   <--- Transaction ID
  "fee": 10000000,
  "timestamp": 1544083814291691000,
  "proofs": [ ... ],
  ...
}

Method 2 (calculate transaction ID offline):

Once you get transaction serialized bytes (refer previous code)

...
tx.buildPaymentTx(public_key, "<recipient address>", "<amount>", "<attachment>", timestamp);
let bytes = tx.toBytes();

Then you hash the bytes with blake2b. And finally show the hashed bytes with base58. That will be the transaction ID.

meiseayoung commented 3 years ago

@SheldonYS Then you hash the bytes with blake2b. And finally show the hashed bytes with base58. That will be the transaction ID. how to do in code. can you show me an example ? think you for you help.

SheldonYS commented 3 years ago

Here is sample code for you reference:

const base58 = require('base-58');
const blake2b = require('blake2b');
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);

// Create Transaction Object
let sender_public_key = "DREQ2s3dQzaKTsvnmKWTnj59ptM1NzXwmap8jYVdPeLC";
let timestamp = 1605847345989000000;
let recipient_address = "AU1L8NVL9NTvu4n4XbP3fPJ8b5gQGjRG17W";
let amount = 3.2;
let tx = new vsys.Transaction(network_byte);
tx.buildPaymentTx(sender_public_key, recipient_address, amount, "", timestamp);

// Get bytes
let bytes = tx.toBytes();
let tx_id_bytes = new Uint8Array(32);

// blake2b hash
blake2b(tx_id_bytes.length).update(bytes).digest(tx_id_bytes);

// base58 encode
let tx_id = base58.encode(tx_id_bytes);

// The Transaction ID should be "FYcfT6A3N8mJMScJjN9QKQHBeBhdhBSLSMyzBiCwGb5Y".
console.log(tx_id);
meiseayoung commented 3 years ago

@SheldonYS it's work. thank you very much

0x100cc commented 3 years ago

There is already a method to get transaction id in the SDK image

Here is the sample code

const base58 = require('base-58');
const blake2b = require('blake2b');
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);

// Create Transaction Object
let sender_public_key = "DREQ2s3dQzaKTsvnmKWTnj59ptM1NzXwmap8jYVdPeLC";
let timestamp = 1605847345989000000;
let recipient_address = "AU1L8NVL9NTvu4n4XbP3fPJ8b5gQGjRG17W";
let amount = 3.2;
let tx = new vsys.Transaction(network_byte);
tx.buildPaymentTx(sender_public_key, recipient_address, amount, "", timestamp);

// Get bytes
let bytes = tx.toBytes();
let tx_id = tx.buildTransactionId(bytes);

// The Transaction ID should be "FYcfT6A3N8mJMScJjN9QKQHBeBhdhBSLSMyzBiCwGb5Y".
console.log(tx_id);
meiseayoung commented 3 years ago

There is already a method to get transaction id in the SDK image

Here is the sample code

const base58 = require('base-58');
const blake2b = require('blake2b');
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);

// Create Transaction Object
let sender_public_key = "DREQ2s3dQzaKTsvnmKWTnj59ptM1NzXwmap8jYVdPeLC";
let timestamp = 1605847345989000000;
let recipient_address = "AU1L8NVL9NTvu4n4XbP3fPJ8b5gQGjRG17W";
let amount = 3.2;
let tx = new vsys.Transaction(network_byte);
tx.buildPaymentTx(sender_public_key, recipient_address, amount, "", timestamp);

// Get bytes
let bytes = tx.toBytes();
let tx_id = tx.buildTransactionId(bytes);

// The Transaction ID should be "FYcfT6A3N8mJMScJjN9QKQHBeBhdhBSLSMyzBiCwGb5Y".
console.log(tx_id);

thank u. it's work