@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,114 +0,0 @@
1
- import { hexlify } from 'ethers'
2
- import { DarkSwapNote, DarkSwapNoteExt, DarkSwapOrderNote } from '../types'
3
- import { P } from '../utils/constants'
4
- import { encodeAddress } from '../utils/encoders'
5
- import { mimc_bn254 } from '../utils/mimc'
6
- import { Fr } from '../aztec/fields/fields'
7
-
8
- let getRandomValues: (buf: Uint8Array) => Uint8Array;
9
-
10
- if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
11
- getRandomValues = (buf) => window.crypto.getRandomValues(buf);
12
- } else {
13
- const nodeCrypto = require('crypto');
14
- getRandomValues = (buf) => {
15
- const randomBytes = nodeCrypto.randomBytes(buf.length);
16
- buf.set(randomBytes);
17
- return buf;
18
- };
19
- }
20
-
21
- export const DOMAIN_NOTE = 2n
22
- export const DOMAIN_ORDER_NOTE = 3n
23
-
24
- export const EMPTY_NOTE: DarkSwapNote = {
25
- address: '0x0000000000000000000000000000000000000000',
26
- rho: 0n,
27
- note: 0n,
28
- amount: 0n,
29
- asset: '0x0000000000000000000000000000000000000000',
30
- }
31
-
32
- export function createNote(
33
- address: string,
34
- asset: string,
35
- amount: bigint,
36
- fuzkPubKey: [Fr, Fr]
37
- ): DarkSwapNoteExt {
38
- const rho = generateRho()
39
- const footer = getNoteFooter(rho, fuzkPubKey)
40
-
41
- const addressMod = encodeAddress(address)
42
- const assetMod = encodeAddress(asset)
43
- const note = mimc_bn254([DOMAIN_NOTE, addressMod, assetMod, amount, footer])
44
- return {
45
- address,
46
- rho,
47
- note,
48
- asset,
49
- amount,
50
- footer,
51
- }
52
- }
53
-
54
- export function getNoteFooter(rho: bigint, publicKey: [Fr, Fr]): bigint {
55
- return mimc_bn254([
56
- mimc_bn254([BigInt(rho)]),
57
- BigInt(publicKey[0].toString()),
58
- BigInt(publicKey[1].toString()),
59
- ])
60
- }
61
-
62
- function generateRho(): bigint {
63
- const securityLevel = 128
64
- const primeByteLength = Math.ceil(P.toString(2).length / 8)
65
- const totalBytes = primeByteLength + Math.ceil(securityLevel / 8)
66
-
67
- let rho = BigInt(0)
68
- do {
69
- let ab = new ArrayBuffer(totalBytes)
70
- let buf = new Uint8Array(ab)
71
- rho = BigInt(hexlify(getRandomValues(buf))) % P
72
- } while (rho === BigInt(0))
73
-
74
- return rho
75
- }
76
-
77
- export function calcNullifier(rho: bigint, fuzkPubKey: [Fr, Fr]): bigint {
78
- return mimc_bn254([
79
- rho,
80
- BigInt(fuzkPubKey[0].toString()),
81
- BigInt(fuzkPubKey[1].toString()),
82
- ])
83
- }
84
-
85
- export function createOrderNoteExt(
86
- address: string,
87
- asset: string,
88
- amount: bigint,
89
- feeRatio: bigint,
90
- fuzkPubKey: [Fr, Fr]
91
- ): DarkSwapOrderNote {
92
- const rho = generateRho()
93
- const footer = getNoteFooter(rho, fuzkPubKey)
94
-
95
- const assetMod = encodeAddress(asset)
96
- const addressMod = encodeAddress(address)
97
- const noteCommitment = mimc_bn254([
98
- DOMAIN_ORDER_NOTE,
99
- addressMod,
100
- assetMod,
101
- amount,
102
- feeRatio,
103
- footer,
104
- ])
105
-
106
- return {
107
- address,
108
- rho,
109
- note: noteCommitment,
110
- asset,
111
- amount,
112
- feeRatio,
113
- }
114
- }
@@ -1,126 +0,0 @@
1
- import proCancelOrderCircuit from "../../../circuits/pro/dark_swap_cancel_order_compiled_circuit.json";
2
- import { BaseProofInput, BaseProofParam, BaseProofResult, DarkSwapNote, DarkSwapOrderNote, DarkSwapProofError, EMPTY_NULLIFIER, PROOF_DOMAIN } from "../../../types";
3
- import { encodeAddress } from "../../../utils/encoders";
4
- import { bn_to_0xhex, bn_to_hex } from "../../../utils/formatters";
5
- import { mimc_bn254 } from "../../../utils/mimc";
6
- import { uint8ArrayToNumberArray } from "../../../utils/proofUtils";
7
- import { generateProof, signMessage } from "../../baseProofService";
8
- import { generateKeyPair } from "../../keyService";
9
- import { calcNullifier, getNoteFooter } from "../../noteService";
10
-
11
-
12
- type ProCancelOrderProofInput = BaseProofInput & {
13
- merkle_root: string,
14
- merkle_index: number[],
15
- merkle_path: string[],
16
- merkle_index_remaining: number[],
17
- merkle_path_remaining: string[],
18
-
19
- asset: string,
20
-
21
- //order note
22
- order_note: string,
23
- order_rho: string,
24
- order_nullifier: string,
25
- order_amount: string,
26
- fee_ratio: string,
27
-
28
- //account remaining note
29
- remaining_note: string,
30
- remaining_rho: string,
31
- remaining_nullifier: string,
32
- remaining_amount: string,
33
-
34
- //account available note after cancel
35
- account_note: string,
36
- account_rho: string,
37
- account_note_footer: string,
38
- }
39
-
40
- export type ProCancelOrderProofParam = BaseProofParam & {
41
- merkleRoot: string,
42
- merkleIndex: number[],
43
- merklePath: string[],
44
- merkleIndexRemaining: number[],
45
- merklePathRemaining: string[],
46
- orderNote: DarkSwapOrderNote,
47
- oldBalanceNote: DarkSwapNote,
48
- newBalanceNote: DarkSwapNote,
49
- }
50
-
51
- export type ProCancelOrderProofResult = BaseProofResult & {
52
- orderNullifier: string,
53
- oldBalanceNullifier: string,
54
- newBalanceNoteFooter: string,
55
- }
56
-
57
- export async function generateProCancelOrderProof(param: ProCancelOrderProofParam): Promise<ProCancelOrderProofResult> {
58
- if (param.orderNote.amount <= 0n) {
59
- throw new DarkSwapProofError("Invalid order amount");
60
- }
61
-
62
- if (param.oldBalanceNote.amount < 0n) {
63
- throw new DarkSwapProofError("Invalid old balance amount");
64
- }
65
- if (param.newBalanceNote.amount < 0n) {
66
- throw new DarkSwapProofError("Invalid new balance amount");
67
- }
68
-
69
- if (param.orderNote.amount != param.newBalanceNote.amount - param.oldBalanceNote.amount) {
70
- throw new DarkSwapProofError("Invalid order amount");
71
- }
72
-
73
- const [[fuzkPubKeyX, fuzkPubKeyY], fuzkPriKey] = await generateKeyPair(param.signedMessage);
74
-
75
- const orderNullifier = calcNullifier(param.orderNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
76
- let oldBalanceNullifier = EMPTY_NULLIFIER;
77
- if (param.oldBalanceNote.amount != 0n) {
78
- oldBalanceNullifier = calcNullifier(param.oldBalanceNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
79
- }
80
- const newBalanceNoteFooter = getNoteFooter(param.newBalanceNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
81
-
82
- const addressMod = encodeAddress(param.address);
83
- const message = bn_to_hex(mimc_bn254([
84
- BigInt(PROOF_DOMAIN.PRO_CANCEL_ORDER),
85
- orderNullifier,
86
- param.orderNote.feeRatio,
87
- oldBalanceNullifier,
88
- param.newBalanceNote.note,
89
- ]));
90
- const signature = await signMessage(message, fuzkPriKey);
91
-
92
- const inputs: ProCancelOrderProofInput = {
93
- address: bn_to_0xhex(addressMod),
94
- merkle_root: param.merkleRoot,
95
- merkle_index: param.merkleIndex,
96
- merkle_path: param.merklePath.map((x) => bn_to_0xhex(BigInt(x))),
97
- merkle_index_remaining: param.merkleIndexRemaining,
98
- merkle_path_remaining: param.merklePathRemaining.map((x) => bn_to_0xhex(BigInt(x))),
99
- order_note: bn_to_0xhex(param.orderNote.note),
100
- order_rho: bn_to_0xhex(param.orderNote.rho),
101
- order_nullifier: bn_to_0xhex(orderNullifier),
102
- order_amount: bn_to_0xhex(param.orderNote.amount),
103
- fee_ratio: bn_to_0xhex(param.orderNote.feeRatio),
104
-
105
- remaining_note: bn_to_0xhex(param.oldBalanceNote.note),
106
- remaining_rho: bn_to_0xhex(param.oldBalanceNote.rho),
107
- remaining_nullifier: bn_to_0xhex(oldBalanceNullifier),
108
- remaining_amount: bn_to_0xhex(param.oldBalanceNote.amount),
109
-
110
- account_note: bn_to_0xhex(param.newBalanceNote.note),
111
- account_rho: bn_to_0xhex(param.newBalanceNote.rho),
112
- account_note_footer: bn_to_0xhex(newBalanceNoteFooter),
113
-
114
- asset: bn_to_0xhex(encodeAddress(param.orderNote.asset)),
115
-
116
- pub_key: [fuzkPubKeyX.toString(), fuzkPubKeyY.toString()],
117
- signature: uint8ArrayToNumberArray(signature),
118
- };
119
- const proof = await generateProof(proCancelOrderCircuit, inputs);
120
- return {
121
- ...proof,
122
- orderNullifier: inputs.order_nullifier,
123
- oldBalanceNullifier: inputs.remaining_nullifier,
124
- newBalanceNoteFooter: inputs.account_note_footer,
125
- }
126
- };
@@ -1,136 +0,0 @@
1
- import proCreateOrderCircuit from "../../../circuits/pro/dark_swap_pro_create_order_compiled_circuit.json";
2
- import { BaseProofInput, BaseProofParam, BaseProofResult, DarkSwapNote, DarkSwapOrderNote, DarkSwapProofError, EMPTY_FOOTER, FEE_RATIO_PRECISION, PROOF_DOMAIN } from "../../../types";
3
- import { encodeAddress } from "../../../utils/encoders";
4
- import { bn_to_0xhex, bn_to_hex } from "../../../utils/formatters";
5
- import { mimc_bn254 } from "../../../utils/mimc";
6
- import { uint8ArrayToNumberArray } from "../../../utils/proofUtils";
7
- import { generateProof, signMessage } from "../../baseProofService";
8
- import { generateKeyPair } from "../../keyService";
9
- import { calcNullifier, getNoteFooter } from "../../noteService";
10
-
11
- type ProCreateOrderProofInput = BaseProofInput & {
12
- merkle_root: string,
13
- merkle_index: number[],
14
- merkle_path: string[],
15
-
16
- out_note: string,
17
- out_rho: string,
18
- out_nullifier: string,
19
- out_amount: string,
20
-
21
- //fee
22
- fee_ratio: string,
23
- fee_amount: string,
24
-
25
- //new balance note
26
- change_note: string,
27
- change_rho: string,
28
- change_note_footer: string,
29
- change_amount: string,
30
-
31
- //order note swap out
32
- order_note: string,
33
- order_rho: string,
34
- order_note_footer: string,
35
-
36
- //note swap in
37
- order_asset: string,
38
- order_amount: string,
39
- in_asset: string,
40
- in_amount: string,
41
- }
42
-
43
- export type ProCreateOrderProofParam = BaseProofParam & {
44
- merkleRoot: string,
45
- merkleIndex: number[],
46
- merklePath: string[],
47
- oldBalanceNote: DarkSwapNote,
48
- newBalanceNote: DarkSwapNote,
49
- orderNote: DarkSwapOrderNote,
50
- inAsset: string,
51
- inAmount: bigint,
52
- }
53
-
54
- export type ProCreateOrderProofResult = BaseProofResult & {
55
- oldBalanceNullifier: string,
56
- newBalanceFooter: string,
57
- orderNoteFooter: string,
58
- }
59
-
60
- export async function generateProCreateOrderProof(param: ProCreateOrderProofParam): Promise<ProCreateOrderProofResult> {
61
- if (param.orderNote.feeRatio < 0n) {
62
- throw new DarkSwapProofError("Invalid fee ratio");
63
- }
64
-
65
- if (param.newBalanceNote.amount < 0n
66
- || param.oldBalanceNote.amount <= 0n
67
- || param.inAmount <= 0n
68
- || param.orderNote.amount <= 0n) {
69
- throw new DarkSwapProofError("Invalid note amount");
70
- }
71
-
72
- if (param.orderNote.amount != param.oldBalanceNote.amount - param.newBalanceNote.amount) {
73
- throw new DarkSwapProofError("Invalid order amount");
74
- }
75
-
76
- const feeAmount = param.inAmount * param.orderNote.feeRatio / FEE_RATIO_PRECISION;
77
-
78
- const [[fuzkPubKeyX, fuzkPubKeyY], fuzkPriKey] = await generateKeyPair(param.signedMessage);
79
-
80
- let newBalanceFooter = EMPTY_FOOTER;
81
- if (param.newBalanceNote.amount != 0n) {
82
- newBalanceFooter = getNoteFooter(param.newBalanceNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
83
- }
84
-
85
- const oldBalanceNullifier = calcNullifier(param.oldBalanceNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
86
- const orderNoteFooter = getNoteFooter(param.orderNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
87
-
88
- const addressMod = encodeAddress(param.address);
89
- const message = bn_to_hex(mimc_bn254([
90
- BigInt(PROOF_DOMAIN.PRO_CREATE_ORDER),
91
- oldBalanceNullifier,
92
- param.orderNote.feeRatio,
93
- param.newBalanceNote.note,
94
- param.orderNote.note,
95
- encodeAddress(param.inAsset),
96
- param.inAmount
97
- ]));
98
- const signature = await signMessage(message, fuzkPriKey);
99
-
100
- const inputs: ProCreateOrderProofInput = {
101
- merkle_root: param.merkleRoot,
102
- merkle_index: param.merkleIndex,
103
- merkle_path: param.merklePath,
104
-
105
- address: bn_to_0xhex(addressMod),
106
- out_note: bn_to_0xhex(param.oldBalanceNote.note),
107
- out_rho: bn_to_0xhex(param.oldBalanceNote.rho),
108
- out_nullifier: bn_to_0xhex(oldBalanceNullifier),
109
- out_amount: bn_to_0xhex(param.oldBalanceNote.amount),
110
- fee_ratio: bn_to_0xhex(param.orderNote.feeRatio),
111
- fee_amount: bn_to_0xhex(feeAmount),
112
-
113
- change_note: bn_to_0xhex(param.newBalanceNote.note),
114
- change_rho: bn_to_0xhex(param.newBalanceNote.rho),
115
- change_note_footer: bn_to_0xhex(newBalanceFooter),
116
- change_amount: bn_to_0xhex(param.newBalanceNote.amount),
117
-
118
- order_note: bn_to_0xhex(param.orderNote.note),
119
- order_rho: bn_to_0xhex(param.orderNote.rho),
120
- order_note_footer: bn_to_0xhex(orderNoteFooter),
121
- order_asset: bn_to_0xhex(encodeAddress(param.orderNote.asset)),
122
- order_amount: bn_to_0xhex(param.orderNote.amount),
123
- in_asset: bn_to_0xhex(encodeAddress(param.inAsset)),
124
- in_amount: bn_to_0xhex(param.inAmount),
125
-
126
- pub_key: [fuzkPubKeyX.toString(), fuzkPubKeyY.toString()],
127
- signature: uint8ArrayToNumberArray(signature),
128
- };
129
- const proof = await generateProof(proCreateOrderCircuit, inputs);
130
- return {
131
- ...proof,
132
- oldBalanceNullifier: inputs.out_nullifier,
133
- newBalanceFooter: inputs.change_note_footer,
134
- orderNoteFooter: inputs.order_note_footer,
135
- }
136
- };
@@ -1,189 +0,0 @@
1
- import swapCircuit from "../../../circuits/pro/dark_swap_pro_swap_compiled_circuit.json";
2
- import { BaseProofResult, DarkSwapMessage, DarkSwapNote, DarkSwapOrderNote, DarkSwapProofError, EMPTY_FOOTER, PROOF_DOMAIN } from "../../../types";
3
- import { encodeAddress } from "../../../utils/encoders";
4
- import { bn_to_0xhex, bn_to_hex } from "../../../utils/formatters";
5
- import { mimc_bn254 } from "../../../utils/mimc";
6
- import { hexStringToSignature, uint8ArrayToNumberArray } from "../../../utils/proofUtils";
7
- import { generateProof, signMessage } from "../../baseProofService";
8
- import { generateKeyPair } from "../../keyService";
9
- import { calcNullifier, getNoteFooter } from "../../noteService";
10
-
11
- type ProSwapProofInput = {
12
- merkle_root: string,
13
-
14
- // Alice input
15
- alice_merkle_index: number[],
16
- alice_merkle_path: string[],
17
- alice_address: string,
18
- alice_out_note: string,
19
- alice_out_amount: string,
20
- alice_out_rho: string,
21
- alice_out_nullifier: string,
22
-
23
- //Alice fee
24
- alice_fee_ratio: string,
25
- alice_fee_amount: string,
26
-
27
- // Alice output
28
- alice_in_note: string,
29
- alice_in_rho: string,
30
- alice_in_note_footer: string,
31
-
32
- alice_change_note: string,
33
- alice_change_rho: string,
34
- alice_change_note_footer: string,
35
-
36
- // Alice pub key and signature
37
- alice_pub_key: string[],
38
- alice_signature: any,
39
-
40
- //Bob order
41
- bob_out_asset: string,
42
- bob_out_amount: string,
43
- bob_in_asset: string,
44
- bob_in_amount: string,
45
-
46
- // Bob input
47
- bob_merkle_index: number[],
48
- bob_merkle_path: string[],
49
- bob_address: string,
50
- bob_out_note: string,
51
-
52
- bob_out_rho: string,
53
- bob_out_nullifier: string,
54
-
55
- //bob fee
56
- bob_fee_ratio: string,
57
- bob_fee_amount: string,
58
-
59
- // Bob output
60
- bob_in_note: string,
61
- bob_in_rho: string,
62
- bob_in_note_footer: string,
63
-
64
- // Bob pub key and signature
65
- bob_pub_key: string[],
66
- bob_signature: any,
67
- }
68
-
69
- export type ProSwapProofParam = {
70
- merkleRoot: string,
71
- aliceMerkleIndex: number[],
72
- aliceMerklePath: string[],
73
- aliceAddress: string,
74
- aliceOrderNote: DarkSwapOrderNote,
75
- aliceFeeAmount: bigint,
76
- aliceInNote: DarkSwapNote,
77
- aliceChangeNote: DarkSwapNote,
78
- aliceSignedMessage: string,
79
-
80
- bobMerkleIndex: number[],
81
- bobMerklePath: string[],
82
- bobAddress: string,
83
- bobMessage: DarkSwapMessage,
84
- }
85
-
86
- export type ProSwapProofResult = BaseProofResult & {
87
- aliceOutNullifier: string,
88
- aliceInNoteFooter: string,
89
- aliceChangeNoteFooter: string,
90
- bobOutNullifier: string,
91
- bobInNoteFooter: string,
92
- }
93
-
94
- export async function generateProSwapProof(param: ProSwapProofParam): Promise<ProSwapProofResult> {
95
- if (param.aliceOrderNote.feeRatio < 0n
96
- || param.bobMessage.orderNote.feeRatio < 0n) {
97
- throw new DarkSwapProofError("Invalid fee ratio");
98
- }
99
-
100
- if (param.aliceChangeNote.amount < 0n
101
- || param.aliceOrderNote.amount <= 0n
102
- || param.aliceInNote.amount <= 0n
103
- || param.bobMessage.inNote.amount <= 0n
104
- || param.bobMessage.orderNote.amount <= 0n) {
105
- throw new DarkSwapProofError("Invalid note amount");
106
- }
107
-
108
- if (param.aliceOrderNote.amount != param.aliceChangeNote.amount + param.bobMessage.inNote.amount + param.bobMessage.feeAmount
109
- || param.bobMessage.orderNote.amount != param.aliceInNote.amount + param.aliceFeeAmount) {
110
- throw new DarkSwapProofError("Invalid order amount");
111
- }
112
-
113
- const [[fuzkPubKeyX, fuzkPubKeyY], fuzkPriKey] = await generateKeyPair(param.aliceSignedMessage);
114
-
115
- const aliceOrderNoteNullifier = calcNullifier(param.aliceOrderNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
116
- const aliceInNoteFooter = getNoteFooter(param.aliceInNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
117
- const aliceChangeNoteFooter = param.aliceChangeNote.amount == 0n ? EMPTY_FOOTER : getNoteFooter(param.aliceChangeNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
118
- const bobOrderNoteNullifier = calcNullifier(param.bobMessage.orderNote.rho, param.bobMessage.publicKey);
119
- const bobInNoteFooter = getNoteFooter(param.bobMessage.inNote.rho, param.bobMessage.publicKey);
120
-
121
- const aliceAddressMod = encodeAddress(param.aliceAddress);
122
- const bobAddressMod = encodeAddress(param.bobAddress);
123
- const message = bn_to_hex(mimc_bn254([
124
- BigInt(PROOF_DOMAIN.PRO_SWAP),
125
- aliceOrderNoteNullifier,
126
- param.aliceOrderNote.feeRatio,
127
- param.bobMessage.orderNote.feeRatio,
128
- bobOrderNoteNullifier,
129
- param.aliceInNote.note,
130
- param.aliceChangeNote.note,
131
- param.bobMessage.inNote.note
132
- ]));
133
- const signature = await signMessage(message, fuzkPriKey);
134
-
135
- const inputs: ProSwapProofInput = {
136
- merkle_root: param.merkleRoot,
137
- alice_merkle_index: param.aliceMerkleIndex,
138
- alice_merkle_path: param.aliceMerklePath,
139
- alice_address: bn_to_0xhex(aliceAddressMod),
140
- alice_out_note: bn_to_0xhex(param.aliceOrderNote.note),
141
- alice_out_rho: bn_to_0xhex(param.aliceOrderNote.rho),
142
- alice_out_nullifier: bn_to_0xhex(aliceOrderNoteNullifier),
143
- alice_out_amount: bn_to_0xhex(param.aliceOrderNote.amount),
144
- alice_fee_ratio: bn_to_0xhex(param.aliceOrderNote.feeRatio),
145
- alice_fee_amount: bn_to_0xhex(param.aliceFeeAmount),
146
-
147
- alice_in_note: bn_to_0xhex(param.aliceInNote.note),
148
- alice_in_rho: bn_to_0xhex(param.aliceInNote.rho),
149
- alice_in_note_footer: bn_to_0xhex(aliceInNoteFooter),
150
-
151
- alice_change_note: bn_to_0xhex(param.aliceChangeNote.note),
152
- alice_change_rho: bn_to_0xhex(param.aliceChangeNote.rho),
153
- alice_change_note_footer: bn_to_0xhex(aliceChangeNoteFooter),
154
-
155
- alice_pub_key: [fuzkPubKeyX.toString(), fuzkPubKeyY.toString()],
156
- alice_signature: uint8ArrayToNumberArray(signature),
157
-
158
- bob_out_asset: bn_to_0xhex(encodeAddress(param.bobMessage.orderNote.asset)),
159
- bob_out_amount: bn_to_0xhex(param.bobMessage.orderNote.amount),
160
- bob_in_asset: bn_to_0xhex(encodeAddress(param.bobMessage.inNote.asset)),
161
- bob_in_amount: bn_to_0xhex(param.bobMessage.inNote.amount + param.bobMessage.feeAmount),
162
-
163
- bob_merkle_index: param.bobMerkleIndex,
164
- bob_merkle_path: param.bobMerklePath,
165
- bob_address: bn_to_0xhex(bobAddressMod),
166
-
167
- bob_out_note: bn_to_0xhex(param.bobMessage.orderNote.note),
168
- bob_out_rho: bn_to_0xhex(param.bobMessage.orderNote.rho),
169
- bob_out_nullifier: bn_to_0xhex(bobOrderNoteNullifier),
170
- bob_fee_ratio: bn_to_0xhex(param.bobMessage.orderNote.feeRatio),
171
- bob_fee_amount: bn_to_0xhex(param.bobMessage.feeAmount),
172
-
173
- bob_in_note: bn_to_0xhex(param.bobMessage.inNote.note),
174
- bob_in_rho: bn_to_0xhex(param.bobMessage.inNote.rho),
175
- bob_in_note_footer: bn_to_0xhex(bobInNoteFooter),
176
-
177
- bob_pub_key: [param.bobMessage.publicKey[0].toString(), param.bobMessage.publicKey[1].toString()],
178
- bob_signature: uint8ArrayToNumberArray(hexStringToSignature(param.bobMessage.signature)),
179
- };
180
- const proof = await generateProof(swapCircuit, inputs);
181
- return {
182
- ...proof,
183
- aliceOutNullifier: inputs.alice_out_nullifier,
184
- aliceInNoteFooter: inputs.alice_in_note_footer,
185
- aliceChangeNoteFooter: inputs.alice_change_note_footer,
186
- bobOutNullifier: inputs.bob_out_nullifier,
187
- bobInNoteFooter: inputs.bob_in_note_footer,
188
- }
189
- };
@@ -1,78 +0,0 @@
1
- import retailCancelOrderCircuit from "../../circuits/retail/dark_swap_cancel_order_withdraw_compiled_circuit.json";
2
- import { BaseProofInput, BaseProofParam, BaseProofResult, DarkSwapOrderNote, DarkSwapProofError, PROOF_DOMAIN } from "../../types";
3
- import { encodeAddress } from "../../utils/encoders";
4
- import { bn_to_0xhex, bn_to_hex } from "../../utils/formatters";
5
- import { mimc_bn254 } from "../../utils/mimc";
6
- import { uint8ArrayToNumberArray } from "../../utils/proofUtils";
7
- import { generateProof, signMessage } from "../baseProofService";
8
- import { generateKeyPair } from "../keyService";
9
- import { calcNullifier } from "../noteService";
10
-
11
-
12
- type RetailCancelOrderProofInput = BaseProofInput & {
13
- merkle_root: string,
14
- merkle_index: number[],
15
- merkle_path: string[],
16
- //order note
17
- out_note: string,
18
- out_asset: string,
19
- out_amount: string,
20
- out_rho: string,
21
- out_nullifier: string,
22
- fee_ratio: string,
23
- }
24
-
25
- export type RetailCancelOrderProofParam = BaseProofParam & {
26
- merkleRoot: string,
27
- merkleIndex: number[],
28
- merklePath: string[],
29
- orderNote: DarkSwapOrderNote,
30
- }
31
-
32
- export type RetailCancelOrderProofResult = BaseProofResult & {
33
- nullifier: string,
34
- }
35
-
36
- export async function generateRetailCancelOrderProof(param: RetailCancelOrderProofParam): Promise<RetailCancelOrderProofResult> {
37
- if (param.orderNote.amount <= 0n) {
38
- throw new DarkSwapProofError("Order amount must be greater than 0");
39
- }
40
-
41
- if (param.orderNote.feeRatio < 0n) {
42
- throw new DarkSwapProofError("Fee ratio must be greater or equal to 0");
43
- }
44
-
45
- const [[fuzkPubKeyX, fuzkPubKeyY], fuzkPriKey] = await generateKeyPair(param.signedMessage);
46
-
47
- const nullifier = calcNullifier(param.orderNote.rho, [fuzkPubKeyX, fuzkPubKeyY]);
48
-
49
- const addressMod = encodeAddress(param.address);
50
- const message = bn_to_hex(mimc_bn254([
51
- BigInt(PROOF_DOMAIN.RETAIL_CANCEL_ORDER),
52
- addressMod,
53
- nullifier,
54
- param.orderNote.feeRatio,
55
- ]));
56
- const signature = await signMessage(message, fuzkPriKey);
57
-
58
- const inputs: RetailCancelOrderProofInput = {
59
- address: bn_to_0xhex(addressMod),
60
- merkle_root: param.merkleRoot,
61
- merkle_index: param.merkleIndex,
62
- merkle_path: param.merklePath.map((x) => bn_to_0xhex(BigInt(x))),
63
- out_note: bn_to_0xhex(param.orderNote.note),
64
- out_asset: bn_to_0xhex(encodeAddress(param.orderNote.asset)),
65
- out_amount: bn_to_0xhex(param.orderNote.amount),
66
- out_rho: bn_to_0xhex(param.orderNote.rho),
67
- out_nullifier: bn_to_0xhex(nullifier),
68
- fee_ratio: bn_to_0xhex(param.orderNote.feeRatio),
69
-
70
- pub_key: [fuzkPubKeyX.toString(), fuzkPubKeyY.toString()],
71
- signature: uint8ArrayToNumberArray(signature),
72
- };
73
- const proof = await generateProof(retailCancelOrderCircuit, inputs);
74
- return {
75
- ...proof,
76
- nullifier: inputs.out_nullifier,
77
- }
78
- };