@midnight-ntwrk/wallet-sdk-shielded 2.1.0 → 3.0.1

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.
@@ -1,103 +0,0 @@
1
- // This file is part of MIDNIGHT-WALLET-SDK.
2
- // Copyright (C) Midnight Foundation
3
- // SPDX-License-Identifier: Apache-2.0
4
- // Licensed under the Apache License, Version 2.0 (the "License");
5
- // You may not use this file except in compliance with the License.
6
- // You may obtain a copy of the License at
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- // Unless required by applicable law or agreed to in writing, software
9
- // distributed under the License is distributed on an "AS IS" BASIS,
10
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- // See the License for the specific language governing permissions and
12
- // limitations under the License.
13
- import * as ledger from '@midnight-ntwrk/ledger-v8';
14
- import { 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
- lastBlockTime: 1n,
37
- })));
38
- static init(genesisMints) {
39
- const emptyState = ledger.LedgerState.blank(NetworkId.NetworkId.Undeployed);
40
- const noStrictness = wellFormedStrictness();
41
- const makeTransactions = (context) => Effect.gen(function* () {
42
- const nowMillis = yield* Clock.currentTimeMillis;
43
- const verificationTime = new Date(nowMillis);
44
- const tx = pipe(genesisMints, Arr.map((transfer) => {
45
- const coin = ledger.createShieldedCoinInfo(transfer.type, transfer.amount);
46
- const output = ledger.ZswapOutput.new(coin, 0, transfer.recipient.coinPublicKey, transfer.recipient.encryptionPublicKey);
47
- return ledger.ZswapOffer.fromOutput(output, transfer.type, transfer.amount);
48
- }), ArrayOps.fold((acc, offer) => acc.merge(offer)), (offer) => ledger.Transaction.fromParts(NetworkId.NetworkId.Undeployed, offer).eraseProofs(), (tx) => tx.wellFormed(emptyState, noStrictness, verificationTime));
49
- const [initialState, initialResult] = emptyState.apply(tx, new ledger.TransactionContext(emptyState, context));
50
- const postBlockUpdateState = initialState.postBlockUpdate(verificationTime);
51
- return {
52
- initialResult,
53
- initialState: postBlockUpdateState,
54
- tx,
55
- };
56
- });
57
- return Effect.gen(function* () {
58
- const context = yield* Simulator.nextBlockContext(0n);
59
- const init = yield* makeTransactions(context);
60
- const initialState = {
61
- ledger: init.initialState,
62
- lastTxResult: init.initialResult,
63
- lastTxNumber: 0n,
64
- };
65
- const ref = yield* SubscriptionRef.make(initialState);
66
- const changesStream = yield* Stream.share(ref.changes, {
67
- capacity: 'unbounded',
68
- replay: Number.MAX_SAFE_INTEGER,
69
- });
70
- yield* pipe(changesStream, Stream.runDrain, Effect.forkScoped);
71
- return new Simulator(ref, changesStream);
72
- });
73
- }
74
- #stateRef;
75
- state$;
76
- constructor(stateRef, state$) {
77
- this.#stateRef = stateRef;
78
- this.state$ = state$;
79
- }
80
- submitTransaction(tx) {
81
- return pipe(this.#stateRef, SubscriptionRef.modifyEffect((simulatorState) => Effect.gen(function* () {
82
- const nextNumber = simulatorState.lastTxNumber + 1n;
83
- const context = yield* Simulator.nextBlockContext(nextNumber);
84
- const nowMillis = yield* Clock.currentTimeMillis;
85
- const verificationTime = new Date(nowMillis);
86
- const noStrictness = wellFormedStrictness();
87
- const verifiedTx = tx.wellFormed(simulatorState.ledger, noStrictness, verificationTime);
88
- const [newState, result] = simulatorState.ledger.apply(verifiedTx, new ledger.TransactionContext(simulatorState.ledger, context));
89
- const postBlockUpdatedState = newState.postBlockUpdate(verificationTime);
90
- const newSimulatorState = {
91
- ...simulatorState,
92
- ledger: postBlockUpdatedState,
93
- lastTxResult: result,
94
- lastTxNumber: nextNumber,
95
- };
96
- const output = {
97
- blockNumber: nextNumber,
98
- blockHash: context.parentBlockHash,
99
- };
100
- return [output, newSimulatorState];
101
- })));
102
- }
103
- }