GSNBouncerSignature.test.js 3.3 KB

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