@midnight-ntwrk/midnight-js-types 4.1.1 → 5.0.0-beta.1

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/dist/index.d.mts CHANGED
@@ -1,12 +1,13 @@
1
1
  import { ContractExecutable } from '@midnight-ntwrk/midnight-js-protocol/compact-js/effect';
2
+ import { SigningKey, ContractAddress as ContractAddress$2, ContractState } from '@midnight-ntwrk/midnight-js-protocol/compact-runtime';
2
3
  import { ContractAddress as ContractAddress$1 } from '@midnight-ntwrk/midnight-js-protocol/platform-js';
3
4
  import { Option, Exit, ConfigError } from 'effect';
4
5
  import { ManagedRuntime } from 'effect/ManagedRuntime';
5
- import { Contract } from '@midnight-ntwrk/midnight-js-protocol/compact-js';
6
+ import { Contract, ContractKeyLocation } from '@midnight-ntwrk/midnight-js-protocol/compact-js';
7
+ export { ContractKeyLocation, encodeContractKeyLocation, hashVerifierKey, parseContractKeyLocation } from '@midnight-ntwrk/midnight-js-protocol/compact-js';
6
8
  import { Transaction, SignatureEnabled, Proof, Binding, TransactionId, TransactionHash, ContractAddress, IntentHash, RawTokenType, FinalizedTransaction, UnprovenTransaction, PreBinding, ProvingProvider, CostModel, ZswapChainState, LedgerParameters, CoinPublicKey, EncPublicKey } from '@midnight-ntwrk/midnight-js-protocol/ledger';
7
9
  export { Transaction } from '@midnight-ntwrk/midnight-js-protocol/ledger';
8
10
  import { LogFn } from 'pino';
9
- import { ContractAddress as ContractAddress$2, SigningKey, ContractState } from '@midnight-ntwrk/midnight-js-protocol/compact-runtime';
10
11
  import { Observable } from 'rxjs';
11
12
 
12
13
  type AnyProvableCircuitId = Contract.ProvableCircuitId<Contract.Any>;
@@ -313,7 +314,7 @@ type ContractExecutableRuntimeOptions = {
313
314
  /** The current user's ZSwap public key. */
314
315
  readonly coinPublicKey: string;
315
316
  /** The signing key to add as the to-be-deployed contract's maintenance authority. */
316
- readonly signingKey?: string;
317
+ readonly signingKey?: SigningKey;
317
318
  };
318
319
  /**
319
320
  * Constructs an Effect managed runtime configured to execute contract executables.
@@ -896,6 +897,19 @@ type BlockHashConfig = {
896
897
  */
897
898
  readonly blockHash: string;
898
899
  };
900
+ /**
901
+ * Minimal identifying information for a block.
902
+ */
903
+ type BlockInfo = {
904
+ /**
905
+ * The hex-encoded block hash.
906
+ */
907
+ readonly hash: string;
908
+ /**
909
+ * The block height.
910
+ */
911
+ readonly height: number;
912
+ };
899
913
  /**
900
914
  * The configuration for a contract state observable. The corresponding observables may begin at different
901
915
  * places (e.g. after a specific transaction identifier / block height) depending on the configuration, but
@@ -909,11 +923,191 @@ type ContractStateObservableConfig = ((TxIdConfig | BlockHashConfig | BlockHeigh
909
923
  */
910
924
  readonly inclusive?: boolean;
911
925
  }) | Latest | All;
