@xyo-network/xl1-validation 1.26.9 → 1.26.11

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.
@@ -1,4 +1,7 @@
1
- import type { HydratedBlockStateValidationFunction } from '@xyo-network/xl1-protocol-lib';
1
+ import { type Address } from '@xylabs/sdk-js';
2
+ import type { HydratedBlockStateValidationFunction, HydratedBlockWithHashMeta, TransactionBoundWitnessWithHashMeta } from '@xyo-network/xl1-protocol-lib';
3
+ /** Accumulate outflows per address from fees and transfers in the given transactions. */
4
+ export declare function accumulateOutflows(transactions: TransactionBoundWitnessWithHashMeta[], payloads: HydratedBlockWithHashMeta[1]): Record<Address, bigint>;
2
5
  /** Creates a block state validator that checks cumulative outflows per address do not exceed pre-block balances. */
3
6
  export declare function BlockCumulativeBalanceValidatorFactory(): HydratedBlockStateValidationFunction;
4
7
  //# sourceMappingURL=BlockCumulativeBalanceValidator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BlockCumulativeBalanceValidator.d.ts","sourceRoot":"","sources":["../../../../src/block/validators/BlockCumulativeBalanceValidator.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,oCAAoC,EAErC,MAAM,+BAA+B,CAAA;AAetC,oHAAoH;AACpH,wBAAgB,sCAAsC,IAAI,oCAAoC,CAqD7F"}
