@aztec-labs/pxe 6.0.0-rc.2 → 6.0.0-rc.4

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.
Files changed (103) hide show
  1. package/dest/config/index.d.ts +1 -1
  2. package/dest/config/index.d.ts.map +1 -1
  3. package/dest/config/index.js +2 -1
  4. package/dest/contract_function_simulator/index.d.ts +2 -2
  5. package/dest/contract_function_simulator/index.d.ts.map +1 -1
  6. package/dest/contract_function_simulator/index.js +1 -1
  7. package/dest/contract_function_simulator/noir-structs/fact_collection.d.ts +6 -12
  8. package/dest/contract_function_simulator/noir-structs/fact_collection.d.ts.map +1 -1
  9. package/dest/contract_function_simulator/noir-structs/fact_collection.js +1 -15
  10. package/dest/contract_function_simulator/oracle/oracle_registry.d.ts +7 -7
  11. package/dest/contract_function_simulator/oracle/oracle_registry.d.ts.map +1 -1
  12. package/dest/contract_function_simulator/oracle/oracle_registry.js +19 -15
  13. package/dest/contract_function_simulator/oracle/oracle_type_mappings.d.ts +5 -2
  14. package/dest/contract_function_simulator/oracle/oracle_type_mappings.d.ts.map +1 -1
  15. package/dest/contract_function_simulator/oracle/oracle_type_mappings.js +26 -2
  16. package/dest/contract_function_simulator/oracle/private_execution.d.ts +1 -1
  17. package/dest/contract_function_simulator/oracle/private_execution.d.ts.map +1 -1
  18. package/dest/contract_function_simulator/oracle/private_execution.js +1 -0
  19. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts +15 -7
  20. package/dest/contract_function_simulator/oracle/utility_execution_oracle.d.ts.map +1 -1
  21. package/dest/contract_function_simulator/oracle/utility_execution_oracle.js +19 -3
  22. package/dest/contract_function_simulator/pick_notes.d.ts +1 -1
  23. package/dest/contract_function_simulator/pick_notes.d.ts.map +1 -1
  24. package/dest/contract_function_simulator/pick_notes.js +1 -0
  25. package/dest/entrypoints/server/circuit_recording/simulator_recorder_wrapper.d.ts +1 -1
  26. package/dest/entrypoints/server/circuit_recording/simulator_recorder_wrapper.d.ts.map +1 -1
  27. package/dest/entrypoints/server/circuit_recording/simulator_recorder_wrapper.js +1 -0
  28. package/dest/entrypoints/server/utils.d.ts +1 -1
  29. package/dest/entrypoints/server/utils.d.ts.map +1 -1
  30. package/dest/entrypoints/server/utils.js +3 -1
  31. package/dest/oracle_version.d.ts +3 -3
  32. package/dest/oracle_version.js +3 -3
  33. package/dest/private_kernel/batch_planner.d.ts +1 -1
  34. package/dest/private_kernel/batch_planner.d.ts.map +1 -1
  35. package/dest/private_kernel/batch_planner.js +1 -0
  36. package/dest/private_kernel/hints/test_utils.d.ts +1 -1
  37. package/dest/private_kernel/hints/test_utils.d.ts.map +1 -1
  38. package/dest/private_kernel/hints/test_utils.js +1 -0
  39. package/dest/private_kernel/private_kernel_execution_prover.d.ts +1 -1
  40. package/dest/private_kernel/private_kernel_execution_prover.d.ts.map +1 -1
  41. package/dest/private_kernel/private_kernel_execution_prover.js +1 -0
  42. package/dest/pxe.d.ts +27 -2
  43. package/dest/pxe.d.ts.map +1 -1
  44. package/dest/pxe.js +201 -128
  45. package/dest/storage/address_store/address_store.d.ts +1 -1
  46. package/dest/storage/address_store/address_store.d.ts.map +1 -1
  47. package/dest/storage/address_store/address_store.js +1 -0
  48. package/dest/storage/backwards_compatibility_tests/kv_store_snapshot.d.ts +1 -1
  49. package/dest/storage/backwards_compatibility_tests/kv_store_snapshot.d.ts.map +1 -1
  50. package/dest/storage/backwards_compatibility_tests/kv_store_snapshot.js +1 -0
  51. package/dest/storage/backwards_compatibility_tests/schema_tests.d.ts +1 -1
  52. package/dest/storage/backwards_compatibility_tests/schema_tests.d.ts.map +1 -1
  53. package/dest/storage/backwards_compatibility_tests/schema_tests.js +23 -1
  54. package/dest/storage/contract_store/contract_store.d.ts +2 -1
  55. package/dest/storage/contract_store/contract_store.d.ts.map +1 -1
  56. package/dest/storage/contract_store/contract_store.js +1 -0
  57. package/dest/storage/fact_store/fact_scope.d.ts +19 -0
  58. package/dest/storage/fact_store/fact_scope.d.ts.map +1 -0
  59. package/dest/storage/fact_store/fact_scope.js +37 -0
  60. package/dest/storage/fact_store/fact_service.d.ts +2 -2
  61. package/dest/storage/fact_store/fact_service.d.ts.map +1 -1
  62. package/dest/storage/fact_store/fact_service.js +19 -5
  63. package/dest/storage/fact_store/fact_store_keys.d.ts +6 -5
  64. package/dest/storage/fact_store/fact_store_keys.d.ts.map +1 -1
  65. package/dest/storage/fact_store/fact_store_keys.js +24 -8
  66. package/dest/storage/fact_store/index.d.ts +2 -1
  67. package/dest/storage/fact_store/index.d.ts.map +1 -1
  68. package/dest/storage/fact_store/index.js +1 -0
  69. package/dest/storage/fact_store/stored_fact.d.ts +9 -3
  70. package/dest/storage/fact_store/stored_fact.d.ts.map +1 -1
  71. package/dest/storage/fact_store/stored_fact.js +45 -3
  72. package/dest/storage/store_identity.d.ts +1 -1
  73. package/dest/storage/store_identity.d.ts.map +1 -1
  74. package/dest/storage/store_identity.js +1 -0
  75. package/package.json +21 -20
  76. package/src/config/index.ts +2 -1
  77. package/src/contract_function_simulator/index.ts +1 -0
  78. package/src/contract_function_simulator/noir-structs/fact_collection.ts +6 -22
  79. package/src/contract_function_simulator/oracle/oracle_registry.ts +15 -15
  80. package/src/contract_function_simulator/oracle/oracle_type_mappings.ts +21 -1
  81. package/src/contract_function_simulator/oracle/private_execution.ts +1 -0
  82. package/src/contract_function_simulator/oracle/utility_execution_oracle.ts +34 -8
  83. package/src/contract_function_simulator/pick_notes.ts +1 -0
  84. package/src/entrypoints/server/circuit_recording/simulator_recorder_wrapper.ts +1 -0
  85. package/src/entrypoints/server/utils.ts +4 -2
  86. package/src/oracle_version.ts +3 -3
  87. package/src/private_kernel/batch_planner.ts +1 -0
  88. package/src/private_kernel/hints/test_utils.ts +1 -0
  89. package/src/private_kernel/private_kernel_execution_prover.ts +1 -0
  90. package/src/pxe.ts +291 -189
  91. package/src/storage/address_store/address_store.ts +1 -0
  92. package/src/storage/backwards_compatibility_tests/__snapshots__/AddressStore.json +6 -6
  93. package/src/storage/backwards_compatibility_tests/__snapshots__/FactStore.json +20 -0
  94. package/src/storage/backwards_compatibility_tests/__snapshots__/KeyStore.json +12 -12
  95. package/src/storage/backwards_compatibility_tests/kv_store_snapshot.ts +1 -0
  96. package/src/storage/backwards_compatibility_tests/schema_tests.ts +18 -1
  97. package/src/storage/contract_store/contract_store.ts +1 -0
  98. package/src/storage/fact_store/fact_scope.ts +39 -0
  99. package/src/storage/fact_store/fact_service.ts +20 -5
  100. package/src/storage/fact_store/fact_store_keys.ts +28 -15
  101. package/src/storage/fact_store/index.ts +1 -0
  102. package/src/storage/fact_store/stored_fact.ts +50 -14
  103. package/src/storage/store_identity.ts +1 -0
