@keetanetwork/anchor 0.0.78 → 0.0.80
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/lib/anchor-external.d.ts +94 -1
- package/lib/anchor-external.d.ts.map +1 -1
- package/lib/anchor-external.generated.js +52 -3
- package/lib/anchor-external.generated.js.map +1 -1
- package/lib/anchor-external.js +126 -3
- package/lib/anchor-external.js.map +1 -1
- package/lib/anchor-metadata-server.d.ts +15 -2
- package/lib/anchor-metadata-server.d.ts.map +1 -1
- package/lib/anchor-metadata-server.js +54 -0
- package/lib/anchor-metadata-server.js.map +1 -1
- package/lib/anchor-status.d.ts +149 -0
- package/lib/anchor-status.d.ts.map +1 -0
- package/lib/anchor-status.js +93 -0
- package/lib/anchor-status.js.map +1 -0
- package/lib/chaining.d.ts +2 -1
- package/lib/chaining.d.ts.map +1 -1
- package/lib/chaining.js +67 -8
- package/lib/chaining.js.map +1 -1
- package/lib/history.d.ts +405 -0
- package/lib/history.d.ts.map +1 -0
- package/lib/history.js +723 -0
- package/lib/history.js.map +1 -0
- package/lib/http-server/index.d.ts +1 -0
- package/lib/http-server/index.d.ts.map +1 -1
- package/lib/http-server/index.js +1 -0
- package/lib/http-server/index.js.map +1 -1
- package/lib/index.d.ts +6 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/metadata.types.d.ts +3 -0
- package/lib/metadata.types.d.ts.map +1 -1
- package/lib/metadata.types.js.map +1 -1
- package/lib/resolver.d.ts +28 -7
- package/lib/resolver.d.ts.map +1 -1
- package/lib/resolver.generated.js +528 -453
- package/lib/resolver.generated.js.map +1 -1
- package/lib/resolver.js +67 -8
- package/lib/resolver.js.map +1 -1
- package/lib/utils/brand.d.ts +5 -0
- package/lib/utils/brand.d.ts.map +1 -1
- package/lib/utils/brand.js +4 -1
- package/lib/utils/brand.js.map +1 -1
- package/npm-shrinkwrap.json +10 -17
- package/package.json +2 -2
- package/services/asset-movement/client.d.ts +7 -1
- package/services/asset-movement/client.d.ts.map +1 -1
- package/services/asset-movement/client.js +70 -15
- package/services/asset-movement/client.js.map +1 -1
- package/services/asset-movement/common.d.ts +1 -1
- package/services/asset-movement/common.d.ts.map +1 -1
- package/services/asset-movement/common.generated.js +11 -409
- package/services/asset-movement/common.generated.js.map +1 -1
- package/services/asset-movement/common.js.map +1 -1
- package/services/asset-movement/common.server.generated.js +1153 -15478
- package/services/asset-movement/common.server.generated.js.map +1 -1
- package/services/asset-movement/lib/location.d.ts.map +1 -1
- package/services/asset-movement/lib/location.js +1 -0
- package/services/asset-movement/lib/location.js.map +1 -1
- package/services/asset-movement/server.d.ts +1 -1
- package/services/asset-movement/server.d.ts.map +1 -1
- package/services/asset-movement/server.js +1 -1
- package/services/asset-movement/server.js.map +1 -1
- package/services/asset-movement/status-source.d.ts +32 -0
- package/services/asset-movement/status-source.d.ts.map +1 -0
- package/services/asset-movement/status-source.js +77 -0
- package/services/asset-movement/status-source.js.map +1 -0
- package/services/fx/server.d.ts +2 -2
- package/services/fx/server.d.ts.map +1 -1
- package/services/fx/server.js +2 -1
- package/services/fx/server.js.map +1 -1
- package/services/kyc/server.d.ts +1 -1
- package/services/kyc/server.d.ts.map +1 -1
- package/services/kyc/server.js +1 -1
- package/services/kyc/server.js.map +1 -1
- package/services/notification/client.js +50 -6
- package/services/notification/client.js.map +1 -1
- package/services/notification/server.d.ts.map +1 -1
- package/services/notification/server.js +4 -1
- package/services/notification/server.js.map +1 -1
- package/services/storage/client.js +50 -6
- package/services/storage/client.js.map +1 -1
- package/services/storage/clients/contacts.d.ts +5 -0
- package/services/storage/clients/contacts.d.ts.map +1 -1
- package/services/storage/clients/contacts.js +20 -9
- package/services/storage/clients/contacts.js.map +1 -1
- package/services/storage/server.d.ts +1 -1
- package/services/storage/server.d.ts.map +1 -1
- package/services/storage/server.js +2 -2
- package/services/storage/server.js.map +1 -1
- package/services/username/client.js +50 -6
- package/services/username/client.js.map +1 -1
- package/services/username/server.d.ts.map +1 -1
- package/services/username/server.js +4 -1
- package/services/username/server.js.map +1 -1
package/lib/history.js
ADDED
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
import { lib as KeetaNetLib } from '@keetanetwork/keetanet-client';
|
|
2
|
+
import { isCompletedTransferStatus } from './anchor-status.js';
|
|
3
|
+
import { convertAssetSearchInputToCanonical, isChainLocation, toAssetLocationFromString, toAssetPair } from '../services/asset-movement/common.js';
|
|
4
|
+
const OperationType = KeetaNetLib.Block.OperationType;
|
|
5
|
+
/**
|
|
6
|
+
* Parse a value string into smallest-unit units, defaulting to zero on a
|
|
7
|
+
* value that is not a whole-number string.
|
|
8
|
+
*/
|
|
9
|
+
function toUnits(value) {
|
|
10
|
+
try {
|
|
11
|
+
return (BigInt(value));
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return (0n);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Whether a location string denotes a chain (optionally a specific one).
|
|
19
|
+
*/
|
|
20
|
+
function locationIsChain(location, chainType) {
|
|
21
|
+
let parsed;
|
|
22
|
+
try {
|
|
23
|
+
parsed = toAssetLocationFromString(location);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return (false);
|
|
27
|
+
}
|
|
28
|
+
return (isChainLocation(parsed, chainType));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Derive the logical type of an anchor transfer from its endpoints.
|
|
32
|
+
*/
|
|
33
|
+
export function logicalTypeFromTransfer(transfer) {
|
|
34
|
+
const fromKeeta = locationIsChain(transfer.from.location, 'keeta');
|
|
35
|
+
const toKeeta = locationIsChain(transfer.to.location, 'keeta');
|
|
36
|
+
if (fromKeeta && toKeeta) {
|
|
37
|
+
return ('swap');
|
|
38
|
+
}
|
|
39
|
+
const fromChain = locationIsChain(transfer.from.location);
|
|
40
|
+
const toChain = locationIsChain(transfer.to.location);
|
|
41
|
+
if ((fromKeeta && toChain) || (toKeeta && fromChain)) {
|
|
42
|
+
return ('bridge');
|
|
43
|
+
}
|
|
44
|
+
if (fromKeeta) {
|
|
45
|
+
return ('withdraw');
|
|
46
|
+
}
|
|
47
|
+
if (toKeeta) {
|
|
48
|
+
return ('deposit');
|
|
49
|
+
}
|
|
50
|
+
return ('other');
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Map an anchor transfer's raw status onto the standardized status, carrying
|
|
54
|
+
* the raw provider string for the wallet to interpret.
|
|
55
|
+
*/
|
|
56
|
+
export function statusFromTransfer(transfer) {
|
|
57
|
+
if (isCompletedTransferStatus(transfer.status)) {
|
|
58
|
+
return ({ status: 'complete', providerStatus: transfer.status });
|
|
59
|
+
}
|
|
60
|
+
return ({ status: 'pending', providerStatus: transfer.status });
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Derive value flow for an anchor transfer type.
|
|
64
|
+
*/
|
|
65
|
+
function directionFromTransfer(type, fromKeeta) {
|
|
66
|
+
if (type === 'deposit') {
|
|
67
|
+
return ('in');
|
|
68
|
+
}
|
|
69
|
+
if (type === 'withdraw') {
|
|
70
|
+
return ('out');
|
|
71
|
+
}
|
|
72
|
+
if (type === 'bridge') {
|
|
73
|
+
if (fromKeeta) {
|
|
74
|
+
return ('out');
|
|
75
|
+
}
|
|
76
|
+
return ('in');
|
|
77
|
+
}
|
|
78
|
+
return ('self');
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Collect the chain transaction ids an anchor transfer references.
|
|
82
|
+
*/
|
|
83
|
+
function collectAnchorSubTransactions(transfer) {
|
|
84
|
+
const ids = [];
|
|
85
|
+
const groups = [transfer.from.transactions, transfer.to.transactions];
|
|
86
|
+
for (const group of groups) {
|
|
87
|
+
for (const value of Object.values(group)) {
|
|
88
|
+
if (value !== null && value !== undefined) {
|
|
89
|
+
ids.push(value.id);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return (ids);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Reduce a block operation to its logical view, or `null` for non-transfer
|
|
97
|
+
* operations.
|
|
98
|
+
*/
|
|
99
|
+
function operationView(operation) {
|
|
100
|
+
if (operation.type === OperationType.SEND) {
|
|
101
|
+
return ({
|
|
102
|
+
opType: 'send',
|
|
103
|
+
counterparty: operation.to.publicKeyString.get(),
|
|
104
|
+
amount: { token: operation.token.publicKeyString.get(), amount: operation.amount }
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (operation.type === OperationType.RECEIVE) {
|
|
108
|
+
return ({
|
|
109
|
+
opType: 'receive',
|
|
110
|
+
counterparty: operation.from.publicKeyString.get(),
|
|
111
|
+
amount: { token: operation.token.publicKeyString.get(), amount: operation.amount }
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return (null);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Reduce an operation to its logical view relative to the block's
|
|
118
|
+
* perspective account.
|
|
119
|
+
*/
|
|
120
|
+
function viewOf(block, operation) {
|
|
121
|
+
const native = operationView(operation);
|
|
122
|
+
if (native === null) {
|
|
123
|
+
return (null);
|
|
124
|
+
}
|
|
125
|
+
if (block.perspective === undefined || block.perspective === block.account) {
|
|
126
|
+
return (native);
|
|
127
|
+
}
|
|
128
|
+
if (native.opType === 'send' && native.counterparty === block.perspective) {
|
|
129
|
+
return ({ opType: 'receive', counterparty: block.account, amount: native.amount });
|
|
130
|
+
}
|
|
131
|
+
return (null);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The account on the other side of a transfer operation, if any.
|
|
135
|
+
*/
|
|
136
|
+
function counterpartyID(operation) {
|
|
137
|
+
if (operation.type === OperationType.SEND) {
|
|
138
|
+
return (operation.to.publicKeyString.get());
|
|
139
|
+
}
|
|
140
|
+
if (operation.type === OperationType.RECEIVE) {
|
|
141
|
+
return (operation.from.publicKeyString.get());
|
|
142
|
+
}
|
|
143
|
+
return (undefined);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Partition a block's operations into send/receive views.
|
|
147
|
+
*/
|
|
148
|
+
function viewsOf(block) {
|
|
149
|
+
const sends = [];
|
|
150
|
+
const receives = [];
|
|
151
|
+
let otherCount = 0;
|
|
152
|
+
for (const enriched of block.operations) {
|
|
153
|
+
const view = viewOf(block, enriched.operation);
|
|
154
|
+
if (view === null) {
|
|
155
|
+
otherCount++;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const indexed = { ...view, index: enriched.index };
|
|
159
|
+
if (indexed.opType === 'send') {
|
|
160
|
+
sends.push(indexed);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
receives.push(indexed);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return ({ sends, receives, otherCount });
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The shared counterparty across views, or `undefined` when they differ.
|
|
170
|
+
*/
|
|
171
|
+
function uniformCounterparty(views) {
|
|
172
|
+
const first = views[0];
|
|
173
|
+
if (first === undefined) {
|
|
174
|
+
return (undefined);
|
|
175
|
+
}
|
|
176
|
+
for (const view of views) {
|
|
177
|
+
if (view.counterparty !== first.counterparty) {
|
|
178
|
+
return (undefined);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return (first.counterparty);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Build on-chain legs for every transfer operation in a block.
|
|
185
|
+
*/
|
|
186
|
+
function onchainLegs(block) {
|
|
187
|
+
const legs = [];
|
|
188
|
+
for (const enriched of block.operations) {
|
|
189
|
+
const view = viewOf(block, enriched.operation);
|
|
190
|
+
if (view === null) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
legs.push({
|
|
194
|
+
kind: 'onchain',
|
|
195
|
+
blockHash: block.blockHash,
|
|
196
|
+
operationIndex: enriched.index,
|
|
197
|
+
opType: view.opType,
|
|
198
|
+
counterparty: view.counterparty,
|
|
199
|
+
amount: view.amount
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return (legs);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Collect raw `external` strings carried by a block's SEND operations.
|
|
206
|
+
*/
|
|
207
|
+
function blockExternals(block) {
|
|
208
|
+
const externals = [];
|
|
209
|
+
for (const enriched of block.operations) {
|
|
210
|
+
const operation = enriched.operation;
|
|
211
|
+
if (operation.type === OperationType.SEND && operation.external !== undefined) {
|
|
212
|
+
externals.push(operation.external);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return (externals);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Build the refs block for a logical transaction.
|
|
219
|
+
*/
|
|
220
|
+
function buildRefs(block, anchorTxIDs) {
|
|
221
|
+
const refs = { blockHashes: [block.blockHash], anchorTxIDs };
|
|
222
|
+
const externals = blockExternals(block);
|
|
223
|
+
if (externals.length > 0) {
|
|
224
|
+
refs.external = externals;
|
|
225
|
+
}
|
|
226
|
+
return (refs);
|
|
227
|
+
}
|
|
228
|
+
// #endregion Operation views
|
|
229
|
+
// #region Classifiers
|
|
230
|
+
/**
|
|
231
|
+
* Build the logical transaction backing one enriched anchor operation.
|
|
232
|
+
*/
|
|
233
|
+
function buildAnchorTransaction(block, enriched, transfer) {
|
|
234
|
+
const type = logicalTypeFromTransfer(transfer);
|
|
235
|
+
const fromKeeta = locationIsChain(transfer.from.location, 'keeta');
|
|
236
|
+
const direction = directionFromTransfer(type, fromKeeta);
|
|
237
|
+
const settled = statusFromTransfer(transfer);
|
|
238
|
+
const assets = toAssetPair(transfer.asset);
|
|
239
|
+
const view = viewOf(block, enriched.operation);
|
|
240
|
+
const legs = [];
|
|
241
|
+
if (view !== null) {
|
|
242
|
+
legs.push({
|
|
243
|
+
kind: 'onchain',
|
|
244
|
+
blockHash: block.blockHash,
|
|
245
|
+
operationIndex: enriched.index,
|
|
246
|
+
opType: view.opType,
|
|
247
|
+
counterparty: view.counterparty,
|
|
248
|
+
amount: view.amount
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
const anchorID = enriched.anchorID ?? view?.counterparty ?? transfer.id;
|
|
252
|
+
legs.push({
|
|
253
|
+
kind: 'anchor',
|
|
254
|
+
anchorID,
|
|
255
|
+
transactionID: transfer.id,
|
|
256
|
+
subTransactions: collectAnchorSubTransactions(transfer)
|
|
257
|
+
});
|
|
258
|
+
const refs = { blockHashes: [block.blockHash], anchorTxIDs: [transfer.id] };
|
|
259
|
+
const externals = blockExternals(block);
|
|
260
|
+
if (externals.length > 0) {
|
|
261
|
+
refs.external = externals;
|
|
262
|
+
}
|
|
263
|
+
const transaction = {
|
|
264
|
+
id: `${block.blockHash}:${enriched.index}`,
|
|
265
|
+
type,
|
|
266
|
+
status: settled.status,
|
|
267
|
+
providerStatus: settled.providerStatus,
|
|
268
|
+
direction,
|
|
269
|
+
timestamp: block.timestamp,
|
|
270
|
+
send: { token: convertAssetSearchInputToCanonical(assets.from), amount: toUnits(transfer.from.value) },
|
|
271
|
+
receive: { token: convertAssetSearchInputToCanonical(assets.to), amount: toUnits(transfer.to.value) },
|
|
272
|
+
legs,
|
|
273
|
+
refs
|
|
274
|
+
};
|
|
275
|
+
transaction.counterparty = { kind: 'anchor', id: anchorID };
|
|
276
|
+
if (transfer.fee !== null) {
|
|
277
|
+
transaction.fee = { token: convertAssetSearchInputToCanonical(transfer.fee.asset), amount: toUnits(transfer.fee.value) };
|
|
278
|
+
}
|
|
279
|
+
return (transaction);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Emits one logical transaction per operation that resolved to an anchor
|
|
283
|
+
* transfer; the logical type comes from the transfer, not block shape.
|
|
284
|
+
*/
|
|
285
|
+
const anchorTransferClassifier = {
|
|
286
|
+
classify(block) {
|
|
287
|
+
const transactions = [];
|
|
288
|
+
for (const enriched of block.operations) {
|
|
289
|
+
const transfer = enriched.transfer;
|
|
290
|
+
if (transfer === undefined) {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
transactions.push(buildAnchorTransaction(block, enriched, transfer));
|
|
294
|
+
}
|
|
295
|
+
if (transactions.length === 0) {
|
|
296
|
+
return (null);
|
|
297
|
+
}
|
|
298
|
+
return (transactions);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
/**
|
|
302
|
+
* Classifies an on-chain atomic swap: SEND(s) and RECEIVE(s) against a single
|
|
303
|
+
* counterparty with differing send/receive tokens.
|
|
304
|
+
*/
|
|
305
|
+
const atomicSwapClassifier = {
|
|
306
|
+
classify(block) {
|
|
307
|
+
const { sends, receives, otherCount } = viewsOf(block);
|
|
308
|
+
if (otherCount !== 0 || sends.length === 0 || receives.length === 0) {
|
|
309
|
+
return (null);
|
|
310
|
+
}
|
|
311
|
+
const counterparty = uniformCounterparty([...sends, ...receives]);
|
|
312
|
+
if (counterparty === undefined) {
|
|
313
|
+
return (null);
|
|
314
|
+
}
|
|
315
|
+
const received = receives[0];
|
|
316
|
+
if (received === undefined) {
|
|
317
|
+
return (null);
|
|
318
|
+
}
|
|
319
|
+
const principal = sends.find(send => send.amount.token !== received.amount.token);
|
|
320
|
+
if (principal === undefined) {
|
|
321
|
+
return (null);
|
|
322
|
+
}
|
|
323
|
+
const fee = sends.find(send => send.index !== principal.index);
|
|
324
|
+
const transaction = {
|
|
325
|
+
id: block.blockHash,
|
|
326
|
+
type: 'swap',
|
|
327
|
+
status: 'complete',
|
|
328
|
+
direction: 'self',
|
|
329
|
+
timestamp: block.timestamp,
|
|
330
|
+
send: principal.amount,
|
|
331
|
+
receive: received.amount,
|
|
332
|
+
counterparty: { kind: 'liquidity', id: counterparty },
|
|
333
|
+
legs: onchainLegs(block),
|
|
334
|
+
refs: buildRefs(block, [])
|
|
335
|
+
};
|
|
336
|
+
if (fee !== undefined) {
|
|
337
|
+
transaction.fee = fee.amount;
|
|
338
|
+
}
|
|
339
|
+
return ([transaction]);
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
/**
|
|
343
|
+
* Classifies a SEND-only block as a plain send.
|
|
344
|
+
*/
|
|
345
|
+
const sendClassifier = {
|
|
346
|
+
classify(block) {
|
|
347
|
+
const { sends, receives, otherCount } = viewsOf(block);
|
|
348
|
+
if (otherCount !== 0 || receives.length !== 0 || sends.length === 0) {
|
|
349
|
+
return (null);
|
|
350
|
+
}
|
|
351
|
+
const primary = sends[0];
|
|
352
|
+
if (primary === undefined) {
|
|
353
|
+
return (null);
|
|
354
|
+
}
|
|
355
|
+
const transaction = {
|
|
356
|
+
id: block.blockHash,
|
|
357
|
+
type: 'send',
|
|
358
|
+
status: 'complete',
|
|
359
|
+
direction: 'out',
|
|
360
|
+
timestamp: block.timestamp,
|
|
361
|
+
send: primary.amount,
|
|
362
|
+
legs: onchainLegs(block),
|
|
363
|
+
refs: buildRefs(block, [])
|
|
364
|
+
};
|
|
365
|
+
const counterparty = uniformCounterparty(sends);
|
|
366
|
+
if (counterparty !== undefined) {
|
|
367
|
+
transaction.counterparty = { kind: 'account', id: counterparty };
|
|
368
|
+
}
|
|
369
|
+
return ([transaction]);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
/**
|
|
373
|
+
* Classifies a RECEIVE-only block as a plain receive.
|
|
374
|
+
*/
|
|
375
|
+
const receiveClassifier = {
|
|
376
|
+
classify(block) {
|
|
377
|
+
const { sends, receives, otherCount } = viewsOf(block);
|
|
378
|
+
if (otherCount !== 0 || sends.length !== 0 || receives.length === 0) {
|
|
379
|
+
return (null);
|
|
380
|
+
}
|
|
381
|
+
const primary = receives[0];
|
|
382
|
+
if (primary === undefined) {
|
|
383
|
+
return (null);
|
|
384
|
+
}
|
|
385
|
+
const transaction = {
|
|
386
|
+
id: block.blockHash,
|
|
387
|
+
type: 'receive',
|
|
388
|
+
status: 'complete',
|
|
389
|
+
direction: 'in',
|
|
390
|
+
timestamp: block.timestamp,
|
|
391
|
+
receive: primary.amount,
|
|
392
|
+
legs: onchainLegs(block),
|
|
393
|
+
refs: buildRefs(block, [])
|
|
394
|
+
};
|
|
395
|
+
const counterparty = uniformCounterparty(receives);
|
|
396
|
+
if (counterparty !== undefined) {
|
|
397
|
+
transaction.counterparty = { kind: 'account', id: counterparty };
|
|
398
|
+
}
|
|
399
|
+
return ([transaction]);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
/**
|
|
403
|
+
* Fallback that emits a single `other` transaction for any unmatched block.
|
|
404
|
+
*/
|
|
405
|
+
const otherClassifier = {
|
|
406
|
+
classify(block) {
|
|
407
|
+
const transaction = {
|
|
408
|
+
id: block.blockHash,
|
|
409
|
+
type: 'other',
|
|
410
|
+
status: 'complete',
|
|
411
|
+
direction: 'self',
|
|
412
|
+
timestamp: block.timestamp,
|
|
413
|
+
legs: onchainLegs(block),
|
|
414
|
+
refs: buildRefs(block, [])
|
|
415
|
+
};
|
|
416
|
+
return ([transaction]);
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
/**
|
|
420
|
+
* Default ordered classifier list, specific-first.
|
|
421
|
+
*/
|
|
422
|
+
export const defaultClassifiers = [
|
|
423
|
+
anchorTransferClassifier,
|
|
424
|
+
atomicSwapClassifier,
|
|
425
|
+
sendClassifier,
|
|
426
|
+
receiveClassifier,
|
|
427
|
+
otherClassifier
|
|
428
|
+
];
|
|
429
|
+
/**
|
|
430
|
+
* Fold enriched blocks into logical transactions by running the ordered
|
|
431
|
+
* classifiers; the first classifier that applies to a block wins.
|
|
432
|
+
*/
|
|
433
|
+
export function foldHistory(blocks, classifiers) {
|
|
434
|
+
const results = [];
|
|
435
|
+
for (const block of blocks) {
|
|
436
|
+
for (const classifier of classifiers) {
|
|
437
|
+
const classified = classifier.classify(block);
|
|
438
|
+
if (classified !== null) {
|
|
439
|
+
results.push(...classified);
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return (results);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* The first transfer reported as a status across an external's anchors.
|
|
448
|
+
*/
|
|
449
|
+
function firstResolvedTransfer(results) {
|
|
450
|
+
for (const [anchorID, result] of Object.entries(results)) {
|
|
451
|
+
if (result.kind === 'status') {
|
|
452
|
+
return ({ anchorID, transfer: result.status.transaction });
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return (undefined);
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Build decode + status options for an external resolution.
|
|
459
|
+
*/
|
|
460
|
+
function buildExternalOptions(options) {
|
|
461
|
+
const built = {};
|
|
462
|
+
if (options?.decryptionKeys !== undefined) {
|
|
463
|
+
built.decryptionKeys = options.decryptionKeys;
|
|
464
|
+
}
|
|
465
|
+
if (options?.requesterAccount !== undefined) {
|
|
466
|
+
built.requesterAccount = options.requesterAccount;
|
|
467
|
+
}
|
|
468
|
+
return (built);
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Attach raw sources to folded transactions by their source block hash.
|
|
472
|
+
*
|
|
473
|
+
* A transaction id is its block hash, or `blockHash:operationIndex` when a
|
|
474
|
+
* block yields more than one logical transaction.
|
|
475
|
+
*/
|
|
476
|
+
function attachSources(transactions, sources) {
|
|
477
|
+
for (const transaction of transactions) {
|
|
478
|
+
const [blockHash] = transaction.id.split(':');
|
|
479
|
+
if (blockHash === undefined) {
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
const source = sources.get(blockHash);
|
|
483
|
+
if (source !== undefined) {
|
|
484
|
+
transaction.source = source;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Normalize the queried account to its public key string.
|
|
490
|
+
*/
|
|
491
|
+
function perspectiveOf(account) {
|
|
492
|
+
if (account === null) {
|
|
493
|
+
return (undefined);
|
|
494
|
+
}
|
|
495
|
+
if (typeof account === 'string') {
|
|
496
|
+
return (account);
|
|
497
|
+
}
|
|
498
|
+
return (account.publicKeyString.get());
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Build status options for a reverse-lookup.
|
|
502
|
+
*/
|
|
503
|
+
function buildStatusOptions(options) {
|
|
504
|
+
const built = {};
|
|
505
|
+
if (options?.requesterAccount !== undefined) {
|
|
506
|
+
built.requesterAccount = options.requesterAccount;
|
|
507
|
+
}
|
|
508
|
+
return (built);
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Folds a user's on-chain history and anchor transfer data into a condensed
|
|
512
|
+
* list of {@link LogicalTransaction}.
|
|
513
|
+
*/
|
|
514
|
+
export class UserHistory {
|
|
515
|
+
#history;
|
|
516
|
+
#status;
|
|
517
|
+
#classifiers;
|
|
518
|
+
#logger;
|
|
519
|
+
constructor(config) {
|
|
520
|
+
this.#history = config.history;
|
|
521
|
+
if (config.status !== undefined) {
|
|
522
|
+
this.#status = config.status;
|
|
523
|
+
}
|
|
524
|
+
if (config.logger !== undefined) {
|
|
525
|
+
this.#logger = config.logger;
|
|
526
|
+
}
|
|
527
|
+
this.#classifiers = config.classifiers ?? defaultClassifiers;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Fetch, enrich and fold a user's history into logical transactions.
|
|
531
|
+
*/
|
|
532
|
+
async list(account, options) {
|
|
533
|
+
const transactions = [];
|
|
534
|
+
for await (const transaction of this.iterate(account, options)) {
|
|
535
|
+
transactions.push(transaction);
|
|
536
|
+
}
|
|
537
|
+
return (transactions);
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Stream logical transactions page by page instead of buffering the full
|
|
541
|
+
* result set in memory.
|
|
542
|
+
*/
|
|
543
|
+
async *iterate(account, options) {
|
|
544
|
+
const externalCache = new Map();
|
|
545
|
+
const readerCache = new Map();
|
|
546
|
+
const perspective = perspectiveOf(account);
|
|
547
|
+
let cursor = options?.cursor;
|
|
548
|
+
let yielded = 0;
|
|
549
|
+
while (true) {
|
|
550
|
+
const page = await this.#fetchPage(account, cursor, options);
|
|
551
|
+
if (page.length === 0) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
for (const entry of page) {
|
|
555
|
+
const timestamp = entry.voteStaple.timestamp().toISOString();
|
|
556
|
+
const blocks = [];
|
|
557
|
+
const sources = new Map();
|
|
558
|
+
for (const block of entry.voteStaple.blocks) {
|
|
559
|
+
const enriched = await this.#enrichBlock(block, timestamp, perspective, options, externalCache, readerCache);
|
|
560
|
+
blocks.push(enriched);
|
|
561
|
+
if (options?.includeSource === true) {
|
|
562
|
+
sources.set(enriched.blockHash, { staple: entry.voteStaple, enriched });
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const logical = foldHistory(blocks, this.#classifiers);
|
|
566
|
+
attachSources(logical, sources);
|
|
567
|
+
for (const transaction of logical) {
|
|
568
|
+
yield transaction;
|
|
569
|
+
yielded += 1;
|
|
570
|
+
if (options?.limit !== undefined && yielded >= options.limit) {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
const last = page.at(-1);
|
|
576
|
+
if (last === undefined) {
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
cursor = last.voteStaple.blocksHash.toString();
|
|
580
|
+
if (options?.limit === undefined) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Fetch one page of on-chain history.
|
|
587
|
+
*/
|
|
588
|
+
async #fetchPage(account, cursor, options) {
|
|
589
|
+
const query = {};
|
|
590
|
+
if (options?.depth !== undefined) {
|
|
591
|
+
query.depth = options.depth;
|
|
592
|
+
}
|
|
593
|
+
if (options?.pageSize !== undefined) {
|
|
594
|
+
query.pageSize = options.pageSize;
|
|
595
|
+
}
|
|
596
|
+
if (cursor !== undefined) {
|
|
597
|
+
query.startBlocksHash = cursor;
|
|
598
|
+
}
|
|
599
|
+
const result = await this.#history.getHistory(account, query);
|
|
600
|
+
return (result);
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Enrich each operation of a block with its anchor transfer, if resolvable.
|
|
604
|
+
*/
|
|
605
|
+
async #enrichBlock(block, timestamp, perspective, options, externalCache, readerCache) {
|
|
606
|
+
const blockHash = block.hash.toString();
|
|
607
|
+
const account = block.account.publicKeyString.get();
|
|
608
|
+
const enrichEnabled = options?.enrich !== false && this.#status !== undefined;
|
|
609
|
+
const operations = [];
|
|
610
|
+
for (let index = 0; index < block.operations.length; index++) {
|
|
611
|
+
const operation = block.operations[index];
|
|
612
|
+
if (operation === undefined) {
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
const enriched = { operation, index };
|
|
616
|
+
if (enrichEnabled) {
|
|
617
|
+
const resolved = await this.#resolveTransfer(block, operation, index, perspective, options, externalCache, readerCache);
|
|
618
|
+
if (resolved !== undefined) {
|
|
619
|
+
enriched.transfer = resolved.transfer;
|
|
620
|
+
enriched.anchorID = resolved.anchorID;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
operations.push(enriched);
|
|
624
|
+
}
|
|
625
|
+
const result = { blockHash, account, timestamp, operations };
|
|
626
|
+
if (perspective !== undefined) {
|
|
627
|
+
result.perspective = perspective;
|
|
628
|
+
}
|
|
629
|
+
return (result);
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Resolve one operation to its anchor transfer, preferring the SEND's own
|
|
633
|
+
* `external` and falling back to a reverse-lookup by on-chain coordinates.
|
|
634
|
+
*/
|
|
635
|
+
async #resolveTransfer(block, operation, index, perspective, options, externalCache, readerCache) {
|
|
636
|
+
const status = this.#status;
|
|
637
|
+
if (status === undefined) {
|
|
638
|
+
return (undefined);
|
|
639
|
+
}
|
|
640
|
+
if (operation.type === OperationType.SEND && operation.external !== undefined) {
|
|
641
|
+
const fromExternal = await this.#resolveFromExternal(status, operation.external, options, externalCache);
|
|
642
|
+
if (fromExternal !== undefined) {
|
|
643
|
+
return (fromExternal);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
const issuer = block.account.publicKeyString.get();
|
|
647
|
+
let candidateAnchor;
|
|
648
|
+
if (perspective !== undefined && issuer !== perspective) {
|
|
649
|
+
candidateAnchor = issuer;
|
|
650
|
+
}
|
|
651
|
+
else {
|
|
652
|
+
candidateAnchor = counterpartyID(operation);
|
|
653
|
+
}
|
|
654
|
+
if (candidateAnchor === undefined) {
|
|
655
|
+
return (undefined);
|
|
656
|
+
}
|
|
657
|
+
const fromOnChain = await this.#resolveByOnChain(status, candidateAnchor, block.network, block.hash.toString(), index, options, readerCache);
|
|
658
|
+
return (fromOnChain);
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Resolve a transfer from a SEND's `external` envelope, deduped per blob.
|
|
662
|
+
*/
|
|
663
|
+
async #resolveFromExternal(status, external, options, externalCache) {
|
|
664
|
+
const cached = externalCache.get(external);
|
|
665
|
+
if (cached !== undefined || externalCache.has(external)) {
|
|
666
|
+
return (cached);
|
|
667
|
+
}
|
|
668
|
+
let resolved;
|
|
669
|
+
try {
|
|
670
|
+
const results = await status.getStatusesFromExternal(external, buildExternalOptions(options));
|
|
671
|
+
resolved = firstResolvedTransfer(results);
|
|
672
|
+
}
|
|
673
|
+
catch (error) {
|
|
674
|
+
this.#logger?.debug('UserHistory::resolveFromExternal', `External did not resolve to a transfer, degrading to block-shape classification: ${String(error)}`);
|
|
675
|
+
resolved = undefined;
|
|
676
|
+
}
|
|
677
|
+
externalCache.set(external, resolved);
|
|
678
|
+
return (resolved);
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Reverse-lookup a transfer by on-chain coordinates through the anchor the
|
|
682
|
+
* counterparty resolves to.
|
|
683
|
+
*/
|
|
684
|
+
async #resolveByOnChain(status, counterparty, networkID, blockHash, operationIndex, options, readerCache) {
|
|
685
|
+
const reader = await this.#getReader(status, counterparty, readerCache);
|
|
686
|
+
if (reader?.findByOnChain === undefined) {
|
|
687
|
+
return (undefined);
|
|
688
|
+
}
|
|
689
|
+
try {
|
|
690
|
+
const result = await reader.findByOnChain({ keetaNetworkID: networkID, blockHash, operationIndex }, buildStatusOptions(options));
|
|
691
|
+
if (result === null) {
|
|
692
|
+
return (undefined);
|
|
693
|
+
}
|
|
694
|
+
return ({ anchorID: counterparty, transfer: result.transaction });
|
|
695
|
+
}
|
|
696
|
+
catch (error) {
|
|
697
|
+
this.#logger?.debug('UserHistory::resolveByOnChain', `Reverse-lookup at ${counterparty} for ${blockHash}:${operationIndex} failed: ${String(error)}`);
|
|
698
|
+
return (undefined);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Resolve and cache the reader for an anchor counterparty.
|
|
703
|
+
*/
|
|
704
|
+
async #getReader(status, counterparty, readerCache) {
|
|
705
|
+
const cached = readerCache.get(counterparty);
|
|
706
|
+
if (cached !== undefined || readerCache.has(counterparty)) {
|
|
707
|
+
return (cached ?? null);
|
|
708
|
+
}
|
|
709
|
+
let reader;
|
|
710
|
+
try {
|
|
711
|
+
const anchor = KeetaNetLib.Account.fromPublicKeyString(counterparty);
|
|
712
|
+
reader = await status.getReader(anchor);
|
|
713
|
+
}
|
|
714
|
+
catch (error) {
|
|
715
|
+
this.#logger?.debug('UserHistory::getReader', `No reader resolved for ${counterparty}: ${String(error)}`);
|
|
716
|
+
reader = null;
|
|
717
|
+
}
|
|
718
|
+
readerCache.set(counterparty, reader);
|
|
719
|
+
return (reader);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
export default UserHistory;
|
|
723
|
+
//# sourceMappingURL=history.js.map
|