@hashspan/core 0.2.0 → 0.4.0
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/README.md +33 -12
- package/dist/index.cjs +264 -20
- package/dist/index.d.cts +217 -17
- package/dist/index.d.mts +217 -17
- package/dist/index.mjs +252 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,8 +8,11 @@ Transaction lifecycle tracing for the on-chain actions of AI agents, built on Op
|
|
|
8
8
|
- **`confirm {chainId}`**: waiting for the receipt: status, block, gas, fees (including the OP-stack L1 fee) and
|
|
9
9
|
revert reason. It carries a span link to its `send` span.
|
|
10
10
|
|
|
11
|
+
Payments that another party settles on chain, such as x402 payments, become a **`payment {chainId}`** span instead
|
|
12
|
+
of a `send` span.
|
|
13
|
+
|
|
11
14
|
The core is library-agnostic and read-only: it never signs, sends or fetches anything. Adapters such as
|
|
12
|
-
[`@hashspan/viem`](https://github.com/selimaytac/hashspan/tree/
|
|
15
|
+
[`@hashspan/viem`](https://github.com/selimaytac/hashspan/tree/@hashspan/core@0.4.0/packages/viem) call it for you. Use the core directly to instrument any other send path.
|
|
13
16
|
|
|
14
17
|
## Install
|
|
15
18
|
|
|
@@ -17,7 +20,7 @@ The core is library-agnostic and read-only: it never signs, sends or fetches any
|
|
|
17
20
|
npm install @hashspan/core @opentelemetry/api
|
|
18
21
|
```
|
|
19
22
|
|
|
20
|
-
`@opentelemetry/api` is the only peer dependency. Bring your own OpenTelemetry SDK and exporter. Requires Node.js 22.3
|
|
23
|
+
`@opentelemetry/api` is the only peer dependency. Bring your own [OpenTelemetry SDK and exporter](https://opentelemetry.io/docs/languages/js/getting-started/nodejs/). Requires Node.js 22.3
|
|
21
24
|
or later; in other runtimes, `hashed` address mode needs a custom `hash` function and otherwise records no addresses,
|
|
22
25
|
with a `diag` warning.
|
|
23
26
|
|
|
@@ -25,15 +28,17 @@ with a `diag` warning.
|
|
|
25
28
|
|
|
26
29
|
```ts
|
|
27
30
|
import { createTxTracker } from '@hashspan/core';
|
|
31
|
+
import { context } from '@opentelemetry/api';
|
|
28
32
|
|
|
29
33
|
const tracker = createTxTracker({ agent: { name: 'treasury-bot' } });
|
|
30
34
|
|
|
31
35
|
// Inside your tool, where the agent framework's span is active:
|
|
32
36
|
const send = tracker.startSend({ chainId: 8453, from, to, value, functionName: 'transfer' });
|
|
33
|
-
let hash;
|
|
37
|
+
let hash: string;
|
|
34
38
|
try {
|
|
35
|
-
|
|
36
|
-
send.
|
|
39
|
+
// Run in the send span's context, so that wallet or RPC spans of the call nest under it.
|
|
40
|
+
hash = await context.with(send.context, () => sendSomehow());
|
|
41
|
+
send.end({ hash });
|
|
37
42
|
} catch (error) {
|
|
38
43
|
send.fail(error);
|
|
39
44
|
throw error;
|
|
@@ -57,10 +62,21 @@ confirm.end({
|
|
|
57
62
|
share one confirm span. A receipt from any of them ends it; a timeout or failure ends it once every caller gave up.
|
|
58
63
|
End every handle you start, since an open handle keeps the shared span open.
|
|
59
64
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
`send.fail(error, { errorType })` records a library's machine-readable error code as `error.type` instead
|
|
66
|
+
of the error's class name, if it is a short identifier (`[A-Za-z0-9_.-]`, at most 64 characters); `exception.type`
|
|
67
|
+
stays the class name.
|
|
68
|
+
|
|
69
|
+
`tracker.startPayment({ chainId, protocol, payer, recipient, asset, amount })` records a payment that another
|
|
70
|
+
party settles on chain, such as an x402 facilitator, as a `payment {chainId}` span; end it with
|
|
71
|
+
`end({ status, hash })` or `fail(error)`. A settlement with a hash links the transaction's confirm span to the payment
|
|
72
|
+
span ([ADR 0013](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0013-x402-payments.md)).
|
|
73
|
+
|
|
74
|
+
All three calls accept an explicit parent `Context` as a second argument. An integration that learns about a call only
|
|
75
|
+
after it started can record it after the fact: pass `startTime` in the input and `endTime` in the options of the
|
|
76
|
+
handle method, e.g. `send.end({ hash }, { endTime })` ([ADR 0009](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0009-telemetry-off-the-call-path.md)). Every method is safe to call: failures inside
|
|
77
|
+
the instrumentation are reported through `diag` and never thrown into your code. The positional forms of earlier
|
|
78
|
+
releases, `send.end(hash, endTime)` and `send.fail(error, endTime, { errorType })`, still work and are deprecated
|
|
79
|
+
until 1.0 ([ADR 0014](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0014-core-api-boundary.md)).
|
|
64
80
|
|
|
65
81
|
## Options
|
|
66
82
|
|
|
@@ -68,7 +84,8 @@ the instrumentation are reported through `diag` and never thrown into your code.
|
|
|
68
84
|
|---|---|---|
|
|
69
85
|
| `tracerProvider` | global provider | Tracer provider to use |
|
|
70
86
|
| `address` | `'raw'` | `'raw'`, `'hashed'`, `'off'`, or `{ mode: 'hashed', hash: (address) => string }` |
|
|
71
|
-
| `errorMessages` | `'off'` | What failed spans record about the error: `'off'` (type only), `'sanitized'` (first line, addresses per `address` mode, calldata removed; in `hashed` and `off` mode any hex longer than an address) or `'raw'` (full message and stack trace). `'raw'` can record RPC URLs that include API keys, as some libraries put the request URL in the message; `'sanitized'` keeps only the first line (viem puts the URL on a later line), which is best effort. See [ADR 0006](https://github.com/selimaytac/hashspan/blob/
|
|
87
|
+
| `errorMessages` | `'off'` | What failed spans record about the error: `'off'` (type only), `'sanitized'` (first line, addresses per `address` mode, calldata removed; in `hashed` and `off` mode any hex longer than an address) or `'raw'` (full message and stack trace). `'raw'` can record RPC URLs that include API keys, as some libraries put the request URL in the message; `'sanitized'` keeps only the first line (viem puts the URL on a later line), which is best effort. See [ADR 0006](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0006-error-privacy.md) |
|
|
88
|
+
| `paymentResource` | `'origin'` | How much of a paid resource's URL `x402.resource` records: `'origin'` (scheme, host and port; nothing for a resource that is not a URL), `'path'` (also the path, never the query string, fragment or user info) or `'off'`. Paths often carry user or account identifiers. At most 512 characters are recorded |
|
|
72
89
|
| `recordFunctionArguments` | `false` | Record `functionArguments` as a JSON array in `blockchain.contract.function.arguments`: bigints as decimal strings, addresses per `address` mode (longer hex values become `<hex>` in `hashed` and `off` mode), at most 4096 characters. Reads only own enumerable data properties: `toJSON()` and getters are never called, so a `Date` records as `{}`; a Proxy's traps still run |
|
|
73
90
|
| `agent` | none | Agent `{ id, name }`; a field set here always wins, unset fields come from the Baggage entries `gen_ai.agent.id` / `gen_ai.agent.name` |
|
|
74
91
|
| `agentFromBaggage` | `true` | Read agent identity fields that `agent` leaves unset from Baggage; set to `false` in services that accept requests from outside their trust boundary |
|
|
@@ -82,7 +99,7 @@ Chain id, transaction hash, sender/recipient (per `address` mode), value, nonce,
|
|
|
82
99
|
on confirmation, status, block number, gas used, effective gas price, L1 fee, total fee and revert reason. Decoded
|
|
83
100
|
call arguments are recorded only with `recordFunctionArguments`, and error messages only with `errorMessages`.
|
|
84
101
|
Attribute definitions:
|
|
85
|
-
[docs/semconv.md](https://github.com/selimaytac/hashspan/blob/
|
|
102
|
+
[docs/semconv.md](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/semconv.md).
|
|
86
103
|
|
|
87
104
|
## Privacy notes
|
|
88
105
|
|
|
@@ -97,9 +114,13 @@ Attribute definitions:
|
|
|
97
114
|
propagated, or strip the entries before outbound calls.
|
|
98
115
|
- **Inbound Baggage can claim an identity.** A caller can send Baggage entries with any agent id. A field set in the
|
|
99
116
|
`agent` option cannot be overridden that way; to ignore identity from Baggage entirely, set `agentFromBaggage: false`
|
|
100
|
-
([ADR 0011](https://github.com/selimaytac/hashspan/blob/
|
|
117
|
+
([ADR 0011](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0011-agent-identity-precedence.md)).
|
|
101
118
|
- The redaction hook (`redact`) runs last on every attribute set and on exception attributes; use it for anything
|
|
102
119
|
else your policy forbids.
|
|
120
|
+
- **Your callbacks' errors go to the diagnostic logger.** If a custom `hash` function or the `redact` hook throws,
|
|
121
|
+
its error object is logged through the OpenTelemetry `diag` logger, outside the address mode and the redaction
|
|
122
|
+
hook. Errors of the instrumented call never are. Do not put sensitive values, such as the address being hashed,
|
|
123
|
+
into errors your callbacks throw, or route `diag` to a sink your policy allows.
|
|
103
124
|
|
|
104
125
|
## License
|
|
105
126
|
|
package/dist/index.cjs
CHANGED
|
@@ -5,7 +5,8 @@ const ATTR_GEN_AI_AGENT_ID = "gen_ai.agent.id";
|
|
|
5
5
|
const ATTR_GEN_AI_AGENT_NAME = "gen_ai.agent.name";
|
|
6
6
|
/**
|
|
7
7
|
* Agent identity as GenAI attributes. A field set in the static identity always wins; Baggage, which a remote caller
|
|
8
|
-
* can set, only fills fields it leaves unset, and is not read at all with `fromBaggage` false
|
|
8
|
+
* can set, only fills fields it leaves unset, and is not read at all with `fromBaggage` false
|
|
9
|
+
* (https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0011-agent-identity-precedence.md).
|
|
9
10
|
*/
|
|
10
11
|
function agentAttributes(ctx, identity, fromBaggage = true) {
|
|
11
12
|
const baggage = fromBaggage ? _opentelemetry_api.propagation.getBaggage(ctx) : void 0;
|
|
@@ -21,8 +22,8 @@ function agentAttributes(ctx, identity, fromBaggage = true) {
|
|
|
21
22
|
/**
|
|
22
23
|
* Attribute keys emitted by hashspan.
|
|
23
24
|
*
|
|
24
|
-
* Stability: development. See docs/semconv.md for
|
|
25
|
-
* These names are a public contract: changes follow the deprecation policy in AGENTS.md.
|
|
25
|
+
* Stability: development. See https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/semconv.md for
|
|
26
|
+
* definitions and value types. These names are a public contract: changes follow the deprecation policy in AGENTS.md.
|
|
26
27
|
*/
|
|
27
28
|
const ATTR_BLOCKCHAIN_SYSTEM = "blockchain.system";
|
|
28
29
|
const ATTR_BLOCKCHAIN_CHAIN_ID = "blockchain.chain.id";
|
|
@@ -43,16 +44,44 @@ const ATTR_BLOCKCHAIN_TX_REPLACEMENT_REASON = "blockchain.tx.replacement.reason"
|
|
|
43
44
|
const ATTR_BLOCKCHAIN_BLOCK_NUMBER = "blockchain.block.number";
|
|
44
45
|
const ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_NAME = "blockchain.contract.function.name";
|
|
45
46
|
const ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_SELECTOR = "blockchain.contract.function.selector";
|
|
46
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* Opt-in: decoded call arguments as a JSON array. See
|
|
49
|
+
* https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0004-privacy-defaults.md.
|
|
50
|
+
*/
|
|
47
51
|
const ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_ARGUMENTS = "blockchain.contract.function.arguments";
|
|
52
|
+
/**
|
|
53
|
+
* Payments settled on chain by a party other than the agent, e.g. an x402 facilitator. See
|
|
54
|
+
* https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0013-x402-payments.md.
|
|
55
|
+
*/
|
|
56
|
+
const ATTR_BLOCKCHAIN_PAYMENT_PROTOCOL = "blockchain.payment.protocol";
|
|
57
|
+
const ATTR_BLOCKCHAIN_PAYMENT_PAYER = "blockchain.payment.payer";
|
|
58
|
+
const ATTR_BLOCKCHAIN_PAYMENT_RECIPIENT = "blockchain.payment.recipient";
|
|
59
|
+
const ATTR_BLOCKCHAIN_PAYMENT_ASSET = "blockchain.payment.asset";
|
|
60
|
+
const ATTR_BLOCKCHAIN_PAYMENT_AMOUNT = "blockchain.payment.amount";
|
|
61
|
+
const ATTR_BLOCKCHAIN_PAYMENT_STATUS = "blockchain.payment.status";
|
|
62
|
+
/** x402's own payment fields. */
|
|
63
|
+
const ATTR_X402_SCHEME = "x402.scheme";
|
|
64
|
+
const ATTR_X402_RESOURCE = "x402.resource";
|
|
48
65
|
/** Values for {@link ATTR_BLOCKCHAIN_SYSTEM}. */
|
|
49
66
|
const BLOCKCHAIN_SYSTEM_VALUE_EVM = "evm";
|
|
50
67
|
/** Values for {@link ATTR_BLOCKCHAIN_OPERATION_NAME}. */
|
|
51
68
|
const BLOCKCHAIN_OPERATION_NAME_VALUE_SEND = "send";
|
|
52
69
|
const BLOCKCHAIN_OPERATION_NAME_VALUE_CONFIRM = "confirm";
|
|
70
|
+
const BLOCKCHAIN_OPERATION_NAME_VALUE_PAYMENT = "payment";
|
|
71
|
+
/** Values for {@link ATTR_BLOCKCHAIN_PAYMENT_PROTOCOL}. */
|
|
72
|
+
const BLOCKCHAIN_PAYMENT_PROTOCOL_VALUE_X402 = "x402";
|
|
73
|
+
/** Values for {@link ATTR_BLOCKCHAIN_PAYMENT_STATUS}. */
|
|
74
|
+
const BLOCKCHAIN_PAYMENT_STATUS_VALUE_SETTLED = "settled";
|
|
75
|
+
const BLOCKCHAIN_PAYMENT_STATUS_VALUE_PENDING = "pending";
|
|
76
|
+
const BLOCKCHAIN_PAYMENT_STATUS_VALUE_FAILED = "failed";
|
|
53
77
|
/** Values for {@link ATTR_BLOCKCHAIN_TX_STATUS}. */
|
|
54
78
|
const BLOCKCHAIN_TX_STATUS_VALUE_SUCCESS = "success";
|
|
55
79
|
const BLOCKCHAIN_TX_STATUS_VALUE_REVERTED = "reverted";
|
|
80
|
+
/**
|
|
81
|
+
* @deprecated A confirm span that gave up waiting records `error.type` `timeout`; this value of
|
|
82
|
+
* `blockchain.tx.status` stops being recorded in a later minor release and the constant is removed in 1.0. See
|
|
83
|
+
* https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0016-timeout-is-an-observer-outcome.md.
|
|
84
|
+
*/
|
|
56
85
|
const BLOCKCHAIN_TX_STATUS_VALUE_TIMEOUT = "timeout";
|
|
57
86
|
const BLOCKCHAIN_TX_STATUS_VALUE_REPLACED = "replaced";
|
|
58
87
|
/** Values for {@link ATTR_BLOCKCHAIN_TX_REPLACEMENT_REASON}, as reported by the instrumented library. */
|
|
@@ -300,9 +329,56 @@ function resolveErrorMessageMode(mode) {
|
|
|
300
329
|
_opentelemetry_api.diag.warn(`hashspan: unknown error message mode "${String(mode)}"; recording error types only`);
|
|
301
330
|
return "off";
|
|
302
331
|
}
|
|
332
|
+
function resolvePaymentResourceMode(mode) {
|
|
333
|
+
if (mode === void 0 || mode === "origin" || mode === "path" || mode === "off") return mode ?? "origin";
|
|
334
|
+
_opentelemetry_api.diag.warn(`hashspan: unknown payment resource mode "${String(mode)}"; not recording payment resources`);
|
|
335
|
+
return "off";
|
|
336
|
+
}
|
|
337
|
+
/** The origin of a URL: `scheme://`, then the host and port, without user info. */
|
|
338
|
+
const URL_ORIGIN = /^([A-Za-z][A-Za-z0-9+.-]*:\/\/)(?:[^/?#]*@)?([^/?#]+)/;
|
|
339
|
+
/**
|
|
340
|
+
* The part of a payment's resource that `mode` records, or undefined for none. Works on the text, so it never throws
|
|
341
|
+
* for a resource that is not a URL.
|
|
342
|
+
*/
|
|
343
|
+
function paymentResourceOf(resource, mode) {
|
|
344
|
+
if (mode === "off" || hidesUserInfo(resource)) return void 0;
|
|
345
|
+
const recorded = mode === "path" ? sanitizeResource(resource) : originOf(resource);
|
|
346
|
+
if (recorded === void 0) return void 0;
|
|
347
|
+
return recorded.length > MAX_RESOURCE_LENGTH ? `${recorded.slice(0, MAX_RESOURCE_LENGTH)}...` : recorded;
|
|
348
|
+
}
|
|
349
|
+
/** Longest `x402.resource` recorded; the value comes from the server that asks for the payment. */
|
|
350
|
+
const MAX_RESOURCE_LENGTH = 512;
|
|
351
|
+
function originOf(resource) {
|
|
352
|
+
const origin = URL_ORIGIN.exec(resource);
|
|
353
|
+
return origin ? `${origin[1]}${origin[2]}` : void 0;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* True for `scheme://` text whose user info contains `?` or `#`, such as `https://user:p?ss@host`. A valid URL
|
|
357
|
+
* percent-encodes them; reading such text by its first `?` or `#` would record part of the user info as the host.
|
|
358
|
+
*/
|
|
359
|
+
function hidesUserInfo(resource) {
|
|
360
|
+
const scheme = URL_SCHEME.exec(resource);
|
|
361
|
+
if (!scheme) return false;
|
|
362
|
+
const rest = resource.slice(scheme[0].length);
|
|
363
|
+
const slash = rest.indexOf("/");
|
|
364
|
+
const authority = slash === -1 ? rest : rest.slice(0, slash);
|
|
365
|
+
const at = authority.lastIndexOf("@");
|
|
366
|
+
return at !== -1 && /[?#]/.test(authority.slice(0, at));
|
|
367
|
+
}
|
|
368
|
+
const URL_SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:\/\//;
|
|
369
|
+
/** `scheme://user:password@` at the start of a URL; the user info is removed. */
|
|
370
|
+
const URL_USER_INFO = /^([A-Za-z][A-Za-z0-9+.-]*:\/\/)[^/?#]*@/;
|
|
371
|
+
/**
|
|
372
|
+
* The resource of a payment without what can carry credentials: the query string, the fragment and the user info.
|
|
373
|
+
* Works on the text, so names that are not URLs are kept as they are.
|
|
374
|
+
*/
|
|
375
|
+
function sanitizeResource(resource) {
|
|
376
|
+
const end = resource.search(/[?#]/);
|
|
377
|
+
return (end === -1 ? resource : resource.slice(0, end)).replace(URL_USER_INFO, "$1");
|
|
378
|
+
}
|
|
303
379
|
//#endregion
|
|
304
380
|
//#region src/version.ts
|
|
305
|
-
const VERSION = "0.
|
|
381
|
+
const VERSION = "0.4.0";
|
|
306
382
|
//#endregion
|
|
307
383
|
//#region src/tracker.ts
|
|
308
384
|
const INSTRUMENTATION_NAME = "@hashspan/core";
|
|
@@ -322,18 +398,37 @@ const NON_SENSITIVE_KEYS = /* @__PURE__ */ new Set([
|
|
|
322
398
|
ATTR_BLOCKCHAIN_TX_STATUS,
|
|
323
399
|
ATTR_BLOCKCHAIN_TX_REPLACEMENT_HASH,
|
|
324
400
|
ATTR_BLOCKCHAIN_TX_REPLACEMENT_REASON,
|
|
401
|
+
ATTR_BLOCKCHAIN_PAYMENT_PROTOCOL,
|
|
402
|
+
ATTR_BLOCKCHAIN_PAYMENT_STATUS,
|
|
325
403
|
ATTR_ERROR_TYPE,
|
|
326
404
|
ATTR_EXCEPTION_TYPE
|
|
327
405
|
]);
|
|
328
406
|
const TX_HASH = /^0x[0-9a-fA-F]{64}$/;
|
|
407
|
+
const ADDRESS = /^0x[0-9a-fA-F]{40}$/;
|
|
408
|
+
/** A non-negative integer that fits in 256 bits. */
|
|
409
|
+
const AMOUNT = /^(0|[1-9][0-9]{0,77})$/;
|
|
410
|
+
/** `error.type` of a payment whose outcome was never learned, as for a confirmation that timed out. */
|
|
411
|
+
const PAYMENT_TIMEOUT = "timeout";
|
|
412
|
+
const PAYMENT_STATUSES = /* @__PURE__ */ new Set([
|
|
413
|
+
BLOCKCHAIN_PAYMENT_STATUS_VALUE_SETTLED,
|
|
414
|
+
BLOCKCHAIN_PAYMENT_STATUS_VALUE_PENDING,
|
|
415
|
+
BLOCKCHAIN_PAYMENT_STATUS_VALUE_FAILED
|
|
416
|
+
]);
|
|
329
417
|
const REPLACEMENT_REASONS = /* @__PURE__ */ new Set([
|
|
330
418
|
BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_REPRICED,
|
|
331
419
|
BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_CANCELLED,
|
|
332
420
|
BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_REPLACED
|
|
333
421
|
]);
|
|
334
|
-
|
|
422
|
+
/** Records nothing; its context is the parent, so a call run in it still nests under the caller. */
|
|
423
|
+
const noopSend = (parent) => ({
|
|
424
|
+
context: parent,
|
|
335
425
|
end: () => {},
|
|
336
426
|
fail: () => {}
|
|
427
|
+
});
|
|
428
|
+
const NOOP_PAYMENT = {
|
|
429
|
+
end: () => {},
|
|
430
|
+
fail: () => {},
|
|
431
|
+
timeout: () => {}
|
|
337
432
|
};
|
|
338
433
|
const NOOP_CONFIRM = {
|
|
339
434
|
end: () => {},
|
|
@@ -357,6 +452,61 @@ function toInt(value) {
|
|
|
357
452
|
function errorType(error) {
|
|
358
453
|
return error instanceof Error && error.name ? error.name : ERROR_TYPE_VALUE_OTHER;
|
|
359
454
|
}
|
|
455
|
+
const ERROR_TYPE_OVERRIDE = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
456
|
+
/** `value` if it is a short identifier, the only kind of free text recorded from a remote party. */
|
|
457
|
+
function identifier(value) {
|
|
458
|
+
return typeof value === "string" && ERROR_TYPE_OVERRIDE.test(value) ? value : void 0;
|
|
459
|
+
}
|
|
460
|
+
/** A decimal amount, or undefined when `value` is not a non-negative integer. */
|
|
461
|
+
function amount(value) {
|
|
462
|
+
const text = typeof value === "bigint" ? value.toString() : value;
|
|
463
|
+
return typeof text === "string" && AMOUNT.test(text) ? text : void 0;
|
|
464
|
+
}
|
|
465
|
+
/** The `error.type` for a failure: an adapter's override when it is a short identifier, else the class name. */
|
|
466
|
+
/** A finite number, an `HrTime` pair or a `Date`: what the deprecated positional `endTime` argument takes. */
|
|
467
|
+
function isTimeInput(value) {
|
|
468
|
+
if (typeof value === "number") return Number.isFinite(value);
|
|
469
|
+
if (Array.isArray(value)) return value.length === 2 && typeof value[0] === "number" && typeof value[1] === "number";
|
|
470
|
+
return Object.prototype.toString.call(value) === "[object Date]";
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Reads the options of a handle method called as `(what, options?)` or, deprecated, as `(what, endTime?, options?)`
|
|
474
|
+
* (ADR 0014). A positional end time wins over `options.endTime`. Never throws: an argument of neither form, or an
|
|
475
|
+
* end time that is not one, is ignored.
|
|
476
|
+
*/
|
|
477
|
+
function handleOptions(second, third) {
|
|
478
|
+
try {
|
|
479
|
+
const positional = isTimeInput(second) ? second : void 0;
|
|
480
|
+
const given = positional !== void 0 || second === void 0 ? third : second;
|
|
481
|
+
if (given !== void 0 && (typeof given !== "object" || given === null || isTimeInput(given))) {
|
|
482
|
+
_opentelemetry_api.diag.debug("hashspan: ignoring a handle argument that is neither options nor an end time");
|
|
483
|
+
return positional !== void 0 ? { endTime: positional } : {};
|
|
484
|
+
}
|
|
485
|
+
const options = given;
|
|
486
|
+
const endTime = positional ?? options?.endTime;
|
|
487
|
+
if (endTime !== void 0 && !isTimeInput(endTime)) _opentelemetry_api.diag.debug("hashspan: ignoring an end time that is not a TimeInput");
|
|
488
|
+
return {
|
|
489
|
+
endTime: isTimeInput(endTime) ? endTime : void 0,
|
|
490
|
+
errorType: options?.errorType
|
|
491
|
+
};
|
|
492
|
+
} catch (error) {
|
|
493
|
+
_opentelemetry_api.diag.debug(`hashspan: could not read handle options (${errorType(error)})`);
|
|
494
|
+
return {};
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
function reportedErrorType(error, options) {
|
|
498
|
+
const override = options?.errorType;
|
|
499
|
+
if (override === void 0) return errorType(error);
|
|
500
|
+
if (typeof override === "string" && ERROR_TYPE_OVERRIDE.test(override)) return override;
|
|
501
|
+
_opentelemetry_api.diag.debug("hashspan: ignoring an error type that is not a short identifier");
|
|
502
|
+
return errorType(error);
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Creates a tracker that records transactions as `send` and `confirm` spans, and payments as `payment` spans, with
|
|
506
|
+
* `@opentelemetry/api` (https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/semconv.md). It makes
|
|
507
|
+
* no network calls; the caller passes hashes and receipts. Its methods and handles never throw: failures are logged
|
|
508
|
+
* via `diag`, and a method that fails returns a handle that records nothing.
|
|
509
|
+
*/
|
|
360
510
|
function createTxTracker(options = {}) {
|
|
361
511
|
const links = new LinkStore({
|
|
362
512
|
ttlMs: options.linkTtlMs ?? DEFAULT_LINK_TTL_MS,
|
|
@@ -368,6 +518,7 @@ function createTxTracker(options = {}) {
|
|
|
368
518
|
});
|
|
369
519
|
const formatAddress = safely("configure address mode", () => resolveAddressFormatter(options.address), OFF_ADDRESS_FORMATTER);
|
|
370
520
|
const errorMessages = safely("configure error message mode", () => resolveErrorMessageMode(options.errorMessages), "off");
|
|
521
|
+
const paymentResource = safely("configure payment resource mode", () => resolvePaymentResourceMode(options.paymentResource), "off");
|
|
371
522
|
let tracer;
|
|
372
523
|
const getTracer = () => {
|
|
373
524
|
tracer ??= (options.tracerProvider ?? _opentelemetry_api.trace.getTracerProvider()).getTracer(INSTRUMENTATION_NAME, VERSION);
|
|
@@ -390,8 +541,9 @@ function createTxTracker(options = {}) {
|
|
|
390
541
|
return redacted;
|
|
391
542
|
};
|
|
392
543
|
/**
|
|
393
|
-
* Exception event attributes for `error`, per the error message mode. The error object itself is never handed
|
|
394
|
-
*
|
|
544
|
+
* Exception event attributes for `error`, per the error message mode. The error object itself is never handed to
|
|
545
|
+
* the SDK: its message and stack can carry addresses and calldata
|
|
546
|
+
* (https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0006-error-privacy.md).
|
|
395
547
|
*/
|
|
396
548
|
const exceptionAttributes = (type, error) => {
|
|
397
549
|
const attributes = { [ATTR_EXCEPTION_TYPE]: type };
|
|
@@ -406,12 +558,15 @@ function createTxTracker(options = {}) {
|
|
|
406
558
|
if (error instanceof Error && error.stack) attributes[ATTR_EXCEPTION_STACKTRACE] = error.stack;
|
|
407
559
|
return attributes;
|
|
408
560
|
};
|
|
409
|
-
/**
|
|
410
|
-
|
|
561
|
+
/**
|
|
562
|
+
* Error names are free text too: they follow the address mode and pass through the redaction hook.
|
|
563
|
+
* `exceptionName` is the class name for `exception.type` when `errorName` is an adapter's error type.
|
|
564
|
+
*/
|
|
565
|
+
const markError = (span, errorName, error, exceptionName = errorName) => {
|
|
411
566
|
const type = formatAddressesIn(errorName, formatAddress);
|
|
412
567
|
let message;
|
|
413
568
|
if (error !== void 0) {
|
|
414
|
-
const exception = redact(exceptionAttributes(
|
|
569
|
+
const exception = redact(exceptionAttributes(formatAddressesIn(exceptionName, formatAddress), error));
|
|
415
570
|
span.addEvent(EXCEPTION_EVENT, exception);
|
|
416
571
|
const recorded = exception[ATTR_EXCEPTION_MESSAGE];
|
|
417
572
|
if (typeof recorded === "string") message = recorded;
|
|
@@ -442,6 +597,10 @@ function createTxTracker(options = {}) {
|
|
|
442
597
|
const formatted = formatAddress(address);
|
|
443
598
|
if (formatted !== void 0) attributes[key] = formatted;
|
|
444
599
|
};
|
|
600
|
+
/** Records `address` only if it is one: payment addresses come from remote parties. */
|
|
601
|
+
const setPaymentAddress = (attributes, key, address) => {
|
|
602
|
+
if (typeof address === "string" && ADDRESS.test(address)) setAddress(attributes, key, address);
|
|
603
|
+
};
|
|
445
604
|
const baseAttributes = (chainId, operation, ctx) => ({
|
|
446
605
|
[ATTR_BLOCKCHAIN_SYSTEM]: "evm",
|
|
447
606
|
[ATTR_BLOCKCHAIN_CHAIN_ID]: chainId,
|
|
@@ -469,14 +628,23 @@ function createTxTracker(options = {}) {
|
|
|
469
628
|
}, parent);
|
|
470
629
|
const finish = finisher(span);
|
|
471
630
|
return {
|
|
472
|
-
|
|
631
|
+
context: _opentelemetry_api.trace.setSpan(parent, span),
|
|
632
|
+
end: (result, second) => finish("record transaction hash", () => {
|
|
633
|
+
const hash = typeof result === "string" ? result : result?.hash;
|
|
634
|
+
if (typeof hash !== "string") {
|
|
635
|
+
_opentelemetry_api.diag.debug("hashspan: ending a send span without a transaction hash");
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
473
638
|
links.set(input.chainId, hash, {
|
|
474
639
|
spanContext: span.spanContext(),
|
|
475
640
|
parent
|
|
476
641
|
});
|
|
477
642
|
span.setAttributes(redact({ [ATTR_BLOCKCHAIN_TX_HASH]: hash }));
|
|
478
|
-
}, endTime),
|
|
479
|
-
fail: (error,
|
|
643
|
+
}, handleOptions(second).endTime),
|
|
644
|
+
fail: (error, second, third) => {
|
|
645
|
+
const options = handleOptions(second, third);
|
|
646
|
+
finish("record send failure", () => markError(span, reportedErrorType(error, options), error, errorType(error)), options.endTime);
|
|
647
|
+
}
|
|
480
648
|
};
|
|
481
649
|
};
|
|
482
650
|
const receiptAttributes = (receipt) => {
|
|
@@ -556,7 +724,10 @@ function createTxTracker(options = {}) {
|
|
|
556
724
|
const { replacementReason: _reason, ...mined } = receipt;
|
|
557
725
|
confirm.receipt(mined, endTime);
|
|
558
726
|
};
|
|
559
|
-
/**
|
|
727
|
+
/**
|
|
728
|
+
* Ends `shared` with `receipt`, attributing it to the transaction that was mined
|
|
729
|
+
* (https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0008-replaced-transactions.md).
|
|
730
|
+
*/
|
|
560
731
|
const endWithReceipt = (chainId, hash, shared, receipt, endTime) => {
|
|
561
732
|
const mined = receipt.transactionHash;
|
|
562
733
|
if (mined === void 0) {
|
|
@@ -605,20 +776,80 @@ function createTxTracker(options = {}) {
|
|
|
605
776
|
end();
|
|
606
777
|
};
|
|
607
778
|
return {
|
|
608
|
-
end: (receipt,
|
|
779
|
+
end: (receipt, second) => {
|
|
780
|
+
const { endTime } = handleOptions(second);
|
|
609
781
|
if (done || shared.ended) return;
|
|
610
782
|
done = true;
|
|
611
783
|
shared.active -= 1;
|
|
612
784
|
shared.ended = true;
|
|
613
785
|
safely("record receipt", () => endWithReceipt(chainId, hash, shared, receipt, endTime), void 0);
|
|
614
786
|
},
|
|
615
|
-
timeout: (
|
|
616
|
-
|
|
787
|
+
timeout: (second) => {
|
|
788
|
+
const { endTime } = handleOptions(second);
|
|
789
|
+
withdraw(() => shared.timeout(endTime));
|
|
790
|
+
},
|
|
791
|
+
fail: (error, second) => {
|
|
792
|
+
const { endTime } = handleOptions(second);
|
|
793
|
+
withdraw(() => shared.fail(error, endTime));
|
|
794
|
+
}
|
|
795
|
+
};
|
|
796
|
+
};
|
|
797
|
+
const startPayment = (input, parentCtx) => {
|
|
798
|
+
const parent = parentCtx ?? _opentelemetry_api.context.active();
|
|
799
|
+
const attributes = baseAttributes(input.chainId, BLOCKCHAIN_OPERATION_NAME_VALUE_PAYMENT, parent);
|
|
800
|
+
const protocol = identifier(input.protocol);
|
|
801
|
+
if (protocol !== void 0) attributes[ATTR_BLOCKCHAIN_PAYMENT_PROTOCOL] = protocol;
|
|
802
|
+
setPaymentAddress(attributes, ATTR_BLOCKCHAIN_PAYMENT_PAYER, input.payer);
|
|
803
|
+
const knownPayer = typeof input.payer === "string" && ADDRESS.test(input.payer);
|
|
804
|
+
setPaymentAddress(attributes, ATTR_BLOCKCHAIN_PAYMENT_RECIPIENT, input.recipient);
|
|
805
|
+
setPaymentAddress(attributes, ATTR_BLOCKCHAIN_PAYMENT_ASSET, input.asset);
|
|
806
|
+
const paid = amount(input.amount);
|
|
807
|
+
if (paid !== void 0) attributes[ATTR_BLOCKCHAIN_PAYMENT_AMOUNT] = paid;
|
|
808
|
+
const scheme = identifier(input.x402?.scheme);
|
|
809
|
+
if (scheme !== void 0) attributes[ATTR_X402_SCHEME] = scheme;
|
|
810
|
+
const resource = input.x402?.resource;
|
|
811
|
+
const recorded = typeof resource === "string" ? paymentResourceOf(resource, paymentResource) : void 0;
|
|
812
|
+
if (recorded) attributes[ATTR_X402_RESOURCE] = formatAddressesIn(recorded, formatAddress);
|
|
813
|
+
const span = getTracer().startSpan(`payment ${input.chainId}`, {
|
|
814
|
+
kind: _opentelemetry_api.SpanKind.CLIENT,
|
|
815
|
+
attributes: redact(attributes),
|
|
816
|
+
...input.startTime !== void 0 ? { startTime: input.startTime } : {}
|
|
817
|
+
}, parent);
|
|
818
|
+
const finish = finisher(span);
|
|
819
|
+
const recordSettlement = (settlement) => {
|
|
820
|
+
const status = settlement.status;
|
|
821
|
+
if (!PAYMENT_STATUSES.has(status)) {
|
|
822
|
+
_opentelemetry_api.diag.debug("hashspan: ignoring a payment settlement with an unknown status");
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
const settled = { [ATTR_BLOCKCHAIN_PAYMENT_STATUS]: status };
|
|
826
|
+
const hash = settlement.hash;
|
|
827
|
+
if (typeof hash === "string" && TX_HASH.test(hash)) {
|
|
828
|
+
if (!links.get(input.chainId, hash)) links.set(input.chainId, hash, {
|
|
829
|
+
spanContext: span.spanContext(),
|
|
830
|
+
parent
|
|
831
|
+
});
|
|
832
|
+
settled[ATTR_BLOCKCHAIN_TX_HASH] = hash;
|
|
833
|
+
}
|
|
834
|
+
if (!knownPayer) setPaymentAddress(settled, ATTR_BLOCKCHAIN_PAYMENT_PAYER, settlement.payer);
|
|
835
|
+
const settledAmount = paid === void 0 ? amount(settlement.amount) : void 0;
|
|
836
|
+
if (settledAmount !== void 0) settled[ATTR_BLOCKCHAIN_PAYMENT_AMOUNT] = settledAmount;
|
|
837
|
+
span.setAttributes(redact(settled));
|
|
838
|
+
if (status === "failed") markError(span, identifier(settlement.errorReason) ?? "_OTHER");
|
|
839
|
+
};
|
|
840
|
+
return {
|
|
841
|
+
end: (settlement, options) => finish("record payment settlement", () => recordSettlement(settlement), handleOptions(options).endTime),
|
|
842
|
+
fail: (error, options) => {
|
|
843
|
+
const read = handleOptions(options);
|
|
844
|
+
finish("record payment failure", () => markError(span, reportedErrorType(error, read), error, errorType(error)), read.endTime);
|
|
845
|
+
},
|
|
846
|
+
timeout: (options) => finish("record payment timeout", () => markError(span, PAYMENT_TIMEOUT), handleOptions(options).endTime)
|
|
617
847
|
};
|
|
618
848
|
};
|
|
619
849
|
return {
|
|
620
|
-
startSend: (input, parent) => safely("start send span", () => startSend(input, parent),
|
|
621
|
-
startConfirm: (input, parent) => safely("start confirm span", () => startConfirm(input, parent), NOOP_CONFIRM)
|
|
850
|
+
startSend: (input, parent) => safely("start send span", () => startSend(input, parent), noopSend(parent ?? _opentelemetry_api.context.active())),
|
|
851
|
+
startConfirm: (input, parent) => safely("start confirm span", () => startConfirm(input, parent), NOOP_CONFIRM),
|
|
852
|
+
startPayment: (input, parent) => safely("start payment span", () => startPayment(input, parent), NOOP_PAYMENT)
|
|
622
853
|
};
|
|
623
854
|
}
|
|
624
855
|
//#endregion
|
|
@@ -628,6 +859,12 @@ exports.ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_ARGUMENTS = ATTR_BLOCKCHAIN_CONTRACT_F
|
|
|
628
859
|
exports.ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_NAME = ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_NAME;
|
|
629
860
|
exports.ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_SELECTOR = ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_SELECTOR;
|
|
630
861
|
exports.ATTR_BLOCKCHAIN_OPERATION_NAME = ATTR_BLOCKCHAIN_OPERATION_NAME;
|
|
862
|
+
exports.ATTR_BLOCKCHAIN_PAYMENT_AMOUNT = ATTR_BLOCKCHAIN_PAYMENT_AMOUNT;
|
|
863
|
+
exports.ATTR_BLOCKCHAIN_PAYMENT_ASSET = ATTR_BLOCKCHAIN_PAYMENT_ASSET;
|
|
864
|
+
exports.ATTR_BLOCKCHAIN_PAYMENT_PAYER = ATTR_BLOCKCHAIN_PAYMENT_PAYER;
|
|
865
|
+
exports.ATTR_BLOCKCHAIN_PAYMENT_PROTOCOL = ATTR_BLOCKCHAIN_PAYMENT_PROTOCOL;
|
|
866
|
+
exports.ATTR_BLOCKCHAIN_PAYMENT_RECIPIENT = ATTR_BLOCKCHAIN_PAYMENT_RECIPIENT;
|
|
867
|
+
exports.ATTR_BLOCKCHAIN_PAYMENT_STATUS = ATTR_BLOCKCHAIN_PAYMENT_STATUS;
|
|
631
868
|
exports.ATTR_BLOCKCHAIN_SYSTEM = ATTR_BLOCKCHAIN_SYSTEM;
|
|
632
869
|
exports.ATTR_BLOCKCHAIN_TX_EFFECTIVE_GAS_PRICE = ATTR_BLOCKCHAIN_TX_EFFECTIVE_GAS_PRICE;
|
|
633
870
|
exports.ATTR_BLOCKCHAIN_TX_FEE = ATTR_BLOCKCHAIN_TX_FEE;
|
|
@@ -645,8 +882,15 @@ exports.ATTR_BLOCKCHAIN_TX_VALUE = ATTR_BLOCKCHAIN_TX_VALUE;
|
|
|
645
882
|
exports.ATTR_ERROR_TYPE = ATTR_ERROR_TYPE;
|
|
646
883
|
exports.ATTR_GEN_AI_AGENT_ID = ATTR_GEN_AI_AGENT_ID;
|
|
647
884
|
exports.ATTR_GEN_AI_AGENT_NAME = ATTR_GEN_AI_AGENT_NAME;
|
|
885
|
+
exports.ATTR_X402_RESOURCE = ATTR_X402_RESOURCE;
|
|
886
|
+
exports.ATTR_X402_SCHEME = ATTR_X402_SCHEME;
|
|
648
887
|
exports.BLOCKCHAIN_OPERATION_NAME_VALUE_CONFIRM = BLOCKCHAIN_OPERATION_NAME_VALUE_CONFIRM;
|
|
888
|
+
exports.BLOCKCHAIN_OPERATION_NAME_VALUE_PAYMENT = BLOCKCHAIN_OPERATION_NAME_VALUE_PAYMENT;
|
|
649
889
|
exports.BLOCKCHAIN_OPERATION_NAME_VALUE_SEND = BLOCKCHAIN_OPERATION_NAME_VALUE_SEND;
|
|
890
|
+
exports.BLOCKCHAIN_PAYMENT_PROTOCOL_VALUE_X402 = BLOCKCHAIN_PAYMENT_PROTOCOL_VALUE_X402;
|
|
891
|
+
exports.BLOCKCHAIN_PAYMENT_STATUS_VALUE_FAILED = BLOCKCHAIN_PAYMENT_STATUS_VALUE_FAILED;
|
|
892
|
+
exports.BLOCKCHAIN_PAYMENT_STATUS_VALUE_PENDING = BLOCKCHAIN_PAYMENT_STATUS_VALUE_PENDING;
|
|
893
|
+
exports.BLOCKCHAIN_PAYMENT_STATUS_VALUE_SETTLED = BLOCKCHAIN_PAYMENT_STATUS_VALUE_SETTLED;
|
|
650
894
|
exports.BLOCKCHAIN_SYSTEM_VALUE_EVM = BLOCKCHAIN_SYSTEM_VALUE_EVM;
|
|
651
895
|
exports.BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_CANCELLED = BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_CANCELLED;
|
|
652
896
|
exports.BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_REPLACED = BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_REPLACED;
|