@midnight-ntwrk/wallet-sdk-shielded 1.0.0-beta.14 → 1.0.0-beta.16

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.
@@ -2,7 +2,7 @@ import { ProtocolState, ProtocolVersion } from '@midnight-ntwrk/wallet-sdk-abstr
2
2
  import { BaseV1Configuration, DefaultV1Configuration, V1Variant, CoreWallet } from './v1/index.js';
3
3
  import * as ledger from '@midnight-ntwrk/ledger-v7';
4
4
  import * as rx from 'rxjs';
5
- import { ProvingRecipe } from './v1/ProvingRecipe.js';
5
+ import { BalancingResult } from './v1/Transacting.js';
6
6
  import { SerializationCapability } from './v1/Serialization.js';
7
7
  import { ProgressUpdate, TransactionHistoryCapability } from './v1/TransactionHistory.js';
8
8
  import { AvailableCoin, CoinsAndBalancesCapability, PendingCoin } from './v1/CoinsAndBalances.js';
@@ -18,6 +18,7 @@ export type ShieldedWalletCapabilities<TSerialized = string, TTransaction = ledg
18
18
  keys: KeysCapability<CoreWallet>;
19
19
  transactionHistory: TransactionHistoryCapability<CoreWallet, TTransaction>;
20
20
  };