926
+ /**
927
+ * The eleven contract event variants surfaced by the indexer (MIP-0002 public
928
+ * contract log emission). The variant *set* is identical to compact-js's
929
+ * `LogEventType`; only the string casing differs (PascalCase here, kebab-case
930
+ * in compact-js, SCREAMING_SNAKE on the indexer wire). Adding a variant is a
931
+ * breaking change — the mapping, filter translation, and exhaustiveness guards
932
+ * all key off this union.
933
+ */
934
+ type ContractEventType = 'ShieldedSpend' | 'ShieldedReceive' | 'ShieldedMint' | 'ShieldedBurn' | 'UnshieldedSpend' | 'UnshieldedReceive' | 'UnshieldedMint' | 'UnshieldedBurn' | 'Paused' | 'Unpaused' | 'Misc';
935
+ /**
936
+ * A `sender` / `recipient` on an unshielded event. The indexer returns a tagged
937
+ * union (`Either<ZswapCoinPublicKey, ContractAddress>`); this preserves the
938
+ * discriminator so consumers can tell a user address from a contract address
939
+ * rather than receiving a bare, ambiguous string.
940
+ */
941
+ interface ContractEventAddress {
942
+ /** Which kind of address `value` holds. */
943
+ readonly kind: 'user' | 'contract';
944
+ /** The hex-encoded address. */
945
+ readonly value: string;
946
+ }
947
+ /**
948
+ * Fields common to every {@link ContractEvent} variant, regardless of type.
949
+ */
950
+ interface ContractEventBase {
951
+ /**
952
+ * Monotonic indexer cursor for this event. Inclusive resumption point — to
953
+ * resume *after* this event, pass `{ fromId: id + 1 }`.
954
+ */
955
+ readonly id: number;
956
+ /**
957
+ * Highest event id the indexer currently knows (the chain tip for events).
958
+ * Compare against {@link id} to detect catch-up / whether more events exist.
959
+ */
960
+ readonly maxId: number;
961
+ /**
962
+ * Payload schema version — selects the (future) per-event payload decoder.
963
+ * Iteration-1 events are `version: 1`.
964
+ */
965
+ readonly version: number;
966
+ /** Address of the contract that emitted the event. */
967
+ readonly contractAddress: ContractAddress;
968
+ /**
969
+ * Indexer-internal `BIGSERIAL` row id of the emitting transaction — **not**
970
+ * the chain transaction hash. To fetch the chain transaction, issue a
971
+ * separate query. Note the asymmetry with {@link ContractEventFilterBase.transactionHash},
972
+ * which narrows by chain hash.
973
+ */
974
+ readonly transactionId: number;
975
+ /**
976
+ * Opaque hex `VersionedLogItem` bytes, carried verbatim. Never decoded or
977
+ * validated by this provider — the forward bridge to a future compact-js
978
+ * payload decoder.
979
+ */
980
+ readonly raw: string;
981
+ }
982
+ /**
983
+ * A decoded contract event. Discriminated union keyed on `eventType`; narrow on
984
+ * it to access the variant-specific payload fields.
985
+ *
986
+ * `amount` is always a `string` (encodes up to a 16-byte integer) — never round
987
+ * it through `Number()`. Absent nullable fields are normalized to `undefined`
988
+ * (never `null`).
989
+ */
990
+ type ContractEvent = (ContractEventBase & {
991
+ readonly eventType: 'ShieldedSpend';
992
+ readonly nullifier: string;
993
+ }) | (ContractEventBase & {
994
+ readonly eventType: 'ShieldedReceive';
995
+ readonly commitment: string;
996
+ readonly ciphertext?: string;
997
+ readonly receivingContractAddress?: string;
998
+ }) | (ContractEventBase & {
999
+ readonly eventType: 'ShieldedMint';
1000
+ readonly commitment: string;
1001
+ readonly domainSep: string;
1002
+ readonly amount?: string;
1003
+ }) | (ContractEventBase & {
1004
+ readonly eventType: 'ShieldedBurn';
1005
+ readonly nullifier: string;
1006
+ readonly amount?: string;
1007
+ }) | (ContractEventBase & {
1008
+ readonly eventType: 'UnshieldedSpend';
1009
+ readonly sender: ContractEventAddress;
1010
+ readonly domainSep: string;
1011
+ readonly tokenType: string;
1012
+ readonly amount: string;
1013
+ }) | (ContractEventBase & {
1014
+ readonly eventType: 'UnshieldedReceive';
1015
+ readonly recipient: ContractEventAddress;
1016
+ readonly domainSep: string;
1017
+ readonly tokenType: string;
1018
+ readonly amount: string;
1019
+ }) | (ContractEventBase & {
1020
+ readonly eventType: 'UnshieldedMint';
1021
+ readonly domainSep: string;
1022
+ readonly tokenType: string;
1023
+ readonly amount: string;
1024
+ }) | (ContractEventBase & {
1025
+ readonly eventType: 'UnshieldedBurn';
1026
+ readonly sender: ContractEventAddress;
1027
+ readonly tokenType: string;
1028
+ readonly amount: string;
1029
+ }) | (ContractEventBase & {
1030
+ readonly eventType: 'Paused';
1031
+ }) | (ContractEventBase & {
1032
+ readonly eventType: 'Unpaused';
1033
+ }) | (ContractEventBase & {
1034
+ readonly eventType: 'Misc';
1035
+ readonly name: string;
1036
+ readonly payload: string;
1037
+ });
1038
+ /**
1039
+ * A single prefix filter on an indexed field of a standard event. `prefix` is
1040
+ * hex-encoded; the empty string matches all values.
1041
+ */
1042
+ interface ContractEventFieldPrefix {
1043
+ readonly fieldName: string;
1044
+ readonly prefix: string;
1045
+ }
1046
+ /**
1047
+ * Filter fields shared by the query and the subscription.
1048
+ */
1049
+ interface ContractEventFilterBase {
1050
+ /** Required: the contract whose events to return. */
1051
+ readonly contractAddress: ContractAddress;
1052
+ /**
1053
+ * Optional subset of event types. Omit to mean "all types". An empty array
1054
+ * is rejected (it would silently match nothing).
1055
+ */
1056
+ readonly types?: ContractEventType[];
1057
+ /**
1058
+ * Optional prefix filters on indexed fields. Accepted only when every
1059
+ * filtered type is a standard (non-`Misc`) variant — see method docs.
1060
+ */
1061
+ readonly fieldPrefixes?: ContractEventFieldPrefix[];
1062
+ /** Optional: narrow to events emitted from the transaction with this chain hash. */
1063
+ readonly transactionHash?: string;
1064
+ }
1065
+ /**
1066
+ * Filter for {@link PublicDataProvider.queryContractEvents}. `fromBlock` /
1067
+ * `toBlock` are inclusive block-height bounds for a finite, point-in-time read.
1068
+ */
1069
+ interface ContractEventQueryFilter extends ContractEventFilterBase {
1070
+ readonly fromBlock?: number;
1071
+ readonly toBlock?: number;
1072
+ }
1073
+ /**
1074
+ * Filter for {@link PublicDataProvider.contractEventsObservable}. The stream
1075
+ * start is supplied separately via {@link ContractEventCursor}; `toBlock`
1076
+ * terminates the stream once the chain reaches that height.
1077
+ */
1078
+ interface ContractEventSubscriptionFilter extends ContractEventFilterBase {
1079
+ readonly toBlock?: number;
1080
+ }
1081
+ /**
1082
+ * Where a subscription begins. Exactly one addressing mode per call — two
1083
+ * competing start points are unrepresentable by construction.
1084
+ */
1085
+ type ContractEventCursor = {
1086
+ readonly fromId: number;
1087
+ } | {
1088
+ readonly fromBlock: number;
1089
+ };
1090
+ /**
1091
+ * Pagination window for {@link PublicDataProvider.queryContractEvents}.
1092
+ * `offset` is only stable within a window with a fixed upper bound — pin
1093
+ * `toBlock` for multi-page reads.
1094
+ */
1095
+ interface ContractEventsPage {
1096
+ readonly limit?: number;
1097
+ readonly offset?: number;
1098
+ }
912
1099
  /**
913
1100
  * Interface for a public data service. This service retrieves public data from the blockchain.
914
1101
  * TODO: Add timeouts or retry limits to 'watchFor' queries.
915
1102
  */
