Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
29 changes: 20 additions & 9 deletions src/ethauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,40 @@ 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 {
this.validators = validators
}
}

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[]) => {
Expand Down
14 changes: 13 additions & 1 deletion tests/ethauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -74,3 +80,9 @@ describe('ETHAuth', () => {
})

})

const sleep = (time: number) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, time)
})
}