@hsuite/native-connect-types 2.2.0 → 2.2.1
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/hedera-transfer-legs.d.ts +80 -0
- package/dist/hedera-transfer-legs.d.ts.map +1 -0
- package/dist/hedera-transfer-legs.js +95 -0
- package/dist/hedera-transfer-legs.js.map +1 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/ledger.d.ts +20 -0
- package/dist/ledger.d.ts.map +1 -1
- package/dist/ledger.js +22 -0
- package/dist/ledger.js.map +1 -1
- package/dist/messages.d.ts +5 -0
- package/dist/messages.d.ts.map +1 -1
- package/dist/multisig-account.d.ts +41 -1
- package/dist/multisig-account.d.ts.map +1 -1
- package/dist/multisig-account.js +6 -1
- package/dist/multisig-account.js.map +1 -1
- package/dist/rpc.d.ts +7 -0
- package/dist/rpc.d.ts.map +1 -1
- package/dist/transaction-amount-utils.d.ts +88 -0
- package/dist/transaction-amount-utils.d.ts.map +1 -0
- package/dist/transaction-amount-utils.js +94 -0
- package/dist/transaction-amount-utils.js.map +1 -0
- package/dist/transaction-approval.d.ts +104 -1
- package/dist/transaction-approval.d.ts.map +1 -1
- package/dist/vault.d.ts +24 -12
- package/dist/vault.d.ts.map +1 -1
- package/dist/widget.types.d.ts +19 -2
- package/dist/widget.types.d.ts.map +1 -1
- package/dist/widget.types.js.map +1 -1
- package/dist/xrpl-trustline.d.ts +31 -0
- package/dist/xrpl-trustline.d.ts.map +1 -0
- package/dist/xrpl-trustline.js +31 -0
- package/dist/xrpl-trustline.js.map +1 -0
- package/package.json +16 -10
- package/src/hedera-transfer-legs.spec.ts +392 -0
- package/src/hedera-transfer-legs.ts +164 -0
- package/src/index.ts +28 -0
- package/src/ledger.spec.ts +44 -1
- package/src/ledger.ts +25 -0
- package/src/multisig-account.ts +25 -1
- package/src/transaction-amount-utils.spec.ts +184 -0
- package/src/transaction-amount-utils.ts +124 -0
- package/src/transaction-approval.ts +103 -0
- package/src/widget.types.ts +20 -2
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HSuite Native Connect
|
|
3
|
+
* Copyright 2024-2025 HSuite (https://hsuite.finance)
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect } from 'vitest';
|
|
12
|
+
import {
|
|
13
|
+
DEFAULT_FEE_LIKE_MAX_SHARE,
|
|
14
|
+
classifyHbarLegs,
|
|
15
|
+
isAtomicHbarTransfer,
|
|
16
|
+
type HbarLeg,
|
|
17
|
+
} from './hedera-transfer-legs.js';
|
|
18
|
+
import { extractUserAmountForHedera } from './transaction-amount-utils.js';
|
|
19
|
+
|
|
20
|
+
const SIGNER = '0.0.100';
|
|
21
|
+
const RECIPIENT = '0.0.200';
|
|
22
|
+
const SECOND_RECIPIENT = '0.0.300';
|
|
23
|
+
const OTHER_PAYER = '0.0.400';
|
|
24
|
+
const APP_FEE_WALLET = '0.0.777';
|
|
25
|
+
const NODE = '0.0.3';
|
|
26
|
+
const FEE_COLLECTOR = '0.0.98';
|
|
27
|
+
|
|
28
|
+
const leg = (accountId: string, amount: number): HbarLeg => ({ accountId, amount });
|
|
29
|
+
const ids = (legs: HbarLeg[]): string[] => legs.map((l) => l.accountId);
|
|
30
|
+
|
|
31
|
+
describe('classifyHbarLegs', () => {
|
|
32
|
+
it('simple A -> B: one recipient, nothing fee-like, not atomic', () => {
|
|
33
|
+
const entries = [leg(SIGNER, -500_000_000), leg(RECIPIENT, 500_000_000)];
|
|
34
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
35
|
+
expect(legs.signerNetTinybars).toBe(-500_000_000);
|
|
36
|
+
expect(legs.recipients).toEqual([leg(RECIPIENT, 500_000_000)]);
|
|
37
|
+
expect(legs.feeLike).toEqual([]);
|
|
38
|
+
expect(legs.otherDebits).toEqual([]);
|
|
39
|
+
expect(isAtomicHbarTransfer(legs)).toBe(false);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('A -> B plus one app fee wallet: the fee leg is fee-like and the net stays gross', () => {
|
|
43
|
+
const entries = [
|
|
44
|
+
leg(SIGNER, -505_000_000),
|
|
45
|
+
leg(RECIPIENT, 500_000_000),
|
|
46
|
+
leg(APP_FEE_WALLET, 5_000_000),
|
|
47
|
+
];
|
|
48
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
49
|
+
// The headline is what leaves the signer's account, fee leg included.
|
|
50
|
+
expect(legs.signerNetTinybars).toBe(-505_000_000);
|
|
51
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
52
|
+
expect(ids(legs.feeLike)).toEqual([APP_FEE_WALLET]);
|
|
53
|
+
expect(legs.otherDebits).toEqual([]);
|
|
54
|
+
expect(isAtomicHbarTransfer(legs)).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('A -> B plus node and fee collector: system credits are fee-like by membership', () => {
|
|
58
|
+
const entries = [
|
|
59
|
+
leg(SIGNER, -501_000_000),
|
|
60
|
+
leg(RECIPIENT, 500_000_000),
|
|
61
|
+
leg(NODE, 500_000),
|
|
62
|
+
leg(FEE_COLLECTOR, 500_000),
|
|
63
|
+
];
|
|
64
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
65
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
66
|
+
expect(ids(legs.feeLike)).toEqual([NODE, FEE_COLLECTOR]);
|
|
67
|
+
expect(isAtomicHbarTransfer(legs)).toBe(false);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('a system credit larger than the primary recipient is still fee-like', () => {
|
|
71
|
+
const entries = [
|
|
72
|
+
leg(SIGNER, -1_400_000_000),
|
|
73
|
+
leg(RECIPIENT, 500_000_000),
|
|
74
|
+
leg(FEE_COLLECTOR, 900_000_000),
|
|
75
|
+
];
|
|
76
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
77
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
78
|
+
expect(ids(legs.feeLike)).toEqual([FEE_COLLECTOR]);
|
|
79
|
+
expect(isAtomicHbarTransfer(legs)).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('genuine two-recipient transfer: both are recipients, ordered by amount, atomic', () => {
|
|
83
|
+
const entries = [
|
|
84
|
+
leg(SIGNER, -500_000_000),
|
|
85
|
+
leg(SECOND_RECIPIENT, 200_000_000),
|
|
86
|
+
leg(RECIPIENT, 300_000_000),
|
|
87
|
+
];
|
|
88
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
89
|
+
expect(legs.signerNetTinybars).toBe(-500_000_000);
|
|
90
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT, SECOND_RECIPIENT]);
|
|
91
|
+
expect(legs.feeLike).toEqual([]);
|
|
92
|
+
expect(isAtomicHbarTransfer(legs)).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('the signer is not the payer', () => {
|
|
96
|
+
it('signer absent from the body: no net, the payer is an other debit, atomic', () => {
|
|
97
|
+
const entries = [leg(OTHER_PAYER, -500_000_000), leg(RECIPIENT, 500_000_000)];
|
|
98
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
99
|
+
expect(legs.signerNetTinybars).toBeUndefined();
|
|
100
|
+
expect(ids(legs.otherDebits)).toEqual([OTHER_PAYER]);
|
|
101
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
102
|
+
expect(isAtomicHbarTransfer(legs)).toBe(true);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('signer only pays an app fee: net is that fee, the payer is an other debit, atomic', () => {
|
|
106
|
+
const entries = [
|
|
107
|
+
leg(OTHER_PAYER, -500_000_000),
|
|
108
|
+
leg(RECIPIENT, 500_000_000),
|
|
109
|
+
leg(SIGNER, -5_000_000),
|
|
110
|
+
leg(APP_FEE_WALLET, 5_000_000),
|
|
111
|
+
];
|
|
112
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
113
|
+
expect(legs.signerNetTinybars).toBe(-5_000_000);
|
|
114
|
+
expect(ids(legs.otherDebits)).toEqual([OTHER_PAYER]);
|
|
115
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
116
|
+
expect(ids(legs.feeLike)).toEqual([APP_FEE_WALLET]);
|
|
117
|
+
expect(isAtomicHbarTransfer(legs)).toBe(true);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("the signer's own credit is never a recipient", () => {
|
|
121
|
+
const entries = [leg(OTHER_PAYER, -500_000_000), leg(SIGNER, 500_000_000)];
|
|
122
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
123
|
+
expect(legs.signerNetTinybars).toBe(500_000_000);
|
|
124
|
+
expect(legs.recipients).toEqual([]);
|
|
125
|
+
expect(ids(legs.otherDebits)).toEqual([OTHER_PAYER]);
|
|
126
|
+
// One payer paying the signer is a plain incoming payment, not a
|
|
127
|
+
// multi-party body: the drawer must not label it atomic.
|
|
128
|
+
expect(isAtomicHbarTransfer(legs)).toBe(false);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('a payment into the signer sizes its fee legs against the signer credit', () => {
|
|
132
|
+
// The marketplace payout shape. Without the signer's own credit in the
|
|
133
|
+
// reference the 0.05 ℏ fee wallet would be its own primary and get
|
|
134
|
+
// promoted to recipient, and the hero would name it.
|
|
135
|
+
const entries = [
|
|
136
|
+
leg(OTHER_PAYER, -505_000_000),
|
|
137
|
+
leg(SIGNER, 500_000_000),
|
|
138
|
+
leg(APP_FEE_WALLET, 5_000_000),
|
|
139
|
+
];
|
|
140
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
141
|
+
expect(legs.signerNetTinybars).toBe(500_000_000);
|
|
142
|
+
expect(legs.recipients).toEqual([]);
|
|
143
|
+
expect(ids(legs.feeLike)).toEqual([APP_FEE_WALLET]);
|
|
144
|
+
expect(ids(legs.otherDebits)).toEqual([OTHER_PAYER]);
|
|
145
|
+
expect(isAtomicHbarTransfer(legs)).toBe(false);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('a payment split between the signer and a third party keeps that party a recipient', () => {
|
|
149
|
+
const entries = [
|
|
150
|
+
leg(OTHER_PAYER, -500_000_000),
|
|
151
|
+
leg(SIGNER, 300_000_000),
|
|
152
|
+
leg(RECIPIENT, 200_000_000),
|
|
153
|
+
];
|
|
154
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
155
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
156
|
+
expect(isAtomicHbarTransfer(legs)).toBe(true);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('two payers into the signer stay atomic', () => {
|
|
160
|
+
const entries = [
|
|
161
|
+
leg(OTHER_PAYER, -300_000_000),
|
|
162
|
+
leg('0.0.401', -200_000_000),
|
|
163
|
+
leg(SIGNER, 500_000_000),
|
|
164
|
+
];
|
|
165
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
166
|
+
expect(legs.recipients).toEqual([]);
|
|
167
|
+
expect(isAtomicHbarTransfer(legs)).toBe(true);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('fee-like threshold and options', () => {
|
|
172
|
+
it('exposes the default share', () => {
|
|
173
|
+
expect(DEFAULT_FEE_LIKE_MAX_SHARE).toBe(0.1);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('a credit at exactly the share of the primary is fee-like', () => {
|
|
177
|
+
const entries = [
|
|
178
|
+
leg(SIGNER, -550_000_000),
|
|
179
|
+
leg(RECIPIENT, 500_000_000),
|
|
180
|
+
leg(SECOND_RECIPIENT, 50_000_000),
|
|
181
|
+
];
|
|
182
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
183
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
184
|
+
expect(ids(legs.feeLike)).toEqual([SECOND_RECIPIENT]);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('a credit just above the share is a recipient', () => {
|
|
188
|
+
const entries = [
|
|
189
|
+
leg(SIGNER, -550_050_000),
|
|
190
|
+
leg(RECIPIENT, 500_000_000),
|
|
191
|
+
leg(SECOND_RECIPIENT, 50_050_000),
|
|
192
|
+
];
|
|
193
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
194
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT, SECOND_RECIPIENT]);
|
|
195
|
+
expect(legs.feeLike).toEqual([]);
|
|
196
|
+
expect(isAtomicHbarTransfer(legs)).toBe(true);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('equal credits are both recipients (ties never fold into fees)', () => {
|
|
200
|
+
const entries = [
|
|
201
|
+
leg(SIGNER, -500_000_000),
|
|
202
|
+
leg(RECIPIENT, 250_000_000),
|
|
203
|
+
leg(SECOND_RECIPIENT, 250_000_000),
|
|
204
|
+
];
|
|
205
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
206
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT, SECOND_RECIPIENT]);
|
|
207
|
+
expect(legs.feeLike).toEqual([]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('feeLikeMaxShare 0 leaves only system accounts fee-like', () => {
|
|
211
|
+
const entries = [
|
|
212
|
+
leg(SIGNER, -505_500_000),
|
|
213
|
+
leg(RECIPIENT, 500_000_000),
|
|
214
|
+
leg(APP_FEE_WALLET, 5_000_000),
|
|
215
|
+
leg(FEE_COLLECTOR, 500_000),
|
|
216
|
+
];
|
|
217
|
+
const legs = classifyHbarLegs(entries, SIGNER, { feeLikeMaxShare: 0 });
|
|
218
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT, APP_FEE_WALLET]);
|
|
219
|
+
expect(ids(legs.feeLike)).toEqual([FEE_COLLECTOR]);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('a custom system set replaces HEDERA_SYSTEM_ACCOUNTS', () => {
|
|
223
|
+
const entries = [
|
|
224
|
+
leg(SIGNER, -1_000_000_000),
|
|
225
|
+
leg(APP_FEE_WALLET, 600_000_000),
|
|
226
|
+
leg(FEE_COLLECTOR, 400_000_000),
|
|
227
|
+
];
|
|
228
|
+
const legs = classifyHbarLegs(entries, SIGNER, {
|
|
229
|
+
systemAccounts: new Set([APP_FEE_WALLET]),
|
|
230
|
+
});
|
|
231
|
+
// The custom set wins even when its member is the largest credit, and
|
|
232
|
+
// 0.0.98 is no longer special: at 100% of the remaining primary it is a
|
|
233
|
+
// recipient.
|
|
234
|
+
expect(ids(legs.feeLike)).toEqual([APP_FEE_WALLET]);
|
|
235
|
+
expect(ids(legs.recipients)).toEqual([FEE_COLLECTOR]);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('ignores zero-amount and non-finite legs', () => {
|
|
239
|
+
const entries = [
|
|
240
|
+
leg(SIGNER, -500_000_000),
|
|
241
|
+
leg(RECIPIENT, 500_000_000),
|
|
242
|
+
leg('0.0.999', 0),
|
|
243
|
+
leg('0.0.998', Number.NaN),
|
|
244
|
+
leg('0.0.997', Number.POSITIVE_INFINITY),
|
|
245
|
+
leg('0.0.996', Number.NEGATIVE_INFINITY),
|
|
246
|
+
];
|
|
247
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
248
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
249
|
+
expect(legs.feeLike).toEqual([]);
|
|
250
|
+
expect(legs.otherDebits).toEqual([]);
|
|
251
|
+
expect(legs.signerNetTinybars).toBe(-500_000_000);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('small legs stay fee-like while their total is within the share', () => {
|
|
255
|
+
const entries = [
|
|
256
|
+
leg(SIGNER, -550_000_000),
|
|
257
|
+
leg(RECIPIENT, 500_000_000),
|
|
258
|
+
leg('0.0.9001', 30_000_000),
|
|
259
|
+
leg('0.0.9002', 20_000_000),
|
|
260
|
+
];
|
|
261
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
262
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
263
|
+
expect(ids(legs.feeLike)).toEqual(['0.0.9001', '0.0.9002']);
|
|
264
|
+
expect(isAtomicHbarTransfer(legs)).toBe(false);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('a second recipient split into small legs is not fee-like (the share caps the total)', () => {
|
|
268
|
+
// Ten legs of 9.96% each would pass one by one; together they are half
|
|
269
|
+
// the payment, and the drawer must say so.
|
|
270
|
+
const splits = Array.from({ length: 10 }, (_, i) => leg(`0.0.${9001 + i}`, 4_990_000_000));
|
|
271
|
+
const entries = [leg(SIGNER, -100_000_000_000), leg(RECIPIENT, 50_100_000_000), ...splits];
|
|
272
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
273
|
+
expect(legs.feeLike).toEqual([]);
|
|
274
|
+
expect(legs.recipients).toHaveLength(11);
|
|
275
|
+
expect(legs.recipients[0].accountId).toBe(RECIPIENT);
|
|
276
|
+
expect(isAtomicHbarTransfer(legs)).toBe(true);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('legs to the same account are judged on their total', () => {
|
|
280
|
+
const entries = [
|
|
281
|
+
leg(SIGNER, -100_000_000_000),
|
|
282
|
+
leg(RECIPIENT, 50_100_000_000),
|
|
283
|
+
leg('0.0.9001', 4_900_000_000),
|
|
284
|
+
leg('0.0.9001', 4_900_000_000),
|
|
285
|
+
leg('0.0.9001', 4_900_000_000),
|
|
286
|
+
];
|
|
287
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
288
|
+
expect(legs.feeLike).toEqual([]);
|
|
289
|
+
expect(legs.recipients).toEqual([
|
|
290
|
+
leg(RECIPIENT, 50_100_000_000),
|
|
291
|
+
leg('0.0.9001', 14_700_000_000),
|
|
292
|
+
]);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('a signer named twice nets across both legs', () => {
|
|
296
|
+
const entries = [
|
|
297
|
+
leg(SIGNER, -500_000_000),
|
|
298
|
+
leg(RECIPIENT, 505_000_000),
|
|
299
|
+
leg(SIGNER, -5_000_000),
|
|
300
|
+
];
|
|
301
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
302
|
+
expect(legs.signerNetTinybars).toBe(-505_000_000);
|
|
303
|
+
expect(legs.otherDebits).toEqual([]);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('a signer whose legs cancel out nets to zero, not to absent', () => {
|
|
307
|
+
const entries = [
|
|
308
|
+
leg(SIGNER, -500_000_000),
|
|
309
|
+
leg(SIGNER, 500_000_000),
|
|
310
|
+
leg(OTHER_PAYER, -300_000_000),
|
|
311
|
+
leg(RECIPIENT, 300_000_000),
|
|
312
|
+
];
|
|
313
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
314
|
+
expect(legs.signerNetTinybars).toBe(0);
|
|
315
|
+
expect(ids(legs.otherDebits)).toEqual([OTHER_PAYER]);
|
|
316
|
+
expect(ids(legs.recipients)).toEqual([RECIPIENT]);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it('empty or undefined entries classify to nothing', () => {
|
|
320
|
+
for (const entries of [[], undefined]) {
|
|
321
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
322
|
+
expect(legs.signerNetTinybars).toBeUndefined();
|
|
323
|
+
expect(legs.recipients).toEqual([]);
|
|
324
|
+
expect(legs.feeLike).toEqual([]);
|
|
325
|
+
expect(legs.otherDebits).toEqual([]);
|
|
326
|
+
expect(isAtomicHbarTransfer(legs)).toBe(false);
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
describe('same derivation as the activity list', () => {
|
|
332
|
+
const fixtures: Array<{ name: string; entries: HbarLeg[] }> = [
|
|
333
|
+
{ name: 'simple', entries: [leg(SIGNER, -500_000_000), leg(RECIPIENT, 500_000_000)] },
|
|
334
|
+
{
|
|
335
|
+
name: 'app fee',
|
|
336
|
+
entries: [
|
|
337
|
+
leg(SIGNER, -505_000_000),
|
|
338
|
+
leg(RECIPIENT, 500_000_000),
|
|
339
|
+
leg(APP_FEE_WALLET, 5_000_000),
|
|
340
|
+
],
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
name: 'two recipients',
|
|
344
|
+
entries: [
|
|
345
|
+
leg(SIGNER, -500_000_000),
|
|
346
|
+
leg(RECIPIENT, 300_000_000),
|
|
347
|
+
leg(SECOND_RECIPIENT, 200_000_000),
|
|
348
|
+
],
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
name: 'signer absent',
|
|
352
|
+
entries: [leg(OTHER_PAYER, -500_000_000), leg(RECIPIENT, 500_000_000)],
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
name: 'signer receives',
|
|
356
|
+
entries: [leg(OTHER_PAYER, -500_000_000), leg(SIGNER, 500_000_000)],
|
|
357
|
+
},
|
|
358
|
+
];
|
|
359
|
+
|
|
360
|
+
it.each(fixtures)(
|
|
361
|
+
'$name: signerNetTinybars equals extractUserAmountForHedera',
|
|
362
|
+
({ entries }) => {
|
|
363
|
+
const legs = classifyHbarLegs(entries, SIGNER);
|
|
364
|
+
const transfers = entries.map((e) => ({ account: e.accountId, amount: e.amount }));
|
|
365
|
+
expect(legs.signerNetTinybars).toBe(extractUserAmountForHedera({ transfers }, SIGNER));
|
|
366
|
+
},
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
it('the pre-signature body and the post-hoc mirror record agree on the gross figure', () => {
|
|
370
|
+
const body = [
|
|
371
|
+
leg(SIGNER, -505_000_000),
|
|
372
|
+
leg(RECIPIENT, 500_000_000),
|
|
373
|
+
leg(APP_FEE_WALLET, 5_000_000),
|
|
374
|
+
];
|
|
375
|
+
const legs = classifyHbarLegs(body, SIGNER);
|
|
376
|
+
|
|
377
|
+
// After submission the mirror node nets the network fee into the
|
|
378
|
+
// signer's row and adds the node / collector legs.
|
|
379
|
+
const mirror = {
|
|
380
|
+
transfers: [
|
|
381
|
+
{ account: SIGNER, amount: -505_500_000 },
|
|
382
|
+
{ account: RECIPIENT, amount: 500_000_000 },
|
|
383
|
+
{ account: APP_FEE_WALLET, amount: 5_000_000 },
|
|
384
|
+
{ account: NODE, amount: 100_000 },
|
|
385
|
+
{ account: FEE_COLLECTOR, amount: 400_000 },
|
|
386
|
+
],
|
|
387
|
+
charged_tx_fee: 500_000,
|
|
388
|
+
};
|
|
389
|
+
expect(extractUserAmountForHedera(mirror, SIGNER)).toBe(legs.signerNetTinybars);
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
});
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HSuite Native Connect
|
|
3
|
+
* Copyright 2024-2025 HSuite (https://hsuite.finance)
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { HEDERA_SYSTEM_ACCOUNTS } from './hedera-system-accounts.js';
|
|
12
|
+
import { extractUserAmountForHedera } from './transaction-amount-utils.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* One HBAR leg of a transfer body: the `transferSummary.entries` shape the
|
|
16
|
+
* wallet parser emits. Signed tinybars, negative = debit.
|
|
17
|
+
*/
|
|
18
|
+
export interface HbarLeg {
|
|
19
|
+
accountId: string;
|
|
20
|
+
amount: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A transfer body's HBAR legs sorted by their role relative to the signer.
|
|
25
|
+
*
|
|
26
|
+
* The approval drawer needs to show what leaves the signer's account as one
|
|
27
|
+
* figure and only then describe the rest. A raw credit/debit count cannot do
|
|
28
|
+
* that: any app fee wallet or routing account is a second credit, and the
|
|
29
|
+
* headline would vanish behind an "atomic transfer" label.
|
|
30
|
+
*/
|
|
31
|
+
export interface ClassifiedHbarLegs {
|
|
32
|
+
/**
|
|
33
|
+
* The signer's net body delta in tinybars, via `extractUserAmountForHedera`
|
|
34
|
+
* (negative = leaving). `undefined` when the signer has no leg at all.
|
|
35
|
+
*/
|
|
36
|
+
signerNetTinybars: number | undefined;
|
|
37
|
+
/** Intended counterparties, largest credit first. */
|
|
38
|
+
recipients: HbarLeg[];
|
|
39
|
+
/** System accounts plus minor non-system credits (app fee wallets, routing). */
|
|
40
|
+
feeLike: HbarLeg[];
|
|
41
|
+
/** Debits by accounts other than the signer. */
|
|
42
|
+
otherDebits: HbarLeg[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Tuning knobs for {@link classifyHbarLegs}; both default to the shared rule. */
|
|
46
|
+
export interface ClassifyHbarLegsOptions {
|
|
47
|
+
/** Accounts that are fee-like whatever their amount. Defaults to HEDERA_SYSTEM_ACCOUNTS. */
|
|
48
|
+
systemAccounts?: ReadonlySet<string>;
|
|
49
|
+
/**
|
|
50
|
+
* A non-system credit at or below this share of the largest non-system
|
|
51
|
+
* credit is fee-like. Defaults to DEFAULT_FEE_LIKE_MAX_SHARE.
|
|
52
|
+
*/
|
|
53
|
+
feeLikeMaxShare?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 10%: an app fee wallet taking a few percent of the payment folds under the
|
|
58
|
+
* hero, a co-recipient taking a real share stays a recipient. Ties are
|
|
59
|
+
* recipients. The share bounds the minor credits together as well as one by
|
|
60
|
+
* one, so a recipient split into many small legs is still a recipient.
|
|
61
|
+
*/
|
|
62
|
+
export const DEFAULT_FEE_LIKE_MAX_SHARE = 0.1;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Classify the HBAR legs of a transfer body relative to `signerAddress`.
|
|
66
|
+
*
|
|
67
|
+
* Structural on purpose: the system set catches Hedera's own node and
|
|
68
|
+
* collector accounts, but an application fee wallet is just another account.
|
|
69
|
+
* Comparing every credit to the largest non-system credit is what folds
|
|
70
|
+
* those legs under the hero without an allowlist nobody could maintain.
|
|
71
|
+
*
|
|
72
|
+
* @param entries the body's HBAR legs (`transferSummary.entries`)
|
|
73
|
+
* @param signerAddress the wallet account asked to sign (e.g. `0.0.12345`)
|
|
74
|
+
* @param options system set and fee-like share overrides
|
|
75
|
+
* @returns the legs grouped by role, plus the signer's net delta
|
|
76
|
+
*/
|
|
77
|
+
export function classifyHbarLegs(
|
|
78
|
+
entries: readonly HbarLeg[] | undefined,
|
|
79
|
+
signerAddress: string,
|
|
80
|
+
options: ClassifyHbarLegsOptions = {},
|
|
81
|
+
): ClassifiedHbarLegs {
|
|
82
|
+
const systemAccounts = options.systemAccounts ?? HEDERA_SYSTEM_ACCOUNTS;
|
|
83
|
+
const feeLikeMaxShare = options.feeLikeMaxShare ?? DEFAULT_FEE_LIKE_MAX_SHARE;
|
|
84
|
+
|
|
85
|
+
// Zero and non-finite legs move nothing and would only distort the
|
|
86
|
+
// primary-credit reference below.
|
|
87
|
+
const finite = (entries ?? []).filter((leg) => Number.isFinite(leg.amount) && leg.amount !== 0);
|
|
88
|
+
|
|
89
|
+
// The same helper the activity list applies to the mirror record after
|
|
90
|
+
// submission. A body carries no charged_tx_fee yet, so this reduces to the
|
|
91
|
+
// signer's gross debit, which is exactly the figure the post-hoc derivation
|
|
92
|
+
// recovers by adding the network fee back. Read from the raw legs so a
|
|
93
|
+
// signer whose legs cancel out reports 0 (a participant), not absent.
|
|
94
|
+
const signerNetTinybars = extractUserAmountForHedera(
|
|
95
|
+
{ transfers: finite.map((leg) => ({ account: leg.accountId, amount: leg.amount })) },
|
|
96
|
+
signerAddress,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// One leg per account, first-seen order. A body that names an account
|
|
100
|
+
// several times is judged on that account's total, never on one piece.
|
|
101
|
+
const legs = mergeByAccount(finite);
|
|
102
|
+
|
|
103
|
+
const otherDebits = legs.filter((leg) => leg.amount < 0 && leg.accountId !== signerAddress);
|
|
104
|
+
const credits = legs.filter((leg) => leg.amount > 0 && leg.accountId !== signerAddress);
|
|
105
|
+
|
|
106
|
+
// The reference includes the signer's own credit. On a body that pays INTO
|
|
107
|
+
// the signer the counterparty credits are all fee legs, and measuring them
|
|
108
|
+
// against each other would crown the largest fee wallet the "recipient".
|
|
109
|
+
const primary = legs.reduce(
|
|
110
|
+
(max, leg) =>
|
|
111
|
+
leg.amount > 0 && !systemAccounts.has(leg.accountId) ? Math.max(max, leg.amount) : max,
|
|
112
|
+
0,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
// `primary` is positive whenever a non-system credit reaches the division.
|
|
116
|
+
const minor = credits.filter(
|
|
117
|
+
(leg) => !systemAccounts.has(leg.accountId) && leg.amount / primary <= feeLikeMaxShare,
|
|
118
|
+
);
|
|
119
|
+
// The share caps the minor credits together, not only one by one: a
|
|
120
|
+
// second recipient split into ten small legs is still a second recipient
|
|
121
|
+
// and must not vanish behind the fee group.
|
|
122
|
+
const minorTotal = minor.reduce((sum, leg) => sum + leg.amount, 0);
|
|
123
|
+
const minorIsFeeLike = minor.length > 0 && minorTotal / primary <= feeLikeMaxShare;
|
|
124
|
+
|
|
125
|
+
const feeLike = credits.filter(
|
|
126
|
+
(leg) => systemAccounts.has(leg.accountId) || (minorIsFeeLike && minor.includes(leg)),
|
|
127
|
+
);
|
|
128
|
+
const recipients = credits
|
|
129
|
+
.filter((leg) => !feeLike.includes(leg))
|
|
130
|
+
.sort((a, b) => b.amount - a.amount);
|
|
131
|
+
|
|
132
|
+
return { signerNetTinybars, recipients, feeLike, otherDebits };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function mergeByAccount(legs: readonly HbarLeg[]): HbarLeg[] {
|
|
136
|
+
const totals = new Map<string, number>();
|
|
137
|
+
for (const leg of legs) {
|
|
138
|
+
totals.set(leg.accountId, (totals.get(leg.accountId) ?? 0) + leg.amount);
|
|
139
|
+
}
|
|
140
|
+
return [...totals]
|
|
141
|
+
.filter(([, amount]) => amount !== 0)
|
|
142
|
+
.map(([accountId, amount]) => ({ accountId, amount }));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* True when "Sending X to Y" would be a lie: several intended recipients, or
|
|
147
|
+
* a payer other than the signer. One recipient plus any number of fee-like
|
|
148
|
+
* legs is a simple send, and so is a single other payer crediting nobody but
|
|
149
|
+
* the signer.
|
|
150
|
+
*
|
|
151
|
+
* @param legs output of {@link classifyHbarLegs}
|
|
152
|
+
* @returns true when the hero must not name a single recipient
|
|
153
|
+
*/
|
|
154
|
+
export function isAtomicHbarTransfer(legs: ClassifiedHbarLegs): boolean {
|
|
155
|
+
// One payer paying the signer and nobody else is a plain incoming payment.
|
|
156
|
+
// The hero names the signer and tells the whole story, so the atomic line
|
|
157
|
+
// under it would only add noise.
|
|
158
|
+
const paysOnlyTheSigner =
|
|
159
|
+
legs.recipients.length === 0 &&
|
|
160
|
+
legs.otherDebits.length === 1 &&
|
|
161
|
+
(legs.signerNetTinybars ?? 0) > 0;
|
|
162
|
+
if (paysOnlyTheSigner) return false;
|
|
163
|
+
return legs.recipients.length > 1 || legs.otherDebits.length > 0;
|
|
164
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -35,6 +35,7 @@ export {
|
|
|
35
35
|
LedgerAdapterHealthSchema,
|
|
36
36
|
LedgerAdapterDiagnosticsSchema,
|
|
37
37
|
DerivationPathSchema,
|
|
38
|
+
hederaBip39Passphrase,
|
|
38
39
|
} from './ledger.js';
|
|
39
40
|
// Type-only re-exports — declared with `export type` so they vanish
|
|
40
41
|
// from dist/index.js (and from dist/index.d.ts they stay as `export
|
|
@@ -204,6 +205,33 @@ export {
|
|
|
204
205
|
HEDERA_SYSTEM_ACCOUNTS,
|
|
205
206
|
} from './hedera-system-accounts.js';
|
|
206
207
|
|
|
208
|
+
// === User-perspective transaction amounts ===
|
|
209
|
+
// Canonical headline-amount derivation (signer's gross transfer, fee added
|
|
210
|
+
// back). The wallet's activity list and account overview call it after
|
|
211
|
+
// submission; the approval drawer in packages/ui calls it before signing so
|
|
212
|
+
// both surfaces show the same figure for the same transaction.
|
|
213
|
+
export type { HederaTransferMetadata, XrplAmountTx } from './transaction-amount-utils.js';
|
|
214
|
+
export {
|
|
215
|
+
extractUserAmountForHedera,
|
|
216
|
+
extractUserAmountForXrpl,
|
|
217
|
+
} from './transaction-amount-utils.js';
|
|
218
|
+
|
|
219
|
+
// === Hedera transfer-leg classification ===
|
|
220
|
+
// Sorts a transfer body's HBAR legs by role relative to the signer (own net
|
|
221
|
+
// debit, intended recipients, fee-like legs, other payers). The approval
|
|
222
|
+
// drawer decides hero vs atomic rendering from it; the system set above is
|
|
223
|
+
// one of its inputs, the amount helper above is its arithmetic.
|
|
224
|
+
export type {
|
|
225
|
+
HbarLeg,
|
|
226
|
+
ClassifiedHbarLegs,
|
|
227
|
+
ClassifyHbarLegsOptions,
|
|
228
|
+
} from './hedera-transfer-legs.js';
|
|
229
|
+
export {
|
|
230
|
+
DEFAULT_FEE_LIKE_MAX_SHARE,
|
|
231
|
+
classifyHbarLegs,
|
|
232
|
+
isAtomicHbarTransfer,
|
|
233
|
+
} from './hedera-transfer-legs.js';
|
|
234
|
+
|
|
207
235
|
// === XRPL Endpoints ===
|
|
208
236
|
// Canonical, ordered rippled endpoint lists per network (primary first, rest
|
|
209
237
|
// are failover). Imported by the ledger-xrpl adapter (WS clients, account
|
package/src/ledger.spec.ts
CHANGED
|
@@ -3,7 +3,12 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { describe, it, expect } from 'vitest';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
LedgerAccountSchema,
|
|
8
|
+
DerivationPathSchema,
|
|
9
|
+
LedgerPluginManifestSchema,
|
|
10
|
+
hederaBip39Passphrase,
|
|
11
|
+
} from './ledger.js';
|
|
7
12
|
import {
|
|
8
13
|
SessionRequestSchema,
|
|
9
14
|
SessionApprovalSchema,
|
|
@@ -415,3 +420,41 @@ describe('LedgerPluginManifestSchema', () => {
|
|
|
415
420
|
}
|
|
416
421
|
});
|
|
417
422
|
});
|
|
423
|
+
|
|
424
|
+
describe('DerivationPathSchema bip39Passphrase', () => {
|
|
425
|
+
it('keeps the field through a parse (zod would strip an undeclared key)', () => {
|
|
426
|
+
const parsed = DerivationPathSchema.parse({
|
|
427
|
+
purpose: 44,
|
|
428
|
+
coinType: 3030,
|
|
429
|
+
addressIndex: 0,
|
|
430
|
+
bip39Passphrase: 'none',
|
|
431
|
+
});
|
|
432
|
+
expect(parsed.bip39Passphrase).toBe('none');
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it('leaves it absent on paths saved before it existed', () => {
|
|
436
|
+
expect(DerivationPathSchema.parse({ purpose: 44, coinType: 3030 }).bip39Passphrase).toBe(
|
|
437
|
+
undefined,
|
|
438
|
+
);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it('rejects an unknown mode', () => {
|
|
442
|
+
expect(
|
|
443
|
+
DerivationPathSchema.safeParse({ purpose: 44, coinType: 3030, bip39Passphrase: 'x' }).success,
|
|
444
|
+
).toBe(false);
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
describe('hederaBip39Passphrase', () => {
|
|
449
|
+
it("is empty on mainnet and the network id elsewhere (the wallet's own scheme)", () => {
|
|
450
|
+
expect(hederaBip39Passphrase('hedera:mainnet')).toBe('');
|
|
451
|
+
expect(hederaBip39Passphrase('hedera:testnet')).toBe('hedera:testnet');
|
|
452
|
+
expect(hederaBip39Passphrase('hedera:testnet', 'network')).toBe('hedera:testnet');
|
|
453
|
+
expect(hederaBip39Passphrase(undefined)).toBe('');
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it("is empty on every network for 'none' (how other wallets derive)", () => {
|
|
457
|
+
expect(hederaBip39Passphrase('hedera:testnet', 'none')).toBe('');
|
|
458
|
+
expect(hederaBip39Passphrase('hedera:previewnet', 'none')).toBe('');
|
|
459
|
+
});
|
|
460
|
+
});
|
package/src/ledger.ts
CHANGED
|
@@ -247,6 +247,13 @@ export const DerivationPathSchema = z.object({
|
|
|
247
247
|
account: z.number().default(0),
|
|
248
248
|
change: z.number().default(0),
|
|
249
249
|
addressIndex: z.number().default(0),
|
|
250
|
+
/**
|
|
251
|
+
* The BIP-39 passphrase a Hedera key was derived with. Absent (or 'network')
|
|
252
|
+
* is the wallet's own scheme: empty on mainnet, the network id elsewhere.
|
|
253
|
+
* 'none' is an empty passphrase on every network, as other wallets derive,
|
|
254
|
+
* set on accounts found by a recovery-phrase import on a non-mainnet network.
|
|
255
|
+
*/
|
|
256
|
+
bip39Passphrase: z.enum(['network', 'none']).optional(),
|
|
250
257
|
});
|
|
251
258
|
|
|
252
259
|
/**
|
|
@@ -254,6 +261,24 @@ export const DerivationPathSchema = z.object({
|
|
|
254
261
|
*/
|
|
255
262
|
export type DerivationPath = z.infer<typeof DerivationPathSchema>;
|
|
256
263
|
|
|
264
|
+
/**
|
|
265
|
+
* The BIP-39 passphrase a Hedera key is derived with. The wallet's own scheme
|
|
266
|
+
* keeps mainnet on an empty passphrase and every other network on its network
|
|
267
|
+
* id, so testnet keys differ from mainnet ones; other wallets use an empty
|
|
268
|
+
* passphrase everywhere, which `mode: 'none'` reproduces.
|
|
269
|
+
*
|
|
270
|
+
* @param networkId - Network the key belongs to (e.g. 'hedera:testnet')
|
|
271
|
+
* @param mode - The path's bip39Passphrase; absent means 'network'
|
|
272
|
+
* @returns The passphrase string, empty when none applies
|
|
273
|
+
*/
|
|
274
|
+
export function hederaBip39Passphrase(
|
|
275
|
+
networkId: string | undefined,
|
|
276
|
+
mode?: DerivationPath['bip39Passphrase'],
|
|
277
|
+
): string {
|
|
278
|
+
if (mode === 'none' || !networkId || networkId === 'hedera:mainnet') return '';
|
|
279
|
+
return networkId;
|
|
280
|
+
}
|
|
281
|
+
|
|
257
282
|
/**
|
|
258
283
|
* @description Reference to a fungible token in a given ledger/network.
|
|
259
284
|
* The identifier fields are ledger-specific and intentionally flexible to be
|