@midnight-ntwrk/wallet-sdk-capabilities 4.0.0-beta.2 → 4.0.0-beta.3

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 (68) hide show
  1. package/README.md +27 -3
  2. package/dist/chainVersion/chainVersionProbe.d.ts +95 -0
  3. package/dist/chainVersion/chainVersionProbe.js +83 -0
  4. package/dist/chainVersion/index.d.ts +1 -0
  5. package/dist/chainVersion/index.js +13 -0
  6. package/dist/codecs/index.d.ts +1 -0
  7. package/dist/codecs/index.js +13 -0
  8. package/dist/codecs/ledgerParameters.d.ts +81 -0
  9. package/dist/codecs/ledgerParameters.js +63 -0
  10. package/dist/index.d.ts +3 -0
  11. package/dist/index.js +3 -0
  12. package/dist/pendingTransactions/pendingTransactions.d.ts +113 -9
  13. package/dist/pendingTransactions/pendingTransactions.js +147 -30
  14. package/dist/pendingTransactions/pendingTransactionsService.d.ts +25 -9
  15. package/dist/pendingTransactions/pendingTransactionsService.js +33 -21
  16. package/dist/proving/index.d.ts +2 -0
  17. package/dist/proving/index.js +2 -0
  18. package/dist/proving/provingService.d.ts +175 -15
  19. package/dist/proving/provingService.js +104 -11
  20. package/dist/proving/v8ProvingService.d.ts +53 -0
  21. package/dist/proving/v8ProvingService.js +71 -0
  22. package/dist/proving/versionedProving.d.ts +42 -0
  23. package/dist/proving/versionedProving.js +103 -0
  24. package/dist/signatures/index.d.ts +2 -0
  25. package/dist/signatures/index.js +14 -0
  26. package/dist/signatures/signing.d.ts +37 -0
  27. package/dist/signatures/signing.js +13 -0
  28. package/dist/signatures/v8Signatures.d.ts +54 -0
  29. package/dist/signatures/v8Signatures.js +62 -0
  30. package/dist/simulation/ForkSimulator.d.ts +114 -0
  31. package/dist/simulation/ForkSimulator.js +209 -0
  32. package/dist/simulation/LedgerTranslation.d.ts +53 -0
  33. package/dist/simulation/LedgerTranslation.js +56 -0
  34. package/dist/simulation/core/VersionTimeline.d.ts +54 -0
  35. package/dist/simulation/core/VersionTimeline.js +56 -0
  36. package/dist/simulation/core/blocks.d.ts +38 -0
  37. package/dist/simulation/core/blocks.js +52 -0
  38. package/dist/simulation/core/index.d.ts +13 -0
  39. package/dist/simulation/core/index.js +25 -0
  40. package/dist/simulation/core/strictness.d.ts +29 -0
  41. package/dist/simulation/core/strictness.js +39 -0
  42. package/dist/simulation/index.d.ts +22 -2
  43. package/dist/simulation/index.js +25 -14
  44. package/dist/simulation/v8/Simulator.d.ts +231 -0
  45. package/dist/simulation/v8/Simulator.js +503 -0
  46. package/dist/simulation/{SimulatorState.d.ts → v8/SimulatorState.d.ts} +31 -44
  47. package/dist/simulation/v8/SimulatorState.js +290 -0
  48. package/dist/simulation/v8/index.d.ts +2 -0
  49. package/dist/simulation/v8/index.js +26 -0
  50. package/dist/simulation/{Simulator.d.ts → v9/Simulator.d.ts} +57 -6
  51. package/dist/simulation/{Simulator.js → v9/Simulator.js} +68 -18
  52. package/dist/simulation/v9/SimulatorState.d.ts +336 -0
  53. package/dist/simulation/{SimulatorState.js → v9/SimulatorState.js} +34 -67
  54. package/dist/simulation/v9/index.d.ts +2 -0
  55. package/dist/simulation/v9/index.js +26 -0
  56. package/dist/submission/submissionService.d.ts +2 -1
  57. package/dist/submission/submissionService.js +1 -1
  58. package/dist/validation/blockData.d.ts +42 -2
  59. package/dist/validation/blockData.js +55 -8
  60. package/dist/validation/index.d.ts +2 -0
  61. package/dist/validation/index.js +2 -0
  62. package/dist/validation/v8ValidationService.d.ts +29 -0
  63. package/dist/validation/v8ValidationService.js +48 -0
  64. package/dist/validation/validationService.d.ts +132 -17
  65. package/dist/validation/validationService.js +88 -42
  66. package/dist/validation/versionedValidation.d.ts +46 -0
  67. package/dist/validation/versionedValidation.js +55 -0
  68. package/package.json +23 -9
@@ -1,7 +1,8 @@
1
1
  import * as ledger from '@midnightntwrk/ledger-v9';
2
2
  import type { KeyMaterialProvider } from '@midnight-ntwrk/zkir-v2';
3
+ import { ProtocolVersion } from '@midnight-ntwrk/wallet-sdk-abstractions';
3
4
  import { type InvalidProtocolSchemeError } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
4
- import { Effect } from 'effect';
5
+ import { Effect, Either } from 'effect';
5
6
  declare const ProvingError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
6
7
  readonly _tag: "Wallet.Proving";
7
8
  } & Readonly<A>;
@@ -10,28 +11,187 @@ export declare class ProvingError extends ProvingError_base<{
10
11
  cause: Error;
11
12
  }> {
12
13
  }
