Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

PTP-535 Allow Provider in Constructor #47

Merged
merged 12 commits into from
Jun 14, 2022
Merged

PTP-535 Allow Provider in Constructor #47

merged 12 commits into from
Jun 14, 2022

Conversation

efecarranza
Copy link
Contributor

@efecarranza efecarranza commented Jun 9, 2022

Changelog

Allow to pass provider in constructor.

@efecarranza efecarranza added help wanted Extra attention is needed question Further information is requested labels Jun 9, 2022
@efecarranza efecarranza added Reviewers Needed and removed help wanted Extra attention is needed question Further information is requested labels Jun 9, 2022
Copy link
Contributor

@kalote kalote left a comment

Choose a reason for hiding this comment

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

On top of all the comments, the integration test (i run npm run test:coverage every time before submitting a PR) doesn't work.
Please check.

@@ -18,29 +19,31 @@ export default class Auth {

#provider;

#signer;
Copy link
Contributor

Choose a reason for hiding this comment

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

this.#signer is not used in the file

// eslint-disable-next-line new-cap
this.#provider = Provider.getProvider(this.#rpcUrl);

this.setProvider(privateKey, provider);
Copy link
Contributor

Choose a reason for hiding this comment

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

function signature should be using this.#privateKey instead of passing args. E.g,

this.setProvider(provider)
throw new Error(
'[Auth.setInjectedProvider] You need to pass an injected provider to this function!',
);
setProvider(privateKey, provider) {
Copy link
Contributor

Choose a reason for hiding this comment

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

function signature should be using this.#privateKey instead of passing args. E.g,

setProvider(provider) {
'[Auth.setInjectedProvider] You need to pass an injected provider to this function!',
);
setProvider(privateKey, provider) {
if (privateKey) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (this.#privateKey) {
);
setProvider(privateKey, provider) {
if (privateKey) {
// eslint-disable-next-line new-cap
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this line

@@ -15,7 +15,7 @@ export default class Provider {
}

static getInjectedProvider(injectedProvider) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure about this method anymore: do we really want the SDK to expose the "provider" class so that the user can use "provider.getInjectedProvider()" ? Shouldn't we have a "Auth.getProvider()" method that acts as the wrapper? or maybe directly passing the "window.ethereum" as a parameter in the Auth object, like:

const accWithProvider = new Auth({
  projectId: process.env.INFURA_PROJECT_ID,
  secretId: process.env.INFURA_PROJECT_SECRET,
  rpcUrl: process.env.EVM_RPC_URL,
  chainId: 5,
  provider: window.ethereum,
});
it('should throw when passing both privateKey and provider', () => {
expect(
() =>
// eslint-disable-next-line implicit-arrow-linebreak
Copy link
Contributor

Choose a reason for hiding this comment

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

eslint is not going through test file. You can remove this line.

PS: if eslint bothers you in test file, add it as an exception in .eslintignore

it('should throw when passing invalid provider', () => {
expect(
() =>
// eslint-disable-next-line implicit-arrow-linebreak
Copy link
Contributor

Choose a reason for hiding this comment

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

remove

projectId: process.env.INFURA_PROJECT_ID,
secretId: process.env.INFURA_PROJECT_SECRET,
rpcUrl: process.env.EVM_RPC_URL,
chainId: 5,
provider: provider,
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for "provider: provider". Just "provider" is enough.

usage.js Outdated
///////// Alternative Auth Instantiation with MetaMask /////////
// When using SDK in a browser
//
// const provider = Provider.getInjectedProvider(window.ethereum);
Copy link
Contributor

Choose a reason for hiding this comment

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

check my comment above about the "usage" from the end-user perspective. I'm not sure we should allow SDK users to use "provider" (and, if it's the case, we need to export it in the index.js)
Best dev experience for me would be to either pass a private key OR pass the window.ethereum object to the Auth constructor.
WDYT @danielpavelicconsensys @salimtb @VGau @adriendelaroche ?

@sonarcloud
Copy link

sonarcloud bot commented Jun 14, 2022

@efecarranza efecarranza merged commit 913dc56 into main Jun 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants