Skip to content

Commit f479374

Browse files
committed
Refactor test to use abi decoder
1 parent c0d98b5 commit f479374

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

Diff for: index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Web3 = require('web3');
22
const ethUtil = require('ethereumjs-util');
33
const ERC1271 = require('./ABIs/ERC1271');
44

5+
// bytes4(keccak256("isValidSignature(bytes32,bytes)")
56
const ERC1271_MAGIC_VALUE = '0x1626ba7e';
67

78
module.exports = class DappAuth {

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"license": "MIT",
2121
"dependencies": {
2222
"ethereumjs-util": "^6.0.0",
23-
"web3": "^1.0.0-beta.36"
23+
"web3": "^1.0.0-beta.36",
24+
"ethereumjs-abi": "^0.6.6",
25+
"safe-buffer": "^5.1.2"
2426
},
2527
"devDependencies": {
2628
"babel-eslint": "^8.0.1",

Diff for: test/contract-mock.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const ethUtil = require('ethereumjs-util');
2+
const ethAbi = require('ethereumjs-abi');
3+
const Buffer = require('safe-buffer').Buffer;
24
const utils = require('./utils');
35

4-
const erc725CoreInterfaceID = '0xd202158d';
5-
const erc725InterfaceID = '0xdc3d2a7b';
6+
// bytes4(keccak256("isValidSignature(bytes32,bytes)")
7+
const ERC1271_METHOD_SIG = '1626ba7e';
68

79
module.exports = class MockContract {
810
constructor(options) {
@@ -12,43 +14,42 @@ module.exports = class MockContract {
1214
}
1315

1416
static _true() {
15-
return '0x1626ba7e00000000000000000000000000000000000000000000000000000000';
17+
return `0x${ERC1271_METHOD_SIG}00000000000000000000000000000000000000000000000000000000`; // a.k.a the "magic value".
1618
}
1719

1820
static _false(callback) {
1921
return '0x0000000000000000000000000000000000000000000000000000000000000000';
2022
}
2123

24+
// @param {String} methodCall
25+
// @param {String} methodParams
26+
// @return {String}
2227
run(methodCall, methodParams) {
2328
switch (methodCall) {
24-
case '1626ba7e':
25-
const hash = `0x${methodParams.substring(0, 32 * 2)}`;
26-
const signatureLength = parseInt(
27-
methodParams.substring(95 * 2, 96 * 2),
28-
16,
29+
case ERC1271_METHOD_SIG:
30+
const [hash, signature] = ethAbi.rawDecode(
31+
['bytes32', 'bytes'],
32+
Buffer.from(methodParams, 'hex'),
2933
);
30-
const signature = `0x${methodParams.substring(
31-
96 * 2,
32-
(96 + signatureLength) * 2,
33-
)}`;
34+
3435
return this._1626ba7e(hash, signature);
3536
default:
3637
throw new Error(`Unexpected method ${methodCall}`);
3738
}
3839
}
3940

4041
// "isValidSignature" method call
42+
// @param {Buffer} hash
43+
// @param {Buffer} signature
44+
// @return {String}
4145
_1626ba7e(hash, signature) {
4246
if (this.errorIsValidSignature) {
4347
throw new Error('isValidSignature call returned an error');
4448
}
4549

4650
// Get the address of whoever signed this message
4751
const { v, r, s } = ethUtil.fromRpcSig(signature);
48-
const erc191MessageHash = utils.erc191MessageHash(
49-
ethUtil.toBuffer(hash),
50-
this.address,
51-
);
52+
const erc191MessageHash = utils.erc191MessageHash(hash, this.address);
5253
const recoveredKey = ethUtil.ecrecover(erc191MessageHash, v, r, s);
5354
const recoveredAddress = ethUtil.publicToAddress(recoveredKey);
5455

Diff for: yarn.lock

+23-2
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ [email protected]:
434434
version "4.11.6"
435435
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.6.tgz#53344adb14617a13f6e8dd2ce28905d1c0ba3215"
436436

437-
bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.11.0, bn.js@^4.11.3, bn.js@^4.11.6, bn.js@^4.4.0:
437+
bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.10.0, bn.js@^4.11.0, bn.js@^4.11.3, bn.js@^4.11.6, bn.js@^4.4.0:
438438
version "4.11.8"
439439
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
440440

@@ -1296,6 +1296,27 @@ [email protected]:
12961296
elliptic "^6.4.0"
12971297
xhr-request-promise "^0.1.2"
12981298

1299+
ethereumjs-abi@^0.6.6:
1300+
version "0.6.6"
1301+
resolved "https://registry.yarnpkg.com/ethereumjs-abi/-/ethereumjs-abi-0.6.6.tgz#f8ba3413a98478173f5a00f7f1316819db1d09ec"
1302+
integrity sha512-w8KubDsA/+OAuqtIR9RGsMcoZ5nhM8vxwjJAJvEIY+clhxA3BHoLG3+ClYQaQhD0n3mlDt3U5rBrmSVJvI3c8A==
1303+
dependencies:
1304+
bn.js "^4.10.0"
1305+
ethereumjs-util "^5.0.0"
1306+
1307+
ethereumjs-util@^5.0.0:
1308+
version "5.2.0"
1309+
resolved "https://registry.yarnpkg.com/ethereumjs-util/-/ethereumjs-util-5.2.0.tgz#3e0c0d1741471acf1036052d048623dee54ad642"
1310+
integrity sha512-CJAKdI0wgMbQFLlLRtZKGcy/L6pzVRgelIZqRqNbuVFM3K9VEnyfbcvz0ncWMRNCe4kaHWjwRYQcYMucmwsnWA==
1311+
dependencies:
1312+
bn.js "^4.11.0"
1313+
create-hash "^1.1.2"
1314+
ethjs-util "^0.1.3"
1315+
keccak "^1.0.2"
1316+
rlp "^2.0.0"
1317+
safe-buffer "^5.1.1"
1318+
secp256k1 "^3.0.1"
1319+
12991320
ethereumjs-util@^6.0.0:
13001321
version "6.0.0"
13011322
resolved "https://registry.yarnpkg.com/ethereumjs-util/-/ethereumjs-util-6.0.0.tgz#f14841c182b918615afefd744207c7932c8536c0"
@@ -1330,7 +1351,7 @@ [email protected]:
13301351
bn.js "4.11.6"
13311352
number-to-bn "1.7.0"
13321353

1333-
ethjs-util@^0.1.6:
1354+
ethjs-util@^0.1.3, ethjs-util@^0.1.6:
13341355
version "0.1.6"
13351356
resolved "https://registry.yarnpkg.com/ethjs-util/-/ethjs-util-0.1.6.tgz#f308b62f185f9fe6237132fb2a9818866a5cd536"
13361357
dependencies:

0 commit comments

Comments
 (0)