Tron - Sign a transaction in JS (NodeJS) - node.js

I am trying to sign a build & sign transaction then create a hex to broadcast the transaction on TRON Network.
I have successfully done this but when i broadcast this transaction i am getting "TRON TAPOS_ERROR" error.
I have searched and got the reason that i have to include last block number and hash.
check this : https://github.com/tronprotocol/java-tron/issues/857
But I don't know how to do this.
I have tried this code :
const CryptoUtils = require("#tronscan/client/src/utils/crypto");
const TransactionUtils = require("#tronscan/client/src/utils/transactionBuilder");
async function transferContractTx() {
const fromAddress = "FROM_ADDRESS";
const toAddress = "TO_ADDRESS";
const privateKey = "MY_PRIVATE_KEY";
const token = "TRX";
const amount = 1000000;
let transaction = TransactionUtils.buildTransferTransaction(token, fromAddress, toAddress, amount);
console.log(JSON.stringify(transaction));
let signedTransaction = CryptoUtils.signTransaction(privateKey, transaction);
console.log(signedTransaction);
}
transferContractTx();

Related

Converting .NodeJS Authentication Token script to Apps Script

I am a newer Apps Scripts user who has been stuck on this problem for some time. I am trying to connect my company's reporting feature to a Google Sheet so I can create reporting dashboards for our users.
I have a .jsNode code snippet which I am able to successfully run outside of the Apps Scripts editor, but I am having issues translating it to Apps Scripts.
My goal is to be able to generate an Authentication Token which will be used in a header within a POST request. I will then use this to get a specific URL that I can pull data from (it will be a .csv file. I already feel I can accomplish this using another script)
Below is the .NodeJS code snippet:
const crypto = require('crypto');
module.exports.init = function () {
let prefix = 'abc-admin-v1'
let businessId = 'XXXX'
let publicAppKeyId = 'XXXX'
let secretKey = 'XXXX'
let unixTimestamp = Math.floor(Date.now() / 1000)
let payload = `${prefix}${businessId}${unixTimestamp}`
let rawKey = Buffer.from(secretKey, 'base64')
let signature = crypto.createHmac('sha256', rawKey).update(payload, 'utf8').digest('base64')
let token = `${signature}${payload}`
let httpBasicPayload = `${publicAppKeyId}:${token}`
let httpBasicCredentials = Buffer.from(httpBasicPayload, 'utf8').toString('base64')
console.log(httpBasicCredentials);
};
Below will be what I have in Apps Scripts:
function apiInfo() {
var secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var apiKey = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
var locationID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
var prefix = "abc-admin-v1";
var timeStamp = Math.round((new Date()).getTime() / 1000);
var timeStampReal = JSON.stringify(timeStamp);
var tokenPayload = prefix + businessID + timeStampReal;
var enCodeTokenPayload = Utilities.base64Encode(tokenPayload);
var deCodeTokenPayload = Utilities.base64Decode(enCodeTokenPayload);
var tokenPayloadReal = JSON.stringify(tokenPayload);
var rawKey = Utilities.base64Decode(secret);
var rawMac = Utilities.computeHmacSha256Signature(tokenPayloadReal, rawKey);
var signature = Utilities.base64Encode(rawMac);
var token = signature + tokenPayload;
var httpBasicPayload = apiKey + ":" + token;
var httpBasicCredentials = Utilities.base64Encode(httpBasicPayload);
When testing using the current GAS code, it produces an invalid Auth Token.
When I run the Node.js version outside of GAS, it produces a valid Auth Token. I am looking to find out what is currently wrong with my code that is producing an invalid auth token. I would like to keep the project within GAS if possible.
I believe your goal is as follows.
You want to convert your showing Node.js script to Google Apps Script. Namely, you want to retrieve the same values of httpBasicCredentials between your Node.js script and Google Apps Script.
const crypto = require('crypto');
module.exports.init = function () {
let prefix = 'abc-admin-v1'
let businessId = 'XXXX'
let publicAppKeyId = 'XXXX'
let secretKey = 'XXXX'
let unixTimestamp = Math.floor(Date.now() / 1000)
let payload = `${prefix}${businessId}${unixTimestamp}`
let rawKey = Buffer.from(secretKey, 'base64')
let signature = crypto.createHmac('sha256', rawKey).update(payload, 'utf8').digest('base64')
let token = `${signature}${payload}`
let httpBasicPayload = `${publicAppKeyId}:${token}`
let httpBasicCredentials = Buffer.from(httpBasicPayload, 'utf8').toString('base64')
console.log(httpBasicCredentials);
};
When I saw your showing Google Apps Script, unfortunately, there are several undeclared variable and unused variables. By this, in this answer, I would like to propose to directly converting your showing Node.js to Google Apps Script.
Sample script:
function myFunction() {
let prefix = 'abc-admin-v1'
let businessId = 'XXXX'
let publicAppKeyId = 'XXXX'
let secretKey = 'XXXX'
let unixTimestamp = Math.floor(Date.now() / 1000);
let payload = `${prefix}${businessId}${unixTimestamp}`;
let payloadBytes = Utilities.newBlob(payload).getBytes();
let rawKey = Utilities.base64Decode(secretKey);
let signature = Utilities.base64Encode(Utilities.computeHmacSha256Signature(payloadBytes, rawKey));
let token = `${signature}${payload}`;
let httpBasicPayload = `${publicAppKeyId}:${token}`;
let httpBasicCredentials = Utilities.base64Encode(httpBasicPayload);
console.log(httpBasicCredentials);
}
Testing:
In the above scripts, when the value of unixTimestamp is 1661403828, both your Node.js and my sample Google Apps Script are the same values as follows.
WFhYWDpGYXBwRk9kaFoxdDVQNGVkV2JJbFIvR1RiMkEyZ0hwYThld05BYjVDVkxnPWFiYy1hZG1pbi12MVhYWFgxNjYxNDAzODI4
Note:
From your question, in this answer, your Node.js is converted to Google Apps Script. Please be careful about this.
References:
base64Encode(data)
computeHmacSha256Signature(value, key)

How to get transaction detail for smart contract, with web3.js

I am using Web3.js with Node JS. I need to get the transaction details for a smart contract under BNB. I developed my codes, but no matter what I did, I couldn't get it to work. Can you help me? Where am I doing wrong?
.env
bnbProvider = https://bsc-dataseed1.binance.org:443
web3crypto.js
const Web3 = require('web3')
const thisWeb3 = new Web3(new Web3.providers.HttpProvider(process.env.bnbProvider))
const minimumABI = [ABI OBJECT]
const tb = new thisWeb3.eth.Contract(minimumABI, process.env.contractId)
my function
exports.checkTransaction = async transactionId => {
const transactionData = await thisWeb3.eth.getTransaction(transactionId)
console.log(transactionData)
return transactionData
}
and Error
Error: Returned error: invalid argument 0: hex string has length 40, want 64 for common.Hash

Getting error "Transaction Oversize" while creating a smart contract in Hedera blockchain

My bin file size is 18kb only. I also get a solution to use IPFS but don't know how to use it. If there is any reference for using IPFS then share me plz. :
Error: PrecheckStatusError: transaction 0.0.34898094#1653658245.135892060 failed precheck with status TRANSACTION_OVERSIZE
Here is my code :
const {
AccountId,
PrivateKey,
Client,
FileCreateTransaction,
ContractCreateTransaction,
ContractFunctionParameters,
ContractExecuteTransaction,
ContractCallQuery,
Hbar
} = require("#hashgraph/sdk");
const fs = require("fs");
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
async function main() {
// Import the compiled contract bytecode
const contractBytecode = fs.readFileSync("first_contract_sol_ABC_TOKEN.bin");
// Create a file on Hedera and store the bytecode
const fileCreateTx = new FileCreateTransaction().setContents(contractBytecode).setKeys([operatorKey]).setMaxTransactionFee(new Hbar(1))
.freezeWith(client);
const fileCreateSign = await fileCreateTx.sign(operatorKey);
console.log(Date.now() / 1000);
const fileCreateSubmit = await fileCreateSign.execute(client);
const fileCreateRx = await fileCreateSubmit.getReceipt(client);
const bytecodeFileId = fileCreateRx.fileId;
console.log(`- The bytecode file ID is: ${bytecodeFileId} \n`);
// Instantiate the smart contract
const contractInstantiateTx = new ContractCreateTransaction()
.setBytecodeFileId(bytecodeFileId)
.setGas(100000)
.setConstructorParameters(
new ContractFunctionParameters().addString("Alice").addUint256(111111)
);
const contractInstantiateSubmit = await contractInstantiateTx.execute(client);
const contractInstantiateRx = await contractInstantiateSubmit.getReceipt(
client
);
const contractId = contractInstantiateRx.contractId;
const contractAddress = contractId.toSolidityAddress();
console.log(`- The smart contract ID is: ${contractId} \n`);
console.log(`- Smart contract ID in Solidity format: ${contractAddress} \n`);
}
main();
You are hitting the TRANSACTION_OVERSIZE error because Hedera transactions have a 6kb size limit including all signatures.
If the compiled bytecode for your contract is relatively large, then you will need to create a file on Hedera, then append the contents in multiple chunks, and then create the contract.
The SDK now handles those 3 steps for you when you use ContractCreateFlow().Here's an example:
const contractCreate = new ContractCreateFlow()
.setGas(100000)
.setBytecode(bytecode);
//Sign the transaction with the client operator key and submit to a Hedera network
const txResponse = contractCreate.execute(client);
//Get the receipt of the transaction
const receipt = (await txResponse).getReceipt(client);

Ethereum sending with web3.js is not working today (the result is fail)

This is my web3.js function to send the ETH.
It works perfectly last month. But today It is not working well.
It took more than 1~5 min and then return the fail.
Some times it sent but It also takes very long time to complete the transaction.
Please help me with this problem.
This is my current codes.
var web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/GfgWbe8c2O82N18RRSuJ'));
// Who holds the token now?
var myAddress = address;
// This file is just JSON stolen from the contract page on etherscan.io under "Contract ABI"
return await web3.eth.getBalance(myAddress);
}
const sendETHCoin = async (from_addr, to_addr, amount, private_key, fee) => {
var content = fs.readFileSync(base_path + 'abiDefinitions/ethAbiContract.json');
content = JSON.parse(content);
///////////////////////////////////
// connect to Infura node
var web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/GfgWbe8c2O82N18RRSuJ'));
// the address that will send the test transaction
const addressFrom = from_addr;
const privKey = private_key;
// the destination address
const addressTo = to_addr;
var gasPrice = "0x02540BE400";
var gasLimit = "0x250CA";
if(fee == ''){
fee = parseInt(gasPrice, 16) * parseInt(gasLimit, 16);
}else{
gasPrice = parseInt(parseInt(fee)/parseInt(gasLimit, 16))+1;
if(gasPrice < 1){
gasPrice = 1;
}
gasPrice = "0x"+gasPrice.toString(16);
}
//gasPrice = "0x03540BE400";
var txCount = await web3.eth.getTransactionCount(addressFrom);
const txData = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(25000),
gasPrice: web3.utils.toHex(10e9), // 10 Gwei
to: addressTo,
from: addressFrom,
value: web3.utils.toHex(web3.utils.toWei(amount, 'wei'))
}
// Signs the given transaction data and sends it. Abstracts some of the details
// of buffering and serializing the transaction for web3.
const privateKey = new Buffer(privKey, 'hex')
const transaction = new Tx(txData)
transaction.sign(privateKey)
const serializedTx = transaction.serialize().toString('hex')
try {
return await web3.eth.sendSignedTransaction('0x' + serializedTx)
}catch(err) {
console.log(err.message);
return err.message;
}
//////////////////////////////
}
I hope someone could help me.
Best Regards.
TianYang
you can check in etherscan why the transaction fails. if the transaction worked last month, probably the problem is in the gas price. last week the gas prices were too high (safe low was 50 gwei) and I see that you are sending with 10gwei, this is most probably the reason why your transactions fail. Try increasing the gas price and see if it works again

