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 e61ff2b commit ca8c0c5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 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,29 @@ export class SignerService {
sig
)}`;
}

/**
* Set `_isEthSigner` value based on a signed message.
* Generates a test message and signs it.
*/
private async _setIsEthrSigner() {
// 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('message'));
// 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 ca8c0c5

Please sign in to comment.