GSNRecipientSignature.test.js 3.4 KB

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