@midnight-ntwrk/wallet-sdk-unshielded-wallet 1.0.0-beta.12 → 1.0.0-beta.14

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 (59) hide show
  1. package/dist/KeyStore.d.ts +3 -7
  2. package/dist/KeyStore.js +2 -2
  3. package/dist/UnshieldedWallet.d.ts +57 -0
  4. package/dist/UnshieldedWallet.js +120 -0
  5. package/dist/index.d.ts +3 -3
  6. package/dist/index.js +3 -2
  7. package/dist/{tx-history-storage → storage}/InMemoryTransactionHistoryStorage.d.ts +3 -7
  8. package/dist/{tx-history-storage → storage}/TransactionHistoryStorage.d.ts +3 -7
  9. package/dist/{tx-history-storage → storage}/TransactionHistoryStorage.js +3 -7
  10. package/dist/{tx-history-storage → storage}/index.d.ts +1 -0
  11. package/dist/{tx-history-storage → storage}/index.js +1 -0
  12. package/dist/v1/CoinsAndBalances.d.ts +13 -0
  13. package/dist/v1/CoinsAndBalances.js +36 -0
  14. package/dist/v1/CoreWallet.d.ts +24 -0
  15. package/dist/v1/CoreWallet.js +60 -0
  16. package/dist/v1/Keys.d.ts +8 -0
  17. package/dist/v1/Keys.js +23 -0
  18. package/dist/v1/RunningV1Variant.d.ts +41 -0
  19. package/dist/v1/RunningV1Variant.js +91 -0
  20. package/dist/v1/Serialization.d.ts +12 -0
  21. package/dist/v1/Serialization.js +64 -0
  22. package/dist/v1/Simulator.d.ts +22 -0
  23. package/dist/v1/Simulator.js +102 -0
  24. package/dist/v1/Sync.d.ts +32 -0
  25. package/dist/v1/Sync.js +85 -0
  26. package/dist/v1/SyncProgress.d.ts +18 -0
  27. package/dist/v1/SyncProgress.js +23 -0
  28. package/dist/v1/SyncSchema.d.ts +449 -0
  29. package/dist/v1/SyncSchema.js +123 -0
  30. package/dist/v1/Transacting.d.ts +45 -0
  31. package/dist/v1/Transacting.js +252 -0
  32. package/dist/v1/Transaction.d.ts +25 -0
  33. package/dist/v1/Transaction.js +155 -0
  34. package/dist/v1/TransactionHistory.d.ts +12 -0
  35. package/dist/v1/TransactionHistory.js +29 -0
  36. package/dist/v1/TransactionImbalances.d.ts +8 -0
  37. package/dist/v1/TransactionImbalances.js +21 -0
  38. package/dist/v1/UnshieldedState.d.ts +37 -0
  39. package/dist/v1/UnshieldedState.js +67 -0
  40. package/dist/v1/V1Builder.d.ts +97 -0
  41. package/dist/v1/V1Builder.js +160 -0
  42. package/dist/v1/WalletError.d.ts +89 -0
  43. package/dist/v1/WalletError.js +53 -0
  44. package/dist/v1/index.d.ts +15 -0
  45. package/dist/v1/index.js +27 -0
  46. package/package.json +8 -8
  47. package/dist/State.d.ts +0 -21
  48. package/dist/State.js +0 -43
  49. package/dist/SyncService.d.ts +0 -55
  50. package/dist/SyncService.js +0 -88
  51. package/dist/TransactionHistoryService.d.ts +0 -31
  52. package/dist/TransactionHistoryService.js +0 -49
  53. package/dist/TransactionService.d.ts +0 -43
  54. package/dist/TransactionService.js +0 -291
  55. package/dist/WalletBuilder.d.ts +0 -37
  56. package/dist/WalletBuilder.js +0 -137
  57. /package/dist/{tx-history-storage → storage}/InMemoryTransactionHistoryStorage.js +0 -0
  58. /package/dist/{tx-history-storage → storage}/NoOpTransactionHistoryStorage.d.ts +0 -0
  59. /package/dist/{tx-history-storage → storage}/NoOpTransactionHistoryStorage.js +0 -0
