@thesingularitynetwork/darkswap-sdk 0.1.6 → 0.1.8
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/index.esm.js +48 -22
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +48 -22
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +48 -21
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/proof/pro/orders/createOrderProof.d.ts +2 -1
- package/dist/types/src/services/base/withdraw.d.ts +1 -1
- package/dist/types/src/services/noteService.d.ts +1 -0
- package/dist/types/src/services/pro/createOrder.d.ts +8 -6
- package/dist/types/src/types.d.ts +4 -0
- package/dist/types/test/services/joinService.test.d.ts +1 -0
- package/dist/types/test/services/withdrawService.test.d.ts +1 -0
- package/dist/types/test/utils/helpers.d.ts +1 -0
- package/package.json +1 -1
- package/src/proof/pro/orders/createOrderProof.ts +6 -5
- package/src/proof/retail/depositOrderProof.ts +3 -1
- package/src/services/base/deposit.ts +0 -7
- package/src/services/base/withdraw.ts +8 -4
- package/src/services/noteService.ts +5 -0
- package/src/services/pro/createOrder.ts +34 -15
- package/src/types.ts +5 -0
package/dist/index.esm.js
CHANGED
|
@@ -857,6 +857,10 @@ async function isNoteValid(darkSwap, note, publicKey) {
|
|
|
857
857
|
const onChainStatus = await getNoteOnChainStatus(darkSwap, hexlify32(note.note), hexlify32(nullifier));
|
|
858
858
|
return onChainStatus === NoteOnChainStatus.ACTIVE;
|
|
859
859
|
}
|
|
860
|
+
async function getNullifierBySignature(note, signature) {
|
|
861
|
+
const [publicKey] = await generateKeyPair(signature);
|
|
862
|
+
return hexlify32(calcNullifier(note.rho, publicKey));
|
|
863
|
+
}
|
|
860
864
|
|
|
861
865
|
var abi$a = [
|
|
862
866
|
{
|
|
@@ -2948,7 +2952,6 @@ class DepositService extends BaseContractService {
|
|
|
2948
2952
|
return tx.hash;
|
|
2949
2953
|
}
|
|
2950
2954
|
else {
|
|
2951
|
-
console.log(context.merkleRoot, context.newBalance.asset, hexlify32(context.depositAmount), context.proof.oldBalanceNullifier, hexlify32(context.newBalance.note), context.proof.newBalanceFooter, context.proof.proof);
|
|
2952
2955
|
const tx = await contract.deposit(context.merkleRoot, context.newBalance.asset, hexlify32(context.depositAmount), context.proof.oldBalanceNullifier, hexlify32(context.newBalance.note), context.proof.newBalanceFooter, context.proof.proof, { value: context.depositAmount });
|
|
2953
2956
|
await tx.wait();
|
|
2954
2957
|
return tx.hash;
|
|
@@ -3282,14 +3285,14 @@ class WithdrawService extends BaseContractService {
|
|
|
3282
3285
|
super(_darkSwap);
|
|
3283
3286
|
}
|
|
3284
3287
|
async prepare(address, currentBalance, withdrawAmount, signature) {
|
|
3285
|
-
const [pubKey
|
|
3288
|
+
const [pubKey] = await generateKeyPair(signature);
|
|
3286
3289
|
const newBalanceNote = createNote(address, currentBalance.asset, currentBalance.amount - withdrawAmount, pubKey);
|
|
3287
3290
|
const context = new WithdrawContext(signature);
|
|
3288
3291
|
context.currentBalance = currentBalance;
|
|
3289
3292
|
context.newBalance = newBalanceNote;
|
|
3290
3293
|
context.withdrawAmount = withdrawAmount;
|
|
3291
3294
|
context.address = address;
|
|
3292
|
-
return { context,
|
|
3295
|
+
return { context, newBalanceNote };
|
|
3293
3296
|
}
|
|
3294
3297
|
async generateProof(context) {
|
|
3295
3298
|
if (!context || !context.currentBalance || !context.newBalance || !context.withdrawAmount || !context.address) {
|
|
@@ -3314,7 +3317,8 @@ class WithdrawService extends BaseContractService {
|
|
|
3314
3317
|
throw new DarkSwapError('Invalid context');
|
|
3315
3318
|
}
|
|
3316
3319
|
const contract = new ethers.Contract(this._darkSwap.contracts.darkSwapAssetManager, DarkSwapAssetManagerAbi.abi, this._darkSwap.signer);
|
|
3317
|
-
const tx = await contract.withdraw(context.merkleRoot, context.proof.oldBalanceNullifier, context.
|
|
3320
|
+
const tx = await contract.withdraw(context.merkleRoot, context.currentBalance.asset, context.withdrawAmount, context.proof.oldBalanceNullifier, hexlify32(context.newBalance.note), context.proof.newBalanceFooter, context.proof.proof);
|
|
3321
|
+
await tx.wait();
|
|
3318
3322
|
return tx.hash;
|
|
3319
3323
|
}
|
|
3320
3324
|
}
|
|
@@ -4234,14 +4238,14 @@ async function generateProCreateOrderProof(param) {
|
|
|
4234
4238
|
}
|
|
4235
4239
|
if (param.newBalanceNote.amount < 0n
|
|
4236
4240
|
|| param.oldBalanceNote.amount <= 0n
|
|
4237
|
-
|| param.
|
|
4241
|
+
|| param.inAmount <= 0n
|
|
4238
4242
|
|| param.orderNote.amount <= 0n) {
|
|
4239
4243
|
throw new DarkSwapProofError("Invalid note amount");
|
|
4240
4244
|
}
|
|
4241
4245
|
if (param.orderNote.amount != param.oldBalanceNote.amount - param.newBalanceNote.amount) {
|
|
4242
4246
|
throw new DarkSwapProofError("Invalid order amount");
|
|
4243
4247
|
}
|
|
4244
|
-
const feeAmount = param.
|
|
4248
|
+
const feeAmount = param.inAmount * param.orderNote.feeRatio / FEE_RATIO_PRECISION;
|
|
4245
4249
|
const [[fuzkPubKeyX, fuzkPubKeyY], fuzkPriKey] = await generateKeyPair(param.signedMessage);
|
|
4246
4250
|
let newBalanceFooter = EMPTY_FOOTER;
|
|
4247
4251
|
if (param.oldBalanceNote.amount != 0n) {
|
|
@@ -4277,8 +4281,8 @@ async function generateProCreateOrderProof(param) {
|
|
|
4277
4281
|
order_note_footer: bn_to_0xhex(orderNoteFooter),
|
|
4278
4282
|
order_asset: bn_to_0xhex(encodeAddress(param.orderNote.asset)),
|
|
4279
4283
|
order_amount: bn_to_0xhex(param.orderNote.amount),
|
|
4280
|
-
in_asset: bn_to_0xhex(encodeAddress(param.
|
|
4281
|
-
in_amount: bn_to_0xhex(param.
|
|
4284
|
+
in_asset: bn_to_0xhex(encodeAddress(param.inAsset)),
|
|
4285
|
+
in_amount: bn_to_0xhex(param.inAmount),
|
|
4282
4286
|
pub_key: [fuzkPubKeyX.toString(), fuzkPubKeyY.toString()],
|
|
4283
4287
|
signature: uint8ArrayToNumberArray(signature),
|
|
4284
4288
|
};
|
|
@@ -4312,7 +4316,13 @@ class ProCreateOrderContext extends BaseContext {
|
|
|
4312
4316
|
writable: true,
|
|
4313
4317
|
value: void 0
|
|
4314
4318
|
});
|
|
4315
|
-
Object.defineProperty(this, "
|
|
4319
|
+
Object.defineProperty(this, "_swapInAsset", {
|
|
4320
|
+
enumerable: true,
|
|
4321
|
+
configurable: true,
|
|
4322
|
+
writable: true,
|
|
4323
|
+
value: void 0
|
|
4324
|
+
});
|
|
4325
|
+
Object.defineProperty(this, "_swapInAmount", {
|
|
4316
4326
|
enumerable: true,
|
|
4317
4327
|
configurable: true,
|
|
4318
4328
|
writable: true,
|
|
@@ -4343,11 +4353,17 @@ class ProCreateOrderContext extends BaseContext {
|
|
|
4343
4353
|
get orderNote() {
|
|
4344
4354
|
return this._orderNote;
|
|
4345
4355
|
}
|
|
4346
|
-
set
|
|
4347
|
-
this.
|
|
4356
|
+
set swapInAsset(swapInAsset) {
|
|
4357
|
+
this._swapInAsset = swapInAsset;
|
|
4348
4358
|
}
|
|
4349
|
-
get
|
|
4350
|
-
return this.
|
|
4359
|
+
get swapInAsset() {
|
|
4360
|
+
return this._swapInAsset;
|
|
4361
|
+
}
|
|
4362
|
+
set swapInAmount(swapInAmount) {
|
|
4363
|
+
this._swapInAmount = swapInAmount;
|
|
4364
|
+
}
|
|
4365
|
+
get swapInAmount() {
|
|
4366
|
+
return this._swapInAmount;
|
|
4351
4367
|
}
|
|
4352
4368
|
set oldBalance(oldBalance) {
|
|
4353
4369
|
this._oldBalance = oldBalance;
|
|
@@ -4385,22 +4401,24 @@ class ProCreateOrderService extends BaseContractService {
|
|
|
4385
4401
|
super(_darkSwap);
|
|
4386
4402
|
}
|
|
4387
4403
|
async prepare(address, orderAsset, orderAmount, swapInAsset, swapInAmount, balanceNote, signature) {
|
|
4388
|
-
const [pubKey
|
|
4404
|
+
const [pubKey] = await generateKeyPair(signature);
|
|
4389
4405
|
const orderNote = createOrderNoteExt(address, orderAsset, orderAmount, FEE_RATIO, pubKey);
|
|
4406
|
+
const orderNullifier = hexlify32(calcNullifier(orderNote.rho, pubKey));
|
|
4390
4407
|
const newBalance = createNote(address, orderAsset, balanceNote.amount - orderAmount, pubKey);
|
|
4391
|
-
const swapInNote = createNote(address, swapInAsset, swapInAmount, pubKey);
|
|
4392
4408
|
const context = new ProCreateOrderContext(signature);
|
|
4393
4409
|
context.orderNote = orderNote;
|
|
4394
|
-
context.
|
|
4410
|
+
context.swapInAsset = swapInAsset;
|
|
4411
|
+
context.swapInAmount = swapInAmount;
|
|
4395
4412
|
context.oldBalance = balanceNote;
|
|
4396
4413
|
context.newBalance = newBalance;
|
|
4397
4414
|
context.address = address;
|
|
4398
|
-
return { context, orderNote,
|
|
4415
|
+
return { context, orderNote: { ...orderNote, nullifier: orderNullifier }, newBalance };
|
|
4399
4416
|
}
|
|
4400
4417
|
async generateProof(context) {
|
|
4401
4418
|
if (!context
|
|
4402
4419
|
|| !context.orderNote
|
|
4403
|
-
|| !context.
|
|
4420
|
+
|| !context.swapInAsset
|
|
4421
|
+
|| !context.swapInAmount
|
|
4404
4422
|
|| !context.oldBalance
|
|
4405
4423
|
|| !context.newBalance
|
|
4406
4424
|
|| !context.address
|
|
@@ -4415,7 +4433,8 @@ class ProCreateOrderService extends BaseContractService {
|
|
|
4415
4433
|
orderNote: context.orderNote,
|
|
4416
4434
|
oldBalanceNote: context.oldBalance,
|
|
4417
4435
|
newBalanceNote: context.newBalance,
|
|
4418
|
-
|
|
4436
|
+
inAsset: context.swapInAsset,
|
|
4437
|
+
inAmount: context.swapInAmount,
|
|
4419
4438
|
address: context.address,
|
|
4420
4439
|
signedMessage: context.signature,
|
|
4421
4440
|
});
|
|
@@ -4424,7 +4443,13 @@ class ProCreateOrderService extends BaseContractService {
|
|
|
4424
4443
|
}
|
|
4425
4444
|
async execute(context) {
|
|
4426
4445
|
await this.generateProof(context);
|
|
4427
|
-
if (!context
|
|
4446
|
+
if (!context
|
|
4447
|
+
|| !context.orderNote
|
|
4448
|
+
|| !context.swapInAsset
|
|
4449
|
+
|| !context.swapInAmount
|
|
4450
|
+
|| !context.oldBalance
|
|
4451
|
+
|| !context.newBalance
|
|
4452
|
+
|| !context.proof) {
|
|
4428
4453
|
throw new DarkSwapError('Invalid context');
|
|
4429
4454
|
}
|
|
4430
4455
|
const contract = new ethers.Contract(this._darkSwap.contracts.darkSwapAssetManager, DarkSwapAssetManagerAbi.abi, this._darkSwap.signer);
|
|
@@ -5931,7 +5956,7 @@ var retailCreateOrderCircuit = {
|
|
|
5931
5956
|
|
|
5932
5957
|
async function generateRetailSwapMessage(address, orderNote, swapInNote, pubKey, privKey) {
|
|
5933
5958
|
const addressMod = encodeAddress(address);
|
|
5934
|
-
calcNullifier(orderNote.rho, pubKey);
|
|
5959
|
+
const orderNoteNullifier = hexlify32(calcNullifier(orderNote.rho, pubKey));
|
|
5935
5960
|
const message = bn_to_hex(mimc_bn254([
|
|
5936
5961
|
BigInt(PROOF_DOMAIN.RETAIL_CREATE_ORDER),
|
|
5937
5962
|
addressMod,
|
|
@@ -5942,6 +5967,7 @@ async function generateRetailSwapMessage(address, orderNote, swapInNote, pubKey,
|
|
|
5942
5967
|
const signature = await signMessage(message, privKey);
|
|
5943
5968
|
return {
|
|
5944
5969
|
orderNote: orderNote,
|
|
5970
|
+
orderNullifier: orderNoteNullifier,
|
|
5945
5971
|
inNote: swapInNote,
|
|
5946
5972
|
publicKey: pubKey,
|
|
5947
5973
|
signature: signature,
|
|
@@ -6200,5 +6226,5 @@ class DarkSwap {
|
|
|
6200
6226
|
}
|
|
6201
6227
|
}
|
|
6202
6228
|
|
|
6203
|
-
export { ChainId, DarkSwap, DarkSwapError, DarkSwapProofError, DepositContext, DepositService, EMPTY_FOOTER, EMPTY_NULLIFIER, EMPTY_PATH, FEE_RATIO, FEE_RATIO_PRECISION, JoinService, NoteOnChainStatus, PROOF_DOMAIN, ProCancelOrderService, ProCreateOrderService, ProSwapService, RetailCancelOrderService, RetailCreateOrderService, TripleJoinService, WithdrawService, contractConfig, generateKeyPair, getMerklePathAndRoot, getNoteOnChainStatusByPublicKey, getNoteOnChainStatusBySignature, hexlify32, isAddressEquals, isHexEquals, isNativeAsset, isNoteActive, isNoteSpent, isNoteValid, legacyTokenConfig, multiGetMerklePathAndRoot };
|
|
6229
|
+
export { ChainId, DarkSwap, DarkSwapError, DarkSwapProofError, DepositContext, DepositService, EMPTY_FOOTER, EMPTY_NULLIFIER, EMPTY_PATH, FEE_RATIO, FEE_RATIO_PRECISION, JoinService, NoteOnChainStatus, PROOF_DOMAIN, ProCancelOrderService, ProCreateOrderService, ProSwapService, RetailCancelOrderService, RetailCreateOrderService, TripleJoinService, WithdrawService, contractConfig, generateKeyPair, getMerklePathAndRoot, getNoteOnChainStatusByPublicKey, getNoteOnChainStatusBySignature, getNullifierBySignature, hexlify32, isAddressEquals, isHexEquals, isNativeAsset, isNoteActive, isNoteSpent, isNoteValid, legacyTokenConfig, multiGetMerklePathAndRoot };
|
|
6204
6230
|
//# sourceMappingURL=index.esm.js.map
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -857,6 +857,10 @@ async function isNoteValid(darkSwap, note, publicKey) {
|
|
|
857
857
|
const onChainStatus = await getNoteOnChainStatus(darkSwap, hexlify32(note.note), hexlify32(nullifier));
|
|
858
858
|
return onChainStatus === NoteOnChainStatus.ACTIVE;
|
|
859
859
|
}
|
|
860
|
+
async function getNullifierBySignature(note, signature) {
|
|
861
|
+
const [publicKey] = await generateKeyPair(signature);
|
|
862
|
+
return hexlify32(calcNullifier(note.rho, publicKey));
|
|
863
|
+
}
|
|
860
864
|
|
|
861
865
|
var abi$a = [
|
|
862
866
|
{
|
|
@@ -2948,7 +2952,6 @@ class DepositService extends BaseContractService {
|
|
|
2948
2952
|
return tx.hash;
|
|
2949
2953
|
}
|
|
2950
2954
|
else {
|
|
2951
|
-
console.log(context.merkleRoot, context.newBalance.asset, hexlify32(context.depositAmount), context.proof.oldBalanceNullifier, hexlify32(context.newBalance.note), context.proof.newBalanceFooter, context.proof.proof);
|
|
2952
2955
|
const tx = await contract.deposit(context.merkleRoot, context.newBalance.asset, hexlify32(context.depositAmount), context.proof.oldBalanceNullifier, hexlify32(context.newBalance.note), context.proof.newBalanceFooter, context.proof.proof, { value: context.depositAmount });
|
|
2953
2956
|
await tx.wait();
|
|
2954
2957
|
return tx.hash;
|
|
@@ -3282,14 +3285,14 @@ class WithdrawService extends BaseContractService {
|
|
|
3282
3285
|
super(_darkSwap);
|
|
3283
3286
|
}
|
|
3284
3287
|
async prepare(address, currentBalance, withdrawAmount, signature) {
|
|
3285
|
-
const [pubKey
|
|
3288
|
+
const [pubKey] = await generateKeyPair(signature);
|
|
3286
3289
|
const newBalanceNote = createNote(address, currentBalance.asset, currentBalance.amount - withdrawAmount, pubKey);
|
|
3287
3290
|
const context = new WithdrawContext(signature);
|
|
3288
3291
|
context.currentBalance = currentBalance;
|
|
3289
3292
|
context.newBalance = newBalanceNote;
|
|
3290
3293
|
context.withdrawAmount = withdrawAmount;
|
|
3291
3294
|
context.address = address;
|
|
3292
|
-
return { context,
|
|
3295
|
+
return { context, newBalanceNote };
|
|
3293
3296
|
}
|
|
3294
3297
|
async generateProof(context) {
|
|
3295
3298
|
if (!context || !context.currentBalance || !context.newBalance || !context.withdrawAmount || !context.address) {
|
|
@@ -3314,7 +3317,8 @@ class WithdrawService extends BaseContractService {
|
|
|
3314
3317
|
throw new DarkSwapError('Invalid context');
|
|
3315
3318
|
}
|
|
3316
3319
|
const contract = new ethers.Contract(this._darkSwap.contracts.darkSwapAssetManager, DarkSwapAssetManagerAbi.abi, this._darkSwap.signer);
|
|
3317
|
-
const tx = await contract.withdraw(context.merkleRoot, context.proof.oldBalanceNullifier, context.
|
|
3320
|
+
const tx = await contract.withdraw(context.merkleRoot, context.currentBalance.asset, context.withdrawAmount, context.proof.oldBalanceNullifier, hexlify32(context.newBalance.note), context.proof.newBalanceFooter, context.proof.proof);
|
|
3321
|
+
await tx.wait();
|
|
3318
3322
|
return tx.hash;
|
|
3319
3323
|
}
|
|
3320
3324
|
}
|
|
@@ -4234,14 +4238,14 @@ async function generateProCreateOrderProof(param) {
|
|
|
4234
4238
|
}
|
|
4235
4239
|
if (param.newBalanceNote.amount < 0n
|
|
4236
4240
|
|| param.oldBalanceNote.amount <= 0n
|
|
4237
|
-
|| param.
|
|
4241
|
+
|| param.inAmount <= 0n
|
|
4238
4242
|
|| param.orderNote.amount <= 0n) {
|
|
4239
4243
|
throw new DarkSwapProofError("Invalid note amount");
|
|
4240
4244
|
}
|
|
4241
4245
|
if (param.orderNote.amount != param.oldBalanceNote.amount - param.newBalanceNote.amount) {
|
|
4242
4246
|
throw new DarkSwapProofError("Invalid order amount");
|
|
4243
4247
|
}
|
|
4244
|
-
const feeAmount = param.
|
|
4248
|
+
const feeAmount = param.inAmount * param.orderNote.feeRatio / FEE_RATIO_PRECISION;
|
|
4245
4249
|
const [[fuzkPubKeyX, fuzkPubKeyY], fuzkPriKey] = await generateKeyPair(param.signedMessage);
|
|
4246
4250
|
let newBalanceFooter = EMPTY_FOOTER;
|
|
4247
4251
|
if (param.oldBalanceNote.amount != 0n) {
|
|
@@ -4277,8 +4281,8 @@ async function generateProCreateOrderProof(param) {
|
|
|
4277
4281
|
order_note_footer: bn_to_0xhex(orderNoteFooter),
|
|
4278
4282
|
order_asset: bn_to_0xhex(encodeAddress(param.orderNote.asset)),
|
|
4279
4283
|
order_amount: bn_to_0xhex(param.orderNote.amount),
|
|
4280
|
-
in_asset: bn_to_0xhex(encodeAddress(param.
|
|
4281
|
-
in_amount: bn_to_0xhex(param.
|
|
4284
|
+
in_asset: bn_to_0xhex(encodeAddress(param.inAsset)),
|
|
4285
|
+
in_amount: bn_to_0xhex(param.inAmount),
|
|
4282
4286
|
pub_key: [fuzkPubKeyX.toString(), fuzkPubKeyY.toString()],
|
|
4283
4287
|
signature: uint8ArrayToNumberArray(signature),
|
|
4284
4288
|
};
|
|
@@ -4312,7 +4316,13 @@ class ProCreateOrderContext extends BaseContext {
|
|
|
4312
4316
|
writable: true,
|
|
4313
4317
|
value: void 0
|
|
4314
4318
|
});
|
|
4315
|
-
Object.defineProperty(this, "
|
|
4319
|
+
Object.defineProperty(this, "_swapInAsset", {
|
|
4320
|
+
enumerable: true,
|
|
4321
|
+
configurable: true,
|
|
4322
|
+
writable: true,
|
|
4323
|
+
value: void 0
|
|
4324
|
+
});
|
|
4325
|
+
Object.defineProperty(this, "_swapInAmount", {
|
|
4316
4326
|
enumerable: true,
|
|
4317
4327
|
configurable: true,
|
|
4318
4328
|
writable: true,
|
|
@@ -4343,11 +4353,17 @@ class ProCreateOrderContext extends BaseContext {
|
|
|
4343
4353
|
get orderNote() {
|
|
4344
4354
|
return this._orderNote;
|
|
4345
4355
|
}
|
|
4346
|
-
set
|
|
4347
|
-
this.
|
|
4356
|
+
set swapInAsset(swapInAsset) {
|
|
4357
|
+
this._swapInAsset = swapInAsset;
|
|
4348
4358
|
}
|
|
4349
|
-
get
|
|
4350
|
-
return this.
|
|
4359
|
+
get swapInAsset() {
|
|
4360
|
+
return this._swapInAsset;
|
|
4361
|
+
}
|
|
4362
|
+
set swapInAmount(swapInAmount) {
|
|
4363
|
+
this._swapInAmount = swapInAmount;
|
|
4364
|
+
}
|
|
4365
|
+
get swapInAmount() {
|
|
4366
|
+
return this._swapInAmount;
|
|
4351
4367
|
}
|
|
4352
4368
|
set oldBalance(oldBalance) {
|
|
4353
4369
|
this._oldBalance = oldBalance;
|
|
@@ -4385,22 +4401,24 @@ class ProCreateOrderService extends BaseContractService {
|
|
|
4385
4401
|
super(_darkSwap);
|
|
4386
4402
|
}
|
|
4387
4403
|
async prepare(address, orderAsset, orderAmount, swapInAsset, swapInAmount, balanceNote, signature) {
|
|
4388
|
-
const [pubKey
|
|
4404
|
+
const [pubKey] = await generateKeyPair(signature);
|
|
4389
4405
|
const orderNote = createOrderNoteExt(address, orderAsset, orderAmount, FEE_RATIO, pubKey);
|
|
4406
|
+
const orderNullifier = hexlify32(calcNullifier(orderNote.rho, pubKey));
|
|
4390
4407
|
const newBalance = createNote(address, orderAsset, balanceNote.amount - orderAmount, pubKey);
|
|
4391
|
-
const swapInNote = createNote(address, swapInAsset, swapInAmount, pubKey);
|
|
4392
4408
|
const context = new ProCreateOrderContext(signature);
|
|
4393
4409
|
context.orderNote = orderNote;
|
|
4394
|
-
context.
|
|
4410
|
+
context.swapInAsset = swapInAsset;
|
|
4411
|
+
context.swapInAmount = swapInAmount;
|
|
4395
4412
|
context.oldBalance = balanceNote;
|
|
4396
4413
|
context.newBalance = newBalance;
|
|
4397
4414
|
context.address = address;
|
|
4398
|
-
return { context, orderNote,
|
|
4415
|
+
return { context, orderNote: { ...orderNote, nullifier: orderNullifier }, newBalance };
|
|
4399
4416
|
}
|
|
4400
4417
|
async generateProof(context) {
|
|
4401
4418
|
if (!context
|
|
4402
4419
|
|| !context.orderNote
|
|
4403
|
-
|| !context.
|
|
4420
|
+
|| !context.swapInAsset
|
|
4421
|
+
|| !context.swapInAmount
|
|
4404
4422
|
|| !context.oldBalance
|
|
4405
4423
|
|| !context.newBalance
|
|
4406
4424
|
|| !context.address
|
|
@@ -4415,7 +4433,8 @@ class ProCreateOrderService extends BaseContractService {
|
|
|
4415
4433
|
orderNote: context.orderNote,
|
|
4416
4434
|
oldBalanceNote: context.oldBalance,
|
|
4417
4435
|
newBalanceNote: context.newBalance,
|
|
4418
|
-
|
|
4436
|
+
inAsset: context.swapInAsset,
|
|
4437
|
+
inAmount: context.swapInAmount,
|
|
4419
4438
|
address: context.address,
|
|
4420
4439
|
signedMessage: context.signature,
|
|
4421
4440
|
});
|
|
@@ -4424,7 +4443,13 @@ class ProCreateOrderService extends BaseContractService {
|
|
|
4424
4443
|
}
|
|
4425
4444
|
async execute(context) {
|
|
4426
4445
|
await this.generateProof(context);
|
|
4427
|
-
if (!context
|
|
4446
|
+
if (!context
|
|
4447
|
+
|| !context.orderNote
|
|
4448
|
+
|| !context.swapInAsset
|
|
4449
|
+
|| !context.swapInAmount
|
|
4450
|
+
|| !context.oldBalance
|
|
4451
|
+
|| !context.newBalance
|
|
4452
|
+
|| !context.proof) {
|
|
4428
4453
|
throw new DarkSwapError('Invalid context');
|
|
4429
4454
|
}
|
|
4430
4455
|
const contract = new ethers.Contract(this._darkSwap.contracts.darkSwapAssetManager, DarkSwapAssetManagerAbi.abi, this._darkSwap.signer);
|
|
@@ -5931,7 +5956,7 @@ var retailCreateOrderCircuit = {
|
|
|
5931
5956
|
|
|
5932
5957
|
async function generateRetailSwapMessage(address, orderNote, swapInNote, pubKey, privKey) {
|
|
5933
5958
|
const addressMod = encodeAddress(address);
|
|
5934
|
-
calcNullifier(orderNote.rho, pubKey);
|
|
5959
|
+
const orderNoteNullifier = hexlify32(calcNullifier(orderNote.rho, pubKey));
|
|
5935
5960
|
const message = bn_to_hex(mimc_bn254([
|
|
5936
5961
|
BigInt(PROOF_DOMAIN.RETAIL_CREATE_ORDER),
|
|
5937
5962
|
addressMod,
|
|
@@ -5942,6 +5967,7 @@ async function generateRetailSwapMessage(address, orderNote, swapInNote, pubKey,
|
|
|
5942
5967
|
const signature = await signMessage(message, privKey);
|
|
5943
5968
|
return {
|
|
5944
5969
|
orderNote: orderNote,
|
|
5970
|
+
orderNullifier: orderNoteNullifier,
|
|
5945
5971
|
inNote: swapInNote,
|
|
5946
5972
|
publicKey: pubKey,
|
|
5947
5973
|
signature: signature,
|
|
@@ -6200,5 +6226,5 @@ class DarkSwap {
|
|
|
6200
6226
|
}
|
|
6201
6227
|
}
|
|
6202
6228
|
|
|
6203
|
-
export { ChainId, DarkSwap, DarkSwapError, DarkSwapProofError, DepositContext, DepositService, EMPTY_FOOTER, EMPTY_NULLIFIER, EMPTY_PATH, FEE_RATIO, FEE_RATIO_PRECISION, JoinService, NoteOnChainStatus, PROOF_DOMAIN, ProCancelOrderService, ProCreateOrderService, ProSwapService, RetailCancelOrderService, RetailCreateOrderService, TripleJoinService, WithdrawService, contractConfig, generateKeyPair, getMerklePathAndRoot, getNoteOnChainStatusByPublicKey, getNoteOnChainStatusBySignature, hexlify32, isAddressEquals, isHexEquals, isNativeAsset, isNoteActive, isNoteSpent, isNoteValid, legacyTokenConfig, multiGetMerklePathAndRoot };
|
|
6229
|
+
export { ChainId, DarkSwap, DarkSwapError, DarkSwapProofError, DepositContext, DepositService, EMPTY_FOOTER, EMPTY_NULLIFIER, EMPTY_PATH, FEE_RATIO, FEE_RATIO_PRECISION, JoinService, NoteOnChainStatus, PROOF_DOMAIN, ProCancelOrderService, ProCreateOrderService, ProSwapService, RetailCancelOrderService, RetailCreateOrderService, TripleJoinService, WithdrawService, contractConfig, generateKeyPair, getMerklePathAndRoot, getNoteOnChainStatusByPublicKey, getNoteOnChainStatusBySignature, getNullifierBySignature, hexlify32, isAddressEquals, isHexEquals, isNativeAsset, isNoteActive, isNoteSpent, isNoteValid, legacyTokenConfig, multiGetMerklePathAndRoot };
|
|
6204
6230
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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ainStatus(darkSwap, hexlify32(note.note), hexlify32(nullifier));
|
|
858
858
|
return onChainStatus === exports.NoteOnChainStatus.ACTIVE;
|
|
859
859
|
}
|
|
860
|
+
async function getNullifierBySignature(note, signature) {
|
|
861
|
+
const [publicKey] = await generateKeyPair(signature);
|
|
862
|
+
return hexlify32(calcNullifier(note.rho, publicKey));
|
|
863
|
+
}
|
|
860
864
|
|
|
861
865
|
var abi$a = [
|
|
862
866
|
{
|
|
@@ -2948,7 +2952,6 @@
|
|
|
2948
2952
|
return tx.hash;
|
|
2949
2953
|
}
|
|
2950
2954
|
else {
|
|
2951
|
-
console.log(context.merkleRoot, context.newBalance.asset, hexlify32(context.depositAmount), context.proof.oldBalanceNullifier, hexlify32(context.newBalance.note), context.proof.newBalanceFooter, context.proof.proof);
|
|
2952
2955
|
const tx = await contract.deposit(context.merkleRoot, context.newBalance.asset, hexlify32(context.depositAmount), context.proof.oldBalanceNullifier, hexlify32(context.newBalance.note), context.proof.newBalanceFooter, context.proof.proof, { value: context.depositAmount });
|
|
2953
2956
|
await tx.wait();
|
|
2954
2957
|
return tx.hash;
|
|
@@ -3282,14 +3285,14 @@
|
|
|
3282
3285
|
super(_darkSwap);
|
|
3283
3286
|
}
|
|
3284
3287
|
async prepare(address, currentBalance, withdrawAmount, signature) {
|
|
3285
|
-
const [pubKey
|
|
3288
|
+
const [pubKey] = await generateKeyPair(signature);
|
|
3286
3289
|
const newBalanceNote = createNote(address, currentBalance.asset, currentBalance.amount - withdrawAmount, pubKey);
|
|
3287
3290
|
const context = new WithdrawContext(signature);
|
|
3288
3291
|
context.currentBalance = currentBalance;
|
|
3289
3292
|
context.newBalance = newBalanceNote;
|
|
3290
3293
|
context.withdrawAmount = withdrawAmount;
|
|
3291
3294
|
context.address = address;
|
|
3292
|
-
return { context,
|
|
3295
|
+
return { context, newBalanceNote };
|
|
3293
3296
|
}
|
|
3294
3297
|
async generateProof(context) {
|
|
3295
3298
|
if (!context || !context.currentBalance || !context.newBalance || !context.withdrawAmount || !context.address) {
|
|
@@ -3314,7 +3317,8 @@
|
|
|
3314
3317
|
throw new DarkSwapError('Invalid context');
|
|
3315
3318
|
}
|
|
3316
3319
|
const contract = new ethers.ethers.Contract(this._darkSwap.contracts.darkSwapAssetManager, DarkSwapAssetManagerAbi.abi, this._darkSwap.signer);
|
|
3317
|
-
const tx = await contract.withdraw(context.merkleRoot, context.proof.oldBalanceNullifier, context.
|
|
3320
|
+
const tx = await contract.withdraw(context.merkleRoot, context.currentBalance.asset, context.withdrawAmount, context.proof.oldBalanceNullifier, hexlify32(context.newBalance.note), context.proof.newBalanceFooter, context.proof.proof);
|
|
3321
|
+
await tx.wait();
|
|
3318
3322
|
return tx.hash;
|
|
3319
3323
|
}
|
|
3320
3324
|
}
|
|
@@ -4234,14 +4238,14 @@
|
|
|
4234
4238
|
}
|
|
4235
4239
|
if (param.newBalanceNote.amount < 0n
|
|
4236
4240
|
|| param.oldBalanceNote.amount <= 0n
|
|
4237
|
-
|| param.
|
|
4241
|
+
|| param.inAmount <= 0n
|
|
4238
4242
|
|| param.orderNote.amount <= 0n) {
|
|
4239
4243
|
throw new DarkSwapProofError("Invalid note amount");
|
|
4240
4244
|
}
|
|
4241
4245
|
if (param.orderNote.amount != param.oldBalanceNote.amount - param.newBalanceNote.amount) {
|
|
4242
4246
|
throw new DarkSwapProofError("Invalid order amount");
|
|
4243
4247
|
}
|
|
4244
|
-
const feeAmount = param.
|
|
4248
|
+
const feeAmount = param.inAmount * param.orderNote.feeRatio / FEE_RATIO_PRECISION;
|
|
4245
4249
|
const [[fuzkPubKeyX, fuzkPubKeyY], fuzkPriKey] = await generateKeyPair(param.signedMessage);
|
|
4246
4250
|
let newBalanceFooter = EMPTY_FOOTER;
|
|
4247
4251
|
if (param.oldBalanceNote.amount != 0n) {
|
|
@@ -4277,8 +4281,8 @@
|
|
|
4277
4281
|
order_note_footer: bn_to_0xhex(orderNoteFooter),
|
|
4278
4282
|
order_asset: bn_to_0xhex(encodeAddress(param.orderNote.asset)),
|
|
4279
4283
|
order_amount: bn_to_0xhex(param.orderNote.amount),
|
|
4280
|
-
in_asset: bn_to_0xhex(encodeAddress(param.
|
|
4281
|
-
in_amount: bn_to_0xhex(param.
|
|
4284
|
+
in_asset: bn_to_0xhex(encodeAddress(param.inAsset)),
|
|
4285
|
+
in_amount: bn_to_0xhex(param.inAmount),
|
|
4282
4286
|
pub_key: [fuzkPubKeyX.toString(), fuzkPubKeyY.toString()],
|
|
4283
4287
|
signature: uint8ArrayToNumberArray(signature),
|
|
4284
4288
|
};
|
|
@@ -4312,7 +4316,13 @@
|
|
|
4312
4316
|
writable: true,
|
|
4313
4317
|
value: void 0
|
|
4314
4318
|
});
|
|
4315
|
-
Object.defineProperty(this, "
|
|
4319
|
+
Object.defineProperty(this, "_swapInAsset", {
|
|
4320
|
+
enumerable: true,
|
|
4321
|
+
configurable: true,
|
|
4322
|
+
writable: true,
|
|
4323
|
+
value: void 0
|
|
4324
|
+
});
|
|
4325
|
+
Object.defineProperty(this, "_swapInAmount", {
|
|
4316
4326
|
enumerable: true,
|
|
4317
4327
|
configurable: true,
|
|
4318
4328
|
writable: true,
|
|
@@ -4343,11 +4353,17 @@
|
|
|
4343
4353
|
get orderNote() {
|
|
4344
4354
|
return this._orderNote;
|
|
4345
4355
|
}
|
|
4346
|
-
set
|
|
4347
|
-
this.
|
|
4356
|
+
set swapInAsset(swapInAsset) {
|
|
4357
|
+
this._swapInAsset = swapInAsset;
|
|
4348
4358
|
}
|
|
4349
|
-
get
|
|
4350
|
-
return this.
|
|
4359
|
+
get swapInAsset() {
|
|
4360
|
+
return this._swapInAsset;
|
|
4361
|
+
}
|
|
4362
|
+
set swapInAmount(swapInAmount) {
|
|
4363
|
+
this._swapInAmount = swapInAmount;
|
|
4364
|
+
}
|
|
4365
|
+
get swapInAmount() {
|
|
4366
|
+
return this._swapInAmount;
|
|
4351
4367
|
}
|
|
4352
4368
|
set oldBalance(oldBalance) {
|
|
4353
4369
|
this._oldBalance = oldBalance;
|
|
@@ -4385,22 +4401,24 @@
|
|
|
4385
4401
|
super(_darkSwap);
|
|
4386
4402
|
}
|
|
4387
4403
|
async prepare(address, orderAsset, orderAmount, swapInAsset, swapInAmount, balanceNote, signature) {
|
|
4388
|
-
const [pubKey
|
|
4404
|
+
const [pubKey] = await generateKeyPair(signature);
|
|
4389
4405
|
const orderNote = createOrderNoteExt(address, orderAsset, orderAmount, FEE_RATIO, pubKey);
|
|
4406
|
+
const orderNullifier = hexlify32(calcNullifier(orderNote.rho, pubKey));
|
|
4390
4407
|
const newBalance = createNote(address, orderAsset, balanceNote.amount - orderAmount, pubKey);
|
|
4391
|
-
const swapInNote = createNote(address, swapInAsset, swapInAmount, pubKey);
|
|
4392
4408
|
const context = new ProCreateOrderContext(signature);
|
|
4393
4409
|
context.orderNote = orderNote;
|
|
4394
|
-
context.
|
|
4410
|
+
context.swapInAsset = swapInAsset;
|
|
4411
|
+
context.swapInAmount = swapInAmount;
|
|
4395
4412
|
context.oldBalance = balanceNote;
|
|
4396
4413
|
context.newBalance = newBalance;
|
|
4397
4414
|
context.address = address;
|
|
4398
|
-
return { context, orderNote,
|
|
4415
|
+
return { context, orderNote: { ...orderNote, nullifier: orderNullifier }, newBalance };
|
|
4399
4416
|
}
|
|
4400
4417
|
async generateProof(context) {
|
|
4401
4418
|
if (!context
|
|
4402
4419
|
|| !context.orderNote
|
|
4403
|
-
|| !context.
|
|
4420
|
+
|| !context.swapInAsset
|
|
4421
|
+
|| !context.swapInAmount
|
|
4404
4422
|
|| !context.oldBalance
|
|
4405
4423
|
|| !context.newBalance
|
|
4406
4424
|
|| !context.address
|
|
@@ -4415,7 +4433,8 @@
|
|
|
4415
4433
|
orderNote: context.orderNote,
|
|
4416
4434
|
oldBalanceNote: context.oldBalance,
|
|
4417
4435
|
newBalanceNote: context.newBalance,
|
|
4418
|
-
|
|
4436
|
+
inAsset: context.swapInAsset,
|
|
4437
|
+
inAmount: context.swapInAmount,
|
|
4419
4438
|
address: context.address,
|
|
4420
4439
|
signedMessage: context.signature,
|
|
4421
4440
|
});
|
|
@@ -4424,7 +4443,13 @@
|
|
|
4424
4443
|
}
|
|
4425
4444
|
async execute(context) {
|
|
4426
4445
|
await this.generateProof(context);
|
|
4427
|
-
if (!context
|
|
4446
|
+
if (!context
|
|
4447
|
+
|| !context.orderNote
|
|
4448
|
+
|| !context.swapInAsset
|
|
4449
|
+
|| !context.swapInAmount
|
|
4450
|
+
|| !context.oldBalance
|
|
4451
|
+
|| !context.newBalance
|
|
4452
|
+
|| !context.proof) {
|
|
4428
4453
|
throw new DarkSwapError('Invalid context');
|
|
4429
4454
|
}
|
|
4430
4455
|
const contract = new ethers.ethers.Contract(this._darkSwap.contracts.darkSwapAssetManager, DarkSwapAssetManagerAbi.abi, this._darkSwap.signer);
|
|
@@ -5931,7 +5956,7 @@
|
|
|
5931
5956
|
|
|
5932
5957
|
async function generateRetailSwapMessage(address, orderNote, swapInNote, pubKey, privKey) {
|
|
5933
5958
|
const addressMod = encodeAddress(address);
|
|
5934
|
-
calcNullifier(orderNote.rho, pubKey);
|
|
5959
|
+
const orderNoteNullifier = hexlify32(calcNullifier(orderNote.rho, pubKey));
|
|
5935
5960
|
const message = bn_to_hex(mimc_bn254([
|
|
5936
5961
|
BigInt(exports.PROOF_DOMAIN.RETAIL_CREATE_ORDER),
|
|
5937
5962
|
addressMod,
|
|
@@ -5942,6 +5967,7 @@
|
|
|
5942
5967
|
const signature = await signMessage(message, privKey);
|
|
5943
5968
|
return {
|
|
5944
5969
|
orderNote: orderNote,
|
|
5970
|
+
orderNullifier: orderNoteNullifier,
|
|
5945
5971
|
inNote: swapInNote,
|
|
5946
5972
|
publicKey: pubKey,
|
|
5947
5973
|
signature: signature,
|
|
@@ -6223,6 +6249,7 @@
|
|
|
6223
6249
|
exports.getMerklePathAndRoot = getMerklePathAndRoot;
|
|
6224
6250
|
exports.getNoteOnChainStatusByPublicKey = getNoteOnChainStatusByPublicKey;
|
|
6225
6251
|
exports.getNoteOnChainStatusBySignature = getNoteOnChainStatusBySignature;
|
|
6252
|
+
exports.getNullifierBySignature = getNullifierBySignature;
|
|
6226
6253
|
exports.hexlify32 = hexlify32;
|
|
6227
6254
|
exports.isAddressEquals = isAddressEquals;
|
|
6228
6255
|
exports.isHexEquals = isHexEquals;
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -6,7 +6,8 @@ export type ProCreateOrderProofParam = BaseProofParam & {
|
|
|
6
6
|
oldBalanceNote: DarkSwapNote;
|
|
7
7
|
newBalanceNote: DarkSwapNote;
|
|
8
8
|
orderNote: DarkSwapOrderNote;
|
|
9
|
-
|
|
9
|
+
inAsset: string;
|
|
10
|
+
inAmount: bigint;
|
|
10
11
|
};
|
|
11
12
|
export type ProCreateOrderProofResult = BaseProofResult & {
|
|
12
13
|
oldBalanceNullifier: string;
|
|
@@ -21,7 +21,7 @@ export declare class WithdrawService extends BaseContractService {
|
|
|
21
21
|
constructor(_darkSwap: DarkSwap);
|
|
22
22
|
prepare(address: string, currentBalance: DarkSwapNote, withdrawAmount: bigint, signature: string): Promise<{
|
|
23
23
|
context: WithdrawContext;
|
|
24
|
-
|
|
24
|
+
newBalanceNote: DarkSwapNote;
|
|
25
25
|
}>;
|
|
26
26
|
private generateProof;
|
|
27
27
|
execute(context: WithdrawContext): Promise<string>;
|
|
@@ -7,3 +7,4 @@ export declare function getNoteOnChainStatusBySignature(darkSwap: DarkSwap, note
|
|
|
7
7
|
export declare function isNoteActive(darkSwap: DarkSwap, note: DarkSwapNote, publicKey: [Fr, Fr]): Promise<boolean>;
|
|
8
8
|
export declare function isNoteSpent(darkSwap: DarkSwap, note: DarkSwapNote, publicKey: [Fr, Fr]): Promise<boolean>;
|
|
9
9
|
export declare function isNoteValid(darkSwap: DarkSwap, note: DarkSwapNote, publicKey: [Fr, Fr]): Promise<boolean>;
|
|
10
|
+
export declare function getNullifierBySignature(note: DarkSwapNote, signature: string): Promise<string>;
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import { DarkSwap } from '../../darkSwap';
|
|
2
2
|
import { ProCreateOrderProofResult } from '../../proof/pro/orders/createOrderProof';
|
|
3
|
-
import { DarkSwapMessage, DarkSwapNote, DarkSwapOrderNote } from '../../types';
|
|
3
|
+
import { DarkSwapMessage, DarkSwapNote, DarkSwapOrderNote, DarkSwapOrderNoteExt } from '../../types';
|
|
4
4
|
import { BaseContext, BaseContractService } from '../BaseService';
|
|
5
5
|
declare class ProCreateOrderContext extends BaseContext {
|
|
6
6
|
private _orderNote?;
|
|
7
7
|
private _oldBalance?;
|
|
8
8
|
private _newBalance?;
|
|
9
|
-
private
|
|
9
|
+
private _swapInAsset?;
|
|
10
|
+
private _swapInAmount?;
|
|
10
11
|
private _proof?;
|
|
11
12
|
private _feeAmount?;
|
|
12
13
|
private _swapMessage?;
|
|
13
14
|
constructor(signature: string);
|
|
14
15
|
set orderNote(orderNote: DarkSwapOrderNote | undefined);
|
|
15
16
|
get orderNote(): DarkSwapOrderNote | undefined;
|
|
16
|
-
set
|
|
17
|
-
get
|
|
17
|
+
set swapInAsset(swapInAsset: string | undefined);
|
|
18
|
+
get swapInAsset(): string | undefined;
|
|
19
|
+
set swapInAmount(swapInAmount: bigint | undefined);
|
|
20
|
+
get swapInAmount(): bigint | undefined;
|
|
18
21
|
set oldBalance(oldBalance: DarkSwapNote | undefined);
|
|
19
22
|
get oldBalance(): DarkSwapNote | undefined;
|
|
20
23
|
set newBalance(newBalance: DarkSwapNote | undefined);
|
|
@@ -30,8 +33,7 @@ export declare class ProCreateOrderService extends BaseContractService {
|
|
|
30
33
|
constructor(_darkSwap: DarkSwap);
|
|
31
34
|
prepare(address: string, orderAsset: string, orderAmount: bigint, swapInAsset: string, swapInAmount: bigint, balanceNote: DarkSwapNote, signature: string): Promise<{
|
|
32
35
|
context: ProCreateOrderContext;
|
|
33
|
-
orderNote:
|
|
34
|
-
swapInNote: DarkSwapNote;
|
|
36
|
+
orderNote: DarkSwapOrderNoteExt;
|
|
35
37
|
newBalance: DarkSwapNote;
|
|
36
38
|
}>;
|
|
37
39
|
private generateProof;
|
|
@@ -24,6 +24,9 @@ export type DarkSwapNote = CreateNoteParam & {
|
|
|
24
24
|
export type DarkSwapOrderNote = DarkSwapNote & {
|
|
25
25
|
feeRatio: bigint;
|
|
26
26
|
};
|
|
27
|
+
export type DarkSwapOrderNoteExt = DarkSwapOrderNote & {
|
|
28
|
+
nullifier: string;
|
|
29
|
+
};
|
|
27
30
|
export type CreateNoteParam = {
|
|
28
31
|
rho: bigint;
|
|
29
32
|
amount: bigint;
|
|
@@ -47,6 +50,7 @@ export type BaseProofInput = {
|
|
|
47
50
|
};
|
|
48
51
|
export type DarkSwapMessage = {
|
|
49
52
|
orderNote: DarkSwapOrderNote;
|
|
53
|
+
orderNullifier: string;
|
|
50
54
|
inNote: DarkSwapNote;
|
|
51
55
|
publicKey: [Fr, Fr];
|
|
52
56
|
signature: any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -2,4 +2,5 @@ import { ethers } from "ethers";
|
|
|
2
2
|
import { DarkSwap } from "../../src";
|
|
3
3
|
export declare function getAliceWallet(): ethers.Wallet;
|
|
4
4
|
export declare function getAliceSignature(): Promise<string>;
|
|
5
|
+
export declare function getAliceWalletBalance(asset: string): Promise<any>;
|
|
5
6
|
export declare function getDarkSwapForAlice(): DarkSwap;
|
package/package.json
CHANGED
|
@@ -47,7 +47,8 @@ export type ProCreateOrderProofParam = BaseProofParam & {
|
|
|
47
47
|
oldBalanceNote: DarkSwapNote,
|
|
48
48
|
newBalanceNote: DarkSwapNote,
|
|
49
49
|
orderNote: DarkSwapOrderNote,
|
|
50
|
-
|
|
50
|
+
inAsset: string,
|
|
51
|
+
inAmount: bigint,
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
export type ProCreateOrderProofResult = BaseProofResult & {
|
|
@@ -63,7 +64,7 @@ export async function generateProCreateOrderProof(param: ProCreateOrderProofPara
|
|
|
63
64
|
|
|
64
65
|
if (param.newBalanceNote.amount < 0n
|
|
65
66
|
|| param.oldBalanceNote.amount <= 0n
|
|
66
|
-
|| param.
|
|
67
|
+
|| param.inAmount <= 0n
|
|
67
68
|
|| param.orderNote.amount <= 0n) {
|
|
68
69
|
throw new DarkSwapProofError("Invalid note amount");
|
|
69
70
|
}
|
|
@@ -72,7 +73,7 @@ export async function generateProCreateOrderProof(param: ProCreateOrderProofPara
|
|
|
72
73
|
throw new DarkSwapProofError("Invalid order amount");
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
const feeAmount = param.
|
|
76
|
+
const feeAmount = param.inAmount * param.orderNote.feeRatio / FEE_RATIO_PRECISION;
|
|
76
77
|
|
|
77
78
|
const [[fuzkPubKeyX, fuzkPubKeyY], fuzkPriKey] = await generateKeyPair(param.signedMessage);
|
|
78
79
|
|
|
@@ -116,8 +117,8 @@ export async function generateProCreateOrderProof(param: ProCreateOrderProofPara
|
|
|
116
117
|
order_note_footer: bn_to_0xhex(orderNoteFooter),
|
|
117
118
|
order_asset: bn_to_0xhex(encodeAddress(param.orderNote.asset)),
|
|
118
119
|
order_amount: bn_to_0xhex(param.orderNote.amount),
|
|
119
|
-
in_asset: bn_to_0xhex(encodeAddress(param.
|
|
120
|
-
in_amount: bn_to_0xhex(param.
|
|
120
|
+
in_asset: bn_to_0xhex(encodeAddress(param.inAsset)),
|
|
121
|
+
in_amount: bn_to_0xhex(param.inAmount),
|
|
121
122
|
|
|
122
123
|
pub_key: [fuzkPubKeyX.toString(), fuzkPubKeyY.toString()],
|
|
123
124
|
signature: uint8ArrayToNumberArray(signature),
|
|
@@ -9,6 +9,7 @@ import { generateKeyPair } from "../keyService";
|
|
|
9
9
|
import { calcNullifier, getNoteFooter } from "../noteService";
|
|
10
10
|
import { Fr } from "@aztec/foundation/fields";
|
|
11
11
|
import retailCreateOrderCircuit from "../../circuits/retail/dark_swap_retail_deposit_create_order_compiled_circuit.json";
|
|
12
|
+
import { hexlify32 } from "../../utils/util";
|
|
12
13
|
|
|
13
14
|
type RetailCreateOrderProofInput = BaseProofInput & {
|
|
14
15
|
deposit_out_note: string,
|
|
@@ -53,7 +54,7 @@ export async function generateRetailSwapMessage(
|
|
|
53
54
|
): Promise<DarkSwapMessage> {
|
|
54
55
|
|
|
55
56
|
const addressMod = encodeAddress(address);
|
|
56
|
-
const orderNoteNullifier = calcNullifier(orderNote.rho, pubKey);
|
|
57
|
+
const orderNoteNullifier = hexlify32(calcNullifier(orderNote.rho, pubKey));
|
|
57
58
|
const message = bn_to_hex(mimc_bn254([
|
|
58
59
|
BigInt(PROOF_DOMAIN.RETAIL_CREATE_ORDER),
|
|
59
60
|
addressMod,
|
|
@@ -65,6 +66,7 @@ export async function generateRetailSwapMessage(
|
|
|
65
66
|
|
|
66
67
|
return {
|
|
67
68
|
orderNote: orderNote,
|
|
69
|
+
orderNullifier: orderNoteNullifier,
|
|
68
70
|
inNote: swapInNote,
|
|
69
71
|
publicKey: pubKey,
|
|
70
72
|
signature: signature,
|
|
@@ -137,13 +137,6 @@ export class DepositService extends BaseContractService {
|
|
|
137
137
|
await tx.wait();
|
|
138
138
|
return tx.hash;
|
|
139
139
|
} else {
|
|
140
|
-
console.log(context.merkleRoot,
|
|
141
|
-
context.newBalance.asset,
|
|
142
|
-
hexlify32(context.depositAmount),
|
|
143
|
-
context.proof.oldBalanceNullifier,
|
|
144
|
-
hexlify32(context.newBalance.note),
|
|
145
|
-
context.proof.newBalanceFooter,
|
|
146
|
-
context.proof.proof);
|
|
147
140
|
const tx = await contract.deposit(
|
|
148
141
|
context.merkleRoot,
|
|
149
142
|
context.newBalance.asset,
|
|
@@ -8,6 +8,7 @@ import { createNote } from '../../proof/noteService';
|
|
|
8
8
|
import { DarkSwapNote } from '../../types';
|
|
9
9
|
import { BaseContext, BaseContractService } from '../BaseService';
|
|
10
10
|
import { getMerklePathAndRoot } from '../merkletree';
|
|
11
|
+
import { hexlify32 } from '../../utils/util';
|
|
11
12
|
|
|
12
13
|
class WithdrawContext extends BaseContext {
|
|
13
14
|
private _currentBalance?: DarkSwapNote;
|
|
@@ -62,8 +63,8 @@ export class WithdrawService extends BaseContractService {
|
|
|
62
63
|
currentBalance: DarkSwapNote,
|
|
63
64
|
withdrawAmount: bigint,
|
|
64
65
|
signature: string
|
|
65
|
-
): Promise<{ context: WithdrawContext;
|
|
66
|
-
const [pubKey
|
|
66
|
+
): Promise<{ context: WithdrawContext; newBalanceNote: DarkSwapNote }> {
|
|
67
|
+
const [pubKey] = await generateKeyPair(signature);
|
|
67
68
|
const newBalanceNote = createNote(address, currentBalance.asset, currentBalance.amount - withdrawAmount, pubKey);
|
|
68
69
|
|
|
69
70
|
const context = new WithdrawContext(signature);
|
|
@@ -71,7 +72,7 @@ export class WithdrawService extends BaseContractService {
|
|
|
71
72
|
context.newBalance = newBalanceNote;
|
|
72
73
|
context.withdrawAmount = withdrawAmount;
|
|
73
74
|
context.address = address;
|
|
74
|
-
return { context,
|
|
75
|
+
return { context, newBalanceNote };
|
|
75
76
|
}
|
|
76
77
|
|
|
77
78
|
private async generateProof(context: WithdrawContext): Promise<void> {
|
|
@@ -109,11 +110,14 @@ export class WithdrawService extends BaseContractService {
|
|
|
109
110
|
|
|
110
111
|
const tx = await contract.withdraw(
|
|
111
112
|
context.merkleRoot,
|
|
113
|
+
context.currentBalance.asset,
|
|
114
|
+
context.withdrawAmount,
|
|
112
115
|
context.proof.oldBalanceNullifier,
|
|
116
|
+
hexlify32(context.newBalance.note),
|
|
113
117
|
context.proof.newBalanceFooter,
|
|
114
|
-
context.withdrawAmount,
|
|
115
118
|
context.proof.proof
|
|
116
119
|
);
|
|
120
|
+
await tx.wait();
|
|
117
121
|
return tx.hash;
|
|
118
122
|
}
|
|
119
123
|
}
|
|
@@ -68,3 +68,8 @@ export async function isNoteValid(darkSwap: DarkSwap, note: DarkSwapNote, public
|
|
|
68
68
|
const onChainStatus = await getNoteOnChainStatus(darkSwap, hexlify32(note.note), hexlify32(nullifier));
|
|
69
69
|
return onChainStatus === NoteOnChainStatus.ACTIVE;
|
|
70
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
|
+
}
|
|
@@ -4,17 +4,19 @@ import { FEE_RATIO } from '../../config/config';
|
|
|
4
4
|
import { DarkSwap } from '../../darkSwap';
|
|
5
5
|
import { DarkSwapError } from '../../entities';
|
|
6
6
|
import { generateKeyPair } from '../../proof/keyService';
|
|
7
|
-
import { createNote, createOrderNoteExt } from '../../proof/noteService';
|
|
7
|
+
import { calcNullifier, createNote, createOrderNoteExt } from '../../proof/noteService';
|
|
8
8
|
import { generateProCreateOrderProof, ProCreateOrderProofResult } from '../../proof/pro/orders/createOrderProof';
|
|
9
|
-
import { DarkSwapMessage, DarkSwapNote, DarkSwapOrderNote } from '../../types';
|
|
9
|
+
import { DarkSwapMessage, DarkSwapNote, DarkSwapOrderNote, DarkSwapOrderNoteExt } from '../../types';
|
|
10
10
|
import { BaseContext, BaseContractService } from '../BaseService';
|
|
11
11
|
import { getMerklePathAndRoot } from '../merkletree';
|
|
12
|
+
import { hexlify32 } from '../../utils/util';
|
|
12
13
|
|
|
13
14
|
class ProCreateOrderContext extends BaseContext {
|
|
14
15
|
private _orderNote?: DarkSwapOrderNote;
|
|
15
16
|
private _oldBalance?: DarkSwapNote;
|
|
16
17
|
private _newBalance?: DarkSwapNote;
|
|
17
|
-
private
|
|
18
|
+
private _swapInAsset?: string;
|
|
19
|
+
private _swapInAmount?: bigint;
|
|
18
20
|
private _proof?: ProCreateOrderProofResult;
|
|
19
21
|
private _feeAmount?: bigint;
|
|
20
22
|
private _swapMessage?: DarkSwapMessage;
|
|
@@ -31,12 +33,20 @@ class ProCreateOrderContext extends BaseContext {
|
|
|
31
33
|
return this._orderNote;
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
set
|
|
35
|
-
this.
|
|
36
|
+
set swapInAsset(swapInAsset: string | undefined) {
|
|
37
|
+
this._swapInAsset = swapInAsset;
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
get
|
|
39
|
-
return this.
|
|
40
|
+
get swapInAsset(): string | undefined {
|
|
41
|
+
return this._swapInAsset;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
set swapInAmount(swapInAmount: bigint | undefined) {
|
|
45
|
+
this._swapInAmount = swapInAmount;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get swapInAmount(): bigint | undefined {
|
|
49
|
+
return this._swapInAmount;
|
|
40
50
|
}
|
|
41
51
|
|
|
42
52
|
set oldBalance(oldBalance: DarkSwapNote | undefined) {
|
|
@@ -93,24 +103,26 @@ export class ProCreateOrderService extends BaseContractService {
|
|
|
93
103
|
swapInAmount: bigint,
|
|
94
104
|
balanceNote: DarkSwapNote,
|
|
95
105
|
signature: string
|
|
96
|
-
): Promise<{ context: ProCreateOrderContext; orderNote:
|
|
97
|
-
const [pubKey
|
|
106
|
+
): Promise<{ context: ProCreateOrderContext; orderNote: DarkSwapOrderNoteExt, newBalance: DarkSwapNote }> {
|
|
107
|
+
const [pubKey] = await generateKeyPair(signature);
|
|
98
108
|
const orderNote = createOrderNoteExt(address, orderAsset, orderAmount, FEE_RATIO, pubKey);
|
|
109
|
+
const orderNullifier = hexlify32(calcNullifier(orderNote.rho, pubKey));
|
|
99
110
|
const newBalance = createNote(address, orderAsset, balanceNote.amount - orderAmount, pubKey);
|
|
100
|
-
const swapInNote = createNote(address, swapInAsset, swapInAmount, pubKey);
|
|
101
111
|
const context = new ProCreateOrderContext(signature);
|
|
102
112
|
context.orderNote = orderNote;
|
|
103
|
-
context.
|
|
113
|
+
context.swapInAsset = swapInAsset;
|
|
114
|
+
context.swapInAmount = swapInAmount;
|
|
104
115
|
context.oldBalance = balanceNote;
|
|
105
116
|
context.newBalance = newBalance;
|
|
106
117
|
context.address = address;
|
|
107
|
-
return { context, orderNote,
|
|
118
|
+
return { context, orderNote: { ...orderNote, nullifier: orderNullifier }, newBalance };
|
|
108
119
|
}
|
|
109
120
|
|
|
110
121
|
private async generateProof(context: ProCreateOrderContext): Promise<void> {
|
|
111
122
|
if (!context
|
|
112
123
|
|| !context.orderNote
|
|
113
|
-
|| !context.
|
|
124
|
+
|| !context.swapInAsset
|
|
125
|
+
|| !context.swapInAmount
|
|
114
126
|
|| !context.oldBalance
|
|
115
127
|
|| !context.newBalance
|
|
116
128
|
|| !context.address
|
|
@@ -127,7 +139,8 @@ export class ProCreateOrderService extends BaseContractService {
|
|
|
127
139
|
orderNote: context.orderNote,
|
|
128
140
|
oldBalanceNote: context.oldBalance,
|
|
129
141
|
newBalanceNote: context.newBalance,
|
|
130
|
-
|
|
142
|
+
inAsset: context.swapInAsset,
|
|
143
|
+
inAmount: context.swapInAmount,
|
|
131
144
|
address: context.address,
|
|
132
145
|
signedMessage: context.signature,
|
|
133
146
|
});
|
|
@@ -137,7 +150,13 @@ export class ProCreateOrderService extends BaseContractService {
|
|
|
137
150
|
|
|
138
151
|
public async execute(context: ProCreateOrderContext): Promise<string> {
|
|
139
152
|
await this.generateProof(context);
|
|
140
|
-
if (!context
|
|
153
|
+
if (!context
|
|
154
|
+
|| !context.orderNote
|
|
155
|
+
|| !context.swapInAsset
|
|
156
|
+
|| !context.swapInAmount
|
|
157
|
+
|| !context.oldBalance
|
|
158
|
+
|| !context.newBalance
|
|
159
|
+
|| !context.proof) {
|
|
141
160
|
throw new DarkSwapError('Invalid context');
|
|
142
161
|
}
|
|
143
162
|
|
package/src/types.ts
CHANGED
|
@@ -35,6 +35,10 @@ export type DarkSwapOrderNote = DarkSwapNote & {
|
|
|
35
35
|
feeRatio: bigint,
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
export type DarkSwapOrderNoteExt = DarkSwapOrderNote & {
|
|
39
|
+
nullifier: string,
|
|
40
|
+
}
|
|
41
|
+
|
|
38
42
|
export type CreateNoteParam = {
|
|
39
43
|
rho: bigint,
|
|
40
44
|
amount: bigint,
|
|
@@ -62,6 +66,7 @@ export type BaseProofInput = {
|
|
|
62
66
|
|
|
63
67
|
export type DarkSwapMessage = {
|
|
64
68
|
orderNote: DarkSwapOrderNote,
|
|
69
|
+
orderNullifier: string,
|
|
65
70
|
inNote: DarkSwapNote,
|
|
66
71
|
publicKey: [Fr, Fr],
|
|
67
72
|
signature: any,
|