GSNBouncerSignature.test.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // eslint-disable-next-line max-len
  30. data.relayerAddress, data.from, data.encodedFunctionCall, toBN(data.txFee), toBN(data.gasPrice), toBN(data.gas)
  31. ), signer
  32. )
  33. );
  34. await gsn.expectError(this.recipient.mockFunction({ value: 0, useGSN: true, approveFunction }));
  35. });
  36. it('accepts relay requests where all parameters are signed', async function () {
  37. const approveFunction = async (data) =>
  38. fixSignature(
  39. await web3.eth.sign(
  40. web3.utils.soliditySha3(
  41. // eslint-disable-next-line max-len
  42. data.relayerAddress, data.from, data.encodedFunctionCall, toBN(data.txFee), toBN(data.gasPrice), toBN(data.gas), toBN(data.nonce), data.relayHubAddress, data.to
  43. ), signer
  44. )
  45. );
  46. const { tx } = await this.recipient.mockFunction({ value: 0, useGSN: true, approveFunction });
  47. await expectEvent.inTransaction(tx, GSNBouncerSignatureMock, 'MockFunctionCalled');
  48. });
  49. it('rejects relay requests where all parameters are signed by an invalid signer', async function () {
  50. const approveFunction = async (data) =>
  51. fixSignature(
  52. await web3.eth.sign(
  53. web3.utils.soliditySha3(
  54. // eslint-disable-next-line max-len
  55. data.relayerAddress, data.from, data.encodedFunctionCall, toBN(data.txFee), toBN(data.gasPrice), toBN(data.gas), toBN(data.nonce), data.relayHubAddress, data.to
  56. ), other
  57. )
  58. );
  59. await gsn.expectError(this.recipient.mockFunction({ value: 0, useGSN: true, approveFunction }));
  60. });
  61. });
  62. });