@midnight-ntwrk/wallet-sdk-prover-client 1.0.0-beta.10
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/effect/HttpProverClient.d.ts +11 -0
- package/dist/effect/HttpProverClient.js +73 -0
- package/dist/effect/ProverClient.d.ts +29 -0
- package/dist/effect/ProverClient.js +18 -0
- package/dist/effect/index.d.ts +2 -0
- package/dist/effect/index.js +14 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +48 -0
- package/package.json +56 -0
|
@@ -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,73 @@
|
|
|
1
|
+
// This file is part of MIDNIGHT-WALLET-SDK.
|
|
2
|
+
// Copyright (C) 2025 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
|
+
import { Chunk, Duration, Effect, Either, Layer, pipe, Schedule, Stream } from 'effect';
|
|
14
|
+
import { FetchHttpClient, HttpClient, HttpClientRequest } from '@effect/platform';
|
|
15
|
+
import { SerializedTransaction, SerializedUnprovenTransaction } from '@midnight-ntwrk/wallet-sdk-abstractions';
|
|
16
|
+
import { ProverClient } from './ProverClient.js';
|
|
17
|
+
import { ClientError, ServerError, HttpURL, } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
|
|
18
|
+
import { BlobOps } from '@midnight-ntwrk/wallet-sdk-utilities';
|
|
19
|
+
import * as ledger from '@midnight-ntwrk/ledger-v6';
|
|
20
|
+
const PROVE_TX_PATH = '/prove';
|
|
21
|
+
const CHECK_TX_PATH = '/check';
|
|
22
|
+
/**
|
|
23
|
+
* Creates a layer for a {@link ProverClient} that sends requests to a Proof Server over HTTP.
|
|
24
|
+
*
|
|
25
|
+
* @param config The server configuration to use when configuring the HTTP elements of the layer.
|
|
26
|
+
* @returns A `Layer` for {@link ProverClient} that sends requests to a configured Proof Server over HTTP.
|
|
27
|
+
* The layer can fail with an `InvalidProtocolSchemeError` if `config` is invalid for a HTTP context.
|
|
28
|
+
*/
|
|
29
|
+
export const layer = (config) => Layer.effect(ProverClient, HttpURL.make(new URL(config.url)).pipe(Either.map((baseUrl) => new HttpProverClientImpl(baseUrl)), Either.match({
|
|
30
|
+
onLeft: (l) => Effect.fail(l),
|
|
31
|
+
onRight: (r) => Effect.succeed(r),
|
|
32
|
+
})));
|
|
33
|
+
class HttpProverClientImpl {
|
|
34
|
+
constructor(baseUrl) {
|
|
35
|
+
this.baseUrl = baseUrl;
|
|
36
|
+
}
|
|
37
|
+
baseUrl;
|
|
38
|
+
request(path, transaction, failurePrefix) {
|
|
39
|
+
const concatBytes = (chunks) => Effect.promise(() => BlobOps.getBytes(new Blob(chunks)));
|
|
40
|
+
const receiveBody = (response) => pipe(response.stream, Stream.runCollect, Effect.flatMap((chunks) => concatBytes(Chunk.toArray(chunks))));
|
|
41
|
+
// Build endpoint URL from the already validated base URL
|
|
42
|
+
const url = HttpURL.HttpURL(new URL(path, this.baseUrl));
|
|
43
|
+
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* () {
|
|
44
|
+
if (response.status !== 200) {
|
|
45
|
+
const text = yield* response.text;
|
|
46
|
+
return yield* new ClientError({ message: `${failurePrefix}: ${text}` });
|
|
47
|
+
}
|
|
48
|
+
return yield* receiveBody(response);
|
|
49
|
+
})), Effect.retry({
|
|
50
|
+
times: 3,
|
|
51
|
+
while: (error) =>
|
|
52
|
+
// Retry if we get a Bad Gateway, Service Unavailable, or Gateway Timeout error.
|
|
53
|
+
error._tag === 'ResponseError' && error.response.status >= 502 && error.response.status <= 504,
|
|
54
|
+
schedule: Schedule.exponential(Duration.seconds(2), 2),
|
|
55
|
+
}));
|
|
56
|
+
return proveTxRequest.pipe(Effect.map(SerializedTransaction), Effect.catchTags({
|
|
57
|
+
RequestError: (err) => new ClientError({ message: `Failed to connect to Proof Server: ${err.message}` }),
|
|
58
|
+
ResponseError: (err) => Effect.orElseSucceed(err.response.text, () => 'Unknown server error').pipe(Effect.flatMap((message) => new ServerError({ message }))),
|
|
59
|
+
}), Effect.provide(FetchHttpClient.layer));
|
|
60
|
+
}
|
|
61
|
+
serverProverProvider = () => ({
|
|
62
|
+
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),
|
|
63
|
+
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),
|
|
64
|
+
});
|
|
65
|
+
proveTransaction(transaction, costModel) {
|
|
66
|
+
return pipe(Effect.succeed(this.serverProverProvider()), Effect.flatMap((provider) => Effect.tryPromise({
|
|
67
|
+
try: () => transaction.prove(provider, costModel),
|
|
68
|
+
catch: (error) => error instanceof ClientError || error instanceof ServerError
|
|
69
|
+
? error
|
|
70
|
+
: new ClientError({ message: 'Failed to prove transaction', cause: error }),
|
|
71
|
+
})));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -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,18 @@
|
|
|
1
|
+
// This file is part of MIDNIGHT-WALLET-SDK.
|
|
2
|
+
// Copyright (C) 2025 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
|
+
import { Context } from 'effect';
|
|
14
|
+
/**
|
|
15
|
+
* A client that provides proof services for unproven transactions.
|
|
16
|
+
*/
|
|
17
|
+
export class ProverClient extends Context.Tag('@midnight-ntwrk/prover-client#ProverClient')() {
|
|
18
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// This file is part of MIDNIGHT-WALLET-SDK.
|
|
2
|
+
// Copyright (C) 2025 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
|
+
export * as ProverClient from './ProverClient.js';
|
|
14
|
+
export * as HttpProverClient from './HttpProverClient.js';
|
package/dist/index.d.ts
ADDED
|
@@ -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,48 @@
|
|
|
1
|
+
// This file is part of MIDNIGHT-WALLET-SDK.
|
|
2
|
+
// Copyright (C) 2025 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
|
+
import { Effect } from 'effect';
|
|
14
|
+
import { ProverClient, HttpProverClient as _HttpProverClient } from './effect/index.js';
|
|
15
|
+
/**
|
|
16
|
+
* Sends serialized unproven transactions to a Proof Server over HTTP.
|
|
17
|
+
*/
|
|
18
|
+
export class HttpProverClient {
|
|
19
|
+
#innerClient;
|
|
20
|
+
/**
|
|
21
|
+
* Initializes a new {@link HttpProverClient}.
|
|
22
|
+
*
|
|
23
|
+
* @param config The server configuration to use when configuring the HTTP elements of the Proof Server.
|
|
24
|
+
* @throws {@link ProverClient.InvalidProtocolSchemeError}
|
|
25
|
+
* The `config` is invalid for a HTTP context. E.g., expecting 'http:' or 'https:' URLs but something other
|
|
26
|
+
* was provided.
|
|
27
|
+
*/
|
|
28
|
+
constructor(config) {
|
|
29
|
+
this.#innerClient = Effect.gen(function* () {
|
|
30
|
+
return yield* ProverClient.ProverClient;
|
|
31
|
+
}).pipe(Effect.provide(_HttpProverClient.layer(config)), Effect.runSync);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Proves an unproven transaction by submitting it over HTTP to an associated Proof Server.
|
|
35
|
+
*
|
|
36
|
+
* @param transaction An unproven ledger transaction.
|
|
37
|
+
* @returns A `Promise` that resolves with a proven transaction or fails with a client or server side error.
|
|
38
|
+
* @throws {@link ClientError}
|
|
39
|
+
* There was an issue with the provided `transaction`, or a connection with the configured Proof
|
|
40
|
+
* Server could not be initiated.
|
|
41
|
+
* @throws {@link ServerError}
|
|
42
|
+
* Unable to establish a connection with the configured Proof Server, or there was an internal error that
|
|
43
|
+
* prevented the proof request from being executed.
|
|
44
|
+
*/
|
|
45
|
+
proveTransaction(transaction, costModel) {
|
|
46
|
+
return this.#innerClient.proveTransaction(transaction, costModel).pipe(Effect.runPromise);
|
|
47
|
+
}
|
|
48
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midnight-ntwrk/wallet-sdk-prover-client",
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"author": "Midnight Foundation",
|
|
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.6",
|
|
33
|
+
"@midnight-ntwrk/wallet-sdk-abstractions": "1.0.0-beta.9",
|
|
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.8.1",
|
|
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
|
+
"format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml}\"",
|
|
51
|
+
"dist": "tsc -b ./tsconfig.build.json",
|
|
52
|
+
"dist:publish": "tsc -b ./tsconfig.publish.json",
|
|
53
|
+
"clean": "rimraf --glob dist 'tsconfig.*.tsbuildinfo' && date +%s > .clean-timestamp",
|
|
54
|
+
"publint": "publint --strict"
|
|
55
|
+
}
|
|
56
|
+
}
|