SignatureBouncer.test.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. const { assertRevert } = require('../helpers/assertRevert');
  2. const { signHex } = require('../helpers/sign');
  3. const Bouncer = artifacts.require('SignatureBouncerMock');
  4. require('chai')
  5. .use(require('chai-as-promised'))
  6. .should();
  7. const getSigner = (contract, signer, data = '') => (addr) => {
  8. // via: https://github.com/OpenZeppelin/zeppelin-solidity/pull/812/files
  9. const message = contract.address.substr(2) + addr.substr(2) + data;
  10. // ^ substr to remove `0x` because in solidity the address is a set of byes, not a string `0xabcd`
  11. return signHex(signer, message);
  12. };
  13. const getMethodId = (methodName, ...paramTypes) => {
  14. // methodId is a sha3 of the first 4 bytes after 0x of 'method(paramType1,...)'
  15. return web3.sha3(`${methodName}(${paramTypes.join(',')})`).substr(2, 8);
  16. };
  17. const stripAndPadHexValue = (hexVal, sizeInBytes, start = true) => {
  18. // strip 0x from the font and pad with 0's for
  19. const strippedHexVal = hexVal.substr(2);
  20. return start ? strippedHexVal.padStart(sizeInBytes * 2, 0) : strippedHexVal.padEnd(sizeInBytes * 2, 0);
  21. };
  22. contract('Bouncer', ([_, owner, authorizedUser, anyone, bouncerAddress, newBouncer]) => {
  23. before(async function () {
  24. this.bouncer = await Bouncer.new({ from: owner });
  25. this.roleBouncer = await this.bouncer.ROLE_BOUNCER();
  26. this.genSig = getSigner(this.bouncer, bouncerAddress);
  27. this.uintValue = 23;
  28. this.checkValidSignatureAndMethodId = getMethodId('checkValidSignatureAndMethod', 'address', 'bytes');
  29. this.uintValueData = stripAndPadHexValue(web3.toHex(this.uintValue), 32);
  30. this.authorizedUserData = stripAndPadHexValue(authorizedUser, 32);
  31. this.bytesValue = web3.toHex('bytesValue');
  32. this.validateSignatureAndDataMsgData = [
  33. getMethodId('checkValidSignatureAndData', 'address', 'bytes', 'uint256', 'bytes'),
  34. stripAndPadHexValue(authorizedUser, 32),
  35. stripAndPadHexValue(web3.toHex(32 * 4), 32), // bytesValue location
  36. this.uintValueData,
  37. stripAndPadHexValue(web3.toHex(32 * 6), 32), // sig location
  38. stripAndPadHexValue(web3.toHex(this.bytesValue.substr(2).length / 2), 32), // bytesValue size
  39. stripAndPadHexValue(this.bytesValue, 32, false), // bytesValue
  40. ];
  41. });
  42. it('should have a default owner of self', async function () {
  43. const theOwner = await this.bouncer.owner();
  44. theOwner.should.eq(owner);
  45. });
  46. it('should allow owner to add a bouncer', async function () {
  47. await this.bouncer.addBouncer(bouncerAddress, { from: owner });
  48. const hasRole = await this.bouncer.hasRole(bouncerAddress, this.roleBouncer);
  49. hasRole.should.eq(true);
  50. });
  51. it('should not allow anyone to add a bouncer', async function () {
  52. await assertRevert(
  53. this.bouncer.addBouncer(bouncerAddress, { from: anyone })
  54. );
  55. });
  56. context('modifiers', () => {
  57. it('should allow valid signature for sender', async function () {
  58. await this.bouncer.onlyWithValidSignature(
  59. this.genSig(authorizedUser),
  60. { from: authorizedUser }
  61. );
  62. });
  63. it('should not allow invalid signature for sender', async function () {
  64. await assertRevert(
  65. this.bouncer.onlyWithValidSignature(
  66. 'abcd',
  67. { from: authorizedUser }
  68. )
  69. );
  70. });
  71. it('should allow valid signature with a valid method for sender', async function () {
  72. const sig = getSigner(
  73. this.bouncer,
  74. bouncerAddress,
  75. getMethodId('onlyWithValidSignatureAndMethod', 'bytes')
  76. )(authorizedUser);
  77. await this.bouncer.onlyWithValidSignatureAndMethod(
  78. sig,
  79. { from: authorizedUser }
  80. );
  81. });
  82. it('should not allow invalid signature with method for sender', async function () {
  83. await assertRevert(
  84. this.bouncer.onlyWithValidSignatureAndMethod(
  85. 'abcd',
  86. { from: authorizedUser }
  87. )
  88. );
  89. });
  90. it('should allow valid signature with a valid data for sender', async function () {
  91. const sig = getSigner(
  92. this.bouncer,
  93. bouncerAddress,
  94. [
  95. getMethodId('onlyWithValidSignatureAndData', 'uint256', 'bytes'),
  96. this.uintValueData,
  97. stripAndPadHexValue(web3.toHex(64), 32),
  98. ].join('')
  99. )(authorizedUser);
  100. await this.bouncer.onlyWithValidSignatureAndData(
  101. this.uintValue,
  102. sig,
  103. { from: authorizedUser }
  104. );
  105. });
  106. it('should not allow invalid signature with data for sender', async function () {
  107. await assertRevert(
  108. this.bouncer.onlyWithValidSignatureAndData(
  109. this.uintValue,
  110. 'abcd',
  111. { from: authorizedUser }
  112. )
  113. );
  114. });
  115. });
  116. context('signatures', () => {
  117. it('should accept valid message for valid user', async function () {
  118. const isValid = await this.bouncer.checkValidSignature(
  119. authorizedUser,
  120. this.genSig(authorizedUser)
  121. );
  122. isValid.should.eq(true);
  123. });
  124. it('should not accept invalid message for valid user', async function () {
  125. const isValid = await this.bouncer.checkValidSignature(
  126. authorizedUser,
  127. this.genSig(anyone)
  128. );
  129. isValid.should.eq(false);
  130. });
  131. it('should not accept invalid message for invalid user', async function () {
  132. const isValid = await this.bouncer.checkValidSignature(
  133. anyone,
  134. 'abcd'
  135. );
  136. isValid.should.eq(false);
  137. });
  138. it('should not accept valid message for invalid user', async function () {
  139. const isValid = await this.bouncer.checkValidSignature(
  140. anyone,
  141. this.genSig(authorizedUser)
  142. );
  143. isValid.should.eq(false);
  144. });
  145. it('should accept valid message with valid method for valid user', async function () {
  146. const sig = getSigner(
  147. this.bouncer,
  148. bouncerAddress,
  149. getMethodId('checkValidSignatureAndMethod', 'address', 'bytes')
  150. )(authorizedUser);
  151. const isValid = await this.bouncer.checkValidSignatureAndMethod(
  152. authorizedUser,
  153. sig
  154. );
  155. isValid.should.eq(true);
  156. });
  157. it('should not accept valid message with an invalid method for valid user', async function () {
  158. const sig = getSigner(
  159. this.bouncer,
  160. bouncerAddress,
  161. getMethodId('invalidMethod', 'address', 'bytes')
  162. )(authorizedUser);
  163. const isValid = await this.bouncer.checkValidSignatureAndMethod(
  164. authorizedUser,
  165. sig
  166. );
  167. isValid.should.eq(false);
  168. });
  169. it('should not accept valid message with a valid method for an invalid user', async function () {
  170. const sig = getSigner(
  171. this.bouncer,
  172. bouncerAddress,
  173. this.checkValidSignatureAndMethodId
  174. )(authorizedUser);
  175. const isValid = await this.bouncer.checkValidSignatureAndMethod(
  176. anyone,
  177. sig
  178. );
  179. isValid.should.eq(false);
  180. });
  181. it('should accept valid method with valid params for valid user', async function () {
  182. const sig = getSigner(
  183. this.bouncer,
  184. bouncerAddress,
  185. this.validateSignatureAndDataMsgData.join('')
  186. )(authorizedUser);
  187. const isValid = await this.bouncer.checkValidSignatureAndData(
  188. authorizedUser,
  189. this.bytesValue,
  190. this.uintValue,
  191. sig
  192. );
  193. isValid.should.eq(true);
  194. });
  195. it('should not accept an invalid method with valid params for valid user', async function () {
  196. this.validateSignatureAndDataMsgData[0] = getMethodId('invalidMethod', 'address', 'bytes', 'uint256', 'bytes');
  197. const sig = getSigner(
  198. this.bouncer,
  199. bouncerAddress,
  200. this.validateSignatureAndDataMsgData.join('')
  201. )(authorizedUser);
  202. const isValid = await this.bouncer.checkValidSignatureAndData(
  203. authorizedUser,
  204. this.bytesValue,
  205. this.uintValue,
  206. sig
  207. );
  208. isValid.should.eq(false);
  209. });
  210. it('should not accept valid method with invalid params for valid user', async function () {
  211. const sig = getSigner(
  212. this.bouncer,
  213. bouncerAddress,
  214. this.validateSignatureAndDataMsgData.join('')
  215. )(authorizedUser);
  216. const isValid = await this.bouncer.checkValidSignatureAndData(
  217. authorizedUser,
  218. this.bytesValue,
  219. 500,
  220. sig
  221. );
  222. isValid.should.eq(false);
  223. });
  224. it('should not accept valid method with valid params for invalid user', async function () {
  225. const sig = getSigner(
  226. this.bouncer,
  227. bouncerAddress,
  228. this.validateSignatureAndDataMsgData.join('')
  229. )(authorizedUser);
  230. const isValid = await this.bouncer.checkValidSignatureAndData(
  231. anyone,
  232. this.bytesValue,
  233. this.uintValue,
  234. sig
  235. );
  236. isValid.should.eq(false);
  237. });
  238. });
  239. context('management', () => {
  240. it('should not allow anyone to add bouncers', async function () {
  241. await assertRevert(
  242. this.bouncer.addBouncer(newBouncer, { from: anyone })
  243. );
  244. });
  245. it('should be able to add bouncers', async function () {
  246. await this.bouncer.addBouncer(newBouncer, { from: owner })
  247. .should.be.fulfilled;
  248. });
  249. it('should not allow adding invalid address', async function () {
  250. await assertRevert(
  251. this.bouncer.addBouncer('0x0', { from: owner })
  252. );
  253. });
  254. it('should not allow anyone to remove bouncer', async function () {
  255. await assertRevert(
  256. this.bouncer.removeBouncer(newBouncer, { from: anyone })
  257. );
  258. });
  259. it('should be able to remove bouncers', async function () {
  260. await this.bouncer.removeBouncer(newBouncer, { from: owner })
  261. .should.be.fulfilled;
  262. });
  263. });
  264. });