From 1f5a10da4b94676c3f8311b7f85059355e9e3b40 Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Thu, 9 Feb 2023 14:09:50 -0500 Subject: [PATCH] v0.9.0 -- require provider on constructor --- package.json | 2 +- src/ethauth.ts | 29 ++++++++++++++++++++--------- tests/ethauth.test.ts | 14 +++++++++++++- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 5cb67fc..602f560 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@0xsequence/ethauth", - "version": "0.8.1", + "version": "0.9.0", "description": "ETHAuth -- self-signed authorization proofs", "repository": "https://github.com/0xsequence/ethauth.js", "main": "dist/index.js", diff --git a/src/ethauth.ts b/src/ethauth.ts index 0b7e599..d7e688b 100644 --- a/src/ethauth.ts +++ b/src/ethauth.ts @@ -14,11 +14,12 @@ export const ETHAuthEIP712Domain = { export class ETHAuth { validators: ValidatorFunc[] - ethereumJsonRpcURL: string provider: ethers.providers.JsonRpcProvider chainId: number - constructor(...validators: ValidatorFunc[]) { + constructor(provider: string | ethers.providers.JsonRpcProvider, ...validators: ValidatorFunc[]) { + this.configProvider(provider) + if (validators.length == 0) { this.validators = [ ValidateEOAProof, ValidateContractAccountProof ] }else { @@ -26,17 +27,27 @@ export class ETHAuth { } } - configJsonRpcProvider = async (ethereumJsonRpcURL: string) => { - this.provider = new ethers.providers.JsonRpcProvider(ethereumJsonRpcURL) + configProvider = async (provider: string | ethers.providers.JsonRpcProvider) => { + if (!provider || (typeof provider === 'string' && provider.length === 0)) { + // skip if empty provider url is passed + return + } + + if (typeof provider === 'string') { + this.provider = new ethers.providers.JsonRpcProvider(provider) + } else { + this.provider = provider + } - const netVersion = await this.provider.send('net_version', []) - this.chainId = parseInt(netVersion) + try { + const hexChainId = await this.provider.send('eth_chainId', []) + this.chainId = ethers.BigNumber.from(hexChainId).toNumber() + } catch (err) { + } - if (!this.chainId || this.chainId === 0 || this.chainId === NaN) { + if (!this.chainId || this.chainId === 0 || Number.isNaN(this.chainId)) { throw new Error('ethauth: unable to get chainId') } - - this.ethereumJsonRpcURL = ethereumJsonRpcURL } configValidators = (...validators: ValidatorFunc[]) => { diff --git a/tests/ethauth.test.ts b/tests/ethauth.test.ts index 377c639..f97424b 100644 --- a/tests/ethauth.test.ts +++ b/tests/ethauth.test.ts @@ -3,6 +3,12 @@ import { ethers } from 'ethers' describe('ETHAuth', () => { + test('init', async () => { + const ethAuth = new ETHAuth('https://nodes.sequence.app/polygon/test') + await sleep(1000) + expect(ethAuth.chainId).toEqual(137) + }) + test('encode and decode', async () => { // TODO/NOTE: this expected value is fixed, but the time in iat and exp moves forward, // this test is brittle and eventually will fail. @@ -49,7 +55,7 @@ describe('ETHAuth', () => { proof.signature = await wallet.signMessage(digest) - const ethAuth = new ETHAuth() + const ethAuth = new ETHAuth('https://nodes.sequence.app/polygon/test') const proofString = await ethAuth.encodeProof(proof) console.log('proof:', proof) @@ -74,3 +80,9 @@ describe('ETHAuth', () => { }) }) + +const sleep = (time: number) => { + return new Promise((resolve, reject) => { + setTimeout(resolve, time) + }) +}