GSNRecipientSignature.test.js 3.5 KB

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