Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dappauth test error for eslint #359

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/dappauth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"control",
"eth_sign"
],
"type": "module",
"repository": "github:blocto/dappauth.js",
"main": "./dist/dappauth.js",
"module": "./dist/dappauth.module.js",
Expand All @@ -18,30 +19,34 @@
},
"scripts": {
"build": "rollup -c",
"test": "npm run eslint && nyc mocha test/*",
"test": "npm run lint && nyc mocha test/*",
mordochi marked this conversation as resolved.
Show resolved Hide resolved
"prettier:watch": "onchange '**/*.js' -- prettier --write {{changed}}",
"eslint": "eslint ./ --ext .js",
"lint": "eslint --ext .js src/",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"author": "Pazams <[email protected]>",
"license": "MIT",
"dependencies": {
"ethereumjs-abi": "^0.6.6",
"ethereumjs-util": "^6.0.0",
"ethereumjs-util": "^7.1.5",
mordochi marked this conversation as resolved.
Show resolved Hide resolved
"safe-buffer": "^5.1.2",
"web3": "^1.3.1"
},
"devDependencies": {
"@babel/core": "^7.23.6",
"@babel/preset-env": "^7.23.6",
"@babel/register": "^7.22.15",
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-node-resolve": "^15.2.1",
"babel-eslint": "^8.0.1",
"babel-plugin-istanbul": "^6.1.1",
"coveralls": "^3.0.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.0",
"mocha": "^10.2.0",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^14.1.1",
"nyc": "^15.1.0",
"onchange": "^7.1.0",
"prettier": "^1.14.3",
"rollup": "^3.29.1",
Expand Down
48 changes: 21 additions & 27 deletions packages/dappauth/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const commonjs = require('@rollup/plugin-commonjs');
const resolve = require('@rollup/plugin-node-resolve');
const json = require('rollup-plugin-json');
const polyfills = require('rollup-plugin-polyfill-node');
const copy = require('rollup-plugin-copy');
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import json from 'rollup-plugin-json';
import polyfills from 'rollup-plugin-polyfill-node';
import copy from 'rollup-plugin-copy';


module.exports = [
export default [
// CommonJS
{
input: 'index.js',
input: 'src/index.js',
output: {
file: 'dist/dappauth.js',
format: 'cjs',
Expand All @@ -18,16 +18,14 @@ module.exports = [
plugins: [
copy({
targets: [
{ src: './ABIs', dest: 'dist' },
{ src: './utils.js', dest: 'dist' },
{ src: './src/ABIs', dest: 'dist' },
{ src: './src/utils', dest: 'dist' },
],
}),
commonjs(),
resolve({
preferBuiltins: true,
browser: true,
}),
commonjs({
include: 'node_modules/**',
preferBuiltins: false,
}),
mordochi marked this conversation as resolved.
Show resolved Hide resolved
json(),
polyfills(),
Expand All @@ -38,7 +36,7 @@ module.exports = [
},
// es
{
input: 'index.js',
input: 'src/index.js',
output: {
file: 'dist/dappauth.module.js',
format: 'es',
Expand All @@ -47,24 +45,22 @@ module.exports = [
plugins: [
copy({
targets: [
{ src: './ABIs', dest: 'dist' },
{ src: './utils.js', dest: 'dist' },
{ src: './src/ABIs', dest: 'dist' },
{ src: './src/utils', dest: 'dist' },
],
}),
commonjs(),
resolve({
preferBuiltins: true,
browser: true,
}),
commonjs({
include: 'node_modules/**',
preferBuiltins: false,
}),
json(),
polyfills(),
],
},
// umd
{
input: 'index.js',
input: 'src/index.js',
output: {
file: 'dist/dappauth.umd.js',
format: 'umd',
Expand All @@ -73,17 +69,15 @@ module.exports = [
plugins: [
copy({
targets: [
{ src: './ABIs', dest: 'dist' },
{ src: './utils.js', dest: 'dist' },
{ src: './src/ABIs', dest: 'dist' },
{ src: './src/utils', dest: 'dist' },
],
}),
commonjs(),
resolve({
preferBuiltins: true,
preferBuiltins: false,
browser: true,
}),
commonjs({
include: 'node_modules/**',
}),
json(),
polyfills(),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-disable */

module.exports = [
export default [
{
constant: true,
inputs: [
Expand Down
30 changes: 15 additions & 15 deletions packages/dappauth/index.js → packages/dappauth/src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Web3 = require('web3');
const ethUtil = require('ethereumjs-util');
const ERC1271 = require('./ABIs/ERC1271.js');
const utils = require('./utils.js');
import Web3 from 'web3';
import { fromRpcSig, ecrecover, publicToAddress, bufferToHex, hashPersonalMessage, keccak } from 'ethereumjs-util';
import ERC1271 from './ABIs/ERC1271.js';
import { mergeErrors, isHexString, removeHexPrefix } from './utils/index.js';

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

module.exports = class DappAuth {
export default class DappAuth {
constructor(web3Provider) {
this.web3 = new Web3(web3Provider);
}
Expand All @@ -19,13 +19,13 @@ module.exports = class DappAuth {
// try direct-keyed wallet
try {
// Get the address of whoever signed this message
const { v, r, s } = ethUtil.fromRpcSig(signature);
const recoveredKey = ethUtil.ecrecover(eoaChallengeHash, v, r, s);
const recoveredAddress = ethUtil.publicToAddress(recoveredKey);
const { v, r, s } = fromRpcSig(signature);
const recoveredKey = ecrecover(eoaChallengeHash, v, r, s);
const recoveredAddress = publicToAddress(recoveredKey);

if (
address.toLowerCase() ===
ethUtil.bufferToHex(recoveredAddress).toLowerCase()
bufferToHex(recoveredAddress).toLowerCase()
) {
isAuthorizedDirectKey = true;
}
Expand Down Expand Up @@ -53,23 +53,23 @@ module.exports = class DappAuth {
return magicValue2 === ERC1271_MAGIC_VALUE;
}
} catch (err) {
throw utils.mergeErrors(errEOA, err);
throw mergeErrors(errEOA, err);
}
}

_hashEOAPersonalMessage(challenge) {
return ethUtil.hashPersonalMessage(this._decodeChallenge(challenge));
return hashPersonalMessage(this._decodeChallenge(challenge));
}

// This is a hash just over the challenge. The smart contract takes this result and hashes on top to an erc191 hash.
_hashSCMessage(challenge) {
return ethUtil.keccak(this._decodeChallenge(challenge));
return keccak(this._decodeChallenge(challenge));
}

// See https://github.com/MetaMask/eth-sig-util/issues/60
_decodeChallenge(challenge) {
return utils.isHexString(challenge)
? Buffer.from(utils.removeHexPrefix(challenge), 'hex')
return isHexString(challenge)
? Buffer.from(removeHexPrefix(challenge), 'hex')
: Buffer.from(challenge);
}
};
}
19 changes: 19 additions & 0 deletions packages/dappauth/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function isHexString(value) {
return (
typeof value === 'string' &&
value.length % 2 === 0 &&
value.match(/^0x[0-9A-Fa-f]*$/)
);
}

export function removeHexPrefix(value) {
return value.slice(0, 2) === '0x' ? value.slice(2) : value;
}

export function mergeErrors(errEOA, errCA) {
const msgEOA = errEOA
? `errored with: ${errEOA.toString()}`
: 'returned false';
return `Authorisation check failed and errored in 2 alternative flows. 'External Owned Account' check ${msgEOA} . 'Contract Account' check errored with: ${errCA.toString()}`;
}

35 changes: 18 additions & 17 deletions packages/dappauth/test/contract-mock.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const ethUtil = require('ethereumjs-util');
const ethAbi = require('ethereumjs-abi');
const Buffer = require('safe-buffer').Buffer;
const testUtils = require('./test-utils.js');
import { fromRpcSig, ecrecover, publicToAddress } from 'ethereumjs-util';
import abi from 'ethereumjs-abi';
import { Buffer } from 'safe-buffer';
import { generateErc191MessageHash } from './test-utils.js';

const { rawDecode } = abi;
// bytes4(keccak256("isValidSignature(bytes32,bytes)")
const ERC1271_METHOD_SIG = '1626ba7e';

module.exports = class MockContract {
export default class MockContract {
constructor(options) {
this.authorizedKey = options.authorizedKey;
this.address = options.address;
Expand All @@ -17,7 +18,7 @@ module.exports = class MockContract {
return `0x${ERC1271_METHOD_SIG}00000000000000000000000000000000000000000000000000000000`; // a.k.a the "magic value".
}

static _false(callback) {
static _false() {
return '0x0000000000000000000000000000000000000000000000000000000000000000';
}

Expand All @@ -26,13 +27,13 @@ module.exports = class MockContract {
// @return {String}
run(methodCall, methodParams) {
switch (methodCall) {
case ERC1271_METHOD_SIG:
const [hash, signature] = ethAbi.rawDecode(
case ERC1271_METHOD_SIG: {
const [hash, signature] = rawDecode(
['bytes32', 'bytes'],
Buffer.from(methodParams, 'hex'),
);

return this._1626ba7e(hash, signature);
);
return this._1626ba7e(hash, signature);
}
default:
throw new Error(`Unexpected method ${methodCall}`);
}
Expand All @@ -53,17 +54,17 @@ module.exports = class MockContract {
const expected_authrorised_sig = multi_sigs[0];

// Get the address of whoever signed this message
const { v, r, s } = ethUtil.fromRpcSig(`0x${expected_authrorised_sig}`);
const erc191MessageHash = testUtils.erc191MessageHash(hash, this.address);
const recoveredKey = ethUtil.ecrecover(erc191MessageHash, v, r, s);
const recoveredAddress = ethUtil.publicToAddress(recoveredKey);
const { v, r, s } = fromRpcSig(`0x${expected_authrorised_sig}`);
const erc191MessageHash = generateErc191MessageHash(hash, this.address);
const recoveredKey = ecrecover(erc191MessageHash, v, r, s);
const recoveredAddress = publicToAddress(recoveredKey);

const expectedAddress = ethUtil.publicToAddress(this.authorizedKey);
const expectedAddress = publicToAddress(this.authorizedKey);

if (recoveredAddress.toString() === expectedAddress.toString()) {
return MockContract._true();
}

return MockContract._false();
}
};
}
4 changes: 2 additions & 2 deletions packages/dappauth/test/provider-mock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = class MockProvider {
export default class MockProvider {
constructor(mockContract) {
this.mockContract = mockContract;
}
Expand All @@ -24,4 +24,4 @@ module.exports = class MockProvider {
send(payload, callback) {
this.sendAsync(payload, callback);
}
};
}
Loading
Loading