@@ -0,0 +1,64 @@
1
+ // This file is part of MIDNIGHT-WALLET-SDK.
2
+ // Copyright (C) 2025 Midnight Foundation
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // You may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ import { Either, pipe, Schema } from 'effect';
14
+ import { WalletError } from './WalletError.js';
15
+ import { CoreWallet } from './CoreWallet.js';
16
+ import { ProtocolVersion } from '@midnight-ntwrk/wallet-sdk-abstractions';
17
+ import { UnshieldedState } from './UnshieldedState.js';
18
+ export const makeDefaultV1SerializationCapability = () => {
19
+ const UtxoWithMetaSchema = Schema.Struct({
20
+ utxo: Schema.Struct({
21
+ value: Schema.BigInt,
22
+ owner: Schema.String,
23
+ type: Schema.String,
24
+ intentHash: Schema.String,
25
+ outputNo: Schema.Number,
26
+ }),
27
+ meta: Schema.Struct({
28
+ ctime: Schema.Date,
29
+ registeredForDustGeneration: Schema.Boolean,
30
+ }),
31
+ });
32
+ const SnapshotSchema = Schema.Struct({
33
+ publicKey: Schema.Struct({
34
+ publicKey: Schema.String,
35
+ addressHex: Schema.String,
36
+ address: Schema.String,
37
+ }),
38
+ state: Schema.Struct({
39
+ availableUtxos: Schema.Array(UtxoWithMetaSchema),
40
+ pendingUtxos: Schema.Array(UtxoWithMetaSchema),
41
+ }),
42
+ protocolVersion: Schema.BigInt,
43
+ appliedId: Schema.optional(Schema.BigInt),
44
+ networkId: Schema.String,
45
+ });
46
+ return {
47
+ serialize: (wallet) => {
48
+ const buildSnapshot = (w) => ({
49
+ publicKey: w.publicKey,
50
+ state: UnshieldedState.toArrays(w.state),
51
+ protocolVersion: w.protocolVersion,
52
+ networkId: w.networkId,
53
+ appliedId: w.progress?.appliedId,
54
+ });
55
+ return pipe(wallet, buildSnapshot, Schema.encodeSync(SnapshotSchema), JSON.stringify);
56
+ },
57
+ deserialize: (serialized) => pipe(serialized, Schema.decodeUnknownEither(Schema.parseJson(SnapshotSchema)), Either.mapLeft((err) => WalletError.other(err)), Either.map((snapshot) => {
58
+ return CoreWallet.restore(UnshieldedState.restore(snapshot.state.availableUtxos, snapshot.state.pendingUtxos), snapshot.publicKey, {
59
+ highestTransactionId: snapshot.appliedId ?? 0n,
60
+ appliedId: snapshot.appliedId ?? 0n,
61
+ }, ProtocolVersion.ProtocolVersion(snapshot.protocolVersion), snapshot.networkId);
62
+ })),
63
+ };
64
+ };
@@ -0,0 +1,22 @@
1
+ import * as ledger from '@midnight-ntwrk/ledger-v6';
2
+ import { Array as Arr, Effect, Scope, Stream, SubscriptionRef } from 'effect';
3
+ export type SimulatorState = Readonly<{
4
+ ledger: ledger.LedgerState;
5
+ lastTxResult: ledger.TransactionResult;
6
+ lastTxNumber: bigint;
7
+ }>;
8
+ export declare class Simulator {
9
+ #private;
10
+ static nextBlockContext: (number: bigint) => Effect.Effect<ledger.BlockContext>;
11
+ static init(genesisMints: Readonly<Arr.NonEmptyArray<{
12
+ amount: bigint;
13
+ type: ledger.RawTokenType;
14
+ recipient: ledger.ZswapSecretKeys;
15
+ }>>): Effect.Effect<Simulator, never, Scope.Scope>;
16
+ readonly state$: Stream.Stream<SimulatorState>;
17
+ constructor(stateRef: SubscriptionRef.SubscriptionRef<SimulatorState>, state$: Stream.Stream<SimulatorState>);
18
+ submitRegularTx(tx: ledger.ProofErasedTransaction): Effect.Effect<{
19
+ blockNumber: bigint;
20
+ blockHash: string;
21
+ }>;
22
+ }
@@ -0,0 +1,102 @@
1
+ // This file is part of MIDNIGHT-WALLET-SDK.
2
+ // Copyright (C) 2025 Midnight Foundation
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // You may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ import * as ledger from '@midnight-ntwrk/ledger-v6';
14
+ import { Array as Arr, Effect, Encoding, pipe, Stream, SubscriptionRef, Clock } from 'effect';
15
+ import { ArrayOps, EitherOps } from '@midnight-ntwrk/wallet-sdk-utilities';
16
+ import * as crypto from 'crypto';
17
+ import { NetworkId } from '@midnight-ntwrk/wallet-sdk-abstractions';
18
+ const simpleHash = (input) => {
19
+ return Encoding.decodeHex(input).pipe(EitherOps.toEffect, Effect.andThen((parsed) => Effect.promise(() => crypto.subtle.digest('SHA-256', parsed))), Effect.andThen((out) => Encoding.encodeHex(new Uint8Array(out))), Effect.orDie);
20
+ };
21
+ const wellFormedStrictness = (params = {}) => {
22
+ const strictness = new ledger.WellFormedStrictness();
23
+ // Note: Enforce balancing should be true by default outside genesis mints
24
+ strictness.enforceBalancing = params?.enforceBalancing ?? false;
25
+ strictness.verifyNativeProofs = params?.verifyNativeProofs ?? false;
26
+ strictness.verifyContractProofs = params?.verifyContractProofs ?? false;
27
+ strictness.enforceLimits = params?.enforceLimits ?? false;
28
+ strictness.verifySignatures = params?.verifySignatures ?? false;
29
+ return strictness;
30
+ };
31
+ export class Simulator {
32
+ static nextBlockContext = (number) => pipe(number.toString(16), (str) => (str.length % 2 == 0 ? str : str.padStart(str.length + 1, '0')), simpleHash, Effect.map((hash) => ({
33
+ parentBlockHash: hash,
34
+ secondsSinceEpoch: number,
35
+ secondsSinceEpochErr: 1,
36
+ })));
37
+ static init(genesisMints) {
38
+ const emptyState = ledger.LedgerState.blank(NetworkId.NetworkId.Undeployed);
39
+ const noStrictness = wellFormedStrictness();
40
+ const makeTransactions = (context) => Effect.gen(function* () {
41
+ const nowMillis = yield* Clock.currentTimeMillis;
42
+ const verificationTime = new Date(nowMillis);
43
+ const tx = pipe(genesisMints, Arr.map((transfer) => {
44
+ const coin = ledger.createShieldedCoinInfo(transfer.type, transfer.amount);
45
+ const output = ledger.ZswapOutput.new(coin, 0, transfer.recipient.coinPublicKey, transfer.recipient.encryptionPublicKey);
46
+ return ledger.ZswapOffer.fromOutput(output, transfer.type, transfer.amount);
47
+ }), ArrayOps.fold((acc, offer) => acc.merge(offer)), (offer) => ledger.Transaction.fromParts(NetworkId.NetworkId.Undeployed, offer).eraseProofs(), (tx) => tx.wellFormed(emptyState, noStrictness, verificationTime));
48
+ const [initialState, initialResult] = emptyState.apply(tx, new ledger.TransactionContext(emptyState, context));
49
+ const postBlockUpdateState = initialState.postBlockUpdate(verificationTime);
50
+ return {
51
+ initialResult,
52
+ initialState: postBlockUpdateState,
53
+ tx,
54
+ };
55
+ });
56
+ return Effect.gen(function* () {
57
+ const context = yield* Simulator.nextBlockContext(0n);
58
+ const init = yield* makeTransactions(context);
59
+ const initialState = {
60
+ ledger: init.initialState,
61
+ lastTxResult: init.initialResult,
62
+ lastTxNumber: 0n,
63
+ };
64
+ const ref = yield* SubscriptionRef.make(initialState);
65
+ const changesStream = yield* Stream.share(ref.changes, {
66
+ capacity: 'unbounded',
67
+ replay: Number.MAX_SAFE_INTEGER,
68
+ });
69
+ yield* pipe(changesStream, Stream.runDrain, Effect.forkScoped);
70
+ return new Simulator(ref, changesStream);
71
+ });
72
+ }
73
+ #stateRef;
74
+ state$;
75
+ constructor(stateRef, state$) {
76
+ this.#stateRef = stateRef;
77
+ this.state$ = state$;
78
+ }
79
+ submitRegularTx(tx) {
80
+ return pipe(this.#stateRef, SubscriptionRef.modifyEffect((simulatorState) => Effect.gen(function* () {
81
+ const nextNumber = simulatorState.lastTxNumber + 1n;
82
+ const context = yield* Simulator.nextBlockContext(nextNumber);
83
+ const nowMillis = yield* Clock.currentTimeMillis;
84
+ const verificationTime = new Date(nowMillis);
85
+ const noStrictness = wellFormedStrictness();
86
+ const verifiedTx = tx.wellFormed(simulatorState.ledger, noStrictness, verificationTime);
87
+ const [newState, result] = simulatorState.ledger.apply(verifiedTx, new ledger.TransactionContext(simulatorState.ledger, context));
88
+ const postBlockUpdatedState = newState.postBlockUpdate(verificationTime);
89
+ const newSimulatorState = {
90
+ ...simulatorState,
91
+ ledger: postBlockUpdatedState,
92
+ lastTxResult: result,
93
+ lastTxNumber: nextNumber,
94
+ };
95
+ const output = {
96
+ blockNumber: nextNumber,
97
+ blockHash: context.parentBlockHash,
98
+ };
99
+ return [output, newSimulatorState];
100
+ })));
101
+ }
102
+ }
@@ -0,0 +1,32 @@
1
+ import { Scope, Stream, Either } from 'effect';
2
+ import { CoreWallet } from './CoreWallet.js';
3
+ import { Simulator, SimulatorState } from './Simulator.js';
4
+ import { WalletError } from './WalletError.js';
5
+ import { TransactionHistoryService } from './TransactionHistory.js';
6
+ import { WalletSyncUpdate } from './SyncSchema.js';
7
+ export interface SyncService<TState, TUpdate> {
8
+ updates: (state: TState) => Stream.Stream<TUpdate, WalletError, Scope.Scope>;
9
+ }
10
+ export interface SyncCapability<TState, TUpdate> {
11
+ applyUpdate: (state: TState, update: TUpdate) => Either.Either<TState, WalletError>;
12
+ }
13
+ export type IndexerClientConnection = {
14
+ indexerHttpUrl: string;
15
+ indexerWsUrl?: string;
16
+ };
17
+ export type DefaultSyncConfiguration = {
18
+ indexerClientConnection: IndexerClientConnection;
19
+ };
20
+ export type DefaultSyncContext = {
21
+ transactionHistoryService: TransactionHistoryService<WalletSyncUpdate>;
22
+ };
23
+ export declare const makeDefaultSyncService: (config: DefaultSyncConfiguration) => SyncService<CoreWallet, WalletSyncUpdate>;
24
+ export declare const makeDefaultSyncCapability: (_config: DefaultSyncConfiguration, getContext: () => DefaultSyncContext) => SyncCapability<CoreWallet, WalletSyncUpdate>;
25
+ export type SimulatorSyncConfiguration = {
26
+ simulator: Simulator;
27
+ };
28
+ export type SimulatorSyncUpdate = {
29
+ update: SimulatorState;
30
+ };
31
+ export declare const makeSimulatorSyncService: (config: SimulatorSyncConfiguration) => SyncService<CoreWallet, SimulatorSyncUpdate>;
32
+ export declare const makeSimulatorSyncCapability: () => SyncCapability<CoreWallet, SimulatorSyncUpdate>;
@@ -0,0 +1,85 @@
1
+ // This file is part of MIDNIGHT-WALLET-SDK.
2
+ // Copyright (C) 2025 Midnight Foundation
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // You may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+ import { Stream, Schema, pipe, Either } from 'effect';
14
+ import { CoreWallet } from './CoreWallet.js';
15
+ import { UnshieldedTransactions } from '@midnight-ntwrk/wallet-sdk-indexer-client';
16
+ import { WsSubscriptionClient, ConnectionHelper } from '@midnight-ntwrk/wallet-sdk-indexer-client/effect';
17
+ import { SyncWalletError } from './WalletError.js';
18
+ import { WsURL } from '@midnight-ntwrk/wallet-sdk-utilities/networking';
19
+ import { EitherOps } from '@midnight-ntwrk/wallet-sdk-utilities';
20
+ import { WalletSyncUpdateSchema } from './SyncSchema.js';
21
+ export const makeDefaultSyncService = (config) => {
22
+ return {
23
+ updates: (state) => {
24
+ const { indexerClientConnection } = config;
25
+ const webSocketUrlResult = ConnectionHelper.createWebSocketUrl(indexerClientConnection.indexerHttpUrl, indexerClientConnection.indexerWsUrl);
26
+ if (Either.isLeft(webSocketUrlResult)) {
27
+ return Stream.fail(new SyncWalletError(new Error(`Could not derive WebSocket URL from indexer HTTP URL: ${webSocketUrlResult.left.message}`)));
28
+ }
29
+ const indexerWsUrlResult = WsURL.make(webSocketUrlResult.right);
30
+ if (Either.isLeft(indexerWsUrlResult)) {
31
+ return Stream.fail(new SyncWalletError(new Error(`Invalid indexer WS URL: ${indexerWsUrlResult.left.message}`)));
32
+ }
33
+ const indexerWsUrl = indexerWsUrlResult.right;
34
+ const { appliedId } = state.progress;
35
+ const { address } = state.publicKey;
36
+ return pipe(UnshieldedTransactions.run({ address, transactionId: Number(appliedId) }), Stream.provideLayer(WsSubscriptionClient.layer({ url: indexerWsUrl })), Stream.mapError((error) => new SyncWalletError(error)), Stream.mapEffect((subscription) => {
37
+ const { unshieldedTransactions } = subscription;
38
+ return pipe(Schema.decodeUnknownEither(WalletSyncUpdateSchema)(unshieldedTransactions), Either.mapLeft((err) => new SyncWalletError(err)), EitherOps.toEffect);
39
+ }));
40
+ },
41
+ };
42
+ };
43
+ export const makeDefaultSyncCapability = (_config, getContext) => {
44
+ return {
45
+ applyUpdate: (state, update) => {
46
+ if (update.type === 'UnshieldedTransactionsProgress') {
47
+ return Either.right(CoreWallet.updateProgress(state, {
48
+ highestTransactionId: BigInt(update.highestTransactionId),
49
+ isConnected: true,
50
+ }));
51
+ }
52
+ else {
53
+ const updatePayload = {
54
+ createdUtxos: update.createdUtxos,
55
+ spentUtxos: update.spentUtxos,
56
+ status: update.status,
57
+ };
58
+ const stateAfterApplyingUpdate = update.status === 'FAILURE'
59
+ ? CoreWallet.applyFailedUpdate(state, updatePayload)
60
+ : CoreWallet.applyUpdate(state, updatePayload);
61
+ return stateAfterApplyingUpdate.pipe(Either.map((wallet) => {
62
+ const stateAfterUpdatingProgress = CoreWallet.updateProgress(wallet, {
63
+ appliedId: BigInt(update.transaction.id),
64
+ });
65
+ const { transactionHistoryService } = getContext();
66
+ void transactionHistoryService.create(update);
67
+ return stateAfterUpdatingProgress;
68
+ }));
69
+ }
70
+ },
71
+ };
72
+ };
73
+ export const makeSimulatorSyncService = (config) => {
74
+ return {
75
+ updates: (_state) => config.simulator.state$.pipe(Stream.map((state) => ({ update: state }))),
76
+ };
77
+ };
78
+ export const makeSimulatorSyncCapability = () => {
79
+ return {
80
+ applyUpdate: (state, _update) => {
81
+ return Either.right(state);
82
+ // return CoreWallet.replayEvents(state, secretKeys, events);
83
+ },
84
+ };
85
+ };
@@ -0,0 +1,18 @@
1
+ export interface SyncProgressData {
2
+ readonly appliedId: bigint;
3
+ readonly highestTransactionId: bigint;
4
+ readonly isConnected: boolean;
5
+ }
6
+ export interface SyncProgressOps {
7
+ isCompleteWithin(data: SyncProgressData, maxGap?: bigint): boolean;
8
+ }
9
+ export interface SyncProgress extends SyncProgressData {
10
+ isStrictlyComplete(): boolean;
11
+ isCompleteWithin(maxGap?: bigint): boolean;
12
+ }
13
+ export declare const SyncProgress: SyncProgressOps;
14
+ export declare const createSyncProgress: (params?: {
15
+ appliedId?: bigint;
16
+ highestTransactionId?: bigint;
17
+ isConnected?: boolean;
18
+ }) => SyncProgress;
@@ -0,0 +1,23 @@
1
+ export const SyncProgress = {
2
+ isCompleteWithin(data, maxGap = 50n) {
3
+ const applyLag = BigInt(Math.abs(Number(data.highestTransactionId - data.appliedId)));
4
+ return data.isConnected && applyLag <= maxGap;
5
+ },
6
+ };
7
+ export const createSyncProgress = (params = {}) => {
8
+ const { appliedId = 0n, highestTransactionId = 0n, isConnected = false } = params;
9
+ const data = {
10
+ appliedId,
11
+ highestTransactionId,
12
+ isConnected,
13
+ };
14
+ return {
15
+ ...data,
16
+ isStrictlyComplete() {
17
+ return SyncProgress.isCompleteWithin(this, 0n);
18
+ },
19
+ isCompleteWithin(maxGap) {
20
+ return SyncProgress.isCompleteWithin(this, maxGap);
21
+ },
22
+ };
23
+ };