@midnight-ntwrk/wallet-sdk-unshielded-wallet 1.0.0-beta.13 → 1.0.0-beta.15

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 +5 -5
  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
@@ -1,137 +0,0 @@
1
- import { MidnightBech32m, UnshieldedAddress } from '@midnight-ntwrk/wallet-sdk-address-format';
2
- import { UnshieldedStateDecoder, UnshieldedStateService } from '@midnight-ntwrk/wallet-sdk-unshielded-state';
3
- import { ObservableOps } from '@midnight-ntwrk/wallet-sdk-utilities';
4
- import { Deferred, Effect, Either, Fiber, Layer, pipe, Stream } from 'effect';
5
- import { StateImpl } from './State.js';
6
- import { SyncService } from './SyncService.js';
7
- import { TransactionHistoryService } from './TransactionHistoryService.js';
8
- import { TransactionService } from './TransactionService.js';
9
- import { NoOpTransactionHistoryStorage } from './tx-history-storage/NoOpTransactionHistoryStorage.js';
10
- const makeWallet = ({ publicKey, networkId, txHistoryStorage, }) => Effect.gen(function* () {
11
- const syncService = yield* SyncService;
12
- const transactionHistoryService = yield* TransactionHistoryService;
13
- const unshieldedState = yield* UnshieldedStateService;
14
- const transactionService = yield* TransactionService;
15
- // TODO: Scope would be a preferred way to handle controlled stop
16
- const stopLatch = yield* Deferred.make();
17
- const bech32mAddress = UnshieldedAddress.codec.encode(networkId, publicKey.address);
18
- const state = new StateImpl(unshieldedState, bech32mAddress.asString());
19
- const applyUpdate = (update) => Effect.gen(function* () {
20
- const { type } = update;
21
- if (type === 'UnshieldedTransaction') {
22
- const { transaction } = update;
23
- yield* unshieldedState.applyTx(update.transaction);
24
- yield* transactionHistoryService.create({
25
- id: transaction.id,
26
- hash: transaction.hash,
27
- protocolVersion: transaction.protocolVersion,
28
- identifiers: transaction.identifiers ?? [],
29
- transactionResult: transaction.transactionResult
30
- ? {
31
- status: transaction.transactionResult.status,
32
- segments: transaction.transactionResult.segments ?? [],
33
- }
34
- : null,
35
- });
36
- }
37
- if (type === 'UnshieldedTransactionsProgress') {
38
- yield* unshieldedState.updateSyncProgress(update.highestTransactionId);
39
- }
40
- });
41
- const start = () => Effect.gen(function* () {
42
- const latestState = yield* unshieldedState.getLatestState();
43
- const fiber = yield* pipe(syncService.startSync(bech32mAddress.asString(), latestState.syncProgress?.highestTransactionId ?? 0), Stream.tapError((error) => Effect.gen(function* () {
44
- // eslint-disable-next-line no-console
45
- yield* Effect.sync(() => console.error(error));
46
- yield* Deferred.die(stopLatch, error);
47
- })), Stream.runForEach(applyUpdate), Effect.fork);
48
- yield* Deferred.await(stopLatch).pipe(Effect.andThen(Fiber.interrupt(fiber)));
49
- });
50
- const stop = () => Deferred.succeed(stopLatch, undefined).pipe(Effect.asVoid);
51
- const transferTransaction = (outputs, ttl) => Effect.gen(function* () {
52
- const latestState = yield* unshieldedState.getLatestState();
53
- if (!latestState.syncProgress) {
54
- return yield* Effect.fail('Unable to get the latest block number');
55
- }
56
- const mappedOutputs = outputs.map((output) => ({
57
- ...output,
58
- receiverAddress: UnshieldedAddress.codec
59
- .decode(networkId, MidnightBech32m.parse(output.receiverAddress))
60
- .data.toString('hex'),
61
- }));
62
- const transaction = yield* transactionService.transferTransaction(mappedOutputs, ttl, networkId);
63
- return (yield* transactionService.balanceTransaction(transaction, unshieldedState, publicKey.address.hexString, publicKey.publicKey));
64
- });
65
- const initSwap = (desiredInputs, outputs, ttl) => Effect.gen(function* () {
66
- const latestState = yield* unshieldedState.getLatestState();
67
- if (!latestState.syncProgress) {
68
- return yield* Effect.fail('Unable to get the latest block number');
69
- }
70
- const mappedOutputs = outputs.map((output) => ({
71
- ...output,
72
- receiverAddress: UnshieldedAddress.codec
73
- .decode(networkId, MidnightBech32m.parse(output.receiverAddress))
74
- .data.toString('hex'),
75
- }));
76
- return yield* transactionService.initSwap(desiredInputs, mappedOutputs, ttl, networkId, unshieldedState, publicKey.address.hexString, publicKey.publicKey);
77
- });
78
- const balanceTransaction = (tx) => transactionService.balanceTransaction(tx, unshieldedState, publicKey.address.hexString, publicKey.publicKey);
79
- const signTransaction = (tx, signSegment) => Effect.gen(function* () {
80
- const segments = transactionService.getSegments(tx);
81
- if (!segments.length) {
82
- return yield* Effect.fail('No segments found in the provided transaction');
83
- }
84
- for (const segment of segments) {
85
- const data = yield* transactionService.getOfferSignatureData(tx, segment);
86
- const signature = yield* signSegment(data);
87
- tx = yield* transactionService.addOfferSignature(tx, signature, segment);
88
- }
89
- // return yield* transactionService.bindTransaction(tx);
90
- return tx;
91
- });
92
- const transactionHistory = txHistoryStorage
93
- ? {
94
- get: (hash) => Effect.runPromise(transactionHistoryService.get(hash)),
95
- getAll: () => ObservableOps.fromStream(transactionHistoryService.getAll()),
96
- changes: () => ObservableOps.fromStream(transactionHistoryService.changes),
97
- }
98
- : undefined;
99
- const result = {
100
- state: () => ObservableOps.fromStream(state.updates()),
101
- start: () => {
102
- return new Promise((resolve) => {
103
- Effect.runFork(Effect.scoped(start()));
104
- resolve(void 0);
105
- });
106
- },
107
- stop: () => Effect.runPromise(stop()),
108
- transferTransaction: (outputs, ttl) => Effect.runPromise(transferTransaction(outputs, ttl)),
109
- initSwap: (desiredInputs, desiredOutputs, ttl) => Effect.runPromise(initSwap(desiredInputs, desiredOutputs, ttl)),
110
- balanceTransaction: (tx) => Effect.runPromise(balanceTransaction(tx)),
111
- signTransaction: (tx, signSegment) => Effect.runPromise(signTransaction(tx, (data) => Effect.try(() => signSegment(data)))),
112
- serializeState: () => Effect.runPromise(state.serialize()),
113
- transactionHistory,
114
- };
115
- return result;
116
- });
117
- export class WalletBuilder {
118
- static async build({ publicKey, networkId, indexerUrl, txHistoryStorage }) {
119
- const txHistoryService = TransactionHistoryService.Live(txHistoryStorage ? txHistoryStorage : new NoOpTransactionHistoryStorage());
120
- const layers = Layer.mergeAll(SyncService.LiveWithIndexer(indexerUrl), UnshieldedStateService.Live(), TransactionService.Live, txHistoryService);
121
- const walletService = makeWallet({ publicKey, networkId, txHistoryStorage });
122
- const wallet = walletService.pipe(Effect.provide(layers));
123
- return Effect.runPromise(wallet);
124
- }
125
- static async restore({ publicKey, networkId, indexerUrl, serializedState, txHistoryStorage, }) {
126
- const parsedState = JSON.parse(serializedState);
127
- const decodedState = UnshieldedStateDecoder(parsedState);
128
- if (Either.isLeft(decodedState)) {
129
- throw new Error(`Failed to decode unshielded state: ${decodedState.left.message}`);
130
- }
131
- const txHistoryService = TransactionHistoryService.Live(txHistoryStorage ? txHistoryStorage : new NoOpTransactionHistoryStorage());
132
- const layer = Layer.mergeAll(SyncService.LiveWithIndexer(indexerUrl), UnshieldedStateService.LiveWithState(decodedState.right), TransactionService.Live, txHistoryService);
133
- const walletService = makeWallet({ publicKey, networkId, txHistoryStorage });
134
- const wallet = walletService.pipe(Effect.provide(layer));
135
- return Effect.runPromise(wallet);
136
- }
137
- }