Skip to content

Commit

Permalink
fix: set check ethr signer for node clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Harasz committed Jul 22, 2022
1 parent e99ceda commit 1a962f5
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/modules/signer/signer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export class SignerService {
} else if (isEthSigner === 'false') {
this._isEthSigner = false;
}
} else {
this._setIsEthrSigner();
}

/**
* @todo provide general way to initialize with previously saved key
*/
Expand Down Expand Up @@ -502,4 +505,43 @@ export class SignerService {
sig
)}`;
}

/**
* Set `_isEthSigner` value based on a signed message.
* Generates a test message and signs it.
*/
private async _setIsEthrSigner() {
const header = {
alg: 'ES256',
typ: 'JWT',
};
const encodedHeader = base64url(JSON.stringify(header));
const address = this._address;
const payload = {
address,
};

const encodedPayload = base64url(JSON.stringify(payload));
const token = `0x${Buffer.from(
`${encodedHeader}.${encodedPayload}`
).toString('hex')}`;
// arrayification is necessary for WalletConnect signatures to work. eth_sign expects message in bytes: https://docs.walletconnect.org/json-rpc-api-methods/ethereum#eth_sign
// keccak256 hash is applied for Metamask to display a coherent hex value when signing
const message = arrayify(keccak256(token));
// Computation of the digest in order to recover the public key under the assumption
// that signature was performed as per the eth_sign spec (https://eth.wiki/json-rpc/API#eth_sign)
const digest = arrayify(hashMessage(message));
const sig = await this._signer.signMessage(message);
const keyFromMessage = recoverPublicKey(message, sig);
const keyFromDigest = recoverPublicKey(digest, sig);
if (getAddress(this._address) === computeAddress(keyFromMessage)) {
this._publicKey = keyFromMessage;
this._isEthSigner = false;
} else if (getAddress(this._address) === computeAddress(keyFromDigest)) {
this._publicKey = keyFromDigest;
this._isEthSigner = true;
} else {
throw new Error(ERROR_MESSAGES.NON_ETH_SIGN_SIGNATURE);
}
}
}

0 comments on commit 1a962f5

Please sign in to comment.