@thesingularitynetwork/darkswap-sdk 0.1.18 → 0.1.19

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 (83) hide show
  1. package/dist/test/utils/helpers.d.ts +9 -0
  2. package/package.json +56 -53
  3. package/src/abis/DarkSwapAssetManager.json +0 -1162
  4. package/src/abis/DarkSwapFeeAssetManager.json +0 -282
  5. package/src/abis/IERC20.json +0 -194
  6. package/src/abis/IERC20_USDT.json +0 -188
  7. package/src/abis/MerkleTreeOperator.json +0 -533
  8. package/src/aztec/bigint-buffer/index.ts +0 -87
  9. package/src/aztec/collection/object.ts +0 -76
  10. package/src/aztec/crypto/poseidon/index.ts +0 -18
  11. package/src/aztec/crypto/schnorr/index.ts +0 -40
  12. package/src/aztec/crypto/schnorr/signature.ts +0 -107
  13. package/src/aztec/crypto/serialize.ts +0 -85
  14. package/src/aztec/crypto/signature/index.ts +0 -17
  15. package/src/aztec/fields/fields.ts +0 -361
  16. package/src/aztec/fields/point.ts +0 -208
  17. package/src/aztec/serialize/buffer_reader.ts +0 -402
  18. package/src/aztec/serialize/field_reader.ts +0 -154
  19. package/src/aztec/serialize/free_funcs.ts +0 -193
  20. package/src/aztec/serialize/serialize.ts +0 -336
  21. package/src/aztec/serialize/types.ts +0 -19
  22. package/src/aztec/string/index.ts +0 -39
  23. package/src/circuits/pro/dark_swap_cancel_order_compiled_circuit.json +0 -1
  24. package/src/circuits/pro/dark_swap_deposit_compiled_circuit.json +0 -1
  25. package/src/circuits/pro/dark_swap_join_compiled_circuit.json +0 -1
  26. package/src/circuits/pro/dark_swap_pro_create_order_compiled_circuit.json +0 -1
  27. package/src/circuits/pro/dark_swap_pro_swap_compiled_circuit.json +0 -1
  28. package/src/circuits/pro/dark_swap_triple_join_compiled_circuit.json +0 -1
  29. package/src/circuits/pro/dark_swap_withdraw_compiled_circuit.json +0 -1
  30. package/src/circuits/retail/dark_swap_cancel_order_withdraw_compiled_circuit.json +0 -1
  31. package/src/circuits/retail/dark_swap_retail_deposit_create_order_compiled_circuit.json +0 -1
  32. package/src/circuits/retail/dark_swap_retail_swap_compiled_circuit.json +0 -1
  33. package/src/config/chain.ts +0 -9
  34. package/src/config/config.ts +0 -9
  35. package/src/config/contractConfig.ts +0 -61
  36. package/src/config/index.ts +0 -3
  37. package/src/darkSwap.ts +0 -31
  38. package/src/entities/error.ts +0 -6
  39. package/src/entities/index.ts +0 -3
  40. package/src/entities/token.ts +0 -9
  41. package/src/entities/types.ts +0 -6
  42. package/src/index.ts +0 -9
  43. package/src/proof/baseProofService.ts +0 -34
  44. package/src/proof/basic/depositProof.ts +0 -101
  45. package/src/proof/basic/joinProof.ts +0 -112
  46. package/src/proof/basic/tripleJoinProof.ts +0 -130
  47. package/src/proof/basic/withdrawProof.ts +0 -97
  48. package/src/proof/keyService.ts +0 -9
  49. package/src/proof/noteService.ts +0 -114
  50. package/src/proof/pro/orders/cancelOrderProof.ts +0 -126
  51. package/src/proof/pro/orders/createOrderProof.ts +0 -136
  52. package/src/proof/pro/orders/swapProof.ts +0 -189
  53. package/src/proof/retail/cancelOrderProof.ts +0 -78
  54. package/src/proof/retail/depositOrderProof.ts +0 -133
  55. package/src/proof/retail/swapProof.ts +0 -158
  56. package/src/services/BaseService.ts +0 -49
  57. package/src/services/EventService.ts +0 -17
  58. package/src/services/agent/index.ts +0 -1
  59. package/src/services/agent/retailSwap.ts +0 -116
  60. package/src/services/base/deposit.ts +0 -174
  61. package/src/services/base/index.ts +0 -4
  62. package/src/services/base/join.ts +0 -135
  63. package/src/services/base/tripleJoin.ts +0 -161
  64. package/src/services/base/withdraw.ts +0 -123
  65. package/src/services/feeRatioService.ts +0 -13
  66. package/src/services/index.ts +0 -7
  67. package/src/services/merkletree.ts +0 -46
  68. package/src/services/noteService.ts +0 -75
  69. package/src/services/pro/cancelOrder.ts +0 -142
  70. package/src/services/pro/createOrder.ts +0 -183
  71. package/src/services/pro/index.ts +0 -3
  72. package/src/services/pro/proSwap.ts +0 -199
  73. package/src/services/retail/cancelAndWithdrawOrder.ts +0 -95
  74. package/src/services/retail/depositAndCreateOrder.ts +0 -150
  75. package/src/services/retail/index.ts +0 -2
  76. package/src/types.ts +0 -76
  77. package/src/utils/constants.ts +0 -3
  78. package/src/utils/encoders.ts +0 -10
  79. package/src/utils/formatters.ts +0 -11
  80. package/src/utils/mimc.ts +0 -143
  81. package/src/utils/proofUtils.ts +0 -18
  82. package/src/utils/swapUtils.ts +0 -55
  83. package/src/utils/util.ts +0 -21
