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

Commit

Permalink
fix(PTP-395): refactor auth class
Browse files Browse the repository at this point in the history
  • Loading branch information
VGau committed May 23, 2022
1 parent 3d7f0d9 commit 1922e4c
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 367 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ignoreRestSiblings": true
}
],
"object-curly-newline": "off",
"import/extensions": ["error", "ignorePackages", {
"js": "never"
}]
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
run: npm run test
env:
RPC_URL: ${{ secrets.RPC_URL }}
PROJECT_ID: ${{ secrets.PROJECT_ID }}
SECRET_ID: ${{ secrets.SECRET_ID }}

- name: Run e2e tests
run: npm run test:e2e
Expand Down
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 2022
}
73 changes: 64 additions & 9 deletions lib/Auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,74 @@
* Copyright(c) https://consensys.net/
* MIT Licensed
*/

/* eslint-disable */
import { ethers } from 'ethers';

export default class Auth {
constructor({ accountAddress, privateKey }) {
this._accountAddress = accountAddress;
this.privateKey = privateKey;
#privateKey = null;

#projectId = null;

#secretId = null;

#rpcUrl = null;

#provider = null;

#chainId = null;

constructor({ privateKey, projectId, secretId, rpcUrl, chainId }) {
if (!privateKey) throw new Error('[Auth.constructor] privateKey is missing!');
if (!projectId) throw new Error('[Auth.constructor] projectId is missing!');
if (!secretId) throw new Error('[Auth.constructor] secretId is missing!');
if (!chainId) throw new Error('[Auth.constructor] chainId is missing!');

if (rpcUrl) {
this.#rpcUrl = rpcUrl;
}

this.#privateKey = privateKey;
this.#projectId = projectId;
this.#secretId = secretId;
this.#chainId = chainId;
}

getChainId() {
return this.#chainId;
}

getApiAuthHeader() {
return {
Authorization: `Basic ${this.#base64encode(this.#projectId, this.#secretId)}`,
};
}

#base64encode() {
return Buffer.from(`${this.#projectId}:${this.#secretId}`).toString('base64');
}

async authenticate() {
const userAccount =
await `User authenticated with address ${this._accountAddress} & key ${this._key}.`;
getApiAuth() {
return this.#base64encode();
}

getSigner() {
if (!this.#provider) throw new Error('[Auth.getSigner] You need to set a provider');
return new ethers.Wallet(this.#privateKey, this.#provider);
}

getProvider(injectedProvider) {
if (!injectedProvider && !this.#rpcUrl) {
throw new Error(
'[Auth.getProvider] You need to pass an rpcUrl to the constructor or pass an injected provider to this function!',
);
}

if (injectedProvider) {
this.#provider = new ethers.providers.Web3Provider(injectedProvider);
return this.#provider;
}

return console.log(userAccount);
// eslint-disable-next-line new-cap
this.#provider = new ethers.providers.getDefaultProvider(this.#rpcUrl);
return this.#provider;
}
}
Loading

0 comments on commit 1922e4c

Please sign in to comment.