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: set check ethr signer for node clients #618

Merged
merged 2 commits into from
Jul 25, 2022
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
16 changes: 16 additions & 0 deletions e2e/signer.service.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { providers, Wallet } from 'ethers';
import { ProviderType, SignerService } from '../src';

describe('Signer tests', () => {
it('should set `isEthrSigner` for node environment during initialization', async () => {
const wallet = Wallet.createRandom();
const provider = new providers.JsonRpcProvider('http://localhost:8544');
const service = new SignerService(
wallet.connect(provider),
ProviderType.PrivateKey
);
expect(service.isEthSigner).toBeUndefined();
await service.init();
expect(service.isEthSigner).toBe(true);
});
});
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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use _calculatePubKeyAndIdentityToken?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but _calculatePubKeyAndIdentityToken is making a call to RPC asking about block number, so for performance reason i decide to chose this approach

// 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('0x'));
// 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);
}
}
}