@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
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes a V2 deposit to V3 format for unified UI consumption.
|
|
3
|
+
*
|
|
4
|
+
* Since deposits are already stored in a unified schema, this function
|
|
5
|
+
* primarily marks the deposit as "appearing" as v3 while preserving
|
|
6
|
+
* the original version information.
|
|
7
|
+
*
|
|
8
|
+
* @param v2Deposit - The v2 deposit data from the indexer
|
|
9
|
+
* @returns The deposit in v3-normalized format
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const v2Deposit = await getDeposit(escrow, depositId, chainId)
|
|
14
|
+
* if (v2Deposit.version === 'v2') {
|
|
15
|
+
* const normalized = normalizeV2Deposit(v2Deposit)
|
|
16
|
+
* // normalized.version === 'v3'
|
|
17
|
+
* // normalized._originalVersion === 'v2'
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function normalizeV2Deposit(v2Deposit) {
|
|
22
|
+
const { version: _v2Version, ...rest } = v2Deposit;
|
|
23
|
+
return {
|
|
24
|
+
...rest,
|
|
25
|
+
version: 'v3',
|
|
26
|
+
_originalVersion: 'v2',
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Normalizes a V2 intent to V3 format for unified UI consumption.
|
|
31
|
+
*
|
|
32
|
+
* V2 intents store currency and rate change ID explicitly, while V3 intents
|
|
33
|
+
* derive this information from the paymentMethodId relationship.
|
|
34
|
+
* This function converts v2 intent data to the v3 format.
|
|
35
|
+
*
|
|
36
|
+
* @param v2Intent - The v2 intent data from the indexer
|
|
37
|
+
* @returns The intent in v3-normalized format
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const intent = await getIntent(intentHash)
|
|
42
|
+
* if (intent.v2Currency !== null) {
|
|
43
|
+
* const normalized = normalizeV2Intent(intent)
|
|
44
|
+
* // normalized.v2Currency === null
|
|
45
|
+
* // normalized._originalVersion === 'v2'
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function normalizeV2Intent(v2Intent) {
|
|
50
|
+
const { v2Currency: _currency, v2ConversionRateChangeId: _changeId, ...rest } = v2Intent;
|
|
51
|
+
return {
|
|
52
|
+
...rest,
|
|
53
|
+
v2Currency: null,
|
|
54
|
+
v2ConversionRateChangeId: null,
|
|
55
|
+
_originalVersion: 'v2',
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Normalizes a V2 payment verification to V3 attestation format.
|
|
60
|
+
*
|
|
61
|
+
* V2 uses ZK proofs verified by on-chain verifier contracts, while V3 uses
|
|
62
|
+
* EIP-712 signed attestations. This function synthesizes an attestation-like
|
|
63
|
+
* structure from v2 verification data.
|
|
64
|
+
*
|
|
65
|
+
* Note: The signature field is null since v2 verifications don't use signatures.
|
|
66
|
+
* The original v2 verification data is preserved in `_v2Verification`.
|
|
67
|
+
*
|
|
68
|
+
* @param v2Verification - The v2 verification data
|
|
69
|
+
* @returns A v3-style attestation structure
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const verification = getV2Verification(intentHash)
|
|
74
|
+
* const attestation = normalizeV2Verification(verification)
|
|
75
|
+
* // attestation.attestation.depositor === verification.depositorAddress
|
|
76
|
+
* // attestation.signature === null (v2 doesn't have signatures)
|
|
77
|
+
* // attestation._originalVersion === 'v2'
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export function normalizeV2Verification(v2Verification) {
|
|
81
|
+
return {
|
|
82
|
+
intentHash: v2Verification.intentHash,
|
|
83
|
+
attestation: {
|
|
84
|
+
depositor: v2Verification.depositorAddress,
|
|
85
|
+
depositId: v2Verification.depositId,
|
|
86
|
+
amount: v2Verification.amount,
|
|
87
|
+
// Synthesize a payment ID from intent hash (v2 doesn't have explicit payment IDs)
|
|
88
|
+
paymentId: `v2-${v2Verification.intentHash.slice(0, 18)}`,
|
|
89
|
+
timestamp: v2Verification.proofTimestamp,
|
|
90
|
+
},
|
|
91
|
+
signature: null,
|
|
92
|
+
_originalVersion: 'v2',
|
|
93
|
+
_v2Verification: v2Verification,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Type guard to check if a deposit is a v2 deposit.
|
|
98
|
+
* @param deposit - The deposit data to check
|
|
99
|
+
* @returns True if the deposit is from v2 escrow
|
|
100
|
+
*/
|
|
101
|
+
export function isV2Deposit(deposit) {
|
|
102
|
+
return deposit.version === 'v2';
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Type guard to check if an intent is a v2 intent.
|
|
106
|
+
* @param intent - The intent data to check
|
|
107
|
+
* @returns True if the intent has v2-specific fields populated
|
|
108
|
+
*/
|
|
109
|
+
export function isV2Intent(intent) {
|
|
110
|
+
return intent.v2Currency !== null;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Type guard to check if data was normalized from v2.
|
|
114
|
+
* @param data - The data to check
|
|
115
|
+
* @returns True if the data has _originalVersion === 'v2'
|
|
116
|
+
*/
|
|
117
|
+
export function isNormalizedFromV2(data) {
|
|
118
|
+
return data._originalVersion === 'v2';
|
|
119
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { normalizeV2Deposit, normalizeV2Intent, normalizeV2Verification, isV2Deposit, isV2Intent, isNormalizedFromV2, } from './normalization.js';
|
|
4
|
+
describe('normalization', () => {
|
|
5
|
+
// Test data fixtures
|
|
6
|
+
const TEST_ESCROW = '0x1234567890123456789012345678901234567890';
|
|
7
|
+
const TEST_TOKEN = '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913';
|
|
8
|
+
const TEST_PARTICIPANT = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd';
|
|
9
|
+
const TEST_DELEGATE = '0x1111111111111111111111111111111111111111';
|
|
10
|
+
const TEST_GUARDIAN = '0x2222222222222222222222222222222222222222';
|
|
11
|
+
const TEST_ORDER_ID = '0x3333333333333333333333333333333333333333333333333333333333333333';
|
|
12
|
+
const TEST_LOG_ID = '0x4444444444444444444444444444444444444444444444444444444444444444';
|
|
13
|
+
const TEST_TX_ID = '0x5555555555555555555555555555555555555555555555555555555555555555';
|
|
14
|
+
const TEST_INTENT_HASH = '0x6666666666666666666666666666666666666666666666666666666666666666';
|
|
15
|
+
const TEST_CURRENCY = '0x7777777777777777777777777777777777777777777777777777777777777777';
|
|
16
|
+
const TEST_PAYMENT_METHOD = '0x8888888888888888888888888888888888888888888888888888888888888888';
|
|
17
|
+
const TEST_VERIFIER = '0x9999999999999999999999999999999999999999';
|
|
18
|
+
const createV2Deposit = () => ({
|
|
19
|
+
escrow: TEST_ESCROW,
|
|
20
|
+
localId: 1n,
|
|
21
|
+
chainId: 8453n,
|
|
22
|
+
orderId: TEST_ORDER_ID,
|
|
23
|
+
logId: TEST_LOG_ID,
|
|
24
|
+
transactionId: TEST_TX_ID,
|
|
25
|
+
token: TEST_TOKEN,
|
|
26
|
+
participantId: TEST_PARTICIPANT,
|
|
27
|
+
deposited: 1000000000n,
|
|
28
|
+
remaining: 500000000n,
|
|
29
|
+
minAmount: 100000000n,
|
|
30
|
+
maxAmount: 1000000000n,
|
|
31
|
+
depositConversionRateCurrencies: [TEST_CURRENCY],
|
|
32
|
+
depositConversionRateVerifiers: [TEST_VERIFIER],
|
|
33
|
+
version: 'v2',
|
|
34
|
+
acceptingIntents: true,
|
|
35
|
+
delegate: TEST_DELEGATE,
|
|
36
|
+
intentGuardian: TEST_GUARDIAN,
|
|
37
|
+
status: 'active',
|
|
38
|
+
});
|
|
39
|
+
const createV2Intent = () => ({
|
|
40
|
+
intentHash: TEST_INTENT_HASH,
|
|
41
|
+
orderId: TEST_ORDER_ID,
|
|
42
|
+
logId: TEST_LOG_ID,
|
|
43
|
+
transactionId: TEST_TX_ID,
|
|
44
|
+
depositEscrow: TEST_ESCROW,
|
|
45
|
+
depositLocalId: 1n,
|
|
46
|
+
depositChainId: 8453n,
|
|
47
|
+
escrow: TEST_ESCROW,
|
|
48
|
+
paymentMethodId: TEST_PAYMENT_METHOD,
|
|
49
|
+
ownerParticipantId: TEST_PARTICIPANT,
|
|
50
|
+
toParticipantId: TEST_DELEGATE,
|
|
51
|
+
amount: 500000000n,
|
|
52
|
+
fiatCurrency: TEST_CURRENCY,
|
|
53
|
+
conversionRate: 1000000n,
|
|
54
|
+
timestamp: 1704067200n,
|
|
55
|
+
v2Currency: TEST_CURRENCY,
|
|
56
|
+
v2ConversionRateChangeId: 42,
|
|
57
|
+
});
|
|
58
|
+
const createV2Verification = () => ({
|
|
59
|
+
intentHash: TEST_INTENT_HASH,
|
|
60
|
+
verifierAddress: TEST_VERIFIER,
|
|
61
|
+
proofTimestamp: 1704067200n,
|
|
62
|
+
transactionId: TEST_TX_ID,
|
|
63
|
+
depositorAddress: TEST_PARTICIPANT,
|
|
64
|
+
depositId: 1n,
|
|
65
|
+
amount: 500000000n,
|
|
66
|
+
});
|
|
67
|
+
describe('normalizeV2Deposit', () => {
|
|
68
|
+
it('should convert v2 deposit to v3 format', () => {
|
|
69
|
+
const v2Deposit = createV2Deposit();
|
|
70
|
+
const normalized = normalizeV2Deposit(v2Deposit);
|
|
71
|
+
assert.equal(normalized.version, 'v3');
|
|
72
|
+
assert.equal(normalized._originalVersion, 'v2');
|
|
73
|
+
});
|
|
74
|
+
it('should preserve all original fields', () => {
|
|
75
|
+
const v2Deposit = createV2Deposit();
|
|
76
|
+
const normalized = normalizeV2Deposit(v2Deposit);
|
|
77
|
+
assert.equal(normalized.escrow, v2Deposit.escrow);
|
|
78
|
+
assert.equal(normalized.localId, v2Deposit.localId);
|
|
79
|
+
assert.equal(normalized.chainId, v2Deposit.chainId);
|
|
80
|
+
assert.equal(normalized.orderId, v2Deposit.orderId);
|
|
81
|
+
assert.equal(normalized.logId, v2Deposit.logId);
|
|
82
|
+
assert.equal(normalized.transactionId, v2Deposit.transactionId);
|
|
83
|
+
assert.equal(normalized.token, v2Deposit.token);
|
|
84
|
+
assert.equal(normalized.participantId, v2Deposit.participantId);
|
|
85
|
+
assert.equal(normalized.deposited, v2Deposit.deposited);
|
|
86
|
+
assert.equal(normalized.remaining, v2Deposit.remaining);
|
|
87
|
+
assert.equal(normalized.minAmount, v2Deposit.minAmount);
|
|
88
|
+
assert.equal(normalized.maxAmount, v2Deposit.maxAmount);
|
|
89
|
+
assert.deepEqual(normalized.depositConversionRateCurrencies, v2Deposit.depositConversionRateCurrencies);
|
|
90
|
+
assert.deepEqual(normalized.depositConversionRateVerifiers, v2Deposit.depositConversionRateVerifiers);
|
|
91
|
+
assert.equal(normalized.acceptingIntents, v2Deposit.acceptingIntents);
|
|
92
|
+
assert.equal(normalized.delegate, v2Deposit.delegate);
|
|
93
|
+
assert.equal(normalized.intentGuardian, v2Deposit.intentGuardian);
|
|
94
|
+
assert.equal(normalized.status, v2Deposit.status);
|
|
95
|
+
});
|
|
96
|
+
it('should not mutate original deposit', () => {
|
|
97
|
+
const v2Deposit = createV2Deposit();
|
|
98
|
+
const originalVersion = v2Deposit.version;
|
|
99
|
+
normalizeV2Deposit(v2Deposit);
|
|
100
|
+
assert.equal(v2Deposit.version, originalVersion);
|
|
101
|
+
});
|
|
102
|
+
it('should handle deposits with different statuses', () => {
|
|
103
|
+
const statuses = [
|
|
104
|
+
'active',
|
|
105
|
+
'underfunded',
|
|
106
|
+
'withdrawn',
|
|
107
|
+
'closed',
|
|
108
|
+
];
|
|
109
|
+
for (const status of statuses) {
|
|
110
|
+
const v2Deposit = { ...createV2Deposit(), status };
|
|
111
|
+
const normalized = normalizeV2Deposit(v2Deposit);
|
|
112
|
+
assert.equal(normalized.status, status);
|
|
113
|
+
assert.equal(normalized.version, 'v3');
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
it('should handle deposits with empty arrays', () => {
|
|
117
|
+
const v2Deposit = {
|
|
118
|
+
...createV2Deposit(),
|
|
119
|
+
depositConversionRateCurrencies: [],
|
|
120
|
+
depositConversionRateVerifiers: [],
|
|
121
|
+
};
|
|
122
|
+
const normalized = normalizeV2Deposit(v2Deposit);
|
|
123
|
+
assert.deepEqual(normalized.depositConversionRateCurrencies, []);
|
|
124
|
+
assert.deepEqual(normalized.depositConversionRateVerifiers, []);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe('normalizeV2Intent', () => {
|
|
128
|
+
it('should convert v2 intent to v3 format', () => {
|
|
129
|
+
const v2Intent = createV2Intent();
|
|
130
|
+
const normalized = normalizeV2Intent(v2Intent);
|
|
131
|
+
assert.equal(normalized.v2Currency, null);
|
|
132
|
+
assert.equal(normalized.v2ConversionRateChangeId, null);
|
|
133
|
+
assert.equal(normalized._originalVersion, 'v2');
|
|
134
|
+
});
|
|
135
|
+
it('should preserve all non-v2-specific fields', () => {
|
|
136
|
+
const v2Intent = createV2Intent();
|
|
137
|
+
const normalized = normalizeV2Intent(v2Intent);
|
|
138
|
+
assert.equal(normalized.intentHash, v2Intent.intentHash);
|
|
139
|
+
assert.equal(normalized.orderId, v2Intent.orderId);
|
|
140
|
+
assert.equal(normalized.logId, v2Intent.logId);
|
|
141
|
+
assert.equal(normalized.transactionId, v2Intent.transactionId);
|
|
142
|
+
assert.equal(normalized.depositEscrow, v2Intent.depositEscrow);
|
|
143
|
+
assert.equal(normalized.depositLocalId, v2Intent.depositLocalId);
|
|
144
|
+
assert.equal(normalized.depositChainId, v2Intent.depositChainId);
|
|
145
|
+
assert.equal(normalized.escrow, v2Intent.escrow);
|
|
146
|
+
assert.equal(normalized.paymentMethodId, v2Intent.paymentMethodId);
|
|
147
|
+
assert.equal(normalized.ownerParticipantId, v2Intent.ownerParticipantId);
|
|
148
|
+
assert.equal(normalized.toParticipantId, v2Intent.toParticipantId);
|
|
149
|
+
assert.equal(normalized.amount, v2Intent.amount);
|
|
150
|
+
assert.equal(normalized.fiatCurrency, v2Intent.fiatCurrency);
|
|
151
|
+
assert.equal(normalized.conversionRate, v2Intent.conversionRate);
|
|
152
|
+
assert.equal(normalized.timestamp, v2Intent.timestamp);
|
|
153
|
+
});
|
|
154
|
+
it('should not mutate original intent', () => {
|
|
155
|
+
const v2Intent = createV2Intent();
|
|
156
|
+
const originalCurrency = v2Intent.v2Currency;
|
|
157
|
+
const originalChangeId = v2Intent.v2ConversionRateChangeId;
|
|
158
|
+
normalizeV2Intent(v2Intent);
|
|
159
|
+
assert.equal(v2Intent.v2Currency, originalCurrency);
|
|
160
|
+
assert.equal(v2Intent.v2ConversionRateChangeId, originalChangeId);
|
|
161
|
+
});
|
|
162
|
+
it('should clear v2-specific rate lookup fields', () => {
|
|
163
|
+
const v2Intent = createV2Intent();
|
|
164
|
+
const normalized = normalizeV2Intent(v2Intent);
|
|
165
|
+
// V3 intents derive rate info from paymentMethodId, not these fields
|
|
166
|
+
assert.strictEqual(normalized.v2Currency, null);
|
|
167
|
+
assert.strictEqual(normalized.v2ConversionRateChangeId, null);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
describe('normalizeV2Verification', () => {
|
|
171
|
+
it('should convert v2 verification to v3 attestation format', () => {
|
|
172
|
+
const v2Verification = createV2Verification();
|
|
173
|
+
const normalized = normalizeV2Verification(v2Verification);
|
|
174
|
+
assert.equal(normalized.intentHash, v2Verification.intentHash);
|
|
175
|
+
assert.equal(normalized._originalVersion, 'v2');
|
|
176
|
+
assert.equal(normalized.signature, null);
|
|
177
|
+
});
|
|
178
|
+
it('should create attestation structure from verification data', () => {
|
|
179
|
+
const v2Verification = createV2Verification();
|
|
180
|
+
const normalized = normalizeV2Verification(v2Verification);
|
|
181
|
+
assert.equal(normalized.attestation.depositor, v2Verification.depositorAddress);
|
|
182
|
+
assert.equal(normalized.attestation.depositId, v2Verification.depositId);
|
|
183
|
+
assert.equal(normalized.attestation.amount, v2Verification.amount);
|
|
184
|
+
assert.equal(normalized.attestation.timestamp, v2Verification.proofTimestamp);
|
|
185
|
+
});
|
|
186
|
+
it('should synthesize paymentId from intent hash', () => {
|
|
187
|
+
const v2Verification = createV2Verification();
|
|
188
|
+
const normalized = normalizeV2Verification(v2Verification);
|
|
189
|
+
assert.ok(normalized.attestation.paymentId.startsWith('v2-'));
|
|
190
|
+
assert.ok(normalized.attestation.paymentId.includes(v2Verification.intentHash.slice(2, 18)));
|
|
191
|
+
});
|
|
192
|
+
it('should preserve original v2 verification data', () => {
|
|
193
|
+
const v2Verification = createV2Verification();
|
|
194
|
+
const normalized = normalizeV2Verification(v2Verification);
|
|
195
|
+
assert.deepEqual(normalized._v2Verification, v2Verification);
|
|
196
|
+
});
|
|
197
|
+
it('should not mutate original verification', () => {
|
|
198
|
+
const v2Verification = createV2Verification();
|
|
199
|
+
const originalHash = v2Verification.intentHash;
|
|
200
|
+
normalizeV2Verification(v2Verification);
|
|
201
|
+
assert.equal(v2Verification.intentHash, originalHash);
|
|
202
|
+
});
|
|
203
|
+
it('should have null signature since v2 does not use signatures', () => {
|
|
204
|
+
const v2Verification = createV2Verification();
|
|
205
|
+
const normalized = normalizeV2Verification(v2Verification);
|
|
206
|
+
assert.strictEqual(normalized.signature, null);
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
describe('isV2Deposit', () => {
|
|
210
|
+
it('should return true for v2 deposits', () => {
|
|
211
|
+
const v2Deposit = createV2Deposit();
|
|
212
|
+
assert.equal(isV2Deposit(v2Deposit), true);
|
|
213
|
+
});
|
|
214
|
+
it('should return false for v3 deposits', () => {
|
|
215
|
+
const v3Deposit = { version: 'v3' };
|
|
216
|
+
assert.equal(isV2Deposit(v3Deposit), false);
|
|
217
|
+
});
|
|
218
|
+
it('should work with normalized deposits', () => {
|
|
219
|
+
const v2Deposit = createV2Deposit();
|
|
220
|
+
const normalized = normalizeV2Deposit(v2Deposit);
|
|
221
|
+
assert.equal(isV2Deposit(normalized), false); // Normalized is now v3
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
describe('isV2Intent', () => {
|
|
225
|
+
it('should return true for v2 intents with non-null v2Currency', () => {
|
|
226
|
+
const v2Intent = createV2Intent();
|
|
227
|
+
assert.equal(isV2Intent(v2Intent), true);
|
|
228
|
+
});
|
|
229
|
+
it('should return false for v3 intents with null v2Currency', () => {
|
|
230
|
+
const v3Intent = { v2Currency: null };
|
|
231
|
+
assert.equal(isV2Intent(v3Intent), false);
|
|
232
|
+
});
|
|
233
|
+
it('should work with normalized intents', () => {
|
|
234
|
+
const v2Intent = createV2Intent();
|
|
235
|
+
const normalized = normalizeV2Intent(v2Intent);
|
|
236
|
+
assert.equal(isV2Intent(normalized), false); // Normalized has null v2Currency
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
describe('isNormalizedFromV2', () => {
|
|
240
|
+
it('should return true for normalized v2 deposits', () => {
|
|
241
|
+
const v2Deposit = createV2Deposit();
|
|
242
|
+
const normalized = normalizeV2Deposit(v2Deposit);
|
|
243
|
+
assert.equal(isNormalizedFromV2(normalized), true);
|
|
244
|
+
});
|
|
245
|
+
it('should return true for normalized v2 intents', () => {
|
|
246
|
+
const v2Intent = createV2Intent();
|
|
247
|
+
const normalized = normalizeV2Intent(v2Intent);
|
|
248
|
+
assert.equal(isNormalizedFromV2(normalized), true);
|
|
249
|
+
});
|
|
250
|
+
it('should return true for normalized v2 verifications', () => {
|
|
251
|
+
const v2Verification = createV2Verification();
|
|
252
|
+
const normalized = normalizeV2Verification(v2Verification);
|
|
253
|
+
assert.equal(isNormalizedFromV2(normalized), true);
|
|
254
|
+
});
|
|
255
|
+
it('should return false for native v3 data without _originalVersion', () => {
|
|
256
|
+
const nativeV3 = { _originalVersion: undefined };
|
|
257
|
+
assert.equal(isNormalizedFromV2(nativeV3), false);
|
|
258
|
+
});
|
|
259
|
+
it('should return false for data without _originalVersion property', () => {
|
|
260
|
+
const noOriginalVersion = {};
|
|
261
|
+
assert.equal(isNormalizedFromV2(noOriginalVersion), false);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
describe('type safety', () => {
|
|
265
|
+
it('normalizeV2Deposit should return V3DepositData type', () => {
|
|
266
|
+
const v2Deposit = createV2Deposit();
|
|
267
|
+
const normalized = normalizeV2Deposit(v2Deposit);
|
|
268
|
+
assert.ok(normalized);
|
|
269
|
+
assert.equal(normalized.version, 'v3');
|
|
270
|
+
});
|
|
271
|
+
it('normalizeV2Intent should return V3IntentData type', () => {
|
|
272
|
+
const v2Intent = createV2Intent();
|
|
273
|
+
const normalized = normalizeV2Intent(v2Intent);
|
|
274
|
+
assert.ok(normalized);
|
|
275
|
+
assert.strictEqual(normalized.v2Currency, null);
|
|
276
|
+
});
|
|
277
|
+
it('normalizeV2Verification should return V3AttestationData type', () => {
|
|
278
|
+
const v2Verification = createV2Verification();
|
|
279
|
+
const normalized = normalizeV2Verification(v2Verification);
|
|
280
|
+
assert.ok(normalized);
|
|
281
|
+
assert.equal(normalized._originalVersion, 'v2');
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
});
|
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* @fileoverview Participant identification utilities for the indexer.
|
|
3
|
+
*
|
|
4
|
+
* Participants (depositors and payers) are identified by a combination of
|
|
5
|
+
* their chain ID and address, hashed to create a unique cross-chain identifier.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Generate a unique participant ID from chain ID and address.
|
|
9
|
+
* Uses keccak256(chainId || address) to create a deterministic identifier
|
|
10
|
+
* that is unique across all chains.
|
|
11
|
+
*
|
|
12
|
+
* @param chainId - The chain ID (number or bigint)
|
|
13
|
+
* @param address - The participant's wallet address
|
|
14
|
+
* @returns A 32-byte hex string representing the participant ID
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const id = generateParticipantId(8453, '0xabc123...')
|
|
19
|
+
* // '0x...' (keccak256 hash)
|
|
20
|
+
* ```
|
|
6
21
|
*/
|
|
7
22
|
export declare function generateParticipantId(chainId: number | bigint, address: string): string;
|
package/dist/src/participants.js
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Participant identification utilities for the indexer.
|
|
3
|
+
*
|
|
4
|
+
* Participants (depositors and payers) are identified by a combination of
|
|
5
|
+
* their chain ID and address, hashed to create a unique cross-chain identifier.
|
|
6
|
+
*/
|
|
1
7
|
import { concatHex, keccak256, numberToHex } from 'viem';
|
|
2
8
|
/**
|
|
3
|
-
* Generate a
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
9
|
+
* Generate a unique participant ID from chain ID and address.
|
|
10
|
+
* Uses keccak256(chainId || address) to create a deterministic identifier
|
|
11
|
+
* that is unique across all chains.
|
|
12
|
+
*
|
|
13
|
+
* @param chainId - The chain ID (number or bigint)
|
|
14
|
+
* @param address - The participant's wallet address
|
|
15
|
+
* @returns A 32-byte hex string representing the participant ID
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const id = generateParticipantId(8453, '0xabc123...')
|
|
20
|
+
* // '0x...' (keccak256 hash)
|
|
21
|
+
* ```
|
|
7
22
|
*/
|
|
8
23
|
export function generateParticipantId(chainId, address) {
|
|
9
24
|
return keccak256(concatHex([
|