13
- export interface ProvingServiceEffect<TTransaction> {
14
- prove(transaction: ledger.UnprovenTransaction): Effect.Effect<TTransaction, ProvingError>;
14
+ declare const ProvingEpochMismatchError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
15
+ readonly _tag: "@midnight-ntwrk/wallet-sdk-capabilities/proving/provingService/ProvingEpochMismatchError";
16
+ } & Readonly<A>;
17
+ /**
18
+ * Raised when a proving backend is handed a transaction from the other side of a protocol boundary.
19
+ *
20
+ * @remarks
21
+ * Every backend is written against exactly one ledger version, and the two ledger versions' transactions are different
22
+ * classes that neither can read. The router has already chosen a backend by the version stamped on the transaction,
23
+ * so reaching this error means the stamp and the bytes disagreed — which is worth saying out loud, and in terms of
24
+ * the epoch the backend serves, rather than letting a foreign object reach the wasm-bindgen boundary and fail there
25
+ * as something unreadable.
26
+ */
27
+ export declare class ProvingEpochMismatchError extends ProvingEpochMismatchError_base<{
28
+ readonly message: string;
29
+ /** The range of protocol versions the backend that refused the transaction serves. */
30
+ readonly epoch: ProtocolVersion.ProtocolVersion.Range;
31
+ }> {
32
+ }
33
+ /** Everything a proving backend can fail with. */
34
+ export type ProvingFailure = ProvingError | ProvingEpochMismatchError;
35
+ /**
36
+ * Turns an unproven transaction into a proven one.
37
+ *
38
+ * @typeParam TProven The proven transaction this backend produces.
39
+ * @typeParam TUnproven The unproven transaction it accepts. Defaults to ledger-v9's, because a proving backend is only
40
+ * ever written against one ledger version — which is exactly why choosing between backends is
41
+ * {@link VersionedProvingServiceEffect}'s job and not this interface's.
42
+ */
43
+ export interface ProvingServiceEffect<TProven, TUnproven = ledger.UnprovenTransaction> {
44
+ prove(transaction: TUnproven): Effect.Effect<TProven, ProvingFailure>;
45
+ }
46
+ export interface ProvingService<TProven, TUnproven = ledger.UnprovenTransaction> {
47
+ prove(transaction: TUnproven): Promise<TProven>;
48
+ }
49
+ declare const UnsupportedProvingVersionError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
50
+ readonly _tag: "@midnight-ntwrk/wallet-sdk-capabilities/proving/provingService/UnsupportedProvingVersionError";
51
+ } & Readonly<A>;
52
+ /** Raised when no proving backend is registered for the protocol version a transaction was built for. */
53
+ export declare class UnsupportedProvingVersionError extends UnsupportedProvingVersionError_base<{
54
+ readonly message: string;
55
+ /** The version the transaction was built for, which no registered backend serves. */
56
+ readonly protocolVersion: ProtocolVersion.ProtocolVersion;
57
+ }> {
15
58
  }
