0

I have a contract where users can buy "credits".

With that credit, users can post a message to a server (a route set up in ExpressJS).

I have the buying of credits working.

But my question is how can the code running on the server know the user actually has a credit?

I'm guessing that the user..

  1. signs a tx
  2. POSTs tx to a route on ExpressJS.
  3. That route calls a provider (like Infura) to check if the user has a credit.
  4. If they have credit, change contract state (remove one credit)
  5. Depending on the result, ExpressJS code allows the user to make a post or not.

Question 1 The piece that is most confusing is how the user (client-side) will sign the transaction, and NOT send it to the provider — but post the tx to Express JS/server. How is this accomplished?

Question 2 I would like to pay the gas for the user. How would I take in a client-side signed tx (necessary to prove the user is who they really are — to check their credits). But at the same time, pay the gas.

Would this mean I would need a 'signer' (with a private key available) in the server environment? How would this work?

Is this the correct way to go about it? Any pointers, articles, etc.. on this topic would be greatly appreciated!

2

Question 1:

For your first question, it is possible to sign the transaction at client-side and send it to the provider from your server. You'll have to call the web3.js method sendSignedTransaction to send the transaction to the blockchain.

Client-Side

var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')

var rawTx = {
  nonce: '0x00',
  gasPrice: '0x09184e72a000',
  gasLimit: '0x2710',
  to: '0x0000000000000000000000000000000000000000',
  value: '0x00',
  data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

Send this serializedTx to the server.

Server Side

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);

Please refer the documentation of web3.js: https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#sendsignedtransaction

Question 2: What you're trying to do is basically a gasless transaction. You can look into Openzepplin's gas station network. I think it'll solve your problem. Essentially it sends transactions to the network without the user having to pay the fees.

Please read through it. I am sure it'll answer your question: https://docs.openzeppelin.com/learn/sending-gasless-transactions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.