21
+ export type UnboundTransaction = ledger.Transaction<ledger.SignatureEnabled, ledger.Proof, ledger.PreBinding>;
21
22
  export declare class ShieldedWalletState<TSerialized = string, TTransaction = ledger.FinalizedTransaction> {
22
23
  static readonly mapState: <TSerialized_1 = string, TTransaction_1 = ledger.FinalizedTransaction>(capabilities: ShieldedWalletCapabilities<TSerialized_1, TTransaction_1>) => (state: ProtocolState.ProtocolState<CoreWallet>) => ShieldedWalletState<TSerialized_1, TTransaction_1>;
23
24
  readonly protocolVersion: ProtocolVersion.ProtocolVersion;
@@ -49,10 +50,10 @@ export interface CustomizedShieldedWallet<TStartAux = ledger.ZswapSecretKeys, TT
49
50
  ]> {
50
51
  readonly state: rx.Observable<ShieldedWalletState<TSerialized, TTransaction>>;
51
52
  start(secretKeys: TStartAux): Promise<void>;
52
- balanceTransaction(secretKeys: ledger.ZswapSecretKeys, tx: ledger.Transaction<ledger.Signaturish, ledger.Proofish, ledger.Bindingish>, newCoins: readonly ledger.ShieldedCoinInfo[]): Promise<ProvingRecipe<TTransaction>>;
53
- transferTransaction(secretKeys: ledger.ZswapSecretKeys, outputs: readonly TokenTransfer[]): Promise<ProvingRecipe<TTransaction>>;
54
- initSwap(secretKeys: ledger.ZswapSecretKeys, desiredInputs: Record<ledger.RawTokenType, bigint>, desiredOutputs: readonly TokenTransfer[]): Promise<ProvingRecipe<TTransaction>>;
55
- finalizeTransaction(recipe: ProvingRecipe<TTransaction>): Promise<TTransaction>;
53
+ balanceTransaction(secretKeys: ledger.ZswapSecretKeys, tx: ledger.Transaction<ledger.Signaturish, ledger.Proofish, ledger.Bindingish>): Promise<BalancingResult>;
54
+ transferTransaction(secretKeys: ledger.ZswapSecretKeys, outputs: readonly TokenTransfer[]): Promise<ledger.UnprovenTransaction>;
55
+ initSwap(secretKeys: ledger.ZswapSecretKeys, desiredInputs: Record<ledger.RawTokenType, bigint>, desiredOutputs: readonly TokenTransfer[]): Promise<ledger.UnprovenTransaction>;
56
+ finalizeTransaction(transaction: ledger.UnprovenTransaction): Promise<TTransaction>;
56
57
  readonly submitTransaction: SubmitTransactionMethod<TTransaction>;
57
58
  serializeState(): Promise<TSerialized>;
58
59
  waitForSyncedState(allowedGap?: bigint): Promise<ShieldedWalletState<TSerialized, TTransaction>>;
@@ -107,10 +107,10 @@ export function CustomShieldedWallet(configuration, builder) {
107
107
  .dispatch({ [V1Tag]: (v1) => v1.initSwap(secretKeys, desiredInputs, desiredOutputs) })
108
108
  .pipe(Effect.runPromise);
109
109
  }
110
- finalizeTransaction(recipe) {
110
+ finalizeTransaction(transaction) {
111
111
  return this.runtime
112
112
  .dispatch({
113
- [V1Tag]: (v1) => v1.finalizeTransaction(recipe),
113
+ [V1Tag]: (v1) => v1.finalizeTransaction(transaction),
114
114
  })
115
115
  .pipe(Effect.runPromise);
116
116
  }
@@ -1,9 +1,8 @@
1
1
  import * as ledger from '@midnight-ntwrk/ledger-v7';
2
2
  import { Effect } from 'effect';
3
- import { ProvingRecipe } from './ProvingRecipe.js';
4
3
  import { WalletError } from './WalletError.js';
5
4
  export interface ProvingService<TTransaction> {
6
- prove(recipe: ProvingRecipe<TTransaction>): Effect.Effect<TTransaction, WalletError>;
5
+ prove(transaction: ledger.UnprovenTransaction): Effect.Effect<TTransaction, WalletError>;
7
6
  }
8
7
  export type DefaultProvingConfiguration = {
9
8
  provingServerUrl: URL;
@@ -19,35 +19,18 @@ export const makeDefaultProvingService = (configuration) => {
19
19
  url: configuration.provingServerUrl,
20
20
  });
21
21
  return {
22
- prove(recipe) {
23
- switch (recipe.type) {
24
- case 'BalanceTransactionToProve':
25
- return pipe(ProverClient.ProverClient, Effect.flatMap((client) => client.proveTransaction(recipe.transactionToProve, ledger.CostModel.initialCostModel())), Effect.map((provenTx) => recipe.transactionToBalance.merge(provenTx.bind())), Effect.provide(clientLayer), Effect.catchAll((error) => Effect.fail(new ProvingError({
26
- message: error.message,
27
- cause: error,
28
- }))));
29
- case 'TransactionToProve':
30
- return pipe(ProverClient.ProverClient, Effect.flatMap((client) => client.proveTransaction(recipe.transaction, ledger.CostModel.initialCostModel())), Effect.map((proven) => proven.bind()), Effect.provide(clientLayer), Effect.catchAll((error) => Effect.fail(new ProvingError({
31
- message: error.message,
32
- cause: error,
33
- }))));
34
- case 'NothingToProve':
35
- return Effect.succeed(recipe.transaction);
36
- }
22
+ prove(transaction) {
23
+ return pipe(ProverClient.ProverClient, Effect.flatMap((client) => client.proveTransaction(transaction, ledger.CostModel.initialCostModel())), Effect.map((transaction) => transaction.bind()), Effect.provide(clientLayer), Effect.catchAll((error) => Effect.fail(new ProvingError({
24
+ message: error.message,
25
+ cause: error,
26
+ }))));
37
27
  },
38
28
  };
39
29
  };
40
30
  export const makeSimulatorProvingService = () => {
41
31
  return {
42
- prove(recipe) {
43
- switch (recipe.type) {
44
- case 'BalanceTransactionToProve':
45
- return pipe(Effect.succeed(recipe.transactionToProve.eraseProofs()), Effect.map((proven) => recipe.transactionToBalance.merge(proven)));
46
- case 'TransactionToProve':
47
- return Effect.succeed(recipe.transaction.eraseProofs());
48
- case 'NothingToProve':
49
- return Effect.succeed(recipe.transaction);
50
- }
32
+ prove(transaction) {
33
+ return Effect.succeed(transaction.eraseProofs());
51
34
  },
52
35
  };
53
36
  };
@@ -2,10 +2,9 @@ import * as ledger from '@midnight-ntwrk/ledger-v7';
2
2
  import { Effect, Scope, Stream } from 'effect';
3
3
  import { WalletRuntimeError, Variant, StateChange } from '@midnight-ntwrk/wallet-sdk-runtime/abstractions';
4
4
  import { ProvingService } from './Proving.js';
5
- import { ProvingRecipe } from './ProvingRecipe.js';
6
5
  import { SerializationCapability } from './Serialization.js';
7
6
  import { EventsSyncUpdate, SyncCapability, SyncService } from './Sync.js';
8
- import { TransactingCapability, TokenTransfer } from './Transacting.js';
7
+ import { TransactingCapability, TokenTransfer, BalancingResult } from './Transacting.js';
9
8
  import { WalletError } from './WalletError.js';
10
9
  import { CoinsAndBalancesCapability } from './CoinsAndBalances.js';
11
10
  import { KeysCapability } from './Keys.js';
@@ -37,10 +36,10 @@ export declare class RunningV1Variant<TSerialized, TSyncUpdate, TTransaction, TS
37
36
  constructor(scope: Scope.Scope, context: Variant.VariantContext<CoreWallet>, v1Context: RunningV1Variant.Context<TSerialized, TSyncUpdate, TTransaction, TStartAux>);
38
37
  startSyncInBackground(startAux: TStartAux): Effect.Effect<void>;
39
38
  startSync(startAux: TStartAux): Stream.Stream<void, WalletError, Scope.Scope>;
40
- balanceTransaction(secretKeys: ledger.ZswapSecretKeys, tx: ledger.Transaction<ledger.Signaturish, ledger.Proofish, ledger.Bindingish>): Effect.Effect<ProvingRecipe<TTransaction>, WalletError>;
41
- transferTransaction(secretKeys: ledger.ZswapSecretKeys, outputs: ReadonlyArray<TokenTransfer>): Effect.Effect<ProvingRecipe<TTransaction>, WalletError>;
42
- initSwap(secretKeys: ledger.ZswapSecretKeys, desiredInputs: Record<ledger.RawTokenType, bigint>, desiredOutputs: ReadonlyArray<TokenTransfer>): Effect.Effect<ProvingRecipe<TTransaction>, WalletError>;
43
- finalizeTransaction(recipe: ProvingRecipe<TTransaction>): Effect.Effect<TTransaction, WalletError>;
39
+ balanceTransaction(secretKeys: ledger.ZswapSecretKeys, tx: ledger.Transaction<ledger.Signaturish, ledger.Proofish, ledger.Bindingish>): Effect.Effect<BalancingResult, WalletError>;
40
+ transferTransaction(secretKeys: ledger.ZswapSecretKeys, outputs: ReadonlyArray<TokenTransfer>): Effect.Effect<ledger.UnprovenTransaction, WalletError>;
41
+ initSwap(secretKeys: ledger.ZswapSecretKeys, desiredInputs: Record<ledger.RawTokenType, bigint>, desiredOutputs: ReadonlyArray<TokenTransfer>): Effect.Effect<ledger.UnprovenTransaction, WalletError>;
42
+ finalizeTransaction(transaction: ledger.UnprovenTransaction): Effect.Effect<TTransaction, WalletError>;
44
43
  submitTransaction: SubmitTransactionMethod<TTransaction>;
45
44
  serializeState(state: CoreWallet): TSerialized;
46
45
  }
@@ -66,28 +66,28 @@ export class RunningV1Variant {
66
66
  }
67
67
  balanceTransaction(secretKeys, tx) {
68
68
  return SubscriptionRef.modifyEffect(this.#context.stateRef, (state) => {
69
- return pipe(this.#v1Context.transactingCapability.balanceTransaction(secretKeys, state, tx), EitherOps.toEffect, Effect.map(({ recipe, newState }) => [recipe, newState]));
69
+ return pipe(this.#v1Context.transactingCapability.balanceTransaction(secretKeys, state, tx), EitherOps.toEffect);
70
70
  });
71
71
  }
72
72
  transferTransaction(secretKeys, outputs) {
73
73
  return SubscriptionRef.modifyEffect(this.#context.stateRef, (state) => {
74
- return pipe(this.#v1Context.transactingCapability.makeTransfer(secretKeys, state, outputs), EitherOps.toEffect, Effect.map(({ recipe, newState }) => [recipe, newState]));
74
+ return pipe(this.#v1Context.transactingCapability.makeTransfer(secretKeys, state, outputs), EitherOps.toEffect);
75
75
  });
76
76
  }
77
77
  initSwap(secretKeys, desiredInputs, desiredOutputs) {
78
78
  return SubscriptionRef.modifyEffect(this.#context.stateRef, (state) => {
79
- return pipe(this.#v1Context.transactingCapability.initSwap(secretKeys, state, desiredInputs, desiredOutputs), EitherOps.toEffect, Effect.map(({ recipe, newState }) => [recipe, newState]));
79
+ return pipe(this.#v1Context.transactingCapability.initSwap(secretKeys, state, desiredInputs, desiredOutputs), EitherOps.toEffect);
80
80
  });
81
81
  }
82
- finalizeTransaction(recipe) {
82
+ finalizeTransaction(transaction) {
83
83
  return this.#v1Context.provingService
84
- .prove(recipe)
85
- .pipe(Effect.tapError(() => SubscriptionRef.updateEffect(this.#context.stateRef, (state) => EitherOps.toEffect(this.#v1Context.transactingCapability.revertRecipe(state, recipe)))));
84
+ .prove(transaction)
85
+ .pipe(Effect.tapError(() => SubscriptionRef.updateEffect(this.#context.stateRef, (state) => EitherOps.toEffect(this.#v1Context.transactingCapability.revertTransaction(state, transaction)))));
86
86
  }
87
87
  submitTransaction = ((transaction, waitForStatus = 'InBlock') => {
88
88
  return this.#v1Context.submissionService
89
89
  .submitTransaction(transaction, waitForStatus)
90
- .pipe(Effect.tapError(() => SubscriptionRef.updateEffect(this.#context.stateRef, (state) => EitherOps.toEffect(this.#v1Context.transactingCapability.revert(state, transaction)))));
90
+ .pipe(Effect.tapError(() => SubscriptionRef.updateEffect(this.#context.stateRef, (state) => EitherOps.toEffect(this.#v1Context.transactingCapability.revertTransaction(state, transaction)))));
91
91
  });
92
92
  serializeState(state) {
93
93
  return this.#v1Context.serializationCapability.serialize(state);
package/dist/v1/Sync.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as ledger from '@midnight-ntwrk/ledger-v7';
2
- import { Scope, Stream, Schema } from 'effect';
2
+ import { Schema, Scope, Stream } from 'effect';
3
3
  import { CoreWallet } from './CoreWallet.js';
4
4
  import { Simulator, SimulatorState } from './Simulator.js';
5
5
  import { WalletError } from './WalletError.js';
@@ -16,6 +16,7 @@ export type IndexerClientConnection = {
16
16
  };
17
17
  export type DefaultSyncConfiguration = {
18
18
  indexerClientConnection: IndexerClientConnection;
19
+ batchSize?: number;
19
20
  };
20
21
  export type DefaultSyncContext = {
21
22
  transactionHistoryCapability: TransactionHistoryCapability<CoreWallet, ledger.FinalizedTransaction>;
@@ -25,11 +26,11 @@ export declare const SecretKeysResource: {
25
26
  create: (secretKeys: ledger.ZswapSecretKeys) => SecretKeysResource;
26
27
  };
27
28
  export type WalletSyncUpdate = {
28
- update: EventsSyncUpdate;
29
+ updates: EventsSyncUpdate[];
29
30
  secretKeys: SecretKeysResource;
30
31
  };
31
32
  export declare const WalletSyncUpdate: {
32
- create: (update: EventsSyncUpdate, secretKeys: ledger.ZswapSecretKeys) => WalletSyncUpdate;
33
+ create: (updates: EventsSyncUpdate[], secretKeys: ledger.ZswapSecretKeys) => WalletSyncUpdate;
33
34
  };
34
35
  export declare const EventsSyncUpdate: Schema.TaggedStruct<"EventsSyncUpdate", {
35
36
  id: typeof Schema.Number;
package/dist/v1/Sync.js CHANGED
@@ -11,10 +11,10 @@
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
13
  import * as ledger from '@midnight-ntwrk/ledger-v7';
14
- import { Effect, ParseResult, Stream, Schema, pipe, Either, Duration } from 'effect';
14
+ import { Chunk, Duration, Effect, Either, ParseResult, pipe, Schedule, Schema, Stream } from 'effect';
15
15
  import { CoreWallet } from './CoreWallet.js';
16
16
  import { ZswapEvents } from '@midnight-ntwrk/wallet-sdk-indexer-client';
17
- import { WsSubscriptionClient, ConnectionHelper } from '@midnight-ntwrk/wallet-sdk-indexer-client/effect';
17
+ import { ConnectionHelper, WsSubscriptionClient } from '@midnight-ntwrk/wallet-sdk-indexer-client/effect';
18
18
  import { SyncWalletError } from './WalletError.js';
19
19
  import { WsURL } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
20
20
  import { EitherOps } from '@midnight-ntwrk/wallet-sdk-utilities';
@@ -39,9 +39,9 @@ export const SecretKeysResource = {
39
39
  },
40
40
  };
41
41
  export const WalletSyncUpdate = {
42
- create: (update, secretKeys) => {
42
+ create: (updates, secretKeys) => {
43
43
  return {
44
- update,
44
+ updates,
45
45
  secretKeys: SecretKeysResource.create(secretKeys),
46
46
  };
47
47
  },
@@ -97,17 +97,20 @@ export const makeEventsSyncService = (config) => {
97
97
  }
98
98
  const indexerWsUrl = indexerWsUrlResult.right;
99
99
  const appliedIndex = state.progress?.appliedIndex ?? 0n;
100
- const batchSize = 50;
101
- const batchTimeout = Duration.seconds(10);
102
- return pipe(ZswapEvents.run({ id: Number(appliedIndex) }), Stream.provideLayer(WsSubscriptionClient.layer({ url: indexerWsUrl })), Stream.mapError((error) => new SyncWalletError(error)), Stream.mapEffect((subscription) => pipe(Schema.decodeUnknownEither(EventsSyncUpdateFromPayload)(subscription.zswapLedgerEvents), Either.mapLeft((err) => new SyncWalletError(err)), EitherOps.toEffect)), Stream.map((data) => WalletSyncUpdate.create(data, secretKeys)), Stream.groupedWithin(batchSize, batchTimeout), Stream.flatMap((chunk) => Stream.fromIterable(chunk)));
100
+ const batchSize = config.batchSize ?? 10;
101
+ return pipe(ZswapEvents.run({ id: Number(appliedIndex) }), Stream.provideLayer(WsSubscriptionClient.layer({ url: indexerWsUrl })), Stream.mapError((error) => new SyncWalletError(error)), Stream.mapEffect((subscription) => pipe(subscription.zswapLedgerEvents, Schema.decodeUnknownEither(EventsSyncUpdateFromPayload), Either.mapLeft((err) => new SyncWalletError(err)), EitherOps.toEffect)), Stream.groupedWithin(batchSize, Duration.millis(1)), Stream.map(Chunk.toArray), Stream.map((data) => WalletSyncUpdate.create(data, secretKeys)), Stream.schedule(Schedule.spaced(Duration.millis(4))));
103
102
  },
104
103
  };
105
104
  };
106
105
  export const makeEventsSyncCapability = () => {
107
106
  return {
108
107
  applyUpdate: (state, wrappedUpdate) => {
109
- const nextIndex = BigInt(wrappedUpdate.update.id);
110
- const highestRelevantWalletIndex = BigInt(wrappedUpdate.update.maxId);
108
+ if (wrappedUpdate.updates.length === 0) {
109
+ return state;
110
+ }
111
+ const lastUpdate = wrappedUpdate.updates.at(-1);
112
+ const nextIndex = BigInt(lastUpdate.id);
113
+ const highestRelevantWalletIndex = BigInt(lastUpdate.maxId);
111
114
  // in case the nextIndex is less than or equal to the appliedIndex
112
115
  // just update highestRelevantWalletIndex
113
116
  if (nextIndex <= state.progress.appliedIndex) {
@@ -117,7 +120,7 @@ export const makeEventsSyncCapability = () => {
117
120
  });
118
121
  }
119
122
  return wrappedUpdate.secretKeys((keys) => {
120
- return CoreWallet.updateProgress(CoreWallet.replayEvents(state, keys, [wrappedUpdate.update.event]), {
123
+ return CoreWallet.updateProgress(CoreWallet.replayEvents(state, keys, wrappedUpdate.updates.map((u) => u.event)), {
121
124
  highestRelevantWalletIndex,
122
125
  appliedIndex: nextIndex,
123
126
  isConnected: true,
@@ -1,12 +1,11 @@
1
1
  import * as ledger from '@midnight-ntwrk/ledger-v7';
2
2
  import { NetworkId } from '@midnight-ntwrk/wallet-sdk-abstractions';
3
3
  import { Array as Arr, Either } from 'effect';
4
- import { ProvingRecipe } from './ProvingRecipe.js';
5
4
  import { CoreWallet } from './CoreWallet.js';
6
5
  import { WalletError } from './WalletError.js';
7
6
  import { CoinSelection } from '@midnight-ntwrk/wallet-sdk-capabilities';
8
7
  import { TransactionImbalances } from './TransactionImbalances.js';
9
- import { TransactionTrait } from './Transaction.js';
8
+ import { TransactionOps } from './TransactionOps.js';
10
9
  import { CoinsAndBalancesCapability } from './CoinsAndBalances.js';
11
10
  import { KeysCapability } from './Keys.js';
12
11
  export interface TokenTransfer {
@@ -14,21 +13,12 @@ export interface TokenTransfer {
14
13
  readonly type: ledger.RawTokenType;
15
14
  readonly receiverAddress: string;
16
15
  }
16
+ export type BalancingResult = ledger.UnprovenTransaction | undefined;
17
17
  export interface TransactingCapability<TSecrets, TState, TTransaction> {
18
- balanceTransaction(secrets: TSecrets, state: TState, tx: ledger.Transaction<ledger.Signaturish, ledger.Proofish, ledger.Bindingish>): Either.Either<{
19
- recipe: ProvingRecipe<TTransaction>;
20
- newState: TState;
21
- }, WalletError>;
22
- makeTransfer(secrets: TSecrets, state: TState, outputs: ReadonlyArray<TokenTransfer>): Either.Either<{
23
- recipe: ProvingRecipe<TTransaction>;
24
- newState: TState;
25
- }, WalletError>;
26
- initSwap(secrets: TSecrets, state: TState, desiredInputs: Record<ledger.RawTokenType, bigint>, desiredOutputs: ReadonlyArray<TokenTransfer>): Either.Either<{
27
- recipe: ProvingRecipe<TTransaction>;
28
- newState: TState;
29
- }, WalletError>;
30
- revert(state: TState, tx: TTransaction): Either.Either<TState, WalletError>;
31
- revertRecipe(state: TState, recipe: ProvingRecipe<TTransaction>): Either.Either<TState, WalletError>;
18
+ balanceTransaction(secrets: TSecrets, state: TState, tx: ledger.Transaction<ledger.Signaturish, ledger.Proofish, ledger.Bindingish>): Either.Either<[BalancingResult, TState], WalletError>;
19
+ makeTransfer(secrets: TSecrets, state: TState, outputs: ReadonlyArray<TokenTransfer>): Either.Either<[ledger.UnprovenTransaction, TState], WalletError>;
20
+ initSwap(secrets: TSecrets, state: TState, desiredInputs: Record<ledger.RawTokenType, bigint>, desiredOutputs: ReadonlyArray<TokenTransfer>): Either.Either<[ledger.UnprovenTransaction, TState], WalletError>;
21
+ revertTransaction(state: TState, transaction: TTransaction | ledger.UnprovenTransaction): Either.Either<TState, WalletError>;
32
22
  }
33
23
  export type DefaultTransactingConfiguration = {
34
24
  networkId: NetworkId.NetworkId;
@@ -44,24 +34,14 @@ export declare class TransactingCapabilityImplementation<TTransaction extends le
44
34
  #private;
45
35
  readonly networkId: NetworkId.NetworkId;
46
36
  readonly getCoinSelection: () => CoinSelection<ledger.QualifiedShieldedCoinInfo>;
47
- readonly txTrait: TransactionTrait<TTransaction>;
37
+ readonly txOps: TransactionOps<TTransaction>;
48
38
  readonly getCoins: () => CoinsAndBalancesCapability<CoreWallet>;
49
39
  readonly getKeys: () => KeysCapability<CoreWallet>;
50
- constructor(networkId: NetworkId.NetworkId, getCoinSelection: () => CoinSelection<ledger.QualifiedShieldedCoinInfo>, getCoins: () => CoinsAndBalancesCapability<CoreWallet>, getKeys: () => KeysCapability<CoreWallet>, txTrait: TransactionTrait<TTransaction>);
51
- balanceTransaction(secretKeys: ledger.ZswapSecretKeys, state: CoreWallet, tx: TTransaction): Either.Either<{
52
- recipe: ProvingRecipe<TTransaction>;
53
- newState: CoreWallet;
54
- }, WalletError>;
55
- makeTransfer(secretKeys: ledger.ZswapSecretKeys, state: CoreWallet, transfers: Arr.NonEmptyReadonlyArray<TokenTransfer>): Either.Either<{
56
- recipe: ProvingRecipe<TTransaction>;
57
- newState: CoreWallet;
58
- }, WalletError>;
59
- initSwap(secretKeys: ledger.ZswapSecretKeys, state: CoreWallet, desiredInputs: Record<ledger.RawTokenType, bigint>, desiredOutputs: ReadonlyArray<TokenTransfer>): Either.Either<{
60
- recipe: ProvingRecipe<TTransaction>;
61
- newState: CoreWallet;
62
- }, WalletError>;
63
- revert(state: CoreWallet, tx: TTransaction): Either.Either<CoreWallet, WalletError>;
64
- revertRecipe(state: CoreWallet, recipe: ProvingRecipe<TTransaction>): Either.Either<CoreWallet, WalletError>;
40
+ constructor(networkId: NetworkId.NetworkId, getCoinSelection: () => CoinSelection<ledger.QualifiedShieldedCoinInfo>, getCoins: () => CoinsAndBalancesCapability<CoreWallet>, getKeys: () => KeysCapability<CoreWallet>, txOps: TransactionOps<TTransaction>);
41
+ balanceTransaction(secretKeys: ledger.ZswapSecretKeys, state: CoreWallet, tx: TTransaction): Either.Either<[BalancingResult, CoreWallet], WalletError>;
42
+ makeTransfer(secretKeys: ledger.ZswapSecretKeys, state: CoreWallet, transfers: Arr.NonEmptyReadonlyArray<TokenTransfer>): Either.Either<[ledger.UnprovenTransaction, CoreWallet], WalletError>;
43
+ initSwap(secretKeys: ledger.ZswapSecretKeys, state: CoreWallet, desiredInputs: Record<ledger.RawTokenType, bigint>, desiredOutputs: ReadonlyArray<TokenTransfer>): Either.Either<[ledger.UnprovenTransaction, CoreWallet], WalletError>;
44
+ revertTransaction(state: CoreWallet, transaction: TTransaction | ledger.UnprovenTransaction): Either.Either<CoreWallet, WalletError>;
65
45
  balanceFallibleSection(secretKeys: ledger.ZswapSecretKeys, state: CoreWallet, imbalances: TransactionImbalances, coinSelection: CoinSelection<ledger.QualifiedShieldedCoinInfo>): Either.Either<{
66
46
  offer: ledger.ZswapOffer<ledger.PreProof> | undefined;
67
47
  newState: CoreWallet;
@@ -13,56 +13,41 @@
13
13
  import * as ledger from '@midnight-ntwrk/ledger-v7';
14
14
  import { Array as Arr, Either, Option, pipe, Record } from 'effect';
15
15
  import { ArrayOps } from '@midnight-ntwrk/wallet-sdk-utilities';
16
- import { BALANCE_TRANSACTION_TO_PROVE, NOTHING_TO_PROVE, TRANSACTION_TO_PROVE, } from './ProvingRecipe.js';
17
16
  import { CoreWallet } from './CoreWallet.js';
18
17
  import { AddressError, InsufficientFundsError, OtherWalletError } from './WalletError.js';
19
18
  import { getBalanceRecipe, Imbalances, InsufficientFundsError as BalancingInsufficientFundsError, } from '@midnight-ntwrk/wallet-sdk-capabilities';
20
19
  import { MidnightBech32m, ShieldedAddress } from '@midnight-ntwrk/wallet-sdk-address-format';
21
20
  import { ShieldedCostModel, TransactionImbalances } from './TransactionImbalances.js';
22
- import { TransactionTrait } from './Transaction.js';
21
+ import { TransactionOps } from './TransactionOps.js';
23
22
  export const makeDefaultTransactingCapability = (config, getContext) => {
24
- return new TransactingCapabilityImplementation(config.networkId, () => getContext().coinSelection, () => getContext().coinsAndBalancesCapability, () => getContext().keysCapability, TransactionTrait.default);
23
+ return new TransactingCapabilityImplementation(config.networkId, () => getContext().coinSelection, () => getContext().coinsAndBalancesCapability, () => getContext().keysCapability, TransactionOps.default);
25
24
  };
26
25
  export const makeSimulatorTransactingCapability = (config, getContext) => {
27
- return new TransactingCapabilityImplementation(config.networkId, () => getContext().coinSelection, () => getContext().coinsAndBalancesCapability, () => getContext().keysCapability, TransactionTrait.proofErased);
26
+ return new TransactingCapabilityImplementation(config.networkId, () => getContext().coinSelection, () => getContext().coinsAndBalancesCapability, () => getContext().keysCapability, TransactionOps.proofErased);
28
27
  };
29
28
  export class TransactingCapabilityImplementation {
30
29
  networkId;
31
30
  getCoinSelection;
32
- txTrait;
31
+ txOps;
33
32
  getCoins;
34
33
  getKeys;
35
- constructor(networkId, getCoinSelection, getCoins, getKeys, txTrait) {
34
+ constructor(networkId, getCoinSelection, getCoins, getKeys, txOps) {
36
35
  this.getCoins = getCoins;
37
36
  this.networkId = networkId;
38
37
  this.getCoinSelection = getCoinSelection;
39
38
  this.getKeys = getKeys;
40
- this.txTrait = txTrait;
39
+ this.txOps = txOps;
41
40
  }
42
41
  balanceTransaction(secretKeys, state, tx) {
43
42
  return Either.gen(this, function* () {
44
43
  const coinSelection = this.getCoinSelection();
45
- const networkId = this.networkId;
46
- const initialImbalances = this.txTrait.getImbalances(tx);
44
+ const initialImbalances = this.txOps.getImbalances(tx);
47
45
  if (TransactionImbalances.areBalanced(initialImbalances)) {
48
- return {
49
- recipe: {
50
- type: NOTHING_TO_PROVE,
51
- transaction: tx,
52
- },
53
- newState: state,
54
- };
46
+ return [undefined, state];
55
47
  }
56
48
  const { newState: afterFallible, offer: maybeFallible } = yield* this.balanceFallibleSection(secretKeys, state, initialImbalances, coinSelection);
57
49
  const { newState: afterGuaranteed, offer: guaranteed } = yield* this.#balanceGuaranteedSection(secretKeys, afterFallible, initialImbalances, coinSelection, Imbalances.empty());
58
- return {
59
- newState: afterGuaranteed,
60
- recipe: {
61
- type: BALANCE_TRANSACTION_TO_PROVE,
62
- transactionToBalance: tx,
63
- transactionToProve: ledger.Transaction.fromParts(networkId, guaranteed, maybeFallible),
64
- },
65
- };
50
+ return [ledger.Transaction.fromParts(this.networkId, guaranteed, maybeFallible), afterGuaranteed];
66
51
  });
67
52
  }
68
53
  makeTransfer(secretKeys, state, transfers) {
@@ -77,17 +62,11 @@ export class TransactingCapabilityImplementation {
77
62
  const { initialOffersAndCoins, selfCoins } = yield* this.#processDesiredOutputs(state, positiveTransfers);
78
63
  const offerToBalance = pipe(initialOffersAndCoins, Arr.map((o) => o.outputOffer), ArrayOps.fold((a, b) => a.merge(b)));
79
64
  const unprovenTxToBalance = ledger.Transaction.fromParts(networkId, offerToBalance);
80
- const imbalances = TransactionTrait.unproven.getImbalances(unprovenTxToBalance);
65
+ const imbalances = TransactionOps.unproven.getImbalances(unprovenTxToBalance);
81
66
  const { offer, newState } = yield* this.#balanceGuaranteedSection(secretKeys, state, imbalances, this.getCoinSelection(), Imbalances.empty());
82
67
  const finalState = CoreWallet.watchCoins(newState, secretKeys, selfCoins);
83
68
  const finalTx = unprovenTxToBalance.merge(ledger.Transaction.fromParts(networkId, offer));
84
- return {
85
- newState: finalState,
86
- recipe: {
87
- type: TRANSACTION_TO_PROVE,
88
- transaction: finalTx,
89
- },
90
- };
69
+ return [finalTx, finalState];
91
70
  });
92
71
  }
93
72
  initSwap(secretKeys, state, desiredInputs, desiredOutputs) {
@@ -113,51 +92,22 @@ export class TransactingCapabilityImplementation {
113
92
  const finalTx = outputsParseResult.unprovenTxToBalance
114
93
  ? outputsParseResult.unprovenTxToBalance.merge(balancingTx)
115
94
  : balancingTx;
116
- return {
117
- newState: finalState,
118
- recipe: {
119
- type: TRANSACTION_TO_PROVE,
120
- transaction: finalTx,
121
- },
122
- };
95
+ return [finalTx, finalState];
123
96
  });
124
97
  }
125
- revert(state, tx) {
98
+ revertTransaction(state, transaction) {
126
99
  return Either.try({
127
100
  try: () => {
128
- return CoreWallet.revertTransaction(state, tx);
101
+ return CoreWallet.revertTransaction(state, transaction);
129
102
  },
130
103
  catch: (err) => {
131
104
  return new OtherWalletError({
132
- message: `Error while reverting transaction ${this.txTrait.id(tx)}`,
105
+ message: `Error while reverting transaction ${transaction.identifiers().at(0)}`,
133
106
  cause: err,
134
107
  });
135
108
  },
136
109
  });
137
110
  }
138
- revertRecipe(state, recipe) {
139
- const doRevert = (tx) => {
140
- return Either.try({
141
- try: () => {
142
- return CoreWallet.revertTransaction(state, tx);
143
- },
144
- catch: (err) => {
145
- return new OtherWalletError({
146
- message: `Error while reverting transaction ${TransactionTrait.unproven.id(tx)}`,
147
- cause: err,
148
- });
149
- },
150
- });
151
- };
152
- switch (recipe.type) {
153
- case TRANSACTION_TO_PROVE:
154
- return doRevert(recipe.transaction);
155
- case BALANCE_TRANSACTION_TO_PROVE:
156
- return doRevert(recipe.transactionToProve);
157
- case NOTHING_TO_PROVE:
158
- return Either.right(state);
159
- }
160
- }
161
111
  #prepareOffer(secretKeys, state, recipe, segment) {
162
112
  const [inputOffers, stateAfterSpends] = CoreWallet.spendCoins(state, secretKeys, recipe.inputs, segment);
163
113
  const stateAfterWatches = CoreWallet.watchCoins(stateAfterSpends, secretKeys, recipe.outputs);
@@ -301,7 +251,7 @@ export class TransactingCapabilityImplementation {
301
251
  const networkId = this.networkId;
302
252
  const offerToBalance = pipe(initialOffersAndCoins, Arr.map((o) => o.outputOffer), ArrayOps.fold((a, b) => a.merge(b)));
303
253
  const unprovenTxToBalance = ledger.Transaction.fromParts(networkId, offerToBalance);
304
- const imbalances = TransactionTrait.unproven.getImbalances(unprovenTxToBalance);
254
+ const imbalances = TransactionOps.unproven.getImbalances(unprovenTxToBalance);
305
255
  return {
306
256
  imbalances,
307
257
  selfCoins,
@@ -1,14 +1,14 @@
1
1
  import { Imbalances } from '@midnight-ntwrk/wallet-sdk-capabilities';
2
2
  import { TransactionImbalances } from './TransactionImbalances.js';
3
3
  import * as ledger from '@midnight-ntwrk/ledger-v7';
4
- export type TransactionTrait<Tx> = {
4
+ export type TransactionOps<Tx> = {
5
5
  getImbalances(tx: Tx): TransactionImbalances;
6
6
  id(tx: Tx): string;
7
7
  };
8
- export declare const TransactionTrait: {
9
- default: TransactionTrait<ledger.FinalizedTransaction>;
10
- proofErased: TransactionTrait<ledger.ProofErasedTransaction>;
11
- unproven: TransactionTrait<ledger.UnprovenTransaction>;
8
+ export declare const TransactionOps: {
9
+ default: TransactionOps<ledger.FinalizedTransaction>;
10
+ proofErased: TransactionOps<ledger.ProofErasedTransaction>;
11
+ unproven: TransactionOps<ledger.UnprovenTransaction>;
12
12
  shared: {
13
13
  getImbalances(tx: ledger.FinalizedTransaction | ledger.UnprovenTransaction | ledger.ProofErasedTransaction): TransactionImbalances;
14
14
  getGuaranteedImbalances: (tx: ledger.FinalizedTransaction | ledger.UnprovenTransaction | ledger.ProofErasedTransaction) => Imbalances;
@@ -12,10 +12,10 @@
12
12
  // limitations under the License.
13
13
  import { pipe } from 'effect';
14
14
  import { Imbalances } from '@midnight-ntwrk/wallet-sdk-capabilities';
15
- export const TransactionTrait = new (class {
15
+ export const TransactionOps = new (class {
16
16
  default = {
17
17
  getImbalances(tx) {
18
- return TransactionTrait.shared.getImbalances(tx);
18
+ return TransactionOps.shared.getImbalances(tx);
19
19
  },
20
20
  id(tx) {
21
21
  return tx.identifiers().at(0);
@@ -23,7 +23,7 @@ export const TransactionTrait = new (class {
23
23
  };
24
24
  proofErased = {
25
25
  getImbalances(tx) {
26
- return TransactionTrait.shared.getImbalances(tx);
26
+ return TransactionOps.shared.getImbalances(tx);
27
27
  },
28
28
  id(tx) {
29
29
  return tx.identifiers().at(0);
@@ -31,7 +31,7 @@ export const TransactionTrait = new (class {
31
31
  };
32
32
  unproven = {
33
33
  getImbalances(tx) {
34
- return TransactionTrait.shared.getImbalances(tx);
34
+ return TransactionOps.shared.getImbalances(tx);
35
35
  },
36
36
  id(tx) {
37
37
  return tx.identifiers().at(0);
@@ -39,8 +39,8 @@ export const TransactionTrait = new (class {
39
39
  };
40
40
  shared = {
41
41
  getImbalances(tx) {
42
- const guaranteedImbalances = TransactionTrait.shared.getGuaranteedImbalances(tx);
43
- const fallibleImbalances = TransactionTrait.shared.getFallibleImbalances(tx);
42
+ const guaranteedImbalances = TransactionOps.shared.getGuaranteedImbalances(tx);
43
+ const fallibleImbalances = TransactionOps.shared.getFallibleImbalances(tx);
44
44
  return pipe({
45
45
  guaranteed: guaranteedImbalances,
46
46
  fallible: fallibleImbalances,
@@ -5,7 +5,6 @@ export * as Transacting from './Transacting.js';
5
5
  export * as TransactionHistory from './TransactionHistory.js';
6
6
  export * as Serialization from './Serialization.js';
7
7
  export * as Proving from './Proving.js';
8
- export * as ProvingRecipe from './ProvingRecipe.js';
9
8
  export * as CoinsAndBalances from './CoinsAndBalances.js';
10
9
  export * as Keys from './Keys.js';
11
10
  export * from './RunningV1Variant.js';
@@ -13,4 +12,4 @@ export * as Simulator from './Simulator.js';
13
12
  export * as Submission from './Submission.js';
14
13
  export * as WalletError from './WalletError.js';
15
14
  export * from './CoreWallet.js';
16
- export * from './Transaction.js';
15
+ export * from './TransactionOps.js';
package/dist/v1/index.js CHANGED
@@ -17,7 +17,6 @@ export * as Transacting from './Transacting.js';
17
17
  export * as TransactionHistory from './TransactionHistory.js';
18
18
  export * as Serialization from './Serialization.js';
19
19
  export * as Proving from './Proving.js';
20
- export * as ProvingRecipe from './ProvingRecipe.js';
21
20
  export * as CoinsAndBalances from './CoinsAndBalances.js';
22
21
  export * as Keys from './Keys.js';
23
22
  export * from './RunningV1Variant.js';
@@ -25,4 +24,4 @@ export * as Simulator from './Simulator.js';
25
24
  export * as Submission from './Submission.js';
26
25
  export * as WalletError from './WalletError.js';
27
26
  export * from './CoreWallet.js';
28
- export * from './Transaction.js';
27
+ export * from './TransactionOps.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midnight-ntwrk/wallet-sdk-shielded",
3
- "version": "1.0.0-beta.14",
3
+ "version": "1.0.0-beta.16",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -28,17 +28,17 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@midnight-ntwrk/ledger-v7": "7.0.0-alpha.1",
31
+ "@midnight-ntwrk/ledger-v7": "7.0.0-rc.1",
32
32
  "@midnight-ntwrk/wallet-sdk-abstractions": "1.0.0-beta.10",
33
- "@midnight-ntwrk/wallet-sdk-address-format": "3.0.0-beta.10",
34
- "@midnight-ntwrk/wallet-sdk-capabilities": "3.0.0-beta.10",
35
- "@midnight-ntwrk/wallet-sdk-indexer-client": "1.0.0-beta.15",
36
- "@midnight-ntwrk/wallet-sdk-node-client": "1.0.0-beta.11",
37
- "@midnight-ntwrk/wallet-sdk-prover-client": "1.0.0-beta.12",
38
- "@midnight-ntwrk/wallet-sdk-runtime": "1.0.0-beta.10",
39
- "@midnight-ntwrk/wallet-sdk-utilities": "1.0.0-beta.9",
33
+ "@midnight-ntwrk/wallet-sdk-address-format": "3.0.0-beta.11",
34
+ "@midnight-ntwrk/wallet-sdk-capabilities": "3.0.0-beta.11",
35
+ "@midnight-ntwrk/wallet-sdk-indexer-client": "1.0.0-beta.16",
36
+ "@midnight-ntwrk/wallet-sdk-node-client": "1.0.0-beta.12",
37
+ "@midnight-ntwrk/wallet-sdk-prover-client": "1.0.0-beta.13",
38
+ "@midnight-ntwrk/wallet-sdk-runtime": "1.0.0-beta.11",
39
+ "@midnight-ntwrk/wallet-sdk-utilities": "1.0.0-beta.10",
40
40
  "@scure/base": "^1.1.9",
41
- "effect": "^3.17.3",
41
+ "effect": "^3.19.14",
42
42
  "isomorphic-ws": "^5.0.0",
43
43
  "node-fetch": "3.3.2",
44
44
  "rxjs": "^7.5",
@@ -1,18 +0,0 @@
1
- import * as ledger from '@midnight-ntwrk/ledger-v7';
2
- export declare const TRANSACTION_TO_PROVE = "TransactionToProve";
3
- export declare const BALANCE_TRANSACTION_TO_PROVE = "BalanceTransactionToProve";
4
- export declare const NOTHING_TO_PROVE = "NothingToProve";
5
- export type TransactionToProve = {
6
- readonly type: typeof TRANSACTION_TO_PROVE;
7
- readonly transaction: ledger.UnprovenTransaction;
8
- };
9
- export type BalanceTransactionToProve<Transaction> = {
10
- readonly type: typeof BALANCE_TRANSACTION_TO_PROVE;
11
- readonly transactionToProve: ledger.UnprovenTransaction;
12
- readonly transactionToBalance: Transaction;
13
- };
14
- export type NothingToProve<Transaction> = {
15
- readonly type: typeof NOTHING_TO_PROVE;
16
- readonly transaction: Transaction;
17
- };
18
- export type ProvingRecipe<Transaction> = TransactionToProve | BalanceTransactionToProve<Transaction> | NothingToProve<Transaction>;
@@ -1,3 +0,0 @@
1
- export const TRANSACTION_TO_PROVE = 'TransactionToProve';
2
- export const BALANCE_TRANSACTION_TO_PROVE = 'BalanceTransactionToProve';
3
- export const NOTHING_TO_PROVE = 'NothingToProve';