@midnight-ntwrk/wallet-sdk-utilities 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/ArrayOps.d.ts +16 -0
- package/dist/ArrayOps.js +34 -0
- package/dist/BlobOps.d.ts +8 -0
- package/dist/BlobOps.js +20 -0
- package/dist/DateOps.d.ts +3 -0
- package/dist/DateOps.js +21 -0
- package/dist/EitherOps.d.ts +19 -0
- package/dist/EitherOps.js +39 -0
- package/dist/Fluent.d.ts +6 -0
- package/dist/Fluent.js +1 -0
- package/dist/LedgerOps.d.ts +13 -0
- package/dist/LedgerOps.js +28 -0
- package/dist/ObservableOps.d.ts +16 -0
- package/dist/ObservableOps.js +65 -0
- package/dist/RecordOps.d.ts +2 -0
- package/dist/RecordOps.js +40 -0
- package/dist/hlist.d.ts +26 -0
- package/dist/hlist.js +49 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +22 -0
- package/dist/networking/ClientServerErrors.d.ts +26 -0
- package/dist/networking/ClientServerErrors.js +27 -0
- package/dist/networking/HttpURL.d.ts +19 -0
- package/dist/networking/HttpURL.js +35 -0
- package/dist/networking/URLError.d.ts +27 -0
- package/dist/networking/URLError.js +23 -0
- package/dist/networking/WsURL.d.ts +19 -0
- package/dist/networking/WsURL.js +35 -0
- package/dist/networking/index.d.ts +4 -0
- package/dist/networking/index.js +16 -0
- package/dist/polyFunction.d.ts +14 -0
- package/dist/polyFunction.js +12 -0
- package/dist/testUtils.d.ts +9 -0
- package/dist/testUtils.js +1 -0
- package/dist/testing/compose.d.ts +31 -0
- package/dist/testing/compose.js +58 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/testing/index.js +14 -0
- package/dist/testing/test-containers.d.ts +13 -0
- package/dist/testing/test-containers.js +58 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.js +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { NonEmptyReadonlyArray } from 'effect/Array';
|
|
2
|
+
export declare const fold: {
|
|
3
|
+
<T>(folder: (acc: T, item: T) => T): (arr: NonEmptyReadonlyArray<T>) => T;
|
|
4
|
+
<T>(arr: NonEmptyReadonlyArray<T>, folder: (acc: T, item: T) => T): T;
|
|
5
|
+
};
|
|
6
|
+
export type Monoid<T> = {
|
|
7
|
+
empty: T;
|
|
8
|
+
combine: (a: T, b: T) => T;
|
|
9
|
+
};
|
|
10
|
+
export declare const generalSum: {
|
|
11
|
+
<T>(monoid: Monoid<T>): (arr: ReadonlyArray<T>) => T;
|
|
12
|
+
<T>(arr: ReadonlyArray<T>, monoid: Monoid<T>): T;
|
|
13
|
+
};
|
|
14
|
+
export declare const sumNumber: (arr: ReadonlyArray<number>) => number;
|
|
15
|
+
export declare const sumBigInt: (arr: ReadonlyArray<bigint>) => bigint;
|
|
16
|
+
export declare const assertNonEmpty: <T>(arr: ReadonlyArray<T>) => NonEmptyReadonlyArray<T>;
|
package/dist/ArrayOps.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
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 { reduce, match } from 'effect/Array';
|
|
14
|
+
import { dual } from 'effect/Function';
|
|
15
|
+
export const fold = dual(2, (arr, folder) => arr.reduce(folder));
|
|
16
|
+
export const generalSum = dual(2, (arr, monoid) => reduce(arr, monoid.empty, monoid.combine));
|
|
17
|
+
const numberAdditionMonoid = {
|
|
18
|
+
empty: 0,
|
|
19
|
+
combine: (a, b) => a + b,
|
|
20
|
+
};
|
|
21
|
+
const bigintAdditionMonoid = {
|
|
22
|
+
empty: 0n,
|
|
23
|
+
combine: (a, b) => a + b,
|
|
24
|
+
};
|
|
25
|
+
export const sumNumber = generalSum(numberAdditionMonoid);
|
|
26
|
+
export const sumBigInt = generalSum(bigintAdditionMonoid);
|
|
27
|
+
export const assertNonEmpty = (arr) => {
|
|
28
|
+
return match(arr, {
|
|
29
|
+
onNonEmpty: (refined) => refined,
|
|
30
|
+
onEmpty: () => {
|
|
31
|
+
throw new Error('Expected non-empty array');
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-compatible utility for converting a Blob to Uint8Array.
|
|
3
|
+
* Uses arrayBuffer() instead of bytes() for broader browser support.
|
|
4
|
+
*
|
|
5
|
+
* @param blob The Blob to convert to bytes
|
|
6
|
+
* @returns A Promise that resolves to a Uint8Array containing the blob's bytes
|
|
7
|
+
*/
|
|
8
|
+
export declare const getBytes: (blob: Blob) => Promise<Uint8Array>;
|
package/dist/BlobOps.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
/**
|
|
14
|
+
* Browser-compatible utility for converting a Blob to Uint8Array.
|
|
15
|
+
* Uses arrayBuffer() instead of bytes() for broader browser support.
|
|
16
|
+
*
|
|
17
|
+
* @param blob The Blob to convert to bytes
|
|
18
|
+
* @returns A Promise that resolves to a Uint8Array containing the blob's bytes
|
|
19
|
+
*/
|
|
20
|
+
export const getBytes = (blob) => blob.arrayBuffer().then((ab) => new Uint8Array(ab));
|
package/dist/DateOps.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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 const dateToSeconds = (date) => {
|
|
14
|
+
return BigInt(Math.floor(date.getTime() / 1000));
|
|
15
|
+
};
|
|
16
|
+
export const secondsToDate = (seconds) => {
|
|
17
|
+
return new Date(Number(seconds) * 1000);
|
|
18
|
+
};
|
|
19
|
+
export const addSeconds = (time, seconds) => {
|
|
20
|
+
return new Date(+time + Number(seconds) * 1000);
|
|
21
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Effect, Either } from 'effect';
|
|
2
|
+
export declare const toEffect: <L, R>(either: Either.Either<R, L>) => Effect.Effect<R, L>;
|
|
3
|
+
export declare const flatMapLeft: {
|
|
4
|
+
<R, L, L2>(either: Either.Either<R, L>, cb: (l: L) => Either.Either<R, L2>): Either.Either<R, L2>;
|
|
5
|
+
<R, L, L2>(cb: (l: L) => Either.Either<R, L2>): (either: Either.Either<R, L>) => Either.Either<R, L2>;
|
|
6
|
+
};
|
|
7
|
+
declare const LeftError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
8
|
+
readonly _tag: "LeftError";
|
|
9
|
+
} & Readonly<A>;
|
|
10
|
+
export declare class LeftError<L> extends LeftError_base<{
|
|
11
|
+
message: string;
|
|
12
|
+
cause: L;
|
|
13
|
+
}> {
|
|
14
|
+
constructor({ cause }: {
|
|
15
|
+
cause: L;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export declare const getOrThrowLeft: <L, R>(either: Either.Either<R, L>) => R;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
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, Either, Data } from 'effect';
|
|
14
|
+
import { dual } from 'effect/Function';
|
|
15
|
+
export const toEffect = (either) => {
|
|
16
|
+
return Either.match(either, {
|
|
17
|
+
onLeft: (l) => Effect.fail(l),
|
|
18
|
+
onRight: (r) => Effect.succeed(r),
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
export const flatMapLeft = dual(2, (either, cb) => {
|
|
22
|
+
return Either.match(either, {
|
|
23
|
+
onRight: (r) => Either.right(r),
|
|
24
|
+
onLeft: cb,
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
export class LeftError extends Data.TaggedError('LeftError') {
|
|
28
|
+
constructor({ cause }) {
|
|
29
|
+
super({ message: 'Unexpected left value', cause });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export const getOrThrowLeft = (either) => {
|
|
33
|
+
return Either.match(either, {
|
|
34
|
+
onRight: (r) => r,
|
|
35
|
+
onLeft: (l) => {
|
|
36
|
+
throw new LeftError({ cause: l });
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
};
|
package/dist/Fluent.d.ts
ADDED
package/dist/Fluent.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Either } from 'effect';
|
|
2
|
+
declare const LedgerError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
3
|
+
readonly _tag: "LedgerError";
|
|
4
|
+
} & Readonly<A>;
|
|
5
|
+
export declare class LedgerError extends LedgerError_base<{
|
|
6
|
+
readonly message: string;
|
|
7
|
+
readonly cause?: unknown;
|
|
8
|
+
}> {
|
|
9
|
+
}
|
|
10
|
+
export declare const ledgerTry: <A>(fn: () => A) => Either.Either<A, LedgerError>;
|
|
11
|
+
export declare const generateHex: (len: number) => string;
|
|
12
|
+
export declare const randomNonce: () => string;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
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 { Either, Data } from 'effect';
|
|
14
|
+
export class LedgerError extends Data.TaggedError('LedgerError') {
|
|
15
|
+
}
|
|
16
|
+
export const ledgerTry = (fn) => {
|
|
17
|
+
return Either.try({
|
|
18
|
+
try: fn,
|
|
19
|
+
catch: (error) => {
|
|
20
|
+
const message = error instanceof Error ? error.message : `${error?.toString()}`;
|
|
21
|
+
return new LedgerError({ message: `Error from ledger: ${message}`, cause: error });
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
export const generateHex = (len) => {
|
|
26
|
+
return Buffer.from(crypto.getRandomValues(new Uint8Array(len / 2))).toString('hex');
|
|
27
|
+
};
|
|
28
|
+
export const randomNonce = () => generateHex(64);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Stream } from 'effect';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
/**
|
|
4
|
+
* A utility that creates a Rx.js `Observable` from a given Effect `Stream`.
|
|
5
|
+
*
|
|
6
|
+
* @param stream The Effect `Stream` from which an `Observable` is required.
|
|
7
|
+
* @returns A Rx.js `Observable` that consumes the elements from `stream`.
|
|
8
|
+
*/
|
|
9
|
+
export declare const fromStream: <A, E = never>(stream: Stream.Stream<A, E>) => Observable<A>;
|
|
10
|
+
/**
|
|
11
|
+
* A utility that creates an Effect `Stream` from a given Rx.js `Observable`.
|
|
12
|
+
*
|
|
13
|
+
* @param observable The Rx.js `Observable` from which a `Stream` is required.
|
|
14
|
+
* @returns A `Stream` the consumes elements from `observable`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const toStream: <A, E = never>(observable: Observable<A>) => Stream.Stream<A, E>;
|
|
@@ -0,0 +1,65 @@
|
|
|
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, Stream, Fiber, Option, Chunk } from 'effect';
|
|
14
|
+
import { Observable } from 'rxjs';
|
|
15
|
+
/**
|
|
16
|
+
* A utility that creates a Rx.js `Observable` from a given Effect `Stream`.
|
|
17
|
+
*
|
|
18
|
+
* @param stream The Effect `Stream` from which an `Observable` is required.
|
|
19
|
+
* @returns A Rx.js `Observable` that consumes the elements from `stream`.
|
|
20
|
+
*/
|
|
21
|
+
export const fromStream = (stream) => new Observable((subscriber) => {
|
|
22
|
+
const fiber = Effect.runFork(Effect.scoped(Effect.gen(function* () {
|
|
23
|
+
const pull = yield* Stream.toPull(stream);
|
|
24
|
+
while (true) {
|
|
25
|
+
const shouldBreak = yield* Effect.match(pull, {
|
|
26
|
+
onSuccess(values) {
|
|
27
|
+
Chunk.forEach(values, (element) => {
|
|
28
|
+
subscriber.next(element);
|
|
29
|
+
});
|
|
30
|
+
return false;
|
|
31
|
+
},
|
|
32
|
+
onFailure(error) {
|
|
33
|
+
return Option.match(error, {
|
|
34
|
+
onNone() {
|
|
35
|
+
subscriber.complete();
|
|
36
|
+
return true; // Stream has completed, signal the break.
|
|
37
|
+
},
|
|
38
|
+
onSome: (err) => {
|
|
39
|
+
subscriber.error(err);
|
|
40
|
+
return true;
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
if (shouldBreak)
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
})));
|
|
49
|
+
// Ensure that if the subscription ends we also dispose of the fiber pulling from the stream.
|
|
50
|
+
subscriber.add(() => Effect.runFork(Fiber.interrupt(fiber)));
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* A utility that creates an Effect `Stream` from a given Rx.js `Observable`.
|
|
54
|
+
*
|
|
55
|
+
* @param observable The Rx.js `Observable` from which a `Stream` is required.
|
|
56
|
+
* @returns A `Stream` the consumes elements from `observable`.
|
|
57
|
+
*/
|
|
58
|
+
export const toStream = (observable) => Stream.async((emit) => {
|
|
59
|
+
const subscription = observable.subscribe({
|
|
60
|
+
next: (value) => void emit.single(value),
|
|
61
|
+
error: (err) => void emit.fail(err),
|
|
62
|
+
complete: () => void emit.end(),
|
|
63
|
+
});
|
|
64
|
+
return Effect.sync(() => subscription.unsubscribe());
|
|
65
|
+
});
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const merge: <K extends string | number | symbol, T>(combine: (a: T, b: T) => T) => (records: Array<Record<K, T>>) => Record<K, T>;
|
|
2
|
+
export declare const mergeWithAccumulator: <K extends string | number | symbol, T, S>(mempty: S, combine: (acc: S, b: T) => S) => (records: Array<Record<K, T>>) => Record<K, S>;
|
|
@@ -0,0 +1,40 @@
|
|
|
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 const merge = (combine) => (records) => {
|
|
14
|
+
const result = {};
|
|
15
|
+
for (const record of records) {
|
|
16
|
+
for (const key in record) {
|
|
17
|
+
if (Object.hasOwn(result, key)) {
|
|
18
|
+
result[key] = combine(result[key], record[key]);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
result[key] = record[key];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
export const mergeWithAccumulator = (mempty, combine) => (records) => {
|
|
28
|
+
const result = {};
|
|
29
|
+
for (const record of records) {
|
|
30
|
+
for (const key in record) {
|
|
31
|
+
if (Object.hasOwn(result, key)) {
|
|
32
|
+
result[key] = combine(result[key], record[key]);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
result[key] = combine(mempty, record[key]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
};
|
package/dist/hlist.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PolyFunction, WithTag } from './polyFunction.js';
|
|
2
|
+
/**
|
|
3
|
+
* Heterogeneous list - as in - list, where elements have different types
|
|
4
|
+
* Here - more as an additional API over TS's tuple type
|
|
5
|
+
*/
|
|
6
|
+
export type Empty = [];
|
|
7
|
+
export type NonEmpty<T> = T extends Array<infer E> ? [E, ...T] : never;
|
|
8
|
+
export type Prepend<List extends unknown[], Element> = [Element, ...List];
|
|
9
|
+
export type Append<List extends unknown[], Element> = [...List, Element];
|
|
10
|
+
export type Reverse<List extends unknown[]> = List extends [...infer Init, infer Last] ? [Last, ...Reverse<Init>] : List extends [] ? [] : never;
|
|
11
|
+
export type HeadOr<List, Default> = List extends [infer TheHead, ...any[]] ? TheHead : List extends [] ? Default : never;
|
|
12
|
+
export type Head<List extends any[]> = HeadOr<List, never>;
|
|
13
|
+
export type Tail<List extends unknown[]> = List extends [unknown, ...infer Tail] ? Tail : [];
|
|
14
|
+
export type Tails<List extends unknown[]> = List extends [unknown, ...infer Tail] ? Tails<Tail> | Tail : [];
|
|
15
|
+
export type Each<List extends unknown[]> = List[number];
|
|
16
|
+
export type Find<List extends any[], Predicate> = List extends [infer TheHead, ...infer Rest] ? TheHead extends Predicate ? TheHead : Find<Rest, Predicate> : never;
|
|
17
|
+
export declare const empty: Empty;
|
|
18
|
+
export declare const prepend: <List extends unknown[], Element>(list: List, element: Element) => Prepend<List, Element>;
|
|
19
|
+
export declare const append: <List extends unknown[], Element>(list: List, element: Element) => Append<List, Element>;
|
|
20
|
+
export declare function headOr<List extends unknown[], Default>(list: List, def: () => Default): HeadOr<List, Default>;
|
|
21
|
+
export declare const head: <List extends unknown[]>(list: List) => Head<List>;
|
|
22
|
+
export declare const tail: <List extends unknown[]>(list: List) => Tail<List>;
|
|
23
|
+
export declare const reverse: <List extends unknown[]>(list: List) => Reverse<List>;
|
|
24
|
+
export declare const find: <List extends unknown[], Predicate>(list: List, predicate: (value: Each<List>) => value is Predicate) => Find<List, Predicate>;
|
|
25
|
+
export declare const foldLeft: <List extends WithTag<string | symbol>[], Acc>(list: List, acc: Acc, folder: (acc: Acc) => PolyFunction<Each<List>, Acc>) => Acc;
|
|
26
|
+
export declare const foldRight: <List extends WithTag<string | symbol>[], Acc>(list: List, acc: Acc, folder: (acc: Acc) => PolyFunction<Each<List>, Acc>) => Acc;
|
package/dist/hlist.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
14
|
+
import { dispatch } from './polyFunction.js';
|
|
15
|
+
export const empty = [];
|
|
16
|
+
export const prepend = (list, element) => {
|
|
17
|
+
return [element, ...list];
|
|
18
|
+
};
|
|
19
|
+
export const append = (list, element) => {
|
|
20
|
+
return [...list, element];
|
|
21
|
+
};
|
|
22
|
+
export function headOr(list, def) {
|
|
23
|
+
if (list.length == 0) {
|
|
24
|
+
return def();
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return list.at(0);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export const head = (list) => {
|
|
31
|
+
return headOr(list, () => {
|
|
32
|
+
throw new Error('Cannot get head from empty hlist');
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
export const tail = (list) => {
|
|
36
|
+
return list.toSpliced(0, 1);
|
|
37
|
+
};
|
|
38
|
+
export const reverse = (list) => {
|
|
39
|
+
return list.toReversed();
|
|
40
|
+
};
|
|
41
|
+
export const find = (list, predicate) => {
|
|
42
|
+
return list.find(predicate);
|
|
43
|
+
};
|
|
44
|
+
export const foldLeft = (list, acc, folder) => {
|
|
45
|
+
return list.reduce((acc, item) => dispatch(item, folder(acc)), acc);
|
|
46
|
+
};
|
|
47
|
+
export const foldRight = (list, acc, folder) => {
|
|
48
|
+
return list.reduceRight((acc, item) => dispatch(item, folder(acc)), acc);
|
|
49
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * as ArrayOps from './ArrayOps.js';
|
|
2
|
+
export * as BlobOps from './BlobOps.js';
|
|
3
|
+
export * as DateOps from './DateOps.js';
|
|
4
|
+
export * as EitherOps from './EitherOps.js';
|
|
5
|
+
export * as Fluent from './Fluent.js';
|
|
6
|
+
export * as HList from './hlist.js';
|
|
7
|
+
export * as LedgerOps from './LedgerOps.js';
|
|
8
|
+
export * as ObservableOps from './ObservableOps.js';
|
|
9
|
+
export * as Poly from './polyFunction.js';
|
|
10
|
+
export * as RecordOps from './RecordOps.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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 ArrayOps from './ArrayOps.js';
|
|
14
|
+
export * as BlobOps from './BlobOps.js';
|
|
15
|
+
export * as DateOps from './DateOps.js';
|
|
16
|
+
export * as EitherOps from './EitherOps.js';
|
|
17
|
+
export * as Fluent from './Fluent.js';
|
|
18
|
+
export * as HList from './hlist.js';
|
|
19
|
+
export * as LedgerOps from './LedgerOps.js';
|
|
20
|
+
export * as ObservableOps from './ObservableOps.js';
|
|
21
|
+
export * as Poly from './polyFunction.js';
|
|
22
|
+
export * as RecordOps from './RecordOps.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
declare const ClientError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
2
|
+
readonly _tag: "ClientError";
|
|
3
|
+
} & Readonly<A>;
|
|
4
|
+
/**
|
|
5
|
+
* An error representing a connection or client-side error.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* This error typically indicates a connection issue with a target server, or when the client has submitted some
|
|
9
|
+
* data that could not be processed.
|
|
10
|
+
*/
|
|
11
|
+
export declare class ClientError extends ClientError_base<{
|
|
12
|
+
readonly message: string;
|
|
13
|
+
readonly cause?: unknown;
|
|
14
|
+
}> {
|
|
15
|
+
}
|
|
16
|
+
declare const ServerError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
17
|
+
readonly _tag: "ServerError";
|
|
18
|
+
} & Readonly<A>;
|
|
19
|
+
/**
|
|
20
|
+
* An error representing a server-side error.
|
|
21
|
+
*/
|
|
22
|
+
export declare class ServerError extends ServerError_base<{
|
|
23
|
+
readonly message: string;
|
|
24
|
+
}> {
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
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 { Data } from 'effect';
|
|
14
|
+
/**
|
|
15
|
+
* An error representing a connection or client-side error.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* This error typically indicates a connection issue with a target server, or when the client has submitted some
|
|
19
|
+
* data that could not be processed.
|
|
20
|
+
*/
|
|
21
|
+
export class ClientError extends Data.TaggedError('ClientError') {
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* An error representing a server-side error.
|
|
25
|
+
*/
|
|
26
|
+
export class ServerError extends Data.TaggedError('ServerError') {
|
|
27
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Either } from 'effect';
|
|
2
|
+
import * as Brand from 'effect/Brand';
|
|
3
|
+
import { InvalidProtocolSchemeError } from './URLError.js';
|
|
4
|
+
/**
|
|
5
|
+
* A 'HTTP' URL.
|
|
6
|
+
*/
|
|
7
|
+
export type HttpUrl = Brand.Branded<URL, 'HttpURL'>;
|
|
8
|
+
/**
|
|
9
|
+
* Constructs a 'HTTP' URL from a source URL, ensuring that the protocol is correct.
|
|
10
|
+
*/
|
|
11
|
+
export declare const HttpURL: Brand.Brand.Constructor<HttpUrl>;
|
|
12
|
+
/**
|
|
13
|
+
* Constructs a new {@link HttpURL} from a given string.
|
|
14
|
+
*
|
|
15
|
+
* @param url The URL to be made into a HTTP URL.
|
|
16
|
+
* @returns An `Either` that represents the valid HTTP URL constructed from `url`; or an
|
|
17
|
+
* {@link InvalidProtocolSchemeError}.
|
|
18
|
+
*/
|
|
19
|
+
export declare const make: (url: URL | string) => Either.Either<HttpUrl, InvalidProtocolSchemeError>;
|
|
@@ -0,0 +1,35 @@
|
|
|
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 { Either } from 'effect';
|
|
14
|
+
import * as Brand from 'effect/Brand';
|
|
15
|
+
import { InvalidProtocolSchemeError } from './URLError.js';
|
|
16
|
+
/**
|
|
17
|
+
* Constructs a 'HTTP' URL from a source URL, ensuring that the protocol is correct.
|
|
18
|
+
*/
|
|
19
|
+
export const HttpURL = Brand.refined((url) => url.protocol === 'http:' || url.protocol === 'https:', (url) => Brand.error(`Invalid protocol scheme '${url.protocol}'. Expected 'http:' or 'https:'`));
|
|
20
|
+
/**
|
|
21
|
+
* Constructs a new {@link HttpURL} from a given string.
|
|
22
|
+
*
|
|
23
|
+
* @param url The URL to be made into a HTTP URL.
|
|
24
|
+
* @returns An `Either` that represents the valid HTTP URL constructed from `url`; or an
|
|
25
|
+
* {@link InvalidProtocolSchemeError}.
|
|
26
|
+
*/
|
|
27
|
+
export const make = (url) => {
|
|
28
|
+
const targetURL = new URL(url);
|
|
29
|
+
try {
|
|
30
|
+
return Either.right(HttpURL(targetURL));
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
return Either.left(new InvalidProtocolSchemeError({ message: String(err), invalidScheme: targetURL.protocol }));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare const InvalidProtocolSchemeError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
2
|
+
readonly _tag: "InvalidProtocolSchemeError";
|
|
3
|
+
} & Readonly<A>;
|
|
4
|
+
/**
|
|
5
|
+
* A configuration error where the protocol scheme of a given server URL was unexpected (e.g., used
|
|
6
|
+
* `'ftp:'` rather than `'http:'` for a server running over HTTP).
|
|
7
|
+
*/
|
|
8
|
+
export declare class InvalidProtocolSchemeError extends InvalidProtocolSchemeError_base<{
|
|
9
|
+
/** A message describing the error. */
|
|
10
|
+
readonly message: string;
|
|
11
|
+
/** The scheme that caused the error. */
|
|
12
|
+
readonly invalidScheme: string;
|
|
13
|
+
}> {
|
|
14
|
+
static readonly tag: "InvalidProtocolSchemeError";
|
|
15
|
+
}
|
|
16
|
+
declare const FailedToDeriveWebSocketUrlError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
17
|
+
readonly _tag: "FailedToDeriveWebSocketUrlError";
|
|
18
|
+
} & Readonly<A>;
|
|
19
|
+
export declare class FailedToDeriveWebSocketUrlError extends FailedToDeriveWebSocketUrlError_base<{
|
|
20
|
+
/** A message describing the error. */
|
|
21
|
+
readonly message: string;
|
|
22
|
+
readonly cause?: unknown;
|
|
23
|
+
}> {
|
|
24
|
+
static readonly tag: "FailedToDeriveWebSocketUrlError";
|
|
25
|
+
}
|
|
26
|
+
export type URLError = InvalidProtocolSchemeError | FailedToDeriveWebSocketUrlError;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
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 { Data } from 'effect';
|
|
14
|
+
/**
|
|
15
|
+
* A configuration error where the protocol scheme of a given server URL was unexpected (e.g., used
|
|
16
|
+
* `'ftp:'` rather than `'http:'` for a server running over HTTP).
|
|
17
|
+
*/
|
|
18
|
+
export class InvalidProtocolSchemeError extends Data.TaggedError('InvalidProtocolSchemeError') {
|
|
19
|
+
static tag = 'InvalidProtocolSchemeError';
|
|
20
|
+
}
|
|
21
|
+
export class FailedToDeriveWebSocketUrlError extends Data.TaggedError('FailedToDeriveWebSocketUrlError') {
|
|
22
|
+
static tag = 'FailedToDeriveWebSocketUrlError';
|
|
23
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Either } from 'effect';
|
|
2
|
+
import * as Brand from 'effect/Brand';
|
|
3
|
+
import { InvalidProtocolSchemeError } from './URLError.js';
|
|
4
|
+
/**
|
|
5
|
+
* A 'HTTP' URL.
|
|
6
|
+
*/
|
|
7
|
+
export type WsURL = Brand.Branded<URL, 'WsURL'>;
|
|
8
|
+
/**
|
|
9
|
+
* Constructs a 'WS' URL from a source URL, ensuring that the protocol is correct.
|
|
10
|
+
*/
|
|
11
|
+
export declare const WsURL: Brand.Brand.Constructor<WsURL>;
|
|
12
|
+
/**
|
|
13
|
+
* Constructs a new {@link WsURL} from a given string.
|
|
14
|
+
*
|
|
15
|
+
* @param url The URL to be made into a WebSocket URL.
|
|
16
|
+
* @returns An `Either` that represents the valid WebSocket URL constructed from `url`; or an
|
|
17
|
+
* {@link InvalidProtocolSchemeError}.
|
|
18
|
+
*/
|
|
19
|
+
export declare const make: (url: URL | string) => Either.Either<WsURL, InvalidProtocolSchemeError>;
|
|
@@ -0,0 +1,35 @@
|
|
|
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 { Either } from 'effect';
|
|
14
|
+
import * as Brand from 'effect/Brand';
|
|
15
|
+
import { InvalidProtocolSchemeError } from './URLError.js';
|
|
16
|
+
/**
|
|
17
|
+
* Constructs a 'WS' URL from a source URL, ensuring that the protocol is correct.
|
|
18
|
+
*/
|
|
19
|
+
export const WsURL = Brand.refined((url) => url.protocol === 'ws:' || url.protocol === 'wss:', (url) => Brand.error(`Invalid protocol scheme '${url.protocol}'. Expected 'ws:' or 'wss:'`));
|
|
20
|
+
/**
|
|
21
|
+
* Constructs a new {@link WsURL} from a given string.
|
|
22
|
+
*
|
|
23
|
+
* @param url The URL to be made into a WebSocket URL.
|
|
24
|
+
* @returns An `Either` that represents the valid WebSocket URL constructed from `url`; or an
|
|
25
|
+
* {@link InvalidProtocolSchemeError}.
|
|
26
|
+
*/
|
|
27
|
+
export const make = (url) => {
|
|
28
|
+
const targetURL = new URL(url);
|
|
29
|
+
try {
|
|
30
|
+
return Either.right(WsURL(targetURL));
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
return Either.left(new InvalidProtocolSchemeError({ message: String(err), invalidScheme: targetURL.protocol }));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
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 HttpURL from './HttpURL.js';
|
|
14
|
+
export * as WsURL from './WsURL.js';
|
|
15
|
+
export * from './URLError.js';
|
|
16
|
+
export * from './ClientServerErrors.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type WithTag<T extends string | symbol> = {
|
|
2
|
+
__polyTag__: T;
|
|
3
|
+
};
|
|
4
|
+
export type TagOf<T> = T extends WithTag<infer Tag> ? Tag : never;
|
|
5
|
+
export type WithTagFrom<T> = WithTag<TagOf<T>>;
|
|
6
|
+
/**
|
|
7
|
+
* Polymorphic function - function defined for multiple types at once
|
|
8
|
+
* Leveraging tagging mechanics it can predictably work at runtime and be quite intuitively defined by hand
|
|
9
|
+
*/
|
|
10
|
+
export type PolyFunction<Variants extends WithTag<string | symbol>, T> = {
|
|
11
|
+
[V in Variants as TagOf<V>]: (variant: V) => T;
|
|
12
|
+
};
|
|
13
|
+
export declare const getTag: <TTag extends string | symbol>(t: WithTag<TTag>) => TTag;
|
|
14
|
+
export declare const dispatch: <TVariant extends WithTag<string | symbol>, TResult>(subject: TVariant, impl: PolyFunction<TVariant, TResult>) => TResult;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const getTag = (t) => t.__polyTag__;
|
|
2
|
+
export const dispatch = (subject, impl) => {
|
|
3
|
+
if (subject.__polyTag__ in impl) {
|
|
4
|
+
//Sadly, the type casts below are needed because eslint or TS limitations
|
|
5
|
+
const subjectTag = subject.__polyTag__;
|
|
6
|
+
const chosen = impl[subjectTag];
|
|
7
|
+
return chosen(subject);
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
throw new Error(`Not found implementation for ${String(subject.__polyTag__)}`);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A utility type that ensures that a given type is `true` or otherwise forces a compile time error.
|
|
3
|
+
*/
|
|
4
|
+
export type Expect<T extends true> = T;
|
|
5
|
+
/**
|
|
6
|
+
* A utility type that exactly compares two types for equality.
|
|
7
|
+
*/
|
|
8
|
+
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
9
|
+
export type ItemType<T> = T extends ReadonlyArray<infer R> ? R : never;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the absolute path to the repository root directory.
|
|
3
|
+
*
|
|
4
|
+
* @returns The absolute path to the repository root
|
|
5
|
+
*/
|
|
6
|
+
export declare function getRepositoryRoot(): string;
|
|
7
|
+
/**
|
|
8
|
+
* Gets the absolute path to the docker compose directory.
|
|
9
|
+
*
|
|
10
|
+
* @returns The absolute path to the compose directory
|
|
11
|
+
*/
|
|
12
|
+
export declare function getComposeDirectory(): string;
|
|
13
|
+
/**
|
|
14
|
+
* Options for building test environment variables.
|
|
15
|
+
*/
|
|
16
|
+
export interface BuildTestEnvironmentVariablesOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Additional environment variables to include.
|
|
19
|
+
* These will be merged with the variables collected from process.env.
|
|
20
|
+
*/
|
|
21
|
+
additionalVars?: Record<string, string>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Builds and validates environment variables for test containers.
|
|
25
|
+
* Throws an error if any required environment variable from envVarsToPass is missing.
|
|
26
|
+
*
|
|
27
|
+
* @param envVarsToPass - Array of environment variable names to collect from process.env
|
|
28
|
+
* @param options - Optional configuration for building environment variables
|
|
29
|
+
* @returns Record of environment variables to pass to Docker Compose
|
|
30
|
+
*/
|
|
31
|
+
export declare function buildTestEnvironmentVariables(envVarsToPass: readonly string[], options?: BuildTestEnvironmentVariablesOptions): Record<string, string>;
|
|
@@ -0,0 +1,58 @@
|
|
|
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 * as path from 'node:path';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
/**
|
|
16
|
+
* Gets the absolute path to the repository root directory.
|
|
17
|
+
*
|
|
18
|
+
* @returns The absolute path to the repository root
|
|
19
|
+
*/
|
|
20
|
+
export function getRepositoryRoot() {
|
|
21
|
+
const currentFile = fileURLToPath(import.meta.url);
|
|
22
|
+
const currentDir = path.dirname(currentFile);
|
|
23
|
+
return path.resolve(currentDir, '../../../../');
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Gets the absolute path to the docker compose directory.
|
|
27
|
+
*
|
|
28
|
+
* @returns The absolute path to the compose directory
|
|
29
|
+
*/
|
|
30
|
+
export function getComposeDirectory() {
|
|
31
|
+
const repoRoot = getRepositoryRoot();
|
|
32
|
+
return path.join(repoRoot, 'infra', 'compose');
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Builds and validates environment variables for test containers.
|
|
36
|
+
* Throws an error if any required environment variable from envVarsToPass is missing.
|
|
37
|
+
*
|
|
38
|
+
* @param envVarsToPass - Array of environment variable names to collect from process.env
|
|
39
|
+
* @param options - Optional configuration for building environment variables
|
|
40
|
+
* @returns Record of environment variables to pass to Docker Compose
|
|
41
|
+
*/
|
|
42
|
+
export function buildTestEnvironmentVariables(envVarsToPass, options) {
|
|
43
|
+
// Add any additional vars first (so they can be overridden by process.env vars if needed)
|
|
44
|
+
const environmentVars = {
|
|
45
|
+
...options?.additionalVars,
|
|
46
|
+
};
|
|
47
|
+
// Collect and validate required environment variables
|
|
48
|
+
for (const envVar of envVarsToPass) {
|
|
49
|
+
const value = process.env[envVar];
|
|
50
|
+
if (value) {
|
|
51
|
+
environmentVars[envVar] = value;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
throw new Error(`Required environment variable ${envVar} is not set. Please ensure it is exported in your shell or CI environment.`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return environmentVars;
|
|
58
|
+
}
|
|
@@ -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 TestContainers from './test-containers.js';
|
|
14
|
+
export * from './compose.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Effect, Scope } from 'effect';
|
|
2
|
+
import { GenericContainer, StartedNetwork, type StartedTestContainer } from 'testcontainers';
|
|
3
|
+
export declare const createNetwork: () => Effect.Effect<StartedNetwork, never, Scope.Scope>;
|
|
4
|
+
export declare const runNodeContainer: (adjustment?: (t: GenericContainer) => GenericContainer) => Effect.Effect<StartedTestContainer, Error, Scope.Scope>;
|
|
5
|
+
export declare const runProofServerContainer: (adjustment?: (t: GenericContainer) => GenericContainer) => Effect.Effect<StartedTestContainer, Error, Scope.Scope>;
|
|
6
|
+
export declare const runTxGenerator: (config: {
|
|
7
|
+
nodeUrl: string;
|
|
8
|
+
destPath: string;
|
|
9
|
+
fileName: string;
|
|
10
|
+
txsPerBatch: number;
|
|
11
|
+
batches: number;
|
|
12
|
+
}, adjustment?: (t: GenericContainer) => GenericContainer) => Effect.Effect<StartedTestContainer, Error, Scope.Scope>;
|
|
13
|
+
export declare const findAvailablePort: Effect.Effect<number>;
|
|
@@ -0,0 +1,58 @@
|
|
|
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, identity } from 'effect';
|
|
14
|
+
import { GenericContainer, Network, Wait } from 'testcontainers';
|
|
15
|
+
import { getPortPromise } from 'portfinder';
|
|
16
|
+
export const createNetwork = () => Effect.acquireRelease(Effect.promise(() => new Network().start()), (net) => Effect.promise(() => net.stop()));
|
|
17
|
+
const startContainer = (container) => {
|
|
18
|
+
return Effect.acquireRelease(Effect.promise(() => container.start()), (container) => Effect.promise(() => container.stop({ timeout: 5_000 })));
|
|
19
|
+
};
|
|
20
|
+
export const runNodeContainer = (adjustment = identity) => {
|
|
21
|
+
const container = new GenericContainer('ghcr.io/midnight-ntwrk/midnight-node:0.20.0-alpha.1')
|
|
22
|
+
.withEnvironment({
|
|
23
|
+
CFG_PRESET: 'dev',
|
|
24
|
+
SIDECHAIN_BLOCK_BENEFICIARY: '04bcf7ad3be7a5c790460be82a713af570f22e0f801f6659ab8e84a52be6969e',
|
|
25
|
+
})
|
|
26
|
+
.withExposedPorts(9944)
|
|
27
|
+
.withWaitStrategy(Wait.forListeningPorts());
|
|
28
|
+
return startContainer(adjustment(container));
|
|
29
|
+
};
|
|
30
|
+
export const runProofServerContainer = (adjustment = identity) => {
|
|
31
|
+
const container = new GenericContainer('ghcr.io/midnight-ntwrk/proof-server:7.0.0-rc.1')
|
|
32
|
+
.withEnvironment({
|
|
33
|
+
RUST_BACKTRACE: 'full',
|
|
34
|
+
})
|
|
35
|
+
.withExposedPorts(6300)
|
|
36
|
+
.withCommand(['midnight-proof-server -v'])
|
|
37
|
+
.withWaitStrategy(Wait.forListeningPorts());
|
|
38
|
+
return startContainer(adjustment(container));
|
|
39
|
+
};
|
|
40
|
+
export const runTxGenerator = (config, adjustment = identity) => {
|
|
41
|
+
const container = new GenericContainer('ghcr.io/midnight-ntwrk/midnight-node-toolkit:0.20.0-alpha.1')
|
|
42
|
+
.withBindMounts([{ source: config.destPath, target: '/tmp', mode: 'rw' }])
|
|
43
|
+
.withCommand([
|
|
44
|
+
'generate-txs',
|
|
45
|
+
'--src-url',
|
|
46
|
+
config.nodeUrl,
|
|
47
|
+
'--dest-file',
|
|
48
|
+
`/tmp/${config.fileName}`,
|
|
49
|
+
'batches',
|
|
50
|
+
'--num-batches',
|
|
51
|
+
String(config.batches),
|
|
52
|
+
'--num-txs-per-batch',
|
|
53
|
+
String(config.txsPerBatch),
|
|
54
|
+
])
|
|
55
|
+
.withWaitStrategy(Wait.forLogMessage('✓ generated transactions'));
|
|
56
|
+
return startContainer(adjustment(container));
|
|
57
|
+
};
|
|
58
|
+
export const findAvailablePort = Effect.promise(() => getPortPromise());
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A utility type that checks whether type A can be assigned to type To
|
|
3
|
+
* It appears to be useful when exact inferred type are slightly too complex to express and we want the express
|
|
4
|
+
* a slightly simplified type rule like Expect<CanAssign<{foo: number}, object & {foo: number}>>
|
|
5
|
+
*/
|
|
6
|
+
export type CanAssign<A, To> = A extends To ? true : false;
|
|
7
|
+
/**
|
|
8
|
+
* A utility type that ensures that a given type is `true` or otherwise forces a compile time error.
|
|
9
|
+
*/
|
|
10
|
+
export type Expect<T extends true> = T;
|
|
11
|
+
export type ItemType<T> = T extends ReadonlyArray<infer R> ? R : never;
|
|
12
|
+
/**
|
|
13
|
+
* A utility type that exactly compares two types for equality.
|
|
14
|
+
*/
|
|
15
|
+
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midnight-ntwrk/wallet-sdk-utilities",
|
|
3
|
+
"description": "Domain-agnostic utilities for the wallet SDK - common operations and types",
|
|
4
|
+
"version": "1.0.0-beta.10",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"author": "Midnight Foundation",
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"registry": "https://npm.pkg.github.com/"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/midnight-ntwrk/artifacts.git"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"import": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./networking": {
|
|
27
|
+
"types": "./dist/networking/index.d.ts",
|
|
28
|
+
"import": "./dist/networking/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./types": {
|
|
31
|
+
"types": "./dist/types.d.ts",
|
|
32
|
+
"import": "./dist/types.js"
|
|
33
|
+
},
|
|
34
|
+
"./testing": {
|
|
35
|
+
"types": "./dist/testing/index.d.ts",
|
|
36
|
+
"import": "./dist/testing/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"effect": "^3.17.3",
|
|
41
|
+
"portfinder": "^1.0.37",
|
|
42
|
+
"rxjs": "^7.5",
|
|
43
|
+
"testcontainers": "^11.10.0"
|
|
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,md}\"",
|
|
50
|
+
"format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml,md}\"",
|
|
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
|
+
"devDependencies": {
|
|
57
|
+
"eslint": "^9.37.0",
|
|
58
|
+
"fast-check": "^4.2.0",
|
|
59
|
+
"prettier": "^3.7.0",
|
|
60
|
+
"publint": "~0.3.14",
|
|
61
|
+
"rimraf": "^6.0.1",
|
|
62
|
+
"typescript": "^5.9.3",
|
|
63
|
+
"vitest": "^4.0.16"
|
|
64
|
+
}
|
|
65
|
+
}
|