ox 0.14.65 → 0.14.67
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 +68 -0
- package/_cjs/core/AbiError.js +25 -4
- package/_cjs/core/AbiError.js.map +1 -1
- package/_cjs/core/AbiEvent.js +18 -3
- package/_cjs/core/AbiEvent.js.map +1 -1
- package/_cjs/core/AbiFunction.js +27 -3
- package/_cjs/core/AbiFunction.js.map +1 -1
- package/_cjs/core/Address.js +1 -0
- package/_cjs/core/Address.js.map +1 -1
- package/_cjs/core/Authorization.js +29 -2
- package/_cjs/core/Authorization.js.map +1 -1
- package/_cjs/core/Keystore.js +18 -0
- package/_cjs/core/Keystore.js.map +1 -1
- package/_cjs/core/Secp256k1.js +5 -1
- package/_cjs/core/Secp256k1.js.map +1 -1
- package/_cjs/core/Signature.js +4 -2
- package/_cjs/core/Signature.js.map +1 -1
- package/_cjs/core/Siwe.js +22 -3
- package/_cjs/core/Siwe.js.map +1 -1
- package/_cjs/core/TxEnvelopeEip1559.js +2 -1
- package/_cjs/core/TxEnvelopeEip1559.js.map +1 -1
- package/_cjs/core/TxEnvelopeEip2930.js +2 -1
- package/_cjs/core/TxEnvelopeEip2930.js.map +1 -1
- package/_cjs/core/TxEnvelopeEip7702.js +5 -1
- package/_cjs/core/TxEnvelopeEip7702.js.map +1 -1
- package/_cjs/core/TxEnvelopeLegacy.js +41 -8
- package/_cjs/core/TxEnvelopeLegacy.js.map +1 -1
- package/_cjs/core/TypedData.js +7 -9
- package/_cjs/core/TypedData.js.map +1 -1
- package/_cjs/core/Value.js +8 -3
- package/_cjs/core/Value.js.map +1 -1
- package/_cjs/core/internal/abiParameters.js +6 -2
- package/_cjs/core/internal/abiParameters.js.map +1 -1
- package/_cjs/core/internal/cursor.js +30 -1
- package/_cjs/core/internal/cursor.js.map +1 -1
- package/_cjs/erc4337/UserOperation.js +1 -1
- package/_cjs/erc4337/UserOperation.js.map +1 -1
- package/_cjs/erc7821/Execute.js +41 -3
- package/_cjs/erc7821/Execute.js.map +1 -1
- package/_cjs/tempo/KeyAuthorization.js +22 -1
- package/_cjs/tempo/KeyAuthorization.js.map +1 -1
- package/_cjs/tempo/SignatureEnvelope.js +44 -50
- package/_cjs/tempo/SignatureEnvelope.js.map +1 -1
- package/_cjs/tempo/TxEnvelopeTempo.js +34 -3
- package/_cjs/tempo/TxEnvelopeTempo.js.map +1 -1
- package/_cjs/tempo/internal/webauthnData.js +57 -0
- package/_cjs/tempo/internal/webauthnData.js.map +1 -0
- package/_cjs/version.js +1 -1
- package/_cjs/webauthn/Registration.js +2 -6
- package/_cjs/webauthn/Registration.js.map +1 -1
- package/_esm/core/AbiError.js +64 -3
- package/_esm/core/AbiError.js.map +1 -1
- package/_esm/core/AbiEvent.js +24 -3
- package/_esm/core/AbiEvent.js.map +1 -1
- package/_esm/core/AbiFunction.js +64 -3
- package/_esm/core/AbiFunction.js.map +1 -1
- package/_esm/core/Address.js +17 -1
- package/_esm/core/Address.js.map +1 -1
- package/_esm/core/Authorization.js +47 -2
- package/_esm/core/Authorization.js.map +1 -1
- package/_esm/core/Keystore.js +33 -2
- package/_esm/core/Keystore.js.map +1 -1
- package/_esm/core/Secp256k1.js +5 -1
- package/_esm/core/Secp256k1.js.map +1 -1
- package/_esm/core/Signature.js +4 -2
- package/_esm/core/Signature.js.map +1 -1
- package/_esm/core/Siwe.js +29 -3
- package/_esm/core/Siwe.js.map +1 -1
- package/_esm/core/TxEnvelopeEip1559.js +2 -1
- package/_esm/core/TxEnvelopeEip1559.js.map +1 -1
- package/_esm/core/TxEnvelopeEip2930.js +2 -1
- package/_esm/core/TxEnvelopeEip2930.js.map +1 -1
- package/_esm/core/TxEnvelopeEip7702.js +6 -1
- package/_esm/core/TxEnvelopeEip7702.js.map +1 -1
- package/_esm/core/TxEnvelopeLegacy.js +49 -9
- package/_esm/core/TxEnvelopeLegacy.js.map +1 -1
- package/_esm/core/TypedData.js +9 -9
- package/_esm/core/TypedData.js.map +1 -1
- package/_esm/core/Value.js +14 -3
- package/_esm/core/Value.js.map +1 -1
- package/_esm/core/internal/abiParameters.js +9 -2
- package/_esm/core/internal/abiParameters.js.map +1 -1
- package/_esm/core/internal/cursor.js +32 -0
- package/_esm/core/internal/cursor.js.map +1 -1
- package/_esm/erc4337/UserOperation.js +1 -1
- package/_esm/erc4337/UserOperation.js.map +1 -1
- package/_esm/erc7821/Execute.js +76 -2
- package/_esm/erc7821/Execute.js.map +1 -1
- package/_esm/index.js +1 -1
- package/_esm/tempo/KeyAuthorization.js +21 -0
- package/_esm/tempo/KeyAuthorization.js.map +1 -1
- package/_esm/tempo/SignatureEnvelope.js +53 -54
- package/_esm/tempo/SignatureEnvelope.js.map +1 -1
- package/_esm/tempo/TxEnvelopeTempo.js +56 -2
- package/_esm/tempo/TxEnvelopeTempo.js.map +1 -1
- package/_esm/tempo/internal/webauthnData.js +79 -0
- package/_esm/tempo/internal/webauthnData.js.map +1 -0
- package/_esm/version.js +1 -1
- package/_esm/webauthn/Registration.js +2 -6
- package/_esm/webauthn/Registration.js.map +1 -1
- package/_types/core/AbiError.d.ts +51 -5
- package/_types/core/AbiError.d.ts.map +1 -1
- package/_types/core/AbiEvent.d.ts +4 -2
- package/_types/core/AbiEvent.d.ts.map +1 -1
- package/_types/core/AbiFunction.d.ts +48 -2
- package/_types/core/AbiFunction.d.ts.map +1 -1
- package/_types/core/Address.d.ts +17 -2
- package/_types/core/Address.d.ts.map +1 -1
- package/_types/core/Authorization.d.ts +24 -2
- package/_types/core/Authorization.d.ts.map +1 -1
- package/_types/core/Keystore.d.ts +31 -2
- package/_types/core/Keystore.d.ts.map +1 -1
- package/_types/core/Secp256k1.d.ts.map +1 -1
- package/_types/core/Signature.d.ts +1 -1
- package/_types/core/Signature.d.ts.map +1 -1
- package/_types/core/Siwe.d.ts +4 -0
- package/_types/core/Siwe.d.ts.map +1 -1
- package/_types/core/TxEnvelopeEip1559.d.ts.map +1 -1
- package/_types/core/TxEnvelopeEip2930.d.ts.map +1 -1
- package/_types/core/TxEnvelopeEip7702.d.ts +1 -1
- package/_types/core/TxEnvelopeEip7702.d.ts.map +1 -1
- package/_types/core/TxEnvelopeLegacy.d.ts.map +1 -1
- package/_types/core/TypedData.d.ts +3 -1
- package/_types/core/TypedData.d.ts.map +1 -1
- package/_types/core/Value.d.ts.map +1 -1
- package/_types/core/internal/abiParameters.d.ts +1 -1
- package/_types/core/internal/abiParameters.d.ts.map +1 -1
- package/_types/core/internal/cursor.d.ts +13 -0
- package/_types/core/internal/cursor.d.ts.map +1 -1
- package/_types/erc7821/Execute.d.ts +46 -1
- package/_types/erc7821/Execute.d.ts.map +1 -1
- package/_types/index.d.ts +1 -1
- package/_types/tempo/KeyAuthorization.d.ts +9 -3
- package/_types/tempo/KeyAuthorization.d.ts.map +1 -1
- package/_types/tempo/SignatureEnvelope.d.ts +1 -1
- package/_types/tempo/SignatureEnvelope.d.ts.map +1 -1
- package/_types/tempo/TxEnvelopeTempo.d.ts +30 -1
- package/_types/tempo/TxEnvelopeTempo.d.ts.map +1 -1
- package/_types/tempo/internal/webauthnData.d.ts +26 -0
- package/_types/tempo/internal/webauthnData.d.ts.map +1 -0
- package/_types/version.d.ts +1 -1
- package/_types/webauthn/Registration.d.ts +2 -0
- package/_types/webauthn/Registration.d.ts.map +1 -1
- package/core/AbiError.ts +81 -7
- package/core/AbiEvent.ts +50 -11
- package/core/AbiFunction.ts +76 -4
- package/core/Address.ts +18 -1
- package/core/Authorization.ts +47 -4
- package/core/Keystore.ts +40 -3
- package/core/Secp256k1.ts +5 -1
- package/core/Signature.ts +7 -2
- package/core/Siwe.ts +29 -3
- package/core/TxEnvelopeEip1559.ts +4 -1
- package/core/TxEnvelopeEip2930.ts +4 -1
- package/core/TxEnvelopeEip7702.ts +9 -2
- package/core/TxEnvelopeLegacy.ts +56 -10
- package/core/TypedData.ts +10 -9
- package/core/Value.ts +14 -3
- package/core/internal/abiParameters.ts +8 -2
- package/core/internal/cursor.ts +36 -0
- package/erc4337/UserOperation.ts +1 -1
- package/erc7821/Execute.ts +84 -3
- package/index.ts +1 -1
- package/package.json +1 -1
- package/tempo/KeyAuthorization.test.ts +91 -0
- package/tempo/KeyAuthorization.ts +32 -3
- package/tempo/SignatureEnvelope.test.ts +300 -28
- package/tempo/SignatureEnvelope.ts +62 -54
- package/tempo/TxEnvelopeTempo.test.ts +310 -0
- package/tempo/TxEnvelopeTempo.ts +64 -1
- package/tempo/internal/webauthnData.ts +89 -0
- package/version.ts +1 -1
- package/webauthn/Registration.ts +4 -6
package/core/Authorization.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type * as Address from './Address.js'
|
|
2
|
-
import
|
|
2
|
+
import * as Errors from './Errors.js'
|
|
3
3
|
import * as Hash from './Hash.js'
|
|
4
4
|
import * as Hex from './Hex.js'
|
|
5
5
|
import type { Compute, Mutable, Undefined } from './internal/types.js'
|
|
@@ -183,7 +183,7 @@ export function fromRpc(authorization: Rpc): Signed {
|
|
|
183
183
|
|
|
184
184
|
return {
|
|
185
185
|
address,
|
|
186
|
-
chainId:
|
|
186
|
+
chainId: toChainId(chainId),
|
|
187
187
|
nonce: BigInt(nonce),
|
|
188
188
|
...signature,
|
|
189
189
|
}
|
|
@@ -270,10 +270,14 @@ export declare namespace fromRpcList {
|
|
|
270
270
|
export function fromTuple<const tuple extends Tuple>(
|
|
271
271
|
tuple: tuple,
|
|
272
272
|
): fromTuple.ReturnType<tuple> {
|
|
273
|
+
if (tuple.length !== 3 && tuple.length !== 6)
|
|
274
|
+
throw new InvalidError(
|
|
275
|
+
'Expected [chainId, address, nonce] or [chainId, address, nonce, yParity, r, s].',
|
|
276
|
+
)
|
|
273
277
|
const [chainId, address, nonce, yParity, r, s] = tuple
|
|
274
278
|
let args = {
|
|
275
279
|
address,
|
|
276
|
-
chainId: chainId === '0x' ? 0 :
|
|
280
|
+
chainId: chainId === '0x' ? 0 : toChainId(chainId),
|
|
277
281
|
nonce: nonce === '0x' ? 0n : BigInt(nonce),
|
|
278
282
|
}
|
|
279
283
|
if (yParity && r && s) {
|
|
@@ -296,7 +300,7 @@ export declare namespace fromTuple {
|
|
|
296
300
|
Authorization<authorization extends Tuple<true> ? true : false>
|
|
297
301
|
>
|
|
298
302
|
|
|
299
|
-
type ErrorType = Errors.GlobalErrorType
|
|
303
|
+
type ErrorType = InvalidError | Errors.GlobalErrorType
|
|
300
304
|
}
|
|
301
305
|
|
|
302
306
|
/**
|
|
@@ -646,6 +650,19 @@ export declare namespace toTupleList {
|
|
|
646
650
|
type ErrorType = Errors.GlobalErrorType
|
|
647
651
|
}
|
|
648
652
|
|
|
653
|
+
// Converts an EIP-7702 authorization `chainId` to a number, and asserts that
|
|
654
|
+
// it is a safe integer.
|
|
655
|
+
function toChainId(chainId: Hex.Hex) {
|
|
656
|
+
const value = Number(chainId)
|
|
657
|
+
if (Number.isSafeInteger(value)) return value
|
|
658
|
+
throw new Hex.IntegerOutOfRangeError({
|
|
659
|
+
max: `${Number.MAX_SAFE_INTEGER}`,
|
|
660
|
+
min: '0',
|
|
661
|
+
signed: false,
|
|
662
|
+
value: `${value}`,
|
|
663
|
+
})
|
|
664
|
+
}
|
|
665
|
+
|
|
649
666
|
// Asserts that an EIP-7702 authorization `yParity` fits in one byte.
|
|
650
667
|
function assertYParity(yParity: number) {
|
|
651
668
|
if (Number.isInteger(yParity) && yParity >= 0 && yParity < 256) return
|
|
@@ -657,3 +674,29 @@ function assertYParity(yParity: number) {
|
|
|
657
674
|
value: `${yParity}`,
|
|
658
675
|
})
|
|
659
676
|
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* Thrown when an EIP-7702 Authorization tuple is structurally invalid.
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
682
|
+
* ```ts twoslash
|
|
683
|
+
* // @noErrors
|
|
684
|
+
* import { Authorization } from 'ox'
|
|
685
|
+
*
|
|
686
|
+
* Authorization.fromTuple([
|
|
687
|
+
* '0x1',
|
|
688
|
+
* '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
|
|
689
|
+
* '0x3',
|
|
690
|
+
* '0x1'
|
|
691
|
+
* ])
|
|
692
|
+
* // @error: Authorization.InvalidError: Invalid authorization.
|
|
693
|
+
* // @error: Details: Expected [chainId, address, nonce] or [chainId, address, nonce, yParity, r, s].
|
|
694
|
+
* ```
|
|
695
|
+
*/
|
|
696
|
+
export class InvalidError extends Errors.BaseError {
|
|
697
|
+
override readonly name = 'Authorization.InvalidError'
|
|
698
|
+
|
|
699
|
+
constructor(details: string) {
|
|
700
|
+
super('Invalid authorization.', { details })
|
|
701
|
+
}
|
|
702
|
+
}
|
package/core/Keystore.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
} from '@noble/hashes/scrypt'
|
|
10
10
|
import { sha256 } from '@noble/hashes/sha2'
|
|
11
11
|
import * as Bytes from './Bytes.js'
|
|
12
|
-
import
|
|
12
|
+
import * as Errors from './Errors.js'
|
|
13
13
|
import * as Hash from './Hash.js'
|
|
14
14
|
import type * as Hex from './Hex.js'
|
|
15
15
|
import type { OneOf } from './internal/types.js'
|
|
@@ -92,7 +92,7 @@ export type ScryptDeriveOpts = BaseDeriveOpts<
|
|
|
92
92
|
* ```
|
|
93
93
|
*
|
|
94
94
|
* @param keystore - JSON keystore.
|
|
95
|
-
* @param key - Key to use for decryption.
|
|
95
|
+
* @param key - Key to use for decryption. Must contain at least 32 bytes; the first 32 bytes are used.
|
|
96
96
|
* @param options - Decryption options.
|
|
97
97
|
* @returns Decrypted private key.
|
|
98
98
|
*/
|
|
@@ -103,6 +103,7 @@ export function decrypt<as extends 'Hex' | 'Bytes' = 'Hex'>(
|
|
|
103
103
|
): decrypt.ReturnType<as> {
|
|
104
104
|
const { as = 'Hex' } = options
|
|
105
105
|
const key_ = Bytes.from(typeof key === 'function' ? key() : key)
|
|
106
|
+
if (key_.length < 32) throw new InvalidKeySizeError({ size: key_.length })
|
|
106
107
|
|
|
107
108
|
const encKey = Bytes.slice(key_, 0, 16)
|
|
108
109
|
const macKey = Bytes.slice(key_, 16, 32)
|
|
@@ -131,6 +132,8 @@ export declare namespace decrypt {
|
|
|
131
132
|
type ReturnType<as extends 'Hex' | 'Bytes' = 'Hex' | 'Bytes'> =
|
|
132
133
|
| (as extends 'Hex' ? Hex.Hex : never)
|
|
133
134
|
| (as extends 'Bytes' ? Bytes.Bytes : never)
|
|
135
|
+
|
|
136
|
+
type ErrorType = InvalidKeySizeError | Errors.GlobalErrorType
|
|
134
137
|
}
|
|
135
138
|
|
|
136
139
|
/**
|
|
@@ -175,7 +178,7 @@ export declare namespace decrypt {
|
|
|
175
178
|
* ```
|
|
176
179
|
*
|
|
177
180
|
* @param privateKey - Private key to encrypt.
|
|
178
|
-
* @param key - Key to use for encryption.
|
|
181
|
+
* @param key - Key to use for encryption. Must contain at least 32 bytes; the first 32 bytes are used.
|
|
179
182
|
* @param options - Encryption options.
|
|
180
183
|
* @returns Encrypted keystore.
|
|
181
184
|
*/
|
|
@@ -187,6 +190,7 @@ export function encrypt(
|
|
|
187
190
|
const { id = crypto.randomUUID(), kdf, kdfparams, iv } = options
|
|
188
191
|
|
|
189
192
|
const key_ = Bytes.from(typeof key === 'function' ? key() : key)
|
|
193
|
+
if (key_.length < 32) throw new InvalidKeySizeError({ size: key_.length })
|
|
190
194
|
const value_ = Bytes.from(privateKey)
|
|
191
195
|
|
|
192
196
|
const encKey = Bytes.slice(key_, 0, 16)
|
|
@@ -214,6 +218,8 @@ export declare namespace encrypt {
|
|
|
214
218
|
/** UUID. */
|
|
215
219
|
id?: string | undefined
|
|
216
220
|
}
|
|
221
|
+
|
|
222
|
+
type ErrorType = InvalidKeySizeError | Errors.GlobalErrorType
|
|
217
223
|
}
|
|
218
224
|
|
|
219
225
|
/**
|
|
@@ -521,6 +527,37 @@ export declare namespace toKeyAsync {
|
|
|
521
527
|
}
|
|
522
528
|
}
|
|
523
529
|
|
|
530
|
+
/**
|
|
531
|
+
* Thrown when a key contains fewer than 32 bytes.
|
|
532
|
+
*
|
|
533
|
+
* @example
|
|
534
|
+
* ```ts twoslash
|
|
535
|
+
* import { Keystore } from 'ox'
|
|
536
|
+
*
|
|
537
|
+
* const [, opts] = Keystore.pbkdf2({
|
|
538
|
+
* password: 'testpassword'
|
|
539
|
+
* })
|
|
540
|
+
*
|
|
541
|
+
* Keystore.encrypt('0x01', '0x0123456789abcdef', opts)
|
|
542
|
+
* // @error: Keystore.InvalidKeySizeError: Key must contain at least 32 bytes. Received 8 bytes.
|
|
543
|
+
* ```
|
|
544
|
+
*/
|
|
545
|
+
export class InvalidKeySizeError extends Errors.BaseError {
|
|
546
|
+
override readonly name = 'Keystore.InvalidKeySizeError'
|
|
547
|
+
|
|
548
|
+
constructor(options: InvalidKeySizeError.Options) {
|
|
549
|
+
super(`Key must contain at least 32 bytes. Received ${options.size} bytes.`)
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
export declare namespace InvalidKeySizeError {
|
|
554
|
+
/** Options for `Keystore.InvalidKeySizeError`. */
|
|
555
|
+
type Options = {
|
|
556
|
+
/** Received key size. */
|
|
557
|
+
size: number
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
524
561
|
///////////////////////////////////////////////////////////////////////////
|
|
525
562
|
|
|
526
563
|
/** @internal */
|
package/core/Secp256k1.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { secp256k1 } from '@noble/curves/secp256k1'
|
|
|
2
2
|
import * as Address from './Address.js'
|
|
3
3
|
import * as Bytes from './Bytes.js'
|
|
4
4
|
import * as Errors from './Errors.js'
|
|
5
|
+
import * as Hash from './Hash.js'
|
|
5
6
|
import * as Hex from './Hex.js'
|
|
6
7
|
import * as Entropy from './internal/entropy.js'
|
|
7
8
|
import * as keyDerivation from './internal/keyDerivation.js'
|
|
@@ -502,7 +503,10 @@ export function verify(options: verify.Options): boolean {
|
|
|
502
503
|
if (options.address)
|
|
503
504
|
return Address.isEqual(
|
|
504
505
|
options.address,
|
|
505
|
-
recoverAddress({
|
|
506
|
+
recoverAddress({
|
|
507
|
+
payload: hash === true ? Hash.sha256(payload) : payload,
|
|
508
|
+
signature: options.signature,
|
|
509
|
+
}),
|
|
506
510
|
)
|
|
507
511
|
return secp256k1.verify(
|
|
508
512
|
options.signature,
|
package/core/Signature.ts
CHANGED
|
@@ -72,14 +72,17 @@ export function assert(
|
|
|
72
72
|
throw new MissingPropertiesError({ signature })
|
|
73
73
|
if (typeof signature.s === 'undefined')
|
|
74
74
|
throw new MissingPropertiesError({ signature })
|
|
75
|
-
if (
|
|
75
|
+
if (
|
|
76
|
+
recovered &&
|
|
77
|
+
(signature.yParity === undefined || signature.yParity === null)
|
|
78
|
+
)
|
|
76
79
|
throw new MissingPropertiesError({ signature })
|
|
77
80
|
if (signature.r < 0n || signature.r > Solidity.maxUint256)
|
|
78
81
|
throw new InvalidRError({ value: signature.r })
|
|
79
82
|
if (signature.s < 0n || signature.s > Solidity.maxUint256)
|
|
80
83
|
throw new InvalidSError({ value: signature.s })
|
|
81
84
|
if (
|
|
82
|
-
|
|
85
|
+
signature.yParity !== undefined &&
|
|
83
86
|
signature.yParity !== 0 &&
|
|
84
87
|
signature.yParity !== 1
|
|
85
88
|
)
|
|
@@ -672,6 +675,7 @@ export declare namespace toRpc {
|
|
|
672
675
|
* @returns The {@link ox#Signature.Tuple}.
|
|
673
676
|
*/
|
|
674
677
|
export function toTuple(signature: Signature): Tuple {
|
|
678
|
+
assert(signature, { recovered: true })
|
|
675
679
|
const { r, s, yParity } = signature
|
|
676
680
|
|
|
677
681
|
return [
|
|
@@ -683,6 +687,7 @@ export function toTuple(signature: Signature): Tuple {
|
|
|
683
687
|
|
|
684
688
|
export declare namespace toTuple {
|
|
685
689
|
type ErrorType =
|
|
690
|
+
| assert.ErrorType
|
|
686
691
|
| Hex.trimLeft.ErrorType
|
|
687
692
|
| Hex.fromNumber.ErrorType
|
|
688
693
|
| Errors.GlobalErrorType
|
package/core/Siwe.ts
CHANGED
|
@@ -23,6 +23,9 @@ export const prefixRegex =
|
|
|
23
23
|
export const suffixRegex =
|
|
24
24
|
/(?:URI: (?<uri>.+))\n(?:Version: (?<version>.+))\n(?:Chain ID: (?<chainId>\d+))\n(?:Nonce: (?<nonce>[a-zA-Z0-9]+))\n(?:Issued At: (?<issuedAt>.+))(?:\nExpiration Time: (?<expirationTime>.+))?(?:\nNot Before: (?<notBefore>.+))?(?:\nRequest ID: (?<requestId>.*))?(?:\nResources:(?<resources>(?:\n- .+)*))?/
|
|
25
25
|
|
|
26
|
+
// Matches `suffixRegex` from the start of the input to its end.
|
|
27
|
+
const anchoredSuffixRegex = new RegExp(`^(?:${suffixRegex.source})$`)
|
|
28
|
+
|
|
26
29
|
// URI validation (see `isUri`).
|
|
27
30
|
const uriIllegalCharacterRegex = /[^a-z0-9:/?#[\]@!$&'()*+,;=.\-_~%]/i
|
|
28
31
|
const uriInvalidEscapeRegex = /%[^0-9a-f]/i
|
|
@@ -211,6 +214,15 @@ export function createMessage(value: Message): string {
|
|
|
211
214
|
`Provided value: ${statement}`,
|
|
212
215
|
],
|
|
213
216
|
})
|
|
217
|
+
if (requestId && /[\n\r]/.test(requestId))
|
|
218
|
+
throw new InvalidMessageFieldError({
|
|
219
|
+
field: 'requestId',
|
|
220
|
+
metaMessages: [
|
|
221
|
+
"- Request ID must not include '\\n' or '\\r'.",
|
|
222
|
+
'',
|
|
223
|
+
`Provided value: ${requestId}`,
|
|
224
|
+
],
|
|
225
|
+
})
|
|
214
226
|
}
|
|
215
227
|
|
|
216
228
|
// Construct message
|
|
@@ -341,6 +353,10 @@ function splitUri(value: string) {
|
|
|
341
353
|
/**
|
|
342
354
|
* [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) formatted message into message fields object.
|
|
343
355
|
*
|
|
356
|
+
* The message can be the prefix (up to the statement), the suffix (from `URI`), or both.
|
|
357
|
+
* Returns an empty object when the message has any other text, such as fields that are
|
|
358
|
+
* out of order or text after the last field.
|
|
359
|
+
*
|
|
344
360
|
* @example
|
|
345
361
|
* ```ts twoslash
|
|
346
362
|
* import { Siwe } from 'ox'
|
|
@@ -371,13 +387,23 @@ function splitUri(value: string) {
|
|
|
371
387
|
* @returns Message fields object.
|
|
372
388
|
*/
|
|
373
389
|
export function parseMessage(message: string): ExactPartial<Message> {
|
|
374
|
-
const
|
|
375
|
-
|
|
390
|
+
const prefixMatch = message.match(prefixRegex)
|
|
391
|
+
const { scheme, statement, ...prefix } = (prefixMatch?.groups ?? {}) as {
|
|
376
392
|
address: Address.Address
|
|
377
393
|
domain: string
|
|
378
394
|
scheme?: string
|
|
379
395
|
statement?: string
|
|
380
396
|
}
|
|
397
|
+
// The suffix starts where the prefix ends. Without a statement, the prefix
|
|
398
|
+
// is followed by one more line feed (as `createMessage` emits) or by none.
|
|
399
|
+
const rest = (() => {
|
|
400
|
+
const rest = message.slice(prefixMatch?.[0].length ?? 0)
|
|
401
|
+
if (prefixMatch && statement === undefined && rest.startsWith('\n'))
|
|
402
|
+
return rest.slice(1)
|
|
403
|
+
return rest
|
|
404
|
+
})()
|
|
405
|
+
const suffixMatch = rest.match(anchoredSuffixRegex)
|
|
406
|
+
if (rest && !suffixMatch) return {}
|
|
381
407
|
const {
|
|
382
408
|
chainId,
|
|
383
409
|
expirationTime,
|
|
@@ -386,7 +412,7 @@ export function parseMessage(message: string): ExactPartial<Message> {
|
|
|
386
412
|
requestId,
|
|
387
413
|
resources: resources_,
|
|
388
414
|
...suffix
|
|
389
|
-
} = (
|
|
415
|
+
} = (suffixMatch?.groups ?? {}) as {
|
|
390
416
|
chainId: string
|
|
391
417
|
expirationTime?: string
|
|
392
418
|
issuedAt?: string
|
|
@@ -68,7 +68,10 @@ export type Type = typeof type
|
|
|
68
68
|
*/
|
|
69
69
|
export function assert(envelope: PartialBy<TxEnvelopeEip1559, 'type'>) {
|
|
70
70
|
const { chainId, maxPriorityFeePerGas, maxFeePerGas, to } = envelope
|
|
71
|
-
if (
|
|
71
|
+
if (
|
|
72
|
+
typeof chainId !== 'undefined' &&
|
|
73
|
+
(!Number.isSafeInteger(chainId) || chainId <= 0)
|
|
74
|
+
)
|
|
72
75
|
throw new TransactionEnvelope.InvalidChainIdError({ chainId })
|
|
73
76
|
if (to) Address.assert(to, { strict: false })
|
|
74
77
|
if (maxFeePerGas && BigInt(maxFeePerGas) > 2n ** 256n - 1n)
|
|
@@ -66,7 +66,10 @@ export type Type = typeof type
|
|
|
66
66
|
*/
|
|
67
67
|
export function assert(envelope: PartialBy<TxEnvelopeEip2930, 'type'>) {
|
|
68
68
|
const { chainId, gasPrice, to } = envelope
|
|
69
|
-
if (
|
|
69
|
+
if (
|
|
70
|
+
typeof chainId !== 'undefined' &&
|
|
71
|
+
(!Number.isSafeInteger(chainId) || chainId <= 0)
|
|
72
|
+
)
|
|
70
73
|
throw new TransactionEnvelope.InvalidChainIdError({ chainId })
|
|
71
74
|
if (to) Address.assert(to, { strict: false })
|
|
72
75
|
if (gasPrice && BigInt(gasPrice) > 2n ** 256n - 1n)
|
|
@@ -183,10 +183,17 @@ export function deserialize(
|
|
|
183
183
|
transaction.maxFeePerGas = 0n
|
|
184
184
|
if (accessList!.length !== 0 && accessList !== '0x')
|
|
185
185
|
transaction.accessList = AccessList.fromTupleList(accessList as never)
|
|
186
|
-
if (authorizationList !== '0x')
|
|
186
|
+
if (authorizationList !== '0x') {
|
|
187
|
+
// A transaction holds signed authorizations only.
|
|
188
|
+
for (const tuple of authorizationList as never as readonly unknown[])
|
|
189
|
+
if (!Array.isArray(tuple) || tuple.length !== 6)
|
|
190
|
+
throw new Authorization.InvalidError(
|
|
191
|
+
'Expected [chainId, address, nonce, yParity, r, s].',
|
|
192
|
+
)
|
|
187
193
|
transaction.authorizationList = Authorization.fromTupleList(
|
|
188
194
|
authorizationList as never,
|
|
189
195
|
)
|
|
196
|
+
}
|
|
190
197
|
|
|
191
198
|
const signature =
|
|
192
199
|
r && s && yParity ? Signature.fromTuple([yParity, r, s]) : undefined
|
|
@@ -202,7 +209,7 @@ export function deserialize(
|
|
|
202
209
|
}
|
|
203
210
|
|
|
204
211
|
export declare namespace deserialize {
|
|
205
|
-
type ErrorType = Errors.GlobalErrorType
|
|
212
|
+
type ErrorType = Authorization.InvalidError | Errors.GlobalErrorType
|
|
206
213
|
}
|
|
207
214
|
|
|
208
215
|
/**
|
package/core/TxEnvelopeLegacy.ts
CHANGED
|
@@ -65,7 +65,10 @@ export type Type = typeof type
|
|
|
65
65
|
export function assert(envelope: PartialBy<TxEnvelopeLegacy, 'type'>) {
|
|
66
66
|
const { chainId, gasPrice, to } = envelope
|
|
67
67
|
if (to) Address.assert(to, { strict: false })
|
|
68
|
-
if (
|
|
68
|
+
if (
|
|
69
|
+
typeof chainId !== 'undefined' &&
|
|
70
|
+
(!Number.isSafeInteger(chainId) || chainId <= 0)
|
|
71
|
+
)
|
|
69
72
|
throw new TransactionEnvelope.InvalidChainIdError({ chainId })
|
|
70
73
|
if (gasPrice && BigInt(gasPrice) > 2n ** 256n - 1n)
|
|
71
74
|
throw new TransactionEnvelope.GasPriceTooHighError({ gasPrice })
|
|
@@ -106,7 +109,12 @@ export function deserialize(serialized: Hex.Hex): Compute<TxEnvelopeLegacy> {
|
|
|
106
109
|
const [nonce, gasPrice, gas, to, value, data, chainIdOrV_, r, s] =
|
|
107
110
|
tuple as readonly Hex.Hex[]
|
|
108
111
|
|
|
109
|
-
if (
|
|
112
|
+
if (
|
|
113
|
+
!Array.isArray(tuple) ||
|
|
114
|
+
!(tuple.length === 6 || tuple.length === 9) ||
|
|
115
|
+
// Every field is a byte string, never a nested list.
|
|
116
|
+
tuple.some((item) => typeof item !== 'string')
|
|
117
|
+
)
|
|
110
118
|
throw new TransactionEnvelope.InvalidSerializedError({
|
|
111
119
|
attributes: {
|
|
112
120
|
nonce,
|
|
@@ -136,21 +144,44 @@ export function deserialize(serialized: Hex.Hex): Compute<TxEnvelopeLegacy> {
|
|
|
136
144
|
if (Hex.validate(nonce))
|
|
137
145
|
transaction.nonce = nonce === '0x' ? 0n : BigInt(nonce)
|
|
138
146
|
if (Hex.validate(value) && value !== '0x') transaction.value = BigInt(value)
|
|
147
|
+
// `gasPrice`, `r` and `s` are bounded by byte length, so that an oversized
|
|
148
|
+
// value is rejected before it is converted.
|
|
149
|
+
if (Hex.size(gasPrice as Hex.Hex) > 32)
|
|
150
|
+
throw new TransactionEnvelope.GasPriceTooHighError()
|
|
139
151
|
if (Hex.validate(gasPrice) && gasPrice !== '0x')
|
|
140
152
|
transaction.gasPrice = BigInt(gasPrice)
|
|
141
153
|
|
|
142
|
-
if (tuple.length === 6)
|
|
154
|
+
if (tuple.length === 6) {
|
|
155
|
+
assert(transaction)
|
|
156
|
+
return transaction
|
|
157
|
+
}
|
|
143
158
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
159
|
+
// `chainId` and `yParity` are derived from `v` as a number, so `v` must be a
|
|
160
|
+
// safe integer (which fits in 7 bytes).
|
|
161
|
+
const chainIdOrV = (() => {
|
|
162
|
+
if (Hex.size(chainIdOrV_ as Hex.Hex) > 7) return Number.NaN
|
|
163
|
+
if (Hex.validate(chainIdOrV_) && chainIdOrV_ !== '0x')
|
|
164
|
+
return Number(chainIdOrV_ as Hex.Hex)
|
|
165
|
+
return 0
|
|
166
|
+
})()
|
|
167
|
+
if (!Number.isSafeInteger(chainIdOrV))
|
|
168
|
+
throw new TransactionEnvelope.InvalidSerializedError({
|
|
169
|
+
attributes: {},
|
|
170
|
+
serialized,
|
|
171
|
+
type,
|
|
172
|
+
})
|
|
148
173
|
|
|
149
174
|
if (s === '0x' && r === '0x') {
|
|
150
175
|
if (chainIdOrV > 0) transaction.chainId = Number(chainIdOrV)
|
|
176
|
+
assert(transaction)
|
|
151
177
|
return transaction
|
|
152
178
|
}
|
|
153
179
|
|
|
180
|
+
if (Hex.size(r as Hex.Hex) > 32)
|
|
181
|
+
throw new Signature.InvalidRError({ value: r })
|
|
182
|
+
if (Hex.size(s as Hex.Hex) > 32)
|
|
183
|
+
throw new Signature.InvalidSError({ value: s })
|
|
184
|
+
|
|
154
185
|
const v = chainIdOrV
|
|
155
186
|
const chainId: number | undefined = Math.floor((v - 35) / 2)
|
|
156
187
|
if (chainId > 0) transaction.chainId = chainId
|
|
@@ -380,11 +411,18 @@ export function hash<presign extends boolean = false>(
|
|
|
380
411
|
options: hash.Options<presign> = {},
|
|
381
412
|
): hash.ReturnType {
|
|
382
413
|
const { presign } = options
|
|
414
|
+
const { chainId, v } = envelope
|
|
383
415
|
return Hash.keccak256(
|
|
384
416
|
serialize({
|
|
385
417
|
...envelope,
|
|
386
418
|
...(presign
|
|
387
419
|
? {
|
|
420
|
+
// An EIP-155 `v` carries the chain ID when `chainId` is absent.
|
|
421
|
+
chainId:
|
|
422
|
+
chainId ??
|
|
423
|
+
(typeof v === 'number' && v >= 37
|
|
424
|
+
? Math.floor((v - 35) / 2)
|
|
425
|
+
: undefined),
|
|
388
426
|
r: undefined,
|
|
389
427
|
s: undefined,
|
|
390
428
|
yParity: undefined,
|
|
@@ -495,6 +533,17 @@ export function serialize(
|
|
|
495
533
|
|
|
496
534
|
if (signature) {
|
|
497
535
|
const v = (() => {
|
|
536
|
+
// EIP-155 (explicit chainId)
|
|
537
|
+
if (chainId > 0) {
|
|
538
|
+
if (signature.v >= 35) {
|
|
539
|
+
// `v` must encode the same chain as `chainId`.
|
|
540
|
+
if (Math.floor((signature.v - 35) / 2) !== chainId)
|
|
541
|
+
throw new TransactionEnvelope.InvalidChainIdError({ chainId })
|
|
542
|
+
return signature.v
|
|
543
|
+
}
|
|
544
|
+
return chainId * 2 + 35 + Signature.vToYParity(signature.v)
|
|
545
|
+
}
|
|
546
|
+
|
|
498
547
|
// EIP-155 (inferred chainId)
|
|
499
548
|
if (signature.v >= 35) {
|
|
500
549
|
const inferredChainId = Math.floor((signature.v - 35) / 2)
|
|
@@ -502,9 +551,6 @@ export function serialize(
|
|
|
502
551
|
return 27 + (signature.v === 35 ? 0 : 1)
|
|
503
552
|
}
|
|
504
553
|
|
|
505
|
-
// EIP-155 (explicit chainId)
|
|
506
|
-
if (chainId > 0) return chainId * 2 + 35 + signature.v - 27
|
|
507
|
-
|
|
508
554
|
// Pre-EIP-155 (no chainId)
|
|
509
555
|
const v = 27 + (signature.v === 27 ? 0 : 1)
|
|
510
556
|
if (signature.v !== v)
|
package/core/TypedData.ts
CHANGED
|
@@ -146,6 +146,8 @@ export function assert<
|
|
|
146
146
|
expectedSize: Number.parseInt(size, 10),
|
|
147
147
|
givenSize: Hex.size(value as Hex.Hex),
|
|
148
148
|
})
|
|
149
|
+
if (size && (value as Hex.Hex).length % 2 !== 0)
|
|
150
|
+
throw new Hex.InvalidLengthError(value as Hex.Hex)
|
|
149
151
|
}
|
|
150
152
|
|
|
151
153
|
if (Object.hasOwn(types, type))
|
|
@@ -184,6 +186,7 @@ export declare namespace assert {
|
|
|
184
186
|
| InvalidPrimaryTypeError
|
|
185
187
|
| InvalidStructTypeError
|
|
186
188
|
| InvalidTypeError
|
|
189
|
+
| Hex.InvalidLengthError
|
|
187
190
|
| Hex.fromNumber.ErrorType
|
|
188
191
|
| Hex.size.ErrorType
|
|
189
192
|
| Errors.GlobalErrorType
|
|
@@ -372,6 +375,8 @@ export declare namespace encodeType {
|
|
|
372
375
|
/**
|
|
373
376
|
* Gets [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema for EIP-721 domain.
|
|
374
377
|
*
|
|
378
|
+
* A field is part of the schema when it is present on the domain (not `undefined` or `null`).
|
|
379
|
+
*
|
|
375
380
|
* @example
|
|
376
381
|
* ```ts twoslash
|
|
377
382
|
* import { TypedData } from 'ox'
|
|
@@ -397,18 +402,14 @@ export function extractEip712DomainTypes(
|
|
|
397
402
|
domain: Domain | undefined,
|
|
398
403
|
): Parameter[] {
|
|
399
404
|
return [
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
name: 'chainId',
|
|
405
|
-
type: 'uint256',
|
|
406
|
-
},
|
|
407
|
-
domain?.verifyingContract && {
|
|
405
|
+
domain?.name != null && { name: 'name', type: 'string' },
|
|
406
|
+
domain?.version != null && { name: 'version', type: 'string' },
|
|
407
|
+
domain?.chainId != null && { name: 'chainId', type: 'uint256' },
|
|
408
|
+
domain?.verifyingContract != null && {
|
|
408
409
|
name: 'verifyingContract',
|
|
409
410
|
type: 'address',
|
|
410
411
|
},
|
|
411
|
-
domain?.salt && { name: 'salt', type: 'bytes32' },
|
|
412
|
+
domain?.salt != null && { name: 'salt', type: 'bytes32' },
|
|
412
413
|
].filter(Boolean) as Parameter[]
|
|
413
414
|
}
|
|
414
415
|
|
package/core/Value.ts
CHANGED
|
@@ -10,7 +10,6 @@ export const exponents = {
|
|
|
10
10
|
} as const
|
|
11
11
|
|
|
12
12
|
const decimalNumberRegex = /^(-?)([0-9]*)\.?([0-9]*)$/
|
|
13
|
-
const trailingZerosRegex = /(0+)$/
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
15
|
* Formats a `bigint` Value to its string representation (divided by the given exponent).
|
|
@@ -39,7 +38,7 @@ export function format(value: bigint, decimals = 0) {
|
|
|
39
38
|
display.slice(0, display.length - decimals),
|
|
40
39
|
display.slice(display.length - decimals),
|
|
41
40
|
]
|
|
42
|
-
fraction = fraction
|
|
41
|
+
fraction = trimTrailingZeros(fraction)
|
|
43
42
|
return `${negative ? '-' : ''}${integer || '0'}${
|
|
44
43
|
fraction ? `.${fraction}` : ''
|
|
45
44
|
}`
|
|
@@ -123,7 +122,7 @@ export function from(value: string, decimals = 0) {
|
|
|
123
122
|
if (negative) integer = integer.slice(1)
|
|
124
123
|
|
|
125
124
|
// trim trailing zeros.
|
|
126
|
-
fraction = fraction
|
|
125
|
+
fraction = trimTrailingZeros(fraction)
|
|
127
126
|
|
|
128
127
|
// round off if the fraction is larger than the number of decimals.
|
|
129
128
|
if (decimals === 0) {
|
|
@@ -159,6 +158,18 @@ export declare namespace from {
|
|
|
159
158
|
type ErrorType = Errors.GlobalErrorType
|
|
160
159
|
}
|
|
161
160
|
|
|
161
|
+
/**
|
|
162
|
+
* Removes trailing zeros with a backward scan, which stays linear in the
|
|
163
|
+
* length of the value.
|
|
164
|
+
*
|
|
165
|
+
* @internal
|
|
166
|
+
*/
|
|
167
|
+
function trimTrailingZeros(value: string): string {
|
|
168
|
+
let end = value.length
|
|
169
|
+
while (end > 0 && value.charCodeAt(end - 1) === 48) end--
|
|
170
|
+
return value.slice(0, end)
|
|
171
|
+
}
|
|
172
|
+
|
|
162
173
|
/**
|
|
163
174
|
* Parses a string representation of Ether to a `bigint` Value (default: wei).
|
|
164
175
|
*
|
|
@@ -280,8 +280,9 @@ export function decodeBytes(
|
|
|
280
280
|
return [Hex.fromBytes(data), 32]
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
-
|
|
284
|
-
|
|
283
|
+
// Read the whole slot so that a slot cut short of its padding is out of bounds.
|
|
284
|
+
const word = cursor.readBytes(32)
|
|
285
|
+
return [Hex.fromBytes(word.subarray(0, Number.parseInt(size, 10))), 32]
|
|
285
286
|
}
|
|
286
287
|
|
|
287
288
|
export declare namespace decodeBytes {
|
|
@@ -664,6 +665,9 @@ export function encodeBytes(
|
|
|
664
665
|
const [, parametersize] = type.split('bytes')
|
|
665
666
|
const bytesSize = Hex.size(value)
|
|
666
667
|
if (!parametersize) {
|
|
668
|
+
// An unpaired nibble has no byte to belong to, so reject it instead of
|
|
669
|
+
// guessing which half of a byte it is.
|
|
670
|
+
if (value.length % 2 !== 0) throw new Hex.InvalidLengthError(value)
|
|
667
671
|
let value_ = value
|
|
668
672
|
// If the size is not divisible by 32 bytes, pad the end
|
|
669
673
|
// with empty bytes to the ceiling 32 bytes.
|
|
@@ -682,12 +686,14 @@ export function encodeBytes(
|
|
|
682
686
|
expectedSize: Number.parseInt(parametersize, 10),
|
|
683
687
|
value,
|
|
684
688
|
})
|
|
689
|
+
if (value.length % 2 !== 0) throw new Hex.InvalidLengthError(value)
|
|
685
690
|
return { dynamic: false, encoded: Hex.padRight(value) }
|
|
686
691
|
}
|
|
687
692
|
|
|
688
693
|
/** @internal */
|
|
689
694
|
export declare namespace encodeBytes {
|
|
690
695
|
type ErrorType =
|
|
696
|
+
| Hex.InvalidLengthError
|
|
691
697
|
| Hex.padLeft.ErrorType
|
|
692
698
|
| Hex.padRight.ErrorType
|
|
693
699
|
| Hex.fromNumber.ErrorType
|