916
1103
  interface PublicDataProvider {
1104
+ /**
1105
+ * Retrieves a block. If no block hash or block height is provided, the latest block is returned.
1106
+ * Immediately returns null if no matching block is found.
1107
+ * @param config The configuration of the query identifying the block of interest.
1108
+ * If `undefined` returns the latest block.
1109
+ */
1110
+ queryBlock(config?: BlockHeightConfig | BlockHashConfig): Promise<BlockInfo | null>;
917
1111
  /**
918
1112
  * Retrieves the on-chain state of a contract. If no block hash or block height are provided, the
919
1113
  * contract state at the address in the latest block is returned.
@@ -1010,6 +1204,56 @@ interface PublicDataProvider {
1010
1204
  * @return {Observable<UnshieldedBalances>} An observable that emits the unshielded balances for the provided address.
1011
1205
  */
1012
1206
  unshieldedBalancesObservable(address: ContractAddress, config: ContractStateObservableConfig): Observable<UnshieldedBalances>;
1207
+ /**
1208
+ * Queries contract events for a contract address — a finite, paginated,
1209
+ * point-in-time read.
1210
+ *
1211
+ * Results are returned in ascending `id` order. The result is a plain array
1212
+ * with no total count: detect the end via `result.length < limit`, and read
1213
+ * `maxId` on the last item to see how far the tip is.
1214
+ *
1215
+ * When `page.limit` is omitted an implementation-defined default page size is
1216
+ * applied (never an undocumented server default). `offset` is only stable
1217
+ * within a window with a fixed upper bound — pin `filter.toBlock` for
1218
+ * multi-page reads, or prefer the `getAllContractEvents` helper / the
1219
+ * subscription for tailing.
1220
+ *
1221
+ * Fails fast (synchronously, before any network call) on an invalid
1222
+ * `contractAddress`, an empty `types` array, `fieldPrefixes` combined with
1223
+ * `Misc` (or with `types` omitted), or an unknown `fieldName`. Network /
1224
+ * GraphQL errors reject the promise — an empty array always means "no
1225
+ * matching events", never a swallowed error.
1226
+ *
1227
+ * @param filter The events to return; `contractAddress` is required.
1228
+ * @param page Optional pagination window.
1229
+ */
1230
+ queryContractEvents(filter: ContractEventQueryFilter, page?: ContractEventsPage): Promise<ContractEvent[]>;
1231
+ /**
1232
+ * Streams contract events for a contract address — replay from a cursor, then
1233
+ * live, in one continuous stream.
1234
+ *
1235
+ * The start is supplied via `opts.startAt`: `{ fromId }` resumes inclusively
1236
+ * from a known event id, `{ fromBlock }` starts from a block height. Omitting
1237
+ * `startAt` streams from the start of history. The indexer replays historical
1238
+ * events from that point in monotonic `id` order, then continues live — there
1239
+ * is no separate backfill query and no client-side dedup.
1240
+ *
1241
+ * `{ fromId }` is **inclusive**; to resume *after* the last seen event pass
1242
+ * `{ fromId: lastSeenId + 1 }`.
1243
+ *
1244
+ * `filter.toBlock` completes the stream once the chain reaches that height;
1245
+ * without it the stream runs until unsubscribed or the provider is disposed.
1246
+ * Delivery is **at-least-once** across transport reconnects (the provider
1247
+ * does not advance the cursor) — persisting consumers should dedup by `id`.
1248
+ * Transport failures surface as an observable `error`, never a silent
1249
+ * completion.
1250
+ *
1251
+ * @param filter The events to stream; `contractAddress` is required.
1252
+ * @param opts Optional stream start.
1253
+ */
1254
+ contractEventsObservable(filter: ContractEventSubscriptionFilter, opts?: {
1255
+ startAt?: ContractEventCursor;
1256
+ }): Observable<ContractEvent>;
1013
1257
  }
