@midnightntwrk/wallet-sdk-shielded 4.0.0-beta.2 → 4.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +11 -6
  2. package/dist/Restore.d.ts +40 -0
  3. package/dist/Restore.js +63 -0
  4. package/dist/ShieldedWallet.d.ts +248 -62
  5. package/dist/ShieldedWallet.js +366 -76
  6. package/dist/ShieldedWalletAPI.d.ts +179 -0
  7. package/dist/ShieldedWalletAPI.js +75 -0
  8. package/dist/SingleVariantShieldedWallet.d.ts +35 -0
  9. package/dist/SingleVariantShieldedWallet.js +240 -0
  10. package/dist/index.d.ts +4 -1
  11. package/dist/index.js +4 -1
  12. package/dist/v1/CoinsAndBalances.d.ts +1 -1
  13. package/dist/v1/CoreWallet.d.ts +48 -1
  14. package/dist/v1/CoreWallet.js +54 -1
  15. package/dist/v1/Migration.d.ts +95 -0
  16. package/dist/v1/Migration.js +82 -0
  17. package/dist/v1/RunningV1Variant.d.ts +1 -1
  18. package/dist/v1/RunningV1Variant.js +37 -8
  19. package/dist/v1/Serialization.js +1 -1
  20. package/dist/v1/Sync.d.ts +132 -9
  21. package/dist/v1/Sync.js +244 -35
  22. package/dist/v1/Transacting.d.ts +1 -1
  23. package/dist/v1/Transacting.js +1 -1
  24. package/dist/v1/TransactionHistory.d.ts +9 -2
  25. package/dist/v1/TransactionHistory.js +26 -5
  26. package/dist/v1/TransactionOps.d.ts +1 -1
  27. package/dist/v1/V1Builder.d.ts +87 -26
  28. package/dist/v1/V1Builder.js +109 -8
  29. package/dist/v1/WalletError.d.ts +1 -1
  30. package/dist/v1/index.d.ts +2 -1
  31. package/dist/v1/index.js +5 -1
  32. package/dist/v2/CoinsAndBalances.d.ts +24 -0
  33. package/dist/v2/CoinsAndBalances.js +41 -0
  34. package/dist/v2/CoreWallet.d.ts +162 -0
  35. package/dist/v2/CoreWallet.js +305 -0
  36. package/dist/v2/Keys.d.ts +8 -0
  37. package/dist/v2/Keys.js +16 -0
  38. package/dist/v2/Migration.d.ts +112 -0
  39. package/dist/v2/Migration.js +96 -0
  40. package/dist/v2/RunningV2Variant.d.ts +40 -0
  41. package/dist/v2/RunningV2Variant.js +127 -0
  42. package/dist/v2/Serialization.d.ts +12 -0
  43. package/dist/v2/Serialization.js +93 -0
  44. package/dist/v2/Sync.d.ts +208 -0
  45. package/dist/v2/Sync.js +436 -0
  46. package/dist/v2/Transacting.d.ts +50 -0
  47. package/dist/v2/Transacting.js +258 -0
  48. package/dist/v2/TransactionHistory.d.ts +100 -0
  49. package/dist/v2/TransactionHistory.js +132 -0
  50. package/dist/v2/TransactionImbalances.d.ts +10 -0
  51. package/dist/v2/TransactionImbalances.js +30 -0
  52. package/dist/v2/TransactionOps.d.ts +17 -0
  53. package/dist/v2/TransactionOps.js +73 -0
  54. package/dist/v2/V2Builder.d.ts +154 -0
  55. package/dist/v2/V2Builder.js +273 -0
  56. package/dist/v2/WalletError.d.ts +74 -0
  57. package/dist/v2/WalletError.js +53 -0
  58. package/dist/v2/index.d.ts +13 -0
  59. package/dist/v2/index.js +26 -0
  60. package/package.json +16 -8
