@midnightntwrk/wallet-sdk-shielded 3.0.2

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,172 @@
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
+ import * as ledger from '@midnight-ntwrk/ledger-v8';
14
+ import { Effect, Scope } from 'effect';
15
+ import { WalletSeed } from '@midnightntwrk/wallet-sdk-abstractions';
16
+ import { RunningV1Variant, V1Tag } from './RunningV1Variant.js';
17
+ import { makeDefaultV1SerializationCapability } from './Serialization.js';
18
+ import { makeEventsSyncService, makeEventsSyncCapability, } from './Sync.js';
19
+ import { makeDefaultTransactingCapability, } from './Transacting.js';
20
+ import { makeDefaultCoinsAndBalancesCapability } from './CoinsAndBalances.js';
21
+ import { makeDefaultKeysCapability } from './Keys.js';
22
+ import { chooseCoin } from '@midnightntwrk/wallet-sdk-capabilities';
23
+ import { CoreWallet, PublicKeys } from './CoreWallet.js';
24
+ import { makeDefaultTransactionHistoryService, } from './TransactionHistory.js';
25
+ const V1BuilderSymbol = {
26
+ typeId: Symbol('@midnight-ntwrk/wallet#V1Builder'),
27
+ };
28
+ export class V1Builder {
29
+ #buildState;
30
+ constructor(buildState = {}) {
31
+ this.#buildState = buildState;
32
+ }
33
+ withDefaults() {
34
+ return this.withDefaultTransactionType()
35
+ .withSyncDefaults()
36
+ .withSerializationDefaults()
37
+ .withTransactingDefaults()
38
+ .withCoinsAndBalancesDefaults()
39
+ .withTransactionHistoryDefaults()
40
+ .withKeysDefaults()
41
+ .withCoinSelectionDefaults();
42
+ }
43
+ withTransactionType() {
44
+ return new V1Builder({
45
+ ...this.#buildState,
46
+ transactingCapability: undefined,
47
+ transactionHistoryService: undefined,
48
+ });
49
+ }
50
+ withDefaultTransactionType() {
51
+ return this.withTransactionType();
52
+ }
53
+ withSyncDefaults() {
54
+ return this.withSync(makeEventsSyncService, makeEventsSyncCapability);
55
+ }
56
+ withSync(syncService, syncCapability) {
57
+ return new V1Builder({
58
+ ...this.#buildState,
59
+ syncService,
60
+ syncCapability,
61
+ });
62
+ }
63
+ withSerializationDefaults() {
64
+ return this.withSerialization(makeDefaultV1SerializationCapability);
65
+ }
66
+ withSerialization(serializationCapability) {
67
+ return new V1Builder({
68
+ ...this.#buildState,
69
+ serializationCapability,
70
+ });
71
+ }
72
+ withTransactingDefaults() {
73
+ return this.withTransacting(makeDefaultTransactingCapability);
74
+ }
75
+ withTransacting(transactingCapability) {
76
+ return new V1Builder({
77
+ ...this.#buildState,
78
+ transactingCapability,
79
+ });
80
+ }
81
+ withCoinSelection(coinSelection) {
82
+ return new V1Builder({
83
+ ...this.#buildState,
84
+ coinSelection,
85
+ });
86
+ }
87
+ withCoinSelectionDefaults() {
88
+ return this.withCoinSelection(() => chooseCoin);
89
+ }
90
+ withCoinsAndBalancesDefaults() {
91
+ return this.withCoinsAndBalances(makeDefaultCoinsAndBalancesCapability);
92
+ }
93
+ withCoinsAndBalances(coinsAndBalancesCapability) {
94
+ return new V1Builder({
95
+ ...this.#buildState,
96
+ coinsAndBalancesCapability,
97
+ });
98
+ }
99
+ withTransactionHistoryDefaults() {
100
+ return this.withTransactionHistory(makeDefaultTransactionHistoryService);
101
+ }
102
+ withTransactionHistory(transactionHistoryService) {
103
+ return new V1Builder({
104
+ ...this.#buildState,
105
+ transactionHistoryService,
106
+ });
107
+ }
108
+ withKeysDefaults() {
109
+ return this.withKeys(makeDefaultKeysCapability);
110
+ }
111
+ withKeys(keysCapability) {
112
+ return new V1Builder({
113
+ ...this.#buildState,
114
+ keysCapability,
115
+ });
116
+ }
117
+ build(configuration) {
118
+ const v1Context = this.#buildContextFromBuildState(configuration);
119
+ const { networkId } = configuration;
120
+ return {
121
+ __polyTag__: V1Tag,
122
+ coinsAndBalances: v1Context.coinsAndBalancesCapability,
123
+ keys: v1Context.keysCapability,
124
+ serialization: v1Context.serializationCapability,
125
+ transactionHistory: v1Context.transactionHistoryService,
126
+ start(context) {
127
+ return Effect.gen(function* () {
128
+ const scope = yield* Scope.Scope;
129
+ return new RunningV1Variant(scope, context, v1Context);
130
+ });
131
+ },
132
+ migrateState(_previousState) {
133
+ const seed = WalletSeed.fromString('0000000000000000000000000000000000000000000000000000000000000001');
134
+ return Effect.succeed(CoreWallet.empty(PublicKeys.fromSecretKeys(ledger.ZswapSecretKeys.fromSeed(seed)), networkId));
135
+ },
136
+ deserializeState: (serialized) => {
137
+ return v1Context.serializationCapability.deserialize(null, serialized);
138
+ },
139
+ };
140
+ }
141
+ #buildContextFromBuildState(configuration) {
142
+ if (!isBuildStateFull(this.#buildState)) {
143
+ throw new Error('Not all components are configured in V1 Builder');
144
+ }
145
+ const { syncCapability, syncService, transactingCapability, serializationCapability, coinSelection, coinsAndBalancesCapability, keysCapability, transactionHistoryService, } = this.#buildState;
146
+ const getContext = () => context;
147
+ const context = {
148
+ serializationCapability: serializationCapability(configuration, getContext),
149
+ syncCapability: syncCapability(configuration, getContext),
150
+ syncService: syncService(configuration, getContext),
151
+ transactingCapability: transactingCapability(configuration, getContext),
152
+ coinsAndBalancesCapability: coinsAndBalancesCapability(configuration, getContext),
153
+ keysCapability: keysCapability(configuration, getContext),
154
+ coinSelection: coinSelection(configuration, getContext),
155
+ transactionHistoryService: transactionHistoryService(configuration, getContext),
156
+ };
157
+ return context;
158
+ }
159
+ }
160
+ const isBuildStateFull = (buildState) => {
161
+ const allBuildStateKeys = [
162
+ 'syncService',
163
+ 'syncCapability',
164
+ 'transactingCapability',
165
+ 'coinSelection',
166
+ 'serializationCapability',
167
+ 'coinsAndBalancesCapability',
168
+ 'keysCapability',
169
+ 'transactionHistoryService',
170
+ ];
171
+ return allBuildStateKeys.every((key) => typeof buildState[key] == 'function');
172
+ };
@@ -0,0 +1,74 @@
1
+ import type * as ledger from '@midnight-ntwrk/ledger-v8';
2
+ import { type LedgerOps } from '@midnightntwrk/wallet-sdk-utilities';
3
+ export declare const WalletError: {
4
+ other(err: unknown): WalletError;
5
+ submission(err: unknown): WalletError;
6
+ };
7
+ export type WalletError = OtherWalletError | InsufficientFundsError | SubmissionError | AddressError | SyncWalletError | InvalidCoinHashesError | TransactingError | LedgerOps.LedgerError;
8
+ declare const OtherWalletError_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 & {
9
+ readonly _tag: "Wallet.Other";
10
+ } & Readonly<A>;
11
+ export declare class OtherWalletError extends OtherWalletError_base<{
12
+ message: string;
13
+ cause?: unknown;
14
+ }> {
15
+ }
16
+ declare const SyncWalletError_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 & {
17
+ readonly _tag: "Wallet.Sync";
18
+ } & Readonly<A>;
19
+ export declare class SyncWalletError extends SyncWalletError_base<{
20
+ message: string;
21
+ cause?: unknown;
22
+ }> {
23
+ }
24
+ declare const SubmissionError_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 & {
25
+ readonly _tag: "Wallet.SubmissionWalletError";
26
+ } & Readonly<A>;
27
+ export declare class SubmissionError extends SubmissionError_base<{
28
+ message: string;
29
+ cause?: unknown;
30
+ }> {
31
+ }
32
+ declare const InsufficientFundsError_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 & {
33
+ readonly _tag: "Wallet.InsufficientFunds";
34
+ } & Readonly<A>;
35
+ export declare class InsufficientFundsError extends InsufficientFundsError_base<{
36
+ message: string;
37
+ tokenType: ledger.RawTokenType;
38
+ amount: bigint;
39
+ }> {
40
+ }
41
+ declare const AddressError_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 & {
42
+ readonly _tag: "Wallet.Address";
43
+ } & Readonly<A>;
44
+ export declare class AddressError extends AddressError_base<{
45
+ message: string;
46
+ originalAddress: string;
47
+ cause?: unknown;
48
+ }> {
49
+ }
50
+ declare const InvalidCoinHashesError_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 & {
51
+ readonly _tag: "Wallet.InvalidCoinHashes";
52
+ } & Readonly<A>;
53
+ export declare class InvalidCoinHashesError extends InvalidCoinHashesError_base<{
54
+ message: string;
55
+ missingNonces: Set<ledger.Nonce>;
56
+ }> {
57
+ }
58
+ declare const TransactingError_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 & {
59
+ readonly _tag: "Wallet.Transacting";
60
+ } & Readonly<A>;
61
+ export declare class TransactingError extends TransactingError_base<{
62
+ message: string;
63
+ cause?: unknown;
64
+ }> {
65
+ }
66
+ declare const TransactionHistoryError_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 & {
67
+ readonly _tag: "Wallet.TransactionHistory";
68
+ } & Readonly<A>;
69
+ export declare class TransactionHistoryError extends TransactionHistoryError_base<{
70
+ message: string;
71
+ cause?: unknown;
72
+ }> {
73
+ }
74
+ export {};
@@ -0,0 +1,53 @@
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
+ import { Data } from 'effect';
14
+ export const WalletError = {
15
+ other(err) {
16
+ let message;
17
+ if (err) {
18
+ if (typeof err == 'object' && 'message' in err) {
19
+ message = String(err.message);
20
+ }
21
+ else if (typeof err == 'string') {
22
+ message = err;
23
+ }
24
+ else {
25
+ message = '';
26
+ }
27
+ }
28
+ else {
29
+ message = '';
30
+ }
31
+ return new OtherWalletError({ message: `Other wallet error: ${message}`, cause: err });
32
+ },
33
+ submission(err) {
34
+ const message = err && typeof err == 'object' && 'message' in err ? String(err.message) : '';
35
+ return new SubmissionError({ message: `Transaction submission error: ${message}`, cause: err });
36
+ },
37
+ };
38
+ export class OtherWalletError extends Data.TaggedError('Wallet.Other') {
39
+ }
40
+ export class SyncWalletError extends Data.TaggedError('Wallet.Sync') {
41
+ }
42
+ export class SubmissionError extends Data.TaggedError('Wallet.SubmissionWalletError') {
43
+ }
44
+ export class InsufficientFundsError extends Data.TaggedError('Wallet.InsufficientFunds') {
45
+ }
46
+ export class AddressError extends Data.TaggedError('Wallet.Address') {
47
+ }
48
+ export class InvalidCoinHashesError extends Data.TaggedError('Wallet.InvalidCoinHashes') {
49
+ }
50
+ export class TransactingError extends Data.TaggedError('Wallet.Transacting') {
51
+ }
52
+ export class TransactionHistoryError extends Data.TaggedError('Wallet.TransactionHistory') {
53
+ }
@@ -0,0 +1,12 @@
1
+ export * from './V1Builder.js';
2
+ export * as Sync from './Sync.js';
3
+ export * as Transacting from './Transacting.js';
4
+ export * as TransactionHistory from './TransactionHistory.js';
5
+ export * as Serialization from './Serialization.js';
6
+ export * as CoinsAndBalances from './CoinsAndBalances.js';
7
+ export * as Keys from './Keys.js';
8
+ export * from './RunningV1Variant.js';
9
+ export * as Simulator from '@midnightntwrk/wallet-sdk-capabilities/simulation';
10
+ export * as WalletError from './WalletError.js';
11
+ export * from './CoreWallet.js';
12
+ export * from './TransactionOps.js';
@@ -0,0 +1,24 @@
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
+ export * from './V1Builder.js';
14
+ export * as Sync from './Sync.js';
15
+ export * as Transacting from './Transacting.js';
16
+ export * as TransactionHistory from './TransactionHistory.js';
17
+ export * as Serialization from './Serialization.js';
18
+ export * as CoinsAndBalances from './CoinsAndBalances.js';
19
+ export * as Keys from './Keys.js';
20
+ export * from './RunningV1Variant.js';
21
+ export * as Simulator from '@midnightntwrk/wallet-sdk-capabilities/simulation';
22
+ export * as WalletError from './WalletError.js';
23
+ export * from './CoreWallet.js';
24
+ export * from './TransactionOps.js';
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@midnightntwrk/wallet-sdk-shielded",
3
+ "version": "3.0.2",
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://registry.npmjs.org/",
12
+ "access": "public"
13
+ },
14
+ "files": [
15
+ "dist/"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/midnightntwrk/midnight-wallet.git",
20
+ "directory": "packages/shielded-wallet"
21
+ },
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js"
26
+ },
27
+ "./v1": {
28
+ "types": "./dist/v1/index.d.ts",
29
+ "import": "./dist/v1/index.js"
30
+ }
31
+ },
32
+ "dependencies": {
33
+ "@midnight-ntwrk/ledger-v8": "^8.1.0",
34
+ "@midnightntwrk/wallet-sdk-abstractions": "^2.1.0",
35
+ "@midnightntwrk/wallet-sdk-address-format": "^3.1.2",
36
+ "@midnightntwrk/wallet-sdk-capabilities": "^3.3.1",
37
+ "@midnightntwrk/wallet-sdk-indexer-client": "^1.2.3",
38
+ "@midnightntwrk/wallet-sdk-runtime": "^1.0.5",
39
+ "@midnightntwrk/wallet-sdk-utilities": "^1.2.0",
40
+ "effect": "^3.19.19",
41
+ "rxjs": "^7.8.2"
42
+ },
43
+ "scripts": {
44
+ "typecheck": "tsc -b ./tsconfig.json --noEmit --force",
45
+ "test": "vitest run",
46
+ "lint": "eslint --max-warnings 0",
47
+ "format": "prettier --write \"**/*.{ts,js,json,yaml,yml,md}\"",
48
+ "format:check": "prettier --check \"**/*.{ts,js,json,yaml,yml,md}\"",
49
+ "dist": "tsc -b ./tsconfig.build.json",
50
+ "dist:publish": "tsc -b ./tsconfig.publish.json",
51
+ "clean": "rimraf --glob dist 'tsconfig.*.tsbuildinfo' && date +%s > .clean-timestamp",
52
+ "publint": "publint --strict"
53
+ }
54
+ }