Skip to content

Commit e2c3167

Browse files
committed
enforce address normalization at compile-time
1 parent c431e53 commit e2c3167

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+550
-516
lines changed

packages/wallet/core/src/envelope.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Config, Payload, Signature } from '@0xsequence/wallet-primitives'
2-
import { Address, Hex } from 'ox'
1+
import { Address, Config, Payload, Signature } from '@0xsequence/wallet-primitives'
2+
import { Hex } from 'ox'
33

44
export type Envelope<T extends Payload.Payload> = {
55
readonly wallet: Address.Address
@@ -33,15 +33,12 @@ export type Signed<T extends Payload.Payload> = Envelope<T> & {
3333

3434
export function signatureForLeaf(envelope: Signed<Payload.Payload>, leaf: Config.Leaf) {
3535
if (Config.isSignerLeaf(leaf)) {
36-
return envelope.signatures.find((sig) => isSignature(sig) && Address.isEqual(sig.address, leaf.address))
36+
return envelope.signatures.find((sig) => isSignature(sig) && sig.address === leaf.address)
3737
}
3838

3939
if (Config.isSapientSignerLeaf(leaf)) {
4040
return envelope.signatures.find(
41-
(sig) =>
42-
isSapientSignature(sig) &&
43-
sig.imageHash === leaf.imageHash &&
44-
Address.isEqual(sig.signature.address, leaf.address),
41+
(sig) => isSapientSignature(sig) && sig.imageHash === leaf.imageHash && sig.signature.address === leaf.address,
4542
)
4643
}
4744

@@ -92,7 +89,7 @@ export function addSignature(
9289
const prev = envelope.signatures.find(
9390
(sig) =>
9491
isSapientSignature(sig) &&
95-
Address.isEqual(sig.signature.address, signature.signature.address) &&
92+
sig.signature.address === signature.signature.address &&
9693
sig.imageHash === signature.imageHash,
9794
) as SapientSignature | undefined
9895

@@ -113,9 +110,9 @@ export function addSignature(
113110
envelope.signatures.push(signature)
114111
} else if (isSignature(signature)) {
115112
// Find if the signature already exists in envelope
116-
const prev = envelope.signatures.find(
117-
(sig) => isSignature(sig) && Address.isEqual(sig.address, signature.address),
118-
) as Signature | undefined
113+
const prev = envelope.signatures.find((sig) => isSignature(sig) && sig.address === signature.address) as
114+
| Signature
115+
| undefined
119116

120117
if (prev) {
121118
// If the signatures are identical, then we can do nothing

packages/wallet/core/src/preconditions/codec.ts

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Address } from 'ox'
21
import {
32
Precondition,
43
NativeBalancePrecondition,
@@ -41,52 +40,37 @@ export function decodePrecondition(p: IntentPrecondition): Precondition | undefi
4140
switch (p.type) {
4241
case 'native-balance':
4342
precondition = new NativeBalancePrecondition(
44-
Address.from(data.address),
43+
data.address,
4544
data.min ? BigInt(data.min) : undefined,
4645
data.max ? BigInt(data.max) : undefined,
4746
)
4847
break
4948

5049
case 'erc20-balance':
5150
precondition = new Erc20BalancePrecondition(
52-
Address.from(data.address),
53-
Address.from(data.token),
51+
data.address,
52+
data.token,
5453
data.min ? BigInt(data.min) : undefined,
5554
data.max ? BigInt(data.max) : undefined,
5655
)
5756
break
5857

5958
case 'erc20-approval':
60-
precondition = new Erc20ApprovalPrecondition(
61-
Address.from(data.address),
62-
Address.from(data.token),
63-
Address.from(data.operator),
64-
BigInt(data.min),
65-
)
59+
precondition = new Erc20ApprovalPrecondition(data.address, data.token, data.operator, BigInt(data.min))
6660
break
6761

6862
case 'erc721-ownership':
69-
precondition = new Erc721OwnershipPrecondition(
70-
Address.from(data.address),
71-
Address.from(data.token),
72-
BigInt(data.tokenId),
73-
data.owned,
74-
)
63+
precondition = new Erc721OwnershipPrecondition(data.address, data.token, BigInt(data.tokenId), data.owned)
7564
break
7665

7766
case 'erc721-approval':
78-
precondition = new Erc721ApprovalPrecondition(
79-
Address.from(data.address),
80-
Address.from(data.token),
81-
BigInt(data.tokenId),
82-
Address.from(data.operator),
83-
)
67+
precondition = new Erc721ApprovalPrecondition(data.address, data.token, BigInt(data.tokenId), data.operator)
8468
break
8569

8670
case 'erc1155-balance':
8771
precondition = new Erc1155BalancePrecondition(
88-
Address.from(data.address),
89-
Address.from(data.token),
72+
data.address,
73+
data.token,
9074
BigInt(data.tokenId),
9175
data.min ? BigInt(data.min) : undefined,
9276
data.max ? BigInt(data.max) : undefined,
@@ -95,10 +79,10 @@ export function decodePrecondition(p: IntentPrecondition): Precondition | undefi
9579

9680
case 'erc1155-approval':
9781
precondition = new Erc1155ApprovalPrecondition(
98-
Address.from(data.address),
99-
Address.from(data.token),
82+
data.address,
83+
data.token,
10084
BigInt(data.tokenId),
101-
Address.from(data.operator),
85+
data.operator,
10286
BigInt(data.min),
10387
)
10488
break

packages/wallet/core/src/preconditions/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Address } from 'ox'
1+
import { Address } from '@0xsequence/wallet-primitives'
22

33
export interface Precondition {
44
type(): string

packages/wallet/core/src/relayer/bundler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Payload } from '@0xsequence/wallet-primitives'
2-
import { Address, Hex } from 'ox'
1+
import { Address, Payload } from '@0xsequence/wallet-primitives'
2+
import { Hex } from 'ox'
33
import { UserOperation } from 'ox/erc4337'
44
import { OperationStatus } from './relayer.js'
55

packages/wallet/core/src/relayer/bundlers/pimlico.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Payload } from '@0xsequence/wallet-primitives'
1+
import { Address, Payload } from '@0xsequence/wallet-primitives'
22
import { Bundler } from '../bundler.js'
3-
import { Provider, Hex, Address, RpcTransport } from 'ox'
3+
import { Provider, Hex, RpcTransport } from 'ox'
44
import { UserOperation } from 'ox/erc4337'
55
import { OperationStatus } from '../relayer.js'
66

@@ -38,7 +38,7 @@ export class PimlicoBundler implements Bundler {
3838
return false
3939
}
4040

41-
return supportedEntryPoints.some((ep) => Address.isEqual(ep, entrypoint))
41+
return supportedEntryPoints.includes(entrypoint)
4242
}
4343

4444
async relay(entrypoint: Address.Address, userOperation: UserOperation.RpcV07): Promise<{ opHash: Hex.Hex }> {

packages/wallet/core/src/relayer/relayer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Payload, Precondition } from '@0xsequence/wallet-primitives'
2-
import { Address, Hex } from 'ox'
1+
import { Address, Payload, Precondition } from '@0xsequence/wallet-primitives'
2+
import { Hex } from 'ox'
33
import { GetMetaTxnReceiptReturn } from './standard/rpc/index.js'
44

55
export interface FeeOption {

packages/wallet/core/src/relayer/standard/eip6963.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { createStore, EIP6963ProviderInfo, EIP6963ProviderDetail } from 'mipd'
22
import { EIP1193ProviderAdapter, LocalRelayer } from './local.js'
33
import { FeeOption, FeeQuote, OperationStatus, Relayer } from '../relayer.js'
4-
import { Address, Hex } from 'ox'
5-
import { Payload } from '@0xsequence/wallet-primitives'
4+
import { Hex } from 'ox'
5+
import { Address, Payload } from '@0xsequence/wallet-primitives'
66
import { IntentPrecondition } from './rpc/relayer.gen.js'
77

88
export class EIP6963Relayer implements Relayer {

packages/wallet/core/src/relayer/standard/local.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Constants, Payload } from '@0xsequence/wallet-primitives'
1+
import { Address, Constants, Payload } from '@0xsequence/wallet-primitives'
22
import { EIP1193Provider } from 'mipd'
3-
import { AbiFunction, Address, Bytes, Hex, TransactionReceipt } from 'ox'
3+
import { AbiFunction, Bytes, Hex, TransactionReceipt } from 'ox'
44
import { FeeOption, FeeQuote, OperationStatus, Relayer } from '../relayer.js'
55
import { IntentPrecondition } from './rpc/relayer.gen.js'
66
import { decodePrecondition } from '../../preconditions/index.js'

packages/wallet/core/src/relayer/standard/pk-relayer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Payload, Precondition } from '@0xsequence/wallet-primitives'
2-
import { Address, Hex, Provider, Secp256k1, TransactionEnvelopeEip1559, TransactionReceipt } from 'ox'
1+
import { Address, Payload, Precondition } from '@0xsequence/wallet-primitives'
2+
import { Hex, Provider, Secp256k1, TransactionEnvelopeEip1559, TransactionReceipt } from 'ox'
33
import { LocalRelayer } from './local.js'
44
import { FeeOption, FeeQuote, OperationStatus, Relayer } from '../relayer.js'
55

@@ -72,10 +72,10 @@ export class PkRelayer implements Relayer {
7272
})
7373
return tx
7474
},
75-
getBalance: async (address: string): Promise<bigint> => {
75+
getBalance: async (address: Address.Address): Promise<bigint> => {
7676
const balanceHex = await this.provider.request({
7777
method: 'eth_getBalance',
78-
params: [address as Address.Address, 'latest'],
78+
params: [address, 'latest'],
7979
})
8080
return BigInt(balanceHex)
8181
},

packages/wallet/core/src/relayer/standard/rpc/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
GetMetaTxnReceiptReturn,
88
} from './relayer.gen.js'
99
import { FeeOption, FeeQuote, OperationStatus, Relayer } from '../../relayer.js'
10-
import { Address, Hex, Bytes, AbiFunction } from 'ox'
11-
import { Payload, Precondition as PrimitivePrecondition } from '@0xsequence/wallet-primitives'
10+
import { Hex, Bytes, AbiFunction } from 'ox'
11+
import { Address, Payload, Precondition as PrimitivePrecondition } from '@0xsequence/wallet-primitives'
1212
import {
1313
IntentPrecondition as RpcIntentPrecondition,
1414
ETHTxnStatus,
@@ -375,8 +375,8 @@ export class RpcRelayer implements Relayer {
375375

376376
private mapRpcFeeTokenToAddress(rpcToken: RpcFeeToken): Address.Address {
377377
if (rpcToken.type === FeeTokenType.ERC20_TOKEN && rpcToken.contractAddress) {
378-
return Address.from(rpcToken.contractAddress)
378+
return Address.normalize(rpcToken.contractAddress)
379379
}
380-
return '0x0000000000000000000000000000000000000000'
380+
return Address.normalize('0x0000000000000000000000000000000000000000')
381381
}
382382
}

0 commit comments

Comments
 (0)