viem 2.57.19 → 2.57.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/_cjs/accounts/index.js +2 -1
- package/_cjs/accounts/index.js.map +1 -1
- package/_cjs/accounts/utils/publicKeyToAddress.js +15 -0
- package/_cjs/accounts/utils/publicKeyToAddress.js.map +1 -1
- package/_cjs/actions/public/call.js +19 -3
- package/_cjs/actions/public/call.js.map +1 -1
- package/_cjs/actions/public/estimateMaxPriorityFeePerGas.js +1 -1
- package/_cjs/actions/public/estimateMaxPriorityFeePerGas.js.map +1 -1
- package/_cjs/actions/wallet/prepareTransactionRequest.js +99 -76
- package/_cjs/actions/wallet/prepareTransactionRequest.js.map +1 -1
- package/_cjs/chains/definitions/darwinia.js +1 -1
- package/_cjs/chains/definitions/darwinia.js.map +1 -1
- package/_cjs/chains/definitions/flowMainnet.js +1 -1
- package/_cjs/chains/definitions/flowMainnet.js.map +1 -1
- package/_cjs/errors/encoding.js +9 -1
- package/_cjs/errors/encoding.js.map +1 -1
- package/_cjs/errors/version.js +1 -1
- package/_cjs/index.js +6 -5
- package/_cjs/index.js.map +1 -1
- package/_cjs/utils/address/getAddress.js +7 -6
- package/_cjs/utils/address/getAddress.js.map +1 -1
- package/_cjs/utils/address/isAddress.js +8 -12
- package/_cjs/utils/address/isAddress.js.map +1 -1
- package/_cjs/utils/encoding/fromRlp.js +33 -8
- package/_cjs/utils/encoding/fromRlp.js.map +1 -1
- package/_cjs/utils/index.js +5 -4
- package/_cjs/utils/index.js.map +1 -1
- package/_cjs/utils/promise/createBatchScheduler.js +7 -2
- package/_cjs/utils/promise/createBatchScheduler.js.map +1 -1
- package/_cjs/utils/typedData.js +5 -9
- package/_cjs/utils/typedData.js.map +1 -1
- package/_esm/accounts/index.js +1 -1
- package/_esm/accounts/index.js.map +1 -1
- package/_esm/accounts/utils/publicKeyToAddress.js +20 -1
- package/_esm/accounts/utils/publicKeyToAddress.js.map +1 -1
- package/_esm/actions/public/call.js +19 -3
- package/_esm/actions/public/call.js.map +1 -1
- package/_esm/actions/public/estimateMaxPriorityFeePerGas.js +1 -1
- package/_esm/actions/public/estimateMaxPriorityFeePerGas.js.map +1 -1
- package/_esm/actions/wallet/prepareTransactionRequest.js +111 -79
- package/_esm/actions/wallet/prepareTransactionRequest.js.map +1 -1
- package/_esm/chains/definitions/darwinia.js +1 -1
- package/_esm/chains/definitions/darwinia.js.map +1 -1
- package/_esm/chains/definitions/flowMainnet.js +1 -1
- package/_esm/chains/definitions/flowMainnet.js.map +1 -1
- package/_esm/errors/encoding.js +8 -0
- package/_esm/errors/encoding.js.map +1 -1
- package/_esm/errors/version.js +1 -1
- package/_esm/index.js +1 -1
- package/_esm/index.js.map +1 -1
- package/_esm/tempo/internal/relay/feePayer.js +1 -1
- package/_esm/tempo/internal/relay/feePayer.js.map +1 -1
- package/_esm/utils/address/getAddress.js +32 -6
- package/_esm/utils/address/getAddress.js.map +1 -1
- package/_esm/utils/address/isAddress.js +32 -12
- package/_esm/utils/address/isAddress.js.map +1 -1
- package/_esm/utils/encoding/fromRlp.js +37 -9
- package/_esm/utils/encoding/fromRlp.js.map +1 -1
- package/_esm/utils/index.js +1 -1
- package/_esm/utils/index.js.map +1 -1
- package/_esm/utils/promise/createBatchScheduler.js +7 -2
- package/_esm/utils/promise/createBatchScheduler.js.map +1 -1
- package/_esm/utils/typedData.js +7 -9
- package/_esm/utils/typedData.js.map +1 -1
- package/_types/accounts/index.d.ts +1 -1
- package/_types/accounts/index.d.ts.map +1 -1
- package/_types/accounts/utils/publicKeyToAddress.d.ts +16 -2
- package/_types/accounts/utils/publicKeyToAddress.d.ts.map +1 -1
- package/_types/actions/public/call.d.ts +1 -1
- package/_types/actions/public/call.d.ts.map +1 -1
- package/_types/actions/public/estimateMaxPriorityFeePerGas.d.ts +1 -1
- package/_types/actions/public/estimateMaxPriorityFeePerGas.d.ts.map +1 -1
- package/_types/actions/wallet/prepareTransactionRequest.d.ts.map +1 -1
- package/_types/chains/definitions/darwinia.d.ts +1 -1
- package/_types/chains/definitions/darwinia.d.ts.map +1 -1
- package/_types/chains/definitions/flowMainnet.d.ts +1 -1
- package/_types/chains/definitions/flowMainnet.d.ts.map +1 -1
- package/_types/errors/encoding.d.ts +9 -0
- package/_types/errors/encoding.d.ts.map +1 -1
- package/_types/errors/version.d.ts +1 -1
- package/_types/index.d.ts +1 -1
- package/_types/index.d.ts.map +1 -1
- package/_types/utils/address/getAddress.d.ts +23 -0
- package/_types/utils/address/getAddress.d.ts.map +1 -1
- package/_types/utils/address/isAddress.d.ts +22 -0
- package/_types/utils/address/isAddress.d.ts.map +1 -1
- package/_types/utils/encoding/fromRlp.d.ts +3 -3
- package/_types/utils/encoding/fromRlp.d.ts.map +1 -1
- package/_types/utils/index.d.ts +1 -1
- package/_types/utils/index.d.ts.map +1 -1
- package/_types/utils/promise/createBatchScheduler.d.ts.map +1 -1
- package/_types/utils/typedData.d.ts.map +1 -1
- package/accounts/index.ts +2 -0
- package/accounts/utils/publicKeyToAddress.ts +27 -1
- package/actions/public/call.ts +20 -3
- package/actions/public/estimateMaxPriorityFeePerGas.ts +2 -2
- package/actions/wallet/prepareTransactionRequest.ts +136 -107
- package/chains/definitions/darwinia.ts +1 -1
- package/chains/definitions/flowMainnet.ts +1 -1
- package/errors/encoding.ts +12 -0
- package/errors/version.ts +1 -1
- package/index.ts +2 -0
- package/package.json +2 -2
- package/tempo/internal/relay/feePayer.ts +1 -1
- package/utils/address/getAddress.ts +31 -6
- package/utils/address/isAddress.ts +31 -10
- package/utils/encoding/fromRlp.ts +37 -5
- package/utils/index.ts +2 -0
- package/utils/promise/createBatchScheduler.ts +10 -2
- package/utils/typedData.ts +7 -9
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type EstimateGasParameters,
|
|
14
14
|
estimateGas,
|
|
15
15
|
} from '../../actions/public/estimateGas.js'
|
|
16
|
+
import { internal_estimateMaxPriorityFeePerGas } from '../../actions/public/estimateMaxPriorityFeePerGas.js'
|
|
16
17
|
import {
|
|
17
18
|
type GetBlockErrorType,
|
|
18
19
|
getBlock as getBlock_,
|
|
@@ -610,133 +611,161 @@ export async function prepareTransactionRequest<
|
|
|
610
611
|
)) ?? request
|
|
611
612
|
}
|
|
612
613
|
|
|
613
|
-
let block: Block | undefined
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
block = await getAction(
|
|
617
|
-
client,
|
|
618
|
-
getBlock_,
|
|
619
|
-
'getBlock',
|
|
620
|
-
)({ blockTag: 'latest' })
|
|
614
|
+
let block: Promise<Block> | undefined
|
|
615
|
+
function getBlock(): Promise<Block> {
|
|
616
|
+
block ??= getAction(client, getBlock_, 'getBlock')({ blockTag: 'latest' })
|
|
621
617
|
return block
|
|
622
618
|
}
|
|
623
619
|
|
|
624
|
-
|
|
620
|
+
// Chain fee hooks receive the block and the request, so the requests they
|
|
621
|
+
// depend on are made in sequence.
|
|
622
|
+
const hasFeeHooks = Object.values(chain?.fees ?? {}).some(
|
|
623
|
+
(fee) => typeof fee === 'function',
|
|
624
|
+
)
|
|
625
|
+
|
|
626
|
+
// The nonce does not depend on the type or the fees, so it is fetched
|
|
627
|
+
// concurrently with them.
|
|
628
|
+
const noncePromise =
|
|
625
629
|
parameters.includes('nonce') &&
|
|
626
630
|
typeof nonce === 'undefined' &&
|
|
627
631
|
account &&
|
|
628
632
|
!nonceManager
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
proofs,
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
633
|
+
? getAction(
|
|
634
|
+
client,
|
|
635
|
+
getTransactionCount,
|
|
636
|
+
'getTransactionCount',
|
|
637
|
+
)({
|
|
638
|
+
address: account.address,
|
|
639
|
+
blockTag: 'pending',
|
|
640
|
+
})
|
|
641
|
+
: undefined
|
|
642
|
+
// A rejection is thrown when the nonce is awaited.
|
|
643
|
+
noncePromise?.catch(() => {})
|
|
644
|
+
if (noncePromise && hasFeeHooks) request.nonce = await noncePromise
|
|
645
|
+
|
|
646
|
+
try {
|
|
647
|
+
if (
|
|
648
|
+
(parameters.includes('blobVersionedHashes') ||
|
|
649
|
+
parameters.includes('sidecars')) &&
|
|
650
|
+
blobs &&
|
|
651
|
+
kzg
|
|
652
|
+
) {
|
|
653
|
+
const commitments = blobsToCommitments({ blobs, kzg })
|
|
654
|
+
|
|
655
|
+
if (parameters.includes('blobVersionedHashes')) {
|
|
656
|
+
const versionedHashes = commitmentsToVersionedHashes({
|
|
657
|
+
commitments,
|
|
658
|
+
to: 'hex',
|
|
659
|
+
})
|
|
660
|
+
request.blobVersionedHashes = versionedHashes
|
|
661
|
+
}
|
|
662
|
+
if (parameters.includes('sidecars')) {
|
|
663
|
+
const proofs = blobsToProofs({ blobs, commitments, kzg })
|
|
664
|
+
const sidecars = toBlobSidecars({
|
|
665
|
+
blobs,
|
|
666
|
+
commitments,
|
|
667
|
+
proofs,
|
|
668
|
+
to: 'hex',
|
|
669
|
+
})
|
|
670
|
+
request.sidecars = sidecars
|
|
671
|
+
}
|
|
663
672
|
}
|
|
664
|
-
}
|
|
665
673
|
|
|
666
|
-
|
|
674
|
+
if (parameters.includes('chainId')) request.chainId = await getChainId()
|
|
667
675
|
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
676
|
+
if (
|
|
677
|
+
(parameters.includes('fees') || parameters.includes('type')) &&
|
|
678
|
+
typeof type === 'undefined'
|
|
679
|
+
) {
|
|
680
|
+
try {
|
|
681
|
+
request.type = getTransactionType(
|
|
682
|
+
request as TransactionSerializable,
|
|
683
|
+
) as any
|
|
684
|
+
} catch {
|
|
685
|
+
let isEip1559Network = eip1559NetworkCache.get(client.uid)
|
|
686
|
+
if (typeof isEip1559Network === 'undefined') {
|
|
687
|
+
const block = await getBlock()
|
|
688
|
+
isEip1559Network = typeof block?.baseFeePerGas === 'bigint'
|
|
689
|
+
eip1559NetworkCache.set(client.uid, isEip1559Network)
|
|
690
|
+
}
|
|
691
|
+
request.type = isEip1559Network ? 'eip1559' : 'legacy'
|
|
682
692
|
}
|
|
683
|
-
request.type = isEip1559Network ? 'eip1559' : 'legacy'
|
|
684
693
|
}
|
|
685
|
-
}
|
|
686
694
|
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
if (request.type !== 'legacy' && request.type !== 'eip2930') {
|
|
691
|
-
// EIP-1559 fees
|
|
692
|
-
if (
|
|
693
|
-
typeof request.maxFeePerGas === 'undefined' ||
|
|
694
|
-
typeof request.maxPriorityFeePerGas === 'undefined'
|
|
695
|
-
) {
|
|
696
|
-
const block = await getBlock()
|
|
697
|
-
const { maxFeePerGas, maxPriorityFeePerGas } =
|
|
698
|
-
await internal_estimateFeesPerGas(client, {
|
|
699
|
-
block: block as Block,
|
|
700
|
-
chain,
|
|
701
|
-
request: request as PrepareTransactionRequestParameters,
|
|
702
|
-
})
|
|
695
|
+
if (parameters.includes('fees')) {
|
|
696
|
+
// TODO(4844): derive blob base fees once https://github.com/ethereum/execution-apis/pull/486 is merged.
|
|
703
697
|
|
|
698
|
+
if (request.type !== 'legacy' && request.type !== 'eip2930') {
|
|
699
|
+
// EIP-1559 fees
|
|
704
700
|
if (
|
|
705
|
-
typeof request.
|
|
706
|
-
typeof request.
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
701
|
+
typeof request.maxFeePerGas === 'undefined' ||
|
|
702
|
+
typeof request.maxPriorityFeePerGas === 'undefined'
|
|
703
|
+
) {
|
|
704
|
+
// Without chain fee hooks, the priority fee does not depend on the
|
|
705
|
+
// block, so it is estimated concurrently with the block request.
|
|
706
|
+
const [block, priorityFee] = await Promise.all([
|
|
707
|
+
getBlock(),
|
|
708
|
+
hasFeeHooks || typeof request.maxPriorityFeePerGas === 'bigint'
|
|
709
|
+
? undefined
|
|
710
|
+
: internal_estimateMaxPriorityFeePerGas(client, {
|
|
711
|
+
block: getBlock(),
|
|
712
|
+
chain,
|
|
713
|
+
request: request as PrepareTransactionRequestParameters,
|
|
714
|
+
}),
|
|
715
|
+
])
|
|
716
|
+
const { maxFeePerGas, maxPriorityFeePerGas } =
|
|
717
|
+
await internal_estimateFeesPerGas(client, {
|
|
718
|
+
block,
|
|
719
|
+
chain,
|
|
720
|
+
request: (typeof priorityFee === 'bigint'
|
|
721
|
+
? { ...request, maxPriorityFeePerGas: priorityFee }
|
|
722
|
+
: request) as PrepareTransactionRequestParameters,
|
|
723
|
+
})
|
|
724
|
+
|
|
725
|
+
if (
|
|
726
|
+
typeof request.maxPriorityFeePerGas === 'undefined' &&
|
|
727
|
+
typeof request.maxFeePerGas === 'bigint' &&
|
|
728
|
+
request.maxFeePerGas < maxPriorityFeePerGas
|
|
729
|
+
)
|
|
730
|
+
throw new MaxFeePerGasTooLowError({
|
|
731
|
+
maxPriorityFeePerGas,
|
|
732
|
+
})
|
|
733
|
+
|
|
734
|
+
request.maxPriorityFeePerGas = maxPriorityFeePerGas
|
|
735
|
+
request.maxFeePerGas = maxFeePerGas
|
|
736
|
+
}
|
|
737
|
+
} else {
|
|
738
|
+
// Legacy fees
|
|
739
|
+
if (
|
|
740
|
+
typeof request.maxFeePerGas !== 'undefined' ||
|
|
741
|
+
typeof request.maxPriorityFeePerGas !== 'undefined'
|
|
734
742
|
)
|
|
735
|
-
|
|
743
|
+
throw new Eip1559FeesNotSupportedError()
|
|
744
|
+
|
|
745
|
+
if (typeof request.gasPrice === 'undefined') {
|
|
746
|
+
const block = await getBlock()
|
|
747
|
+
const { gasPrice: gasPrice_ } = await internal_estimateFeesPerGas(
|
|
748
|
+
client,
|
|
749
|
+
{
|
|
750
|
+
block: block as Block,
|
|
751
|
+
chain,
|
|
752
|
+
request: request as PrepareTransactionRequestParameters,
|
|
753
|
+
type: 'legacy',
|
|
754
|
+
},
|
|
755
|
+
)
|
|
756
|
+
request.gasPrice = gasPrice_
|
|
757
|
+
}
|
|
736
758
|
}
|
|
737
759
|
}
|
|
760
|
+
} catch (error) {
|
|
761
|
+
// A failed nonce request takes precedence over errors from the other
|
|
762
|
+
// parameters.
|
|
763
|
+
await noncePromise
|
|
764
|
+
throw error
|
|
738
765
|
}
|
|
739
766
|
|
|
767
|
+
if (noncePromise) request.nonce = await noncePromise
|
|
768
|
+
|
|
740
769
|
if (parameters.includes('gas') && typeof gas === 'undefined')
|
|
741
770
|
request.gas = await getAction(
|
|
742
771
|
client,
|
package/errors/encoding.ts
CHANGED
|
@@ -89,6 +89,18 @@ export class RlpListBoundaryExceededError extends BaseError {
|
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
export type RlpNonCanonicalErrorType = RlpNonCanonicalError & {
|
|
93
|
+
name: 'RlpNonCanonicalError'
|
|
94
|
+
}
|
|
95
|
+
/** Thrown when an RLP payload uses a non-minimal length prefix or wraps a single byte below `0x80` in a length prefix. */
|
|
96
|
+
export class RlpNonCanonicalError extends BaseError {
|
|
97
|
+
constructor({ reason }: { reason: string }) {
|
|
98
|
+
super(`RLP payload is not canonically encoded: ${reason}.`, {
|
|
99
|
+
name: 'RlpNonCanonicalError',
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
92
104
|
export type RlpTrailingBytesErrorType = RlpTrailingBytesError & {
|
|
93
105
|
name: 'RlpTrailingBytesError'
|
|
94
106
|
}
|
package/errors/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '2.57.
|
|
1
|
+
export const version = '2.57.20'
|
package/index.ts
CHANGED
|
@@ -911,6 +911,8 @@ export {
|
|
|
911
911
|
type RlpDepthLimitExceededErrorType,
|
|
912
912
|
RlpListBoundaryExceededError,
|
|
913
913
|
type RlpListBoundaryExceededErrorType,
|
|
914
|
+
RlpNonCanonicalError,
|
|
915
|
+
type RlpNonCanonicalErrorType,
|
|
914
916
|
RlpTrailingBytesError,
|
|
915
917
|
type RlpTrailingBytesErrorType,
|
|
916
918
|
SizeOverflowError,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "viem",
|
|
3
3
|
"description": "TypeScript Interface for Ethereum",
|
|
4
|
-
"version": "2.57.
|
|
4
|
+
"version": "2.57.20",
|
|
5
5
|
"main": "./_cjs/index.js",
|
|
6
6
|
"module": "./_esm/index.js",
|
|
7
7
|
"types": "./_types/index.d.ts",
|
|
@@ -233,7 +233,7 @@
|
|
|
233
233
|
"@scure/bip39": "1.6.0",
|
|
234
234
|
"abitype": "1.2.3",
|
|
235
235
|
"isows": "1.0.7",
|
|
236
|
-
"ox": "0.14.
|
|
236
|
+
"ox": "0.14.67",
|
|
237
237
|
"ws": "8.21.0"
|
|
238
238
|
},
|
|
239
239
|
"license": "MIT",
|
|
@@ -652,7 +652,7 @@ function isCanonical(serialized: `0x76${string}` | `0x78${string}`) {
|
|
|
652
652
|
: TxEnvelopeTempo.serialize(envelope)
|
|
653
653
|
return serialized_parsed === serialized.toLowerCase()
|
|
654
654
|
} catch {
|
|
655
|
-
//
|
|
655
|
+
// An envelope that cannot be parsed or serialized has no canonical encoding.
|
|
656
656
|
return false
|
|
657
657
|
}
|
|
658
658
|
}
|
|
@@ -17,6 +17,12 @@ export type ChecksumAddressErrorType =
|
|
|
17
17
|
| StringToBytesErrorType
|
|
18
18
|
| ErrorType
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Returns the [EIP-55](https://eips.ethereum.org/EIPS/eip-55) checksummed form
|
|
22
|
+
* of an address. The input's casing is ignored.
|
|
23
|
+
*
|
|
24
|
+
* Does not validate the input; use {@link getAddress} for untrusted strings.
|
|
25
|
+
*/
|
|
20
26
|
export function checksumAddress(
|
|
21
27
|
address_: Address,
|
|
22
28
|
/**
|
|
@@ -31,12 +37,14 @@ export function checksumAddress(
|
|
|
31
37
|
*/
|
|
32
38
|
chainId?: number | undefined,
|
|
33
39
|
): Address {
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
// The checksum depends only on the lowercase address, so every casing of an
|
|
41
|
+
// address shares one cache entry.
|
|
42
|
+
const lower = address_.toLowerCase()
|
|
43
|
+
const cacheKey = `${lower}.${chainId}`
|
|
44
|
+
const cached = checksumAddressCache.get(cacheKey)
|
|
45
|
+
if (cached) return cached
|
|
36
46
|
|
|
37
|
-
const hexAddress = chainId
|
|
38
|
-
? `${chainId}${address_.toLowerCase()}`
|
|
39
|
-
: address_.substring(2).toLowerCase()
|
|
47
|
+
const hexAddress = chainId ? `${chainId}${lower}` : lower.substring(2)
|
|
40
48
|
const hash = keccak256(stringToBytes(hexAddress), 'bytes')
|
|
41
49
|
|
|
42
50
|
const address = (
|
|
@@ -52,7 +60,7 @@ export function checksumAddress(
|
|
|
52
60
|
}
|
|
53
61
|
|
|
54
62
|
const result = `0x${address.join('')}` as const
|
|
55
|
-
checksumAddressCache.set(
|
|
63
|
+
checksumAddressCache.set(cacheKey, result)
|
|
56
64
|
return result
|
|
57
65
|
}
|
|
58
66
|
|
|
@@ -61,6 +69,23 @@ export type GetAddressErrorType =
|
|
|
61
69
|
| IsAddressErrorType
|
|
62
70
|
| ErrorType
|
|
63
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Converts an address into its [EIP-55](https://eips.ethereum.org/EIPS/eip-55)
|
|
74
|
+
* checksummed form. Throws if the string is not a `0x`-prefixed, 20-byte hex
|
|
75
|
+
* string.
|
|
76
|
+
*
|
|
77
|
+
* The input's casing is ignored, so an address with a wrong checksum is
|
|
78
|
+
* returned with the correct one. To reject a wrong checksum, check the input
|
|
79
|
+
* with {@link isAddress} first.
|
|
80
|
+
*
|
|
81
|
+
* - Docs: https://viem.sh/docs/utilities/getAddress
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* import { getAddress } from 'viem'
|
|
85
|
+
*
|
|
86
|
+
* getAddress('0x787bc77f1700dce0f8c1581c18a12d3153d1b150')
|
|
87
|
+
* // '0x787bc77F1700DCE0f8c1581c18A12d3153D1B150'
|
|
88
|
+
*/
|
|
64
89
|
export function getAddress(
|
|
65
90
|
address: string,
|
|
66
91
|
/**
|
|
@@ -19,21 +19,42 @@ export type IsAddressOptions = {
|
|
|
19
19
|
|
|
20
20
|
export type IsAddressErrorType = ErrorType
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Checks if a string is a valid address.
|
|
24
|
+
*
|
|
25
|
+
* In strict mode (the default), a mixed-case address must match its
|
|
26
|
+
* [EIP-55](https://eips.ethereum.org/EIPS/eip-55) checksum. An all-lowercase
|
|
27
|
+
* address has no checksum and is valid.
|
|
28
|
+
*
|
|
29
|
+
* Use this to validate user input. {@link getAddress} does not validate the
|
|
30
|
+
* checksum; it returns the checksummed form of any casing.
|
|
31
|
+
*
|
|
32
|
+
* - Docs: https://viem.sh/docs/utilities/isAddress
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* import { isAddress } from 'viem'
|
|
36
|
+
*
|
|
37
|
+
* isAddress('0x787bc77F1700DCE0f8c1581c18A12d3153D1B150')
|
|
38
|
+
* // true
|
|
39
|
+
* isAddress('0x787bc77F1700DCE0f8c1581c18A12d3153D1b150') // last `B` lowercased
|
|
40
|
+
* // false
|
|
41
|
+
* isAddress('0x787bc77f1700dce0f8c1581c18a12d3153d1b150')
|
|
42
|
+
* // true
|
|
43
|
+
*/
|
|
22
44
|
export function isAddress(
|
|
23
45
|
address: string,
|
|
24
46
|
options?: IsAddressOptions | undefined,
|
|
25
47
|
): address is Address {
|
|
26
48
|
const { strict = true } = options ?? {}
|
|
27
|
-
|
|
49
|
+
if (!addressRegex.test(address)) return false
|
|
50
|
+
if (!strict || address.toLowerCase() === address) return true
|
|
28
51
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
})()
|
|
37
|
-
isAddressCache.set(cacheKey, result)
|
|
52
|
+
// Only the checksum comparison is cached; the checks above are cheaper than a
|
|
53
|
+
// cache lookup.
|
|
54
|
+
let result = isAddressCache.get(address)
|
|
55
|
+
if (result === undefined) {
|
|
56
|
+
result = checksumAddress(address as Address) === address
|
|
57
|
+
isAddressCache.set(address, result)
|
|
58
|
+
}
|
|
38
59
|
return result
|
|
39
60
|
}
|
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
type RlpDepthLimitExceededErrorType,
|
|
7
7
|
RlpListBoundaryExceededError,
|
|
8
8
|
type RlpListBoundaryExceededErrorType,
|
|
9
|
+
RlpNonCanonicalError,
|
|
10
|
+
type RlpNonCanonicalErrorType,
|
|
9
11
|
RlpTrailingBytesError,
|
|
10
12
|
type RlpTrailingBytesErrorType,
|
|
11
13
|
} from '../../errors/encoding.js'
|
|
@@ -35,6 +37,7 @@ export type FromRlpErrorType =
|
|
|
35
37
|
| HexToBytesErrorType
|
|
36
38
|
| InvalidHexValueErrorType
|
|
37
39
|
| RlpDepthLimitExceededErrorType
|
|
40
|
+
| RlpNonCanonicalErrorType
|
|
38
41
|
| RlpTrailingBytesErrorType
|
|
39
42
|
| ErrorType
|
|
40
43
|
|
|
@@ -91,6 +94,11 @@ function fromRlpCursor<to extends To = 'hex'>(
|
|
|
91
94
|
if (prefix < 0xc0) {
|
|
92
95
|
const length = readLength(cursor, prefix, 0x80)
|
|
93
96
|
const bytes = cursor.readBytes(length)
|
|
97
|
+
// A single byte below `0x80` is its own encoding (Yellow Paper, Appendix B).
|
|
98
|
+
if (prefix >= 0x80 && length === 1 && bytes[0]! < 0x80)
|
|
99
|
+
throw new RlpNonCanonicalError({
|
|
100
|
+
reason: 'single byte below `0x80` has a length prefix',
|
|
101
|
+
})
|
|
94
102
|
return (to === 'hex' ? bytesToHex(bytes) : bytes) as FromRlpReturnType<to>
|
|
95
103
|
}
|
|
96
104
|
|
|
@@ -104,18 +112,42 @@ function fromRlpCursor<to extends To = 'hex'>(
|
|
|
104
112
|
) as {} as FromRlpReturnType<to>
|
|
105
113
|
}
|
|
106
114
|
|
|
107
|
-
type ReadLengthErrorType = BaseErrorType | ErrorType
|
|
115
|
+
type ReadLengthErrorType = BaseErrorType | RlpNonCanonicalErrorType | ErrorType
|
|
108
116
|
|
|
109
117
|
function readLength(cursor: Cursor, prefix: number, offset: number) {
|
|
110
118
|
if (offset === 0x80 && prefix < 0x80) return 1
|
|
111
119
|
if (prefix <= offset + 55) return prefix - offset
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (prefix === offset + 55 +
|
|
115
|
-
|
|
120
|
+
// Long-form lengths must be above 55 and have no leading zero bytes
|
|
121
|
+
// (Yellow Paper, Appendix B).
|
|
122
|
+
if (prefix === offset + 55 + 1) {
|
|
123
|
+
const length = cursor.readUint8()
|
|
124
|
+
if (length <= 55) throw nonCanonicalLengthError(length)
|
|
125
|
+
return length
|
|
126
|
+
}
|
|
127
|
+
if (prefix === offset + 55 + 2) {
|
|
128
|
+
const length = cursor.readUint16()
|
|
129
|
+
if (length <= 0xff) throw nonCanonicalLengthError(length)
|
|
130
|
+
return length
|
|
131
|
+
}
|
|
132
|
+
if (prefix === offset + 55 + 3) {
|
|
133
|
+
const length = cursor.readUint24()
|
|
134
|
+
if (length <= 0xffff) throw nonCanonicalLengthError(length)
|
|
135
|
+
return length
|
|
136
|
+
}
|
|
137
|
+
if (prefix === offset + 55 + 4) {
|
|
138
|
+
const length = cursor.readUint32()
|
|
139
|
+
if (length <= 0xffffff) throw nonCanonicalLengthError(length)
|
|
140
|
+
return length
|
|
141
|
+
}
|
|
116
142
|
throw new BaseError('Invalid RLP prefix')
|
|
117
143
|
}
|
|
118
144
|
|
|
145
|
+
function nonCanonicalLengthError(length: number) {
|
|
146
|
+
return new RlpNonCanonicalError({
|
|
147
|
+
reason: `length \`${length}\` does not use the shortest length prefix`,
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
119
151
|
type ReadListErrorType = RlpListBoundaryExceededErrorType | ErrorType
|
|
120
152
|
|
|
121
153
|
function readList<to extends To>(
|
package/utils/index.ts
CHANGED
|
@@ -38,6 +38,10 @@ type CreateBatchSchedulerReturnType<
|
|
|
38
38
|
export type CreateBatchSchedulerErrorType = ErrorType
|
|
39
39
|
|
|
40
40
|
const schedulerCache = /*#__PURE__*/ new Map<number | string, SchedulerItem[]>()
|
|
41
|
+
const timerCache = /*#__PURE__*/ new Map<
|
|
42
|
+
number | string,
|
|
43
|
+
ReturnType<typeof setTimeout>
|
|
44
|
+
>()
|
|
41
45
|
|
|
42
46
|
/** @internal */
|
|
43
47
|
export function createBatchScheduler<
|
|
@@ -78,7 +82,11 @@ export function createBatchScheduler<
|
|
|
78
82
|
})
|
|
79
83
|
}
|
|
80
84
|
|
|
81
|
-
const flush = () =>
|
|
85
|
+
const flush = () => {
|
|
86
|
+
clearTimeout(timerCache.get(id))
|
|
87
|
+
timerCache.delete(id)
|
|
88
|
+
schedulerCache.delete(id)
|
|
89
|
+
}
|
|
82
90
|
|
|
83
91
|
const getBatchedArgs = () =>
|
|
84
92
|
getScheduler().map(({ args }) => args) as parameters[]
|
|
@@ -104,7 +112,7 @@ export function createBatchScheduler<
|
|
|
104
112
|
}
|
|
105
113
|
|
|
106
114
|
setScheduler({ args, resolve, reject })
|
|
107
|
-
setTimeout(exec, wait)
|
|
115
|
+
timerCache.set(id, setTimeout(exec, wait))
|
|
108
116
|
return promise
|
|
109
117
|
},
|
|
110
118
|
} as unknown as CreateBatchSchedulerReturnType<parameters, returnType>
|
package/utils/typedData.ts
CHANGED
|
@@ -145,19 +145,17 @@ export function getTypesForEIP712Domain({
|
|
|
145
145
|
}: {
|
|
146
146
|
domain?: TypedDataDomain | undefined
|
|
147
147
|
}): TypedDataParameter[] {
|
|
148
|
+
// A field is part of the schema when it is present on the domain (not
|
|
149
|
+
// `undefined` or `null`).
|
|
148
150
|
return [
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
name: 'chainId',
|
|
154
|
-
type: 'uint256',
|
|
155
|
-
},
|
|
156
|
-
domain?.verifyingContract && {
|
|
151
|
+
domain?.name != null && { name: 'name', type: 'string' },
|
|
152
|
+
domain?.version != null && { name: 'version', type: 'string' },
|
|
153
|
+
domain?.chainId != null && { name: 'chainId', type: 'uint256' },
|
|
154
|
+
domain?.verifyingContract != null && {
|
|
157
155
|
name: 'verifyingContract',
|
|
158
156
|
type: 'address',
|
|
159
157
|
},
|
|
160
|
-
domain?.salt && { name: 'salt', type: 'bytes32' },
|
|
158
|
+
domain?.salt != null && { name: 'salt', type: 'bytes32' },
|
|
161
159
|
].filter(Boolean) as TypedDataParameter[]
|
|
162
160
|
}
|
|
163
161
|
|