@midnightntwrk/wallet-sdk-capabilities 4.0.0-rc.1 → 4.0.0

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 (62) hide show
  1. package/README.md +10 -3
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/liveness/livenessReads.js +11 -10
  5. package/dist/liveness/livenessService.js +4 -5
  6. package/dist/proving/index.d.ts +1 -0
  7. package/dist/proving/index.js +1 -0
  8. package/dist/proving/provingService.d.ts +16 -49
  9. package/dist/proving/provingService.js +1 -89
  10. package/dist/proving/v8ProvingService.d.ts +34 -7
  11. package/dist/proving/v8ProvingService.js +36 -6
  12. package/dist/proving/v9ProvingService.d.ts +108 -0
  13. package/dist/proving/v9ProvingService.js +146 -0
  14. package/dist/proving/versionedProving.d.ts +22 -4
  15. package/dist/proving/versionedProving.js +28 -2
  16. package/dist/signatures/v8Signatures.d.ts +1 -1
  17. package/dist/simulation/index.d.ts +4 -6
  18. package/dist/simulation/index.js +4 -7
  19. package/dist/simulation/v8/Simulator.d.ts +1 -3
  20. package/dist/simulation/v8/Simulator.js +1 -3
  21. package/dist/simulation/v8/SimulatorState.d.ts +1 -10
  22. package/dist/simulation/v8/SimulatorState.js +1 -9
  23. package/dist/simulation/v8/index.d.ts +1 -1
  24. package/dist/simulation/v8/index.js +1 -1
  25. package/dist/simulation/v9/Simulator.d.ts +0 -2
  26. package/dist/simulation/v9/Simulator.js +0 -2
  27. package/dist/simulation/v9/SimulatorState.d.ts +0 -9
  28. package/dist/simulation/v9/SimulatorState.js +0 -8
  29. package/dist/simulation/v9/index.d.ts +1 -1
  30. package/dist/simulation/v9/index.js +1 -1
  31. package/dist/submission/submissionService.d.ts +57 -9
  32. package/dist/submission/submissionService.js +61 -15
  33. package/dist/transactionLifecycle/SnapshotFormat.d.ts +51 -0
  34. package/dist/transactionLifecycle/SnapshotFormat.js +120 -0
  35. package/dist/transactionLifecycle/index.d.ts +4 -0
  36. package/dist/{pendingTransactions → transactionLifecycle}/index.js +4 -2
  37. package/dist/transactionLifecycle/lifecycle.d.ts +402 -0
  38. package/dist/transactionLifecycle/lifecycle.js +407 -0
  39. package/dist/transactionLifecycle/lifecycleService.d.ts +448 -0
  40. package/dist/transactionLifecycle/lifecycleService.js +350 -0
  41. package/dist/transactionLifecycle/transactionTraits.d.ts +106 -0
  42. package/dist/transactionLifecycle/transactionTraits.js +53 -0
  43. package/dist/validation/blockData.d.ts +23 -4
  44. package/dist/validation/blockData.js +34 -10
  45. package/dist/validation/index.d.ts +1 -0
  46. package/dist/validation/index.js +1 -0
  47. package/dist/validation/promiseBoundary.d.ts +10 -0
  48. package/dist/validation/promiseBoundary.js +30 -0
  49. package/dist/validation/v8ValidationService.d.ts +6 -5
  50. package/dist/validation/v8ValidationService.js +11 -4
  51. package/dist/validation/v9ValidationService.d.ts +30 -0
  52. package/dist/validation/v9ValidationService.js +61 -0
  53. package/dist/validation/validationService.d.ts +54 -32
  54. package/dist/validation/validationService.js +29 -49
  55. package/dist/validation/versionedValidation.d.ts +5 -3
  56. package/dist/validation/versionedValidation.js +5 -3
  57. package/package.json +12 -12
  58. package/dist/pendingTransactions/index.d.ts +0 -2
  59. package/dist/pendingTransactions/pendingTransactions.d.ts +0 -266
  60. package/dist/pendingTransactions/pendingTransactions.js +0 -302
  61. package/dist/pendingTransactions/pendingTransactionsService.d.ts +0 -108
  62. package/dist/pendingTransactions/pendingTransactionsService.js +0 -186