@@ -1,133 +0,0 @@
1
- import { BaseProofInput, BaseProofParam, BaseProofResult, DarkSwapMessage, DarkSwapNote, DarkSwapOrderNote, DarkSwapProofError, PROOF_DOMAIN } from "../../types";
2
- import { encodeAddress } from "../../utils/encoders";
3
- import { bn_to_0xhex } from "../../utils/formatters";
4
- import { bn_to_hex } from "../../utils/formatters";
5
- import { mimc_bn254 } from "../../utils/mimc";
6
- import { signatureToHexString, uint8ArrayToNumberArray } from "../../utils/proofUtils";
7
- import { generateProof, signMessage } from "../baseProofService";
8
- import { generateKeyPair } from "../keyService";
9
- import { calcNullifier, getNoteFooter } from "../noteService";
10
- import retailCreateOrderCircuit from "../../circuits/retail/dark_swap_retail_deposit_create_order_compiled_circuit.json";
11
- import { Fr } from "../../aztec/fields/fields";
12
-
13
- type RetailCreateOrderProofInput = BaseProofInput & {
14
- deposit_out_note: string,
15
- deposit_out_nullifier: string,
16
- deposit_out_note_footer: string,
17
- deposit_out_rho: string,
18
-
19
- //order
20
- out_asset: string,
21
- out_amount: string,
22
- in_asset: string,
23
- in_amount: string,
24
-
25
- //fee
26
- fee_ratio: string,
27
- fee_amount: string,
28
-
29
- //swap in
30
- in_note: string,
31
- in_note_footer: string,
32
- in_rho: string,
33
- }
34
-
35
- export type RetailCreateOrderProofParam = BaseProofParam & {
36
- depositNote: DarkSwapOrderNote,
37
- swapInNote: DarkSwapNote,
38
- feeAmount: bigint
39
- }
40
-
41
- export type RetailCreateOrderProofResult = BaseProofResult & {
42
- depositNullifier: string,
43
- depositFooter: string,
44
- swapInNoteFooter: string,
45
- }
46
-
47
- export async function generateRetailSwapMessage(
48
- address: string,
49
- orderNote: DarkSwapOrderNote,
50
- swapInNote: DarkSwapNote,
51
- feeAmount: bigint,
52
- pubKey: [Fr, Fr],
53
- privKey: Fr
54
- ): Promise<DarkSwapMessage> {
55
-
56
- const addressMod = encodeAddress(address);
57
- const orderNoteNullifier = calcNullifier(orderNote.rho, pubKey);
58
- const message = bn_to_hex(mimc_bn254([
59
- BigInt(PROOF_DOMAIN.RETAIL_CREATE_ORDER),
60
- addressMod,
61
- orderNoteNullifier,
62
- orderNote.feeRatio,
63
- swapInNote.note,
64
- ]));
65
- const signature = await signMessage(message, privKey);
66
-
67
- return {
68
- address: address,
69
- orderNote: orderNote,
70
- orderNullifier: bn_to_0xhex(orderNoteNullifier),
71
- inNote: swapInNote,
72
- feeAmount: feeAmount,
73
- publicKey: pubKey,
74
- signature: signatureToHexString(signature),
75
- }
76
- }
77
-
78
- export async function generateRetailCreateOrderProof(param: RetailCreateOrderProofParam): Promise<RetailCreateOrderProofResult> {
79
- if (param.depositNote.amount <= 0n) {
80
- throw new DarkSwapProofError("Deposit amount must be greater than 0");
81
- }
82
-
83
- if (param.depositNote.feeRatio < 0n) {
84
- throw new DarkSwapProofError("Fee ratio must be greater or equal to 0");
85
- }
86
-
87
- const [[fuzkPubKeyX, fuzkPubKeyY], fuzkPriKey] = await generateKeyPair(param.signedMessage);
88
-
89
- const depositNullifier = calcNullifier(param.depositNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
90
- const depositFooter = getNoteFooter(param.depositNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
91
- const inAmount = param.feeAmount + param.swapInNote.amount;
92
-
93
- const swapInNoteFooter = getNoteFooter(param.swapInNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
94
-
95
- const addressMod = encodeAddress(param.address);
96
- const message = bn_to_hex(mimc_bn254([
97
- BigInt(PROOF_DOMAIN.RETAIL_CREATE_ORDER),
98
- addressMod,
99
- param.depositNote.note,
100
- param.depositNote.feeRatio,
101
- param.swapInNote.note,
102
- ]));
103
- const signature = await signMessage(message, fuzkPriKey);
104
-
105
- const inputs: RetailCreateOrderProofInput = {
106
- address: bn_to_0xhex(addressMod),
107
- deposit_out_note: bn_to_0xhex(param.depositNote.note),
108
- deposit_out_nullifier: bn_to_0xhex(depositNullifier),
109
- deposit_out_note_footer: bn_to_0xhex(depositFooter),
110
- deposit_out_rho: bn_to_0xhex(param.depositNote.rho),
111
-
112
- out_asset: bn_to_0xhex(encodeAddress(param.depositNote.asset)),
113
- out_amount: bn_to_0xhex(param.depositNote.amount),
114
- in_asset: bn_to_0xhex(encodeAddress(param.swapInNote.asset)),
115
- in_amount: bn_to_0xhex(inAmount),
116
-
117
- in_note: bn_to_0xhex(param.swapInNote.note),
118
- in_note_footer: bn_to_0xhex(swapInNoteFooter),
119
- in_rho: bn_to_0xhex(param.swapInNote.rho),
120
- fee_ratio: bn_to_0xhex(param.depositNote.feeRatio),
121
- fee_amount: bn_to_0xhex(param.feeAmount),
122
-
123
- pub_key: [fuzkPubKeyX.toString(), fuzkPubKeyY.toString()],
124
- signature: uint8ArrayToNumberArray(signature),
125
- };
126
- const proof = await generateProof(retailCreateOrderCircuit, inputs);
127
- return {
128
- ...proof,
129
- depositNullifier: inputs.deposit_out_nullifier,
130
- depositFooter: inputs.deposit_out_note_footer,
131
- swapInNoteFooter: inputs.in_note_footer,
132
- }
133
- };
@@ -1,158 +0,0 @@
1
- import retailSwapCircuit from "../../circuits/retail/dark_swap_retail_swap_compiled_circuit.json";
2
- import { BaseProofResult, DarkSwapMessage, DarkSwapProofError, FEE_RATIO_PRECISION } from "../../types";
3
- import { encodeAddress } from "../../utils/encoders";
4
- import { bn_to_0xhex } from "../../utils/formatters";
5
- import { hexStringToSignature, uint8ArrayToNumberArray } from "../../utils/proofUtils";
6
- import { generateProof } from "../baseProofService";
7
- import { calcNullifier, getNoteFooter } from "../noteService";
8
-
9
- type RetailSwapProofInput = {
10
- merkle_root: string,
11
-
12
- // Alice input
13
- alice_merkle_index: number[],
14
- alice_merkle_path: string[],
15
- alice_address: string,
16
- alice_out_note: string,
17
- alice_out_rho: string,
18
- alice_out_nullifier: string,
19
-
20
- //Alice fee
21
- alice_fee_ratio: string,
22
- alice_fee_amount: string,
23
-
24
- // Alice output
25
- alice_in_note: string,
26
- alice_in_rho: string,
27
- alice_in_note_footer: string,
28
-
29
- // Alice pub key and signature
30
- alice_pub_key: string[],
31
- alice_signature: any,
32
-
33
- //order
34
- alice_out_asset: string,
35
- alice_out_amount: string,
36
- alice_in_asset: string,
37
- alice_in_amount: string,
38
-
39
- // Bob input
40
- bob_merkle_index: number[],
41
- bob_merkle_path: string[],
42
- bob_address: string,
43
- bob_out_note: string,
44
- bob_out_rho: string,
45
- bob_out_nullifier: string,
46
-
47
- //bob fee
48
- bob_fee_ratio: string,
49
- bob_fee_amount: string,
50
-
51
- // Bob output
52
- bob_in_note: string,
53
- bob_in_rho: string,
54
- bob_in_note_footer: string,
55
-
56
- // Bob pub key and signature
57
- bob_pub_key: string[],
58
- bob_signature: any,
59
- }
60
-
61
- export type RetailSwapProofParam = {
62
- merkleRoot: string,
63
- aliceMerkleIndex: number[],
64
- aliceMerklePath: string[],
65
- aliceMessage: DarkSwapMessage,
66
-
67
- bobMerkleIndex: number[],
68
- bobMerklePath: string[],
69
- bobMessage: DarkSwapMessage,
70
- }
71
-
72
- export type RetailSwapProofResult = BaseProofResult & {
73
- aliceOrderNullifier: string,
74
- aliceInNoteFooter: string,
75
- bobOrderNullifier: string,
76
- bobInNoteFooter: string,
77
- }
78
-
79
- export async function generateRetailSwapProof(param: RetailSwapProofParam): Promise<RetailSwapProofResult> {
80
- if (param.aliceMessage.orderNote.feeRatio < 0n
81
- || param.bobMessage.orderNote.feeRatio < 0n) {
82
- throw new DarkSwapProofError("Invalid fee ratio");
83
- }
84
-
85
- if (param.aliceMessage.inNote.amount <= 0n
86
- || param.aliceMessage.orderNote.amount <= 0n
87
- || param.aliceMessage.inNote.amount <= 0n
88
- || param.bobMessage.inNote.amount <= 0n
89
- || param.bobMessage.orderNote.amount <= 0n) {
90
- throw new DarkSwapProofError("Invalid note amount");
91
- }
92
-
93
- if (param.aliceMessage.orderNote.amount != param.bobMessage.inNote.amount
94
- || param.bobMessage.orderNote.amount != param.aliceMessage.inNote.amount) {
95
- throw new DarkSwapProofError("Invalid order amount");
96
- }
97
-
98
- const aliceFeeAmount = param.aliceMessage.inNote.amount * param.aliceMessage.orderNote.feeRatio / FEE_RATIO_PRECISION;
99
- const bobFeeAmount = param.bobMessage.inNote.amount * param.bobMessage.orderNote.feeRatio / FEE_RATIO_PRECISION;
100
-
101
- const aliceOrderNoteNullifier = calcNullifier(param.aliceMessage.orderNote.rho, param.aliceMessage.publicKey);
102
- const aliceInNoteFooter = getNoteFooter(param.aliceMessage.inNote.rho, param.aliceMessage.publicKey);
103
- const bobOrderNoteNullifier = calcNullifier(param.bobMessage.orderNote.rho, param.bobMessage.publicKey);
104
- const bobInNoteFooter = getNoteFooter(param.bobMessage.inNote.rho, param.bobMessage.publicKey);
105
-
106
- const aliceAddressMod = encodeAddress(param.aliceMessage.address);
107
- const bobAddressMod = encodeAddress(param.bobMessage.address);
108
-
109
- const inputs: RetailSwapProofInput = {
110
- merkle_root: param.merkleRoot,
111
-
112
- alice_merkle_index: param.aliceMerkleIndex,
113
- alice_merkle_path: param.aliceMerklePath.map((x) => bn_to_0xhex(BigInt(x))),
114
- alice_address: bn_to_0xhex(aliceAddressMod),
115
-
116
- alice_out_rho: bn_to_0xhex(param.aliceMessage.orderNote.rho),
117
- alice_out_asset: bn_to_0xhex(encodeAddress(param.aliceMessage.orderNote.asset)),
118
- alice_out_amount: bn_to_0xhex(param.aliceMessage.orderNote.amount),
119
- alice_out_note: bn_to_0xhex(param.aliceMessage.orderNote.note),
120
- alice_out_nullifier: bn_to_0xhex(aliceOrderNoteNullifier),
121
- alice_fee_ratio: bn_to_0xhex(param.aliceMessage.orderNote.feeRatio),
122
- alice_fee_amount: bn_to_0xhex(aliceFeeAmount),
123
-
124
- alice_in_rho: bn_to_0xhex(param.aliceMessage.inNote.rho),
125
- alice_in_asset: bn_to_0xhex(encodeAddress(param.aliceMessage.inNote.asset)),
126
- alice_in_amount: bn_to_0xhex(param.aliceMessage.inNote.amount),
127
- alice_in_note: bn_to_0xhex(param.aliceMessage.inNote.note),
128
- alice_in_note_footer: bn_to_0xhex(aliceInNoteFooter),
129
-
130
- alice_pub_key: [param.aliceMessage.publicKey[0].toString(), param.aliceMessage.publicKey[1].toString()],
131
- alice_signature: uint8ArrayToNumberArray(hexStringToSignature(param.aliceMessage.signature)),
132
-
133
- bob_merkle_index: param.bobMerkleIndex,
134
- bob_merkle_path: param.bobMerklePath.map((x) => bn_to_0xhex(BigInt(x))),
135
- bob_address: bn_to_0xhex(bobAddressMod),
136
-
137
- bob_out_note: bn_to_0xhex(param.bobMessage.orderNote.note),
138
- bob_out_rho: bn_to_0xhex(param.bobMessage.orderNote.rho),
139
- bob_out_nullifier: bn_to_0xhex(bobOrderNoteNullifier),
140
- bob_fee_ratio: bn_to_0xhex(param.bobMessage.orderNote.feeRatio),
141
- bob_fee_amount: bn_to_0xhex(bobFeeAmount),
142
-
143
- bob_in_note: bn_to_0xhex(param.bobMessage.inNote.note),
144
- bob_in_rho: bn_to_0xhex(param.bobMessage.inNote.rho),
145
- bob_in_note_footer: bn_to_0xhex(bobInNoteFooter),
146
-
147
- bob_pub_key: [param.bobMessage.publicKey[0].toString(), param.bobMessage.publicKey[1].toString()],
148
- bob_signature: uint8ArrayToNumberArray(hexStringToSignature(param.bobMessage.signature)),
149
- };
150
- const proof = await generateProof(retailSwapCircuit, inputs);
151
- return {
152
- ...proof,
153
- aliceOrderNullifier: inputs.alice_out_nullifier,
154
- aliceInNoteFooter: inputs.alice_in_note_footer,
155
- bobOrderNullifier: inputs.bob_out_nullifier,
156
- bobInNoteFooter: inputs.bob_in_note_footer,
157
- }
158
- };
@@ -1,49 +0,0 @@
1
- import { DarkSwap } from '../darkSwap';
2
-
3
- export class BaseContext {
4
- private _address?: string;
5
- private _signature: string;
6
- private _merkleRoot?: string;
7
- private _tx?: string;
8
-
9
- constructor(signature: string) {
10
- this._signature = signature;
11
- }
12
-
13
- set address(address: string | undefined) {
14
- this._address = address;
15
- }
16
-
17
- get address(): string | undefined {
18
- return this._address;
19
- }
20
-
21
- get signature(): string {
22
- return this._signature;
23
- }
24
-
25
- set merkleRoot(merkleRoot: string | undefined) {
26
- this._merkleRoot = merkleRoot;
27
- }
28
-
29
- get merkleRoot(): string | undefined {
30
- return this._merkleRoot;
31
- }
32
-
33
- set tx(tx: string | undefined) {
34
- this._tx = tx;
35
- }
36
-
37
- get tx(): string | undefined {
38
- return this._tx;
39
- }
40
- }
41
-
42
-
43
- export abstract class BaseContractService {
44
- protected _darkSwap: DarkSwap;
45
-
46
- constructor(_darkSwap: DarkSwap) {
47
- this._darkSwap = _darkSwap;
48
- }
49
- }
@@ -1,17 +0,0 @@
1
- import { ethers } from 'ethers';
2
- import { DarkSwap } from '../darkSwap';
3
-
4
- export async function getOutEvent(tx: string, abi: any, eventTopic: string, darkSwap: DarkSwap) {
5
- const iface = new ethers.Interface(abi);
6
- const receipt = await darkSwap.provider.getTransactionReceipt(tx);
7
- if (receipt && receipt.logs.length > 0) {
8
- for (let i = 0; i < receipt.logs.length; i++) {
9
- const parsedLog = iface.parseLog(receipt.logs[i]);
10
- if (parsedLog && parsedLog.name == eventTopic) {
11
- return parsedLog;
12
- }
13
- }
14
- }
15
-
16
- return null;
17
- }
@@ -1 +0,0 @@
1
- export * from './retailSwap';
@@ -1,116 +0,0 @@
1
- import { ethers } from 'ethers';
2
- import DarkSwapAssetManagerAbi from '../../abis/DarkSwapAssetManager.json';
3
- import { DarkSwap } from '../../darkSwap';
4
- import { DarkSwapError } from '../../entities';
5
- import { generateRetailSwapProof, RetailSwapProofResult } from '../../proof/retail/swapProof';
6
- import { DarkSwapMessage } from '../../types';
7
- import { hexlify32 } from '../../utils/util';
8
- import { BaseContext, BaseContractService } from '../BaseService';
9
- import { multiGetMerklePathAndRoot } from '../merkletree';
10
-
11
- class RetailSwapContext extends BaseContext {
12
- private _aliceSwapMessage?: DarkSwapMessage;
13
- private _bobSwapMessage?: DarkSwapMessage;
14
- private _proof?: RetailSwapProofResult;
15
-
16
- constructor(signature: string) {
17
- super(signature);
18
- }
19
-
20
- set aliceSwapMessage(aliceSwapMessage: DarkSwapMessage | undefined) {
21
- this._aliceSwapMessage = aliceSwapMessage;
22
- }
23
-
24
- get aliceSwapMessage(): DarkSwapMessage | undefined {
25
- return this._aliceSwapMessage;
26
- }
27
-
28
- set bobSwapMessage(bobSwapMessage: DarkSwapMessage | undefined) {
29
- this._bobSwapMessage = bobSwapMessage;
30
- }
31
-
32
- get bobSwapMessage(): DarkSwapMessage | undefined {
33
- return this._bobSwapMessage;
34
- }
35
-
36
- set proof(proof: RetailSwapProofResult | undefined) {
37
- this._proof = proof;
38
- }
39
-
40
- get proof(): RetailSwapProofResult | undefined {
41
- return this._proof;
42
- }
43
- }
44
-
45
- export class RetailSwapService extends BaseContractService {
46
- constructor(_darkSwap: DarkSwap) {
47
- super(_darkSwap);
48
- }
49
-
50
- public async prepare(
51
- aliceSwapMessage: DarkSwapMessage,
52
- bobSwapMessage: DarkSwapMessage
53
- ): Promise<{ context: RetailSwapContext }> {
54
- const context = new RetailSwapContext(aliceSwapMessage.signature);
55
- context.aliceSwapMessage = aliceSwapMessage;
56
- context.bobSwapMessage = bobSwapMessage;
57
- return { context };
58
- }
59
-
60
- private async generateProof(context: RetailSwapContext): Promise<void> {
61
- if (!context
62
- || !context.aliceSwapMessage
63
- || !context.bobSwapMessage) {
64
- throw new DarkSwapError('Invalid context');
65
- }
66
-
67
- const merklePathes = await multiGetMerklePathAndRoot([context.aliceSwapMessage.orderNote.note, context.bobSwapMessage.orderNote.note], this._darkSwap);
68
- const aliceOrderNotePath = merklePathes[0];
69
- const bobOrderNotePath = merklePathes[1];
70
-
71
- const proof = await generateRetailSwapProof({
72
- merkleRoot: aliceOrderNotePath.root,
73
- aliceMerkleIndex: aliceOrderNotePath.index,
74
- aliceMerklePath: aliceOrderNotePath.path,
75
- aliceMessage: context.aliceSwapMessage,
76
- bobMerkleIndex: bobOrderNotePath.index,
77
- bobMerklePath: bobOrderNotePath.path,
78
- bobMessage: context.bobSwapMessage,
79
- });
80
- context.merkleRoot = aliceOrderNotePath.root;
81
- context.proof = proof;
82
- }
83
-
84
- public async execute(context: RetailSwapContext): Promise<string> {
85
- await this.generateProof(context);
86
- if (!context
87
- || !context.merkleRoot
88
- || !context.aliceSwapMessage
89
- || !context.bobSwapMessage
90
- || !context.proof) {
91
- throw new DarkSwapError('Invalid context');
92
- }
93
-
94
- const contract = new ethers.Contract(
95
- this._darkSwap.contracts.darkSwapAssetManager,
96
- DarkSwapAssetManagerAbi.abi,
97
- this._darkSwap.signer
98
- );
99
- const tx = await contract.retailSwap(
100
- [
101
- context.merkleRoot,
102
- hexlify32(context.aliceSwapMessage.orderNote.feeRatio),
103
- context.proof.aliceOrderNullifier,
104
- hexlify32(context.aliceSwapMessage.inNote.note),
105
- context.proof.aliceInNoteFooter,
106
- hexlify32(context.bobSwapMessage.orderNote.feeRatio),
107
- context.proof.bobOrderNullifier,
108
- hexlify32(context.bobSwapMessage.inNote.note),
109
- context.proof.bobInNoteFooter
110
- ],
111
- context.proof.proof
112
- );
113
- await tx.wait();
114
- return tx.hash;
115
- }
116
- }
@@ -1,174 +0,0 @@
1
- import { ethers } from 'ethers';
2
- import DarkSwapAssetManagerAbi from '../../abis/DarkSwapAssetManager.json';
3
- import ERC20Abi from '../../abis/IERC20.json';
4
- import ERC20_USDT from '../../abis/IERC20_USDT.json';
5
- import { legacyTokenConfig } from '../../config/config';
6
- import { DarkSwap } from '../../darkSwap';
7
- import { DarkSwapError } from '../../entities';
8
- import { DepositProofResult, generateDepositProof } from '../../proof/basic/depositProof';
9
- import { generateKeyPair } from '../../proof/keyService';
10
- import { createNote } from '../../proof/noteService';
11
- import { DarkSwapNote } from '../../types';
12
- import { MAX_ALLOWANCE } from '../../utils/constants';
13
- import { hexlify32, isNativeAsset } from '../../utils/util';
14
- import { BaseContext, BaseContractService } from '../BaseService';
15
- import { EMPTY_PATH, getMerklePathAndRoot } from '../merkletree';
16
-
17
- export class DepositContext extends BaseContext {
18
- private _currentBalance?: DarkSwapNote;
19
- private _newBalance?: DarkSwapNote;
20
- private _proof?: DepositProofResult;
21
- private _depositAmount?: bigint;
22
-
23
- constructor(signature: string) {
24
- super(signature);
25
- }
26
-
27
- set currentBalance(currentBalance: DarkSwapNote | undefined) {
28
- this._currentBalance = currentBalance;
29
- }
30
-
31
- get currentBalance(): DarkSwapNote | undefined {
32
- return this._currentBalance;
33
- }
34
-
35
- set newBalance(newBalance: DarkSwapNote | undefined) {
36
- this._newBalance = newBalance;
37
- }
38
-
39
- get newBalance(): DarkSwapNote | undefined {
40
- return this._newBalance;
41
- }
42
-
43
- set proof(proof: DepositProofResult | undefined) {
44
- this._proof = proof;
45
- }
46
-
47
- get proof(): DepositProofResult | undefined {
48
- return this._proof;
49
- }
50
-
51
- set depositAmount(depositAmount: bigint | undefined) {
52
- this._depositAmount = depositAmount;
53
- }
54
-
55
- get depositAmount(): bigint | undefined {
56
- return this._depositAmount;
57
- }
58
- }
59
-
60
- export class DepositService extends BaseContractService {
61
- constructor(_darkSwap: DarkSwap) {
62
- super(_darkSwap);
63
- }
64
-
65
- public async prepare(
66
- currentBalance: DarkSwapNote,
67
- depositAsset: string,
68
- depositAmount: bigint,
69
- walletAddress: string,
70
- signature: string,
71
- ): Promise<{ context: DepositContext; newBalanceNote: DarkSwapNote }> {
72
- const [pubKey] = await generateKeyPair(signature);
73
- const newBalanceAmount = depositAmount + currentBalance.amount;
74
- const newBalance = createNote(walletAddress, depositAsset, newBalanceAmount, pubKey);
75
- const context = new DepositContext(signature);
76
- context.currentBalance = currentBalance;
77
- context.newBalance = newBalance;
78
- context.address = walletAddress;
79
- context.depositAmount = depositAmount;
80
- return { context, newBalanceNote: newBalance };
81
- }
82
-
83
- private async generateProof(context: DepositContext): Promise<void> {
84
- if (!context || !context.currentBalance || !context.newBalance || !context.address || !context.signature) {
85
- throw new DarkSwapError('Invalid context');
86
- }
87
-
88
- const path = context.currentBalance.amount === 0n ?
89
- EMPTY_PATH :
90
- await getMerklePathAndRoot(context.currentBalance.note, this._darkSwap);
91
- context.merkleRoot = path.root;
92
-
93
- const proof = await generateDepositProof({
94
- merkleRoot: path.root,
95
- merkleIndex: path.index,
96
- merklePath: path.path,
97
- oldBalanceNote: context.currentBalance,
98
- newBalanceNote: context.newBalance,
99
- signedMessage: context.signature,
100
- address: context.address,
101
- });
102
- context.proof = proof;
103
- }
104
-
105
- public async execute(context: DepositContext): Promise<string> {
106
- await this.generateProof(context);
107
-
108
- if (!context
109
- || !context.currentBalance
110
- || !context.newBalance
111
- || !context.address
112
- || !context.signature
113
- || !context.proof
114
- || !context.depositAmount
115
- ) {
116
- throw new DarkSwapError('Invalid context');
117
- }
118
- const signer = this._darkSwap.signer;
119
- const contract = new ethers.Contract(
120
- this._darkSwap.contracts.darkSwapAssetManager,
121
- DarkSwapAssetManagerAbi.abi,
122
- signer
123
- );
124
-
125
- if (!isNativeAsset(context.newBalance.asset)) {
126
- await this.allowance(context);
127
- const tx = await contract.deposit(
128
- context.merkleRoot,
129
- context.newBalance.asset,
130
- hexlify32(context.depositAmount),
131
- context.proof.oldBalanceNullifier,
132
- hexlify32(context.newBalance.note),
133
- context.proof.newBalanceFooter,
134
- context.proof.proof,
135
- { value: 0n }
136
- );
137
- await tx.wait();
138
- return tx.hash;
139
- } else {
140
- const tx = await contract.deposit(
141
- context.merkleRoot,
142
- context.newBalance.asset,
143
- hexlify32(context.depositAmount),
144
- context.proof.oldBalanceNullifier,
145
- hexlify32(context.newBalance.note),
146
- context.proof.newBalanceFooter,
147
- context.proof.proof,
148
- { value: context.depositAmount }
149
- );
150
- await tx.wait();
151
- return tx.hash;
152
- }
153
- }
154
-
155
- protected async allowance(context: DepositContext) {
156
- if (!context || !context.newBalance || !context.address || !context.signature || !context.proof) {
157
- throw new DarkSwapError('Invalid context');
158
- }
159
- const signer = this._darkSwap.signer;
160
- const allowanceContract = new ethers.Contract(context.newBalance.asset, ERC20Abi.abi, this._darkSwap);
161
- const allowance = await allowanceContract.allowance(
162
- signer.getAddress(),
163
- this._darkSwap.contracts.darkSwapAssetManager
164
- );
165
- if (BigInt(allowance) < context.newBalance.amount) {
166
- const isLegacy =
167
- legacyTokenConfig.hasOwnProperty(this._darkSwap.chainId) &&
168
- legacyTokenConfig[this._darkSwap.chainId].includes(context.newBalance.asset.toLowerCase());
169
- const contract = new ethers.Contract(context.newBalance.asset, isLegacy ? ERC20_USDT.abi : ERC20Abi.abi, signer);
170
- const tx = await contract.approve(this._darkSwap.contracts.darkSwapAssetManager, hexlify32(MAX_ALLOWANCE));
171
- await tx.wait();
172
- }
173
- }
174
- }
@@ -1,4 +0,0 @@
1
- export * from './deposit';
2
- export * from './withdraw';
3
- export * from './join';
4
- export * from './tripleJoin';