@hashspan/core 0.3.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 +30 -13
- package/dist/index.cjs +249 -17
- package/dist/index.d.cts +209 -18
- package/dist/index.d.mts +209 -18
- package/dist/index.mjs +237 -18
- 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/@hashspan/core@0.
|
|
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,14 +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
|
-
`send.fail(error,
|
|
65
|
+
`send.fail(error, { errorType })` records a library's machine-readable error code as `error.type` instead
|
|
61
66
|
of the error's class name, if it is a short identifier (`[A-Za-z0-9_.-]`, at most 64 characters); `exception.type`
|
|
62
67
|
stays the class name.
|
|
63
68
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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)).
|
|
68
80
|
|
|
69
81
|
## Options
|
|
70
82
|
|
|
@@ -72,7 +84,8 @@ the instrumentation are reported through `diag` and never thrown into your code.
|
|
|
72
84
|
|---|---|---|
|
|
73
85
|
| `tracerProvider` | global provider | Tracer provider to use |
|
|
74
86
|
| `address` | `'raw'` | `'raw'`, `'hashed'`, `'off'`, or `{ mode: 'hashed', hash: (address) => string }` |
|
|
75
|
-
| `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.
|
|
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 |
|
|
76
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 |
|
|
77
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` |
|
|
78
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 |
|
|
@@ -86,7 +99,7 @@ Chain id, transaction hash, sender/recipient (per `address` mode), value, nonce,
|
|
|
86
99
|
on confirmation, status, block number, gas used, effective gas price, L1 fee, total fee and revert reason. Decoded
|
|
87
100
|
call arguments are recorded only with `recordFunctionArguments`, and error messages only with `errorMessages`.
|
|
88
101
|
Attribute definitions:
|
|
89
|
-
[docs/semconv.md](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.
|
|
102
|
+
[docs/semconv.md](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/semconv.md).
|
|
90
103
|
|
|
91
104
|
## Privacy notes
|
|
92
105
|
|
|
@@ -101,9 +114,13 @@ Attribute definitions:
|
|
|
101
114
|
propagated, or strip the entries before outbound calls.
|
|
102
115
|
- **Inbound Baggage can claim an identity.** A caller can send Baggage entries with any agent id. A field set in the
|
|
103
116
|
`agent` option cannot be overridden that way; to ignore identity from Baggage entirely, set `agentFromBaggage: false`
|
|
104
|
-
([ADR 0011](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.
|
|
117
|
+
([ADR 0011](https://github.com/selimaytac/hashspan/blob/@hashspan/core@0.4.0/docs/adr/0011-agent-identity-precedence.md)).
|
|
105
118
|
- The redaction hook (`redact`) runs last on every attribute set and on exception attributes; use it for anything
|
|
106
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.
|
|
107
124
|
|
|
108
125
|
## License
|
|
109
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: () => {},
|
|
@@ -358,7 +453,47 @@ function errorType(error) {
|
|
|
358
453
|
return error instanceof Error && error.name ? error.name : ERROR_TYPE_VALUE_OTHER;
|
|
359
454
|
}
|
|
360
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
|
+
}
|
|
361
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
|
+
}
|
|
362
497
|
function reportedErrorType(error, options) {
|
|
363
498
|
const override = options?.errorType;
|
|
364
499
|
if (override === void 0) return errorType(error);
|
|
@@ -366,6 +501,12 @@ function reportedErrorType(error, options) {
|
|
|
366
501
|
_opentelemetry_api.diag.debug("hashspan: ignoring an error type that is not a short identifier");
|
|
367
502
|
return errorType(error);
|
|
368
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
|
+
*/
|
|
369
510
|
function createTxTracker(options = {}) {
|
|
370
511
|
const links = new LinkStore({
|
|
371
512
|
ttlMs: options.linkTtlMs ?? DEFAULT_LINK_TTL_MS,
|
|
@@ -377,6 +518,7 @@ function createTxTracker(options = {}) {
|
|
|
377
518
|
});
|
|
378
519
|
const formatAddress = safely("configure address mode", () => resolveAddressFormatter(options.address), OFF_ADDRESS_FORMATTER);
|
|
379
520
|
const errorMessages = safely("configure error message mode", () => resolveErrorMessageMode(options.errorMessages), "off");
|
|
521
|
+
const paymentResource = safely("configure payment resource mode", () => resolvePaymentResourceMode(options.paymentResource), "off");
|
|
380
522
|
let tracer;
|
|
381
523
|
const getTracer = () => {
|
|
382
524
|
tracer ??= (options.tracerProvider ?? _opentelemetry_api.trace.getTracerProvider()).getTracer(INSTRUMENTATION_NAME, VERSION);
|
|
@@ -399,8 +541,9 @@ function createTxTracker(options = {}) {
|
|
|
399
541
|
return redacted;
|
|
400
542
|
};
|
|
401
543
|
/**
|
|
402
|
-
* Exception event attributes for `error`, per the error message mode. The error object itself is never handed
|
|
403
|
-
*
|
|
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).
|
|
404
547
|
*/
|
|
405
548
|
const exceptionAttributes = (type, error) => {
|
|
406
549
|
const attributes = { [ATTR_EXCEPTION_TYPE]: type };
|
|
@@ -454,6 +597,10 @@ function createTxTracker(options = {}) {
|
|
|
454
597
|
const formatted = formatAddress(address);
|
|
455
598
|
if (formatted !== void 0) attributes[key] = formatted;
|
|
456
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
|
+
};
|
|
457
604
|
const baseAttributes = (chainId, operation, ctx) => ({
|
|
458
605
|
[ATTR_BLOCKCHAIN_SYSTEM]: "evm",
|
|
459
606
|
[ATTR_BLOCKCHAIN_CHAIN_ID]: chainId,
|
|
@@ -481,14 +628,23 @@ function createTxTracker(options = {}) {
|
|
|
481
628
|
}, parent);
|
|
482
629
|
const finish = finisher(span);
|
|
483
630
|
return {
|
|
484
|
-
|
|
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
|
+
}
|
|
485
638
|
links.set(input.chainId, hash, {
|
|
486
639
|
spanContext: span.spanContext(),
|
|
487
640
|
parent
|
|
488
641
|
});
|
|
489
642
|
span.setAttributes(redact({ [ATTR_BLOCKCHAIN_TX_HASH]: hash }));
|
|
490
|
-
}, endTime),
|
|
491
|
-
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
|
+
}
|
|
492
648
|
};
|
|
493
649
|
};
|
|
494
650
|
const receiptAttributes = (receipt) => {
|
|
@@ -568,7 +724,10 @@ function createTxTracker(options = {}) {
|
|
|
568
724
|
const { replacementReason: _reason, ...mined } = receipt;
|
|
569
725
|
confirm.receipt(mined, endTime);
|
|
570
726
|
};
|
|
571
|
-
/**
|
|
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
|
+
*/
|
|
572
731
|
const endWithReceipt = (chainId, hash, shared, receipt, endTime) => {
|
|
573
732
|
const mined = receipt.transactionHash;
|
|
574
733
|
if (mined === void 0) {
|
|
@@ -617,20 +776,80 @@ function createTxTracker(options = {}) {
|
|
|
617
776
|
end();
|
|
618
777
|
};
|
|
619
778
|
return {
|
|
620
|
-
end: (receipt,
|
|
779
|
+
end: (receipt, second) => {
|
|
780
|
+
const { endTime } = handleOptions(second);
|
|
621
781
|
if (done || shared.ended) return;
|
|
622
782
|
done = true;
|
|
623
783
|
shared.active -= 1;
|
|
624
784
|
shared.ended = true;
|
|
625
785
|
safely("record receipt", () => endWithReceipt(chainId, hash, shared, receipt, endTime), void 0);
|
|
626
786
|
},
|
|
627
|
-
timeout: (
|
|
628
|
-
|
|
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)
|
|
629
847
|
};
|
|
630
848
|
};
|
|
631
849
|
return {
|
|
632
|
-
startSend: (input, parent) => safely("start send span", () => startSend(input, parent),
|
|
633
|
-
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)
|
|
634
853
|
};
|
|
635
854
|
}
|
|
636
855
|
//#endregion
|
|
@@ -640,6 +859,12 @@ exports.ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_ARGUMENTS = ATTR_BLOCKCHAIN_CONTRACT_F
|
|
|
640
859
|
exports.ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_NAME = ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_NAME;
|
|
641
860
|
exports.ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_SELECTOR = ATTR_BLOCKCHAIN_CONTRACT_FUNCTION_SELECTOR;
|
|
642
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;
|
|
643
868
|
exports.ATTR_BLOCKCHAIN_SYSTEM = ATTR_BLOCKCHAIN_SYSTEM;
|
|
644
869
|
exports.ATTR_BLOCKCHAIN_TX_EFFECTIVE_GAS_PRICE = ATTR_BLOCKCHAIN_TX_EFFECTIVE_GAS_PRICE;
|
|
645
870
|
exports.ATTR_BLOCKCHAIN_TX_FEE = ATTR_BLOCKCHAIN_TX_FEE;
|
|
@@ -657,8 +882,15 @@ exports.ATTR_BLOCKCHAIN_TX_VALUE = ATTR_BLOCKCHAIN_TX_VALUE;
|
|
|
657
882
|
exports.ATTR_ERROR_TYPE = ATTR_ERROR_TYPE;
|
|
658
883
|
exports.ATTR_GEN_AI_AGENT_ID = ATTR_GEN_AI_AGENT_ID;
|
|
659
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;
|
|
660
887
|
exports.BLOCKCHAIN_OPERATION_NAME_VALUE_CONFIRM = BLOCKCHAIN_OPERATION_NAME_VALUE_CONFIRM;
|
|
888
|
+
exports.BLOCKCHAIN_OPERATION_NAME_VALUE_PAYMENT = BLOCKCHAIN_OPERATION_NAME_VALUE_PAYMENT;
|
|
661
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;
|
|
662
894
|
exports.BLOCKCHAIN_SYSTEM_VALUE_EVM = BLOCKCHAIN_SYSTEM_VALUE_EVM;
|
|
663
895
|
exports.BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_CANCELLED = BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_CANCELLED;
|
|
664
896
|
exports.BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_REPLACED = BLOCKCHAIN_TX_REPLACEMENT_REASON_VALUE_REPLACED;
|