@@ -1,51 +1,51 @@
1
1
  {
2
2
  "key_store": [
3
3
  {
4
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-ivpk_m",
4
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-ivpk_m",
5
5
  "value": "19e95c05f09cb86d1a6257572f9082ca15d4a02373c4d8c9cee23dc61a4c2a882f9017d51bca6616102d8a1d4dba89e4ce12881a2414576f01d2fec9492814a2"
6
6
  },
7
7
  {
8
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-ivpk_m_hash",
8
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-ivpk_m_hash",
9
9
  "value": "1fd17152d394f6a43b527c531590888d610d1bcb9aa71a02498ce820bbf4dc62"
10
10
  },
11
11
  {
12
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-ivsk_m",
12
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-ivsk_m",
13
13
  "value": "1fb01c42d1aaa2662041b899c77cb19e08192193acc5a94405f1b43c974eba7a"
14
14
  },
15
15
  {
16
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-nhk_m",
16
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-nhk_m",
17
17
  "value": "2dd30220767969b10044b1322585bb4c4df4e0c5d9b4d7b3045a879a8e3e91d2"
18
18
  },
19
19
  {
20
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-npk_m",
20
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-npk_m",
21
21
  "value": "214c7a24ddf501afc8734c7379ee502296289eb24cd8cd03cd435e128c06be0000987721e0963b4a5bd81ee862e2740d8849fd90537569064d3d3a4640e858d2"
22
22
  },
23
23
  {
24
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-npk_m_hash",
24
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-npk_m_hash",
25
25
  "value": "13ad6965d11a533a663d07507319f74ac3305325f6f72423c0b24aa1193268bb"
26
26
  },
27
27
  {
28
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-ovpk_m",
28
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-ovpk_m",
29
29
  "value": "2c19ff0b089b07da436e6f7be6b26cdc17b9cee504a528c071ef6a416299683d0b369479d6e50504702d0b56bb56e4e8e0e77171429896651d0c11621e82ebc3"
30
30
  },
31
31
  {
32
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-ovpk_m_hash",
32
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-ovpk_m_hash",
33
33
  "value": "2a1b054e998835d166711839b515d434b1fbc207ad4fdb5b2b43d6ac6b400871"
34
34
  },
35
35
  {
36
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-ovsk_m",
36
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-ovsk_m",
37
37
  "value": "279fe6ef7dd2477b919327b1ebab7497c41b9a7ccfa9d5d52e32698e3b85293b"
38
38
  },
39
39
  {
40
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-tpk_m",
40
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-tpk_m",
41
41
  "value": "2fcdd0d194adafd753dc0ed279f5cfb54ddcce1dd581d4631cf343ba9ee6c1ab2858abca6c00da3def156b1f12a6464384e326d5fa724c2fc2ff2b7f3fc8ad5f"
42
42
  },
43
43
  {
44
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-tpk_m_hash",
44
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-tpk_m_hash",
45
45
  "value": "019f5bb255e87523d43da83ebc9fa95169b653d4abef902e011e9eac620fc45b"
46
46
  },
47
47
  {
48
- "key": "utf8:0x2369ead09116908cedc1642d5e0c714ef288ecbc838e4d434f800a408914ce44-tsk_m",
48
+ "key": "utf8:0x20c1641e3181d87e3383458cd994b80833d42772f0afddb5ff41611c0524493a-tsk_m",
49
49
  "value": "183cb61099458d1564aa57c90c7091ac623a14092ab314150c9cfbb416810320"
50
50
  }
51
51
  ]
@@ -1,4 +1,5 @@
1
1
  import type { AztecAsyncArray, AztecAsyncMap, AztecAsyncSingleton, Key } from '@aztec-labs/kv-store';
2
+ import { Buffer } from 'buffer';
2
3
 
3
4
  /**
4
5
  * This file contains helpers that produce stable, snapshot-friendly text representations of our kv-stores'
@@ -48,11 +48,13 @@ import {
48
48
  TxEffect,
49
49
  TxHash,
50
50
  } from '@aztec-labs/stdlib/tx';
51
+ import { Buffer } from 'buffer';
51
52
 
52
53
  import { AddressStore } from '../address_store/address_store.js';
53
54
  import { AnchorBlockStore } from '../anchor_block_store/index.js';
54
55
  import { CapsuleStore } from '../capsule_store/capsule_store.js';
55
56
  import { ContractStore } from '../contract_store/contract_store.js';
57
+ import type { FactScope } from '../fact_store/fact_scope.js';
56
58
  import { FactStore } from '../fact_store/fact_store.js';
57
59
  import { FactCollectionKey } from '../fact_store/fact_store_keys.js';
58
60
  import { NoteStore } from '../note_store/note_store.js';
@@ -240,7 +242,7 @@ export const SCHEMA_TESTS: readonly SchemaTest[] = [
240
242
  const changeSetId = 'fixture-change-set';
241
243
  factStore.beginChangeSet(changeSetId);
242
244
  const contract = AztecAddress.fromBigIntUnsafe(100n);
243
- const scope = AztecAddress.fromBigIntUnsafe(1n);
245
+ const scope: FactScope = { type: 'account', account: AztecAddress.fromBigIntUnsafe(1n) };
244
246
  const factCollectionTypeId = new Fr(7n);
245
247
  const keyA = FactCollectionKey.from({
246
248
  contractAddress: contract,
@@ -265,6 +267,21 @@ export const SCHEMA_TESTS: readonly SchemaTest[] = [
265
267
  // A collection with a non-retractable and a retractable fact.
266
268
  await factStore.recordFact(keyB, new Fr(1n), [new Fr(9n)], undefined, changeSetId);
267
269
  await factStore.recordFact(keyB, new Fr(2n), [], { blockNumber: 5, blockHash: new Fr(1n) }, changeSetId);
270
+ // A collection under the public scope, with a non-retractable and a retractable fact.
271
+ const publicKey = FactCollectionKey.from({
272
+ contractAddress: contract,
273
+ scope: { type: 'public' },
274
+ factCollectionTypeId,
275
+ factCollectionId: new Fr(0xccn),
276
+ });
277
+ await factStore.recordFact(publicKey, new Fr(4n), [new Fr(8n)], undefined, changeSetId);
278
+ await factStore.recordFact(
279
+ publicKey,
280
+ new Fr(5n),
281
+ [new Fr(10n)],
282
+ { blockNumber: 7, blockHash: new Fr(3n) },
283
+ changeSetId,
284
+ );
268
285
  await kvStore.transactionAsync(() => factStore.commitChangeSet(changeSetId));
269
286
  },
270
287
  snapshotStore: async kvStore => ({
@@ -28,6 +28,7 @@ import {
28
28
  SerializableContractInstancePreimage,
29
29
  getContractClassFromArtifact,
30
30
  } from '@aztec-labs/stdlib/contract';
31
+ import { Buffer } from 'buffer';
31
32
 
32
33
  import { PrivateFunctionsTree } from './private_functions_tree.js';
33
34
 
@@ -0,0 +1,39 @@
1
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
2
+ import { AztecAddress } from '@aztec-labs/stdlib/aztec-address';
3
+
4
+ /**
5
+ * The scope a fact collection belongs to.
6
+ *
7
+ * - `account`: only executions where `account` is in scope can access the collection.
8
+ * - `public`: shared by all accounts, so any execution of the owning contract can access the collection.
9
+ */
10
+ export type FactScope = { type: 'account'; account: AztecAddress } | { type: 'public' };
11
+
12
+ // Noir decodes these discriminants, so their values are part of the oracle interface.
13
+ const ACCOUNT = 1;
14
+ const PUBLIC = 2;
15
+
16
+ /** Serializes a {@link FactScope} to its Noir `[kind, account]` field layout. */
17
+ export function factScopeToFields(scope: FactScope): Fr[] {
18
+ switch (scope.type) {
19
+ case 'account':
20
+ return [new Fr(ACCOUNT), scope.account.toField()];
21
+ case 'public':
22
+ return [new Fr(PUBLIC), Fr.ZERO];
23
+ }
24
+ }
25
+
26
+ /** Deserializes a {@link FactScope}, rejecting malformed values. */
27
+ export function factScopeFromFields(kind: number, account: Fr): FactScope {
28
+ switch (kind) {
29
+ case ACCOUNT:
30
+ return { type: 'account', account: AztecAddress.fromFieldUnsafe(account) };
31
+ case PUBLIC:
32
+ if (!account.isZero()) {
33
+ throw new Error(`Public fact scope must not name an account, got ${account}`);
34
+ }
35
+ return { type: 'public' };
36
+ default:
37
+ throw new Error(`Unrecognized fact scope kind: ${kind}`);
38
+ }
39
+ }
@@ -3,6 +3,7 @@ import type { AztecAddress } from '@aztec-labs/stdlib/aztec-address';
3
3
 
4
4
  import { assertAllowedScope } from '../allowed_scopes.js';
5
5
  import type { ChangeSetId } from '../staged_write_coordinator.js';
6
+ import type { FactScope } from './fact_scope.js';
6
7
  import type { FactStore } from './fact_store.js';
7
8
  import type { BlockReference, FactCollectionKey, FactCollectionTypeKey } from './fact_store_keys.js';
8
9
  import {
@@ -17,7 +18,7 @@ import type { Fact } from './stored_fact.js';
17
18
  * Wraps a {@link FactStore} with scope-based access control.
18
19
  *
19
20
  * Each method asserts scope validity before delegating to FactStore, gating which accounts a contract may record facts
20
- * under or read facts from.
21
+ * under or read facts from. The public scope belongs to no account, so it is always allowed.
21
22
  */
22
23
  export class FactService {
23
24
  constructor(
@@ -32,12 +33,12 @@ export class FactService {
32
33
  originBlock: BlockReference | undefined,
33
34
  changeSetId: ChangeSetId,
34
35
  ): Promise<void> {
35
- assertAllowedScope(factCollectionKey.scope, this.allowedScopes);
36
+ this.#assertAllowedFactScope(factCollectionKey.scope);
36
37
  return this.factStore.recordFact(factCollectionKey, factTypeId, payload, originBlock, changeSetId);
37
38
  }
38
39
 
39
40
  deleteFactCollection(factCollectionKey: FactCollectionKey, changeSetId: ChangeSetId): Promise<void> {
40
- assertAllowedScope(factCollectionKey.scope, this.allowedScopes);
41
+ this.#assertAllowedFactScope(factCollectionKey.scope);
41
42
  return this.factStore.deleteFactCollection(factCollectionKey, changeSetId);
42
43
  }
43
44
 
@@ -46,7 +47,7 @@ export class FactService {
46
47
  tips: TipBlockNumbers,
47
48
  changeSetId: ChangeSetId,
48
49
  ): Promise<FactCollectionWithOriginState | undefined> {
49
- assertAllowedScope(factCollectionKey.scope, this.allowedScopes);
50
+ this.#assertAllowedFactScope(factCollectionKey.scope);
50
51
  const collection = await this.factStore.getFactCollection(factCollectionKey, changeSetId);
51
52
  if (!collection) {
52
53
  return undefined;
@@ -59,11 +60,25 @@ export class FactService {
59
60
  tips: TipBlockNumbers,
60
61
  changeSetId: ChangeSetId,
61
62
  ): Promise<FactCollectionWithOriginState[]> {
62
- assertAllowedScope(factCollectionTypeKey.scope, this.allowedScopes);
63
+ this.#assertAllowedFactScope(factCollectionTypeKey.scope);
63
64
  const collections = await this.factStore.getFactCollectionsByType(factCollectionTypeKey, changeSetId);
64
65
  return collections.map(collection => ({ key: collection.key, facts: this.#annotate(collection.facts, tips) }));
65
66
  }
66
67
 
68
+ #assertAllowedFactScope(scope: FactScope): void {
69
+ switch (scope.type) {
70
+ case 'account':
71
+ assertAllowedScope(scope.account, this.allowedScopes);
72
+ break;
73
+ case 'public':
74
+ break;
75
+ default: {
76
+ const _exhaustive: never = scope;
77
+ throw new Error(`Unhandled fact scope type: ${JSON.stringify(_exhaustive)}`);
78
+ }
79
+ }
80
+ }
81
+
67
82
  #annotate(facts: Fact[], tips: TipBlockNumbers): FactWithOriginState[] {
68
83
  return facts.map(fact => toFactWithOriginState(fact, tips));
69
84
  }
@@ -2,32 +2,47 @@ import { Fr } from '@aztec-labs/foundation/curves/bn254';
2
2
  import type { FieldsOf } from '@aztec-labs/foundation/types';
3
3
  import { AztecAddress } from '@aztec-labs/stdlib/aztec-address';
4
4
 
5
+ import type { FactScope } from './fact_scope.js';
6
+
5
7
  /** A block, identified by its number and hash. */
6
8
  export type BlockReference = { blockNumber: number; blockHash: Fr };
7
9
 
8
- /** Facts are always tied to a real scope; the zero address is not a valid fact scope. */
9
- function assertNonZeroScope(scope: AztecAddress): void {
10
- if (scope.equals(AztecAddress.ZERO)) {
11
- throw new Error('scope must not be the zero address');
10
+ // Account scopes are keyed by their hex address, which can never equal this token.
11
+ const PUBLIC_SCOPE_KEY_PART = 'public';
12
+
13
+ function scopeToKeyPart(scope: FactScope): string {
14
+ switch (scope.type) {
15
+ case 'account':
16
+ return scope.account.toString();
17
+ case 'public':
18
+ return PUBLIC_SCOPE_KEY_PART;
19
+ default: {
20
+ const _exhaustive: never = scope;
21
+ throw new Error(`Unhandled fact scope type: ${JSON.stringify(_exhaustive)}`);
22
+ }
12
23
  }
13
24
  }
14
25
 
26
+ function scopeFromKeyPart(part: string): FactScope {
27
+ return part === PUBLIC_SCOPE_KEY_PART
28
+ ? { type: 'public' }
29
+ : { type: 'account', account: AztecAddress.fromStringUnsafe(part) };
30
+ }
31
+
15
32
  /** Identifies all fact collections of one type within a contract, for one scope. */
16
33
  export class FactCollectionTypeKey {
17
34
  constructor(
18
35
  public readonly contractAddress: AztecAddress,
19
- public readonly scope: AztecAddress,
36
+ public readonly scope: FactScope,
20
37
  public readonly factCollectionTypeId: Fr,
21
- ) {
22
- assertNonZeroScope(scope);
23
- }
38
+ ) {}
24
39
 
25
40
  static from(fields: FieldsOf<FactCollectionTypeKey>): FactCollectionTypeKey {
26
41
  return new FactCollectionTypeKey(fields.contractAddress, fields.scope, fields.factCollectionTypeId);
27
42
  }
28
43
 
29
44
  toString(): string {
30
- return `${this.contractAddress}:${this.scope}:${this.factCollectionTypeId}`;
45
+ return `${this.contractAddress}:${scopeToKeyPart(this.scope)}:${this.factCollectionTypeId}`;
31
46
  }
32
47
  }
33
48
 
@@ -35,12 +50,10 @@ export class FactCollectionTypeKey {
35
50
  export class FactCollectionKey {
36
51
  constructor(
37
52
  public readonly contractAddress: AztecAddress,
38
- public readonly scope: AztecAddress,
53
+ public readonly scope: FactScope,
39
54
  public readonly factCollectionTypeId: Fr,
40
55
  public readonly factCollectionId: Fr,
41
- ) {
42
- assertNonZeroScope(scope);
43
- }
56
+ ) {}
44
57
 
45
58
  static from(fields: FieldsOf<FactCollectionKey>): FactCollectionKey {
46
59
  return new FactCollectionKey(
@@ -56,7 +69,7 @@ export class FactCollectionKey {
56
69
  const [contractAddress, scope, factCollectionTypeId, factCollectionId] = str.split(':');
57
70
  return new FactCollectionKey(
58
71
  AztecAddress.fromStringUnsafe(contractAddress),
59
- AztecAddress.fromStringUnsafe(scope),
72
+ scopeFromKeyPart(scope),
60
73
  Fr.fromString(factCollectionTypeId),
61
74
  Fr.fromString(factCollectionId),
62
75
  );
@@ -68,6 +81,6 @@ export class FactCollectionKey {
68
81
  }
69
82
 
70
83
  toString(): string {
71
- return `${this.contractAddress}:${this.scope}:${this.factCollectionTypeId}:${this.factCollectionId}`;
84
+ return `${this.contractAddress}:${scopeToKeyPart(this.scope)}:${this.factCollectionTypeId}:${this.factCollectionId}`;
72
85
  }
73
86
  }
@@ -1,5 +1,6 @@
1
1
  export { FactStore, type FactCollection } from './fact_store.js';
2
2
  export { FactService } from './fact_service.js';
3
+ export { type FactScope, factScopeFromFields, factScopeToFields } from './fact_scope.js';
3
4
  export { FactCollectionKey, FactCollectionTypeKey, type BlockReference } from './fact_store_keys.js';
4
5
  export type { Fact } from './stored_fact.js';
5
6
  export {
@@ -2,7 +2,9 @@ import { sha256ToField } from '@aztec-labs/foundation/crypto/sha256';
2
2
  import { Fr } from '@aztec-labs/foundation/curves/bn254';
3
3
  import { BufferReader, serializeToBuffer } from '@aztec-labs/foundation/serialize';
4
4
  import { AztecAddress } from '@aztec-labs/stdlib/aztec-address';
5
+ import { Buffer } from 'buffer';
5
6
 
7
+ import type { FactScope } from './fact_scope.js';
6
8
  import { type BlockReference, FactCollectionKey } from './fact_store_keys.js';
7
9
 
8
10
  /** A fact as returned by the fact store. */
@@ -35,24 +37,44 @@ export class StoredFact {
35
37
  }
36
38
 
37
39
  toBuffer(): Buffer {
38
- return serializeToBuffer(
39
- this.factCollectionKey.contractAddress,
40
- this.factCollectionKey.scope,
41
- this.factCollectionKey.factCollectionTypeId,
42
- this.factCollectionKey.factCollectionId,
43
- this.factTypeId,
44
- this.payload.length,
45
- ...this.payload,
46
- this.originBlock !== undefined,
47
- this.originBlock ? this.originBlock.blockNumber : 0,
48
- this.originBlock ? this.originBlock.blockHash : Fr.ZERO,
49
- );
40
+ const { scope } = this.factCollectionKey;
41
+ const record = (scopeAccount: AztecAddress) =>
42
+ serializeToBuffer(
43
+ this.factCollectionKey.contractAddress,
44
+ scopeAccount,
45
+ this.factCollectionKey.factCollectionTypeId,
46
+ this.factCollectionKey.factCollectionId,
47
+ this.factTypeId,
48
+ this.payload.length,
49
+ ...this.payload,
50
+ this.originBlock !== undefined,
51
+ this.originBlock ? this.originBlock.blockNumber : 0,
52
+ this.originBlock ? this.originBlock.blockHash : Fr.ZERO,
53
+ );
54
+ // For DB schema compatibility, account records use the schema's record layout unchanged, and public records extend
55
+ // it with a trailing marker instead of adding a scope kind field.
56
+ switch (scope.type) {
57
+ case 'account':
58
+ return record(scope.account);
59
+ case 'public':
60
+ return Buffer.concat([record(AztecAddress.ZERO), serializeToBuffer(true)]);
61
+ default: {
62
+ const _exhaustive: never = scope;
63
+ throw new Error(`Unhandled fact scope type: ${JSON.stringify(_exhaustive)}`);
64
+ }
65
+ }
50
66
  }
51
67
 
52
- static fromBuffer(buffer: Buffer | BufferReader): StoredFact {
68
+ /**
69
+ * Inverse of {@link toBuffer}.
70
+ *
71
+ * `buffer` must end where the record does, since any bytes after an account record are read as the public scope
72
+ * marker.
73
+ */
74
+ static fromBuffer(buffer: Buffer): StoredFact {
53
75
  const reader = BufferReader.asReader(buffer);
54
76
  const contractAddress = reader.readObject(AztecAddress);
55
- const scope = reader.readObject(AztecAddress);
77
+ const scopeAccount = reader.readObject(AztecAddress);
56
78
  const factCollectionTypeId = reader.readObject(Fr);
57
79
  const factCollectionId = reader.readObject(Fr);
58
80
  const factTypeId = reader.readObject(Fr);
@@ -62,6 +84,20 @@ export class StoredFact {
62
84
  const blockNumber = reader.readNumber();
63
85
  const blockHash = reader.readObject(Fr);
64
86
  const originBlock = hasOriginBlock ? { blockNumber, blockHash } : undefined;
87
+ let scope: FactScope = { type: 'account', account: scopeAccount };
88
+ if (!reader.isEmpty()) {
89
+ const malformed = `Malformed public scope marker in stored fact of contract ${contractAddress}`;
90
+ if (!reader.readBoolean()) {
91
+ throw new Error(`${malformed}: marker is false`);
92
+ }
93
+ if (!reader.isEmpty()) {
94
+ throw new Error(`${malformed}: trailing bytes after the marker`);
95
+ }
96
+ if (!scopeAccount.isZero()) {
97
+ throw new Error(`${malformed}: record names account ${scopeAccount}`);
98
+ }
99
+ scope = { type: 'public' };
100
+ }
65
101
  return new StoredFact(
66
102
  new FactCollectionKey(contractAddress, scope, factCollectionTypeId, factCollectionId),
67
103
  factTypeId,
@@ -1,6 +1,7 @@
1
1
  import type { EthAddress } from '@aztec-labs/foundation/eth-address';
2
2
  import type { AztecAsyncKVStore } from '@aztec-labs/kv-store';
3
3
  import { DatabaseVersion } from '@aztec-labs/stdlib/database-version/version';
4
+ import { Buffer } from 'buffer';
4
5
 
5
6
  /** The triple that determine which physical store a logical store name maps to. */
6
7
  export type StoreIdentity = {