@@ -0,0 +1,154 @@
1
+ import * as ledger from '@midnightntwrk/ledger-v9';
2
+ import { type Either, type Types } from 'effect';
3
+ import { type NetworkId } from '@midnightntwrk/wallet-sdk-abstractions';
4
+ import { type Variant, type VariantBuilder, type StartMaterial } from '@midnightntwrk/wallet-sdk-runtime/abstractions';
5
+ import { type StateMigration } from './Migration.js';
6
+ import { RunningV2Variant, V2Tag } from './RunningV2Variant.js';
7
+ import { type SerializationCapability } from './Serialization.js';
8
+ import { type DefaultSyncContext, type DefaultSyncConfiguration, type ChangesResult, type SyncCapability, type SyncService, type WalletSyncUpdate } from './Sync.js';
9
+ import { type DefaultTransactingConfiguration, type DefaultTransactingContext, type TransactingCapability } from './Transacting.js';
10
+ import { type WalletError } from './WalletError.js';
11
+ import { type CoinsAndBalancesCapability } from './CoinsAndBalances.js';
12
+ import { type KeysCapability } from './Keys.js';
13
+ import { type CoinSelection } from '@midnightntwrk/wallet-sdk-capabilities';
14
+ import { type CoreWallet } from './CoreWallet.js';
15
+ import { type DefaultTransactionHistoryConfiguration, type TransactionHistoryService } from './TransactionHistory.js';
16
+ export type BaseV2Configuration = {
17
+ networkId: NetworkId.NetworkId;
18
+ };
19
+ export type DefaultV2Configuration = BaseV2Configuration & DefaultSyncConfiguration & DefaultTransactingConfiguration & DefaultTransactionHistoryConfiguration;
20
+ declare const V2BuilderSymbol: {
21
+ readonly typeId: unique symbol;
22
+ };
23
+ export type V2Variant<TSerialized, TSyncUpdate, TTransaction, TAuxData, TPreviousState = null> = Variant.Variant<typeof V2Tag, CoreWallet, TPreviousState, RunningV2Variant<TSerialized, TSyncUpdate, TTransaction, TAuxData>> & {
24
+ deserializeState: (serialized: TSerialized) => Either.Either<CoreWallet, WalletError>;
25
+ coinsAndBalances: CoinsAndBalancesCapability<CoreWallet>;
26
+ keys: KeysCapability<CoreWallet>;
27
+ serialization: SerializationCapability<CoreWallet, null, TSerialized>;
28
+ transactionHistory: TransactionHistoryService;
29
+ startAux: StartMaterial.StartAuxCapability<TAuxData>;
30
+ };
31
+ export type AnyV2Variant = V2Variant<any, any, any, any>;
32
+ export type DefaultV2Variant = V2Variant<string, WalletSyncUpdate, ledger.FinalizedTransaction, ledger.ZswapSecretKeys>;
33
+ export type TransactionOf<T extends AnyV2Variant> = T extends V2Variant<any, any, infer TTransaction, any> ? TTransaction : never;
34
+ export type AuxDataOf<T extends AnyV2Variant> = T extends V2Variant<any, any, any, infer TAuxData> ? TAuxData : never;
35
+ export type SerializedStateOf<T extends AnyV2Variant> = T extends V2Variant<infer TSerialized, any, any, any> ? TSerialized : never;
36
+ export type DefaultV2Builder<TPreviousState = null> = V2Builder<DefaultV2Configuration, RunningV2Variant.Context<string, WalletSyncUpdate, ledger.FinalizedTransaction, ledger.ZswapSecretKeys>, string, WalletSyncUpdate, ledger.FinalizedTransaction, ledger.ZswapSecretKeys, TPreviousState>;
37
+ export declare class V2Builder<TConfig extends BaseV2Configuration = BaseV2Configuration, TContext extends Partial<RunningV2Variant.AnyContext> = object, TSerialized = never, TSyncUpdate = never, TTransaction = never, TStartAux extends object = object, TPreviousState = null> implements VariantBuilder.VariantBuilder<V2Variant<TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>, TConfig> {
38
+ #private;
39
+ constructor(buildState?: V2Builder.PartialBuildState<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>);
40
+ withDefaults(this: V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, null>): DefaultV2Builder;
41
+ withTransactionType<Transaction>(): V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, Transaction, TStartAux, TPreviousState>;
42
+ withDefaultTransactionType(): V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, ledger.FinalizedTransaction, TStartAux, TPreviousState>;
43
+ withSyncDefaults(): V2Builder<TConfig & DefaultSyncConfiguration, TContext & DefaultSyncContext, TSerialized, WalletSyncUpdate, TTransaction, ledger.ZswapSecretKeys, TPreviousState>;
44
+ withSync<TSyncConfig, TSyncContext extends Partial<RunningV2Variant.AnyContext>, TSyncUpdate, TStartAux extends object>(syncService: (configuration: TSyncConfig, getContext: () => TSyncContext) => SyncService<CoreWallet, TStartAux, TSyncUpdate>, syncCapability: (configuration: TSyncConfig, getContext: () => TSyncContext) => SyncCapability<CoreWallet, TSyncUpdate, ChangesResult>): V2Builder<TConfig & TSyncConfig, TContext & TSyncContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
45
+ withSerializationDefaults(): V2Builder<TConfig, TContext, string, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
46
+ withSerialization<TSerializationConfig, TSerializationContext extends Partial<RunningV2Variant.AnyContext>, TSerialized>(serializationCapability: (configuration: TSerializationConfig, getContext: () => TSerializationContext) => SerializationCapability<CoreWallet, null, TSerialized>): V2Builder<TConfig & TSerializationConfig, TContext & TSerializationContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
47
+ withTransactingDefaults(this: V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, ledger.FinalizedTransaction, TStartAux, TPreviousState>): V2Builder<TConfig & DefaultTransactingConfiguration, TContext & DefaultTransactingContext, TSerialized, TSyncUpdate, ledger.FinalizedTransaction, TStartAux, TPreviousState>;
48
+ withTransacting<TTransactingConfig, TTransactingContext extends Partial<RunningV2Variant.AnyContext>>(transactingCapability: (config: TTransactingConfig, getContext: () => TTransactingContext) => TransactingCapability<ledger.ZswapSecretKeys, CoreWallet, TTransaction>): V2Builder<TConfig & TTransactingConfig, TContext & TTransactingContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
49
+ withCoinSelection<TCoinSelectionConfig, TCoinSelectionContext extends Partial<RunningV2Variant.AnyContext>>(coinSelection: (config: TCoinSelectionConfig, getContext: () => TCoinSelectionContext) => CoinSelection<ledger.QualifiedShieldedCoinInfo>): V2Builder<TConfig & TCoinSelectionConfig, TContext & TCoinSelectionContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
50
+ withCoinSelectionDefaults(): V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
51
+ withCoinsAndBalancesDefaults(): V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
52
+ withCoinsAndBalances<TBalancesConfig, TBalancesContext extends Partial<RunningV2Variant.AnyContext>>(coinsAndBalancesCapability: (configuration: TBalancesConfig, getContext: () => TBalancesContext) => CoinsAndBalancesCapability<CoreWallet>): V2Builder<TConfig & TBalancesConfig, TContext & TBalancesContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
53
+ withTransactionHistoryDefaults(this: V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, ledger.FinalizedTransaction, TStartAux, TPreviousState>): V2Builder<TConfig & DefaultTransactionHistoryConfiguration, TContext, TSerialized, TSyncUpdate, ledger.FinalizedTransaction, TStartAux, TPreviousState>;
54
+ withTransactionHistory<TTransactionHistoryConfig, TTransactionHistoryContext extends Partial<RunningV2Variant.AnyContext>>(transactionHistoryService: (configuration: TTransactionHistoryConfig, getContext: () => TTransactionHistoryContext) => TransactionHistoryService): V2Builder<TConfig & TTransactionHistoryConfig, TContext & TTransactionHistoryContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
55
+ withKeysDefaults(): V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
56
+ /**
57
+ * Uses the default start-aux derivation: this ledger version's Zswap secret keys, derived from the seed.
58
+ *
59
+ * @remarks
60
+ * The seed is the only key material that crosses a protocol boundary, so every variant must be able to produce its
61
+ * own from one. This is that derivation for this ledger version; a builder whose sync service expects something
62
+ * else supplies its own with {@link V2Builder.withStartAux}.
63
+ */
64
+ withStartAuxDefaults(this: V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, ledger.ZswapSecretKeys, TPreviousState>): V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, ledger.ZswapSecretKeys, TPreviousState>;
65
+ /**
66
+ * Chooses how this variant derives the key material its synchronization is started with from a seed.
67
+ *
68
+ * @param startAux The derivation.
69
+ * @returns A builder that produces a variant able to start sync from a seed alone.
70
+ */
71
+ withStartAux(startAux: StartMaterial.StartAuxCapability<TStartAux>): V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
72
+ /**
73
+ * Uses the default migration: an empty wallet built from nothing.
74
+ *
75
+ * @remarks
76
+ * This is what an unconfigured builder already does, so calling it changes no behaviour — it states the choice rather
77
+ * than inheriting it. Registering this variant as the target of a real fork means calling
78
+ * {@link V2Builder.withMigration} with {@link makeCrossLedgerMigration} instead.
79
+ */
80
+ withMigrationDefaults(this: V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, null>): V2Builder<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, null>;
81
+ /**
82
+ * Chooses how this variant produces its first state from the state that preceded it.
83
+ *
84
+ * @remarks
85
+ * The strategy's input type becomes the variant's `TPreviousState`, which is what the runtime type-checks a
86
+ * registration against: a variant declaring it migrates from a ledger-v8 wallet cannot be registered after one that
87
+ * produces something else.
88
+ * @example
89
+ * ```typescript
90
+ * const forkTarget = new V2Builder().withDefaults().withMigration(makeCrossLedgerMigration);
91
+ * ```;
92
+ *
93
+ * @param migration Builds the migration from the configuration and the sibling capabilities.
94
+ */
95
+ withMigration<TMigrationConfig, TPrevious>(migration: (configuration: TMigrationConfig, getContext: () => TContext) => StateMigration<TPrevious>): V2Builder<TConfig & TMigrationConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPrevious>;
96
+ withKeys<TKeysConfig, TKeysContext extends Partial<RunningV2Variant.AnyContext>>(keysCapability: (configuration: TKeysConfig, getContext: () => TKeysContext) => KeysCapability<CoreWallet>): V2Builder<TConfig & TKeysConfig, TContext & TKeysContext, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
97
+ build(this: V2Builder<TConfig, RunningV2Variant.Context<TSerialized, TSyncUpdate, TTransaction, TStartAux>, TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>, configuration: TConfig): V2Variant<TSerialized, TSyncUpdate, TTransaction, TStartAux, TPreviousState>;
98
+ }
99
+ /** @internal */
100
+ declare namespace V2Builder {
101
+ type HasSync<TConfig, TContext, TSyncUpdate, TStartAux> = {
102
+ readonly syncService: (configuration: TConfig, getContext: () => TContext) => SyncService<CoreWallet, TStartAux, TSyncUpdate>;
103
+ readonly syncCapability: (configuration: TConfig, getContext: () => TContext) => SyncCapability<CoreWallet, TSyncUpdate, ChangesResult>;
104
+ };
105
+ type HasTransacting<TConfig, TContext, TTransaction> = {
106
+ readonly transactingCapability: (configuration: TConfig, getContext: () => TContext) => TransactingCapability<ledger.ZswapSecretKeys, CoreWallet, TTransaction>;
107
+ };
108
+ type HasCoinSelection<TConfig, TContext> = {
109
+ readonly coinSelection: (configuration: TConfig, getContext: () => TContext) => CoinSelection<ledger.QualifiedShieldedCoinInfo>;
110
+ };
111
+ type HasSerialization<TConfig, TContext, TSerialized> = {
112
+ readonly serializationCapability: (configuration: TConfig, getContext: () => TContext) => SerializationCapability<CoreWallet, null, TSerialized>;
113
+ };
114
+ type HasCoinsAndBalances<TConfig, TContext> = {
115
+ readonly coinsAndBalancesCapability: (configuration: TConfig, getContext: () => TContext) => CoinsAndBalancesCapability<CoreWallet>;
116
+ };
117
+ type HasTransactionHistory<TConfig, TContext> = {
118
+ readonly transactionHistoryService: (configuration: TConfig, getContext: () => TContext) => TransactionHistoryService;
119
+ };
120
+ type HasKeys<TConfig, TContext> = {
121
+ readonly keysCapability: (configuration: TConfig, getContext: () => TContext) => KeysCapability<CoreWallet>;
122
+ };
123
+ /** The internal build state of {@link V2Builder}. */
124
+ type FullBuildState<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux> = Types.Simplify<HasSync<TConfig, TContext, TSyncUpdate, TStartAux> & HasSerialization<TConfig, TContext, TSerialized> & HasTransacting<TConfig, TContext, TTransaction> & HasCoinSelection<TConfig, TContext> & HasCoinsAndBalances<TConfig, TContext> & HasKeys<TConfig, TContext> & HasTransactionHistory<TConfig, TContext>>;
125
+ /**
126
+ * The migration entry, kept out of {@link FullBuildState} on purpose: absent means "empty wallet", which is the
127
+ * behaviour every builder had before migrations were configurable, so a builder that never mentions migration must
128
+ * still be considered complete.
129
+ */
130
+ /**
131
+ * The start-aux entry, kept out of {@link FullBuildState} because it is not one of the context-producing capabilities:
132
+ * it takes neither configuration nor sibling capabilities, only a seed.
133
+ */
134
+ type HasStartAux<TStartAux> = {
135
+ readonly startAux: StartMaterial.StartAuxCapability<TStartAux>;
136
+ };
137
+ type HasMigration<TConfig, TContext, TPreviousState> = {
138
+ readonly migration: (configuration: TConfig, getContext: () => TContext) => StateMigration<TPreviousState>;
139
+ };
140
+ /** The capability half of the build state — everything that does not depend on the previous-state parameter. */
141
+ type CapabilityBuildState<TConfig = object, TContext = object, TSerialized = never, TSyncUpdate = never, TTransaction = never, TStartAux = object> = {
142
+ [K in keyof FullBuildState<never, never, never, never, never, never>]?: FullBuildState<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux>[K] | undefined;
143
+ };
144
+ type PartialBuildState<TConfig = object, TContext = object, TSerialized = never, TSyncUpdate = never, TTransaction = never, TStartAux = object, TPreviousState = null> = {
145
+ [K in keyof (FullBuildState<never, never, never, never, never, never> & HasMigration<never, never, never> & HasStartAux<never>)]?: (FullBuildState<TConfig, TContext, TSerialized, TSyncUpdate, TTransaction, TStartAux> & HasMigration<TConfig, TContext, TPreviousState> & HasStartAux<TStartAux>)[K] | undefined;
146
+ };
147
+ /** Utility interface that manages the type variance of {@link V2Builder}. */
148
+ interface Variance<R> {
149
+ readonly [V2BuilderSymbol.typeId]: {
150
+ readonly _R: Types.Covariant<R>;
151
+ };
152
+ }
153
+ }
154
+ export {};
@@ -0,0 +1,273 @@
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 '@midnightntwrk/ledger-v9';
14
+ import { Effect, Scope } from 'effect';
15
+ import { WalletRuntimeError, } from '@midnightntwrk/wallet-sdk-runtime/abstractions';
16
+ import { makeEmptyWalletMigration } from './Migration.js';
17
+ import { RunningV2Variant, V2Tag } from './RunningV2Variant.js';
18
+ import { makeDefaultV2SerializationCapability } from './Serialization.js';
19
+ import { makeEventsSyncService, makeEventsSyncCapability, } from './Sync.js';
20
+ import { makeDefaultTransactingCapability, } from './Transacting.js';
21
+ import { makeDefaultCoinsAndBalancesCapability } from './CoinsAndBalances.js';
22
+ import { makeDefaultKeysCapability } from './Keys.js';
23
+ import { chooseCoin } from '@midnightntwrk/wallet-sdk-capabilities';
24
+ import { makeDefaultTransactionHistoryService, } from './TransactionHistory.js';
25
+ const V2BuilderSymbol = {
26
+ typeId: Symbol('@midnight-ntwrk/wallet#V2Builder'),
27
+ };
28
+ export class V2Builder {
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
+ .withStartAuxDefaults()
42
+ // Type cast required because: the chain accumulates `TContext` as an intersection of the individual
43
+ // `Default*Context` fragments, which is a structurally weaker type than the complete
44
+ // `RunningV2Variant.Context` that `DefaultV2Builder` names — the defaults do produce a full context, but the
45
+ // chain's type cannot express that. Routing through `unknown` because the added `migration` key put the two
46
+ // sides past what TypeScript will accept as directly comparable; the widening itself predates it.
47
+ .withCoinSelectionDefaults();
48
+ }
49
+ withTransactionType() {
50
+ return new V2Builder({
51
+ ...this.#buildState,
52
+ transactingCapability: undefined,
53
+ transactionHistoryService: undefined,
54
+ });
55
+ }
56
+ withDefaultTransactionType() {
57
+ return this.withTransactionType();
58
+ }
59
+ withSyncDefaults() {
60
+ return this.withSync(makeEventsSyncService, makeEventsSyncCapability);
61
+ }
62
+ withSync(syncService, syncCapability) {
63
+ return new V2Builder({
64
+ ...this.#buildState,
65
+ syncService,
66
+ syncCapability,
67
+ // The derivation is typed against the *old* start-aux parameter, so it cannot ride along into a builder whose
68
+ // sync service expects something else — exactly as the migration cannot ride along through `withMigration`.
69
+ startAux: undefined,
70
+ });
71
+ }
72
+ withSerializationDefaults() {
73
+ return this.withSerialization(makeDefaultV2SerializationCapability);
74
+ }
75
+ withSerialization(serializationCapability) {
76
+ return new V2Builder({
77
+ ...this.#buildState,
78
+ serializationCapability,
79
+ });
80
+ }
81
+ withTransactingDefaults() {
82
+ return this.withTransacting(makeDefaultTransactingCapability);
83
+ }
84
+ withTransacting(transactingCapability) {
85
+ return new V2Builder({
86
+ ...this.#buildState,
87
+ transactingCapability,
88
+ });
89
+ }
90
+ withCoinSelection(coinSelection) {
91
+ return new V2Builder({
92
+ ...this.#buildState,
93
+ coinSelection,
94
+ });
95
+ }
96
+ withCoinSelectionDefaults() {
97
+ return this.withCoinSelection(() => chooseCoin);
98
+ }
99
+ withCoinsAndBalancesDefaults() {
100
+ return this.withCoinsAndBalances(makeDefaultCoinsAndBalancesCapability);
101
+ }
102
+ withCoinsAndBalances(coinsAndBalancesCapability) {
103
+ return new V2Builder({
104
+ ...this.#buildState,
105
+ coinsAndBalancesCapability,
106
+ });
107
+ }
108
+ withTransactionHistoryDefaults() {
109
+ return this.withTransactionHistory(makeDefaultTransactionHistoryService);
110
+ }
111
+ withTransactionHistory(transactionHistoryService) {
112
+ return new V2Builder({
113
+ ...this.#buildState,
114
+ transactionHistoryService,
115
+ });
116
+ }
117
+ withKeysDefaults() {
118
+ return this.withKeys(makeDefaultKeysCapability);
119
+ }
120
+ /**
121
+ * Uses the default start-aux derivation: this ledger version's Zswap secret keys, derived from the seed.
122
+ *
123
+ * @remarks
124
+ * The seed is the only key material that crosses a protocol boundary, so every variant must be able to produce its
125
+ * own from one. This is that derivation for this ledger version; a builder whose sync service expects something
126
+ * else supplies its own with {@link V2Builder.withStartAux}.
127
+ */
128
+ withStartAuxDefaults() {
129
+ return this.withStartAux({ fromSeed: (seed) => ledger.ZswapSecretKeys.fromSeed(seed) });
130
+ }
131
+ /**
132
+ * Chooses how this variant derives the key material its synchronization is started with from a seed.
133
+ *
134
+ * @param startAux The derivation.
135
+ * @returns A builder that produces a variant able to start sync from a seed alone.
136
+ */
137
+ withStartAux(startAux) {
138
+ return new V2Builder({
139
+ ...this.#buildState,
140
+ startAux,
141
+ });
142
+ }
143
+ /**
144
+ * Uses the default migration: an empty wallet built from nothing.
145
+ *
146
+ * @remarks
147
+ * This is what an unconfigured builder already does, so calling it changes no behaviour — it states the choice rather
148
+ * than inheriting it. Registering this variant as the target of a real fork means calling
149
+ * {@link V2Builder.withMigration} with {@link makeCrossLedgerMigration} instead.
150
+ */
151
+ withMigrationDefaults() {
152
+ return this.withMigration((configuration) => makeEmptyWalletMigration(configuration));
153
+ }
154
+ /**
155
+ * Chooses how this variant produces its first state from the state that preceded it.
156
+ *
157
+ * @remarks
158
+ * The strategy's input type becomes the variant's `TPreviousState`, which is what the runtime type-checks a
159
+ * registration against: a variant declaring it migrates from a ledger-v8 wallet cannot be registered after one that
160
+ * produces something else.
161
+ * @example
162
+ * ```typescript
163
+ * const forkTarget = new V2Builder().withDefaults().withMigration(makeCrossLedgerMigration);
164
+ * ```;
165
+ *
166
+ * @param migration Builds the migration from the configuration and the sibling capabilities.
167
+ */
168
+ withMigration(migration) {
169
+ const capabilities = this.#buildState;
170
+ return new V2Builder({
171
+ // Only the capability half crosses: the strategy being replaced is typed against the *old* previous-state
172
+ // parameter, so it cannot ride along even though it is about to be overwritten.
173
+ ...capabilities,
174
+ migration,
175
+ });
176
+ }
177
+ withKeys(keysCapability) {
178
+ return new V2Builder({
179
+ ...this.#buildState,
180
+ keysCapability,
181
+ });
182
+ }
183
+ build(configuration) {
184
+ const v2Context = this.#buildContextFromBuildState(configuration);
185
+ const migration = this.#resolveMigration(configuration, () => v2Context);
186
+ return {
187
+ __polyTag__: V2Tag,
188
+ coinsAndBalances: v2Context.coinsAndBalancesCapability,
189
+ keys: v2Context.keysCapability,
190
+ serialization: v2Context.serializationCapability,
191
+ transactionHistory: v2Context.transactionHistoryService,
192
+ startAux: this.#resolveStartAux(),
193
+ start(context) {
194
+ return Effect.gen(function* () {
195
+ const scope = yield* Scope.Scope;
196
+ return new RunningV2Variant(scope, context, v2Context);
197
+ });
198
+ },
199
+ // An unconfigured builder keeps producing an empty wallet, which is what `startEmpty` has always relied on.
200
+ // Anything else — carrying state within a ledger version, or projecting it across one — is the injected
201
+ // strategy's business, and its failures are reported on the runtime's state stream rather than thrown.
202
+ migrateState: (previousState) => migration.migrate(previousState).pipe(Effect.mapError((error) => new WalletRuntimeError({
203
+ message: 'Failed to migrate shielded wallet state into this variant',
204
+ cause: error,
205
+ }))),
206
+ deserializeState: (serialized) => {
207
+ return v2Context.serializationCapability.deserialize(null, serialized);
208
+ },
209
+ };
210
+ }
211
+ /**
212
+ * Resolves the configured migration, or the empty-wallet fallback when none was configured.
213
+ *
214
+ * @remarks
215
+ * The fallback is written as a nullary function on purpose. It ignores whatever preceded it — it builds an empty
216
+ * wallet from nothing — which makes it a valid migration from _any_ previous state, but a `StateMigration<null>`
217
+ * value could only be widened to `StateMigration<TPreviousState>` through a cast. Declaring no parameter says the
218
+ * same thing to the type system without one.
219
+ */
220
+ /**
221
+ * Resolves the configured start-aux derivation.
222
+ *
223
+ * @remarks
224
+ * Unlike the migration there is no sensible fallback: the default derivation produces this ledger version's secret
225
+ * keys, which is only correct for a builder that kept a sync service expecting them. A builder that replaced sync
226
+ * and did not say how to derive its own key material is misconfigured, and is told so at build time rather than
227
+ * silently handed key objects of the wrong shape at the first migration.
228
+ */
229
+ #resolveStartAux() {
230
+ const configured = this.#buildState.startAux;
231
+ if (configured === undefined) {
232
+ throw new Error('Not all components are configured in V2 Builder: startAux');
233
+ }
234
+ return configured;
235
+ }
236
+ #resolveMigration(configuration, getContext) {
237
+ const configured = this.#buildState.migration;
238
+ return configured === undefined
239
+ ? { migrate: () => makeEmptyWalletMigration(configuration).migrate(null) }
240
+ : configured(configuration, getContext);
241
+ }
242
+ #buildContextFromBuildState(configuration) {
243
+ if (!isBuildStateFull(this.#buildState)) {
244
+ throw new Error('Not all components are configured in V2 Builder');
245
+ }
246
+ const { syncCapability, syncService, transactingCapability, serializationCapability, coinSelection, coinsAndBalancesCapability, keysCapability, transactionHistoryService, } = this.#buildState;
247
+ const getContext = () => context;
248
+ const context = {
249
+ serializationCapability: serializationCapability(configuration, getContext),
250
+ syncCapability: syncCapability(configuration, getContext),
251
+ syncService: syncService(configuration, getContext),
252
+ transactingCapability: transactingCapability(configuration, getContext),
253
+ coinsAndBalancesCapability: coinsAndBalancesCapability(configuration, getContext),
254
+ keysCapability: keysCapability(configuration, getContext),
255
+ coinSelection: coinSelection(configuration, getContext),
256
+ transactionHistoryService: transactionHistoryService(configuration, getContext),
257
+ };
258
+ return context;
259
+ }
260
+ }
261
+ const isBuildStateFull = (buildState) => {
262
+ const allBuildStateKeys = [
263
+ 'syncService',
264
+ 'syncCapability',
265
+ 'transactingCapability',
266
+ 'coinSelection',
267
+ 'serializationCapability',
268
+ 'coinsAndBalancesCapability',
269
+ 'keysCapability',
270
+ 'transactionHistoryService',
271
+ ];
272
+ return allBuildStateKeys.every((key) => typeof buildState[key] == 'function');
273
+ };
@@ -0,0 +1,74 @@
1
+ import type * as ledger from '@midnightntwrk/ledger-v9';
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,13 @@
1
+ export * from './V2Builder.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 Migration from './Migration.js';
7
+ export * as CoinsAndBalances from './CoinsAndBalances.js';
8
+ export * as Keys from './Keys.js';
9
+ export * from './RunningV2Variant.js';
10
+ export * as Simulator from '@midnightntwrk/wallet-sdk-capabilities/simulation';
11
+ export * as WalletError from './WalletError.js';
12
+ export * from './CoreWallet.js';
13
+ export * from './TransactionOps.js';
@@ -0,0 +1,26 @@
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
+ // The V2 variant: this wallet on `@midnightntwrk/ledger-v9`, run from `forks.v9`. Its twin is `../v1`.
14
+ export * from './V2Builder.js';
15
+ export * as Sync from './Sync.js';
16
+ export * as Transacting from './Transacting.js';
17
+ export * as TransactionHistory from './TransactionHistory.js';
18
+ export * as Serialization from './Serialization.js';
19
+ export * as Migration from './Migration.js';
20
+ export * as CoinsAndBalances from './CoinsAndBalances.js';
21
+ export * as Keys from './Keys.js';
22
+ export * from './RunningV2Variant.js';
23
+ export * as Simulator from '@midnightntwrk/wallet-sdk-capabilities/simulation';
24
+ export * as WalletError from './WalletError.js';
25
+ export * from './CoreWallet.js';
26
+ export * from './TransactionOps.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midnightntwrk/wallet-sdk-shielded",
3
- "version": "4.0.0-beta.2",
3
+ "version": "4.0.0-beta.3",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -27,19 +27,27 @@
27
27
  "./v1": {
28
28
  "types": "./dist/v1/index.d.ts",
29
29
  "import": "./dist/v1/index.js"
30
+ },
31
+ "./v2": {
32
+ "types": "./dist/v2/index.d.ts",
33
+ "import": "./dist/v2/index.js"
30
34
  }
