Skip to content

Commit

Permalink
Merge pull request #17 from dapperlabs/eoa-hex-decode
Browse files Browse the repository at this point in the history
Add message decoding tests
  • Loading branch information
callmenick committed Sep 17, 2019
2 parents e2d077e + 13b6f80 commit ecbac9e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
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

0 comments on commit ecbac9e

Please sign in to comment.