ava-labs / avalanchejs

The Avalanche Platform JavaScript Library
Other
321 stars 164 forks source link

facing issue of invalid signature when sending delegate transaction. (Error: couldn't issue tx: flow check failed: failed to verify transfer: invalid signature) #869

Open Aayush3Intervision opened 3 months ago

Aayush3Intervision commented 3 months ago

I am trying to use script of delegate transaction from example of p-chain while doing this I am getting Error: couldn't issue tx: flow check failed: failed to verify transfer: invalid signature Here is my .env file

AVAX_PUBLIC_URL="https://api.avax-test.network/" PRIVATE_KEY=0x........ C_CHAIN_ADDRESS=C-fuji12xejcj3g9748wr8x7qrlqqntemnznx6xam44dt X_CHAIN_ADDRESS=X-fuji12xejcj3g9748wr8x7qrlqqntemnznx6xam44dt P_CHAIN_ADDRESS=P-fuji12xejcj3g9748wr8x7qrlqqntemnznx6xam44dt CORETH_ADDRESS=0xAa95D3C6542eB8FFf583ef0368a6f522e0c157c5 NODE_ID=NodeID-Drq7Ntpsewpdd6RtegNPtH98RZz7ioyYX

Here is the code for that ` const { avm, pvm, evm, Context, utils, networkIDs, addTxSignatures, secp256k1 } = require('@avalabs/avalanchejs'); const dotenv = require('dotenv'); const path = require('path'); // const winston = require('winston'); // Logging library

dotenv.config({ path: path.resolve(__dirname, '../.env') });

// const logger = winston.createLogger({ // level: 'info', // format: winston.format.combine( // winston.format.colorize(), // winston.format.simple() // ), // transports: [ // new winston.transports.Console() // ] // });

const P_CHAIN_ADDRESS = process.env.P_CHAIN_ADDRESS; const PRIVATE_KEY = process.env.PRIVATE_KEY;

const NODE_ID = process.env.NODE_ID; const pvmapi = new pvm.PVMApi(process.env.AVAX_PUBLIC_URL);

const main = async () => { // try { if (!P_CHAIN_ADDRESS || !PRIVATE_KEY) { throw new Error('Missing environment variable(s).'); }

const { utxos } = await pvmapi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] });

console.log(utxos); // // Fetch node's staking period // const nodeInfo = await pvmapi.getCurrentValidators({ // nodeID: NODE_ID // });

const context = await Context.getContextFromURI(process.env.AVAX_PUBLIC_URL); const startTime = await pvmapi.getTimestamp(); const startDate = new Date(startTime.timestamp); const start = BigInt(startDate.getTime() / 1000); const endTime = new Date(startTime.timestamp); endTime.setDate(endTime.getDate() + 7); const end = BigInt(endTime.getTime() / 1000); const nodeID = 'NodeID-2ebkCRrcWaBW9EUd94ZXzDJ9cdwbVKMDh';

const tx = pvm.newAddPermissionlessDelegatorTx( context, utxos, [utils.bech32ToBytes(P_CHAIN_ADDRESS)], nodeID, networkIDs.PrimaryNetworkID.toString(), start, end, BigInt(1e9), [utils.bech32ToBytes(P_CHAIN_ADDRESS)], );

await addTxSignatures({ unsignedTx: tx, privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], });

return pvmapi.issueSignedTx(tx.getSignedTx()); };

main()//.catch(error => logger.error(Unhandled Error: ${error.message})); `