31
35
  },
32
36
  "dependencies": {
33
- "@midnightntwrk/ledger-v9": "1.0.0-rc.3",
34
- "@midnightntwrk/wallet-sdk-abstractions": "3.0.0-beta.0",
35
- "@midnightntwrk/wallet-sdk-address-format": "4.0.0-beta.2",
36
- "@midnightntwrk/wallet-sdk-capabilities": "4.0.0-beta.2",
37
- "@midnightntwrk/wallet-sdk-indexer-client": "1.3.0-beta.1",
38
- "@midnightntwrk/wallet-sdk-runtime": "1.0.6-beta.0",
39
- "@midnightntwrk/wallet-sdk-utilities": "1.2.0",
37
+ "@midnight-ntwrk/ledger-v8": "^8.1.0",
38
+ "@midnightntwrk/ledger-v9": "1.0.0-rc.4",
39
+ "@midnightntwrk/wallet-sdk-abstractions": "3.0.0-beta.1",
40
+ "@midnightntwrk/wallet-sdk-address-format": "4.0.0-beta.3",
41
+ "@midnightntwrk/wallet-sdk-capabilities": "4.0.0-beta.3",
42
+ "@midnightntwrk/wallet-sdk-indexer-client": "2.0.0-beta.2",
43
+ "@midnightntwrk/wallet-sdk-runtime": "1.1.0-beta.1",
44
+ "@midnightntwrk/wallet-sdk-utilities": "1.2.2-beta.0",
40
45
  "effect": "^3.19.19",
41
46
  "rxjs": "^7.8.2"
42
47
  },
48
+ "devDependencies": {
49
+ "@midnightntwrk/wallet-sdk-state-translation": "workspace:^"
50
+ },
43
51
  "scripts": {
44
52
  "typecheck": "tsc -b ./tsconfig.json --noEmit --force",
45
53
  "test": "vitest run",