@provex/utils 1.0.16 → 1.2.3
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/src/chain.d.ts +436 -4
- package/dist/src/chain.js +80 -0
- package/dist/src/chain.test.d.ts +1 -0
- package/dist/src/chain.test.js +112 -0
- package/dist/src/contracts.d.ts +196 -20
- package/dist/src/contracts.js +245 -17
- package/dist/src/contracts.test.d.ts +1 -0
- package/dist/src/contracts.test.js +243 -0
- package/dist/src/conversionRates.d.ts +99 -8
- package/dist/src/conversionRates.js +74 -4
- package/dist/src/conversionRates.test.d.ts +1 -0
- package/dist/src/conversionRates.test.js +473 -0
- package/dist/src/currencies.d.ts +113 -9
- package/dist/src/currencies.js +119 -38
- package/dist/src/currencies.test.d.ts +1 -0
- package/dist/src/currencies.test.js +343 -0
- package/dist/src/data/countries.json +166 -166
- package/dist/src/deposit.d.ts +69 -1
- package/dist/src/deposit.js +52 -1
- package/dist/src/depositKeys.d.ts +154 -0
- package/dist/src/depositKeys.js +104 -0
- package/dist/src/depositKeys.test.d.ts +1 -0
- package/dist/src/depositKeys.test.js +231 -0
- package/dist/src/endpoints.d.ts +51 -0
- package/dist/src/endpoints.js +70 -0
- package/dist/src/endpoints.test.d.ts +1 -0
- package/dist/src/endpoints.test.js +54 -0
- package/dist/src/events.d.ts +40 -2
- package/dist/src/events.js +86 -10
- package/dist/src/events.test.d.ts +1 -0
- package/dist/src/events.test.js +111 -0
- package/dist/src/fees.d.ts +136 -0
- package/dist/src/fees.js +178 -0
- package/dist/src/fees.test.d.ts +1 -0
- package/dist/src/fees.test.js +165 -0
- package/dist/src/ids.d.ts +125 -22
- package/dist/src/ids.js +211 -61
- package/dist/src/ids.test.d.ts +1 -0
- package/dist/src/ids.test.js +607 -0
- package/dist/src/index.d.ts +30 -1
- package/dist/src/index.js +30 -1
- package/dist/src/input.d.ts +23 -4
- package/dist/src/input.js +23 -4
- package/dist/src/intent.d.ts +38 -2
- package/dist/src/intent.js +28 -1
- package/dist/src/intent.test.d.ts +1 -0
- package/dist/src/intent.test.js +119 -0
- package/dist/src/json.d.ts +38 -1
- package/dist/src/json.js +38 -2
- package/dist/src/normalization.d.ts +224 -0
- package/dist/src/normalization.js +119 -0
- package/dist/src/normalization.test.d.ts +1 -0
- package/dist/src/normalization.test.js +284 -0
- package/dist/src/participants.d.ts +19 -4
- package/dist/src/participants.js +19 -4
- package/dist/src/payment.d.ts +333 -16
- package/dist/src/payment.js +768 -58
- package/dist/src/payment.test.d.ts +1 -0
- package/dist/src/payment.test.js +272 -0
- package/dist/src/peer-types.d.ts +327 -0
- package/dist/src/peer-types.js +169 -0
- package/dist/src/profit.d.ts +26 -5
- package/dist/src/profit.js +31 -8
- package/dist/src/profit.test.d.ts +1 -0
- package/dist/src/profit.test.js +315 -0
- package/dist/src/reputation.d.ts +125 -0
- package/dist/src/reputation.js +188 -0
- package/dist/src/reputation.test.d.ts +1 -0
- package/dist/src/reputation.test.js +156 -0
- package/dist/src/sort.d.ts +49 -10
- package/dist/src/sort.js +40 -9
- package/dist/src/sort.test.d.ts +1 -0
- package/dist/src/sort.test.js +216 -0
- package/dist/src/stateKey.d.ts +35 -0
- package/dist/src/stateKey.js +36 -0
- package/dist/src/stats.d.ts +55 -1
- package/dist/src/stats.js +46 -1
- package/dist/src/stats.test.d.ts +1 -0
- package/dist/src/stats.test.js +104 -0
- package/dist/src/time.d.ts +59 -9
- package/dist/src/time.js +71 -15
- package/dist/src/time.test.d.ts +1 -0
- package/dist/src/time.test.js +231 -0
- package/dist/src/tokens.d.ts +11972 -145
- package/dist/src/tokens.js +98 -17
- package/dist/src/tokens.test.d.ts +1 -0
- package/dist/src/tokens.test.js +193 -0
- package/dist/src/units.d.ts +183 -0
- package/dist/src/units.js +206 -6
- package/dist/src/units.test.d.ts +1 -0
- package/dist/src/units.test.js +327 -0
- package/dist/src/zkp2p-types.d.ts +273 -4
- package/dist/src/zkp2p-types.js +169 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +116 -26
- package/scripts/convert-svg.ts +0 -22
- package/src/chain.ts +0 -27
- package/src/contracts.ts +0 -89
- package/src/conversionRates.ts +0 -356
- package/src/currencies.ts +0 -286
- package/src/data/countries.json +0 -1099
- package/src/deposit.ts +0 -2
- package/src/events.ts +0 -21
- package/src/flags/eur.svg +0 -4
- package/src/flags/usd.svg +0 -25
- package/src/flags.ts +0 -4
- package/src/global.d.ts +0 -9
- package/src/ids.ts +0 -137
- package/src/index.ts +0 -16
- package/src/input.ts +0 -35
- package/src/intent.ts +0 -15
- package/src/json.ts +0 -42
- package/src/participants.ts +0 -14
- package/src/payment.ts +0 -573
- package/src/profit.ts +0 -55
- package/src/sort.ts +0 -49
- package/src/stats.ts +0 -10
- package/src/time.ts +0 -123
- package/src/tokens.ts +0 -159
- package/src/units.ts +0 -120
- package/src/zkp2p-types.ts +0 -55
- package/tsconfig.json +0 -18
package/dist/src/input.d.ts
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* @fileoverview Input manipulation utilities for numeric string handling.
|
|
3
|
+
*
|
|
4
|
+
* Provides utilities for incrementing/decrementing decimal values while
|
|
5
|
+
* preserving the user's intended precision (trailing zeros).
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Adjust the lowest decimal place of a numeric string by +1 or -1.
|
|
9
|
+
* Preserves the original decimal precision (trailing zeros are maintained).
|
|
10
|
+
*
|
|
11
|
+
* Used for numeric input controls with up/down buttons that increment
|
|
12
|
+
* at the current decimal precision level.
|
|
13
|
+
*
|
|
14
|
+
* @param inputVal - The input value as a string (e.g., "1.50", "100", "0.001")
|
|
15
|
+
* @param direction - Direction to adjust: 1 (increment) or -1 (decrement)
|
|
16
|
+
* @returns The adjusted value with original precision preserved
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* adjustLowestDecimal("1.50", 1) // "1.51"
|
|
21
|
+
* adjustLowestDecimal("1.50", -1) // "1.49"
|
|
22
|
+
* adjustLowestDecimal("100", 1) // "101"
|
|
23
|
+
* adjustLowestDecimal("0.001", 1) // "0.002"
|
|
24
|
+
* ```
|
|
6
25
|
*/
|
|
7
26
|
export declare const adjustLowestDecimal: (inputVal: string, direction: 1 | -1) => string;
|
package/dist/src/input.js
CHANGED
|
@@ -1,8 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* @fileoverview Input manipulation utilities for numeric string handling.
|
|
3
|
+
*
|
|
4
|
+
* Provides utilities for incrementing/decrementing decimal values while
|
|
5
|
+
* preserving the user's intended precision (trailing zeros).
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Adjust the lowest decimal place of a numeric string by +1 or -1.
|
|
9
|
+
* Preserves the original decimal precision (trailing zeros are maintained).
|
|
10
|
+
*
|
|
11
|
+
* Used for numeric input controls with up/down buttons that increment
|
|
12
|
+
* at the current decimal precision level.
|
|
13
|
+
*
|
|
14
|
+
* @param inputVal - The input value as a string (e.g., "1.50", "100", "0.001")
|
|
15
|
+
* @param direction - Direction to adjust: 1 (increment) or -1 (decrement)
|
|
16
|
+
* @returns The adjusted value with original precision preserved
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* adjustLowestDecimal("1.50", 1) // "1.51"
|
|
21
|
+
* adjustLowestDecimal("1.50", -1) // "1.49"
|
|
22
|
+
* adjustLowestDecimal("100", 1) // "101"
|
|
23
|
+
* adjustLowestDecimal("0.001", 1) // "0.002"
|
|
24
|
+
* ```
|
|
6
25
|
*/
|
|
7
26
|
export const adjustLowestDecimal = (inputVal, direction) => {
|
|
8
27
|
let [whole, decimal = ''] = inputVal.split('.');
|
package/dist/src/intent.d.ts
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Intent data structures and validation utilities.
|
|
3
|
+
*
|
|
4
|
+
* An intent represents a buyer's commitment to purchase tokens from a deposit
|
|
5
|
+
* at a specific price. The intent lifecycle:
|
|
6
|
+
*
|
|
7
|
+
* 1. **Signal**: Buyer calls signalIntent() on Orchestrator, funds are soft-locked
|
|
8
|
+
* 2. **Lock**: For V3, explicit lock period with expiry time
|
|
9
|
+
* 3. **Verify**: Buyer proves off-chain payment via zkTLS attestation
|
|
10
|
+
* 4. **Fulfill**: Verifier releases escrowed funds to buyer
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* V2 and V3 intents have different field structures:
|
|
14
|
+
* - V2: Uses paymentVerifier (address) to identify payment method
|
|
15
|
+
* - V3: Uses paymentMethod (bytes32 hash) and includes escrow address
|
|
16
|
+
*
|
|
17
|
+
* @see {@link peer-types} for attestation types
|
|
18
|
+
* @see {@link payment} for payment method definitions
|
|
19
|
+
*/
|
|
1
20
|
import type { Hex } from "viem";
|
|
2
21
|
/**
|
|
3
|
-
* Structure
|
|
22
|
+
* Structure representing an on-chain intent.
|
|
23
|
+
*
|
|
24
|
+
* V2 intents use `paymentVerifier` (address, 20 bytes).
|
|
25
|
+
* V3 intents use `paymentMethod` (bytes32, 32 bytes) and include `escrow`.
|
|
26
|
+
*
|
|
27
|
+
* When working with this interface:
|
|
28
|
+
* - Check `paymentMethod` first (V3), fall back to `paymentVerifier` (V2)
|
|
29
|
+
* - Use `escrow` only when available (V3)
|
|
4
30
|
*/
|
|
5
31
|
export interface IntentStruct {
|
|
6
32
|
owner: Hex;
|
|
@@ -8,7 +34,17 @@ export interface IntentStruct {
|
|
|
8
34
|
depositId: bigint;
|
|
9
35
|
amount: bigint;
|
|
10
36
|
timestamp: bigint;
|
|
11
|
-
paymentVerifier: Hex;
|
|
12
37
|
fiatCurrency: Hex;
|
|
13
38
|
conversionRate: bigint;
|
|
39
|
+
/** V2 only: Payment verifier contract address (20 bytes) */
|
|
40
|
+
paymentVerifier?: Hex;
|
|
41
|
+
/** V3 only: Payment method ID hash (32 bytes) */
|
|
42
|
+
paymentMethod?: Hex;
|
|
43
|
+
/** V3 only: Escrow contract address */
|
|
44
|
+
escrow?: Hex;
|
|
14
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Check if an intent is empty (not found on-chain).
|
|
48
|
+
* V3 Orchestrator returns zero-filled struct when intent doesn't exist.
|
|
49
|
+
*/
|
|
50
|
+
export declare function isEmptyIntent(intent: IntentStruct | null): boolean;
|
package/dist/src/intent.js
CHANGED
|
@@ -1 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Intent data structures and validation utilities.
|
|
3
|
+
*
|
|
4
|
+
* An intent represents a buyer's commitment to purchase tokens from a deposit
|
|
5
|
+
* at a specific price. The intent lifecycle:
|
|
6
|
+
*
|
|
7
|
+
* 1. **Signal**: Buyer calls signalIntent() on Orchestrator, funds are soft-locked
|
|
8
|
+
* 2. **Lock**: For V3, explicit lock period with expiry time
|
|
9
|
+
* 3. **Verify**: Buyer proves off-chain payment via zkTLS attestation
|
|
10
|
+
* 4. **Fulfill**: Verifier releases escrowed funds to buyer
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* V2 and V3 intents have different field structures:
|
|
14
|
+
* - V2: Uses paymentVerifier (address) to identify payment method
|
|
15
|
+
* - V3: Uses paymentMethod (bytes32 hash) and includes escrow address
|
|
16
|
+
*
|
|
17
|
+
* @see {@link peer-types} for attestation types
|
|
18
|
+
* @see {@link payment} for payment method definitions
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Check if an intent is empty (not found on-chain).
|
|
22
|
+
* V3 Orchestrator returns zero-filled struct when intent doesn't exist.
|
|
23
|
+
*/
|
|
24
|
+
export function isEmptyIntent(intent) {
|
|
25
|
+
if (!intent)
|
|
26
|
+
return true;
|
|
27
|
+
return intent.amount === 0n && intent.timestamp === 0n && intent.depositId === 0n;
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { isEmptyIntent } from './intent.js';
|
|
4
|
+
import { zeroAddress } from 'viem';
|
|
5
|
+
/**
|
|
6
|
+
* Tests for IntentStruct and isEmptyIntent utility
|
|
7
|
+
*
|
|
8
|
+
* V2 intents use paymentVerifier (address, 20 bytes)
|
|
9
|
+
* V3 intents use paymentMethod (bytes32, 32 bytes) and include escrow
|
|
10
|
+
*/
|
|
11
|
+
// Sample V3 intent (all fields populated)
|
|
12
|
+
const mockV3Intent = {
|
|
13
|
+
owner: '0x1234567890123456789012345678901234567890',
|
|
14
|
+
to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd',
|
|
15
|
+
escrow: '0x2f121CDDCA6d652f35e8B3E560f9760898888888',
|
|
16
|
+
depositId: 42n,
|
|
17
|
+
amount: 100000000n, // 100 USDC
|
|
18
|
+
timestamp: 1737123456n,
|
|
19
|
+
paymentMethod: '0x1234567890123456789012345678901234567890123456789012345678901234',
|
|
20
|
+
fiatCurrency: '0x0000000000000000000000000000000000000000000000000000000000555344', // USD
|
|
21
|
+
conversionRate: 1000000000000000000n, // 1.0
|
|
22
|
+
};
|
|
23
|
+
// Sample V2 intent (no escrow, uses paymentVerifier)
|
|
24
|
+
const mockV2Intent = {
|
|
25
|
+
owner: '0x1234567890123456789012345678901234567890',
|
|
26
|
+
to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd',
|
|
27
|
+
depositId: 42n,
|
|
28
|
+
amount: 100000000n,
|
|
29
|
+
timestamp: 1737123456n,
|
|
30
|
+
paymentVerifier: '0x9a733B55a875D0DB4915c6B36350b24F8AB99dF5', // venmo verifier
|
|
31
|
+
fiatCurrency: '0x0000000000000000000000000000000000000000000000000000000000555344',
|
|
32
|
+
conversionRate: 1000000000000000000n,
|
|
33
|
+
};
|
|
34
|
+
// Empty intent (what V3 Orchestrator returns when intent not found)
|
|
35
|
+
const emptyIntent = {
|
|
36
|
+
owner: zeroAddress,
|
|
37
|
+
to: zeroAddress,
|
|
38
|
+
escrow: zeroAddress,
|
|
39
|
+
depositId: 0n,
|
|
40
|
+
amount: 0n,
|
|
41
|
+
timestamp: 0n,
|
|
42
|
+
paymentMethod: '0x0000000000000000000000000000000000000000000000000000000000000000',
|
|
43
|
+
fiatCurrency: '0x0000000000000000000000000000000000000000000000000000000000000000',
|
|
44
|
+
conversionRate: 0n,
|
|
45
|
+
};
|
|
46
|
+
describe('IntentStruct type', () => {
|
|
47
|
+
describe('V3 intent structure', () => {
|
|
48
|
+
it('has escrow field for V3', () => {
|
|
49
|
+
assert.ok(mockV3Intent.escrow !== undefined, 'escrow should be defined');
|
|
50
|
+
assert.equal(mockV3Intent.escrow, '0x2f121CDDCA6d652f35e8B3E560f9760898888888');
|
|
51
|
+
});
|
|
52
|
+
it('has paymentMethod field (bytes32) for V3', () => {
|
|
53
|
+
assert.ok(mockV3Intent.paymentMethod !== undefined, 'paymentMethod should be defined');
|
|
54
|
+
assert.equal(mockV3Intent.paymentMethod?.length, 66, '0x + 64 hex chars');
|
|
55
|
+
});
|
|
56
|
+
it('does not require paymentVerifier for V3', () => {
|
|
57
|
+
assert.equal(mockV3Intent.paymentVerifier, undefined);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
describe('V2 intent structure', () => {
|
|
61
|
+
it('has paymentVerifier field (address) for V2', () => {
|
|
62
|
+
assert.ok(mockV2Intent.paymentVerifier !== undefined, 'paymentVerifier should be defined');
|
|
63
|
+
assert.equal(mockV2Intent.paymentVerifier?.length, 42, '0x + 40 hex chars');
|
|
64
|
+
});
|
|
65
|
+
it('does not require escrow for V2', () => {
|
|
66
|
+
assert.equal(mockV2Intent.escrow, undefined);
|
|
67
|
+
});
|
|
68
|
+
it('does not require paymentMethod for V2', () => {
|
|
69
|
+
assert.equal(mockV2Intent.paymentMethod, undefined);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
describe('backwards compatibility', () => {
|
|
73
|
+
it('common fields are present in both V2 and V3', () => {
|
|
74
|
+
const commonFields = ['owner', 'to', 'depositId', 'amount', 'timestamp', 'fiatCurrency', 'conversionRate'];
|
|
75
|
+
for (const field of commonFields) {
|
|
76
|
+
assert.ok(field in mockV3Intent, `V3 intent should have ${field}`);
|
|
77
|
+
assert.ok(field in mockV2Intent, `V2 intent should have ${field}`);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
describe('isEmptyIntent', () => {
|
|
83
|
+
it('returns true for null', () => {
|
|
84
|
+
assert.equal(isEmptyIntent(null), true);
|
|
85
|
+
});
|
|
86
|
+
it('returns true for empty intent (all zeros)', () => {
|
|
87
|
+
assert.equal(isEmptyIntent(emptyIntent), true);
|
|
88
|
+
});
|
|
89
|
+
it('returns false when only amount is 0 (other fields non-zero)', () => {
|
|
90
|
+
const intentWithZeroAmount = { ...mockV3Intent, amount: 0n };
|
|
91
|
+
assert.equal(isEmptyIntent(intentWithZeroAmount), false);
|
|
92
|
+
});
|
|
93
|
+
it('returns false when only timestamp is 0 (other fields non-zero)', () => {
|
|
94
|
+
const intentWithZeroTimestamp = { ...mockV3Intent, timestamp: 0n };
|
|
95
|
+
assert.equal(isEmptyIntent(intentWithZeroTimestamp), false);
|
|
96
|
+
});
|
|
97
|
+
it('returns false when only depositId is 0 (other fields non-zero)', () => {
|
|
98
|
+
const intentWithZeroDepositId = { ...mockV3Intent, depositId: 0n };
|
|
99
|
+
assert.equal(isEmptyIntent(intentWithZeroDepositId), false);
|
|
100
|
+
});
|
|
101
|
+
it('returns false for valid V3 intent', () => {
|
|
102
|
+
assert.equal(isEmptyIntent(mockV3Intent), false);
|
|
103
|
+
});
|
|
104
|
+
it('returns false for valid V2 intent', () => {
|
|
105
|
+
assert.equal(isEmptyIntent(mockV2Intent), false);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
describe('payment identifier handling', () => {
|
|
109
|
+
it('can extract payment identifier from V3 intent', () => {
|
|
110
|
+
const paymentIdentifier = mockV3Intent.paymentMethod ?? mockV3Intent.paymentVerifier ?? '0x';
|
|
111
|
+
assert.equal(paymentIdentifier, mockV3Intent.paymentMethod);
|
|
112
|
+
assert.equal(paymentIdentifier.length, 66, 'bytes32');
|
|
113
|
+
});
|
|
114
|
+
it('can extract payment identifier from V2 intent', () => {
|
|
115
|
+
const paymentIdentifier = mockV2Intent.paymentMethod ?? mockV2Intent.paymentVerifier ?? '0x';
|
|
116
|
+
assert.equal(paymentIdentifier, mockV2Intent.paymentVerifier);
|
|
117
|
+
assert.equal(paymentIdentifier.length, 42, 'address');
|
|
118
|
+
});
|
|
119
|
+
});
|
package/dist/src/json.d.ts
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* JSON
|
|
2
|
+
* @fileoverview JSON utilities for handling bigint serialization.
|
|
3
|
+
*
|
|
4
|
+
* Standard JSON.stringify cannot handle bigint values. This module provides
|
|
5
|
+
* a serializer that converts bigints to strings, and utilities for
|
|
6
|
+
* converting string fields back to bigints after parsing.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* JSON serializer with bigint support.
|
|
10
|
+
* Converts bigints to strings during stringify, allowing JSON.parse to read them later.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const obj = { amount: 100_000000n, name: 'test' }
|
|
15
|
+
* const json = jsonSerializer.stringify(obj)
|
|
16
|
+
* // '{"amount":"100000000","name":"test"}'
|
|
17
|
+
*
|
|
18
|
+
* const parsed = jsonSerializer.parse(json)
|
|
19
|
+
* // { amount: "100000000", name: "test" } (amount is still a string!)
|
|
20
|
+
*
|
|
21
|
+
* // Use mapBigIntFields to convert back to bigint
|
|
22
|
+
* const restored = mapBigIntFields(parsed, ['amount'])
|
|
23
|
+
* // { amount: 100000000n, name: 'test' }
|
|
24
|
+
* ```
|
|
3
25
|
*/
|
|
4
26
|
export declare const jsonSerializer: {
|
|
5
27
|
/**
|
|
@@ -15,4 +37,19 @@ export declare const jsonSerializer: {
|
|
|
15
37
|
*/
|
|
16
38
|
stringify: (obj: unknown) => string;
|
|
17
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Recursively convert specified string fields to BigInt in an object or array.
|
|
42
|
+
* Used after JSON.parse to restore bigint values that were serialized as strings.
|
|
43
|
+
*
|
|
44
|
+
* @param obj - The object or array to process
|
|
45
|
+
* @param fields - Array of field names that should be converted to BigInt
|
|
46
|
+
* @returns The object with specified fields converted to BigInt
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const data = { amount: "100", nested: { value: "50" }, name: "test" }
|
|
51
|
+
* const result = mapBigIntFields(data, ['amount', 'value'])
|
|
52
|
+
* // { amount: 100n, nested: { value: 50n }, name: "test" }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
18
55
|
export declare function mapBigIntFields<T>(obj: T, fields: string[]): T;
|
package/dist/src/json.js
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* JSON
|
|
2
|
+
* @fileoverview JSON utilities for handling bigint serialization.
|
|
3
|
+
*
|
|
4
|
+
* Standard JSON.stringify cannot handle bigint values. This module provides
|
|
5
|
+
* a serializer that converts bigints to strings, and utilities for
|
|
6
|
+
* converting string fields back to bigints after parsing.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* JSON serializer with bigint support.
|
|
10
|
+
* Converts bigints to strings during stringify, allowing JSON.parse to read them later.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const obj = { amount: 100_000000n, name: 'test' }
|
|
15
|
+
* const json = jsonSerializer.stringify(obj)
|
|
16
|
+
* // '{"amount":"100000000","name":"test"}'
|
|
17
|
+
*
|
|
18
|
+
* const parsed = jsonSerializer.parse(json)
|
|
19
|
+
* // { amount: "100000000", name: "test" } (amount is still a string!)
|
|
20
|
+
*
|
|
21
|
+
* // Use mapBigIntFields to convert back to bigint
|
|
22
|
+
* const restored = mapBigIntFields(parsed, ['amount'])
|
|
23
|
+
* // { amount: 100000000n, name: 'test' }
|
|
24
|
+
* ```
|
|
3
25
|
*/
|
|
4
26
|
export const jsonSerializer = {
|
|
5
27
|
/**
|
|
@@ -20,7 +42,21 @@ export const jsonSerializer = {
|
|
|
20
42
|
return value;
|
|
21
43
|
})
|
|
22
44
|
};
|
|
23
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Recursively convert specified string fields to BigInt in an object or array.
|
|
47
|
+
* Used after JSON.parse to restore bigint values that were serialized as strings.
|
|
48
|
+
*
|
|
49
|
+
* @param obj - The object or array to process
|
|
50
|
+
* @param fields - Array of field names that should be converted to BigInt
|
|
51
|
+
* @returns The object with specified fields converted to BigInt
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const data = { amount: "100", nested: { value: "50" }, name: "test" }
|
|
56
|
+
* const result = mapBigIntFields(data, ['amount', 'value'])
|
|
57
|
+
* // { amount: 100n, nested: { value: 50n }, name: "test" }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
24
60
|
export function mapBigIntFields(obj, fields) {
|
|
25
61
|
if (Array.isArray(obj)) {
|
|
26
62
|
return obj.map(item => mapBigIntFields(item, fields));
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import type { Hex } from 'viem';
|
|
2
|
+
import type { Version } from './contracts.js';
|
|
3
|
+
/**
|
|
4
|
+
* V2 Deposit structure as stored in the database.
|
|
5
|
+
* This represents the raw v2 deposit data from the indexer.
|
|
6
|
+
*/
|
|
7
|
+
export type V2DepositData = {
|
|
8
|
+
escrow: Hex;
|
|
9
|
+
localId: bigint;
|
|
10
|
+
chainId: bigint;
|
|
11
|
+
orderId: Hex;
|
|
12
|
+
logId: Hex;
|
|
13
|
+
transactionId: Hex;
|
|
14
|
+
token: Hex;
|
|
15
|
+
participantId: Hex;
|
|
16
|
+
deposited: bigint;
|
|
17
|
+
remaining: bigint;
|
|
18
|
+
minAmount: bigint;
|
|
19
|
+
maxAmount: bigint;
|
|
20
|
+
depositConversionRateCurrencies: Hex[];
|
|
21
|
+
depositConversionRateVerifiers: Hex[];
|
|
22
|
+
version: 'v2';
|
|
23
|
+
acceptingIntents: boolean;
|
|
24
|
+
delegate: Hex;
|
|
25
|
+
intentGuardian: Hex;
|
|
26
|
+
status: 'active' | 'underfunded' | 'withdrawn' | 'closed';
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* V3 Deposit structure - the normalized format.
|
|
30
|
+
* V3 deposits have the same core fields but may include additional v3-specific metadata.
|
|
31
|
+
*/
|
|
32
|
+
export type V3DepositData = Omit<V2DepositData, 'version'> & {
|
|
33
|
+
version: 'v3';
|
|
34
|
+
/**
|
|
35
|
+
* Tracks the original version if this deposit was normalized from v2.
|
|
36
|
+
* Undefined for native v3 deposits.
|
|
37
|
+
*/
|
|
38
|
+
_originalVersion?: Version;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Normalizes a V2 deposit to V3 format for unified UI consumption.
|
|
42
|
+
*
|
|
43
|
+
* Since deposits are already stored in a unified schema, this function
|
|
44
|
+
* primarily marks the deposit as "appearing" as v3 while preserving
|
|
45
|
+
* the original version information.
|
|
46
|
+
*
|
|
47
|
+
* @param v2Deposit - The v2 deposit data from the indexer
|
|
48
|
+
* @returns The deposit in v3-normalized format
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const v2Deposit = await getDeposit(escrow, depositId, chainId)
|
|
53
|
+
* if (v2Deposit.version === 'v2') {
|
|
54
|
+
* const normalized = normalizeV2Deposit(v2Deposit)
|
|
55
|
+
* // normalized.version === 'v3'
|
|
56
|
+
* // normalized._originalVersion === 'v2'
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function normalizeV2Deposit(v2Deposit: V2DepositData): V3DepositData;
|
|
61
|
+
/**
|
|
62
|
+
* V2 Intent structure as stored in the database.
|
|
63
|
+
* V2 intents use v2Currency and v2ConversionRateChangeId for rate lookups.
|
|
64
|
+
*/
|
|
65
|
+
export type V2IntentData = {
|
|
66
|
+
intentHash: Hex;
|
|
67
|
+
orderId: Hex;
|
|
68
|
+
logId: Hex;
|
|
69
|
+
transactionId: Hex;
|
|
70
|
+
depositEscrow: Hex;
|
|
71
|
+
depositLocalId: bigint;
|
|
72
|
+
depositChainId: bigint;
|
|
73
|
+
escrow: Hex;
|
|
74
|
+
paymentMethodId: Hex;
|
|
75
|
+
ownerParticipantId: Hex;
|
|
76
|
+
toParticipantId: Hex;
|
|
77
|
+
amount: bigint;
|
|
78
|
+
fiatCurrency: Hex;
|
|
79
|
+
conversionRate: bigint;
|
|
80
|
+
timestamp: bigint;
|
|
81
|
+
/** V2-specific: Currency hash used for conversion rate lookup */
|
|
82
|
+
v2Currency: Hex;
|
|
83
|
+
/** V2-specific: Conversion rate change ID at intent time */
|
|
84
|
+
v2ConversionRateChangeId: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* V3 Intent structure - the normalized format.
|
|
88
|
+
* V3 intents rely on paymentMethodId for all rate/currency information.
|
|
89
|
+
*/
|
|
90
|
+
export type V3IntentData = {
|
|
91
|
+
intentHash: Hex;
|
|
92
|
+
orderId: Hex;
|
|
93
|
+
logId: Hex;
|
|
94
|
+
transactionId: Hex;
|
|
95
|
+
depositEscrow: Hex;
|
|
96
|
+
depositLocalId: bigint;
|
|
97
|
+
depositChainId: bigint;
|
|
98
|
+
escrow: Hex;
|
|
99
|
+
paymentMethodId: Hex;
|
|
100
|
+
ownerParticipantId: Hex;
|
|
101
|
+
toParticipantId: Hex;
|
|
102
|
+
amount: bigint;
|
|
103
|
+
fiatCurrency: Hex;
|
|
104
|
+
conversionRate: bigint;
|
|
105
|
+
timestamp: bigint;
|
|
106
|
+
/** V2-specific fields are null in v3 native intents */
|
|
107
|
+
v2Currency: null;
|
|
108
|
+
v2ConversionRateChangeId: null;
|
|
109
|
+
/**
|
|
110
|
+
* Tracks the original version if this intent was normalized from v2.
|
|
111
|
+
* Undefined for native v3 intents.
|
|
112
|
+
*/
|
|
113
|
+
_originalVersion?: Version;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Normalizes a V2 intent to V3 format for unified UI consumption.
|
|
117
|
+
*
|
|
118
|
+
* V2 intents store currency and rate change ID explicitly, while V3 intents
|
|
119
|
+
* derive this information from the paymentMethodId relationship.
|
|
120
|
+
* This function converts v2 intent data to the v3 format.
|
|
121
|
+
*
|
|
122
|
+
* @param v2Intent - The v2 intent data from the indexer
|
|
123
|
+
* @returns The intent in v3-normalized format
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const intent = await getIntent(intentHash)
|
|
128
|
+
* if (intent.v2Currency !== null) {
|
|
129
|
+
* const normalized = normalizeV2Intent(intent)
|
|
130
|
+
* // normalized.v2Currency === null
|
|
131
|
+
* // normalized._originalVersion === 'v2'
|
|
132
|
+
* }
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export declare function normalizeV2Intent(v2Intent: V2IntentData): V3IntentData;
|
|
136
|
+
/**
|
|
137
|
+
* V2 Payment Verification data.
|
|
138
|
+
* V2 uses ZK proofs verified on-chain via payment verifier contracts.
|
|
139
|
+
*/
|
|
140
|
+
export type V2VerificationData = {
|
|
141
|
+
intentHash: Hex;
|
|
142
|
+
verifierAddress: Hex;
|
|
143
|
+
proofTimestamp: bigint;
|
|
144
|
+
transactionId: Hex;
|
|
145
|
+
depositorAddress: Hex;
|
|
146
|
+
depositId: bigint;
|
|
147
|
+
amount: bigint;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* V3-style attestation format.
|
|
151
|
+
* V3 uses EIP-712 signed attestations from trusted attesters.
|
|
152
|
+
*/
|
|
153
|
+
export type V3AttestationData = {
|
|
154
|
+
intentHash: Hex;
|
|
155
|
+
attestation: {
|
|
156
|
+
depositor: Hex;
|
|
157
|
+
depositId: bigint;
|
|
158
|
+
amount: bigint;
|
|
159
|
+
/** Payment identifier (synthesized from v2 proof data) */
|
|
160
|
+
paymentId: string;
|
|
161
|
+
timestamp: bigint;
|
|
162
|
+
};
|
|
163
|
+
/** Signature placeholder - v2 doesn't have signatures */
|
|
164
|
+
signature: null;
|
|
165
|
+
/**
|
|
166
|
+
* Tracks the original version if this attestation was normalized from v2.
|
|
167
|
+
*/
|
|
168
|
+
_originalVersion: 'v2';
|
|
169
|
+
/** Original v2 verification data preserved for reference */
|
|
170
|
+
_v2Verification: V2VerificationData;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Normalizes a V2 payment verification to V3 attestation format.
|
|
174
|
+
*
|
|
175
|
+
* V2 uses ZK proofs verified by on-chain verifier contracts, while V3 uses
|
|
176
|
+
* EIP-712 signed attestations. This function synthesizes an attestation-like
|
|
177
|
+
* structure from v2 verification data.
|
|
178
|
+
*
|
|
179
|
+
* Note: The signature field is null since v2 verifications don't use signatures.
|
|
180
|
+
* The original v2 verification data is preserved in `_v2Verification`.
|
|
181
|
+
*
|
|
182
|
+
* @param v2Verification - The v2 verification data
|
|
183
|
+
* @returns A v3-style attestation structure
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const verification = getV2Verification(intentHash)
|
|
188
|
+
* const attestation = normalizeV2Verification(verification)
|
|
189
|
+
* // attestation.attestation.depositor === verification.depositorAddress
|
|
190
|
+
* // attestation.signature === null (v2 doesn't have signatures)
|
|
191
|
+
* // attestation._originalVersion === 'v2'
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
export declare function normalizeV2Verification(v2Verification: V2VerificationData): V3AttestationData;
|
|
195
|
+
/**
|
|
196
|
+
* Type guard to check if a deposit is a v2 deposit.
|
|
197
|
+
* @param deposit - The deposit data to check
|
|
198
|
+
* @returns True if the deposit is from v2 escrow
|
|
199
|
+
*/
|
|
200
|
+
export declare function isV2Deposit(deposit: {
|
|
201
|
+
version: Version;
|
|
202
|
+
}): deposit is {
|
|
203
|
+
version: 'v2';
|
|
204
|
+
} & typeof deposit;
|
|
205
|
+
/**
|
|
206
|
+
* Type guard to check if an intent is a v2 intent.
|
|
207
|
+
* @param intent - The intent data to check
|
|
208
|
+
* @returns True if the intent has v2-specific fields populated
|
|
209
|
+
*/
|
|
210
|
+
export declare function isV2Intent(intent: {
|
|
211
|
+
v2Currency: Hex | null;
|
|
212
|
+
}): intent is {
|
|
213
|
+
v2Currency: Hex;
|
|
214
|
+
} & typeof intent;
|
|
215
|
+
/**
|
|
216
|
+
* Type guard to check if data was normalized from v2.
|
|
217
|
+
* @param data - The data to check
|
|
218
|
+
* @returns True if the data has _originalVersion === 'v2'
|
|
219
|
+
*/
|
|
220
|
+
export declare function isNormalizedFromV2(data: {
|
|
221
|
+
_originalVersion?: Version;
|
|
222
|
+
}): data is {
|
|
223
|
+
_originalVersion: 'v2';
|
|
224
|
+
} & typeof data;
|