Sending raw transaction and signature to the network

Hello @singhparshant, how are you?

I am unsure If I understand what you are trying to achieve.

But I assume that is to get the hash of a ScriptTransaction and use it to generate an ECDSA signature, then add this signature to the ScriptTransaction before submitting it.

If so this can be achieved by the following approach:

import { splitSignature } from '@ethersproject/bytes';
import { hexToBytes } from '@ethereumjs/util';
import Web3 from 'web3';


const provider = await Provider.create(FUEL_NETWORK_URL);
const chainId = provider.getChainId();

// Transaction Hash (ID)
const hashedTransaction = scriptTransactionRequest.getTransactionId(chainId);

// If you are using web3
const web3 = new Web3('https://provider.url.here');
const privateKey = '0xYOUR_PRIVATE_KEY';
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(account);
const signature = await web3.eth.personal.sign(hashedTransaction, account.address, account.privateKey)

// Transform the signature into compact form for Sway to understand, in case it will be used within a Sway program
const compactSignature = splitSignature(hexToBytes(signature)).compact;

// Add signature to the transaction witnesses
scriptTransactionRequest.witnesses.push(compactSignature);

const tx = await fuelWallet.sendTransaction(scriptTransactionRequest)
const response = await tx.waitForResult()

Please let me know if I misunderstood your request.

2 Likes