Get Document Id's that satisfy the condition in a Query

I want to get document id which satisfies a given query in cloud firestore.
The schema for my database is like this
deyaPayusers/AuthId
/user details
PhoneNo:
/Wallet
/Transaction
I know PhoneNo, then is there a way to get the corresponding AuthId.
I implemented my idea as described below and I'm encountering this error
Argument "documentPath" is not a valid ResourcePath. Path must be a
non-empty string.
const functions = require('firebase-functions');
const Firestore = require('#google-cloud/firestore');
const firestore = new Firestore();
const admin = require('firebase-admin');
exports.transactionDetails = functions.firestore
.document('deyaPayusers/{authid}/Transaction/{authid1}')
.onWrite(event=>{
const db1 = admin.firestore();
const MAuth = event.params.authid
const transid = event.params.authid1
var payeeDocId
var newValue = event.data.data();
var payee = newValue.Payee;//Phone number of money receiver
var amt = newValue.Amount;//Amount willing to pay to payee(money receiver)
var usersRef = db1.collection('deyaPayusers').doc(MAuth);//Refers to payer doc
var payer = usersRef.PhoneNo;//Gets Phonenumber attribute of payer
const walletRefPayer = db1.collection('deyaPayusers').doc(MAuth).collection('Wallet').doc(MAuth);//Wallet reference of Payer
var walletAmt = walletRefPayer.Usd//Accessing the total amount of a person(payer) stored in the wallet
//Getting the payee details assuming, payee has an account in DeyaPay else we need to send the invite to the payee
var payeeInfo = db1.collection('deyaPayusers');//Query to retrieve the payee details from his phone number
let payeeQuery = payeeInfo.where('PhoneNo','==',payee)//We are retrieving it here,before checking the condition because we need to update the transaction details with status either it is failure or success
.get()//To get the doc ID of the Payee based upon the PhoneNo
.then(snapshot => {
snapshot.forEach(doc=>{
payeeDocId = doc.id;
}
});
/*.catch(err =>{
console.log('Error getting documents',err);
});*/
//const docID = payeeQuery.getId();//Gets the Document ID of the payee with the above phone number,in our case it is Authenticated ID.
var payeeWalletRef = db1.collection('deyaPayusers').doc(payeeDocId).collection('Wallet').doc(payeeDocId);
if(walletAmt>=amt){
//Write transactions here,becuase both actions(increment and decrement) must complete together
var payeeTransaction = db1.runTransactions(pT=>{
return pT.get(payeeWalletRef)
.then(doc =>{
var payeeDoc = doc.data(Usd)+amt;//Crediting the amount to be added
return pT.update(payeeWalletRef,{
Usd:payeeDoc});//end of update
})
})//end of payeeTransaction
var payerTransaction = db1.runTransactions(ev=>{
return ev.get(walletRefPayer)
.then(doc=>{
var payerDoc = doc.data(Usd)-amt;//Debitting the amount to be transfered
return ev.update(walletRefPayer,{
Usd:payerDoc});//end of update
});//end of then for payerTransaction
})//end of payerTransaction
}
});//onWrite() end
.document('deyaPayusers/{authid}/Transaction/{authid1}')
You aren't properly interpolating this string.
You need
.document(`deyaPayusers/{authid}/Transaction/{authid1}`)
Also, if you're coming here from google. Any non-string parameter to .document will result in this exception as well.

Resources