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

Add message decoding tests #17

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ module.exports = class DappAuth {
}

async isAuthorizedSigner(challenge, signature, address) {
const challengeHash = ethUtil.hashPersonalMessage(
ethUtil.toBuffer(challenge),
);

const eoaChallengeHash = this._hashEOAPersonalMessage(challenge);
let isAuthorizedDirectKey;
let errEOA;

// try direct-keyed wallet
try {
// Get the address of whoever signed this message
const { v, r, s } = ethUtil.fromRpcSig(signature);
const recoveredKey = ethUtil.ecrecover(challengeHash, v, r, s);
const recoveredKey = ethUtil.ecrecover(eoaChallengeHash, v, r, s);
const recoveredAddress = ethUtil.publicToAddress(recoveredKey);

if (
Expand All @@ -49,6 +46,10 @@ module.exports = class DappAuth {
throw mergeErrors(errEOA, err);
}
}

_hashEOAPersonalMessage(challenge) {
return ethUtil.hashPersonalMessage(ethUtil.toBuffer(challenge));
}
};

function mergeErrors(errEOA, errCA) {
Expand Down
39 changes: 39 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,45 @@ describe('dappauth', function() {
}),
);

it('It should decode challenge as utf8 by default when computing EOA personal messages hash', async function() {
const dappAuth = new DappAuth(
new ProviderMock(
new ContractMock({
authorizedKey: null,
address: null,
errorIsValidSignature: false,
}),
),
);

const eoaHash = dappAuth._hashEOAPersonalMessage('foo');
assert.equal(
`0x${eoaHash.toString('hex')}`,
'0x76b2e96714d3b5e6eb1d1c509265430b907b44f72b2a22b06fcd4d96372b8565',
);
});

// See https://github.com/MetaMask/eth-sig-util/issues/60
it('It should decode challenge as hex if hex is detected when computing EOA personal messages hash', async function() {
const dappAuth = new DappAuth(
new ProviderMock(
new ContractMock({
authorizedKey: null,
address: null,
errorIsValidSignature: false,
}),
),
);

// result if 0xffff is decoded as hex: 13a6aa3102b2d639f36804a2d7c31469618fd7a7907c658a7b2aa91a06e31e47
// result if 0xffff is decoded as utf8: 247aefb5d2e5b17fca61f786c779f7388485460c13e51308f88b2ff84ffa6851
const eoaHash = dappAuth._hashEOAPersonalMessage('0xffff');
assert.equal(
`0x${eoaHash.toString('hex')}`,
'0x13a6aa3102b2d639f36804a2d7c31469618fd7a7907c658a7b2aa91a06e31e47',
);
});

// This test is needed for 100% coverage
it('Invalid signature should fail', async function() {
const dappAuth = new DappAuth(
Expand Down