@interest-protocol/vortex-sdk 0.0.1-alpha.0 → 1.0.0
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/.eslingignore +1 -0
- package/dist/__tests__/entities/keypair.spec.d.ts +2 -0
- package/dist/__tests__/entities/keypair.spec.d.ts.map +1 -0
- package/dist/__tests__/test-utils.d.ts +25 -0
- package/dist/__tests__/test-utils.d.ts.map +1 -0
- package/dist/__tests__/types.d.ts +3 -0
- package/dist/__tests__/types.d.ts.map +1 -0
- package/dist/__tests__/vortex.test.d.ts +2 -0
- package/dist/__tests__/vortex.test.d.ts.map +1 -0
- package/dist/constants.d.ts +44 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/crypto/ff/f1field.d.ts +76 -0
- package/dist/crypto/ff/f1field.d.ts.map +1 -0
- package/dist/crypto/ff/index.d.ts +6 -0
- package/dist/crypto/ff/index.d.ts.map +1 -0
- package/dist/crypto/ff/random.d.ts +2 -0
- package/dist/crypto/ff/random.d.ts.map +1 -0
- package/dist/crypto/ff/scalar.d.ts +45 -0
- package/dist/crypto/ff/scalar.d.ts.map +1 -0
- package/dist/crypto/ff/utils.d.ts +6 -0
- package/dist/crypto/ff/utils.d.ts.map +1 -0
- package/dist/crypto/index.d.ts +6 -0
- package/dist/crypto/index.d.ts.map +1 -0
- package/dist/crypto/poseidon/index.d.ts +2 -0
- package/dist/crypto/poseidon/index.d.ts.map +1 -0
- package/dist/crypto/poseidon/poseidon-constants-opt.d.ts +7 -0
- package/dist/crypto/poseidon/poseidon-constants-opt.d.ts.map +1 -0
- package/dist/crypto/poseidon/poseidon-opt.d.ts +16 -0
- package/dist/crypto/poseidon/poseidon-opt.d.ts.map +1 -0
- package/dist/deposit.d.ts +4 -0
- package/dist/deposit.d.ts.map +1 -0
- package/dist/entities/index.d.ts +4 -0
- package/dist/entities/index.d.ts.map +1 -0
- package/dist/entities/keypair.d.ts +29 -0
- package/dist/entities/keypair.d.ts.map +1 -0
- package/dist/entities/merkle-tree.d.ts +81 -0
- package/dist/entities/merkle-tree.d.ts.map +1 -0
- package/dist/entities/utxo.d.ts +24 -0
- package/dist/entities/utxo.d.ts.map +1 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +38280 -4459
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +38244 -4453
- package/dist/index.mjs.map +1 -1
- package/dist/jest-setup.d.ts +2 -0
- package/dist/jest-setup.d.ts.map +1 -0
- package/dist/keys/index.d.ts +3 -0
- package/dist/keys/index.d.ts.map +1 -0
- package/dist/pkg/nodejs/vortex.d.ts +11 -0
- package/dist/pkg/nodejs/vortex.d.ts.map +1 -0
- package/dist/pkg/web/vortex.d.ts +44 -0
- package/dist/pkg/web/vortex.d.ts.map +1 -0
- package/dist/utils/decrypt.d.ts +12 -0
- package/dist/utils/decrypt.d.ts.map +1 -0
- package/dist/utils/env.d.ts +2 -0
- package/dist/utils/env.d.ts.map +1 -0
- package/dist/utils/events.d.ts +7 -0
- package/dist/utils/events.d.ts.map +1 -0
- package/dist/utils/ext-data.d.ts +3 -0
- package/dist/utils/ext-data.d.ts.map +1 -0
- package/dist/utils/index.d.ts +50 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/prove.d.ts +3 -0
- package/dist/utils/prove.d.ts.map +1 -0
- package/dist/vortex.d.ts +51 -21
- package/dist/vortex.d.ts.map +1 -1
- package/dist/vortex.types.d.ts +74 -50
- package/dist/vortex.types.d.ts.map +1 -1
- package/dist/vortex_bg.wasm +0 -0
- package/dist/withdraw.d.ts +4 -0
- package/dist/withdraw.d.ts.map +1 -0
- package/jest.config.js +31 -0
- package/package.json +20 -5
- package/src/__tests__/entities/keypair.spec.ts +191 -0
- package/src/__tests__/test-utils.ts +76 -0
- package/src/__tests__/types.ts +3 -0
- package/src/__tests__/vortex.test.ts +25 -0
- package/src/constants.ts +104 -0
- package/src/crypto/ff/f1field.ts +464 -0
- package/src/crypto/ff/index.ts +6 -0
- package/src/crypto/ff/random.ts +32 -0
- package/src/crypto/ff/readme.md +8 -0
- package/src/crypto/ff/scalar.ts +264 -0
- package/src/crypto/ff/utils.ts +121 -0
- package/src/crypto/index.ts +8 -0
- package/src/crypto/poseidon/index.ts +1 -0
- package/src/crypto/poseidon/poseidon-constants-opt.ts +24806 -0
- package/src/crypto/poseidon/poseidon-opt.ts +184 -0
- package/src/deposit.ts +168 -0
- package/src/entities/index.ts +3 -0
- package/src/entities/keypair.ts +262 -0
- package/src/entities/merkle-tree.ts +256 -0
- package/src/entities/utxo.ts +52 -0
- package/src/index.ts +6 -2
- package/src/jest-setup.ts +2 -0
- package/src/keys/index.ts +5 -0
- package/src/pkg/nodejs/vortex.d.ts +36 -0
- package/src/pkg/nodejs/vortex.js +332 -0
- package/src/pkg/nodejs/vortex_bg.wasm +0 -0
- package/src/pkg/nodejs/vortex_bg.wasm.d.ts +12 -0
- package/src/pkg/web/vortex.d.ts +72 -0
- package/src/pkg/web/vortex.js +442 -0
- package/src/pkg/web/vortex_bg.wasm +0 -0
- package/src/pkg/web/vortex_bg.wasm.d.ts +12 -0
- package/src/utils/decrypt.ts +46 -0
- package/src/utils/env.ts +18 -0
- package/src/utils/events.ts +16 -0
- package/src/utils/ext-data.ts +43 -0
- package/src/utils/index.ts +152 -0
- package/src/utils/prove.ts +18 -0
- package/src/vortex.ts +235 -111
- package/src/vortex.types.ts +74 -54
- package/src/withdraw.ts +159 -0
- package/tsconfig.json +4 -2
- package/dist/admin.d.ts +0 -17
- package/dist/admin.d.ts.map +0 -1
- package/dist/utils.d.ts +0 -11
- package/dist/utils.d.ts.map +0 -1
- package/src/admin.ts +0 -124
- package/src/utils.ts +0 -66
package/src/vortex.types.ts
CHANGED
|
@@ -1,84 +1,104 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { MaybeTx } from '@interest-protocol/sui-core-sdk';
|
|
2
|
+
import { TransactionResult } from '@mysten/sui/transactions';
|
|
3
|
+
import { Vortex } from './vortex';
|
|
4
|
+
import { VortexKeypair } from './entities/keypair';
|
|
5
|
+
import { MerkleTree } from './entities/merkle-tree';
|
|
6
|
+
import { Utxo } from './entities/utxo';
|
|
3
7
|
|
|
4
|
-
export enum
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
// 1 Sui
|
|
8
|
-
dolphin = 1_000_000_000,
|
|
9
|
-
// 10 Sui
|
|
10
|
-
whale = 10_000_000_000,
|
|
8
|
+
export enum Action {
|
|
9
|
+
Deposit,
|
|
10
|
+
Withdraw,
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
proof = 'vortex_proof',
|
|
16
|
-
merkleTree = 'vortex_merkle_tree',
|
|
13
|
+
export interface RegisterArgs extends MaybeTx {
|
|
14
|
+
encryptionKey: string;
|
|
17
15
|
}
|
|
18
16
|
|
|
19
|
-
export interface
|
|
17
|
+
export interface SharedObjectData {
|
|
20
18
|
objectId: string;
|
|
21
19
|
initialSharedVersion: string;
|
|
22
20
|
}
|
|
23
21
|
|
|
24
|
-
export interface
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface SdkConstructorArgs {
|
|
31
|
-
client: SuiClient;
|
|
22
|
+
export interface ConstructorArgs {
|
|
23
|
+
registry: SharedObjectData;
|
|
24
|
+
vortex: SharedObjectData;
|
|
25
|
+
fullNodeUrl?: string;
|
|
32
26
|
packageId: string;
|
|
33
|
-
pools: VortexPools;
|
|
34
27
|
}
|
|
35
28
|
|
|
36
|
-
export interface
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
export interface ExtDataHashArgs {
|
|
30
|
+
recipient: string; // Sui address
|
|
31
|
+
value: bigint;
|
|
32
|
+
valueSign: boolean;
|
|
33
|
+
relayer: string; // Sui address
|
|
34
|
+
relayerFee: bigint;
|
|
35
|
+
encryptedOutput0: Uint8Array;
|
|
36
|
+
encryptedOutput1: Uint8Array;
|
|
39
37
|
}
|
|
40
38
|
|
|
41
|
-
export interface
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
export interface Proof {
|
|
40
|
+
proofA: number[];
|
|
41
|
+
proofB: number[];
|
|
42
|
+
proofC: number[];
|
|
43
|
+
publicInputs: [string, string, string, string, string, string, string];
|
|
44
|
+
proofSerializedHex: string;
|
|
45
|
+
publicInputsHex: string;
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
export interface
|
|
48
|
-
tx?: Transaction;
|
|
49
|
-
pool: Pool;
|
|
50
|
-
proofPointsHex: string;
|
|
51
|
-
root: bigint;
|
|
52
|
-
nullifier: bigint;
|
|
48
|
+
export interface NewExtDataArgs extends MaybeTx {
|
|
53
49
|
recipient: string;
|
|
50
|
+
value: bigint;
|
|
54
51
|
relayer: string;
|
|
55
52
|
relayerFee: bigint;
|
|
53
|
+
encryptedOutput0: Uint8Array;
|
|
54
|
+
encryptedOutput1: Uint8Array;
|
|
55
|
+
action: Action;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
export interface
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
export interface NewProofArgs extends MaybeTx {
|
|
59
|
+
proofPoints: Uint8Array;
|
|
60
|
+
root: bigint;
|
|
61
|
+
publicValue: bigint;
|
|
62
|
+
action: Action;
|
|
63
|
+
extDataHash: bigint;
|
|
64
|
+
inputNullifier0: bigint;
|
|
65
|
+
inputNullifier1: bigint;
|
|
66
|
+
outputCommitment0: bigint;
|
|
67
|
+
outputCommitment1: bigint;
|
|
61
68
|
}
|
|
62
69
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
70
|
+
interface NestedResult {
|
|
71
|
+
$kind: 'NestedResult';
|
|
72
|
+
NestedResult: [number, number];
|
|
66
73
|
}
|
|
67
74
|
|
|
68
|
-
export interface
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
75
|
+
export interface TransactArgs extends MaybeTx {
|
|
76
|
+
proof: TransactionResult;
|
|
77
|
+
extData: TransactionResult;
|
|
78
|
+
deposit: TransactionResult | NestedResult;
|
|
72
79
|
}
|
|
73
80
|
|
|
74
|
-
export interface
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
81
|
+
export interface DepositArgs extends MaybeTx {
|
|
82
|
+
amount: bigint;
|
|
83
|
+
vortex: Vortex;
|
|
84
|
+
vortexKeypair: VortexKeypair;
|
|
85
|
+
merkleTree: MerkleTree;
|
|
86
|
+
unspentUtxos?: Utxo[];
|
|
78
87
|
}
|
|
79
88
|
|
|
80
|
-
export interface
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
89
|
+
export interface WithdrawArgs extends MaybeTx {
|
|
90
|
+
amount: bigint;
|
|
91
|
+
unspentUtxos: Utxo[];
|
|
92
|
+
vortex: Vortex;
|
|
93
|
+
vortexKeypair: VortexKeypair;
|
|
94
|
+
merkleTree: MerkleTree;
|
|
95
|
+
recipient: string;
|
|
96
|
+
relayer: string;
|
|
97
|
+
relayerFee: bigint;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface ParsedCommitmentEvent {
|
|
101
|
+
commitment: bigint;
|
|
102
|
+
index: bigint;
|
|
103
|
+
encryptedOutput: string;
|
|
84
104
|
}
|
package/src/withdraw.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
2
|
+
import { Utxo } from './entities/utxo';
|
|
3
|
+
import { VortexKeypair } from './entities/keypair';
|
|
4
|
+
import invariant from 'tiny-invariant';
|
|
5
|
+
import { BN } from 'bn.js';
|
|
6
|
+
import { normalizeSuiAddress } from '@mysten/sui/utils';
|
|
7
|
+
|
|
8
|
+
import { fromHex } from '@mysten/sui/utils';
|
|
9
|
+
import {
|
|
10
|
+
bytesToBigInt,
|
|
11
|
+
reverseBytes,
|
|
12
|
+
toProveInput,
|
|
13
|
+
computeExtDataHash,
|
|
14
|
+
} from './utils';
|
|
15
|
+
import { BN254_FIELD_MODULUS } from './constants';
|
|
16
|
+
import { prove, verify } from './utils';
|
|
17
|
+
import { Proof, Action, WithdrawArgs } from './vortex.types';
|
|
18
|
+
|
|
19
|
+
export const withdraw = async ({
|
|
20
|
+
tx = new Transaction(),
|
|
21
|
+
amount,
|
|
22
|
+
unspentUtxos = [],
|
|
23
|
+
vortex,
|
|
24
|
+
vortexKeypair,
|
|
25
|
+
merkleTree,
|
|
26
|
+
recipient,
|
|
27
|
+
relayer,
|
|
28
|
+
relayerFee,
|
|
29
|
+
}: WithdrawArgs) => {
|
|
30
|
+
invariant(1 >= unspentUtxos.length, 'Must have at least 1 unspent UTXO');
|
|
31
|
+
|
|
32
|
+
unspentUtxos.sort((a, b) => new BN(b.amount).cmp(new BN(a.amount)));
|
|
33
|
+
|
|
34
|
+
const totalUnspentUtxosAmount = unspentUtxos
|
|
35
|
+
.slice(0, 2)
|
|
36
|
+
.reduce((acc, utxo) => acc + utxo.amount, 0n);
|
|
37
|
+
|
|
38
|
+
invariant(
|
|
39
|
+
totalUnspentUtxosAmount >= amount + relayerFee,
|
|
40
|
+
'Total unspent UTXOs amount must be greater than or equal to amount'
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const randomVortexKeypair = VortexKeypair.generate();
|
|
44
|
+
|
|
45
|
+
const inputUtxo0 = unspentUtxos[0];
|
|
46
|
+
|
|
47
|
+
const inputUtxo1 =
|
|
48
|
+
unspentUtxos.length > 1 && unspentUtxos[1].amount > 0n
|
|
49
|
+
? unspentUtxos[1]
|
|
50
|
+
: new Utxo({
|
|
51
|
+
amount: 0n,
|
|
52
|
+
keypair: vortexKeypair,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const totalWithdrawAmount = inputUtxo0.amount + inputUtxo1.amount;
|
|
56
|
+
|
|
57
|
+
const changeAmount = totalWithdrawAmount - amount - relayerFee;
|
|
58
|
+
|
|
59
|
+
const nextIndex = await vortex.nextIndex();
|
|
60
|
+
|
|
61
|
+
const outputUtxo0 = new Utxo({
|
|
62
|
+
amount: changeAmount,
|
|
63
|
+
index: nextIndex,
|
|
64
|
+
keypair: vortexKeypair,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const outputUtxo1 = new Utxo({
|
|
68
|
+
amount: 0n,
|
|
69
|
+
index: nextIndex + 1n,
|
|
70
|
+
keypair: vortexKeypair,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const [nullifier0, nullifier1, commitment0, commitment1] = [
|
|
74
|
+
inputUtxo0.nullifier(),
|
|
75
|
+
inputUtxo1.nullifier(),
|
|
76
|
+
outputUtxo0.commitment(),
|
|
77
|
+
outputUtxo1.commitment(),
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
const encryptedUtxo0 = VortexKeypair.encryptUtxoFor(
|
|
81
|
+
outputUtxo0.payload(),
|
|
82
|
+
vortexKeypair.encryptionKey
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
// UTXO1 is a dummy UTXO for obfuscation, so we use a random Vortex keypair.
|
|
86
|
+
const encryptedUtxo1 = VortexKeypair.encryptUtxoFor(
|
|
87
|
+
outputUtxo1.payload(),
|
|
88
|
+
randomVortexKeypair.encryptionKey
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const extDataHash = computeExtDataHash({
|
|
92
|
+
recipient,
|
|
93
|
+
value: amount,
|
|
94
|
+
valueSign: false,
|
|
95
|
+
relayer: normalizeSuiAddress(relayer),
|
|
96
|
+
relayerFee,
|
|
97
|
+
encryptedOutput0: fromHex(encryptedUtxo0),
|
|
98
|
+
encryptedOutput1: fromHex(encryptedUtxo1),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const extDataHashBigInt = bytesToBigInt(reverseBytes(extDataHash));
|
|
102
|
+
|
|
103
|
+
// Prepare circuit input
|
|
104
|
+
const input = toProveInput({
|
|
105
|
+
merkleTree,
|
|
106
|
+
publicAmount: BN254_FIELD_MODULUS - (amount + relayerFee),
|
|
107
|
+
extDataHash: extDataHashBigInt,
|
|
108
|
+
nullifier0,
|
|
109
|
+
nullifier1,
|
|
110
|
+
commitment0,
|
|
111
|
+
commitment1,
|
|
112
|
+
vortexKeypair,
|
|
113
|
+
inputUtxo0,
|
|
114
|
+
inputUtxo1,
|
|
115
|
+
outputUtxo0,
|
|
116
|
+
outputUtxo1,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const proofJson: string = prove(JSON.stringify(input));
|
|
120
|
+
|
|
121
|
+
const proof: Proof = JSON.parse(proofJson);
|
|
122
|
+
|
|
123
|
+
invariant(verify(proofJson), 'Proof verification failed');
|
|
124
|
+
|
|
125
|
+
const { extData, tx: tx2 } = vortex.newExtData({
|
|
126
|
+
tx,
|
|
127
|
+
recipient,
|
|
128
|
+
value: amount,
|
|
129
|
+
action: Action.Withdraw,
|
|
130
|
+
relayer: normalizeSuiAddress(relayer),
|
|
131
|
+
relayerFee,
|
|
132
|
+
encryptedOutput0: fromHex(encryptedUtxo0),
|
|
133
|
+
encryptedOutput1: fromHex(encryptedUtxo1),
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const { proof: moveProof, tx: tx3 } = vortex.newProof({
|
|
137
|
+
tx: tx2,
|
|
138
|
+
proofPoints: fromHex('0x' + proof.proofSerializedHex),
|
|
139
|
+
root: merkleTree.root(),
|
|
140
|
+
publicValue: amount + relayerFee,
|
|
141
|
+
action: Action.Withdraw,
|
|
142
|
+
extDataHash: extDataHashBigInt,
|
|
143
|
+
inputNullifier0: nullifier0,
|
|
144
|
+
inputNullifier1: nullifier1,
|
|
145
|
+
outputCommitment0: commitment0,
|
|
146
|
+
outputCommitment1: commitment1,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const zeroSuiCoin = tx3.splitCoins(tx3.gas, [tx3.pure.u64(0n)]);
|
|
150
|
+
|
|
151
|
+
const { tx: tx4 } = vortex.transact({
|
|
152
|
+
tx: tx3,
|
|
153
|
+
proof: moveProof,
|
|
154
|
+
extData: extData,
|
|
155
|
+
deposit: zeroSuiCoin,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return tx4;
|
|
159
|
+
};
|
package/tsconfig.json
CHANGED
|
@@ -11,8 +11,10 @@
|
|
|
11
11
|
"rootDir": "src",
|
|
12
12
|
"strict": true,
|
|
13
13
|
"skipLibCheck": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"allowJs": true
|
|
15
17
|
},
|
|
16
18
|
"@interest-protocol/*": ["../*/src"],
|
|
17
|
-
"exclude": ["node_modules", "dist"]
|
|
19
|
+
"exclude": ["node_modules", "dist", "jest.config.js", "rollup.config.js"]
|
|
18
20
|
}
|
package/dist/admin.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { AdminSdkConstructorArgs } from './vortex.types';
|
|
2
|
-
import { Transaction } from '@mysten/sui/transactions';
|
|
3
|
-
import { NewPoolArgs, SharePoolArgs, SetDepositFeeArgs, SetWithdrawFeeArgs, SetGroth16VkArgs } from './vortex.types';
|
|
4
|
-
export declare class VortexAdminSdk {
|
|
5
|
-
private packageId;
|
|
6
|
-
private adminCap;
|
|
7
|
-
constructor(args: AdminSdkConstructorArgs);
|
|
8
|
-
newPool({ pool, tx }: NewPoolArgs): {
|
|
9
|
-
vortex: import("@mysten/sui/dist/cjs/transactions").TransactionResult;
|
|
10
|
-
tx: Transaction;
|
|
11
|
-
};
|
|
12
|
-
sharePool({ tx, pool }: SharePoolArgs): Transaction;
|
|
13
|
-
setDepositFee({ tx, pool, fee }: SetDepositFeeArgs): Transaction;
|
|
14
|
-
setWithdrawFee({ tx, pool, fee }: SetWithdrawFeeArgs): Transaction;
|
|
15
|
-
setGroth16Vk({ tx, pool, vk }: SetGroth16VkArgs): Transaction;
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=admin.d.ts.map
|
package/dist/admin.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"admin.d.ts","sourceRoot":"","sources":["../src/admin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAEvD,OAAO,EACL,WAAW,EAEX,aAAa,EACb,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EACjB,MAAM,gBAAgB,CAAC;AAExB,qBAAa,cAAc;IACzB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,QAAQ,CAAS;gBAEb,IAAI,EAAE,uBAAuB;IAKzC,OAAO,CAAC,EAAE,IAAI,EAAE,EAAsB,EAAE,EAAE,WAAW;;;;IAcrD,SAAS,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,aAAa;IAWrC,aAAa,CAAC,EAAE,EAAsB,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,iBAAiB;IAwBtE,cAAc,CAAC,EAAE,EAAsB,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,kBAAkB;IAwBxE,YAAY,CAAC,EAAE,EAAsB,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,gBAAgB;CA6BpE"}
|
package/dist/utils.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export declare const BN254_FIELD_MODULUS = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
2
|
-
export declare const generateRandomNote: () => {
|
|
3
|
-
nullifier: bigint;
|
|
4
|
-
secret: bigint;
|
|
5
|
-
commitment: bigint;
|
|
6
|
-
};
|
|
7
|
-
export declare function bigIntToFieldElement(value: bigint): bigint;
|
|
8
|
-
export declare function addressToFieldElement(address: string): bigint;
|
|
9
|
-
export declare const stringToField: (s: string) => bigint;
|
|
10
|
-
export declare function zeros(treeLevels: number): bigint[];
|
|
11
|
-
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,mBAAmB,iFACgD,CAAC;AAYjF,eAAO,MAAM,kBAAkB;;;;CAS9B,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,UAEjD;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,UAMpD;AAED,eAAO,MAAM,aAAa,GAAI,GAAG,MAAM,WAKtC,CAAC;AAEF,wBAAgB,KAAK,CAAC,UAAU,EAAE,MAAM,YAiBvC"}
|
package/src/admin.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { AdminSdkConstructorArgs } from './vortex.types';
|
|
2
|
-
import { Transaction } from '@mysten/sui/transactions';
|
|
3
|
-
import { fromHex } from '@mysten/sui/utils';
|
|
4
|
-
import {
|
|
5
|
-
NewPoolArgs,
|
|
6
|
-
Modules,
|
|
7
|
-
SharePoolArgs,
|
|
8
|
-
SetDepositFeeArgs,
|
|
9
|
-
SetWithdrawFeeArgs,
|
|
10
|
-
SetGroth16VkArgs,
|
|
11
|
-
} from './vortex.types';
|
|
12
|
-
|
|
13
|
-
export class VortexAdminSdk {
|
|
14
|
-
private packageId: string;
|
|
15
|
-
private adminCap: string;
|
|
16
|
-
|
|
17
|
-
constructor(args: AdminSdkConstructorArgs) {
|
|
18
|
-
this.packageId = args.packageId;
|
|
19
|
-
this.adminCap = args.adminCap;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
newPool({ pool, tx = new Transaction() }: NewPoolArgs) {
|
|
23
|
-
const vortex = tx.moveCall({
|
|
24
|
-
package: this.packageId,
|
|
25
|
-
module: Modules.vortex,
|
|
26
|
-
function: 'new',
|
|
27
|
-
arguments: [tx.object(this.adminCap), tx.pure.u64(pool)],
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
vortex,
|
|
32
|
-
tx,
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
sharePool({ tx, pool }: SharePoolArgs) {
|
|
37
|
-
tx.moveCall({
|
|
38
|
-
package: this.packageId,
|
|
39
|
-
module: Modules.vortex,
|
|
40
|
-
function: 'share',
|
|
41
|
-
arguments: [pool],
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
return tx;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
setDepositFee({ tx = new Transaction(), pool, fee }: SetDepositFeeArgs) {
|
|
48
|
-
if (typeof pool === 'object') {
|
|
49
|
-
tx.moveCall({
|
|
50
|
-
package: this.packageId,
|
|
51
|
-
module: Modules.vortex,
|
|
52
|
-
function: 'set_deposit_fee',
|
|
53
|
-
arguments: [pool, tx.object(this.adminCap), tx.pure.u64(fee)],
|
|
54
|
-
});
|
|
55
|
-
} else {
|
|
56
|
-
tx.moveCall({
|
|
57
|
-
package: this.packageId,
|
|
58
|
-
module: Modules.vortex,
|
|
59
|
-
function: 'set_deposit_fee',
|
|
60
|
-
arguments: [
|
|
61
|
-
tx.object(pool),
|
|
62
|
-
tx.object(this.adminCap),
|
|
63
|
-
tx.pure.u64(fee),
|
|
64
|
-
],
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return tx;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
setWithdrawFee({ tx = new Transaction(), pool, fee }: SetWithdrawFeeArgs) {
|
|
72
|
-
if (typeof pool === 'object') {
|
|
73
|
-
tx.moveCall({
|
|
74
|
-
package: this.packageId,
|
|
75
|
-
module: Modules.vortex,
|
|
76
|
-
function: 'set_withdraw_fee',
|
|
77
|
-
arguments: [pool, tx.object(this.adminCap), tx.pure.u64(fee)],
|
|
78
|
-
});
|
|
79
|
-
} else {
|
|
80
|
-
tx.moveCall({
|
|
81
|
-
package: this.packageId,
|
|
82
|
-
module: Modules.vortex,
|
|
83
|
-
function: 'set_withdraw_fee',
|
|
84
|
-
arguments: [
|
|
85
|
-
tx.object(pool),
|
|
86
|
-
tx.object(this.adminCap),
|
|
87
|
-
tx.pure.u64(fee),
|
|
88
|
-
],
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return tx;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
setGroth16Vk({ tx = new Transaction(), pool, vk }: SetGroth16VkArgs) {
|
|
96
|
-
const vkBytes = fromHex(vk);
|
|
97
|
-
|
|
98
|
-
if (typeof pool === 'object') {
|
|
99
|
-
tx.moveCall({
|
|
100
|
-
package: this.packageId,
|
|
101
|
-
module: Modules.vortex,
|
|
102
|
-
function: 'set_groth16_vk',
|
|
103
|
-
arguments: [
|
|
104
|
-
pool,
|
|
105
|
-
tx.object(this.adminCap),
|
|
106
|
-
tx.pure.vector('u8', vkBytes),
|
|
107
|
-
],
|
|
108
|
-
});
|
|
109
|
-
} else {
|
|
110
|
-
tx.moveCall({
|
|
111
|
-
package: this.packageId,
|
|
112
|
-
module: Modules.vortex,
|
|
113
|
-
function: 'set_groth16_vk',
|
|
114
|
-
arguments: [
|
|
115
|
-
tx.object(pool),
|
|
116
|
-
tx.object(this.adminCap),
|
|
117
|
-
tx.pure.vector('u8', vkBytes),
|
|
118
|
-
],
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return tx;
|
|
123
|
-
}
|
|
124
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { poseidon2, poseidon1 } from 'poseidon-lite';
|
|
2
|
-
import invariant from 'tiny-invariant';
|
|
3
|
-
|
|
4
|
-
import { isValidSuiAddress, normalizeSuiAddress } from '@mysten/sui/utils';
|
|
5
|
-
|
|
6
|
-
export const BN254_FIELD_MODULUS =
|
|
7
|
-
21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
8
|
-
|
|
9
|
-
function randomBigIntHex(byteLength: number): bigint {
|
|
10
|
-
const hexString = Array.from(
|
|
11
|
-
crypto.getRandomValues(new Uint8Array(byteLength))
|
|
12
|
-
)
|
|
13
|
-
.map((b) => b.toString(16).padStart(2, '0'))
|
|
14
|
-
.join('');
|
|
15
|
-
|
|
16
|
-
return BigInt('0x' + hexString);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const generateRandomNote = () => {
|
|
20
|
-
const nullifier = randomBigIntHex(31);
|
|
21
|
-
const secret = randomBigIntHex(31);
|
|
22
|
-
const commitment = poseidon2([nullifier, secret]);
|
|
23
|
-
return {
|
|
24
|
-
nullifier,
|
|
25
|
-
secret,
|
|
26
|
-
commitment,
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export function bigIntToFieldElement(value: bigint) {
|
|
31
|
-
return value % BN254_FIELD_MODULUS;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function addressToFieldElement(address: string) {
|
|
35
|
-
invariant(isValidSuiAddress(address), 'Invalid Sui address');
|
|
36
|
-
|
|
37
|
-
const cleanAddress = normalizeSuiAddress(address, true);
|
|
38
|
-
|
|
39
|
-
return bigIntToFieldElement(BigInt(cleanAddress));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export const stringToField = (s: string) => {
|
|
43
|
-
const bytes = new TextEncoder().encode(s);
|
|
44
|
-
let acc = 0n;
|
|
45
|
-
for (const b of bytes) acc = (acc << 8n) | BigInt(b);
|
|
46
|
-
return acc % BN254_FIELD_MODULUS;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export function zeros(treeLevels: number) {
|
|
50
|
-
if (treeLevels < 1) throw new Error('treeLevels must be >= 1');
|
|
51
|
-
|
|
52
|
-
// ZERO_VALUE := Poseidon("vortex")
|
|
53
|
-
const ZERO_VALUE = poseidon1([stringToField('vortex')]);
|
|
54
|
-
|
|
55
|
-
const zeros: bigint[] = [];
|
|
56
|
-
|
|
57
|
-
let currentZero = ZERO_VALUE;
|
|
58
|
-
zeros.push(currentZero);
|
|
59
|
-
|
|
60
|
-
for (let i = 1; i < treeLevels; i++) {
|
|
61
|
-
currentZero = poseidon2([currentZero, currentZero]);
|
|
62
|
-
zeros.push(currentZero);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return zeros;
|
|
66
|
-
}
|