1014
1258
 
1015
1259
  /**
@@ -1065,5 +1309,64 @@ interface MidnightProviders<PCK extends AnyProvableCircuitId = AnyProvableCircui
1065
1309
  readonly loggerProvider?: LoggerProvider;
1066
1310
  }
1067
1311
 
1068
- export { ExportDecryptionError, FailEntirely, FailFallible, ImportConflictError, InvalidExportFormatError, InvalidProtocolSchemeError, LogLevel, MAX_EXPORT_SIGNING_KEYS, MAX_EXPORT_STATES, PrivateStateExportError, PrivateStateImportError, SegmentFail, SegmentSuccess, SigningKeyExportError, SucceedEntirely, ZKConfigProvider, asContractAddress, asEffectOption, createProofProvider, createProverKey, createVerifierKey, createZKIR, exitResultOrError, makeContractExecutableRuntime, zkConfigToProvingKeyMaterial };
1069
- export type { All, AnyPrivateState, AnyProvableCircuitId, BlockHash, BlockHashConfig, BlockHeightConfig, ContractExecutableRuntimeOptions, ContractStateObservableConfig, ExportPrivateStatesOptions, ExportSigningKeysOptions, Fees, FinalizedTxData, ImportPrivateStatesOptions, ImportPrivateStatesResult, ImportSigningKeysOptions, ImportSigningKeysResult, KeyMaterialProvider, Latest, LoggerProvider, MidnightProvider, MidnightProviders, PrivateStateExport, PrivateStateId, PrivateStateImportErrorCause, PrivateStateProvider, ProofProvider, ProveTxConfig, ProverKey, PublicDataProvider, SegmentStatus, SigningKeyExport, TxIdConfig, TxStatus, UnboundTransaction, UnshieldedBalance, UnshieldedBalances, UnshieldedUtxo, UnshieldedUtxos, VerifierKey, WalletProvider, ZKConfig, ZKIR };
1312
+ /**
1313
+ * Thrown when a contract key location parses but no artifact source contains a bundle whose
1314
+ * verifier key matches the deployed one — i.e. the local artifacts have drifted from (or were
1315
+ * never compiled for) the deployed contract.
1316
+ */
1317
+ declare class ZKArtifactNotFoundError extends Error {
1318
+ readonly keyLocation: ContractKeyLocation;
1319
+ constructor(keyLocation: ContractKeyLocation);
1320
+ }
1321
+ /**
1322
+ * Resolves canonical contract key locations to ZK artifacts across a *set* of compiled-contract
1323
+ * artifact sources.
1324
+ *
1325
+ * A cross-contract call transaction carries one proof per contract in the call tree, so proving
1326
+ * requires artifacts for several compiled contracts, keyed by `(contractAddress, circuitId)`. No
1327
+ * registration of addresses is required: the binding is *derived* by joining on the verifier key.
1328
+ * Each location embeds the SHA-256 of the call's deployed verifier key (known at transaction
1329
+ * assembly from the contract's resolved on-chain state), and resolution selects the source whose
1330
+ * local verifier key for the circuit matches. The join is sound because it is the predicate the
1331
+ * chain itself enforces — a proof must verify against the deployed key — and it makes the
1332
+ * resolution immune to redeploys, multiple deployments of one contract, and circuit-name
1333
+ * collisions across contracts.
1334
+ *
1335
+ * The sources are the per-contract {@link ZKConfigProvider}s the application already constructs;
1336
+ * resolutions are memoized per location.
1337
+ */
1338
+ declare class ZKConfigRegistry {
1339
+ private readonly sources;
1340
+ private readonly resolved;
1341
+ /**
1342
+ * @param sources The compiled-contract artifact sources to resolve against — one per compiled
1343
+ * contract the application can call (its own contracts and any cross-contract call targets).
1344
+ */
1345
+ constructor(sources: Iterable<ZKConfigProvider<string>>);
1346
+ /**
1347
+ * Resolves the ZK artifacts for a structured contract key.
1348
+ *
1349
+ * @param location The contract address, circuit, and deployed verifier key hash to resolve.
1350
+ * @throws ZKArtifactNotFoundError If no source's verifier key for the circuit matches.
1351
+ */
1352
+ get(location: ContractKeyLocation): Promise<ZKConfig<string>>;
1353
+ /**
1354
+ * Resolves the ZK artifacts for a key-location string from a proof preimage.
1355
+ *
1356
+ * @param keyLocation The key-location string.
1357
+ * @returns The matched artifacts, or `undefined` if `keyLocation` is not a contract key
1358
+ * location (for example, a `midnight/` protocol builtin, which provers resolve elsewhere).
1359
+ * @throws ZKArtifactNotFoundError If `keyLocation` is a contract key location but no source's
1360
+ * verifier key for the circuit matches the embedded hash.
1361
+ */
1362
+ resolveKeyLocation(keyLocation: string): Promise<ZKConfig<string> | undefined>;
1363
+ /**
1364
+ * Adapts this registry to the DApp connector's {@link KeyMaterialProvider}, allowing a wallet
1365
+ * to resolve the key locations of a transaction assembled by this application.
1366
+ */
1367
+ asKeyMaterialProvider(): KeyMaterialProvider;
1368
+ private resolve;
1369
+ }
1370
+
1371
+ export { ExportDecryptionError, FailEntirely, FailFallible, ImportConflictError, InvalidExportFormatError, InvalidProtocolSchemeError, LogLevel, MAX_EXPORT_SIGNING_KEYS, MAX_EXPORT_STATES, PrivateStateExportError, PrivateStateImportError, SegmentFail, SegmentSuccess, SigningKeyExportError, SucceedEntirely, ZKArtifactNotFoundError, ZKConfigProvider, ZKConfigRegistry, asContractAddress, asEffectOption, createProofProvider, createProverKey, createVerifierKey, createZKIR, exitResultOrError, makeContractExecutableRuntime, zkConfigToProvingKeyMaterial };
1372
+ export type { All, AnyPrivateState, AnyProvableCircuitId, BlockHash, BlockHashConfig, BlockHeightConfig, BlockInfo, ContractEvent, ContractEventAddress, ContractEventBase, ContractEventCursor, ContractEventFieldPrefix, ContractEventFilterBase, ContractEventQueryFilter, ContractEventSubscriptionFilter, ContractEventType, ContractEventsPage, ContractExecutableRuntimeOptions, ContractStateObservableConfig, ExportPrivateStatesOptions, ExportSigningKeysOptions, Fees, FinalizedTxData, ImportPrivateStatesOptions, ImportPrivateStatesResult, ImportSigningKeysOptions, ImportSigningKeysResult, KeyMaterialProvider, Latest, LoggerProvider, MidnightProvider, MidnightProviders, PrivateStateExport, PrivateStateId, PrivateStateImportErrorCause, PrivateStateProvider, ProofProvider, ProveTxConfig, ProverKey, PublicDataProvider, SegmentStatus, SigningKeyExport, TxIdConfig, TxStatus, UnboundTransaction, UnshieldedBalance, UnshieldedBalances, UnshieldedUtxo, UnshieldedUtxos, VerifierKey, WalletProvider, ZKConfig, ZKIR };