1
+ {"version":3,"file":"BlockCumulativeBalanceValidator.d.ts","sourceRoot":"","sources":["../../../../src/block/validators/BlockCumulativeBalanceValidator.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EACb,MAAM,gBAAgB,CAAA;AACvB,OAAO,KAAK,EACV,oCAAoC,EACpC,yBAAyB,EACzB,mCAAmC,EACpC,MAAM,+BAA+B,CAAA;AAgBtC,yFAAyF;AACzF,wBAAgB,kBAAkB,CAChC,YAAY,EAAE,mCAAmC,EAAE,EACnD,QAAQ,EAAE,yBAAyB,CAAC,CAAC,CAAC,GACrC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAsBzB;AAED,oHAAoH;AACpH,wBAAgB,sCAAsC,IAAI,oCAAoC,CAqC7F"}
@@ -1 +1 @@
1
- {"version":3,"file":"BoundWitnessReferences.d.ts","sourceRoot":"","sources":["../../../../src/boundwitness/validators/BoundWitnessReferences.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EAEZ,MAAM,EAEP,MAAM,qBAAqB,CAAA;AAE5B,OAAO,KAAK,EAAE,sCAAsC,EAAoC,MAAM,+BAA+B,CAAA;AAO7H,qKAAqK;AACrK,eAAO,MAAM,+BAA+B,GAEvC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,iBAAiB,MAAM,EAAE,KAAG,sCAAsC,CAAC,CAAC,CAuC7G,CAAA"}
1
+ {"version":3,"file":"BoundWitnessReferences.d.ts","sourceRoot":"","sources":["../../../../src/boundwitness/validators/BoundWitnessReferences.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EAEZ,MAAM,EAEP,MAAM,qBAAqB,CAAA;AAE5B,OAAO,KAAK,EAAE,sCAAsC,EAAoC,MAAM,+BAA+B,CAAA;AAO7H,qKAAqK;AACrK,eAAO,MAAM,+BAA+B,GAEvC,CAAC,SAAS,YAAY,GAAG,YAAY,EAAE,iBAAiB,MAAM,EAAE,KAAG,sCAAsC,CAAC,CAAC,CAsC7G,CAAA"}
@@ -5,42 +5,48 @@ import {
5
5
  import {
6
6
  HydratedBlockStateValidationError,
7
7
  isTransactionBoundWitnessWithHashMeta,
8
- isTransfer
8
+ isTransfer,
9
+ XYO_ZERO_ADDRESS
9
10
  } from "@xyo-network/xl1-protocol-lib";
10
- function transactionFeeCost(tx) {
11
+ function maxTransactionFeeCost(tx) {
11
12
  const {
12
13
  base,
13
14
  gasLimit,
14
- gasPrice,
15
15
  priority
16
16
  } = tx.fees;
17
- return hexToBigInt(base) + hexToBigInt(priority) + hexToBigInt(gasLimit) * hexToBigInt(gasPrice);
17
+ return hexToBigInt(base) + hexToBigInt(priority) + hexToBigInt(gasLimit);
18
+ }
19
+ function accumulateOutflows(transactions, payloads) {
20
+ const outflows = {};
21
+ for (const tx of transactions) {
22
+ const feeCost = maxTransactionFeeCost(tx);
23
+ const feePayer = tx.from;
24
+ outflows[feePayer] = (outflows[feePayer] ?? 0n) + feeCost;
25
+ const txPayloadHashes = new Set(tx.payload_hashes);
26
+ for (const payload of payloads) {
27
+ if (!txPayloadHashes.has(payload._hash) || !isTransfer(payload)) continue;
28
+ const { from } = payload;
29
+ for (const [to, amount] of Object.entries(payload.transfers)) {
30
+ if (to === from) continue;
31
+ outflows[from] = (outflows[from] ?? 0n) + hexToBigInt(amount);
32
+ }
33
+ }
34
+ }
35
+ return outflows;
18
36
  }
19
37
  function BlockCumulativeBalanceValidatorFactory() {
20
38
  return async (context, hydratedBlock) => {
21
39
  const [blockBw, payloads] = hydratedBlock;
22
40
  const transactions = payloads.filter(isTransactionBoundWitnessWithHashMeta);
23
41
  if (transactions.length === 0) return [];
24
- const outflows = {};
25
- for (const tx of transactions) {
26
- const feeCost = transactionFeeCost(tx);
27
- const feePayer = tx.from;
28
- outflows[feePayer] = (outflows[feePayer] ?? 0n) + feeCost;
29
- const txPayloadHashes = new Set(tx.payload_hashes);
30
- for (const payload of payloads) {
31
- if (!txPayloadHashes.has(payload._hash) || !isTransfer(payload)) continue;
32
- const { from } = payload;
33
- for (const amount of Object.values(payload.transfers)) {
34
- outflows[from] = (outflows[from] ?? 0n) + hexToBigInt(amount);
35
- }
36
- }
37
- }
42
+ const outflows = accumulateOutflows(transactions, payloads);
38
43
  const addresses = Object.keys(outflows);
39
44
  if (addresses.length === 0) return [];
40
45
  const balances = await context.accountBalance.accountBalances(addresses);
41
46
  const chainId = await context.chainIdAtBlockNumber(blockBw.block);
42
47
  const errors = [];
43
48
  for (const address of addresses) {
49
+ if (address === XYO_ZERO_ADDRESS) continue;
44
50
  const balance = balances[address] ?? 0n;
45
51
  const totalOutflow = outflows[address];
46
52
  if (totalOutflow > balance) {
@@ -90,8 +96,7 @@ var BoundWitnessReferencesValidator = (allowedSchemas) => ([bw, payloadSet]) =>
90
96
  }
91
97
  }
92
98
  } catch (ex) {
93
- const error = new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], `validation excepted: ${ex}`);
94
- error.cause = ex;
99
+ const error = new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], `validation excepted: ${ex}`, ex);
95
100
  errors.push(error);
96
101
  }
97
102
  return errors;
@@ -312,9 +317,9 @@ var TransactionJsonSchemaValidator = (context, tx) => {
312
317
  const error = new HydratedTransactionValidationError5(
313
318
  tx?.[0]?._hash ?? ZERO_HASH7,
314
319
  tx,
315
- `failed JSON schema validation: ${ajv.errorsText(validate.errors, { separator: "\n" })}`
320
+ `failed JSON schema validation: ${ajv.errorsText(validate.errors, { separator: "\n" })}`,
321
+ validate.errors
316
322
  );
317
- error.cause = validate.errors;
318
323
  errors.push(error);
319
324
  }
320
325
  } catch (ex) {
@@ -424,6 +429,7 @@ export {
424
429
  TransactionJsonSchemaValidator,
425
430
  TransactionProtocolValidator,
426
431
  TransactionTransfersValidatorFactory,
432
+ accumulateOutflows,
427
433
  validateTransaction
428
434
  };
429
435
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/block/validators/BlockCumulativeBalanceValidator.ts","../../src/boundwitness/validators/BoundWitnessReferences.ts","../../src/boundwitness/validators/BoundWitnessSignatures.ts","../../src/transaction/validateTransaction.ts","../../src/transaction/validators/TransactionDurationValidator.ts","../../src/transaction/validators/TransactionElevationValidator.ts","../../src/transaction/validators/TransactionFromValidator.ts","../../src/transaction/validators/TransactionGasValidator.ts","../../src/transaction/validators/TransactionJsonSchemaValidator.ts","../../src/transaction/validators/TransactionProtocolValidator.ts","../../src/transaction/validators/TransactionTransfersValidator.ts"],"sourcesContent":["import {\n type Address, type Hex, hexToBigInt,\n} from '@xylabs/sdk-js'\nimport type {\n HydratedBlockStateValidationFunction,\n TransactionBoundWitnessWithHashMeta,\n} from '@xyo-network/xl1-protocol-lib'\nimport {\n HydratedBlockStateValidationError,\n isTransactionBoundWitnessWithHashMeta,\n isTransfer,\n} from '@xyo-network/xl1-protocol-lib'\n\n/** Compute the maximum fee commitment for a transaction (base + priority + gasLimit * gasPrice). */\nfunction transactionFeeCost(tx: TransactionBoundWitnessWithHashMeta): bigint {\n const {\n base, gasLimit, gasPrice, priority,\n } = tx.fees\n return hexToBigInt(base) + hexToBigInt(priority) + (hexToBigInt(gasLimit) * hexToBigInt(gasPrice))\n}\n\n/** Creates a block state validator that checks cumulative outflows per address do not exceed pre-block balances. */\nexport function BlockCumulativeBalanceValidatorFactory(): HydratedBlockStateValidationFunction {\n return async (context, hydratedBlock) => {\n const [blockBw, payloads] = hydratedBlock\n\n // Find all transactions in the block\n const transactions = payloads.filter(isTransactionBoundWitnessWithHashMeta)\n\n if (transactions.length === 0) return []\n\n // Accumulate outflows per address\n const outflows: Record<Address, bigint> = {}\n\n for (const tx of transactions) {\n // Fee cost charged to the transaction sender\n const feeCost = transactionFeeCost(tx)\n const feePayer = tx.from\n outflows[feePayer] = (outflows[feePayer] ?? 0n) + feeCost\n\n // Find transfer payloads belonging to this transaction\n const txPayloadHashes = new Set(tx.payload_hashes)\n for (const payload of payloads) {\n if (!txPayloadHashes.has(payload._hash) || !isTransfer(payload)) continue\n const { from } = payload\n for (const amount of Object.values(payload.transfers) as Hex[]) {\n outflows[from] = (outflows[from] ?? 0n) + hexToBigInt(amount)\n }\n }\n }\n\n // Query pre-block balances for all addresses with outflows\n const addresses = Object.keys(outflows) as Address[]\n if (addresses.length === 0) return []\n\n const balances = await context.accountBalance.accountBalances(addresses)\n\n // Check each address\n const chainId = await context.chainIdAtBlockNumber(blockBw.block)\n const errors: HydratedBlockStateValidationError[] = []\n for (const address of addresses) {\n const balance = balances[address] ?? 0n\n const totalOutflow = outflows[address]\n if (totalOutflow > balance) {\n errors.push(new HydratedBlockStateValidationError(\n blockBw._hash,\n chainId,\n hydratedBlock,\n `Cumulative outflow for address ${address} exceeds available balance: outflow ${totalOutflow} > balance ${balance}`,\n ))\n }\n }\n\n return errors\n }\n}\n","import type { Hash, Promisable } from '@xylabs/sdk-js'\nimport { ZERO_HASH } from '@xylabs/sdk-js'\nimport type {\n BoundWitness,\n Payload,\n Schema,\n WithHashMeta,\n} from '@xyo-network/sdk-js'\nimport { isAnyPayload } from '@xyo-network/sdk-js'\nimport type { HydratedBoundWitnessValidationFunction, HydratedBoundWitnessWithHashMeta } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedBoundWitnessValidationError } from '@xyo-network/xl1-protocol-lib'\n\nfunction getPayloadsFromPayloadArray(payloads: WithHashMeta<Payload>[], hashes: Hash[]): (WithHashMeta<Payload> | undefined)[] {\n return hashes.map(hash => payloads.find(payload => payload._hash === hash || payload._dataHash === hash))\n}\n\n/** Creates a validator that checks all payload references in a BoundWitness are present, have matching schemas, and optionally conform to an allowed schema list. */\nexport const BoundWitnessReferencesValidator\n\n = <T extends BoundWitness = BoundWitness>(allowedSchemas?: Schema[]): HydratedBoundWitnessValidationFunction<T> => (\n [bw, payloadSet]: HydratedBoundWitnessWithHashMeta<T>,\n // eslint-disable-next-line complexity\n ): Promisable<HydratedBoundWitnessValidationError[]> => {\n const errors: HydratedBoundWitnessValidationError[] = []\n try {\n const payloads = getPayloadsFromPayloadArray(payloadSet, bw.payload_hashes)\n if (payloads.length !== bw.payload_hashes.length) {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], 'unable to locate payloads'))\n }\n\n // check if payloads are valid and if their schemas match the declared schemas\n for (let payload of payloads) {\n if (isAnyPayload(payload)) {\n const payloadHashIndex = bw.payload_hashes.indexOf(payload._hash)\n const payloadDataHashIndex = bw.payload_hashes.indexOf(payload._dataHash)\n const payloadIndex = Math.max(payloadHashIndex, payloadDataHashIndex)\n if (payloadIndex === -1) {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], 'payload hash not found'))\n }\n\n const declaredSchema = bw.payload_schemas[payloadIndex]\n if (declaredSchema !== payload.schema) {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], 'mismatched schema'))\n }\n\n if (allowedSchemas && !allowedSchemas.includes(payload.schema)) {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], `disallowed schema [${payload.schema}]`))\n }\n } else {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], 'invalid payload'))\n }\n }\n } catch (ex) {\n const error = new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], `validation excepted: ${ex}`)\n error.cause = ex\n errors.push(error)\n }\n return errors\n }\n","import { type Address, ZERO_HASH } from '@xylabs/sdk-js'\nimport { toArrayBuffer } from '@xylabs/sdk-js'\nimport type { BoundWitness, WithStorageMeta } from '@xyo-network/sdk-js'\nimport { BoundWitnessBuilder, BoundWitnessValidator } from '@xyo-network/sdk-js'\nimport type { BoundWitnessValidationFunction } from '@xyo-network/xl1-protocol-lib'\nimport { BoundWitnessValidationError } from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that all signatures on a BoundWitness are cryptographically valid for their corresponding addresses. */\nexport const BoundWitnessSignaturesValidator: BoundWitnessValidationFunction = async (\n bw: BoundWitness,\n) => {\n const errors: BoundWitnessValidationError[] = []\n try {\n const dataHash = await BoundWitnessBuilder.dataHash(bw)\n const results: [Address, Error[]][] = await Promise.all(bw.addresses.map(async (address, index) => {\n return [address, await BoundWitnessValidator.validateSignature(\n toArrayBuffer(dataHash),\n toArrayBuffer(address),\n toArrayBuffer(bw.$signatures[index] ?? undefined),\n )]\n }))\n for (const [, bwErrors] of results) {\n for (const bwError of bwErrors) {\n errors.push(new BoundWitnessValidationError((bw as WithStorageMeta<BoundWitness>)?._hash ?? ZERO_HASH, bw, 'validation errors', bwError))\n }\n }\n } catch (ex) {\n errors.push(new BoundWitnessValidationError((bw as WithStorageMeta<BoundWitness>)?._hash ?? ZERO_HASH, bw, 'validation excepted', ex))\n }\n return errors\n}\n","import type {\n HydratedTransactionValidationFunction,\n HydratedTransactionValidationFunctionContext,\n HydratedTransactionWithHashMeta,\n} from '@xyo-network/xl1-protocol-lib'\nimport { isTransactionBoundWitness } from '@xyo-network/xl1-protocol-lib'\n\nimport {\n TransactionDurationValidator,\n TransactionElevationValidator, TransactionFromValidator, TransactionGasValidator, TransactionProtocolValidator,\n} from './validators/index.ts'\n\n/** Validates a hydrated transaction using built-in validators plus any additional validators provided. */\nexport async function validateTransaction(\n context: HydratedTransactionValidationFunctionContext,\n tx: HydratedTransactionWithHashMeta,\n additionalValidators?: HydratedTransactionValidationFunction[],\n) {\n try {\n if (!isTransactionBoundWitness(tx[0])) {\n return [new Error('failed isTransactionBoundWitness identity check')]\n }\n\n const validators: HydratedTransactionValidationFunction<HydratedTransactionValidationFunctionContext>[] = [\n TransactionProtocolValidator,\n TransactionDurationValidator,\n TransactionFromValidator,\n TransactionGasValidator,\n TransactionElevationValidator,\n ...(additionalValidators ?? []),\n ]\n return (await Promise.all(validators.map(v => v(context, tx)))).flat()\n } catch (ex) {\n return [(new Error(`Failed TransactionGasValidator: ${ex}`))]\n }\n}\n","import { ZERO_HASH } from '@xylabs/sdk-js'\nimport type { HydratedTransactionValidationFunction } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that transaction timing fields (nbf and exp) are positive, correctly ordered, and within allowed duration limits. */\nexport const TransactionDurationValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n// eslint-disable-next-line complexity\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n const { exp, nbf } = tx[0]\n if (nbf < 0) errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'Transaction nbf must be positive'))\n\n if (exp < 0) errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'Transaction exp must be positive'))\n if (exp <= nbf) errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'Transaction exp must greater than nbf'))\n if (exp - nbf > 10_000) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'Transaction exp must not be too far in the future',\n ))\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `Failed TransactionDurationValidator: ${ex}`,\n ex,\n ))\n }\n\n return errors\n}\n","import { ZERO_HASH } from '@xylabs/sdk-js'\nimport type { HydratedTransactionValidationFunction } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\nimport { extractElevatedHashes } from '@xyo-network/xl1-protocol-sdk'\n\n/** Validates that a hydrated transaction includes all required elevated script hashes. */\nexport const TransactionElevationValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n try {\n extractElevatedHashes(tx)\n } catch {\n errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'Hydrated transaction does not include all script hashes'))\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `Failed TransactionElevationValidator: ${ex}`,\n ex,\n ))\n }\n return errors\n}\n","import { asAddress, ZERO_HASH } from '@xylabs/sdk-js'\nimport { addressesContains } from '@xyo-network/sdk-js'\nimport type { HydratedTransactionValidationFunction } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that the transaction's from field is a valid address and is included in the BoundWitness addresses. */\nexport const TransactionFromValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n const from = asAddress(tx[0].from)\n if (from === undefined)errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'Transaction from is not a valid address',\n ))\n else if (!addressesContains(tx[0], from)) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'Transaction from address must be listed in addresses',\n ))\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `Failed TransactionFromValidator: ${ex}`,\n ex,\n ))\n }\n return errors\n}\n","import { hexToBigInt, ZERO_HASH } from '@xylabs/sdk-js'\nimport type {\n HydratedTransactionValidationFunction,\n TransactionFeesBigInt, TransactionFeesHex,\n} from '@xyo-network/xl1-protocol-lib'\nimport {\n AttoXL1,\n HydratedTransactionValidationError,\n minTransactionFees,\n} from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that transaction fee fields (base, gasLimit, gasPrice, priority) are present and meet minimum requirements. */\nexport const TransactionGasValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n// eslint-disable-next-line complexity\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n if (tx?.[0].fees === undefined) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'Missing fees',\n ))\n } else {\n const {\n base, gasLimit, gasPrice, priority,\n } = parseFees(tx[0].fees)\n\n if (base === undefined) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'fees.base must be defined and a valid number',\n ))\n else if (base < minTransactionFees.base) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `fees.base must be >= ${minTransactionFees.base}`,\n ))\n\n if (gasLimit === undefined) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'fees.gasLimit must be defined and a valid number',\n ))\n else if (gasLimit < minTransactionFees.gasLimit) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `fees.gasLimit must be >= ${minTransactionFees.gasLimit}`,\n ))\n\n if (gasPrice === undefined) errors.push(\n new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'fees.gasPrice must be defined and a valid number',\n ),\n )\n else if (gasPrice < minTransactionFees.gasPrice) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `fees.gasPrice must be >= ${minTransactionFees.gasPrice}`,\n ))\n\n if (priority === undefined) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'fees.priority must be defined and a valid number',\n ))\n else if (priority < minTransactionFees.priority) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `fees.priority must be >= ${minTransactionFees.priority}`,\n ))\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `Failed TransactionGasValidator: ${ex}`,\n ex,\n ))\n }\n return errors\n}\n\nconst parseFees = (fees: TransactionFeesHex): Partial<TransactionFeesBigInt> => {\n const ret: Partial<TransactionFeesBigInt> = {}\n const {\n base, gasLimit, gasPrice, priority,\n } = fees\n if (base !== undefined) ret.base = AttoXL1(hexToBigInt(base))\n if (gasLimit !== undefined) ret.gasLimit = AttoXL1(hexToBigInt(gasLimit))\n if (gasPrice !== undefined) ret.gasPrice = AttoXL1(hexToBigInt(gasPrice))\n if (priority !== undefined) ret.priority = AttoXL1(hexToBigInt(priority))\n return ret\n}\n","import { ZERO_HASH } from '@xylabs/sdk-js'\nimport { PayloadBuilder } from '@xyo-network/sdk-js'\nimport type { HydratedTransactionValidationFunction, TransactionBoundWitness } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\nimport { TransactionBoundWitnessJsonSchema } from '@xyo-network/xl1-schema'\nimport type { ValidateFunction } from 'ajv'\nimport { Ajv } from 'ajv'\n\nconst ajv = new Ajv({ allErrors: true, strict: true })\n\nlet validate: ValidateFunction<TransactionBoundWitness> | undefined\n\n/** Validates a transaction against the TransactionBoundWitness JSON schema using AJV. */\nexport const TransactionJsonSchemaValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n if (validate === undefined) validate = ajv.compile(TransactionBoundWitnessJsonSchema)\n if (!validate(PayloadBuilder.omitStorageMeta(tx[0]))) {\n const error = new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `failed JSON schema validation: ${ajv.errorsText(validate.errors, { separator: '\\n' })}`,\n )\n error.cause = validate.errors\n errors.push(error)\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'validation excepted', ex))\n }\n return errors\n}\n","import { ZERO_HASH } from '@xylabs/sdk-js'\nimport type {\n ChainId,\n HydratedTransactionValidationFunction,\n} from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that the transaction's chain ID matches the expected chain ID from the validation context. */\nexport const TransactionProtocolValidator: HydratedTransactionValidationFunction = async (\n context: { chainId?: ChainId },\n tx,\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n if (context?.chainId !== undefined && tx[0].chain !== context.chainId) {\n errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, `invalid chain id [${context.chainId}, ${tx[0].chain}]`))\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'validation excepted', ex))\n }\n return await Promise.resolve(errors)\n}\n","import type { Address } from '@xylabs/sdk-js'\nimport { isDefined, isUndefined } from '@xylabs/sdk-js'\nimport type {\n HydratedTransactionValidationFunction, StepIdentity,\n Transfer,\n} from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError, isTransfer } from '@xyo-network/xl1-protocol-lib'\nimport {\n completedStepRewardAddress, derivedReceiveAddress, elevatedPayloads,\n} from '@xyo-network/xl1-protocol-sdk'\n\n/** Function type that checks whether a signer is authorized to sign for a given signee address. */\nexport type SignerValidator = (signer: Address, signee: Address, context?: { address?: Address; scope?: string; step?: StepIdentity }) => boolean\n\n/** Maps addresses to their list of authorized signer addresses. */\nexport type SignerMapping = Map<Address, Address[]>\n\n/** A signer validator that only allows an address to sign for itself. */\nexport const SelfSignerValidator: SignerValidator = (signer: Address, signee: Address) => signer === signee\n\n/** Creates a signer validator that authorizes specified signers to sign for completed step reward addresses. */\nexport const CompletedStepRewardAddressValidatorFactory = (allowedSigners: Address[]): SignerValidator => (\n signer: Address,\n signee: Address,\n context?: { step?: StepIdentity },\n) => {\n const step = context?.step\n if (isDefined(step)) {\n const contextAddress = completedStepRewardAddress(step)\n return allowedSigners.includes(signer) && signee === contextAddress\n } else {\n return false\n }\n}\n\n/** Creates a signer validator that authorizes specified signers to sign for derived receive addresses within a given scope. */\nexport const DerivedReceiveAddressValidatorFactory = (allowedSigners: Address[], allowedScope: string): SignerValidator => (\n signer: Address,\n signee: Address,\n context?: { address?: Address; scope?: string },\n) => {\n const { address, scope } = context ?? {}\n if (scope !== allowedScope) {\n return false\n }\n if (isDefined(address)) {\n const derivedAddress = derivedReceiveAddress(address, scope)\n return allowedSigners.includes(signer) && signee === derivedAddress\n } else {\n return false\n }\n}\n\n/** Creates a transaction validator that checks all transfers are authorized by the transaction signer. */\nexport function TransactionTransfersValidatorFactory(\n signerValidators: SignerValidator[] = [SelfSignerValidator],\n): HydratedTransactionValidationFunction {\n return async (\n context,\n hydratedTx,\n ) => {\n const errors: HydratedTransactionValidationError[] = []\n const signer = hydratedTx[0].from\n try {\n const payloads = elevatedPayloads(hydratedTx)\n const transfers = payloads.filter(isTransfer) as Transfer[]\n for (const transfer of transfers) {\n if (isUndefined(signerValidators.find(v => v(signer, transfer.from, transfer.context)))) {\n errors.push(new HydratedTransactionValidationError(\n hydratedTx[0]._hash,\n hydratedTx,\n `transfer from address ${transfer.from} is not authorized by signer ${signer}`,\n ))\n }\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(hydratedTx[0]._hash, hydratedTx, 'validation excepted', ex))\n }\n return await Promise.resolve(errors)\n }\n}\n"],"mappings":";AAAA;AAAA,EAC0B;AAAA,OACnB;AAKP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAGP,SAAS,mBAAmB,IAAiD;AAC3E,QAAM;AAAA,IACJ;AAAA,IAAM;AAAA,IAAU;AAAA,IAAU;AAAA,EAC5B,IAAI,GAAG;AACP,SAAO,YAAY,IAAI,IAAI,YAAY,QAAQ,IAAK,YAAY,QAAQ,IAAI,YAAY,QAAQ;AAClG;AAGO,SAAS,yCAA+E;AAC7F,SAAO,OAAO,SAAS,kBAAkB;AACvC,UAAM,CAAC,SAAS,QAAQ,IAAI;AAG5B,UAAM,eAAe,SAAS,OAAO,qCAAqC;AAE1E,QAAI,aAAa,WAAW,EAAG,QAAO,CAAC;AAGvC,UAAM,WAAoC,CAAC;AAE3C,eAAW,MAAM,cAAc;AAE7B,YAAM,UAAU,mBAAmB,EAAE;AACrC,YAAM,WAAW,GAAG;AACpB,eAAS,QAAQ,KAAK,SAAS,QAAQ,KAAK,MAAM;AAGlD,YAAM,kBAAkB,IAAI,IAAI,GAAG,cAAc;AACjD,iBAAW,WAAW,UAAU;AAC9B,YAAI,CAAC,gBAAgB,IAAI,QAAQ,KAAK,KAAK,CAAC,WAAW,OAAO,EAAG;AACjE,cAAM,EAAE,KAAK,IAAI;AACjB,mBAAW,UAAU,OAAO,OAAO,QAAQ,SAAS,GAAY;AAC9D,mBAAS,IAAI,KAAK,SAAS,IAAI,KAAK,MAAM,YAAY,MAAM;AAAA,QAC9D;AAAA,MACF;AAAA,IACF;AAGA,UAAM,YAAY,OAA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AK;AAAA,EACvE,SAAS,IAAI;AACX,WAAO,CAAE,IAAI,MAAM,mCAAmC,EAAE,EAAE,CAAE;AAAA,EAC9D;AACF;","names":["ZERO_HASH","ZERO_HASH","ZERO_HASH","HydratedTransactionValidationError","ZERO_HASH","HydratedTransactionValidationError","hexToBigInt","ZERO_HASH","HydratedTransactionValidationError","ZERO_HASH","HydratedTransactionValidationError","ZERO_HASH","HydratedTransactionValidationError","HydratedTransactionValidationError","isTransfer"]}
1
+ {"version":3,"sources":["../../src/block/validators/BlockCumulativeBalanceValidator.ts","../../src/boundwitness/validators/BoundWitnessReferences.ts","../../src/boundwitness/validators/BoundWitnessSignatures.ts","../../src/transaction/validateTransaction.ts","../../src/transaction/validators/TransactionDurationValidator.ts","../../src/transaction/validators/TransactionElevationValidator.ts","../../src/transaction/validators/TransactionFromValidator.ts","../../src/transaction/validators/TransactionGasValidator.ts","../../src/transaction/validators/TransactionJsonSchemaValidator.ts","../../src/transaction/validators/TransactionProtocolValidator.ts","../../src/transaction/validators/TransactionTransfersValidator.ts"],"sourcesContent":["import {\n type Address, type Hex, hexToBigInt,\n} from '@xylabs/sdk-js'\nimport type {\n HydratedBlockStateValidationFunction,\n HydratedBlockWithHashMeta,\n TransactionBoundWitnessWithHashMeta,\n} from '@xyo-network/xl1-protocol-lib'\nimport {\n HydratedBlockStateValidationError,\n isTransactionBoundWitnessWithHashMeta,\n isTransfer,\n XYO_ZERO_ADDRESS,\n} from '@xyo-network/xl1-protocol-lib'\n\n/** Compute the maximum fee commitment for a transaction (base + priority + gasLimit). */\nfunction maxTransactionFeeCost(tx: TransactionBoundWitnessWithHashMeta): bigint {\n const {\n base, gasLimit, priority,\n } = tx.fees\n return hexToBigInt(base) + hexToBigInt(priority) + hexToBigInt(gasLimit)\n}\n\n/** Accumulate outflows per address from fees and transfers in the given transactions. */\nexport function accumulateOutflows(\n transactions: TransactionBoundWitnessWithHashMeta[],\n payloads: HydratedBlockWithHashMeta[1],\n): Record<Address, bigint> {\n const outflows: Record<Address, bigint> = {}\n\n for (const tx of transactions) {\n // Fee cost charged to the transaction sender\n const feeCost = maxTransactionFeeCost(tx)\n const feePayer = tx.from\n outflows[feePayer] = (outflows[feePayer] ?? 0n) + feeCost\n\n // Find transfer payloads belonging to this transaction\n const txPayloadHashes = new Set(tx.payload_hashes)\n for (const payload of payloads) {\n if (!txPayloadHashes.has(payload._hash) || !isTransfer(payload)) continue\n const { from } = payload\n for (const [to, amount] of Object.entries(payload.transfers) as [Address, Hex][]) {\n if (to === from) continue // self-transfers are not outflows\n outflows[from] = (outflows[from] ?? 0n) + hexToBigInt(amount)\n }\n }\n }\n\n return outflows\n}\n\n/** Creates a block state validator that checks cumulative outflows per address do not exceed pre-block balances. */\nexport function BlockCumulativeBalanceValidatorFactory(): HydratedBlockStateValidationFunction {\n return async (context, hydratedBlock) => {\n const [blockBw, payloads] = hydratedBlock\n\n // Find all transactions in the block\n const transactions = payloads.filter(isTransactionBoundWitnessWithHashMeta)\n\n if (transactions.length === 0) return []\n\n const outflows = accumulateOutflows(transactions, payloads)\n\n // Query pre-block balances for all addresses with outflows\n const addresses = Object.keys(outflows) as Address[]\n if (addresses.length === 0) return []\n\n const balances = await context.accountBalance.accountBalances(addresses)\n\n // Check each address\n const chainId = await context.chainIdAtBlockNumber(blockBw.block)\n const errors: HydratedBlockStateValidationError[] = []\n for (const address of addresses) {\n if (address === XYO_ZERO_ADDRESS) continue // Skip zero address as it's used for burn transactions and doesn't have a balance\n // TODO: Add specific validation for block reward transfer\n const balance = balances[address] ?? 0n\n const totalOutflow = outflows[address]\n if (totalOutflow > balance) {\n errors.push(new HydratedBlockStateValidationError(\n blockBw._hash,\n chainId,\n hydratedBlock,\n `Cumulative outflow for address ${address} exceeds available balance: outflow ${totalOutflow} > balance ${balance}`,\n ))\n }\n }\n\n return errors\n }\n}\n","import type { Hash, Promisable } from '@xylabs/sdk-js'\nimport { ZERO_HASH } from '@xylabs/sdk-js'\nimport type {\n BoundWitness,\n Payload,\n Schema,\n WithHashMeta,\n} from '@xyo-network/sdk-js'\nimport { isAnyPayload } from '@xyo-network/sdk-js'\nimport type { HydratedBoundWitnessValidationFunction, HydratedBoundWitnessWithHashMeta } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedBoundWitnessValidationError } from '@xyo-network/xl1-protocol-lib'\n\nfunction getPayloadsFromPayloadArray(payloads: WithHashMeta<Payload>[], hashes: Hash[]): (WithHashMeta<Payload> | undefined)[] {\n return hashes.map(hash => payloads.find(payload => payload._hash === hash || payload._dataHash === hash))\n}\n\n/** Creates a validator that checks all payload references in a BoundWitness are present, have matching schemas, and optionally conform to an allowed schema list. */\nexport const BoundWitnessReferencesValidator\n\n = <T extends BoundWitness = BoundWitness>(allowedSchemas?: Schema[]): HydratedBoundWitnessValidationFunction<T> => (\n [bw, payloadSet]: HydratedBoundWitnessWithHashMeta<T>,\n // eslint-disable-next-line complexity\n ): Promisable<HydratedBoundWitnessValidationError[]> => {\n const errors: HydratedBoundWitnessValidationError[] = []\n try {\n const payloads = getPayloadsFromPayloadArray(payloadSet, bw.payload_hashes)\n if (payloads.length !== bw.payload_hashes.length) {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], 'unable to locate payloads'))\n }\n\n // check if payloads are valid and if their schemas match the declared schemas\n for (let payload of payloads) {\n if (isAnyPayload(payload)) {\n const payloadHashIndex = bw.payload_hashes.indexOf(payload._hash)\n const payloadDataHashIndex = bw.payload_hashes.indexOf(payload._dataHash)\n const payloadIndex = Math.max(payloadHashIndex, payloadDataHashIndex)\n if (payloadIndex === -1) {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], 'payload hash not found'))\n }\n\n const declaredSchema = bw.payload_schemas[payloadIndex]\n if (declaredSchema !== payload.schema) {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], 'mismatched schema'))\n }\n\n if (allowedSchemas && !allowedSchemas.includes(payload.schema)) {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], `disallowed schema [${payload.schema}]`))\n }\n } else {\n errors.push(new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], 'invalid payload'))\n }\n }\n } catch (ex) {\n const error = new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], `validation excepted: ${ex}`, ex)\n errors.push(error)\n }\n return errors\n }\n","import { type Address, ZERO_HASH } from '@xylabs/sdk-js'\nimport { toArrayBuffer } from '@xylabs/sdk-js'\nimport type { BoundWitness, WithStorageMeta } from '@xyo-network/sdk-js'\nimport { BoundWitnessBuilder, BoundWitnessValidator } from '@xyo-network/sdk-js'\nimport type { BoundWitnessValidationFunction } from '@xyo-network/xl1-protocol-lib'\nimport { BoundWitnessValidationError } from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that all signatures on a BoundWitness are cryptographically valid for their corresponding addresses. */\nexport const BoundWitnessSignaturesValidator: BoundWitnessValidationFunction = async (\n bw: BoundWitness,\n) => {\n const errors: BoundWitnessValidationError[] = []\n try {\n const dataHash = await BoundWitnessBuilder.dataHash(bw)\n const results: [Address, Error[]][] = await Promise.all(bw.addresses.map(async (address, index) => {\n return [address, await BoundWitnessValidator.validateSignature(\n toArrayBuffer(dataHash),\n toArrayBuffer(address),\n toArrayBuffer(bw.$signatures[index] ?? undefined),\n )]\n }))\n for (const [, bwErrors] of results) {\n for (const bwError of bwErrors) {\n errors.push(new BoundWitnessValidationError((bw as WithStorageMeta<BoundWitness>)?._hash ?? ZERO_HASH, bw, 'validation errors', bwError))\n }\n }\n } catch (ex) {\n errors.push(new BoundWitnessValidationError((bw as WithStorageMeta<BoundWitness>)?._hash ?? ZERO_HASH, bw, 'validation excepted', ex))\n }\n return errors\n}\n","import type {\n HydratedTransactionValidationFunction,\n HydratedTransactionValidationFunctionContext,\n HydratedTransactionWithHashMeta,\n} from '@xyo-network/xl1-protocol-lib'\nimport { isTransactionBoundWitness } from '@xyo-network/xl1-protocol-lib'\n\nimport {\n TransactionDurationValidator,\n TransactionElevationValidator, TransactionFromValidator, TransactionGasValidator, TransactionProtocolValidator,\n} from './validators/index.ts'\n\n/** Validates a hydrated transaction using built-in validators plus any additional validators provided. */\nexport async function validateTransaction(\n context: HydratedTransactionValidationFunctionContext,\n tx: HydratedTransactionWithHashMeta,\n additionalValidators?: HydratedTransactionValidationFunction[],\n) {\n try {\n if (!isTransactionBoundWitness(tx[0])) {\n return [new Error('failed isTransactionBoundWitness identity check')]\n }\n\n const validators: HydratedTransactionValidationFunction<HydratedTransactionValidationFunctionContext>[] = [\n TransactionProtocolValidator,\n TransactionDurationValidator,\n TransactionFromValidator,\n TransactionGasValidator,\n TransactionElevationValidator,\n ...(additionalValidators ?? []),\n ]\n return (await Promise.all(validators.map(v => v(context, tx)))).flat()\n } catch (ex) {\n return [(new Error(`Failed TransactionGasValidator: ${ex}`))]\n }\n}\n","import { ZERO_HASH } from '@xylabs/sdk-js'\nimport type { HydratedTransactionValidationFunction } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that transaction timing fields (nbf and exp) are positive, correctly ordered, and within allowed duration limits. */\nexport const TransactionDurationValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n// eslint-disable-next-line complexity\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n const { exp, nbf } = tx[0]\n if (nbf < 0) errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'Transaction nbf must be positive'))\n\n if (exp < 0) errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'Transaction exp must be positive'))\n if (exp <= nbf) errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'Transaction exp must greater than nbf'))\n if (exp - nbf > 10_000) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'Transaction exp must not be too far in the future',\n ))\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `Failed TransactionDurationValidator: ${ex}`,\n ex,\n ))\n }\n\n return errors\n}\n","import { ZERO_HASH } from '@xylabs/sdk-js'\nimport type { HydratedTransactionValidationFunction } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\nimport { extractElevatedHashes } from '@xyo-network/xl1-protocol-sdk'\n\n/** Validates that a hydrated transaction includes all required elevated script hashes. */\nexport const TransactionElevationValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n try {\n extractElevatedHashes(tx)\n } catch {\n errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'Hydrated transaction does not include all script hashes'))\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `Failed TransactionElevationValidator: ${ex}`,\n ex,\n ))\n }\n return errors\n}\n","import { asAddress, ZERO_HASH } from '@xylabs/sdk-js'\nimport { addressesContains } from '@xyo-network/sdk-js'\nimport type { HydratedTransactionValidationFunction } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that the transaction's from field is a valid address and is included in the BoundWitness addresses. */\nexport const TransactionFromValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n const from = asAddress(tx[0].from)\n if (from === undefined)errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'Transaction from is not a valid address',\n ))\n else if (!addressesContains(tx[0], from)) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'Transaction from address must be listed in addresses',\n ))\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `Failed TransactionFromValidator: ${ex}`,\n ex,\n ))\n }\n return errors\n}\n","import { hexToBigInt, ZERO_HASH } from '@xylabs/sdk-js'\nimport type {\n HydratedTransactionValidationFunction,\n TransactionFeesBigInt, TransactionFeesHex,\n} from '@xyo-network/xl1-protocol-lib'\nimport {\n AttoXL1,\n HydratedTransactionValidationError,\n minTransactionFees,\n} from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that transaction fee fields (base, gasLimit, gasPrice, priority) are present and meet minimum requirements. */\nexport const TransactionGasValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n// eslint-disable-next-line complexity\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n if (tx?.[0].fees === undefined) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'Missing fees',\n ))\n } else {\n const {\n base, gasLimit, gasPrice, priority,\n } = parseFees(tx[0].fees)\n\n if (base === undefined) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'fees.base must be defined and a valid number',\n ))\n else if (base < minTransactionFees.base) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `fees.base must be >= ${minTransactionFees.base}`,\n ))\n\n if (gasLimit === undefined) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'fees.gasLimit must be defined and a valid number',\n ))\n else if (gasLimit < minTransactionFees.gasLimit) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `fees.gasLimit must be >= ${minTransactionFees.gasLimit}`,\n ))\n\n if (gasPrice === undefined) errors.push(\n new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'fees.gasPrice must be defined and a valid number',\n ),\n )\n else if (gasPrice < minTransactionFees.gasPrice) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `fees.gasPrice must be >= ${minTransactionFees.gasPrice}`,\n ))\n\n if (priority === undefined) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n 'fees.priority must be defined and a valid number',\n ))\n else if (priority < minTransactionFees.priority) errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `fees.priority must be >= ${minTransactionFees.priority}`,\n ))\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `Failed TransactionGasValidator: ${ex}`,\n ex,\n ))\n }\n return errors\n}\n\nconst parseFees = (fees: TransactionFeesHex): Partial<TransactionFeesBigInt> => {\n const ret: Partial<TransactionFeesBigInt> = {}\n const {\n base, gasLimit, gasPrice, priority,\n } = fees\n if (base !== undefined) ret.base = AttoXL1(hexToBigInt(base))\n if (gasLimit !== undefined) ret.gasLimit = AttoXL1(hexToBigInt(gasLimit))\n if (gasPrice !== undefined) ret.gasPrice = AttoXL1(hexToBigInt(gasPrice))\n if (priority !== undefined) ret.priority = AttoXL1(hexToBigInt(priority))\n return ret\n}\n","import { ZERO_HASH } from '@xylabs/sdk-js'\nimport { PayloadBuilder } from '@xyo-network/sdk-js'\nimport type { HydratedTransactionValidationFunction, TransactionBoundWitness } from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\nimport { TransactionBoundWitnessJsonSchema } from '@xyo-network/xl1-schema'\nimport type { ValidateFunction } from 'ajv'\nimport { Ajv } from 'ajv'\n\nconst ajv = new Ajv({ allErrors: true, strict: true })\n\nlet validate: ValidateFunction<TransactionBoundWitness> | undefined\n\n/** Validates a transaction against the TransactionBoundWitness JSON schema using AJV. */\nexport const TransactionJsonSchemaValidator: HydratedTransactionValidationFunction = (\n context,\n tx,\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n if (validate === undefined) validate = ajv.compile(TransactionBoundWitnessJsonSchema)\n if (!validate(PayloadBuilder.omitStorageMeta(tx[0]))) {\n const error = new HydratedTransactionValidationError(\n tx?.[0]?._hash ?? ZERO_HASH,\n tx,\n `failed JSON schema validation: ${ajv.errorsText(validate.errors, { separator: '\\n' })}`,\n validate.errors,\n )\n errors.push(error)\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'validation excepted', ex))\n }\n return errors\n}\n","import { ZERO_HASH } from '@xylabs/sdk-js'\nimport type {\n ChainId,\n HydratedTransactionValidationFunction,\n} from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError } from '@xyo-network/xl1-protocol-lib'\n\n/** Validates that the transaction's chain ID matches the expected chain ID from the validation context. */\nexport const TransactionProtocolValidator: HydratedTransactionValidationFunction = async (\n context: { chainId?: ChainId },\n tx,\n) => {\n const errors: HydratedTransactionValidationError[] = []\n try {\n if (context?.chainId !== undefined && tx[0].chain !== context.chainId) {\n errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, `invalid chain id [${context.chainId}, ${tx[0].chain}]`))\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH, tx, 'validation excepted', ex))\n }\n return await Promise.resolve(errors)\n}\n","import type { Address } from '@xylabs/sdk-js'\nimport { isDefined, isUndefined } from '@xylabs/sdk-js'\nimport type {\n HydratedTransactionValidationFunction, StepIdentity,\n Transfer,\n} from '@xyo-network/xl1-protocol-lib'\nimport { HydratedTransactionValidationError, isTransfer } from '@xyo-network/xl1-protocol-lib'\nimport {\n completedStepRewardAddress, derivedReceiveAddress, elevatedPayloads,\n} from '@xyo-network/xl1-protocol-sdk'\n\n/** Function type that checks whether a signer is authorized to sign for a given signee address. */\nexport type SignerValidator = (signer: Address, signee: Address, context?: { address?: Address; scope?: string; step?: StepIdentity }) => boolean\n\n/** Maps addresses to their list of authorized signer addresses. */\nexport type SignerMapping = Map<Address, Address[]>\n\n/** A signer validator that only allows an address to sign for itself. */\nexport const SelfSignerValidator: SignerValidator = (signer: Address, signee: Address) => signer === signee\n\n/** Creates a signer validator that authorizes specified signers to sign for completed step reward addresses. */\nexport const CompletedStepRewardAddressValidatorFactory = (allowedSigners: Address[]): SignerValidator => (\n signer: Address,\n signee: Address,\n context?: { step?: StepIdentity },\n) => {\n const step = context?.step\n if (isDefined(step)) {\n const contextAddress = completedStepRewardAddress(step)\n return allowedSigners.includes(signer) && signee === contextAddress\n } else {\n return false\n }\n}\n\n/** Creates a signer validator that authorizes specified signers to sign for derived receive addresses within a given scope. */\nexport const DerivedReceiveAddressValidatorFactory = (allowedSigners: Address[], allowedScope: string): SignerValidator => (\n signer: Address,\n signee: Address,\n context?: { address?: Address; scope?: string },\n) => {\n const { address, scope } = context ?? {}\n if (scope !== allowedScope) {\n return false\n }\n if (isDefined(address)) {\n const derivedAddress = derivedReceiveAddress(address, scope)\n return allowedSigners.includes(signer) && signee === derivedAddress\n } else {\n return false\n }\n}\n\n/** Creates a transaction validator that checks all transfers are authorized by the transaction signer. */\nexport function TransactionTransfersValidatorFactory(\n signerValidators: SignerValidator[] = [SelfSignerValidator],\n): HydratedTransactionValidationFunction {\n return async (\n context,\n hydratedTx,\n ) => {\n const errors: HydratedTransactionValidationError[] = []\n const signer = hydratedTx[0].from\n try {\n const payloads = elevatedPayloads(hydratedTx)\n const transfers = payloads.filter(isTransfer) as Transfer[]\n for (const transfer of transfers) {\n if (isUndefined(signerValidators.find(v => v(signer, transfer.from, transfer.context)))) {\n errors.push(new HydratedTransactionValidationError(\n hydratedTx[0]._hash,\n hydratedTx,\n `transfer from address ${transfer.from} is not authorized by signer ${signer}`,\n ))\n }\n }\n } catch (ex) {\n errors.push(new HydratedTransactionValidationError(hydratedTx[0]._hash, hydratedTx, 'validation excepted', ex))\n }\n return await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H","ZERO_HASH","HydratedTransactionValidationError","ZERO_HASH","HydratedTransactionValidationError","hexToBigInt","ZERO_HASH","HydratedTransactionValidationError","ZERO_HASH","HydratedTransactionValidationError","ZERO_HASH","HydratedTransactionValidationError","HydratedTransactionValidationError","isTransfer"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/xl1-validation",
3
- "version": "1.26.9",
3
+ "version": "1.26.11",
4
4
  "description": "XYO Layer One SDK Validation",
5
5
  "homepage": "https://xylabs.com",
6
6
  "bugs": {
@@ -38,9 +38,9 @@
38
38
  "!**/*.test.*"
39
39
  ],
40
40
  "dependencies": {
41
- "@xyo-network/xl1-protocol-lib": "~1.26.9",
42
- "@xyo-network/xl1-protocol-sdk": "~1.26.9",
43
- "@xyo-network/xl1-schema": "~1.26.9"
41
+ "@xyo-network/xl1-protocol-lib": "~1.26.11",
42
+ "@xyo-network/xl1-protocol-sdk": "~1.26.11",
43
+ "@xyo-network/xl1-schema": "~1.26.11"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@opentelemetry/api": "^1.9.1",
@@ -3,20 +3,50 @@ import {
3
3
  } from '@xylabs/sdk-js'
4
4
  import type {
5
5
  HydratedBlockStateValidationFunction,
6
+ HydratedBlockWithHashMeta,
6
7
  TransactionBoundWitnessWithHashMeta,
7
8
  } from '@xyo-network/xl1-protocol-lib'
8
9
  import {
9
10
  HydratedBlockStateValidationError,
10
11
  isTransactionBoundWitnessWithHashMeta,
11
12
  isTransfer,
13
+ XYO_ZERO_ADDRESS,
12
14
  } from '@xyo-network/xl1-protocol-lib'
13
15
 
14
- /** Compute the maximum fee commitment for a transaction (base + priority + gasLimit * gasPrice). */
15
- function transactionFeeCost(tx: TransactionBoundWitnessWithHashMeta): bigint {
16
+ /** Compute the maximum fee commitment for a transaction (base + priority + gasLimit). */
17
+ function maxTransactionFeeCost(tx: TransactionBoundWitnessWithHashMeta): bigint {
16
18
  const {
17
- base, gasLimit, gasPrice, priority,
19
+ base, gasLimit, priority,
18
20
  } = tx.fees
19
- return hexToBigInt(base) + hexToBigInt(priority) + (hexToBigInt(gasLimit) * hexToBigInt(gasPrice))
21
+ return hexToBigInt(base) + hexToBigInt(priority) + hexToBigInt(gasLimit)
22
+ }
23
+
24
+ /** Accumulate outflows per address from fees and transfers in the given transactions. */
25
+ export function accumulateOutflows(
26
+ transactions: TransactionBoundWitnessWithHashMeta[],
27
+ payloads: HydratedBlockWithHashMeta[1],
28
+ ): Record<Address, bigint> {
29
+ const outflows: Record<Address, bigint> = {}
30
+
31
+ for (const tx of transactions) {
32
+ // Fee cost charged to the transaction sender
33
+ const feeCost = maxTransactionFeeCost(tx)
34
+ const feePayer = tx.from
35
+ outflows[feePayer] = (outflows[feePayer] ?? 0n) + feeCost
36
+
37
+ // Find transfer payloads belonging to this transaction
38
+ const txPayloadHashes = new Set(tx.payload_hashes)
39
+ for (const payload of payloads) {
40
+ if (!txPayloadHashes.has(payload._hash) || !isTransfer(payload)) continue
41
+ const { from } = payload
42
+ for (const [to, amount] of Object.entries(payload.transfers) as [Address, Hex][]) {
43
+ if (to === from) continue // self-transfers are not outflows
44
+ outflows[from] = (outflows[from] ?? 0n) + hexToBigInt(amount)
45
+ }
46
+ }
47
+ }
48
+
49
+ return outflows
20
50
  }
21
51
 
22
52
  /** Creates a block state validator that checks cumulative outflows per address do not exceed pre-block balances. */
@@ -29,25 +59,7 @@ export function BlockCumulativeBalanceValidatorFactory(): HydratedBlockStateVali
29
59
 
30
60
  if (transactions.length === 0) return []
31
61
 
32
- // Accumulate outflows per address
33
- const outflows: Record<Address, bigint> = {}
34
-
35
- for (const tx of transactions) {
36
- // Fee cost charged to the transaction sender
37
- const feeCost = transactionFeeCost(tx)
38
- const feePayer = tx.from
39
- outflows[feePayer] = (outflows[feePayer] ?? 0n) + feeCost
40
-
41
- // Find transfer payloads belonging to this transaction
42
- const txPayloadHashes = new Set(tx.payload_hashes)
43
- for (const payload of payloads) {
44
- if (!txPayloadHashes.has(payload._hash) || !isTransfer(payload)) continue
45
- const { from } = payload
46
- for (const amount of Object.values(payload.transfers) as Hex[]) {
47
- outflows[from] = (outflows[from] ?? 0n) + hexToBigInt(amount)
48
- }
49
- }
50
- }
62
+ const outflows = accumulateOutflows(transactions, payloads)
51
63
 
52
64
  // Query pre-block balances for all addresses with outflows
53
65
  const addresses = Object.keys(outflows) as Address[]
@@ -59,6 +71,8 @@ export function BlockCumulativeBalanceValidatorFactory(): HydratedBlockStateVali
59
71
  const chainId = await context.chainIdAtBlockNumber(blockBw.block)
60
72
  const errors: HydratedBlockStateValidationError[] = []
61
73
  for (const address of addresses) {
74
+ if (address === XYO_ZERO_ADDRESS) continue // Skip zero address as it's used for burn transactions and doesn't have a balance
75
+ // TODO: Add specific validation for block reward transfer
62
76
  const balance = balances[address] ?? 0n
63
77
  const totalOutflow = outflows[address]
64
78
  if (totalOutflow > balance) {
@@ -51,8 +51,7 @@ export const BoundWitnessReferencesValidator
51
51
  }
52
52
  }
53
53
  } catch (ex) {
54
- const error = new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], `validation excepted: ${ex}`)
55
- error.cause = ex
54
+ const error = new HydratedBoundWitnessValidationError(bw?._hash ?? ZERO_HASH, [bw, payloadSet], `validation excepted: ${ex}`, ex)
56
55
  errors.push(error)
57
56
  }
58
57
  return errors
@@ -23,8 +23,8 @@ export const TransactionJsonSchemaValidator: HydratedTransactionValidationFuncti
23
23
  tx?.[0]?._hash ?? ZERO_HASH,
24
24
  tx,
25
25
  `failed JSON schema validation: ${ajv.errorsText(validate.errors, { separator: '\n' })}`,
26
+ validate.errors,
26
27
  )
27
- error.cause = validate.errors
28
28
  errors.push(error)
29
29
  }
30
30
  } catch (ex) {