16
- export interface ProvingService<TTransaction> {
17
- prove(transaction: ledger.UnprovenTransaction): Promise<TTransaction>;
59
+ /**
60
+ * Proves a transaction with the backend registered for the protocol version it was built for.
61
+ *
62
+ * @remarks
63
+ * The version is the transaction's own stamp, taken when it was built, and never the version the chain has reached by
64
+ * the time proving happens. A fork can land between balancing and proving; the bytes the prover has to read were
65
+ * fixed before it did.
66
+ */
67
+ export interface VersionedProvingServiceEffect<TProven, TUnproven = ledger.UnprovenTransaction> {
68
+ prove(transaction: TUnproven, protocolVersion: ProtocolVersion.ProtocolVersion): Effect.Effect<TProven, ProvingFailure | UnsupportedProvingVersionError>;
18
69
  }
19
- export type UnboundTransaction = ledger.Transaction<ledger.SignatureEnabled, ledger.Proof, ledger.PreBinding>;
20
- export declare const fromProvingProviderEffect: (provider: Effect.Effect<ledger.ProvingProvider, InvalidProtocolSchemeError>) => ProvingServiceEffect<UnboundTransaction>;
21
- export declare const fromProvingProvider: (provider: ledger.ProvingProvider) => ProvingServiceEffect<UnboundTransaction>;
70
+ export interface VersionedProvingService<TProven, TUnproven = ledger.UnprovenTransaction> {
71
+ prove(transaction: TUnproven, protocolVersion: ProtocolVersion.ProtocolVersion): Promise<TProven>;
72
+ }
73
+ /** The proving backends a wallet can use, keyed by the protocol version range each one serves. */
74
+ export type ProvingServices<TProven, TUnproven = ledger.UnprovenTransaction> = ProtocolVersion.Registry<ProvingServiceEffect<TProven, TUnproven>>;
75
+ /**
76
+ * Builds a proving service that routes on the version a transaction was built for.
77
+ *
78
+ * @param services The backends and the version ranges they serve.
79
+ * @returns A proving service that fails with {@link UnsupportedProvingVersionError} for a version nothing serves.
80
+ */
81
+ export declare const makeVersionedProvingServiceEffect: <TProven, TUnproven>(services: ProvingServices<TProven, TUnproven>) => VersionedProvingServiceEffect<TProven, TUnproven>;
82
+ /**
83
+ * Lets one backend answer for every protocol version.
84
+ *
85
+ * @remarks
86
+ * Says out loud what an unversioned proving service was implicitly claiming: that it can prove anything, whatever
87
+ * version produced it. True for a wallet on one side of a fork, and a lie the moment it crosses — so it has to be
88
+ * written down rather than assumed.
89
+ * @param service The backend to use for every version.
90
+ * @returns The same backend, addressed by version.
91
+ */
92
+ export declare const singleVersionProvingServiceEffect: <TProven, TUnproven>(service: ProvingServiceEffect<TProven, TUnproven>) => VersionedProvingServiceEffect<TProven, TUnproven>;
93
+ export type V9UnboundTransaction = ledger.Transaction<ledger.SignatureEnabled, ledger.Proof, ledger.PreBinding>;
94
+ /** Adapts a version-routed proving service to the promise-facing surface the facade exposes. */
95
+ export declare const wrapVersionedEffectService: <TProven, TUnproven>(effectService: VersionedProvingServiceEffect<TProven, TUnproven>) => VersionedProvingService<TProven, TUnproven>;
96
+ export declare const fromV9ProvingProviderEffect: (provider: Effect.Effect<ledger.ProvingProvider, InvalidProtocolSchemeError>) => ProvingServiceEffect<V9UnboundTransaction>;
97
+ export declare const fromV9ProvingProvider: (provider: ledger.ProvingProvider) => ProvingServiceEffect<V9UnboundTransaction>;
22
98
  export type ServerProvingConfiguration = {
23
99
  provingServerUrl: URL;
24
100
  };
25
101
  export type WasmProvingConfiguration = {
26
102
  keyMaterialProvider?: KeyMaterialProvider;
27
103
  };
28
- export type DefaultProvingConfiguration = ServerProvingConfiguration;
29
- export declare const makeServerProvingServiceEffect: (configuration: ServerProvingConfiguration) => ProvingServiceEffect<UnboundTransaction>;
30
- export declare const makeWasmProvingServiceEffect: (configuration?: WasmProvingConfiguration) => ProvingServiceEffect<UnboundTransaction>;
104
+ /**
105
+ * Where proving happens: at a proof server over HTTP, or in this process.
106
+ *
107
+ * @remarks
108
+ * Deliberately says nothing about a ledger version. Which ledger drives a backend follows from the key it is registered
109
+ * under in {@link ProvingBackends}, so the same description can be named for either side of a fork and mean the right
110
+ * thing both times.
111
+ */
112
+ export type ProvingBackend = Readonly<{
113
+ kind: 'server';
114
+ url: URL;
115
+ }> | Readonly<{
116
+ kind: 'wasm';
117
+ keyMaterialProvider?: KeyMaterialProvider;
118
+ }>;
119
+ /**
120
+ * Which proving backend proves the transactions each ledger version authors.
121
+ *
122
+ * @remarks
123
+ * Keyed the way {@link ProtocolVersion.ForkSchedule} is, so a configuration states each boundary once: `v8` proves below
124
+ * `forks.v9` and `v9` from it, and where one ends and the next begins is read off the schedule rather than restated
125
+ * here. The newest ledger version's backend is required, because it is what every new transaction is proved with; an
126
+ * older one may be left out on a chain whose history below the boundary the wallet will never author for, and a
127
+ * transaction stamped there then fails with {@link UnsupportedProvingVersionError}. The next hard fork adds a key
128
+ * (`v10`) rather than changing this shape.
129
+ */
130
+ export type ProvingBackends = Readonly<{
131
+ /** Proves the transactions ledger-v8 authored, below `forks.v9`. */
132
+ v8?: ProvingBackend;
133
+ /** Proves the transactions ledger-v9 authored, from `forks.v9`. */
134
+ v9: ProvingBackend;
135
+ }>;
136
+ /**
137
+ * Which proving backend serves which ledger version.
138
+ *
139
+ * @remarks
140
+ * The two settings are alternatives, not a set: `provers` is the general form and `provingServerUrl` the shorthand for
141
+ * "this one proof server, for every ledger version". Giving both is not an error — `provers` wins — but naming
142
+ * neither is, because there is then nothing to prove with.
143
+ *
144
+ * The single URL is registered once per ledger version and driven by each in turn, which is what makes it frame its
145
+ * requests correctly on both sides of a fork. Whether a given proof server can in fact serve both is an operational
146
+ * fact about that server, not something the SDK can know or enforce — today's images serve one side each, so a chain
147
+ * that spans a fork wants `provers` with a server per side.
148
+ */
149
+ export type DefaultProvingConfiguration = {
150
+ /** One proof server for every ledger version. */
151
+ provingServerUrl?: URL;
152
+ /** A proving backend per ledger version. */
153
+ provers?: ProvingBackends;
154
+ };
155
+ declare const ProvingConfigurationError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
156
+ readonly _tag: "@midnight-ntwrk/wallet-sdk-capabilities/proving/provingService/ProvingConfigurationError";
157
+ } & Readonly<A>;
158
+ /** Raised when the proving configuration names no backend at all. */
159
+ export declare class ProvingConfigurationError extends ProvingConfigurationError_base<{
160
+ readonly message: string;
161
+ readonly cause?: unknown;
162
+ }> {
163
+ }
164
+ /**
165
+ * Reads a proving configuration as the backend it names for each ledger version.
166
+ *
167
+ * @remarks
168
+ * Where the precedence between the two settings is decided, once, so that reading a configuration and building services
169
+ * from it cannot disagree about which one was meant. Says nothing about protocol versions: which range each backend
170
+ * serves is the fork schedule's to say, and `makeDefaultProvingServices` reads it there.
171
+ * @param configuration The proving configuration.
172
+ * @returns The backend per ledger version, or the reason the configuration names none.
173
+ */
174
+ export declare const resolveProvingBackends: (configuration: DefaultProvingConfiguration) => Either.Either<ProvingBackends, ProvingConfigurationError>;
175
+ /**
176
+ * Registers the in-process WASM prover, driven by ledger-v9, from a given protocol version upwards.
177
+ *
178
+ * @remarks
179
+ * Every entry here is ledger-v9's backend, so `sinceVersion` should not be below a protocol boundary: what a version
180
+ * below it needs is the ledger-v8 driver, which `makeDefaultProvingServices` registers for the epoch the fork
181
+ * schedule it is given places below `forks.v9`. Registering nothing below is what turns that into an
182
+ * {@link UnsupportedProvingVersionError} naming the version, rather than a ledger-v8 transaction handed to a ledger
183
+ * that cannot read it.
184
+ * @param sinceVersion The first protocol version to register the bundled prover for.
185
+ * @param configuration Optional key material override.
186
+ * @returns The proving backends, keyed by version.
187
+ */
188
+ export declare const makeWasmProvingServices: (sinceVersion: ProtocolVersion.ProtocolVersion, configuration?: WasmProvingConfiguration) => ProvingServices<V9UnboundTransaction>;
189
+ export declare const makeV9ServerProvingServiceEffect: (configuration: ServerProvingConfiguration) => ProvingServiceEffect<V9UnboundTransaction>;
190
+ export declare const makeV9WasmProvingServiceEffect: (configuration?: WasmProvingConfiguration) => ProvingServiceEffect<V9UnboundTransaction>;
31
191
  export declare const makeSimulatorProvingServiceEffect: () => ProvingServiceEffect<ledger.ProofErasedTransaction>;