@@ -1,108 +0,0 @@
1
- import { type Clock, Effect, Option, type ParseResult, Scope, Stream } from 'effect';
2
- import * as PendingTransactions from './pendingTransactions.js';
3
- import { type ProtocolVersion } from '@midnightntwrk/wallet-sdk-abstractions';
4
- import type * as rx from 'rxjs';
5
- import { type QueryClient } from '@midnightntwrk/wallet-sdk-indexer-client/effect';
6
- export type PendingTransactionsService<TTransaction> = {
7
- start: () => Promise<void>;
8
- stop: () => Promise<void>;
9
- state: () => rx.Observable<PendingTransactions.PendingTransactions<TTransaction>>;
10
- /**
11
- * Records a transaction as pending, stamped with the protocol version the chain had reached when it was authored.
12
- *
13
- * @param tx The transaction.
14
- * @param protocolVersion The observed chain version, or `Option.none()` when no wallet has reported one yet.
15
- */
16
- addPendingTransaction: (tx: TTransaction, protocolVersion: Option.Option<ProtocolVersion.ProtocolVersion>) => Promise<void>;
17
- clear: (tx: TTransaction) => Promise<void>;
18
- /**
19
- * Gives up on every pending transaction whose protocol version epoch the chain has moved past.
20
- *
21
- * @param chainNow The protocol version the wallets have reached.
22
- */
23
- orphanBeyond: (chainNow: ProtocolVersion.ProtocolVersion) => Promise<void>;
24
- /**
25
- * Records that a balanced transaction has coins reserved, before it is proven or submitted.
26
- *
27
- * Between balancing and submission nothing else says those coins are spoken for, so this is the only durable record
28
- * of them. Any earlier reservation sharing an identifier is replaced, so re-balancing the same spend does not
29
- * accumulate records.
30
- *
31
- * @example
32
- * await service.addReservation({ identifiers: tx.identifiers(), inputs: { unshielded: ids }, ttl, ... });
33
- *
34
- * @param reservation - What the balanced transaction booked, and when the ledger stops accepting it
35
- * @returns A promise that resolves once the record is held
36
- */
37
- addReservation: (reservation: PendingTransactions.Reservation) => Promise<void>;
38
- /**
39
- * Forgets the reservation holding any of `identifiers`, once its coins have been released.
40
- *
41
- * Call it wherever the booking it stands for ends: the transaction is registered and tracked on its own, the booking
42
- * is reverted, or the record has expired. A record left behind outlives the coins it named.
43
- *
44
- * @example
45
- * await service.clearReservation([...tx.identifiers()]);
46
- *
47
- * @param identifiers - Identifiers of the spend whose record is finished with; unknown ones are ignored
48
- * @returns A promise that resolves once the record is gone
49
- */
50
- clearReservation: (identifiers: readonly string[]) => Promise<void>;
51
- };
52
- export type IndexerClientConnection = {
53
- indexerHttpUrl: string;
54
- indexerWsUrl?: string;
55
- };
56
- export type DefaultPendingTransactionsServiceConfiguration = {
57
- indexerClientConnection: IndexerClientConnection;
58
- };
59
- export type InitParams<TTransaction> = {
60
- txTraits: PendingTransactions.VersionedTransactionTrait<TTransaction>;
61
- initialState?: PendingTransactions.PendingTransactions<TTransaction>;
62
- configuration: DefaultPendingTransactionsServiceConfiguration;
63
- };
64
- export declare class PendingTransactionsServiceImpl<TTransaction> implements PendingTransactionsService<TTransaction> {
65
- #private;
66
- static init<TTransaction>(initParams: InitParams<TTransaction>): Promise<PendingTransactionsServiceImpl<TTransaction>>;
67
- static restore<TTransaction>(data: string, txTraits: PendingTransactions.VersionedTransactionTrait<TTransaction>, configuration: DefaultPendingTransactionsServiceConfiguration): Promise<PendingTransactionsServiceImpl<TTransaction>>;
68
- private static initEffect;
69
- constructor(effectService: PendingTransactionsServiceEffect<TTransaction>, scope: Scope.CloseableScope, configuration: DefaultPendingTransactionsServiceConfiguration);
70
- addPendingTransaction(tx: TTransaction, protocolVersion: Option.Option<ProtocolVersion.ProtocolVersion>): Promise<void>;
71
- clear(tx: TTransaction): Promise<void>;
72
- orphanBeyond(chainNow: ProtocolVersion.ProtocolVersion): Promise<void>;
73
- addReservation(reservation: PendingTransactions.Reservation): Promise<void>;
74
- clearReservation(identifiers: readonly string[]): Promise<void>;
75
- start(): Promise<void>;
76
- state(): rx.Observable<PendingTransactions.PendingTransactions<TTransaction>>;
77
- stop(): Promise<void>;
78
- }
79
- export type PendingTransactionsServiceEffect<TTransaction> = {
80
- startPolling: (ticks: Stream.Stream<unknown>) => Effect.Effect<void, Error, QueryClient | Scope.Scope | Clock.Clock>;
81
- state: () => Stream.Stream<PendingTransactions.PendingTransactions<TTransaction>>;
82
- addPendingTransaction: (tx: TTransaction, protocolVersion: Option.Option<ProtocolVersion.ProtocolVersion>) => Effect.Effect<void, never, never>;
83
- clear: (tx: TTransaction) => Effect.Effect<void, never, never>;
84
- orphanBeyond: (chainNow: ProtocolVersion.ProtocolVersion) => Effect.Effect<void, never, never>;
85
- addReservation: (reservation: PendingTransactions.Reservation) => Effect.Effect<void, never, never>;
86
- clearReservation: (identifiers: readonly string[]) => Effect.Effect<void, never, never>;
87
- };
88
- export declare class PendingTransactionsServiceEffectImpl<TTransaction> implements PendingTransactionsServiceEffect<TTransaction> {
89
- #private;
90
- static restore<TTransaction>(data: string, txTraits: PendingTransactions.VersionedTransactionTrait<TTransaction>): Effect.Effect<PendingTransactionsServiceEffectImpl<TTransaction>, ParseResult.ParseError>;
91
- constructor(txTraits: PendingTransactions.VersionedTransactionTrait<TTransaction>, initialState?: PendingTransactions.PendingTransactions<TTransaction>);
92
- /**
93
- * The state, published when it moves. The poll writes the ref on every tick whether or not anything expired, and a
94
- * write publishes regardless, so consecutive identical values are dropped here. Every real change builds a new value,
95
- * so comparing by identity keeps a change from being mistaken for a repeat.
96
- */
97
- state(): Stream.Stream<PendingTransactions.PendingTransactions<TTransaction>>;
98
- startPolling(ticks: Stream.Stream<unknown>): Effect.Effect<void, Error, QueryClient | Scope.Scope>;
99
- addPendingTransaction(tx: TTransaction, protocolVersion: Option.Option<ProtocolVersion.ProtocolVersion>): Effect.Effect<void>;
100
- addReservation(reservation: PendingTransactions.Reservation): Effect.Effect<void>;
101
- clearReservation(identifiers: readonly string[]): Effect.Effect<void>;
102
- /** Marks the reservations whose transactions the ledger would no longer accept. Runs on every poll. */
103
- private expireReservations;
104
- clear(tx: TTransaction): Effect.Effect<void>;
105
- orphanBeyond(chainNow: ProtocolVersion.ProtocolVersion): Effect.Effect<void>;
106
- private saveResult;
107
- private queryForStatus;
108
- }
@@ -1,186 +0,0 @@
1
- /*
2
- * This file is part of MIDNIGHT-WALLET-SDK.
3
- * Copyright (C) Midnight Foundation
4
- * SPDX-License-Identifier: Apache-2.0
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * You may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
- import { DateTime, DefaultServices, Duration, Effect, Exit, Iterable, Option, pipe, Schedule, Scope, Stream, SubscriptionRef, } from 'effect';
16
- import * as PendingTransactions from './pendingTransactions.js';
17
- import { HttpQueryClient } from '@midnightntwrk/wallet-sdk-indexer-client/effect';
18
- import { TransactionStatus } from '@midnightntwrk/wallet-sdk-indexer-client';
19
- import { EitherOps, ObservableOps } from '@midnightntwrk/wallet-sdk-utilities';
20
- export class PendingTransactionsServiceImpl {
21
- static init(initParams) {
22
- return PendingTransactionsServiceImpl.initEffect(initParams).pipe(Effect.runPromise);
23
- }
24
- static restore(data, txTraits, configuration) {
25
- return pipe(PendingTransactions.deserialize(data, txTraits), EitherOps.toEffect, Effect.andThen((state) => PendingTransactionsServiceImpl.initEffect({ txTraits, initialState: state, configuration })), Effect.runPromise);
26
- }
27
- static initEffect(initParams) {
28
- return Effect.gen(function* () {
29
- const service = new PendingTransactionsServiceEffectImpl(initParams.txTraits, initParams.initialState);
30
- const scope = yield* Scope.make();
31
- return new PendingTransactionsServiceImpl(service, scope, initParams.configuration);
32
- });
33
- }
34
- #effectService;
35
- #scope;
36
- #configuration;
37
- constructor(effectService, scope, configuration) {
38
- this.#effectService = effectService;
39
- this.#scope = scope;
40
- this.#configuration = configuration;
41
- }
42
- addPendingTransaction(tx, protocolVersion) {
43
- return this.#effectService.addPendingTransaction(tx, protocolVersion).pipe(Effect.runPromise);
44
- }
45
- clear(tx) {
46
- return this.#effectService.clear(tx).pipe(Effect.runPromise);
47
- }
48
- orphanBeyond(chainNow) {
49
- return this.#effectService.orphanBeyond(chainNow).pipe(Effect.runPromise);
50
- }
51
- addReservation(reservation) {
52
- return this.#effectService.addReservation(reservation).pipe(Effect.runPromise);
53
- }
54
- clearReservation(identifiers) {
55
- return this.#effectService.clearReservation(identifiers).pipe(Effect.runPromise);
56
- }
57
- start() {
58
- return this.#effectService.startPolling(Stream.tick(Duration.seconds(1))).pipe(Effect.provide(HttpQueryClient.layer({
59
- url: this.#configuration.indexerClientConnection.indexerHttpUrl,
60
- })), Effect.provideService(Scope.Scope, this.#scope), Effect.provide(DefaultServices.liveServices), Effect.runFork, () => Promise.resolve());
61
- }
62
- state() {
63
- return this.#effectService.state().pipe(ObservableOps.fromStream);
64
- }
65
- stop() {
66
- return pipe(Scope.close(this.#scope, Exit.succeed(undefined)), Effect.runPromise);
67
- }
68
- }
69
- export class PendingTransactionsServiceEffectImpl {
70
- #state;
71
- #txTraits;
72
- static restore(data, txTraits) {
73
- return pipe(data, (data) => PendingTransactions.deserialize(data, txTraits), EitherOps.toEffect, Effect.map((state) => new PendingTransactionsServiceEffectImpl(txTraits, state)));
74
- }
75
- constructor(txTraits, initialState) {
76
- this.#txTraits = txTraits;
77
- this.#state = SubscriptionRef.make(initialState ?? PendingTransactions.empty()).pipe(Effect.runSync); // Should not be here, but otherwise initialization would be too involved
78
- }
79
- /**
80
- * The state, published when it moves. The poll writes the ref on every tick whether or not anything expired, and a
81
- * write publishes regardless, so consecutive identical values are dropped here. Every real change builds a new value,
82
- * so comparing by identity keeps a change from being mistaken for a repeat.
83
- */
84
- state() {
85
- return Stream.concat(Stream.fromEffect(SubscriptionRef.get(this.#state)), this.#state.changes).pipe(Stream.changesWith((a, b) => a === b));
86
- }
87
- startPolling(ticks) {
88
- return ticks.pipe(
89
- // A reservation stands in for a transaction that was never submitted, so no indexer query can settle it. The
90
- // only thing that can is its TTL, checked on the same beat that reaps the transactions.
91
- Stream.tap(() => this.expireReservations()), Stream.mapEffect(() => SubscriptionRef.get(this.#state)), Stream.mapConcat(PendingTransactions.allPending), Stream.mapConcatEffect((item) => {
92
- return Effect.gen(this, function* () {
93
- const now = yield* DateTime.now;
94
- const trait = PendingTransactions.traitForVersion(this.#txTraits, item.protocolVersion);
95
- if (Option.isNone(trait))
96
- return [];
97
- const result = yield* this.queryForStatus(item.tx, trait.value);
98
- return Option.match(result, {
99
- onSome: (status) => [{ ...item, result: status }],
100
- onNone: () => {
101
- const failedResult = {
102
- status: 'FAILURE',
103
- segments: [],
104
- };
105
- return trait.value.hasTTLExpired(item.tx, item.creationTime, now)
106
- ? [{ ...item, result: failedResult }]
107
- : [];
108
- },
109
- });
110
- });
111
- }), Stream.retry(pipe(Schedule.exponential(Duration.millis(1)), Schedule.jitteredWith({ min: 0.1, max: 1.2 }), Schedule.resetAfter(Duration.minutes(5)))), Stream.catchAll((error) => {
112
- return Stream.execute(Effect.logWarning(error, 'Caught error in PendingTransactionsService'));
113
- }), Stream.runForEachScoped((item) => {
114
- return this.saveResult(item.tx, item.result);
115
- }));
116
- }
117
- addPendingTransaction(tx, protocolVersion) {
118
- return SubscriptionRef.updateEffect(this.#state, (state) => {
119
- return DateTime.now.pipe(Effect.andThen((now) => PendingTransactions.addPendingTransaction(state, tx, now, this.#txTraits, protocolVersion)));
120
- });
121
- }
122
- addReservation(reservation) {
123
- return SubscriptionRef.update(this.#state, (state) => PendingTransactions.addReservation(state, reservation));
124
- }
125
- clearReservation(identifiers) {
126
- return SubscriptionRef.update(this.#state, (state) => PendingTransactions.clearReservation(state, identifiers));
127
- }
128
- /** Marks the reservations whose transactions the ledger would no longer accept. Runs on every poll. */
129
- expireReservations() {
130
- return SubscriptionRef.updateEffect(this.#state, (state) => DateTime.now.pipe(Effect.map((now) => PendingTransactions.expireReservations(state, now))));
131
- }
132
- clear(tx) {
133
- return SubscriptionRef.update(this.#state, (state) => {
134
- return PendingTransactions.clear(state, tx, this.#txTraits);
135
- });
136
- }
137
- orphanBeyond(chainNow) {
138
- return SubscriptionRef.update(this.#state, (state) => {
139
- return PendingTransactions.orphanBeyond(state, this.#txTraits, chainNow);
140
- });
141
- }
142
- saveResult(tx, result) {
143
- switch (result.status) {
144
- case 'SUCCESS':
145
- return this.clear(tx);
146
- case 'FAILURE':
147
- case 'PARTIAL_SUCCESS':
148
- case 'ORPHANED_BY_FORK':
149
- return SubscriptionRef.update(this.#state, (state) => {
150
- return PendingTransactions.saveResult(state, tx, result, this.#txTraits);
151
- });
152
- }
153
- }
154
- queryForStatus(tx, txTrait) {
155
- return Effect.gen(function* () {
156
- const statusQuery = yield* TransactionStatus;
157
- const result = yield* statusQuery({ transactionId: txTrait.firstId(tx) }).pipe(Effect.catchAll((error) => {
158
- const fallback = { transactions: [] };
159
- return pipe(Effect.logWarning(error, 'Observed error in PendingTransactionsService, retrying'), Effect.as(fallback));
160
- }));
161
- return pipe(result.transactions, Iterable.filterMap((res) => {
162
- // Only RegularTransaction carries the identifiers and transactionResult this matching
163
- // needs — and wallet-submitted transactions are always regular. Narrowing to it (rather
164
- // than excluding known other variants) keeps future schema variants ignored instead of
165
- // breaking compilation, as BridgeClaimTransaction (indexer 4.4.0) once did.
166
- if (res.__typename !== 'RegularTransaction') {
167
- return Option.none();
168
- }
169
- if (res.transactionResult.status != 'SUCCESS' &&
170
- res.transactionResult.status != 'FAILURE' &&
171
- res.transactionResult.status != 'PARTIAL_SUCCESS') {
172
- return Option.none();
173
- }
174
- if (txTrait.areAllTxIdsIncluded(tx, res.identifiers)) {
175
- return Option.some({
176
- status: res.transactionResult.status,
177
- segments: res.transactionResult.segments ?? [],
178
- });
179
- }
180
- else {
181
- return Option.none();
182
- }
183
- }), Iterable.head);
184
- });
185
- }
186
- }