@interest-protocol/vortex-sdk 11.3.1 → 12.0.4

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.
@@ -1,12 +1,12 @@
1
- import { PaginatedEvents } from '@mysten/sui/client';
1
+ import type { PaginatedEvents } from '@mysten/sui/client';
2
+ import type { Commitment } from '../vortex-api.types';
3
+ import type { UtxoPayload, VortexKeypair } from '../entities/keypair';
4
+ import type { Vortex } from '../vortex';
5
+ import type { VortexPool } from '../vortex.types';
6
+
2
7
  import { parseNewCommitmentEvent } from './events';
3
- import { Commitment } from '../vortex-api.types';
4
- import { UtxoPayload } from '../entities/keypair';
5
- import { VortexKeypair } from '../entities/keypair';
6
8
  import { Utxo } from '../entities/utxo';
7
9
  import { normalizeStructTag, toHex } from '@mysten/sui/utils';
8
- import { Vortex } from '../vortex';
9
- import { VortexPool } from '../vortex.types';
10
10
  import invariant from 'tiny-invariant';
11
11
 
12
12
  interface GetUnspentUtxosArgs {
@@ -24,23 +24,30 @@ export const getUnspentUtxos = async ({
24
24
  }: GetUnspentUtxosArgs) => {
25
25
  const commitments = parseNewCommitmentEvent(commitmentEvents);
26
26
 
27
- const allUtxos = [] as UtxoPayload[];
27
+ const vortexObjectId =
28
+ typeof vortexPool === 'string' ? vortexPool : vortexPool.objectId;
29
+
30
+ const decryptedWithIndex: { utxo: UtxoPayload; chainIndex: bigint }[] = [];
28
31
 
29
32
  commitments.forEach((commitment) => {
30
33
  try {
31
34
  const utxo = vortexKeypair.decryptUtxo(commitment.encryptedOutput);
32
- allUtxos.push(utxo);
35
+ // Use index from chain (commitment.index) instead of decrypted index
36
+ // to avoid concurrency/latency issues where encrypted index can be stale
37
+ decryptedWithIndex.push({ utxo, chainIndex: commitment.index });
33
38
  } catch {
34
- // Do nothing
39
+ // HMAC verification failed - wrong keypair
35
40
  }
36
41
  });
37
42
 
38
- const vortexObjectId =
39
- typeof vortexPool === 'string' ? vortexPool : vortexPool.objectId;
40
-
41
- const utxos = allUtxos.map(
42
- (utxo) =>
43
- new Utxo({ ...utxo, keypair: vortexKeypair, vortexPool: vortexObjectId })
43
+ const utxos = decryptedWithIndex.map(
44
+ ({ utxo, chainIndex }) =>
45
+ new Utxo({
46
+ ...utxo,
47
+ index: chainIndex, // Override with on-chain index
48
+ keypair: vortexKeypair,
49
+ vortexPool: vortexObjectId,
50
+ })
44
51
  );
45
52
 
46
53
  const nullifiers = utxos.map((utxo) => utxo.nullifier());
@@ -65,7 +72,7 @@ interface GetUnspentUtxosWithApiArgs {
65
72
  }
66
73
 
67
74
  interface GetUnspentUtxosWithApiAndCommitmentsArgs {
68
- commitments: Pick<Commitment, 'coinType' | 'encryptedOutput'>[];
75
+ commitments: Pick<Commitment, 'coinType' | 'encryptedOutput' | 'index'>[];
69
76
  vortexKeypair: VortexKeypair;
70
77
  vortexSdk: Vortex;
71
78
  vortexPool: string | VortexPool;
@@ -77,7 +84,7 @@ export const getUnspentUtxosWithApi = async ({
77
84
  vortexSdk,
78
85
  vortexPool,
79
86
  }: GetUnspentUtxosWithApiArgs) => {
80
- const allUtxos = [] as UtxoPayload[];
87
+ const decryptedWithIndex: { utxo: UtxoPayload; chainIndex: bigint }[] = [];
81
88
 
82
89
  const vortexObject = await vortexSdk.resolveVortexPool(vortexPool);
83
90
 
@@ -92,16 +99,19 @@ export const getUnspentUtxosWithApi = async ({
92
99
  Uint8Array.from(commitment.encryptedOutput)
93
100
  );
94
101
  const utxo = vortexKeypair.decryptUtxo(encryptedOutputHex);
95
- allUtxos.push(utxo);
102
+ // Use index from chain (commitment.index) instead of decrypted index
103
+ // to avoid concurrency/latency issues where encrypted index can be stale
104
+ decryptedWithIndex.push({ utxo, chainIndex: BigInt(commitment.index) });
96
105
  } catch {
97
- // Do nothing
106
+ // HMAC verification failed - wrong keypair
98
107
  }
99
108
  });
100
109
 
101
- const utxos = allUtxos.map(
102
- (utxo) =>
110
+ const utxos = decryptedWithIndex.map(
111
+ ({ utxo, chainIndex }) =>
103
112
  new Utxo({
104
113
  ...utxo,
114
+ index: chainIndex, // Override with on-chain index
105
115
  keypair: vortexKeypair,
106
116
  vortexPool: vortexObject.objectId,
107
117
  })
@@ -127,11 +137,8 @@ export const getUnspentUtxosWithApiAndCommitments = async ({
127
137
  vortexSdk,
128
138
  vortexPool,
129
139
  }: GetUnspentUtxosWithApiAndCommitmentsArgs) => {
130
- const allUtxos = [] as UtxoPayload[];
131
- const userCommitments = [] as Pick<
132
- Commitment,
133
- 'coinType' | 'encryptedOutput'
134
- >[];
140
+ const decryptedWithIndex: { utxo: UtxoPayload; chainIndex: bigint }[] = [];
141
+ const userCommitments = [] as Pick<Commitment, 'coinType' | 'encryptedOutput' | 'index'>[];
135
142
 
136
143
  const vortexObject = await vortexSdk.resolveVortexPool(vortexPool);
137
144
 
@@ -149,17 +156,21 @@ export const getUnspentUtxosWithApiAndCommitments = async ({
149
156
  userCommitments.push({
150
157
  coinType: commitment.coinType,
151
158
  encryptedOutput: commitment.encryptedOutput,
159
+ index: commitment.index,
152
160
  });
153
- allUtxos.push(utxo);
161
+ // Use index from chain (commitment.index) instead of decrypted index
162
+ // to avoid concurrency/latency issues where encrypted index can be stale
163
+ decryptedWithIndex.push({ utxo, chainIndex: BigInt(commitment.index) });
154
164
  } catch {
155
- // Do nothing
165
+ // HMAC verification failed - wrong keypair
156
166
  }
157
167
  });
158
168
 
159
- const utxos = allUtxos.map(
160
- (utxo) =>
169
+ const utxos = decryptedWithIndex.map(
170
+ ({ utxo, chainIndex }) =>
161
171
  new Utxo({
162
172
  ...utxo,
173
+ index: chainIndex, // Override with on-chain index
163
174
  keypair: vortexKeypair,
164
175
  vortexPool: vortexObject.objectId,
165
176
  })
@@ -1,2 +0,0 @@
1
- export declare function getRandomBytes(length: number): Uint8Array;
2
- //# sourceMappingURL=random.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../../../src/crypto/ff/random.ts"],"names":[],"mappings":"AAUA,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAqBzD"}
@@ -1,32 +0,0 @@
1
- /**
2
- * Type definition for the browser crypto object.
3
- */
4
- type BrowserCrypto = {
5
- crypto?: { getRandomValues?: (arg0: Uint8Array) => void };
6
- };
7
-
8
- // Type declaration for Node.js require (used in Node.js environment fallback)
9
- declare const require: (module: string) => any;
10
-
11
- export function getRandomBytes(length: number): Uint8Array {
12
- if (length <= 0) {
13
- throw new Error('Length must be greater than 0');
14
- }
15
- const global = globalThis as BrowserCrypto;
16
- if (global.crypto?.getRandomValues) {
17
- const randomValues = new Uint8Array(length);
18
- global.crypto.getRandomValues(randomValues);
19
- return randomValues;
20
- }
21
- // eslint-disable-next-line no-unused-labels
22
- NODE: {
23
- // eslint-disable-next-line @typescript-eslint/no-require-imports
24
- // eslint-disable-next-line @typescript-eslint/no-require-imports
25
- const crypto = require('crypto');
26
- return crypto.randomBytes(length);
27
- }
28
-
29
- throw new Error(
30
- 'Random byte generation is not supported in this environment'
31
- );
32
- }