ox 0.14.35 → 0.14.37
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 +12 -0
- package/_cjs/core/Hash.js +1 -1
- package/_cjs/core/Hash.js.map +1 -1
- package/_cjs/core/P256.js +2 -2
- package/_cjs/core/P256.js.map +1 -1
- package/_cjs/core/Secp256k1.js +2 -2
- package/_cjs/core/Secp256k1.js.map +1 -1
- package/_cjs/core/internal/mnemonic.js +18 -0
- package/_cjs/core/internal/mnemonic.js.map +1 -0
- package/_cjs/tempo/MultisigOperation.js +400 -0
- package/_cjs/tempo/MultisigOperation.js.map +1 -0
- package/_cjs/tempo/index.js +2 -1
- package/_cjs/tempo/index.js.map +1 -1
- package/_cjs/version.js +1 -1
- package/_esm/core/Hash.js +1 -1
- package/_esm/core/Hash.js.map +1 -1
- package/_esm/core/P256.js +2 -2
- package/_esm/core/P256.js.map +1 -1
- package/_esm/core/Secp256k1.js +2 -2
- package/_esm/core/Secp256k1.js.map +1 -1
- package/_esm/core/internal/mnemonic.js +16 -0
- package/_esm/core/internal/mnemonic.js.map +1 -0
- package/_esm/tempo/MultisigOperation.js +502 -0
- package/_esm/tempo/MultisigOperation.js.map +1 -0
- package/_esm/tempo/index.js +11 -1
- package/_esm/tempo/index.js.map +1 -1
- package/_esm/version.js +1 -1
- package/_types/core/Secp256k1.d.ts +1 -1
- package/_types/core/Secp256k1.d.ts.map +1 -1
- package/_types/core/internal/mnemonic.d.ts +13 -0
- package/_types/core/internal/mnemonic.d.ts.map +1 -0
- package/_types/tempo/MultisigOperation.d.ts +153 -0
- package/_types/tempo/MultisigOperation.d.ts.map +1 -0
- package/_types/tempo/RpcSchemaTempo.d.ts +35 -0
- package/_types/tempo/RpcSchemaTempo.d.ts.map +1 -1
- package/_types/tempo/index.d.ts +11 -1
- package/_types/tempo/index.d.ts.map +1 -1
- package/_types/version.d.ts +1 -1
- package/core/Hash.ts +1 -1
- package/core/P256.ts +2 -2
- package/core/Secp256k1.ts +3 -2
- package/core/internal/mnemonic.ts +31 -0
- package/package.json +16 -1
- package/tempo/MultisigOperation/package.json +6 -0
- package/tempo/MultisigOperation.test-d/package.json +6 -0
- package/tempo/MultisigOperation.test-d.ts +52 -0
- package/tempo/MultisigOperation.test.ts +1052 -0
- package/tempo/MultisigOperation.ts +720 -0
- package/tempo/RpcSchemaTempo.test-d/package.json +6 -0
- package/tempo/RpcSchemaTempo.test-d.ts +56 -0
- package/tempo/RpcSchemaTempo.ts +37 -0
- package/tempo/index.ts +11 -1
- package/version.ts +1 -1
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
import * as Address from '../core/Address.js'
|
|
2
|
+
import * as Errors from '../core/Errors.js'
|
|
3
|
+
import * as Hash from '../core/Hash.js'
|
|
4
|
+
import * as Hex from '../core/Hex.js'
|
|
5
|
+
import * as KeyAuthorization_ from './KeyAuthorization.js'
|
|
6
|
+
import * as MultisigConfig from './MultisigConfig.js'
|
|
7
|
+
import * as SignatureEnvelope from './SignatureEnvelope.js'
|
|
8
|
+
import * as TxEnvelopeTempo from './TxEnvelopeTempo.js'
|
|
9
|
+
|
|
10
|
+
/** Fields shared by every multisig operation. */
|
|
11
|
+
export type Base<quantity = bigint> = {
|
|
12
|
+
/** Root multisig account. */
|
|
13
|
+
account: Address.Address
|
|
14
|
+
/** Every retained serialized owner approval. */
|
|
15
|
+
approvals: readonly Hex.Hex[]
|
|
16
|
+
/** Root configuration used to verify approvals. */
|
|
17
|
+
config: MultisigConfig.Config
|
|
18
|
+
/** Root configuration version. */
|
|
19
|
+
configVersion: quantity
|
|
20
|
+
/** Unix creation time in milliseconds. */
|
|
21
|
+
createdAt: number
|
|
22
|
+
/** Deterministic multisig operation hash. */
|
|
23
|
+
hash: Hex.Hex
|
|
24
|
+
/** Whether the operation initializes the root multisig account. */
|
|
25
|
+
init: boolean
|
|
26
|
+
/** Number of approvals selected for quorum evaluation. */
|
|
27
|
+
signatureCount: number
|
|
28
|
+
/** Required root owner weight. */
|
|
29
|
+
threshold: number
|
|
30
|
+
/** Unix time of the last update in milliseconds. */
|
|
31
|
+
updatedAt: number
|
|
32
|
+
/** Root owner weight reached by the selected approvals. */
|
|
33
|
+
weight: number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Multisig transaction approval operation. */
|
|
37
|
+
export type TransactionOperation<quantity = bigint> = Base<quantity> & {
|
|
38
|
+
/** Time when another relay may reclaim the submission lease. */
|
|
39
|
+
expiresAt?: number | undefined
|
|
40
|
+
/** Current operation state. */
|
|
41
|
+
status: 'pending' | 'submitting' | 'success'
|
|
42
|
+
/** Fencing token owned by the current submitter. */
|
|
43
|
+
submissionId?: Hex.Hex | undefined
|
|
44
|
+
/** Canonical serialized Tempo envelope without its outer sender signature. */
|
|
45
|
+
transaction: Hex.Hex
|
|
46
|
+
/** Hash returned after the downstream submitter accepts the transaction. */
|
|
47
|
+
transactionHash?: Hex.Hex | undefined
|
|
48
|
+
/** Operation kind. */
|
|
49
|
+
type: 'transaction'
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Multisig key authorization approval operation. */
|
|
53
|
+
export type KeyAuthorizationOperation<quantity = bigint> = Base<quantity> & {
|
|
54
|
+
/** Canonical serialized key authorization. */
|
|
55
|
+
keyAuthorization: Hex.Hex
|
|
56
|
+
/** Current operation state. */
|
|
57
|
+
status: 'pending' | 'success'
|
|
58
|
+
/** Operation kind. */
|
|
59
|
+
type: 'keyAuthorization'
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Transaction or key authorization multisig operation. */
|
|
63
|
+
export type Operation<quantity = bigint> =
|
|
64
|
+
| TransactionOperation<quantity>
|
|
65
|
+
| KeyAuthorizationOperation<quantity>
|
|
66
|
+
|
|
67
|
+
/** JSON-RPC multisig transaction operation. */
|
|
68
|
+
export type TransactionRpc = TransactionOperation<Hex.Hex>
|
|
69
|
+
|
|
70
|
+
/** JSON-RPC multisig key authorization operation. */
|
|
71
|
+
export type KeyAuthorizationRpc = KeyAuthorizationOperation<Hex.Hex>
|
|
72
|
+
|
|
73
|
+
/** JSON-RPC multisig operation. */
|
|
74
|
+
export type Rpc = Operation<Hex.Hex>
|
|
75
|
+
|
|
76
|
+
/** Maximum supported multisig configuration version. */
|
|
77
|
+
const maxConfigVersion = 2n ** 64n - 1n
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Validates and normalizes a multisig operation.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts twoslash
|
|
84
|
+
* // @noErrors
|
|
85
|
+
* import { MultisigOperation } from 'ox/tempo'
|
|
86
|
+
*
|
|
87
|
+
* const operation = MultisigOperation.from(value)
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @param operation - Multisig operation.
|
|
91
|
+
* @returns The validated operation.
|
|
92
|
+
*/
|
|
93
|
+
export function from<const operation extends Operation>(
|
|
94
|
+
operation: operation,
|
|
95
|
+
): from.ReturnValue<operation> {
|
|
96
|
+
try {
|
|
97
|
+
const config = MultisigConfig.from(operation.config)
|
|
98
|
+
if (
|
|
99
|
+
typeof config.threshold !== 'number' ||
|
|
100
|
+
config.owners.some((owner) => typeof owner.weight !== 'number')
|
|
101
|
+
)
|
|
102
|
+
throw new InvalidOperationError({
|
|
103
|
+
reason: 'config threshold and owner weights must be numbers',
|
|
104
|
+
})
|
|
105
|
+
assertBase(operation, config)
|
|
106
|
+
if (operation.type === 'transaction') assertTransaction(operation)
|
|
107
|
+
else if (operation.type === 'keyAuthorization')
|
|
108
|
+
assertKeyAuthorization(operation, config)
|
|
109
|
+
else throw new InvalidOperationError({ reason: 'unknown operation type' })
|
|
110
|
+
return { ...operation, config } as never
|
|
111
|
+
} catch (cause) {
|
|
112
|
+
if (cause instanceof InvalidOperationError) throw cause
|
|
113
|
+
throw new InvalidOperationError({ cause })
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export declare namespace from {
|
|
118
|
+
/** Return type for `from`. */
|
|
119
|
+
export type ReturnValue<operation extends Operation> =
|
|
120
|
+
operation extends TransactionOperation
|
|
121
|
+
? TransactionOperation
|
|
122
|
+
: KeyAuthorizationOperation
|
|
123
|
+
|
|
124
|
+
/** Error type for `from`. */
|
|
125
|
+
export type ErrorType = InvalidOperationError | Errors.GlobalErrorType
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Converts a JSON-RPC multisig operation to its domain representation.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts twoslash
|
|
133
|
+
* // @noErrors
|
|
134
|
+
* import { MultisigOperation } from 'ox/tempo'
|
|
135
|
+
*
|
|
136
|
+
* const operation = MultisigOperation.fromRpc(value)
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @param operation - JSON-RPC multisig operation.
|
|
140
|
+
* @returns The validated operation.
|
|
141
|
+
*/
|
|
142
|
+
export function fromRpc<const operation extends Rpc>(
|
|
143
|
+
operation: operation,
|
|
144
|
+
): fromRpc.ReturnValue<operation> {
|
|
145
|
+
try {
|
|
146
|
+
if (
|
|
147
|
+
typeof operation.configVersion !== 'string' ||
|
|
148
|
+
!Hex.validate(operation.configVersion)
|
|
149
|
+
)
|
|
150
|
+
throw new InvalidOperationError({
|
|
151
|
+
reason: 'configVersion must be a hexadecimal quantity',
|
|
152
|
+
})
|
|
153
|
+
const configVersion = Hex.toBigInt(operation.configVersion)
|
|
154
|
+
if (Hex.fromNumber(configVersion) !== operation.configVersion)
|
|
155
|
+
throw new InvalidOperationError({
|
|
156
|
+
reason: 'configVersion must use canonical quantity encoding',
|
|
157
|
+
})
|
|
158
|
+
return from({ ...operation, configVersion } as Operation) as never
|
|
159
|
+
} catch (cause) {
|
|
160
|
+
if (cause instanceof InvalidOperationError) throw cause
|
|
161
|
+
throw new InvalidOperationError({ cause })
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export declare namespace fromRpc {
|
|
166
|
+
/** Return type for `fromRpc`. */
|
|
167
|
+
export type ReturnValue<operation extends Rpc> =
|
|
168
|
+
operation extends TransactionRpc
|
|
169
|
+
? TransactionOperation
|
|
170
|
+
: KeyAuthorizationOperation
|
|
171
|
+
|
|
172
|
+
/** Error type for `fromRpc`. */
|
|
173
|
+
export type ErrorType = InvalidOperationError | Errors.GlobalErrorType
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Converts a multisig operation to its JSON-RPC representation.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts twoslash
|
|
181
|
+
* // @noErrors
|
|
182
|
+
* import { MultisigOperation } from 'ox/tempo'
|
|
183
|
+
*
|
|
184
|
+
* const operationRpc = MultisigOperation.toRpc(operation)
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* @param operation - Multisig operation.
|
|
188
|
+
* @returns The JSON-RPC operation.
|
|
189
|
+
*/
|
|
190
|
+
export function toRpc<const operation extends Operation>(
|
|
191
|
+
operation: operation,
|
|
192
|
+
): toRpc.ReturnValue<operation> {
|
|
193
|
+
const value = from(operation)
|
|
194
|
+
return {
|
|
195
|
+
...value,
|
|
196
|
+
configVersion: Hex.fromNumber(value.configVersion),
|
|
197
|
+
} as never
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export declare namespace toRpc {
|
|
201
|
+
/** Return type for `toRpc`. */
|
|
202
|
+
export type ReturnValue<operation extends Operation> =
|
|
203
|
+
operation extends TransactionOperation
|
|
204
|
+
? TransactionRpc
|
|
205
|
+
: KeyAuthorizationRpc
|
|
206
|
+
|
|
207
|
+
/** Error type for `toRpc`. */
|
|
208
|
+
export type ErrorType = from.ErrorType | Hex.fromNumber.ErrorType
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Validates fields shared by every operation.
|
|
213
|
+
*
|
|
214
|
+
* @internal
|
|
215
|
+
*/
|
|
216
|
+
function assertBase(operation: Operation, config: MultisigConfig.Config): void {
|
|
217
|
+
if (!Address.validate(operation.account))
|
|
218
|
+
throw new InvalidOperationError({ reason: 'account is invalid' })
|
|
219
|
+
if (Hex.toBigInt(operation.account) === 0n)
|
|
220
|
+
throw new InvalidOperationError({ reason: 'account cannot be zero' })
|
|
221
|
+
if (!Hash.validate(operation.hash))
|
|
222
|
+
throw new InvalidOperationError({ reason: 'hash is invalid' })
|
|
223
|
+
if (typeof operation.init !== 'boolean')
|
|
224
|
+
throw new InvalidOperationError({ reason: 'init must be a boolean' })
|
|
225
|
+
if (
|
|
226
|
+
typeof operation.configVersion !== 'bigint' ||
|
|
227
|
+
operation.configVersion < 0n ||
|
|
228
|
+
operation.configVersion > maxConfigVersion
|
|
229
|
+
)
|
|
230
|
+
throw new InvalidOperationError({
|
|
231
|
+
reason: 'configVersion must be an unsigned 64-bit integer',
|
|
232
|
+
})
|
|
233
|
+
assertInteger(operation.createdAt, 'createdAt')
|
|
234
|
+
assertInteger(operation.updatedAt, 'updatedAt')
|
|
235
|
+
if (operation.updatedAt < operation.createdAt)
|
|
236
|
+
throw new InvalidOperationError({
|
|
237
|
+
reason: 'updatedAt cannot precede createdAt',
|
|
238
|
+
})
|
|
239
|
+
assertInteger(operation.signatureCount, 'signatureCount')
|
|
240
|
+
assertInteger(operation.threshold, 'threshold')
|
|
241
|
+
assertInteger(operation.weight, 'weight')
|
|
242
|
+
if (operation.threshold !== Number(config.threshold))
|
|
243
|
+
throw new InvalidOperationError({
|
|
244
|
+
reason: 'threshold must equal config.threshold',
|
|
245
|
+
})
|
|
246
|
+
if (operation.weight > 0xff)
|
|
247
|
+
throw new InvalidOperationError({ reason: 'weight exceeds u8 max' })
|
|
248
|
+
if (operation.signatureCount > MultisigConfig.maxSignatures)
|
|
249
|
+
throw new InvalidOperationError({ reason: 'too many selected signatures' })
|
|
250
|
+
if (!Array.isArray(operation.approvals))
|
|
251
|
+
throw new InvalidOperationError({ reason: 'approvals must be an array' })
|
|
252
|
+
if (operation.approvals.length > config.owners.length)
|
|
253
|
+
throw new InvalidOperationError({ reason: 'too many retained approvals' })
|
|
254
|
+
if (operation.signatureCount > operation.approvals.length)
|
|
255
|
+
throw new InvalidOperationError({
|
|
256
|
+
reason: 'signatureCount exceeds retained approvals',
|
|
257
|
+
})
|
|
258
|
+
if ((operation.signatureCount === 0) !== (operation.weight === 0))
|
|
259
|
+
throw new InvalidOperationError({
|
|
260
|
+
reason: 'signatureCount and weight must both be zero or nonzero',
|
|
261
|
+
})
|
|
262
|
+
const owners = new Map(
|
|
263
|
+
config.owners.map((owner) => [
|
|
264
|
+
owner.owner.toLowerCase(),
|
|
265
|
+
Number(owner.weight),
|
|
266
|
+
]),
|
|
267
|
+
)
|
|
268
|
+
const approvalWeights: number[] = []
|
|
269
|
+
const seen = new Set<string>()
|
|
270
|
+
for (const approval of operation.approvals) {
|
|
271
|
+
if (
|
|
272
|
+
typeof approval !== 'string' ||
|
|
273
|
+
!Hex.validate(approval, { strict: true })
|
|
274
|
+
)
|
|
275
|
+
throw new InvalidOperationError({ reason: 'approval is invalid' })
|
|
276
|
+
const signature = assertApproval(
|
|
277
|
+
operation.account,
|
|
278
|
+
approval as SignatureEnvelope.Serialized,
|
|
279
|
+
)
|
|
280
|
+
const address = SignatureEnvelope.extractAddress({
|
|
281
|
+
payload: operation.hash,
|
|
282
|
+
signature,
|
|
283
|
+
})
|
|
284
|
+
const key = address.toLowerCase()
|
|
285
|
+
const weight = owners.get(key)
|
|
286
|
+
if (weight === undefined)
|
|
287
|
+
throw new InvalidOperationError({
|
|
288
|
+
reason: 'approval is from a non-owner',
|
|
289
|
+
})
|
|
290
|
+
if (seen.has(key))
|
|
291
|
+
throw new InvalidOperationError({
|
|
292
|
+
reason:
|
|
293
|
+
operation.type === 'keyAuthorization'
|
|
294
|
+
? 'key authorization contains duplicate owner approvals'
|
|
295
|
+
: 'duplicate owner approval',
|
|
296
|
+
})
|
|
297
|
+
seen.add(key)
|
|
298
|
+
approvalWeights.push(weight)
|
|
299
|
+
}
|
|
300
|
+
if (
|
|
301
|
+
!isWeightReachable(
|
|
302
|
+
approvalWeights,
|
|
303
|
+
operation.signatureCount,
|
|
304
|
+
operation.weight,
|
|
305
|
+
)
|
|
306
|
+
)
|
|
307
|
+
throw new InvalidOperationError({
|
|
308
|
+
reason:
|
|
309
|
+
'weight is not reachable by signatureCount retained owner approvals',
|
|
310
|
+
})
|
|
311
|
+
if (operation.init) {
|
|
312
|
+
if (operation.configVersion !== 0n)
|
|
313
|
+
throw new InvalidOperationError({
|
|
314
|
+
reason: 'bootstrap operations must use config version zero',
|
|
315
|
+
})
|
|
316
|
+
if (
|
|
317
|
+
MultisigConfig.getAddress(config).toLowerCase() !==
|
|
318
|
+
operation.account.toLowerCase()
|
|
319
|
+
)
|
|
320
|
+
throw new InvalidOperationError({
|
|
321
|
+
reason: 'bootstrap config does not derive the operation account',
|
|
322
|
+
})
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Validates a transaction operation and its state-specific fields.
|
|
328
|
+
*
|
|
329
|
+
* @internal
|
|
330
|
+
*/
|
|
331
|
+
function assertTransaction(operation: TransactionOperation): void {
|
|
332
|
+
if (
|
|
333
|
+
'keyAuthorization' in operation &&
|
|
334
|
+
operation.keyAuthorization !== undefined
|
|
335
|
+
)
|
|
336
|
+
throw new InvalidOperationError({
|
|
337
|
+
reason: 'transaction operations cannot contain keyAuthorization',
|
|
338
|
+
})
|
|
339
|
+
const expiresAt = operation.expiresAt
|
|
340
|
+
const submissionId = operation.submissionId
|
|
341
|
+
const transactionHash = operation.transactionHash
|
|
342
|
+
if (operation.status === 'pending') {
|
|
343
|
+
if (
|
|
344
|
+
expiresAt !== undefined ||
|
|
345
|
+
submissionId !== undefined ||
|
|
346
|
+
transactionHash !== undefined
|
|
347
|
+
)
|
|
348
|
+
throw new InvalidOperationError({
|
|
349
|
+
reason: 'pending transactions cannot contain submission fields',
|
|
350
|
+
})
|
|
351
|
+
} else if (operation.status === 'submitting') {
|
|
352
|
+
assertInteger(expiresAt, 'expiresAt')
|
|
353
|
+
if (!Hash.validate(submissionId ?? ''))
|
|
354
|
+
throw new InvalidOperationError({ reason: 'submissionId is invalid' })
|
|
355
|
+
if (submissionId!.toLowerCase() === operation.hash.toLowerCase())
|
|
356
|
+
throw new InvalidOperationError({
|
|
357
|
+
reason: 'submissionId must differ from the operation hash',
|
|
358
|
+
})
|
|
359
|
+
if (transactionHash !== undefined)
|
|
360
|
+
throw new InvalidOperationError({
|
|
361
|
+
reason: 'submitting transactions cannot contain transactionHash',
|
|
362
|
+
})
|
|
363
|
+
} else if (operation.status === 'success') {
|
|
364
|
+
if (!Hash.validate(transactionHash ?? ''))
|
|
365
|
+
throw new InvalidOperationError({ reason: 'transactionHash is invalid' })
|
|
366
|
+
if (expiresAt !== undefined || submissionId !== undefined)
|
|
367
|
+
throw new InvalidOperationError({
|
|
368
|
+
reason: 'successful transactions cannot contain submission fields',
|
|
369
|
+
})
|
|
370
|
+
} else
|
|
371
|
+
throw new InvalidOperationError({ reason: 'invalid transaction status' })
|
|
372
|
+
if (
|
|
373
|
+
operation.status !== 'pending' &&
|
|
374
|
+
(operation.weight < operation.threshold || operation.signatureCount === 0)
|
|
375
|
+
)
|
|
376
|
+
throw new InvalidOperationError({
|
|
377
|
+
reason: 'submitted transactions must have quorum',
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
if (typeof operation.transaction !== 'string')
|
|
381
|
+
throw new InvalidOperationError({ reason: 'transaction is invalid' })
|
|
382
|
+
const transaction = TxEnvelopeTempo.deserialize(
|
|
383
|
+
operation.transaction as TxEnvelopeTempo.Serialized,
|
|
384
|
+
)
|
|
385
|
+
if (transaction.signature)
|
|
386
|
+
throw new InvalidOperationError({
|
|
387
|
+
reason: 'transaction must not contain an outer sender signature',
|
|
388
|
+
})
|
|
389
|
+
if (
|
|
390
|
+
transaction.from &&
|
|
391
|
+
transaction.from.toLowerCase() !== operation.account.toLowerCase()
|
|
392
|
+
)
|
|
393
|
+
throw new InvalidOperationError({
|
|
394
|
+
reason: 'transaction sender does not match the operation account',
|
|
395
|
+
})
|
|
396
|
+
assertOperationHash(operation, TxEnvelopeTempo.getSignPayload(transaction))
|
|
397
|
+
const feePayer = operation.transaction.startsWith(
|
|
398
|
+
TxEnvelopeTempo.feePayerMagic,
|
|
399
|
+
)
|
|
400
|
+
const serialized = TxEnvelopeTempo.serialize(
|
|
401
|
+
transaction,
|
|
402
|
+
feePayer
|
|
403
|
+
? transaction.from
|
|
404
|
+
? { format: 'feePayer', sender: transaction.from }
|
|
405
|
+
: { format: 'feePayer' }
|
|
406
|
+
: {},
|
|
407
|
+
)
|
|
408
|
+
if (serialized.toLowerCase() !== operation.transaction.toLowerCase())
|
|
409
|
+
throw new InvalidOperationError({
|
|
410
|
+
reason: 'transaction is not canonically serialized',
|
|
411
|
+
})
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Validates a key authorization operation and its serialized payload.
|
|
416
|
+
*
|
|
417
|
+
* @internal
|
|
418
|
+
*/
|
|
419
|
+
function assertKeyAuthorization(
|
|
420
|
+
operation: KeyAuthorizationOperation,
|
|
421
|
+
config: MultisigConfig.Config,
|
|
422
|
+
): void {
|
|
423
|
+
const transactionFields = operation as KeyAuthorizationOperation & {
|
|
424
|
+
expiresAt?: unknown
|
|
425
|
+
submissionId?: unknown
|
|
426
|
+
transaction?: unknown
|
|
427
|
+
transactionHash?: unknown
|
|
428
|
+
}
|
|
429
|
+
if (
|
|
430
|
+
transactionFields.expiresAt !== undefined ||
|
|
431
|
+
transactionFields.submissionId !== undefined ||
|
|
432
|
+
transactionFields.transaction !== undefined ||
|
|
433
|
+
transactionFields.transactionHash !== undefined
|
|
434
|
+
)
|
|
435
|
+
throw new InvalidOperationError({
|
|
436
|
+
reason: 'key authorization operations cannot contain transaction fields',
|
|
437
|
+
})
|
|
438
|
+
if (operation.status !== 'pending' && operation.status !== 'success')
|
|
439
|
+
throw new InvalidOperationError({
|
|
440
|
+
reason: 'invalid key authorization status',
|
|
441
|
+
})
|
|
442
|
+
if (
|
|
443
|
+
operation.status === 'success' &&
|
|
444
|
+
(operation.weight < operation.threshold || operation.signatureCount === 0)
|
|
445
|
+
)
|
|
446
|
+
throw new InvalidOperationError({
|
|
447
|
+
reason: 'successful key authorizations must have quorum',
|
|
448
|
+
})
|
|
449
|
+
if (operation.status === 'pending' && operation.weight >= operation.threshold)
|
|
450
|
+
throw new InvalidOperationError({
|
|
451
|
+
reason: 'pending key authorizations cannot have quorum',
|
|
452
|
+
})
|
|
453
|
+
if (typeof operation.keyAuthorization !== 'string')
|
|
454
|
+
throw new InvalidOperationError({ reason: 'keyAuthorization is invalid' })
|
|
455
|
+
const authorization = KeyAuthorization_.deserialize(
|
|
456
|
+
operation.keyAuthorization,
|
|
457
|
+
)
|
|
458
|
+
if (
|
|
459
|
+
!authorization.account ||
|
|
460
|
+
authorization.account.toLowerCase() !== operation.account.toLowerCase()
|
|
461
|
+
)
|
|
462
|
+
throw new InvalidOperationError({
|
|
463
|
+
reason: 'key authorization account does not match the operation account',
|
|
464
|
+
})
|
|
465
|
+
const signature = authorization.signature
|
|
466
|
+
if (operation.status === 'pending' && signature)
|
|
467
|
+
throw new InvalidOperationError({
|
|
468
|
+
reason: 'pending key authorizations must be unsigned',
|
|
469
|
+
})
|
|
470
|
+
if (operation.status === 'success') {
|
|
471
|
+
if (signature?.type !== 'multisig')
|
|
472
|
+
throw new InvalidOperationError({
|
|
473
|
+
reason: 'successful key authorizations require a multisig signature',
|
|
474
|
+
})
|
|
475
|
+
if (signature.account.toLowerCase() !== operation.account.toLowerCase())
|
|
476
|
+
throw new InvalidOperationError({
|
|
477
|
+
reason: 'key authorization signature account does not match',
|
|
478
|
+
})
|
|
479
|
+
if (signature.signatures.length !== operation.signatureCount)
|
|
480
|
+
throw new InvalidOperationError({
|
|
481
|
+
reason: 'key authorization signatureCount does not match its signature',
|
|
482
|
+
})
|
|
483
|
+
assertSelectedApprovals(operation, signature.signatures, authorization)
|
|
484
|
+
if (!!signature.init !== operation.init)
|
|
485
|
+
throw new InvalidOperationError({
|
|
486
|
+
reason: 'key authorization bootstrap state does not match',
|
|
487
|
+
})
|
|
488
|
+
if (signature.init && !sameConfig(signature.init, config))
|
|
489
|
+
throw new InvalidOperationError({
|
|
490
|
+
reason: 'key authorization bootstrap config does not match',
|
|
491
|
+
})
|
|
492
|
+
}
|
|
493
|
+
assertOperationHash(
|
|
494
|
+
operation,
|
|
495
|
+
KeyAuthorization_.getSignPayload(authorization),
|
|
496
|
+
)
|
|
497
|
+
if (
|
|
498
|
+
KeyAuthorization_.serialize(authorization).toLowerCase() !==
|
|
499
|
+
operation.keyAuthorization.toLowerCase()
|
|
500
|
+
)
|
|
501
|
+
throw new InvalidOperationError({
|
|
502
|
+
reason: 'keyAuthorization is not canonically serialized',
|
|
503
|
+
})
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Validates a retained signature in the root owner's approval context.
|
|
508
|
+
*
|
|
509
|
+
* @internal
|
|
510
|
+
*/
|
|
511
|
+
function assertApproval(
|
|
512
|
+
account: Address.Address,
|
|
513
|
+
serialized: SignatureEnvelope.Serialized,
|
|
514
|
+
): SignatureEnvelope.SignatureEnvelope {
|
|
515
|
+
const approval = SignatureEnvelope.deserialize(serialized)
|
|
516
|
+
SignatureEnvelope.assert({
|
|
517
|
+
account,
|
|
518
|
+
signatures: [approval],
|
|
519
|
+
type: 'multisig',
|
|
520
|
+
})
|
|
521
|
+
if (
|
|
522
|
+
SignatureEnvelope.serialize(approval).toLowerCase() !==
|
|
523
|
+
serialized.toLowerCase()
|
|
524
|
+
)
|
|
525
|
+
throw new InvalidOperationError({ reason: 'approval is not canonical' })
|
|
526
|
+
return approval
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Checks that a successful key authorization uses retained approvals in canonical order.
|
|
531
|
+
*
|
|
532
|
+
* @internal
|
|
533
|
+
*/
|
|
534
|
+
function assertSelectedApprovals(
|
|
535
|
+
operation: KeyAuthorizationOperation,
|
|
536
|
+
selected: readonly SignatureEnvelope.SignatureEnvelope[],
|
|
537
|
+
authorization: KeyAuthorization_.KeyAuthorization,
|
|
538
|
+
): void {
|
|
539
|
+
const retained = operation.approvals.map((approval) =>
|
|
540
|
+
SignatureEnvelope.deserialize(approval),
|
|
541
|
+
)
|
|
542
|
+
for (const approval of selected) {
|
|
543
|
+
const index = retained.findIndex((candidate) =>
|
|
544
|
+
includesApproval(candidate, approval),
|
|
545
|
+
)
|
|
546
|
+
if (index === -1)
|
|
547
|
+
throw new InvalidOperationError({
|
|
548
|
+
reason: 'key authorization signature is not a retained approval',
|
|
549
|
+
})
|
|
550
|
+
retained.splice(index, 1)
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const digest = MultisigConfig.getSignPayload({
|
|
554
|
+
account: operation.account,
|
|
555
|
+
payload: KeyAuthorization_.getSignPayload(authorization),
|
|
556
|
+
version: operation.configVersion,
|
|
557
|
+
})
|
|
558
|
+
const addresses = selected.map((signature) =>
|
|
559
|
+
SignatureEnvelope.extractAddress({ payload: digest, signature }),
|
|
560
|
+
)
|
|
561
|
+
for (let index = 1; index < addresses.length; index++) {
|
|
562
|
+
const previous = Hex.toBigInt(addresses[index - 1]!)
|
|
563
|
+
const current = Hex.toBigInt(addresses[index]!)
|
|
564
|
+
if (previous === current)
|
|
565
|
+
throw new InvalidOperationError({
|
|
566
|
+
reason: 'key authorization contains duplicate owner approvals',
|
|
567
|
+
})
|
|
568
|
+
if (previous > current)
|
|
569
|
+
throw new InvalidOperationError({
|
|
570
|
+
reason: 'key authorization approvals are not canonically ordered',
|
|
571
|
+
})
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Checks whether a selected approval is contained in a retained approval tree.
|
|
577
|
+
*
|
|
578
|
+
* @internal
|
|
579
|
+
*/
|
|
580
|
+
function includesApproval(
|
|
581
|
+
retained: SignatureEnvelope.SignatureEnvelope,
|
|
582
|
+
selected: SignatureEnvelope.SignatureEnvelope,
|
|
583
|
+
): boolean {
|
|
584
|
+
if (retained.type !== 'multisig' || selected.type !== 'multisig')
|
|
585
|
+
return (
|
|
586
|
+
SignatureEnvelope.serialize(retained).toLowerCase() ===
|
|
587
|
+
SignatureEnvelope.serialize(selected).toLowerCase()
|
|
588
|
+
)
|
|
589
|
+
if (retained.account.toLowerCase() !== selected.account.toLowerCase())
|
|
590
|
+
return false
|
|
591
|
+
// Nested versions are not serialized, so selected child approvals must preserve the validated retained order.
|
|
592
|
+
let index = 0
|
|
593
|
+
for (const approval of selected.signatures) {
|
|
594
|
+
while (
|
|
595
|
+
index < retained.signatures.length &&
|
|
596
|
+
!includesApproval(retained.signatures[index]!, approval)
|
|
597
|
+
)
|
|
598
|
+
index++
|
|
599
|
+
if (index === retained.signatures.length) return false
|
|
600
|
+
index++
|
|
601
|
+
}
|
|
602
|
+
return true
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* Checks whether exactly `signatureCount` retained owners can produce `weight`.
|
|
607
|
+
*
|
|
608
|
+
* @internal
|
|
609
|
+
*/
|
|
610
|
+
function isWeightReachable(
|
|
611
|
+
weights: readonly number[],
|
|
612
|
+
signatureCount: number,
|
|
613
|
+
weight: number,
|
|
614
|
+
): boolean {
|
|
615
|
+
const reachable = Array.from(
|
|
616
|
+
{ length: signatureCount + 1 },
|
|
617
|
+
() => new Set<number>(),
|
|
618
|
+
)
|
|
619
|
+
reachable[0]!.add(0)
|
|
620
|
+
for (const ownerWeight of weights)
|
|
621
|
+
for (let count = signatureCount; count > 0; count--)
|
|
622
|
+
for (const current of reachable[count - 1]!)
|
|
623
|
+
reachable[count]!.add(current + ownerWeight)
|
|
624
|
+
return reachable[signatureCount]!.has(weight)
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Validates the deterministic operation hash.
|
|
629
|
+
*
|
|
630
|
+
* @internal
|
|
631
|
+
*/
|
|
632
|
+
function assertOperationHash(operation: Operation, payload: Hex.Hex): void {
|
|
633
|
+
const hash = MultisigConfig.getSignPayload({
|
|
634
|
+
account: operation.account,
|
|
635
|
+
payload,
|
|
636
|
+
version: operation.configVersion,
|
|
637
|
+
})
|
|
638
|
+
if (hash.toLowerCase() !== operation.hash.toLowerCase())
|
|
639
|
+
throw new InvalidOperationError({
|
|
640
|
+
reason: 'hash does not match the operation payload',
|
|
641
|
+
})
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Validates a nonnegative safe integer field.
|
|
646
|
+
*
|
|
647
|
+
* @internal
|
|
648
|
+
*/
|
|
649
|
+
function assertInteger(value: unknown, field: string): asserts value is number {
|
|
650
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0)
|
|
651
|
+
throw new InvalidOperationError({
|
|
652
|
+
reason: `${field} must be a nonnegative safe integer`,
|
|
653
|
+
})
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Compares normalized multisig configurations.
|
|
658
|
+
*
|
|
659
|
+
* @internal
|
|
660
|
+
*/
|
|
661
|
+
function sameConfig(
|
|
662
|
+
a: MultisigConfig.Config,
|
|
663
|
+
b: MultisigConfig.Config,
|
|
664
|
+
): boolean {
|
|
665
|
+
const configA = MultisigConfig.from(a)
|
|
666
|
+
const configB = MultisigConfig.from(b)
|
|
667
|
+
return (
|
|
668
|
+
Hex.isEqual(
|
|
669
|
+
configA.salt ?? MultisigConfig.zeroSalt,
|
|
670
|
+
configB.salt ?? MultisigConfig.zeroSalt,
|
|
671
|
+
) &&
|
|
672
|
+
configA.threshold === configB.threshold &&
|
|
673
|
+
configA.owners.length === configB.owners.length &&
|
|
674
|
+
configA.owners.every((owner, index) => {
|
|
675
|
+
const other = configB.owners[index]!
|
|
676
|
+
return (
|
|
677
|
+
Address.isEqual(owner.owner, other.owner) &&
|
|
678
|
+
owner.weight === other.weight
|
|
679
|
+
)
|
|
680
|
+
})
|
|
681
|
+
)
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/** Thrown when a multisig operation is malformed or internally inconsistent. */
|
|
685
|
+
export class InvalidOperationError extends Errors.BaseError<Error | undefined> {
|
|
686
|
+
override readonly name = 'MultisigOperation.InvalidOperationError'
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Creates an invalid multisig operation error.
|
|
690
|
+
*
|
|
691
|
+
* @example
|
|
692
|
+
* ```ts twoslash
|
|
693
|
+
* import { MultisigOperation } from 'ox/tempo'
|
|
694
|
+
*
|
|
695
|
+
* throw new MultisigOperation.InvalidOperationError({
|
|
696
|
+
* reason: 'hash does not match the operation payload',
|
|
697
|
+
* })
|
|
698
|
+
* ```
|
|
699
|
+
*
|
|
700
|
+
* @param options - Error options.
|
|
701
|
+
*/
|
|
702
|
+
constructor(options: InvalidOperationError.Options = {}) {
|
|
703
|
+
super(
|
|
704
|
+
options.reason
|
|
705
|
+
? `Invalid multisig operation: ${options.reason}.`
|
|
706
|
+
: 'Invalid multisig operation.',
|
|
707
|
+
{ cause: options.cause as Error | undefined },
|
|
708
|
+
)
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
export declare namespace InvalidOperationError {
|
|
713
|
+
/** Error construction options. */
|
|
714
|
+
export type Options = {
|
|
715
|
+
/** Underlying error. */
|
|
716
|
+
cause?: unknown | undefined
|
|
717
|
+
/** Validation failure. */
|
|
718
|
+
reason?: string | undefined
|
|
719
|
+
}
|
|
720
|
+
}
|