Skip to content

Commit

Permalink
Use EIP6492 for authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Jun 30, 2023
1 parent 3f678c3 commit fb8958b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 91 deletions.
7 changes: 5 additions & 2 deletions packages/account/src/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { commons, universal } from '@0xsequence/core'
import { EIP_6492_SUFFIX } from '@0xsequence/core/src/commons/validateEIP6492'
import { migrator, defaults, version } from '@0xsequence/migration'
import { NetworkConfig } from '@0xsequence/network'
import { FeeOption, FeeQuote, isRelayer, Relayer, RpcRelayer } from '@0xsequence/relayer'
Expand Down Expand Up @@ -480,7 +479,7 @@ export class Account {

return ethers.utils.solidityPack(
['bytes', 'bytes32'],
[encoded, EIP_6492_SUFFIX]
[encoded, commons.EIP6492.EIP_6492_SUFFIX]
)
}

Expand Down Expand Up @@ -873,3 +872,7 @@ export class Account {
return allSigners
}
}

export function isAccount(value: any): value is Account {
return value instanceof Account
}
14 changes: 11 additions & 3 deletions packages/auth/src/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ethers } from 'ethers'
import { ETHAuth, Proof } from '@0xsequence/ethauth'
import { ChainIdLike } from '@0xsequence/network'
import { ChainIdLike, toChainIdNumber } from '@0xsequence/network'
import { TypedData } from '@0xsequence/utils'
import { Signer } from '@0xsequence/wallet'
import { DEFAULT_SESSION_EXPIRATION } from './session'
import { Account } from '@0xsequence/account'

export interface AuthorizationOptions {
// app name string, ie 'Skyweaver'
Expand All @@ -26,7 +27,7 @@ export interface ETHAuthProof {

// signAuthorization will perform an EIP712 typed-data message signing of ETHAuth domain via the provided
// Signer and authorization options.
export const signAuthorization = async (signer: Pick<Signer, 'getAddress' | 'signTypedData'>, chainId: ChainIdLike, options: AuthorizationOptions): Promise<ETHAuthProof> => {
export const signAuthorization = async (signer: Signer | Account, chainId: ChainIdLike, options: AuthorizationOptions): Promise<ETHAuthProof> => {
const address = ethers.utils.getAddress(await signer.getAddress())
if (!address || address === '' || address === '0x') {
throw ErrAccountIsRequired
Expand All @@ -44,7 +45,14 @@ export const signAuthorization = async (signer: Pick<Signer, 'getAddress' | 'sig
proof.setExpiryIn(options.expiry ? Math.max(options.expiry, 200) : DEFAULT_SESSION_EXPIRATION)

const typedData = proof.messageTypedData()
proof.signature = await signer.signTypedData(typedData.domain, typedData.types, typedData.message, chainId)

const chainIdNumber = toChainIdNumber(chainId)

proof.signature = await (signer instanceof Account ?
// Account can sign EIP-6492 signatures, so it doesn't require deploying the wallet
signer.signTypedData(typedData.domain, typedData.types, typedData.message, chainIdNumber, 'eip6492') :
signer.signTypedData(typedData.domain, typedData.types, typedData.message, chainIdNumber)
)

const ethAuth = new ETHAuth()
const proofString = await ethAuth.encodeProof(proof, true)
Expand Down
15 changes: 14 additions & 1 deletion packages/network/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BigNumberish, providers } from 'ethers'
import { BigNumberish, ethers, providers } from 'ethers'
import { Indexer } from '@0xsequence/indexer'
import { Relayer, RpcRelayerOptions } from '@0xsequence/relayer'
import { findNetworkConfig, stringTemplate, validateAndSortNetworks } from './utils'
import { isBigNumberish } from '@0xsequence/utils'

export enum ChainId {
// Ethereum
Expand Down Expand Up @@ -334,6 +335,18 @@ export function findSupportedNetwork(chainIdOrName: string | ChainIdLike): Netwo

export type ChainIdLike = NetworkConfig | BigNumberish

export function toChainIdNumber(chainIdLike: ChainIdLike): ethers.BigNumber {
if (ethers.BigNumber.isBigNumber(chainIdLike)) {
return chainIdLike
}

if (isBigNumberish(chainIdLike)) {
return ethers.BigNumber.from(chainIdLike)
}

return ethers.BigNumber.from(chainIdLike.chainId)
}

const genUrls = (network: string) => {
const rpcUrl = nodesURL(network)
return {
Expand Down
Loading

0 comments on commit fb8958b

Please sign in to comment.