@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.
- package/dist/test/utils/helpers.d.ts +9 -0
- package/package.json +56 -53
- package/src/abis/DarkSwapAssetManager.json +0 -1162
- package/src/abis/DarkSwapFeeAssetManager.json +0 -282
- package/src/abis/IERC20.json +0 -194
- package/src/abis/IERC20_USDT.json +0 -188
- package/src/abis/MerkleTreeOperator.json +0 -533
- package/src/aztec/bigint-buffer/index.ts +0 -87
- package/src/aztec/collection/object.ts +0 -76
- package/src/aztec/crypto/poseidon/index.ts +0 -18
- package/src/aztec/crypto/schnorr/index.ts +0 -40
- package/src/aztec/crypto/schnorr/signature.ts +0 -107
- package/src/aztec/crypto/serialize.ts +0 -85
- package/src/aztec/crypto/signature/index.ts +0 -17
- package/src/aztec/fields/fields.ts +0 -361
- package/src/aztec/fields/point.ts +0 -208
- package/src/aztec/serialize/buffer_reader.ts +0 -402
- package/src/aztec/serialize/field_reader.ts +0 -154
- package/src/aztec/serialize/free_funcs.ts +0 -193
- package/src/aztec/serialize/serialize.ts +0 -336
- package/src/aztec/serialize/types.ts +0 -19
- package/src/aztec/string/index.ts +0 -39
- package/src/circuits/pro/dark_swap_cancel_order_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_deposit_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_join_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_pro_create_order_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_pro_swap_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_triple_join_compiled_circuit.json +0 -1
- package/src/circuits/pro/dark_swap_withdraw_compiled_circuit.json +0 -1
- package/src/circuits/retail/dark_swap_cancel_order_withdraw_compiled_circuit.json +0 -1
- package/src/circuits/retail/dark_swap_retail_deposit_create_order_compiled_circuit.json +0 -1
- package/src/circuits/retail/dark_swap_retail_swap_compiled_circuit.json +0 -1
- package/src/config/chain.ts +0 -9
- package/src/config/config.ts +0 -9
- package/src/config/contractConfig.ts +0 -61
- package/src/config/index.ts +0 -3
- package/src/darkSwap.ts +0 -31
- package/src/entities/error.ts +0 -6
- package/src/entities/index.ts +0 -3
- package/src/entities/token.ts +0 -9
- package/src/entities/types.ts +0 -6
- package/src/index.ts +0 -9
- package/src/proof/baseProofService.ts +0 -34
- package/src/proof/basic/depositProof.ts +0 -101
- package/src/proof/basic/joinProof.ts +0 -112
- package/src/proof/basic/tripleJoinProof.ts +0 -130
- package/src/proof/basic/withdrawProof.ts +0 -97
- package/src/proof/keyService.ts +0 -9
- package/src/proof/noteService.ts +0 -114
- package/src/proof/pro/orders/cancelOrderProof.ts +0 -126
- package/src/proof/pro/orders/createOrderProof.ts +0 -136
- package/src/proof/pro/orders/swapProof.ts +0 -189
- package/src/proof/retail/cancelOrderProof.ts +0 -78
- package/src/proof/retail/depositOrderProof.ts +0 -133
- package/src/proof/retail/swapProof.ts +0 -158
- package/src/services/BaseService.ts +0 -49
- package/src/services/EventService.ts +0 -17
- package/src/services/agent/index.ts +0 -1
- package/src/services/agent/retailSwap.ts +0 -116
- package/src/services/base/deposit.ts +0 -174
- package/src/services/base/index.ts +0 -4
- package/src/services/base/join.ts +0 -135
- package/src/services/base/tripleJoin.ts +0 -161
- package/src/services/base/withdraw.ts +0 -123
- package/src/services/feeRatioService.ts +0 -13
- package/src/services/index.ts +0 -7
- package/src/services/merkletree.ts +0 -46
- package/src/services/noteService.ts +0 -75
- package/src/services/pro/cancelOrder.ts +0 -142
- package/src/services/pro/createOrder.ts +0 -183
- package/src/services/pro/index.ts +0 -3
- package/src/services/pro/proSwap.ts +0 -199
- package/src/services/retail/cancelAndWithdrawOrder.ts +0 -95
- package/src/services/retail/depositAndCreateOrder.ts +0 -150
- package/src/services/retail/index.ts +0 -2
- package/src/types.ts +0 -76
- package/src/utils/constants.ts +0 -3
- package/src/utils/encoders.ts +0 -10
- package/src/utils/formatters.ts +0 -11
- package/src/utils/mimc.ts +0 -143
- package/src/utils/proofUtils.ts +0 -18
- package/src/utils/swapUtils.ts +0 -55
- package/src/utils/util.ts +0 -21
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { ethers } from 'ethers';
|
|
2
|
-
import { generateJoinProof, JoinProofResult } from '../../proof/basic/joinProof';
|
|
3
|
-
import { DarkSwapNote, EMPTY_NULLIFIER } from '../../types';
|
|
4
|
-
import { hexlify32, isAddressEquals } from '../../utils/util';
|
|
5
|
-
import { BaseContext, BaseContractService } from '../BaseService';
|
|
6
|
-
import { multiGetMerklePathAndRoot } from '../merkletree';
|
|
7
|
-
import { DarkSwap } from '../../darkSwap';
|
|
8
|
-
import { DarkSwapError } from '../../entities';
|
|
9
|
-
import { generateKeyPair } from '../../proof/keyService';
|
|
10
|
-
import { createNote } from '../../proof/noteService';
|
|
11
|
-
import DarkSwapAssetManagerAbi from '../../abis/DarkSwapAssetManager.json';
|
|
12
|
-
|
|
13
|
-
class JoinContext extends BaseContext {
|
|
14
|
-
private _inNote1?: DarkSwapNote;
|
|
15
|
-
private _inNote2?: DarkSwapNote;
|
|
16
|
-
private _outNote?: DarkSwapNote;
|
|
17
|
-
private _proof?: JoinProofResult;
|
|
18
|
-
|
|
19
|
-
constructor(signature: string) {
|
|
20
|
-
super(signature);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
set inNote1(note: DarkSwapNote | undefined) {
|
|
24
|
-
this._inNote1 = note;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
get inNote1(): DarkSwapNote | undefined {
|
|
28
|
-
return this._inNote1;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
set inNote2(note: DarkSwapNote | undefined) {
|
|
32
|
-
this._inNote2 = note;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
get inNote2(): DarkSwapNote | undefined {
|
|
36
|
-
return this._inNote2;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
set outNote(note: DarkSwapNote | undefined) {
|
|
40
|
-
this._outNote = note;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
get outNote(): DarkSwapNote | undefined {
|
|
44
|
-
return this._outNote;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
set proof(proof: JoinProofResult | undefined) {
|
|
48
|
-
this._proof = proof;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get proof(): JoinProofResult | undefined {
|
|
52
|
-
return this._proof;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export class JoinService extends BaseContractService {
|
|
57
|
-
constructor(_darkSwap: DarkSwap) {
|
|
58
|
-
super(_darkSwap);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public async prepare(
|
|
62
|
-
address: string,
|
|
63
|
-
inNote1: DarkSwapNote,
|
|
64
|
-
inNote2: DarkSwapNote,
|
|
65
|
-
signature: string
|
|
66
|
-
): Promise<{ context: JoinContext; outNote: DarkSwapNote }> {
|
|
67
|
-
if (!isAddressEquals(inNote1.asset, inNote2.asset)) {
|
|
68
|
-
throw new DarkSwapError('inNote1 and inNote2 must have the same asset');
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (inNote1.note === inNote2.note) {
|
|
72
|
-
throw new DarkSwapError('inNote1 and inNote2 must have different note');
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const [pubKey] = await generateKeyPair(signature);
|
|
76
|
-
const outNote = createNote(address, inNote1.asset, inNote1.amount + inNote2.amount, pubKey);
|
|
77
|
-
const context = new JoinContext(signature);
|
|
78
|
-
context.inNote1 = inNote1;
|
|
79
|
-
context.inNote2 = inNote2;
|
|
80
|
-
context.outNote = outNote;
|
|
81
|
-
context.address = address;
|
|
82
|
-
return { context, outNote };
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private async generateProof(context: JoinContext): Promise<void> {
|
|
86
|
-
if (!context || !context.inNote1 || !context.inNote2 || !context.outNote || !context.address) {
|
|
87
|
-
throw new DarkSwapError('Invalid context');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const merklePathes = await multiGetMerklePathAndRoot([context.inNote1.note, context.inNote2.note], this._darkSwap);
|
|
91
|
-
const path1 = merklePathes[0];
|
|
92
|
-
const path2 = merklePathes[1];
|
|
93
|
-
|
|
94
|
-
const proof = await generateJoinProof({
|
|
95
|
-
inNote1: context.inNote1,
|
|
96
|
-
inNote2: context.inNote2,
|
|
97
|
-
outNote: context.outNote,
|
|
98
|
-
merkleRoot: path1.root,
|
|
99
|
-
inMerklePath1: path1.path,
|
|
100
|
-
inMerkleIndex1: path1.index,
|
|
101
|
-
inMerklePath2: path2.path,
|
|
102
|
-
inMerkleIndex2: path2.index,
|
|
103
|
-
signedMessage: context.signature,
|
|
104
|
-
address: context.address,
|
|
105
|
-
});
|
|
106
|
-
context.merkleRoot = path1.root;
|
|
107
|
-
context.proof = proof;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
public async execute(context: JoinContext): Promise<string> {
|
|
111
|
-
await this.generateProof(context);
|
|
112
|
-
if (!context || !context.inNote1 || !context.inNote2 || !context.outNote || !context.proof || !context.merkleRoot) {
|
|
113
|
-
throw new DarkSwapError('Invalid context');
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const contract = new ethers.Contract(
|
|
117
|
-
this._darkSwap.contracts.darkSwapAssetManager,
|
|
118
|
-
DarkSwapAssetManagerAbi.abi,
|
|
119
|
-
this._darkSwap.signer
|
|
120
|
-
);
|
|
121
|
-
const tx = await contract.join(
|
|
122
|
-
context.merkleRoot,
|
|
123
|
-
[
|
|
124
|
-
context.proof.inNullifier1,
|
|
125
|
-
context.proof.inNullifier2,
|
|
126
|
-
hexlify32(EMPTY_NULLIFIER)
|
|
127
|
-
],
|
|
128
|
-
hexlify32(context.outNote.note),
|
|
129
|
-
context.proof.outNoteFooter,
|
|
130
|
-
context.proof.proof
|
|
131
|
-
);
|
|
132
|
-
await tx.wait();
|
|
133
|
-
return tx.hash;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
@@ -1,161 +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 { generateTripleJoinProof, TripleJoinProofResult } from '../../proof/basic/tripleJoinProof';
|
|
6
|
-
import { generateKeyPair } from '../../proof/keyService';
|
|
7
|
-
import { createNote } from '../../proof/noteService';
|
|
8
|
-
import { DarkSwapNote } from '../../types';
|
|
9
|
-
import { hexlify32, isAddressEquals } from '../../utils/util';
|
|
10
|
-
import { BaseContext, BaseContractService } from '../BaseService';
|
|
11
|
-
import { multiGetMerklePathAndRoot } from '../merkletree';
|
|
12
|
-
|
|
13
|
-
class TripleJoinContext extends BaseContext {
|
|
14
|
-
private _inNote1?: DarkSwapNote;
|
|
15
|
-
private _inNote2?: DarkSwapNote;
|
|
16
|
-
private _inNote3?: DarkSwapNote;
|
|
17
|
-
private _outNote?: DarkSwapNote;
|
|
18
|
-
private _proof?: TripleJoinProofResult;
|
|
19
|
-
|
|
20
|
-
constructor(signature: string) {
|
|
21
|
-
super(signature);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
set inNote1(note: DarkSwapNote | undefined) {
|
|
25
|
-
this._inNote1 = note;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
get inNote1(): DarkSwapNote | undefined {
|
|
29
|
-
return this._inNote1;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
set inNote2(note: DarkSwapNote | undefined) {
|
|
33
|
-
this._inNote2 = note;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
get inNote2(): DarkSwapNote | undefined {
|
|
37
|
-
return this._inNote2;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
set inNote3(note: DarkSwapNote | undefined) {
|
|
41
|
-
this._inNote3 = note;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
get inNote3(): DarkSwapNote | undefined {
|
|
45
|
-
return this._inNote3;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
set outNote(note: DarkSwapNote | undefined) {
|
|
50
|
-
this._outNote = note;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get outNote(): DarkSwapNote | undefined {
|
|
54
|
-
return this._outNote;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
set proof(proof: TripleJoinProofResult | undefined) {
|
|
58
|
-
this._proof = proof;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
get proof(): TripleJoinProofResult | undefined {
|
|
62
|
-
return this._proof;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export class TripleJoinService extends BaseContractService {
|
|
67
|
-
constructor(_darkSwap: DarkSwap) {
|
|
68
|
-
super(_darkSwap);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public async prepare(
|
|
72
|
-
address: string,
|
|
73
|
-
inNote1: DarkSwapNote,
|
|
74
|
-
inNote2: DarkSwapNote,
|
|
75
|
-
inNote3: DarkSwapNote,
|
|
76
|
-
signature: string
|
|
77
|
-
): Promise<{ context: TripleJoinContext; outNote: DarkSwapNote }> {
|
|
78
|
-
if (!isAddressEquals(inNote1.asset, inNote2.asset)) {
|
|
79
|
-
throw new DarkSwapError('inNote1 and inNote2 must have the same asset');
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (inNote1.note === inNote2.note) {
|
|
83
|
-
throw new DarkSwapError('inNote1 and inNote2 must have different note');
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const [pubKey] = await generateKeyPair(signature);
|
|
87
|
-
const outNote = createNote(address, inNote1.asset, inNote1.amount + inNote2.amount + inNote3.amount, pubKey);
|
|
88
|
-
const context = new TripleJoinContext(signature);
|
|
89
|
-
context.inNote1 = inNote1;
|
|
90
|
-
context.inNote2 = inNote2;
|
|
91
|
-
context.inNote3 = inNote3;
|
|
92
|
-
context.outNote = outNote;
|
|
93
|
-
context.address = address;
|
|
94
|
-
return { context, outNote };
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private async generateProof(context: TripleJoinContext): Promise<void> {
|
|
98
|
-
if (!context
|
|
99
|
-
|| !context.inNote1
|
|
100
|
-
|| !context.inNote2
|
|
101
|
-
|| !context.inNote3
|
|
102
|
-
|| !context.outNote
|
|
103
|
-
|| !context.address) {
|
|
104
|
-
throw new DarkSwapError('Invalid context');
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const merklePathes = await multiGetMerklePathAndRoot([context.inNote1.note, context.inNote2.note, context.inNote3.note], this._darkSwap);
|
|
108
|
-
const path1 = merklePathes[0];
|
|
109
|
-
const path2 = merklePathes[1];
|
|
110
|
-
const path3 = merklePathes[2];
|
|
111
|
-
|
|
112
|
-
const proof = await generateTripleJoinProof({
|
|
113
|
-
inNote1: context.inNote1,
|
|
114
|
-
inNote2: context.inNote2,
|
|
115
|
-
inNote3: context.inNote3,
|
|
116
|
-
outNote: context.outNote,
|
|
117
|
-
merkleRoot: path1.root,
|
|
118
|
-
inMerklePath1: path1.path,
|
|
119
|
-
inMerkleIndex1: path1.index,
|
|
120
|
-
inMerklePath2: path2.path,
|
|
121
|
-
inMerkleIndex2: path2.index,
|
|
122
|
-
inMerklePath3: path3.path,
|
|
123
|
-
inMerkleIndex3: path3.index,
|
|
124
|
-
signedMessage: context.signature,
|
|
125
|
-
address: context.address,
|
|
126
|
-
});
|
|
127
|
-
context.merkleRoot = path1.root;
|
|
128
|
-
context.proof = proof;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
public async execute(context: TripleJoinContext): Promise<string> {
|
|
132
|
-
await this.generateProof(context);
|
|
133
|
-
if (!context
|
|
134
|
-
|| !context.inNote1
|
|
135
|
-
|| !context.inNote2
|
|
136
|
-
|| !context.inNote3
|
|
137
|
-
|| !context.outNote
|
|
138
|
-
|| !context.proof
|
|
139
|
-
|| !context.merkleRoot) {
|
|
140
|
-
throw new DarkSwapError('Invalid context');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const contract = new ethers.Contract(
|
|
144
|
-
this._darkSwap.contracts.darkSwapAssetManager,
|
|
145
|
-
DarkSwapAssetManagerAbi.abi,
|
|
146
|
-
this._darkSwap.signer
|
|
147
|
-
);
|
|
148
|
-
const tx = await contract.join(
|
|
149
|
-
context.merkleRoot,
|
|
150
|
-
[
|
|
151
|
-
context.proof.inNullifier1,
|
|
152
|
-
context.proof.inNullifier2,
|
|
153
|
-
context.proof.inNullifier3
|
|
154
|
-
],
|
|
155
|
-
hexlify32(context.outNote.note),
|
|
156
|
-
context.proof.outNoteFooter,
|
|
157
|
-
context.proof.proof
|
|
158
|
-
);
|
|
159
|
-
return tx.hash;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
@@ -1,123 +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 { generateWithdrawProof, WithdrawProofResult } from '../../proof/basic/withdrawProof';
|
|
6
|
-
import { generateKeyPair } from '../../proof/keyService';
|
|
7
|
-
import { createNote } from '../../proof/noteService';
|
|
8
|
-
import { DarkSwapNote } from '../../types';
|
|
9
|
-
import { BaseContext, BaseContractService } from '../BaseService';
|
|
10
|
-
import { getMerklePathAndRoot } from '../merkletree';
|
|
11
|
-
import { hexlify32 } from '../../utils/util';
|
|
12
|
-
|
|
13
|
-
class WithdrawContext extends BaseContext {
|
|
14
|
-
private _currentBalance?: DarkSwapNote;
|
|
15
|
-
private _newBalance?: DarkSwapNote;
|
|
16
|
-
private _withdrawAmount?: bigint;
|
|
17
|
-
private _proof?: WithdrawProofResult;
|
|
18
|
-
|
|
19
|
-
constructor(signature: string) {
|
|
20
|
-
super(signature);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
set currentBalance(note: DarkSwapNote | undefined) {
|
|
24
|
-
this._currentBalance = note;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
get currentBalance(): DarkSwapNote | undefined {
|
|
28
|
-
return this._currentBalance;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
set newBalance(note: DarkSwapNote | undefined) {
|
|
32
|
-
this._newBalance = note;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
get newBalance(): DarkSwapNote | undefined {
|
|
36
|
-
return this._newBalance;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
set withdrawAmount(amount: bigint | undefined) {
|
|
40
|
-
this._withdrawAmount = amount;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
get withdrawAmount(): bigint | undefined {
|
|
44
|
-
return this._withdrawAmount;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
set proof(proof: WithdrawProofResult | undefined) {
|
|
48
|
-
this._proof = proof;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get proof(): WithdrawProofResult | undefined {
|
|
52
|
-
return this._proof;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export class WithdrawService extends BaseContractService {
|
|
57
|
-
constructor(_darkSwap: DarkSwap) {
|
|
58
|
-
super(_darkSwap);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public async prepare(
|
|
62
|
-
address: string,
|
|
63
|
-
currentBalance: DarkSwapNote,
|
|
64
|
-
withdrawAmount: bigint,
|
|
65
|
-
signature: string
|
|
66
|
-
): Promise<{ context: WithdrawContext; newBalanceNote: DarkSwapNote }> {
|
|
67
|
-
const [pubKey] = await generateKeyPair(signature);
|
|
68
|
-
const newBalanceNote = createNote(address, currentBalance.asset, currentBalance.amount - withdrawAmount, pubKey);
|
|
69
|
-
|
|
70
|
-
const context = new WithdrawContext(signature);
|
|
71
|
-
context.currentBalance = currentBalance;
|
|
72
|
-
context.newBalance = newBalanceNote;
|
|
73
|
-
context.withdrawAmount = withdrawAmount;
|
|
74
|
-
context.address = address;
|
|
75
|
-
return { context, newBalanceNote };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private async generateProof(context: WithdrawContext): Promise<void> {
|
|
79
|
-
if (!context || !context.currentBalance || !context.newBalance || !context.withdrawAmount || !context.address) {
|
|
80
|
-
throw new DarkSwapError('Invalid context');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const path = await getMerklePathAndRoot(context.currentBalance.note, this._darkSwap);
|
|
84
|
-
context.merkleRoot = path.root;
|
|
85
|
-
|
|
86
|
-
const proof = await generateWithdrawProof({
|
|
87
|
-
oldBalance: context.currentBalance,
|
|
88
|
-
newBalance: context.newBalance,
|
|
89
|
-
address: context.address,
|
|
90
|
-
merkleRoot: path.root,
|
|
91
|
-
merklePath: path.path,
|
|
92
|
-
merkleIndex: path.index,
|
|
93
|
-
signedMessage: context.signature,
|
|
94
|
-
});
|
|
95
|
-
context.proof = proof;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
public async execute(context: WithdrawContext): Promise<string> {
|
|
99
|
-
await this.generateProof(context);
|
|
100
|
-
|
|
101
|
-
if (!context || !context.currentBalance || !context.newBalance || !context.proof || !context.merkleRoot) {
|
|
102
|
-
throw new DarkSwapError('Invalid context');
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const contract = new ethers.Contract(
|
|
106
|
-
this._darkSwap.contracts.darkSwapAssetManager,
|
|
107
|
-
DarkSwapAssetManagerAbi.abi,
|
|
108
|
-
this._darkSwap.signer
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
const tx = await contract.withdraw(
|
|
112
|
-
context.merkleRoot,
|
|
113
|
-
context.currentBalance.asset,
|
|
114
|
-
context.withdrawAmount,
|
|
115
|
-
context.proof.oldBalanceNullifier,
|
|
116
|
-
hexlify32(context.newBalance.note),
|
|
117
|
-
context.proof.newBalanceFooter,
|
|
118
|
-
context.proof.proof
|
|
119
|
-
);
|
|
120
|
-
await tx.wait();
|
|
121
|
-
return tx.hash;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { ethers } from 'ethers';
|
|
2
|
-
import DarkSwapFeeAssetManagerAbi from '../abis/DarkSwapFeeAssetManager.json';
|
|
3
|
-
import { DarkSwap } from '../darkSwap';
|
|
4
|
-
|
|
5
|
-
function getContract(address: string, darkSwap: DarkSwap) {
|
|
6
|
-
const provider = darkSwap.provider;
|
|
7
|
-
return new ethers.Contract(address, DarkSwapFeeAssetManagerAbi.abi, provider);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export async function getFeeRatio(wallet: string, darkSwap: DarkSwap) {
|
|
11
|
-
const contract = getContract(darkSwap.contracts.darkSwapFeeAssetManager, darkSwap);
|
|
12
|
-
return await contract.getServiceFeePercentage(wallet);
|
|
13
|
-
}
|
package/src/services/index.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { ethers } from 'ethers';
|
|
2
|
-
import MerkleAbi from '../abis/MerkleTreeOperator.json';
|
|
3
|
-
import { hexlify32 } from '../utils/util';
|
|
4
|
-
import { DarkSwap } from '../darkSwap';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export interface MerklePath {
|
|
8
|
-
noteCommitment: bigint;
|
|
9
|
-
path: string[];
|
|
10
|
-
index: number[];
|
|
11
|
-
root: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const EMPTY_PATH: MerklePath = {
|
|
15
|
-
noteCommitment: 0n,
|
|
16
|
-
path: Array(32).fill('0x0000000000000000000000000000000000000000000000000000000000000000'),
|
|
17
|
-
index: Array(32).fill(0),
|
|
18
|
-
root: '0x0000000000000000000000000000000000000000000000000000000000000000'
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function getContract(address: string, darkSwap: DarkSwap) {
|
|
22
|
-
const provider = darkSwap.provider;
|
|
23
|
-
return new ethers.Contract(address, MerkleAbi.abi, provider);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export async function getMerklePathAndRoot(note: bigint, darkSwap: DarkSwap): Promise<MerklePath> {
|
|
27
|
-
const result = await multiGetMerklePathAndRoot([note], darkSwap);
|
|
28
|
-
return result[0];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export async function multiGetMerklePathAndRoot(notes: bigint[], darkSwap: DarkSwap): Promise<MerklePath[]> {
|
|
32
|
-
const contract = getContract(darkSwap.contracts.merkleTreeOperator, darkSwap);
|
|
33
|
-
|
|
34
|
-
const [root, paths, indexes] = await contract.getMultiMerklePaths(notes.map(note => hexlify32(note)));
|
|
35
|
-
const results: MerklePath[] = [];
|
|
36
|
-
for (let i = 0; i < notes.length; i++) {
|
|
37
|
-
results.push({
|
|
38
|
-
noteCommitment: notes[i],
|
|
39
|
-
path: paths[i],
|
|
40
|
-
index: indexes[i].map((x: boolean) => (x ? 1 : 0)),
|
|
41
|
-
root
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return results;
|
|
46
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { hexlify32 } from '../utils/util';
|
|
2
|
-
import { ethers } from 'ethers';
|
|
3
|
-
import MerkleAbi from '../abis/MerkleTreeOperator.json';
|
|
4
|
-
import { NoteOnChainStatus } from '../entities';
|
|
5
|
-
import { DarkSwap } from '../darkSwap';
|
|
6
|
-
import { DarkSwapNote } from '../types';
|
|
7
|
-
import { calcNullifier } from '../proof/noteService';
|
|
8
|
-
import { generateKeyPair } from '../proof/keyService';
|
|
9
|
-
import { Fr } from '../aztec/fields/fields';
|
|
10
|
-
|
|
11
|
-
function getContract(address: string, darkSwap: DarkSwap) {
|
|
12
|
-
const provider = darkSwap.provider;
|
|
13
|
-
return new ethers.Contract(address, MerkleAbi.abi, provider);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async function getNoteOnChainStatus(darkSwap: DarkSwap, note: string, nullifier: string) {
|
|
17
|
-
const contract = getContract(darkSwap.contracts.merkleTreeOperator, darkSwap);
|
|
18
|
-
const isCreated = (await contract.noteCommitmentsCreated(note)) as boolean;
|
|
19
|
-
if (!isCreated) {
|
|
20
|
-
return NoteOnChainStatus.UNKNOWN;
|
|
21
|
-
}
|
|
22
|
-
const isSpent = (await contract.nullifiersUsed(nullifier)) as boolean;
|
|
23
|
-
if (isSpent) {
|
|
24
|
-
return NoteOnChainStatus.SPENT;
|
|
25
|
-
}
|
|
26
|
-
const isLocked = (await contract.nullifiersLocked(nullifier)) as boolean;
|
|
27
|
-
if (isLocked) {
|
|
28
|
-
return NoteOnChainStatus.LOCKED;
|
|
29
|
-
}
|
|
30
|
-
return NoteOnChainStatus.ACTIVE;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export async function getNoteOnChainStatusByPublicKey(
|
|
34
|
-
darkSwap: DarkSwap,
|
|
35
|
-
note: DarkSwapNote,
|
|
36
|
-
publicKey: [Fr, Fr]
|
|
37
|
-
): Promise<NoteOnChainStatus> {
|
|
38
|
-
const nullifier = calcNullifier(note.rho, publicKey);
|
|
39
|
-
const onChainStatus = await getNoteOnChainStatus(darkSwap, hexlify32(note.note), hexlify32(nullifier));
|
|
40
|
-
return onChainStatus;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export async function getNoteOnChainStatusBySignature(
|
|
44
|
-
darkSwap: DarkSwap,
|
|
45
|
-
note: DarkSwapNote,
|
|
46
|
-
signature: string
|
|
47
|
-
): Promise<NoteOnChainStatus> {
|
|
48
|
-
const [publicKey] = await generateKeyPair(signature);
|
|
49
|
-
const nullifier = calcNullifier(note.rho, publicKey);
|
|
50
|
-
const onChainStatus = await getNoteOnChainStatus(darkSwap, hexlify32(note.note), hexlify32(nullifier));
|
|
51
|
-
return onChainStatus;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export async function isNoteActive(darkSwap: DarkSwap, note: DarkSwapNote, publicKey: [Fr, Fr]): Promise<boolean> {
|
|
55
|
-
const nullifier = calcNullifier(note.rho, publicKey);
|
|
56
|
-
const onChainStatus = await getNoteOnChainStatus(darkSwap, hexlify32(note.note), hexlify32(nullifier));
|
|
57
|
-
return onChainStatus === NoteOnChainStatus.ACTIVE;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export async function isNoteSpent(darkSwap: DarkSwap, note: DarkSwapNote, publicKey: [Fr, Fr]) {
|
|
61
|
-
const nullifier = calcNullifier(note.rho, publicKey);
|
|
62
|
-
const onChainStatus = await getNoteOnChainStatus(darkSwap, hexlify32(note.note), hexlify32(nullifier));
|
|
63
|
-
return onChainStatus === NoteOnChainStatus.SPENT;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export async function isNoteValid(darkSwap: DarkSwap, note: DarkSwapNote, publicKey: [Fr, Fr]) {
|
|
67
|
-
const nullifier = calcNullifier(note.rho, publicKey);
|
|
68
|
-
const onChainStatus = await getNoteOnChainStatus(darkSwap, hexlify32(note.note), hexlify32(nullifier));
|
|
69
|
-
return onChainStatus === NoteOnChainStatus.ACTIVE;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export async function getNullifierBySignature(note: DarkSwapNote, signature: string): Promise<string> {
|
|
73
|
-
const [publicKey] = await generateKeyPair(signature);
|
|
74
|
-
return hexlify32(calcNullifier(note.rho, publicKey));
|
|
75
|
-
}
|