ox 1.8.13 → 1.8.15
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 +52 -0
- package/dist/core/AbiError.d.ts +2 -2
- package/dist/core/AbiError.d.ts.map +1 -1
- package/dist/core/AbiError.js +6 -2
- package/dist/core/AbiError.js.map +1 -1
- package/dist/core/AbiEvent.d.ts +71 -1
- package/dist/core/AbiEvent.d.ts.map +1 -1
- package/dist/core/AbiEvent.js +160 -19
- package/dist/core/AbiEvent.js.map +1 -1
- package/dist/core/AbiFunction.d.ts +1 -1
- package/dist/core/AbiFunction.d.ts.map +1 -1
- package/dist/core/AbiFunction.js +3 -5
- package/dist/core/AbiFunction.js.map +1 -1
- package/dist/core/AbiItem.d.ts.map +1 -1
- package/dist/core/AbiItem.js +18 -8
- package/dist/core/AbiItem.js.map +1 -1
- package/dist/core/Base32.d.ts +3 -3
- package/dist/core/Base32.d.ts.map +1 -1
- package/dist/core/Base32.js +5 -2
- package/dist/core/Base32.js.map +1 -1
- package/dist/core/Bytes.d.ts.map +1 -1
- package/dist/core/Bytes.js +4 -3
- package/dist/core/Bytes.js.map +1 -1
- package/dist/core/Caches.d.ts +1 -2
- package/dist/core/Caches.d.ts.map +1 -1
- package/dist/core/Caches.js +1 -4
- package/dist/core/Caches.js.map +1 -1
- package/dist/core/Cbor.d.ts.map +1 -1
- package/dist/core/Cbor.js +16 -2
- package/dist/core/Cbor.js.map +1 -1
- package/dist/core/CompactSize.d.ts +8 -1
- package/dist/core/CompactSize.d.ts.map +1 -1
- package/dist/core/CompactSize.js +9 -0
- package/dist/core/CompactSize.js.map +1 -1
- package/dist/core/Hex.d.ts.map +1 -1
- package/dist/core/Hex.js +9 -4
- package/dist/core/Hex.js.map +1 -1
- package/dist/core/Rlp.d.ts +9 -2
- package/dist/core/Rlp.d.ts.map +1 -1
- package/dist/core/Rlp.js +51 -8
- package/dist/core/Rlp.js.map +1 -1
- package/dist/core/RpcTransport.d.ts.map +1 -1
- package/dist/core/RpcTransport.js +11 -4
- package/dist/core/RpcTransport.js.map +1 -1
- package/dist/core/Siwe.d.ts.map +1 -1
- package/dist/core/Siwe.js +24 -9
- package/dist/core/Siwe.js.map +1 -1
- package/dist/core/TypedData.d.ts.map +1 -1
- package/dist/core/TypedData.js +13 -9
- package/dist/core/TypedData.js.map +1 -1
- package/dist/core/Value.d.ts.map +1 -1
- package/dist/core/Value.js +5 -3
- package/dist/core/Value.js.map +1 -1
- package/dist/core/internal/abiItem.d.ts +3 -1
- package/dist/core/internal/abiItem.d.ts.map +1 -1
- package/dist/core/internal/abiItem.js +5 -3
- package/dist/core/internal/abiItem.js.map +1 -1
- package/dist/core/internal/caches.d.ts +10 -0
- package/dist/core/internal/caches.d.ts.map +1 -0
- package/dist/core/internal/caches.js +10 -0
- package/dist/core/internal/caches.js.map +1 -0
- package/dist/core/internal/frameNonce.d.ts.map +1 -1
- package/dist/core/internal/frameNonce.js +2 -2
- package/dist/core/internal/frameNonce.js.map +1 -1
- package/dist/erc4337/UserOperation.d.ts +15 -2
- package/dist/erc4337/UserOperation.d.ts.map +1 -1
- package/dist/erc4337/UserOperation.js +8 -2
- package/dist/erc4337/UserOperation.js.map +1 -1
- package/dist/erc8021/Attribution.d.ts.map +1 -1
- package/dist/erc8021/Attribution.js +12 -3
- package/dist/erc8021/Attribution.js.map +1 -1
- package/dist/tempo/AccountOperation.d.ts +8 -0
- package/dist/tempo/AccountOperation.d.ts.map +1 -1
- package/dist/tempo/AccountOperation.js +26 -6
- package/dist/tempo/AccountOperation.js.map +1 -1
- package/dist/tempo/SignatureEnvelope.d.ts +4 -4
- package/dist/tempo/SignaturePreAuthorized.d.ts +192 -0
- package/dist/tempo/SignaturePreAuthorized.d.ts.map +1 -0
- package/dist/tempo/SignaturePreAuthorized.js +299 -0
- package/dist/tempo/SignaturePreAuthorized.js.map +1 -0
- package/dist/tempo/Tick.d.ts.map +1 -1
- package/dist/tempo/Tick.js +4 -3
- package/dist/tempo/Tick.js.map +1 -1
- package/dist/tempo/index.d.ts +26 -0
- package/dist/tempo/index.d.ts.map +1 -1
- package/dist/tempo/index.js +26 -0
- package/dist/tempo/index.js.map +1 -1
- package/dist/zod/Address.d.ts.map +1 -1
- package/dist/zod/Address.js +2 -1
- package/dist/zod/Address.js.map +1 -1
- package/dist/zod/FrameRequest.d.ts.map +1 -1
- package/dist/zod/FrameRequest.js +2 -1
- package/dist/zod/FrameRequest.js.map +1 -1
- package/dist/zod/Hex.d.ts.map +1 -1
- package/dist/zod/Hex.js +2 -1
- package/dist/zod/Hex.js.map +1 -1
- package/dist/zod/Solidity.d.ts.map +1 -1
- package/dist/zod/Solidity.js +12 -7
- package/dist/zod/Solidity.js.map +1 -1
- package/dist/zod/TypedData.d.ts +12 -12
- package/package.json +6 -1
- package/src/core/AbiError.ts +33 -22
- package/src/core/AbiEvent.ts +177 -18
- package/src/core/AbiFunction.ts +10 -7
- package/src/core/AbiItem.ts +17 -8
- package/src/core/Base32.ts +8 -3
- package/src/core/Bytes.ts +4 -3
- package/src/core/Caches.ts +1 -6
- package/src/core/Cbor.ts +20 -2
- package/src/core/CompactSize.ts +13 -0
- package/src/core/Hex.ts +10 -5
- package/src/core/Rlp.ts +55 -5
- package/src/core/RpcTransport.ts +11 -4
- package/src/core/Siwe.ts +26 -11
- package/src/core/TypedData.ts +14 -9
- package/src/core/Value.ts +6 -3
- package/src/core/_test/AbiError.test-d.ts +10 -0
- package/src/core/_test/AbiError.test.ts +73 -0
- package/src/core/_test/AbiEvent.fuzz.ts +260 -0
- package/src/core/_test/AbiEvent.snap-d.ts +9 -0
- package/src/core/_test/AbiEvent.test.ts +421 -0
- package/src/core/_test/AbiFunction.snap-d.ts +25 -0
- package/src/core/_test/AbiFunction.test.ts +101 -18
- package/src/core/_test/AbiItem.snap-d.ts +11 -0
- package/src/core/_test/AbiItem.test.ts +155 -1
- package/src/core/_test/Base32.test.ts +16 -0
- package/src/core/_test/Bytes.test.ts +15 -0
- package/src/core/_test/Caches.test.ts +10 -1
- package/src/core/_test/Cbor.test.ts +23 -0
- package/src/core/_test/CompactSize.test.ts +14 -0
- package/src/core/_test/Hex.test.ts +25 -0
- package/src/core/_test/Rlp.test.ts +51 -0
- package/src/core/_test/RpcTransport.test.ts +47 -0
- package/src/core/_test/Siwe.test.ts +28 -0
- package/src/core/_test/TypedData.test.ts +17 -2
- package/src/core/internal/abiItem.ts +16 -7
- package/src/core/internal/caches.ts +12 -0
- package/src/core/internal/frameNonce.ts +3 -4
- package/src/erc4337/UserOperation.ts +30 -3
- package/src/erc4337/_test/UserOperation.test.ts +132 -35
- package/src/erc8021/Attribution.ts +9 -7
- package/src/erc8021/_test/Attribution.test.ts +37 -0
- package/src/tempo/AccountOperation.test.ts +116 -0
- package/src/tempo/AccountOperation.ts +38 -5
- package/src/tempo/AccountSimulation.test.ts +1 -1
- package/src/tempo/SignatureEnvelope.test.ts +12 -0
- package/src/tempo/SignatureEnvelope.ts +4 -4
- package/src/tempo/SignaturePreAuthorized.test-d.ts +29 -0
- package/src/tempo/SignaturePreAuthorized.test.ts +410 -0
- package/src/tempo/SignaturePreAuthorized.ts +370 -0
- package/src/tempo/Tick.test.ts +10 -0
- package/src/tempo/Tick.ts +5 -3
- package/src/tempo/index.ts +26 -0
- package/src/version.ts +1 -1
- package/src/zod/Address.ts +3 -3
- package/src/zod/FrameRequest.ts +3 -4
- package/src/zod/Hex.ts +3 -1
- package/src/zod/Solidity.ts +15 -18
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import * as Errors from '../core/Errors.js'
|
|
2
|
+
import * as Hex from '../core/Hex.js'
|
|
3
|
+
import * as Rlp from '../core/Rlp.js'
|
|
4
|
+
import * as KeyAuthorization from './KeyAuthorization.js'
|
|
5
|
+
import * as SignatureEnvelope from './SignatureEnvelope.js'
|
|
6
|
+
|
|
7
|
+
/** Unwrapped [TIP-1165](https://tips.sh/1165) pre-authorized signature. */
|
|
8
|
+
export type Unwrapped = {
|
|
9
|
+
/** Root key's signed authorization of the access key. */
|
|
10
|
+
keyAuthorization: KeyAuthorization.Signed
|
|
11
|
+
/** V2 keychain signature by the access key. */
|
|
12
|
+
signature: SignatureEnvelope.Keychain
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Wrapped [TIP-1165](https://tips.sh/1165) pre-authorized signature. */
|
|
16
|
+
export type Wrapped = Hex.Hex
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Magic bytes used to identify [TIP-1165](https://tips.sh/1165) pre-authorized signatures.
|
|
20
|
+
*/
|
|
21
|
+
export const magicBytes =
|
|
22
|
+
'0x1165116511651165116511651165116511651165116511651165116511651165' as const
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Asserts that a value is a valid pre-authorized signature.
|
|
26
|
+
*
|
|
27
|
+
* A wrapped value must end with {@link ox#SignaturePreAuthorized.magicBytes} and decode strictly.
|
|
28
|
+
* An unwrapped value must hold a V2 keychain signature with a primitive inner
|
|
29
|
+
* signature, and a signed key authorization.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts twoslash
|
|
33
|
+
* import { SignaturePreAuthorized } from 'ox/tempo'
|
|
34
|
+
*
|
|
35
|
+
* SignaturePreAuthorized.assert('0xdeadbeef')
|
|
36
|
+
* // @error: InvalidWrappedSignatureError: Value `0xdeadbeef` is an invalid pre-authorized signature.
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @param value - The value to assert.
|
|
40
|
+
*/
|
|
41
|
+
export function assert(value: Unwrapped | Wrapped): void {
|
|
42
|
+
if (typeof value === 'string') {
|
|
43
|
+
unwrap(value)
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
if (value === null || typeof value !== 'object')
|
|
47
|
+
throw new InvalidUnwrappedSignatureError({
|
|
48
|
+
reason: 'expected an object',
|
|
49
|
+
value,
|
|
50
|
+
})
|
|
51
|
+
const { keyAuthorization, signature } = value
|
|
52
|
+
assertKeychain(signature, value)
|
|
53
|
+
if (!keyAuthorization || typeof keyAuthorization !== 'object')
|
|
54
|
+
throw new InvalidUnwrappedSignatureError({
|
|
55
|
+
reason: 'missing key authorization',
|
|
56
|
+
value,
|
|
57
|
+
})
|
|
58
|
+
if (!keyAuthorization.signature)
|
|
59
|
+
throw new InvalidUnwrappedSignatureError({
|
|
60
|
+
reason: 'key authorization is not signed',
|
|
61
|
+
value,
|
|
62
|
+
})
|
|
63
|
+
// The discriminants alone don't make an encodable value: require both
|
|
64
|
+
// constituents to serialize.
|
|
65
|
+
const reason = (() => {
|
|
66
|
+
try {
|
|
67
|
+
SignatureEnvelope.assert(signature.inner)
|
|
68
|
+
SignatureEnvelope.serialize(signature)
|
|
69
|
+
} catch {
|
|
70
|
+
return 'malformed keychain signature'
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
SignatureEnvelope.assert(keyAuthorization.signature)
|
|
74
|
+
KeyAuthorization.serialize(keyAuthorization)
|
|
75
|
+
} catch {
|
|
76
|
+
return 'malformed signed key authorization'
|
|
77
|
+
}
|
|
78
|
+
return undefined
|
|
79
|
+
})()
|
|
80
|
+
if (reason) throw new InvalidUnwrappedSignatureError({ reason, value })
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export declare namespace assert {
|
|
84
|
+
type ErrorType =
|
|
85
|
+
| InvalidWrappedSignatureError
|
|
86
|
+
| InvalidUnwrappedSignatureError
|
|
87
|
+
| Errors.GlobalErrorType
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Instantiates a [TIP-1165](https://tips.sh/1165) pre-authorized signature from its wrapped or unwrapped form.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts twoslash
|
|
95
|
+
* // @noErrors
|
|
96
|
+
* import { SignaturePreAuthorized } from 'ox/tempo'
|
|
97
|
+
*
|
|
98
|
+
* // Instantiate from serialized format. // [!code focus]
|
|
99
|
+
* const unwrapped = SignaturePreAuthorized.from('0x...') // [!code focus]
|
|
100
|
+
* // @log: { keyAuthorization: { ... }, signature: { ... } } // [!code focus]
|
|
101
|
+
*
|
|
102
|
+
* // Instantiate from constituent parts. // [!code focus]
|
|
103
|
+
* const unwrapped = SignaturePreAuthorized.from({
|
|
104
|
+
* // [!code focus]
|
|
105
|
+
* keyAuthorization, // [!code focus]
|
|
106
|
+
* signature // [!code focus]
|
|
107
|
+
* }) // [!code focus]
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @param value - Wrapped or unwrapped pre-authorized signature.
|
|
111
|
+
* @returns Unwrapped pre-authorized signature.
|
|
112
|
+
*/
|
|
113
|
+
export function from(value: Unwrapped | Wrapped): Unwrapped {
|
|
114
|
+
if (typeof value === 'string') return unwrap(value)
|
|
115
|
+
assert(value)
|
|
116
|
+
return value
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export declare namespace from {
|
|
120
|
+
type ReturnType = Unwrapped
|
|
121
|
+
|
|
122
|
+
type ErrorType = assert.ErrorType | unwrap.ErrorType | Errors.GlobalErrorType
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Parses a [TIP-1165](https://tips.sh/1165) pre-authorized signature into its keychain signature and signed key authorization.
|
|
127
|
+
*
|
|
128
|
+
* Decoding is strict: the value must end with {@link ox#SignaturePreAuthorized.magicBytes},
|
|
129
|
+
* and the rest must be one canonical RLP list of exactly two items with no trailing bytes.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts twoslash
|
|
133
|
+
* // @noErrors
|
|
134
|
+
* import { SignaturePreAuthorized } from 'ox/tempo'
|
|
135
|
+
*
|
|
136
|
+
* const { keyAuthorization, signature } =
|
|
137
|
+
* SignaturePreAuthorized.unwrap('0x...')
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @param wrapped - Wrapped pre-authorized signature to parse.
|
|
141
|
+
* @returns Unwrapped pre-authorized signature.
|
|
142
|
+
*/
|
|
143
|
+
export function unwrap(wrapped: Wrapped): Unwrapped {
|
|
144
|
+
if (
|
|
145
|
+
typeof wrapped !== 'string' ||
|
|
146
|
+
!Hex.validate(wrapped, { even: true, strict: true }) ||
|
|
147
|
+
Hex.size(wrapped) <= Hex.size(magicBytes) ||
|
|
148
|
+
Hex.slice(wrapped, -Hex.size(magicBytes)).toLowerCase() !== magicBytes
|
|
149
|
+
)
|
|
150
|
+
throw new InvalidWrappedSignatureError(wrapped)
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const body = Hex.slice(wrapped, 0, -Hex.size(magicBytes))
|
|
154
|
+
const decoded = Rlp.toHex(body)
|
|
155
|
+
if (!Array.isArray(decoded) || decoded.length !== 2)
|
|
156
|
+
throw new Errors.BaseError('expected an RLP list of exactly two items')
|
|
157
|
+
// The RLP decoder ignores trailing input, so require the list to span
|
|
158
|
+
// the whole body.
|
|
159
|
+
if (Rlp.fromHex(decoded).toLowerCase() !== body.toLowerCase())
|
|
160
|
+
throw new Errors.BaseError('unexpected bytes after the RLP list')
|
|
161
|
+
const [signatureSerialized, keyAuthorizationTuple] = decoded
|
|
162
|
+
if (
|
|
163
|
+
typeof signatureSerialized !== 'string' ||
|
|
164
|
+
!Hex.validate(signatureSerialized)
|
|
165
|
+
)
|
|
166
|
+
throw new Errors.BaseError('keychain signature is not an RLP string')
|
|
167
|
+
if (!Array.isArray(keyAuthorizationTuple))
|
|
168
|
+
throw new Errors.BaseError('signed key authorization is not an RLP list')
|
|
169
|
+
if (keyAuthorizationTuple.length !== 2)
|
|
170
|
+
throw new Errors.BaseError('key authorization is not signed')
|
|
171
|
+
|
|
172
|
+
const signature = SignatureEnvelope.deserialize(signatureSerialized)
|
|
173
|
+
assertKeychain(signature, wrapped)
|
|
174
|
+
// Deserialization normalizes some inputs (such as a trailing envelope
|
|
175
|
+
// magic). Require the exact bytes back so the result encodes the input.
|
|
176
|
+
if (
|
|
177
|
+
SignatureEnvelope.serialize(signature).toLowerCase() !==
|
|
178
|
+
signatureSerialized.toLowerCase()
|
|
179
|
+
)
|
|
180
|
+
throw new Errors.BaseError('keychain signature is not canonical')
|
|
181
|
+
|
|
182
|
+
const keyAuthorization = KeyAuthorization.fromTuple(
|
|
183
|
+
keyAuthorizationTuple as unknown as KeyAuthorization.Tuple<true>,
|
|
184
|
+
) as KeyAuthorization.Signed
|
|
185
|
+
if (
|
|
186
|
+
KeyAuthorization.serialize(keyAuthorization).toLowerCase() !==
|
|
187
|
+
Rlp.fromHex(keyAuthorizationTuple).toLowerCase()
|
|
188
|
+
)
|
|
189
|
+
throw new Errors.BaseError('signed key authorization is not canonical')
|
|
190
|
+
|
|
191
|
+
return { keyAuthorization, signature }
|
|
192
|
+
} catch (error) {
|
|
193
|
+
if (error instanceof InvalidWrappedSignatureError) throw error
|
|
194
|
+
throw new InvalidWrappedSignatureError(wrapped, { cause: error as Error })
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export declare namespace unwrap {
|
|
199
|
+
type ErrorType = InvalidWrappedSignatureError | Errors.GlobalErrorType
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Serializes a [TIP-1165](https://tips.sh/1165) pre-authorized signature:
|
|
204
|
+
* `rlp([keychain_signature, signed_key_authorization]) ‖ magic`.
|
|
205
|
+
*
|
|
206
|
+
* The signature must be a V2 keychain signature whose inner signature is
|
|
207
|
+
* secp256k1, P256, or WebAuthn.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```ts twoslash
|
|
211
|
+
* import { Hash, Hex, Secp256k1 } from 'ox'
|
|
212
|
+
* import {
|
|
213
|
+
* KeyAuthorization,
|
|
214
|
+
* SignatureEnvelope,
|
|
215
|
+
* SignaturePreAuthorized
|
|
216
|
+
* } from 'ox/tempo'
|
|
217
|
+
*
|
|
218
|
+
* const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
|
|
219
|
+
* const accessKey = Secp256k1.randomPrivateKey()
|
|
220
|
+
* const rootKey = Secp256k1.randomPrivateKey()
|
|
221
|
+
*
|
|
222
|
+
* const authorization = KeyAuthorization.from({
|
|
223
|
+
* address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
|
|
224
|
+
* chainId: 4217n,
|
|
225
|
+
* type: 'secp256k1'
|
|
226
|
+
* })
|
|
227
|
+
* const keyAuthorization = KeyAuthorization.from(
|
|
228
|
+
* authorization,
|
|
229
|
+
* {
|
|
230
|
+
* signature: SignatureEnvelope.from(
|
|
231
|
+
* Secp256k1.sign({
|
|
232
|
+
* payload:
|
|
233
|
+
* KeyAuthorization.getSignPayload(authorization),
|
|
234
|
+
* privateKey: rootKey
|
|
235
|
+
* })
|
|
236
|
+
* )
|
|
237
|
+
* }
|
|
238
|
+
* )
|
|
239
|
+
*
|
|
240
|
+
* const hash = Hash.keccak256(Hex.fromString('hello'))
|
|
241
|
+
* const signature = SignatureEnvelope.from({
|
|
242
|
+
* inner: SignatureEnvelope.from(
|
|
243
|
+
* Secp256k1.sign({
|
|
244
|
+
* payload: Hash.keccak256(
|
|
245
|
+
* Hex.concat('0x04', hash, account)
|
|
246
|
+
* ),
|
|
247
|
+
* privateKey: accessKey
|
|
248
|
+
* })
|
|
249
|
+
* ),
|
|
250
|
+
* userAddress: account
|
|
251
|
+
* })
|
|
252
|
+
*
|
|
253
|
+
* const wrapped = SignaturePreAuthorized.wrap({
|
|
254
|
+
* // [!code focus]
|
|
255
|
+
* keyAuthorization, // [!code focus]
|
|
256
|
+
* signature // [!code focus]
|
|
257
|
+
* }) // [!code focus]
|
|
258
|
+
* ```
|
|
259
|
+
*
|
|
260
|
+
* @param value - Unwrapped pre-authorized signature.
|
|
261
|
+
* @returns Wrapped pre-authorized signature.
|
|
262
|
+
*/
|
|
263
|
+
export function wrap(value: Unwrapped): Wrapped {
|
|
264
|
+
assert(value)
|
|
265
|
+
const { keyAuthorization, signature } = value
|
|
266
|
+
return Hex.concat(
|
|
267
|
+
Rlp.fromHex([
|
|
268
|
+
SignatureEnvelope.serialize(signature),
|
|
269
|
+
KeyAuthorization.toTuple(keyAuthorization) as never,
|
|
270
|
+
]),
|
|
271
|
+
magicBytes,
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export declare namespace wrap {
|
|
276
|
+
type ErrorType =
|
|
277
|
+
| assert.ErrorType
|
|
278
|
+
| SignatureEnvelope.serialize.ErrorType
|
|
279
|
+
| KeyAuthorization.toTuple.ErrorType
|
|
280
|
+
| Rlp.fromHex.ErrorType
|
|
281
|
+
| Hex.concat.ErrorType
|
|
282
|
+
| Errors.GlobalErrorType
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Validates a wrapped pre-authorized signature. Returns `true` if it decodes strictly, `false` otherwise.
|
|
287
|
+
*
|
|
288
|
+
* This checks the encoding only. It does not verify either signature or read chain state.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```ts twoslash
|
|
292
|
+
* import { SignaturePreAuthorized } from 'ox/tempo'
|
|
293
|
+
*
|
|
294
|
+
* const valid = SignaturePreAuthorized.validate('0xdeadbeef')
|
|
295
|
+
* // @log: false
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
298
|
+
* @param wrapped - The wrapped signature to validate.
|
|
299
|
+
* @returns `true` if the wrapped signature is valid, `false` otherwise.
|
|
300
|
+
*/
|
|
301
|
+
export function validate(wrapped: Wrapped): boolean {
|
|
302
|
+
try {
|
|
303
|
+
unwrap(wrapped)
|
|
304
|
+
return true
|
|
305
|
+
} catch {
|
|
306
|
+
return false
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export declare namespace validate {
|
|
311
|
+
type ErrorType = Errors.GlobalErrorType
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function assertKeychain(
|
|
315
|
+
signature: SignatureEnvelope.SignatureEnvelope | undefined,
|
|
316
|
+
value: unknown,
|
|
317
|
+
): asserts signature is SignatureEnvelope.Keychain {
|
|
318
|
+
const reason = (() => {
|
|
319
|
+
if (!signature || typeof signature !== 'object')
|
|
320
|
+
return 'missing keychain signature'
|
|
321
|
+
if (signature.type !== 'keychain')
|
|
322
|
+
return `expected a keychain signature, got \`${signature.type}\``
|
|
323
|
+
if (signature.version === 'v1')
|
|
324
|
+
return 'keychain V1 signatures are not supported'
|
|
325
|
+
const type = (() => {
|
|
326
|
+
try {
|
|
327
|
+
return SignatureEnvelope.getType(signature.inner)
|
|
328
|
+
} catch {
|
|
329
|
+
return undefined
|
|
330
|
+
}
|
|
331
|
+
})()
|
|
332
|
+
if (type !== 'secp256k1' && type !== 'p256' && type !== 'webAuthn')
|
|
333
|
+
return `expected a secp256k1, p256, or webAuthn inner signature, got \`${type}\``
|
|
334
|
+
return undefined
|
|
335
|
+
})()
|
|
336
|
+
if (!reason) return
|
|
337
|
+
if (typeof value === 'string')
|
|
338
|
+
throw new InvalidWrappedSignatureError(value, {
|
|
339
|
+
cause: new Errors.BaseError(reason),
|
|
340
|
+
})
|
|
341
|
+
throw new InvalidUnwrappedSignatureError({ reason, value })
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Thrown when a wrapped pre-authorized signature is invalid. */
|
|
345
|
+
export class InvalidWrappedSignatureError extends Errors.BaseError<
|
|
346
|
+
Error | undefined
|
|
347
|
+
> {
|
|
348
|
+
override readonly name = 'SignaturePreAuthorized.InvalidWrappedSignatureError'
|
|
349
|
+
|
|
350
|
+
constructor(wrapped: unknown, options: { cause?: Error | undefined } = {}) {
|
|
351
|
+
super(
|
|
352
|
+
`Value \`${wrapped}\` is an invalid pre-authorized signature.`,
|
|
353
|
+
options,
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Thrown when an unwrapped pre-authorized signature is malformed. */
|
|
359
|
+
export class InvalidUnwrappedSignatureError extends Errors.BaseError {
|
|
360
|
+
override readonly name =
|
|
361
|
+
'SignaturePreAuthorized.InvalidUnwrappedSignatureError'
|
|
362
|
+
|
|
363
|
+
/** The malformed value. */
|
|
364
|
+
readonly value: unknown
|
|
365
|
+
|
|
366
|
+
constructor({ reason, value }: { reason: string; value: unknown }) {
|
|
367
|
+
super(`Value is an invalid unwrapped pre-authorized signature: ${reason}.`)
|
|
368
|
+
this.value = value
|
|
369
|
+
}
|
|
370
|
+
}
|
package/src/tempo/Tick.test.ts
CHANGED
|
@@ -115,6 +115,16 @@ describe('fromPrice', () => {
|
|
|
115
115
|
expect(Tick.fromPrice(' 1.0 ')).toBe(0)
|
|
116
116
|
expect(Tick.fromPrice(' 1.001 ')).toBe(100)
|
|
117
117
|
})
|
|
118
|
+
|
|
119
|
+
test('throws error when price is negative', () => {
|
|
120
|
+
expect(() => Tick.fromPrice('-0.999')).toThrowErrorMatchingInlineSnapshot(`
|
|
121
|
+
[Tick.PriceOutOfBoundsError: Price "-0.999" results in tick -199900 which is out of bounds.
|
|
122
|
+
|
|
123
|
+
Tick must be between -2000 and 2000.]
|
|
124
|
+
`)
|
|
125
|
+
expect(() => Tick.fromPrice('-0.98')).toThrow(Tick.PriceOutOfBoundsError)
|
|
126
|
+
expect(() => Tick.fromPrice('-0.99999')).toThrow(Tick.PriceOutOfBoundsError)
|
|
127
|
+
})
|
|
118
128
|
})
|
|
119
129
|
|
|
120
130
|
describe('round-trip conversions', () => {
|
package/src/tempo/Tick.ts
CHANGED
|
@@ -101,14 +101,16 @@ export function fromPrice(price: fromPrice.Price): fromPrice.ReturnType {
|
|
|
101
101
|
throw new InvalidPriceFormatError({ price })
|
|
102
102
|
|
|
103
103
|
// Parse price using string manipulation to avoid float precision issues
|
|
104
|
-
const
|
|
104
|
+
const negative = priceStr.startsWith('-')
|
|
105
|
+
const [w, d = '0'] = (negative ? priceStr.slice(1) : priceStr).split('.')
|
|
105
106
|
const whole = BigInt(w!)
|
|
106
107
|
|
|
107
108
|
// Pad or truncate decimal to exactly 5 digits
|
|
108
109
|
const decimal = BigInt(d.padEnd(5, '0').slice(0, 5))
|
|
109
110
|
|
|
110
|
-
// Calculate price
|
|
111
|
-
const priceInt =
|
|
111
|
+
// Calculate price, applying the sign last since `BigInt('-0')` is `0n`
|
|
112
|
+
const priceInt =
|
|
113
|
+
(negative ? -1n : 1n) * (whole * BigInt(priceScale) + decimal)
|
|
112
114
|
|
|
113
115
|
// Calculate tick
|
|
114
116
|
const tick = Number(priceInt - BigInt(priceScale))
|
package/src/tempo/index.ts
CHANGED
|
@@ -317,6 +317,32 @@ export * as RpcSchemaTempo from './RpcSchemaTempo.js'
|
|
|
317
317
|
* @category Reference
|
|
318
318
|
*/
|
|
319
319
|
export * as SignatureEnvelope from './SignatureEnvelope.js'
|
|
320
|
+
/**
|
|
321
|
+
* Pre-authorized access key signature utilities (TIP-1165).
|
|
322
|
+
*
|
|
323
|
+
* A pre-authorized signature bundles an access key's V2 keychain signature with
|
|
324
|
+
* the root key's signed key authorization, followed by a 32-byte magic suffix.
|
|
325
|
+
* Offchain verifiers use it to accept signatures from an access key that is
|
|
326
|
+
* authorized but not yet registered onchain.
|
|
327
|
+
*
|
|
328
|
+
* [TIP-1165](https://tips.sh/1165)
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* ```ts twoslash
|
|
332
|
+
* // @noErrors
|
|
333
|
+
* import { SignaturePreAuthorized } from 'ox/tempo'
|
|
334
|
+
*
|
|
335
|
+
* const wrapped = SignaturePreAuthorized.wrap({
|
|
336
|
+
* keyAuthorization,
|
|
337
|
+
* signature
|
|
338
|
+
* })
|
|
339
|
+
* const { keyAuthorization, signature } =
|
|
340
|
+
* SignaturePreAuthorized.unwrap(wrapped)
|
|
341
|
+
* ```
|
|
342
|
+
*
|
|
343
|
+
* @category Reference
|
|
344
|
+
*/
|
|
345
|
+
export * as SignaturePreAuthorized from './SignaturePreAuthorized.js'
|
|
320
346
|
/**
|
|
321
347
|
* Tick-based pricing utilities for DEX price conversions.
|
|
322
348
|
*
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** @internal */
|
|
2
|
-
export const version = '1.8.
|
|
2
|
+
export const version = '1.8.15'
|
package/src/zod/Address.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/* eslint-disable jsdoc-js/require-jsdoc, jsdoc-js/require-description, jsdoc-js/require-example */
|
|
2
2
|
import * as z from 'zod/mini'
|
|
3
3
|
|
|
4
|
+
const addressRegex = /^0x[0-9a-fA-F]{40}$/
|
|
5
|
+
|
|
4
6
|
/** Address schema. */
|
|
5
7
|
export const Address = z
|
|
6
8
|
.templateLiteral([z.literal('0x'), z.string()])
|
|
7
|
-
.check(
|
|
8
|
-
z.refine((value) => /^0x[0-9a-fA-F]{40}$/.test(value), 'expected address'),
|
|
9
|
-
)
|
|
9
|
+
.check(z.refine((value) => addressRegex.test(value), 'expected address'))
|
package/src/zod/FrameRequest.ts
CHANGED
|
@@ -5,11 +5,10 @@ import * as z_Frame from './Frame.js'
|
|
|
5
5
|
import * as z_Hex from './Hex.js'
|
|
6
6
|
import * as z from 'zod/mini'
|
|
7
7
|
|
|
8
|
+
const quantityRegex = /^0x(0|[1-9a-f][0-9a-f]*)$/
|
|
9
|
+
|
|
8
10
|
const quantity = z_Hex.Hex.check(
|
|
9
|
-
z.refine(
|
|
10
|
-
(value) => /^0x(0|[1-9a-f][0-9a-f]*)$/.test(value),
|
|
11
|
-
'Invalid RPC quantity',
|
|
12
|
-
),
|
|
11
|
+
z.refine((value) => quantityRegex.test(value), 'Invalid RPC quantity'),
|
|
13
12
|
)
|
|
14
13
|
|
|
15
14
|
const rpc = z
|
package/src/zod/Hex.ts
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
import * as core_Hex from '../core/Hex.js'
|
|
3
3
|
import * as z from 'zod/mini'
|
|
4
4
|
|
|
5
|
+
const hexRegex = /^0x[0-9a-fA-F]*$/
|
|
6
|
+
|
|
5
7
|
/** Hex string schema. */
|
|
6
8
|
export const Hex = z
|
|
7
9
|
.templateLiteral([z.literal('0x'), z.string()])
|
|
8
|
-
.check(z.refine((value) =>
|
|
10
|
+
.check(z.refine((value) => hexRegex.test(value), 'expected hex'))
|
|
9
11
|
|
|
10
12
|
/** Returns a byte-sized hex string schema. */
|
|
11
13
|
export function sizedHex(size: number) {
|
package/src/zod/Solidity.ts
CHANGED
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
import type * as abitype from 'abitype'
|
|
3
3
|
import * as z from 'zod/mini'
|
|
4
4
|
|
|
5
|
+
const identifierRegex = /^[a-zA-Z$_][a-zA-Z0-9$_]*$/
|
|
6
|
+
const bytesRegex = /^bytes([1-9]|1[0-9]|2[0-9]|3[0-2])?$/
|
|
7
|
+
const intRegex =
|
|
8
|
+
/^u?int(8|16|24|32|40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)?$/
|
|
9
|
+
const arrayWithoutTupleRegex =
|
|
10
|
+
/^(address|bool|function|string|bytes([1-9]|1[0-9]|2[0-9]|3[0-2])?|u?int(8|16|24|32|40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)?)(\[[0-9]{0,}\])+$/
|
|
11
|
+
const arrayWithTupleRegex = /^tuple(\[[0-9]{0,}\])+$/
|
|
12
|
+
|
|
5
13
|
export const Identifier = z
|
|
6
14
|
.string()
|
|
7
15
|
.check(
|
|
8
|
-
z.refine(
|
|
9
|
-
(value) => /^[a-zA-Z$_][a-zA-Z0-9$_]*$/.test(value),
|
|
10
|
-
'expected identifier',
|
|
11
|
-
),
|
|
16
|
+
z.refine((value) => identifierRegex.test(value), 'expected identifier'),
|
|
12
17
|
)
|
|
13
18
|
|
|
14
19
|
export const Address = z.literal('address')
|
|
@@ -17,8 +22,7 @@ export const Bytes = z
|
|
|
17
22
|
.string()
|
|
18
23
|
.check(
|
|
19
24
|
z.refine(
|
|
20
|
-
(value): value is abitype.SolidityBytes =>
|
|
21
|
-
/^bytes([1-9]|1[0-9]|2[0-9]|3[0-2])?$/.test(value),
|
|
25
|
+
(value): value is abitype.SolidityBytes => bytesRegex.test(value),
|
|
22
26
|
'expected Solidity bytes type',
|
|
23
27
|
),
|
|
24
28
|
)
|
|
@@ -29,10 +33,7 @@ export const Int = z
|
|
|
29
33
|
.string()
|
|
30
34
|
.check(
|
|
31
35
|
z.refine(
|
|
32
|
-
(value): value is abitype.SolidityInt =>
|
|
33
|
-
/^u?int(8|16|24|32|40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)?$/.test(
|
|
34
|
-
value,
|
|
35
|
-
),
|
|
36
|
+
(value): value is abitype.SolidityInt => intRegex.test(value),
|
|
36
37
|
'expected Solidity integer type',
|
|
37
38
|
),
|
|
38
39
|
)
|
|
@@ -41,9 +42,7 @@ export const ArrayWithoutTuple = z
|
|
|
41
42
|
.check(
|
|
42
43
|
z.refine(
|
|
43
44
|
(value): value is abitype.SolidityArrayWithoutTuple =>
|
|
44
|
-
|
|
45
|
-
value,
|
|
46
|
-
),
|
|
45
|
+
arrayWithoutTupleRegex.test(value),
|
|
47
46
|
'expected Solidity array type',
|
|
48
47
|
),
|
|
49
48
|
)
|
|
@@ -52,7 +51,7 @@ export const ArrayWithTuple = z
|
|
|
52
51
|
.check(
|
|
53
52
|
z.refine(
|
|
54
53
|
(value): value is abitype.SolidityArrayWithTuple =>
|
|
55
|
-
|
|
54
|
+
arrayWithTupleRegex.test(value),
|
|
56
55
|
'expected Solidity tuple array type',
|
|
57
56
|
),
|
|
58
57
|
)
|
|
@@ -75,9 +74,7 @@ export function isType(type: string) {
|
|
|
75
74
|
type === 'bool' ||
|
|
76
75
|
type === 'function' ||
|
|
77
76
|
type === 'string' ||
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
type,
|
|
81
|
-
)
|
|
77
|
+
bytesRegex.test(type) ||
|
|
78
|
+
intRegex.test(type)
|
|
82
79
|
)
|
|
83
80
|
}
|