32
- export declare const makeDefaultProvingServiceEffect: (configuration: DefaultProvingConfiguration) => ProvingServiceEffect<UnboundTransaction>;
33
- export declare const makeDefaultProvingService: (configuration: DefaultProvingConfiguration) => ProvingService<UnboundTransaction>;
34
- export declare const makeServerProvingService: (configuration: ServerProvingConfiguration) => ProvingService<UnboundTransaction>;
35
- export declare const makeWasmProvingService: (configuration?: WasmProvingConfiguration) => ProvingService<UnboundTransaction>;
192
+ export declare const makeDefaultProvingServiceEffect: (configuration: ServerProvingConfiguration) => ProvingServiceEffect<V9UnboundTransaction>;
193
+ export declare const makeDefaultProvingService: (configuration: ServerProvingConfiguration) => ProvingService<V9UnboundTransaction>;
194
+ export declare const makeV9ServerProvingService: (configuration: ServerProvingConfiguration) => ProvingService<V9UnboundTransaction>;
195
+ export declare const makeV9WasmProvingService: (configuration?: WasmProvingConfiguration) => ProvingService<V9UnboundTransaction>;
36
196
  export declare const makeSimulatorProvingService: () => ProvingService<ledger.ProofErasedTransaction>;
37
197
  export {};
@@ -11,15 +11,63 @@
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
13
  import * as ledger from '@midnightntwrk/ledger-v9';
14
+ import { ProtocolVersion } from '@midnight-ntwrk/wallet-sdk-abstractions';
14
15
  import { HttpProverClient, WasmProver } from '@midnight-ntwrk/wallet-sdk-prover-client/effect';
15
16
  import { ClientError, ServerError, } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
16
- import { Data, Effect, pipe } from 'effect';
17
+ import { Data, Effect, Either, Option, pipe } from 'effect';
17
18
  export class ProvingError extends Data.TaggedError('Wallet.Proving') {
18
19
  }
20
+ /**
21
+ * Raised when a proving backend is handed a transaction from the other side of a protocol boundary.
22
+ *
23
+ * @remarks
24
+ * Every backend is written against exactly one ledger version, and the two ledger versions' transactions are different
25
+ * classes that neither can read. The router has already chosen a backend by the version stamped on the transaction,
26
+ * so reaching this error means the stamp and the bytes disagreed — which is worth saying out loud, and in terms of
27
+ * the epoch the backend serves, rather than letting a foreign object reach the wasm-bindgen boundary and fail there
28
+ * as something unreadable.
29
+ */
30
+ export class ProvingEpochMismatchError extends Data.TaggedError('@midnight-ntwrk/wallet-sdk-capabilities/proving/provingService/ProvingEpochMismatchError') {
31
+ }
32
+ /** Raised when no proving backend is registered for the protocol version a transaction was built for. */
33
+ export class UnsupportedProvingVersionError extends Data.TaggedError('@midnight-ntwrk/wallet-sdk-capabilities/proving/provingService/UnsupportedProvingVersionError') {
34
+ }
35
+ /**
36
+ * Builds a proving service that routes on the version a transaction was built for.
37
+ *
38
+ * @param services The backends and the version ranges they serve.
39
+ * @returns A proving service that fails with {@link UnsupportedProvingVersionError} for a version nothing serves.
40
+ */
41
+ export const makeVersionedProvingServiceEffect = (services) => ({
42
+ prove: (transaction, protocolVersion) => Option.match(ProtocolVersion.select(services, protocolVersion), {
43
+ onNone: () => Effect.fail(new UnsupportedProvingVersionError({
44
+ message: `No proving backend is registered for protocol version ${protocolVersion}.`,
45
+ protocolVersion,
46
+ })),
47
+ onSome: (service) => service.prove(transaction),
48
+ }),
49
+ });
50
+ /**
51
+ * Lets one backend answer for every protocol version.
52
+ *
53
+ * @remarks
54
+ * Says out loud what an unversioned proving service was implicitly claiming: that it can prove anything, whatever
55
+ * version produced it. True for a wallet on one side of a fork, and a lie the moment it crosses — so it has to be
56
+ * written down rather than assumed.
57
+ * @param service The backend to use for every version.
58
+ * @returns The same backend, addressed by version.
59
+ */
60
+ export const singleVersionProvingServiceEffect = (service) => ({
61
+ prove: (transaction) => service.prove(transaction),
62
+ });
19
63
  const wrapEffectService = (effectService) => ({
20
64
  prove: (transaction) => Effect.runPromise(effectService.prove(transaction)),
21
65
  });
