Skip to content
Advertisement

Can I generate a transaction on the server and send it to the client for payment

I have built a smart contract method to which I pass some sensitive data that needs to be stored on the blockchain and alter the state of the contract. I, the creator of the contract don’t want to be the one paying for the fees of that transaction. I want the user on the browser to approve and pay for it.

However, I do not want to generate the transaction object on the browser as I want some of the data that will be passed to the contract to be hidden from the client. If I understand the web3 syntax correctly, in the code below, I’m doing just that

web3.eth.sendTransaction({
   from: walletAddressOfTheUserThatWillPayForTheTransaction,
   data: myContract.methods.changeState(..sensitive data...).encodeABI()
})

However I do not want the above to happen on the browser. In my head, the sequence of events should look like this (pseudocode):

// server
let transactionObject = {
   from: walletAddressOfTheUserThatWillPayForTheTransaction,
   data: myContract.methods.changeState(..sensitive data...).encodeABI()
}

sendToClient(encrypt(transactionObject)) 


// client
let encryptedTransactionObject = await fetchEncryptedTransactionObjectFromServer()

// this should open up Metamask for the user so that they may approve and finalise the transaction on the browser
web3.eth.sendTransaction(encryptedTransactionObject)

Is this possible ? Is there some other way of achieving this? Could you provide me with some hints about the actual syntax to be used?

Advertisement

Answer

However, I do not want to generate the transaction object on the browser as I want some of the data that will be passed to the contract to be hidden from the client.

Then you should not be using public blockchains in the first place, as all data on public blockchains, by definition, is public. Anyone can read it.

Advertisement