GSNBouncerSignature.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const { expectEvent } = require('openzeppelin-test-helpers');
  2. const gsn = require('@openzeppelin/gsn-helpers');
  3. const { fixSignature } = require('../helpers/sign');
  4. const { utils: { toBN } } = require('web3');
  5. const GSNBouncerSignatureMock = artifacts.require('GSNBouncerSignatureMock');
  6. contract('GSNBouncerSignature', function ([_, signer, other]) {
  7. beforeEach(async function () {
  8. this.recipient = await GSNBouncerSignatureMock.new(signer);
  9. });
  10. context('when called directly', function () {
  11. it('mock function can be called', async function () {
  12. const { logs } = await this.recipient.mockFunction();
  13. expectEvent.inLogs(logs, 'MockFunctionCalled');
  14. });
  15. });
  16. context('when relay-called', function () {
  17. beforeEach(async function () {
  18. await gsn.fundRecipient(web3, { recipient: this.recipient.address });
  19. });
  20. it('rejects unsigned relay requests', async function () {
  21. await gsn.expectError(this.recipient.mockFunction({ value: 0, useGSN: true }));
  22. });
  23. it('rejects relay requests where some parameters are signed', async function () {
  24. const approveFunction = async (data) =>
  25. fixSignature(
  26. await web3.eth.sign(
  27. web3.utils.soliditySha3(
  28. // the nonce is not signed
  29. data.relayerAddress, data.from, data.encodedFunctionCall, data.txFee, data.gasPrice, data.gas
  30. ), signer
  31. )
  32. );
  33. await gsn.expectError(this.recipient.mockFunction({ value: 0, useGSN: true, approveFunction }));
  34. });
  35. it('accepts relay requests where all parameters are signed', async function () {
  36. const approveFunction = async (data) =>
  37. fixSignature(
  38. await web3.eth.sign(
  39. web3.utils.soliditySha3(
  40. // eslint-disable-next-line max-len
  41. data.relayerAddress, data.from, data.encodedFunctionCall, toBN(data.txFee), toBN(data.gasPrice), toBN(data.gas), toBN(data.nonce), data.relayHubAddress, data.to
  42. ), signer
  43. )
  44. );
  45. const { tx } = await this.recipient.mockFunction({ value: 0, useGSN: true, approveFunction });
  46. await expectEvent.inTransaction(tx, GSNBouncerSignatureMock, 'MockFunctionCalled');
  47. });
  48. it('rejects relay requests where all parameters are signed by an invalid signer', async function () {
  49. const approveFunction = async (data) =>
  50. fixSignature(
  51. await web3.eth.sign(
  52. web3.utils.soliditySha3(
  53. // eslint-disable-next-line max-len
  54. data.relay_address, data.from, data.encodedFunctionCall, data.txfee, data.gasPrice, data.gas, data.nonce, data.relayHubAddress, data.to
  55. ), other
  56. )
  57. );
  58. await gsn.expectError(this.recipient.mockFunction({ value: 0, useGSN: true, approveFunction }));
  59. });
  60. });
  61. });