22
- export const fromProvingProviderEffect = (provider) => {
66
+ /** Adapts a version-routed proving service to the promise-facing surface the facade exposes. */
67
+ export const wrapVersionedEffectService = (effectService) => ({
68
+ prove: (transaction, protocolVersion) => Effect.runPromise(effectService.prove(transaction, protocolVersion)),
69
+ });
70
+ export const fromV9ProvingProviderEffect = (provider) => {
23
71
  return {
24
72
  prove(transaction) {
25
73
  return pipe(provider, Effect.flatMap((provider) => Effect.tryPromise({
@@ -34,18 +82,63 @@ export const fromProvingProviderEffect = (provider) => {
34
82
  },
35
83
  };
36
84
  };
37
- export const fromProvingProvider = (provider) => {
38
- return fromProvingProviderEffect(Effect.succeed(provider));
85
+ export const fromV9ProvingProvider = (provider) => {
86
+ return fromV9ProvingProviderEffect(Effect.succeed(provider));
39
87
  };
40
- export const makeServerProvingServiceEffect = (configuration) => {
88
+ /** Raised when the proving configuration names no backend at all. */
89
+ export class ProvingConfigurationError extends Data.TaggedError('@midnight-ntwrk/wallet-sdk-capabilities/proving/provingService/ProvingConfigurationError') {
90
+ }
91
+ const noBackendNamed = () => new ProvingConfigurationError({
92
+ message: "Missing required configuration: set 'provers' (or 'provingServerUrl'), or provide a custom provingService in init parameters.",
93
+ });
94
+ /**
95
+ * Reads a proving configuration as the backend it names for each ledger version.
96
+ *
97
+ * @remarks
98
+ * Where the precedence between the two settings is decided, once, so that reading a configuration and building services
99
+ * from it cannot disagree about which one was meant. Says nothing about protocol versions: which range each backend
100
+ * serves is the fork schedule's to say, and `makeDefaultProvingServices` reads it there.
101
+ * @param configuration The proving configuration.
102
+ * @returns The backend per ledger version, or the reason the configuration names none.
103
+ */
104
+ export const resolveProvingBackends = (configuration) => configuration.provers !== undefined
105
+ ? Either.right(configuration.provers)
106
+ : configuration.provingServerUrl !== undefined
107
+ ? Either.right({
108
+ v8: { kind: 'server', url: configuration.provingServerUrl },
109
+ v9: { kind: 'server', url: configuration.provingServerUrl },
110
+ })
111
+ : Either.left(noBackendNamed());
112
+ /**
113
+ * Registers the in-process WASM prover, driven by ledger-v9, from a given protocol version upwards.
114
+ *
115
+ * @remarks
116
+ * Every entry here is ledger-v9's backend, so `sinceVersion` should not be below a protocol boundary: what a version
117
+ * below it needs is the ledger-v8 driver, which `makeDefaultProvingServices` registers for the epoch the fork
118
+ * schedule it is given places below `forks.v9`. Registering nothing below is what turns that into an
119
+ * {@link UnsupportedProvingVersionError} naming the version, rather than a ledger-v8 transaction handed to a ledger
120
+ * that cannot read it.
121
+ * @param sinceVersion The first protocol version to register the bundled prover for.
122
+ * @param configuration Optional key material override.
123
+ * @returns The proving backends, keyed by version.
124
+ */
125
+ export const makeWasmProvingServices = (sinceVersion, configuration) => ({
126
+ entries: [
127
+ {
128
+ range: ProtocolVersion.makeRange(sinceVersion, ProtocolVersion.MaxSupportedVersion),
129
+ value: makeV9WasmProvingServiceEffect(configuration),
130
+ },
131
+ ],
132
+ });
133
+ export const makeV9ServerProvingServiceEffect = (configuration) => {
41
134
  return pipe(HttpProverClient.create({
42
135
  url: configuration.provingServerUrl,
43
- }), Effect.map((client) => client.asProvingProvider()), fromProvingProviderEffect);
136
+ }), Effect.map((client) => client.asProvingProvider()), fromV9ProvingProviderEffect);
44
137
  };
45
- export const makeWasmProvingServiceEffect = (configuration) => {
138
+ export const makeV9WasmProvingServiceEffect = (configuration) => {
46
139
  return pipe(WasmProver.create({
47
140
  keyMaterialProvider: configuration?.keyMaterialProvider ?? WasmProver.makeDefaultKeyMaterialProvider(),
48
- }), Effect.map((prover) => prover.asProvingProvider()), fromProvingProviderEffect);
141
+ }), Effect.map((prover) => prover.asProvingProvider()), fromV9ProvingProviderEffect);
49
142
  };
50
143
  export const makeSimulatorProvingServiceEffect = () => {
51
144
  return {
@@ -54,8 +147,8 @@ export const makeSimulatorProvingServiceEffect = () => {
54
147
  },
55
148
  };
56
149
  };
57
- export const makeDefaultProvingServiceEffect = (configuration) => makeServerProvingServiceEffect(configuration);
150
+ export const makeDefaultProvingServiceEffect = (configuration) => makeV9ServerProvingServiceEffect(configuration);
58
151
  export const makeDefaultProvingService = (configuration) => wrapEffectService(makeDefaultProvingServiceEffect(configuration));
59
- export const makeServerProvingService = (configuration) => wrapEffectService(makeServerProvingServiceEffect(configuration));
60
- export const makeWasmProvingService = (configuration) => wrapEffectService(makeWasmProvingServiceEffect(configuration));
152
+ export const makeV9ServerProvingService = (configuration) => wrapEffectService(makeV9ServerProvingServiceEffect(configuration));
153
+ export const makeV9WasmProvingService = (configuration) => wrapEffectService(makeV9WasmProvingServiceEffect(configuration));
61
154
  export const makeSimulatorProvingService = () => wrapEffectService(makeSimulatorProvingServiceEffect());
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Proving on ledger-v8.
3
+ *
4
+ * @remarks
5
+ * The ledger-v8 twin of the backends in `provingService.ts`: structurally the same three steps — obtain a proving
6
+ * provider, drive the transaction's own `prove` with that ledger version's cost model, report failures as a
7
+ * {@link ProvingError} — against a different ledger version's classes. It is the classes that make this a separate
8
+ * module: a ledger-v8 transaction handed ledger-v9's cost model fails at the wasm-bindgen boundary, and a
9
+ * proof-server request framed by ledger-v9 is not the request ledger-v8 would have sent.
10
+ */
11
+ import * as ledger from '@midnight-ntwrk/ledger-v8';
12
+ import { type InvalidProtocolSchemeError } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
13
+ import { Effect } from 'effect';
14
+ import { type ProvingServiceEffect, type ServerProvingConfiguration, type WasmProvingConfiguration } from './provingService.js';
15
+ /** A ledger-v8 transaction that has not been proved yet. */
16
+ export type V8UnprovenTransaction = ledger.UnprovenTransaction;
17
+ /** A ledger-v8 transaction that has been proved but not yet bound. */
18
+ export type V8UnboundTransaction = ledger.Transaction<ledger.SignatureEnabled, ledger.Proof, ledger.PreBinding>;
19
+ /** A proving backend written against ledger-v8. */
20
+ export type V8ProvingServiceEffect = ProvingServiceEffect<V8UnboundTransaction, V8UnprovenTransaction>;
21
+ /**
22
+ * Drives a ledger-v8 transaction's proving with a low-level proving provider.
23
+ *
24
+ * @param provider The proving provider, or the reason one could not be built.
25
+ * @returns A backend that proves ledger-v8 transactions.
26
+ */
27
+ export declare const fromV8ProvingProviderEffect: (provider: Effect.Effect<ledger.ProvingProvider, InvalidProtocolSchemeError>) => V8ProvingServiceEffect;
28
+ /**
29
+ * Drives a ledger-v8 transaction's proving with a low-level proving provider.
30
+ *
31
+ * @param provider The proving provider.
32
+ * @returns A backend that proves ledger-v8 transactions.
33
+ */
34
+ export declare const fromV8ProvingProvider: (provider: ledger.ProvingProvider) => V8ProvingServiceEffect;
35
+ /**
36
+ * Proves ledger-v8 transactions at a proof server.
37
+ *
38
+ * @param configuration The proof server to send ledger-v8 proving requests to.
39
+ * @returns A backend that proves ledger-v8 transactions over HTTP.
40
+ */
41
+ export declare const makeV8ServerProvingServiceEffect: (configuration: ServerProvingConfiguration) => V8ProvingServiceEffect;
42
+ /**
43
+ * Proves ledger-v8 transactions in this process.
44
+ *
45
+ * @remarks
46
+ * The zkir runtime the bundled prover drives is shared by both ledger lines, so there is nothing version-specific about
47
+ * the proving loop — and the key material ledger-v8 accepts turns out to be the same line the current one uses, which
48
+ * is why no key-material override is applied here. See the ledger-v8 spike in `prover-client`'s
49
+ * `v8WasmProver.integration.test.ts` for the evidence.
50
+ * @param configuration Optional key material override.
51
+ * @returns A backend that proves ledger-v8 transactions in-process.
52
+ */
53
+ export declare const makeV8WasmProvingServiceEffect: (configuration?: WasmProvingConfiguration) => V8ProvingServiceEffect;
@@ -0,0 +1,71 @@
1
+ // This file is part of MIDNIGHT-WALLET-SDK.
2
+ // Copyright (C) Midnight Foundation
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // You may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ /**
14
+ * Proving on ledger-v8.
15
+ *
16
+ * @remarks
17
+ * The ledger-v8 twin of the backends in `provingService.ts`: structurally the same three steps — obtain a proving
18
+ * provider, drive the transaction's own `prove` with that ledger version's cost model, report failures as a
19
+ * {@link ProvingError} — against a different ledger version's classes. It is the classes that make this a separate
20
+ * module: a ledger-v8 transaction handed ledger-v9's cost model fails at the wasm-bindgen boundary, and a
21
+ * proof-server request framed by ledger-v9 is not the request ledger-v8 would have sent.
22
+ */
23
+ import * as ledger from '@midnight-ntwrk/ledger-v8';
24
+ import { HttpProverClient, WasmProver } from '@midnight-ntwrk/wallet-sdk-prover-client/effect';
25
+ import { ClientError, ServerError, } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
26
+ import { Effect, pipe } from 'effect';
27
+ import { ProvingError, } from './provingService.js';
28
+ /**
29
+ * Drives a ledger-v8 transaction's proving with a low-level proving provider.
30
+ *
31
+ * @param provider The proving provider, or the reason one could not be built.
32
+ * @returns A backend that proves ledger-v8 transactions.
33
+ */
34
+ export const fromV8ProvingProviderEffect = (provider) => ({
35
+ prove(transaction) {
36
+ return pipe(provider, Effect.flatMap((provider) => Effect.tryPromise({
37
+ try: () => transaction.prove(provider, ledger.CostModel.initialCostModel()),
38
+ catch: (error) => error instanceof ClientError || error instanceof ServerError
39
+ ? error
40
+ : new ClientError({ message: 'Failed to prove transaction', cause: error }),
41
+ })), Effect.catchAll((error) => Effect.fail(new ProvingError({ message: error.message, cause: error }))));
42
+ },
43
+ });
44
+ /**
45
+ * Drives a ledger-v8 transaction's proving with a low-level proving provider.
46
+ *
47
+ * @param provider The proving provider.
48
+ * @returns A backend that proves ledger-v8 transactions.
49
+ */
50
+ export const fromV8ProvingProvider = (provider) => fromV8ProvingProviderEffect(Effect.succeed(provider));
51
+ /**
52
+ * Proves ledger-v8 transactions at a proof server.
53
+ *
54
+ * @param configuration The proof server to send ledger-v8 proving requests to.
55
+ * @returns A backend that proves ledger-v8 transactions over HTTP.
56
+ */
57
+ export const makeV8ServerProvingServiceEffect = (configuration) => pipe(HttpProverClient.create({ url: configuration.provingServerUrl }), Effect.map((client) => client.asV8ProvingProvider()), fromV8ProvingProviderEffect);
58
+ /**
59
+ * Proves ledger-v8 transactions in this process.
60
+ *
61
+ * @remarks
62
+ * The zkir runtime the bundled prover drives is shared by both ledger lines, so there is nothing version-specific about
63
+ * the proving loop — and the key material ledger-v8 accepts turns out to be the same line the current one uses, which
64
+ * is why no key-material override is applied here. See the ledger-v8 spike in `prover-client`'s
65
+ * `v8WasmProver.integration.test.ts` for the evidence.
66
+ * @param configuration Optional key material override.
67
+ * @returns A backend that proves ledger-v8 transactions in-process.
68
+ */
69
+ export const makeV8WasmProvingServiceEffect = (configuration) => pipe(WasmProver.create({
70
+ keyMaterialProvider: configuration?.keyMaterialProvider ?? WasmProver.makeDefaultKeyMaterialProvider(),
71
+ }), Effect.map((prover) => prover.asProvingProvider()), fromV8ProvingProviderEffect);
@@ -0,0 +1,42 @@
1
+ import * as ledgerV9 from '@midnightntwrk/ledger-v9';
2
+ import { ProtocolVersion } from '@midnight-ntwrk/wallet-sdk-abstractions';
3
+ import { Either } from 'effect';
4
+ import { type V8UnboundTransaction, type V8UnprovenTransaction } from './v8ProvingService.js';
5
+ import { type DefaultProvingConfiguration, type ProvingConfigurationError, type ProvingServiceEffect, type ProvingServices, type V9UnboundTransaction, type VersionedProvingService, type VersionedProvingServiceEffect } from './provingService.js';
6
+ /**
7
+ * Every unproven transaction either ledger version can be asked to prove.
8
+ *
9
+ * @remarks
10
+ * A genuine union: the two ledger versions' transaction types are nominally distinct, so a caller holding one of them
11
+ * is holding something the other version's backend provably cannot read.
12
+ */
13
+ /** Ledger-v9's unproven transaction, named for symmetry with {@link V8UnprovenTransaction}. */
14
+ export type V9UnprovenTransaction = ledgerV9.UnprovenTransaction;
15
+ export type AnyVersionUnprovenTransaction = V9UnprovenTransaction | V8UnprovenTransaction;
16
+ /** Every proved-but-unbound transaction either ledger version can hand back. */
17
+ export type AnyVersionUnboundTransaction = V9UnboundTransaction | V8UnboundTransaction;
18
+ /** A backend registered in a two-version registry, whichever ledger version it was written against. */
19
+ export type VersionProvingServiceEffect = ProvingServiceEffect<AnyVersionUnboundTransaction, AnyVersionUnprovenTransaction>;
20
+ /**
21
+ * Registers a proving backend either side of the protocol boundary.
22
+ *
23
+ * @remarks
24
+ * The configuration names a backend per ledger version and the fork schedule says where each ledger version begins; the
25
+ * range a backend serves is the meeting of the two, computed here and nowhere else, so the wallets and their provers
26
+ * cannot place the boundary differently.
27
+ * @param configuration The proving configuration.
28
+ * @param forks Where each ledger version begins on the chain.
29
+ * @returns The backends and the version ranges they serve, or the reason the configuration names none.
30
+ */
31
+ export declare const makeDefaultProvingServices: (configuration: DefaultProvingConfiguration, forks: ProtocolVersion.ForkSchedule) => Either.Either<ProvingServices<AnyVersionUnboundTransaction, AnyVersionUnprovenTransaction>, ProvingConfigurationError>;
32
+ /**
33
+ * Builds the version-routed proving service an SDK spanning a protocol boundary proves with.
34
+ *
35
+ * @param configuration The proving configuration.
36
+ * @param forks Where each ledger version begins on the chain.
37
+ * @returns A proving service that routes on the version a transaction was built for, or the reason the configuration
38
+ * names no backend.
39
+ */
40
+ export declare const makeDefaultVersionedProvingServiceEffect: (configuration: DefaultProvingConfiguration, forks: ProtocolVersion.ForkSchedule) => Either.Either<VersionedProvingServiceEffect<AnyVersionUnboundTransaction, AnyVersionUnprovenTransaction>, ProvingConfigurationError>;
41
+ /** The promise-facing surface of {@link makeDefaultVersionedProvingServiceEffect}, for the facade to expose. */
42
+ export declare const makeDefaultVersionedProvingService: (configuration: DefaultProvingConfiguration, forks: ProtocolVersion.ForkSchedule) => Either.Either<VersionedProvingService<AnyVersionUnboundTransaction, AnyVersionUnprovenTransaction>, ProvingConfigurationError>;
@@ -0,0 +1,103 @@
1
+ // This file is part of MIDNIGHT-WALLET-SDK.
2
+ // Copyright (C) Midnight Foundation
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // You may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ /**
14
+ * Proving for an SDK that spans a protocol boundary: one backend per ledger version, chosen by the version a
15
+ * transaction was built for.
16
+ *
17
+ * @remarks
18
+ * The routing already existed and the backends already existed; what this module supplies is the only thing neither
19
+ * could: the registration that says which range of protocol versions each backend answers for, taken from the same
20
+ * fork schedule the wallets are built with. Written the same way validation's `versionedValidation.ts` is, because it
21
+ * is the same problem.
22
+ */
23
+ import * as ledgerV8 from '@midnight-ntwrk/ledger-v8';
24
+ import * as ledgerV9 from '@midnightntwrk/ledger-v9';
25
+ import { ProtocolVersion } from '@midnight-ntwrk/wallet-sdk-abstractions';
26
+ import { Effect, Either, Option } from 'effect';
27
+ import { makeV8ServerProvingServiceEffect, makeV8WasmProvingServiceEffect, } from './v8ProvingService.js';
28
+ import { makeV9ServerProvingServiceEffect, makeVersionedProvingServiceEffect, makeV9WasmProvingServiceEffect, ProvingEpochMismatchError, resolveProvingBackends, wrapVersionedEffectService, } from './provingService.js';
29
+ /**
30
+ * Narrows a backend written against one ledger version to the union the registry is keyed by.
31
+ *
32
+ * @remarks
33
+ * The router has already chosen this backend by the version stamped on the transaction, so the check is a restatement
34
+ * of that choice rather than a second one — but it is where a transaction that does not belong is refused rather than
35
+ * handed to a ledger that cannot read it. What comes back from a wasm-bindgen boundary handed a foreign object is not
36
+ * an error anyone can act on; this is.
37
+ * @param service The backend, written against one ledger version.
38
+ * @param isOwn Whether a transaction is that ledger version's.
39
+ * @param epoch The range of protocol versions this backend answers for.
40
+ * @returns The same backend, in terms of the union.
41
+ */
42
+ const onlyFrom = (service, isOwn, epoch) => ({
43
+ prove: (transaction) => isOwn(transaction)
44
+ ? service.prove(transaction)
45
+ : Effect.fail(new ProvingEpochMismatchError({
46
+ message: `The proving backend registered for protocol versions [${epoch[0]}, ${epoch[1]}) was handed a transaction built by the other ledger version.`,
47
+ epoch,
48
+ })),
49
+ });
50
+ const isV8Transaction = (transaction) => transaction instanceof ledgerV8.Transaction;
51
+ const isV9Transaction = (transaction) => transaction instanceof ledgerV9.Transaction;
52
+ /** The in-process backend's configuration, with a key material override only when one was named. */
53
+ const wasmConfigurationOf = (backend) => backend.keyMaterialProvider === undefined ? {} : { keyMaterialProvider: backend.keyMaterialProvider };
54
+ /** Builds the backend a description names, driven by ledger-v8, for the epoch below `forks.v9`. */
55
+ const makeV8Backend = (backend, epoch) => onlyFrom(backend.kind === 'server'
56
+ ? makeV8ServerProvingServiceEffect({ provingServerUrl: backend.url })
57
+ : makeV8WasmProvingServiceEffect(wasmConfigurationOf(backend)), isV8Transaction, epoch);
58
+ /** Builds the backend a description names, driven by ledger-v9, for the epoch from `forks.v9`. */
59
+ const makeV9Backend = (backend, epoch) => onlyFrom(backend.kind === 'server'
60
+ ? makeV9ServerProvingServiceEffect({ provingServerUrl: backend.url })
61
+ : makeV9WasmProvingServiceEffect(wasmConfigurationOf(backend)), isV9Transaction, epoch);
62
+ /**
63
+ * The range of protocol versions each ledger version reads on a chain, from where the chain says the hand-over is.
64
+ *
65
+ * @remarks
66
+ * A chain whose boundary is at or below the minimum supported version has no history ledger-v8 authored, so there is no
67
+ * ledger-v8 epoch on it and a ledger-v8 backend has nothing to serve.
68
+ */
69
+ const epochsOf = (forks) => ({
70
+ v8: forks.v9 > ProtocolVersion.MinSupportedVersion
71
+ ? Option.some(ProtocolVersion.epochOf(ProtocolVersion.MinSupportedVersion, forks.v9))
72
+ : Option.none(),
73
+ v9: ProtocolVersion.epochOf(forks.v9, forks.v9),
74
+ });
75
+ /**
76
+ * Registers a proving backend either side of the protocol boundary.
77
+ *
78
+ * @remarks
79
+ * The configuration names a backend per ledger version and the fork schedule says where each ledger version begins; the
80
+ * range a backend serves is the meeting of the two, computed here and nowhere else, so the wallets and their provers
81
+ * cannot place the boundary differently.
82
+ * @param configuration The proving configuration.
83
+ * @param forks Where each ledger version begins on the chain.
84
+ * @returns The backends and the version ranges they serve, or the reason the configuration names none.
85
+ */
86
+ export const makeDefaultProvingServices = (configuration, forks) => resolveProvingBackends(configuration).pipe(Either.map((backends) => {
87
+ const epochs = epochsOf(forks);
88
+ const v8Entry = Option.all([Option.fromNullable(backends.v8), epochs.v8]).pipe(Option.map(([backend, range]) => ({ range, value: makeV8Backend(backend, range) })));
89
+ return {
90
+ entries: [...Option.toArray(v8Entry), { range: epochs.v9, value: makeV9Backend(backends.v9, epochs.v9) }],
91
+ };
92
+ }));
93
+ /**
94
+ * Builds the version-routed proving service an SDK spanning a protocol boundary proves with.
95
+ *
96
+ * @param configuration The proving configuration.
97
+ * @param forks Where each ledger version begins on the chain.
98
+ * @returns A proving service that routes on the version a transaction was built for, or the reason the configuration
99
+ * names no backend.
100
+ */
101
+ export const makeDefaultVersionedProvingServiceEffect = (configuration, forks) => makeDefaultProvingServices(configuration, forks).pipe(Either.map(makeVersionedProvingServiceEffect));
102
+ /** The promise-facing surface of {@link makeDefaultVersionedProvingServiceEffect}, for the facade to expose. */
103
+ export const makeDefaultVersionedProvingService = (configuration, forks) => makeDefaultVersionedProvingServiceEffect(configuration, forks).pipe(Either.map(wrapVersionedEffectService));
@@ -0,0 +1,2 @@
1
+ export * as Signing from './signing.js';
2
+ export * from './v8Signatures.js';