@midnight-ntwrk/wallet-sdk-prover-client 1.0.0-beta.8

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.
@@ -0,0 +1,11 @@
1
+ import { Layer } from 'effect';
2
+ import { ProverClient } from './ProverClient.js';
3
+ import { InvalidProtocolSchemeError } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
4
+ /**
5
+ * Creates a layer for a {@link ProverClient} that sends requests to a Proof Server over HTTP.
6
+ *
7
+ * @param config The server configuration to use when configuring the HTTP elements of the layer.
8
+ * @returns A `Layer` for {@link ProverClient} that sends requests to a configured Proof Server over HTTP.
9
+ * The layer can fail with an `InvalidProtocolSchemeError` if `config` is invalid for a HTTP context.
10
+ */
11
+ export declare const layer: (config: ProverClient.ServerConfig) => Layer.Layer<ProverClient, InvalidProtocolSchemeError>;
@@ -0,0 +1,61 @@
1
+ import { Chunk, Duration, Effect, Either, Layer, pipe, Schedule, Stream } from 'effect';
2
+ import { FetchHttpClient, HttpClient, HttpClientRequest } from '@effect/platform';
3
+ import { SerializedTransaction, SerializedUnprovenTransaction } from '@midnight-ntwrk/wallet-sdk-abstractions';
4
+ import { ProverClient } from './ProverClient.js';
5
+ import { ClientError, ServerError, HttpURL, } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
6
+ import { BlobOps } from '@midnight-ntwrk/wallet-sdk-utilities';
7
+ import * as ledger from '@midnight-ntwrk/ledger-v6';
8
+ const PROVE_TX_PATH = '/prove';
9
+ const CHECK_TX_PATH = '/check';
10
+ /**
11
+ * Creates a layer for a {@link ProverClient} that sends requests to a Proof Server over HTTP.
12
+ *
13
+ * @param config The server configuration to use when configuring the HTTP elements of the layer.
14
+ * @returns A `Layer` for {@link ProverClient} that sends requests to a configured Proof Server over HTTP.
15
+ * The layer can fail with an `InvalidProtocolSchemeError` if `config` is invalid for a HTTP context.
16
+ */
17
+ export const layer = (config) => Layer.effect(ProverClient, HttpURL.make(new URL(config.url)).pipe(Either.map((baseUrl) => new HttpProverClientImpl(baseUrl)), Either.match({
18
+ onLeft: (l) => Effect.fail(l),
19
+ onRight: (r) => Effect.succeed(r),
20
+ })));
21
+ class HttpProverClientImpl {
22
+ constructor(baseUrl) {
23
+ this.baseUrl = baseUrl;
24
+ }
25
+ baseUrl;
26
+ request(path, transaction, failurePrefix) {
27
+ const concatBytes = (chunks) => Effect.promise(() => BlobOps.getBytes(new Blob(chunks)));
28
+ const receiveBody = (response) => pipe(response.stream, Stream.runCollect, Effect.flatMap((chunks) => concatBytes(Chunk.toArray(chunks))));
29
+ // Build endpoint URL from the already validated base URL
30
+ const url = HttpURL.HttpURL(new URL(path, this.baseUrl));
31
+ const proveTxRequest = pipe(Effect.succeed(transaction), Effect.map((body) => HttpClientRequest.post(url).pipe(HttpClientRequest.bodyUint8Array(body))), Effect.flatMap(HttpClient.execute), Effect.flatMap((response) => Effect.gen(function* () {
32
+ if (response.status !== 200) {
33
+ const text = yield* response.text;
34
+ return yield* new ClientError({ message: `${failurePrefix}: ${text}` });
35
+ }
36
+ return yield* receiveBody(response);
37
+ })), Effect.retry({
38
+ times: 3,
39
+ while: (error) =>
40
+ // Retry if we get a Bad Gateway, Service Unavailable, or Gateway Timeout error.
41
+ error._tag === 'ResponseError' && error.response.status >= 502 && error.response.status <= 504,
42
+ schedule: Schedule.exponential(Duration.seconds(2), 2),
43
+ }));
44
+ return proveTxRequest.pipe(Effect.map(SerializedTransaction), Effect.catchTags({
45
+ RequestError: (err) => new ClientError({ message: `Failed to connect to Proof Server: ${err.message}` }),
46
+ ResponseError: (err) => Effect.orElseSucceed(err.response.text, () => 'Unknown server error').pipe(Effect.flatMap((message) => new ServerError({ message }))),
47
+ }), Effect.provide(FetchHttpClient.layer));
48
+ }
49
+ serverProverProvider = () => ({
50
+ check: async (serializedPreimage, _keyLocation) => pipe(Effect.succeed(ledger.createCheckPayload(serializedPreimage)), Effect.map(SerializedTransaction), Effect.flatMap((tx) => this.request(CHECK_TX_PATH, tx, 'Failed to check')), Effect.map((response) => ledger.parseCheckResult(response)), Effect.runPromise),
51
+ prove: async (serializedPreimage, _keyLocation, overwriteBindingInput) => pipe(Effect.succeed(ledger.createProvingPayload(serializedPreimage, overwriteBindingInput)), Effect.map(SerializedUnprovenTransaction), Effect.flatMap((tx) => this.request(PROVE_TX_PATH, tx, 'Failed to prove')), Effect.runPromise),
52
+ });
53
+ proveTransaction(transaction, costModel) {
54
+ return pipe(Effect.succeed(this.serverProverProvider()), Effect.flatMap((provider) => Effect.tryPromise({
55
+ try: () => transaction.prove(provider, costModel),
56
+ catch: (error) => error instanceof ClientError || error instanceof ServerError
57
+ ? error
58
+ : new ClientError({ message: 'Failed to prove transaction', cause: error }),
59
+ })));
60
+ }
61
+ }
@@ -0,0 +1,29 @@
1
+ import { Effect, Context } from 'effect';
2
+ import * as ledger from '@midnight-ntwrk/ledger-v6';
3
+ import { ClientError, ServerError } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
4
+ declare const ProverClient_base: Context.TagClass<ProverClient, "@midnight-ntwrk/prover-client#ProverClient", ProverClient.Service>;
5
+ /**
6
+ * A client that provides proof services for unproven transactions.
7
+ */
8
+ export declare class ProverClient extends ProverClient_base {
9
+ }
10
+ export declare namespace ProverClient {
11
+ /**
12
+ * Provides server related configuration for {@link ProverClient} implementations.
13
+ */
14
+ interface ServerConfig {
15
+ /** The base URL to the Proof Server. */
16
+ readonly url: URL | string;
17
+ }
18
+ interface Service {
19
+ /**
20
+ * Proves an unproven transaction by submitting it to an associated Proof Server.
21
+ *
22
+ * @param transaction A serialized unproven transaction.
23
+ * @returns An `Effect` that yields with a serialized transaction representing the proven version of `transaction`;
24
+ * or fails with a client or server side error.
25
+ */
26
+ proveTransaction<S extends ledger.Signaturish, B extends ledger.Bindingish>(tx: ledger.Transaction<S, ledger.PreProof, B>, costModel?: ledger.CostModel): Effect.Effect<ledger.Transaction<S, ledger.Proof, B>, ClientError | ServerError>;
27
+ }
28
+ }
29
+ export {};
@@ -0,0 +1,6 @@
1
+ import { Context } from 'effect';
2
+ /**
3
+ * A client that provides proof services for unproven transactions.
4
+ */
5
+ export class ProverClient extends Context.Tag('@midnight-ntwrk/prover-client#ProverClient')() {
6
+ }
@@ -0,0 +1,2 @@
1
+ export * as ProverClient from './ProverClient.js';
2
+ export * as HttpProverClient from './HttpProverClient.js';
@@ -0,0 +1,2 @@
1
+ export * as ProverClient from './ProverClient.js';
2
+ export * as HttpProverClient from './HttpProverClient.js';
@@ -0,0 +1,30 @@
1
+ import { ProverClient } from './effect/index.js';
2
+ import * as ledger from '@midnight-ntwrk/ledger-v6';
3
+ /**
4
+ * Sends serialized unproven transactions to a Proof Server over HTTP.
5
+ */
6
+ export declare class HttpProverClient {
7
+ #private;
8
+ /**
9
+ * Initializes a new {@link HttpProverClient}.
10
+ *
11
+ * @param config The server configuration to use when configuring the HTTP elements of the Proof Server.
12
+ * @throws {@link ProverClient.InvalidProtocolSchemeError}
13
+ * The `config` is invalid for a HTTP context. E.g., expecting 'http:' or 'https:' URLs but something other
14
+ * was provided.
15
+ */
16
+ constructor(config: ProverClient.ProverClient.ServerConfig);
17
+ /**
18
+ * Proves an unproven transaction by submitting it over HTTP to an associated Proof Server.
19
+ *
20
+ * @param transaction An unproven ledger transaction.
21
+ * @returns A `Promise` that resolves with a proven transaction or fails with a client or server side error.
22
+ * @throws {@link ClientError}
23
+ * There was an issue with the provided `transaction`, or a connection with the configured Proof
24
+ * Server could not be initiated.
25
+ * @throws {@link ServerError}
26
+ * Unable to establish a connection with the configured Proof Server, or there was an internal error that
27
+ * prevented the proof request from being executed.
28
+ */
29
+ proveTransaction<S extends ledger.Signaturish, B extends ledger.Bindingish>(transaction: ledger.Transaction<S, ledger.PreProof, B>, costModel?: ledger.CostModel): Promise<ledger.Transaction<S, ledger.Proof, B>>;
30
+ }
package/dist/index.js ADDED
@@ -0,0 +1,36 @@
1
+ import { Effect } from 'effect';
2
+ import { ProverClient, HttpProverClient as _HttpProverClient } from './effect/index.js';
3
+ /**
4
+ * Sends serialized unproven transactions to a Proof Server over HTTP.
5
+ */
6
+ export class HttpProverClient {
7
+ #innerClient;
8
+ /**
9
+ * Initializes a new {@link HttpProverClient}.
10
+ *
11
+ * @param config The server configuration to use when configuring the HTTP elements of the Proof Server.
12
+ * @throws {@link ProverClient.InvalidProtocolSchemeError}
13
+ * The `config` is invalid for a HTTP context. E.g., expecting 'http:' or 'https:' URLs but something other
14
+ * was provided.
15
+ */
16
+ constructor(config) {
17
+ this.#innerClient = Effect.gen(function* () {
18
+ return yield* ProverClient.ProverClient;
19
+ }).pipe(Effect.provide(_HttpProverClient.layer(config)), Effect.runSync);
20
+ }
21
+ /**
22
+ * Proves an unproven transaction by submitting it over HTTP to an associated Proof Server.
23
+ *
24
+ * @param transaction An unproven ledger transaction.
25
+ * @returns A `Promise` that resolves with a proven transaction or fails with a client or server side error.
26
+ * @throws {@link ClientError}
27
+ * There was an issue with the provided `transaction`, or a connection with the configured Proof
28
+ * Server could not be initiated.
29
+ * @throws {@link ServerError}
30
+ * Unable to establish a connection with the configured Proof Server, or there was an internal error that
31
+ * prevented the proof request from being executed.
32
+ */
33
+ proveTransaction(transaction, costModel) {
34
+ return this.#innerClient.proveTransaction(transaction, costModel).pipe(Effect.runPromise);
35
+ }
36
+ }
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@midnight-ntwrk/wallet-sdk-prover-client",
3
+ "version": "1.0.0-beta.8",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "author": "IOHK",
9
+ "license": "Apache-2.0",
10
+ "publishConfig": {
11
+ "registry": "https://npm.pkg.github.com/"
12
+ },
13
+ "files": [
14
+ "dist/"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/midnight-ntwrk/artifacts.git"
19
+ },
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ },
25
+ "./effect": {
26
+ "types": "./dist/effect/index.d.ts",
27
+ "import": "./dist/effect/index.js"
28
+ }
29
+ },
30
+ "dependencies": {
31
+ "@effect/platform": "^0.90.0",
32
+ "@midnight-ntwrk/ledger-v6": "6.1.0-alpha.5",
33
+ "@midnight-ntwrk/wallet-sdk-abstractions": "1.0.0-beta.8",
34
+ "@midnight-ntwrk/wallet-sdk-utilities": "1.0.0-beta.7",
35
+ "effect": "^3.17.3"
36
+ },
37
+ "devDependencies": {
38
+ "eslint": "^9.37.0",
39
+ "publint": "~0.3.14",
40
+ "rimraf": "^6.0.1",
41
+ "testcontainers": "^11.4.0",
42
+ "typescript": "^5.9.3",
43
+ "vitest": "^3.2.4"
44
+ },
45
+ "scripts": {
46
+ "typecheck": "tsc -b ./tsconfig.json --noEmit",
47
+ "test": "vitest run",
48
+ "lint": "eslint --max-warnings 0",
49
+ "format": "prettier --write \"**/*.{ts,js,json,yaml,yml}\"",
50
+ "dist": "tsc -b ./tsconfig.build.json",
51
+ "dist:publish": "tsc -b ./tsconfig.publish.json",
52
+ "clean": "rimraf --glob dist 'tsconfig.*.tsbuildinfo' && date +%s > .clean-timestamp",
53
+ "publint": "publint --strict"
54
+ }
55
+ }