inamprotocol 0.13.0 → 0.15.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/dist/a2a.d.ts ADDED
@@ -0,0 +1,52 @@
1
+ import type { InamClient } from "./client.js";
2
+ import type { AgentRecord, ReputationResult } from "./types.js";
3
+ import { type X402Policy } from "./x402.js";
4
+ /** INAM reputation on an A2A Agent Card (SPEC.md §11.3).
5
+ *
6
+ * A data-only A2A extension: the card names its INAM ID, and a client
7
+ * accepts it only if that ID has, with its own key, linked the same service
8
+ * endpoint (`linked.a2a_endpoint`, SPEC.md §2). Both sides name each other,
9
+ * so a card can't borrow a reputable agent's ID and an ID can't adopt
10
+ * someone else's endpoint. */
11
+ export declare const INAM_A2A_EXTENSION_URI = "https://inamprotocol.org/ext/a2a/v1";
12
+ export type A2APolicy = X402Policy;
13
+ export interface A2ADecision {
14
+ allow: boolean;
15
+ reason: string;
16
+ did?: string;
17
+ reputation?: ReputationResult;
18
+ }
19
+ interface AgentExtension {
20
+ uri: string;
21
+ params?: {
22
+ did?: unknown;
23
+ };
24
+ }
25
+ /** The parts of an A2A Agent Card this check reads: v1.0 `supportedInterfaces`, v0.3 `url`/`additionalInterfaces`. */
26
+ export interface A2AAgentCardLike {
27
+ url?: string;
28
+ supportedInterfaces?: {
29
+ url: string;
30
+ }[];
31
+ additionalInterfaces?: {
32
+ url: string;
33
+ }[];
34
+ capabilities?: {
35
+ extensions?: AgentExtension[];
36
+ };
37
+ }
38
+ /** The `capabilities.extensions` entry an agent adds to its card. Never `required`: it is data-only. */
39
+ export declare function inamA2AExtension(did: string): {
40
+ uri: string;
41
+ description: string;
42
+ required: boolean;
43
+ params: {
44
+ did: string;
45
+ };
46
+ };
47
+ export declare function cardEndpoints(card: A2AAgentCardLike): string[];
48
+ /** Pure check, for callers that already fetched the agent and its reputation. */
49
+ export declare function decideA2A(card: A2AAgentCardLike, agent: AgentRecord, reputation: ReputationResult, policy?: A2APolicy): A2ADecision;
50
+ /** Reads the INAM extension off a card and checks it against the registry `inam` points at. */
51
+ export declare function verifyA2ACard(card: A2AAgentCardLike, inam: InamClient, policy?: A2APolicy): Promise<A2ADecision>;
52
+ export {};
package/dist/a2a.js ADDED
@@ -0,0 +1,58 @@
1
+ import { policyFailure } from "./x402.js";
2
+ /** INAM reputation on an A2A Agent Card (SPEC.md §11.3).
3
+ *
4
+ * A data-only A2A extension: the card names its INAM ID, and a client
5
+ * accepts it only if that ID has, with its own key, linked the same service
6
+ * endpoint (`linked.a2a_endpoint`, SPEC.md §2). Both sides name each other,
7
+ * so a card can't borrow a reputable agent's ID and an ID can't adopt
8
+ * someone else's endpoint. */
9
+ export const INAM_A2A_EXTENSION_URI = "https://inamprotocol.org/ext/a2a/v1";
10
+ /** The `capabilities.extensions` entry an agent adds to its card. Never `required`: it is data-only. */
11
+ export function inamA2AExtension(did) {
12
+ return {
13
+ uri: INAM_A2A_EXTENSION_URI,
14
+ description: "This agent's INAM ID. Verify it links this endpoint, then read its receipt-based reputation.",
15
+ required: false,
16
+ params: { did },
17
+ };
18
+ }
19
+ function normalize(u) {
20
+ try {
21
+ return new URL(u).href.replace(/\/+$/, "");
22
+ }
23
+ catch {
24
+ return null;
25
+ }
26
+ }
27
+ export function cardEndpoints(card) {
28
+ return [card.url, ...(card.supportedInterfaces ?? []), ...(card.additionalInterfaces ?? [])]
29
+ .map((x) => (typeof x === "string" ? x : x?.url))
30
+ .filter((x) => typeof x === "string");
31
+ }
32
+ /** Pure check, for callers that already fetched the agent and its reputation. */
33
+ export function decideA2A(card, agent, reputation, policy = {}) {
34
+ const base = { did: agent.id, reputation };
35
+ if (agent.revokedAt)
36
+ return { ...base, allow: false, reason: "INAM ID is revoked" };
37
+ const linked = agent.linked.a2a_endpoint && normalize(agent.linked.a2a_endpoint);
38
+ if (!linked || !cardEndpoints(card).some((u) => normalize(u) === linked)) {
39
+ return { ...base, allow: false, reason: "no endpoint on this card is the a2a_endpoint the INAM ID linked" };
40
+ }
41
+ const failed = policyFailure(reputation, policy);
42
+ return failed ? { ...base, allow: false, reason: failed } : { ...base, allow: true, reason: "ok" };
43
+ }
44
+ /** Reads the INAM extension off a card and checks it against the registry `inam` points at. */
45
+ export async function verifyA2ACard(card, inam, policy = {}) {
46
+ const did = card.capabilities?.extensions?.find((e) => e.uri === INAM_A2A_EXTENSION_URI)?.params?.did;
47
+ if (typeof did !== "string")
48
+ return { allow: false, reason: "card names no INAM ID" };
49
+ let agent, reputation;
50
+ try {
51
+ [agent, reputation] = await Promise.all([inam.getAgent(did), inam.getReputation(did)]);
52
+ }
53
+ catch {
54
+ return { allow: false, did, reason: "INAM ID not found in the registry" };
55
+ }
56
+ return decideA2A(card, agent, reputation, policy);
57
+ }
58
+ //# sourceMappingURL=a2a.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"a2a.js","sourceRoot":"","sources":["../src/a2a.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAmB,MAAM,WAAW,CAAC;AAE3D;;;;;;8BAM8B;AAE9B,MAAM,CAAC,MAAM,sBAAsB,GAAG,qCAAqC,CAAC;AAoB5E,wGAAwG;AACxG,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC1C,OAAO;QACL,GAAG,EAAE,sBAAsB;QAC3B,WAAW,EAAE,8FAA8F;QAC3G,QAAQ,EAAE,KAAK;QACf,MAAM,EAAE,EAAE,GAAG,EAAE;KAChB,CAAC;AACJ,CAAC;AAED,SAAS,SAAS,CAAC,CAAS;IAC1B,IAAI,CAAC;QACH,OAAO,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC7C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,IAAsB;IAClD,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,CAAC,m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,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AACrG,CAAC;AAED,+FAA+F;AAC/F,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,IAAsB,EAAE,IAAgB,EAAE,SAAoB,EAAE;IAClG,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,sBAAsB,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC;IACtG,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,uBAAuB,EAAE,CAAC;IACtF,IAAI,KAAkB,EAAE,UAA4B,CAAC;IACrD,IAAI,CAAC;QACH,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACzF,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,mCAAmC,EAAE,CAAC;IAC5E,CAAC;IACD,OAAO,SAAS,CAAC,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;AACpD,CAAC"}
package/dist/index.d.ts CHANGED
@@ -11,3 +11,7 @@ export { type TransparencyEntryType, payloadHash } from "./core/transparencyLog.
11
11
  export { type AttestationVerdict, type EvidenceLevel, type EvidenceDimensions } from "./core/attestation.js";
12
12
  export { registerAgentSchema, linkChallengeSchema, linkSchema, postJobSchema, offerSchema, acceptOfferSchema, draftReceiptSchema, countersignSchema, disputeSchema, submitVerificationSchema, setVerifierStatusSchema, } from "./core/schemas.js";
13
13
  export type { LinkedIdentities, ExternalKeyType, LinkChallenge, AgentRecord, VerificationMethod, ReceiptOutcome, ReceiptStatus, DisputeStatus, ExecutionReceipt, SignableReceiptContent, ReputationComponents, ReputationRoleBreakdown, ReputationResult, JobStatus, JobOffer, JobRecord, IndependentVerificationMethod, VerificationResult, VerificationRecord, } from "./types.js";
14
+ export { INAM_X402_EXTENSION, inamX402Extension, decideX402, withInamX402Gate, X402PaymentBlocked } from "./x402.js";
15
+ export type { X402Policy, X402Decision } from "./x402.js";
16
+ export { INAM_A2A_EXTENSION_URI, inamA2AExtension, decideA2A, verifyA2ACard, cardEndpoints } from "./a2a.js";
17
+ export type { A2APolicy, A2ADecision, A2AAgentCardLike } from "./a2a.js";
package/dist/index.js CHANGED
@@ -20,4 +20,6 @@ export { payloadHash } from "./core/transparencyLog.js";
20
20
  // publicly too: a caller building requests by hand (rather than through
21
21
  // InamClient) can validate a payload client-side before sending it.
22
22
  export { registerAgentSchema, linkChallengeSchema, linkSchema, postJobSchema, offerSchema, acceptOfferSchema, draftReceiptSchema, countersignSchema, disputeSchema, submitVerificationSchema, setVerifierStatusSchema, } from "./core/schemas.js";
23
+ export { INAM_X402_EXTENSION, inamX402Extension, decideX402, withInamX402Gate, X402PaymentBlocked } from "./x402.js";
24
+ export { INAM_A2A_EXTENSION_URI, inamA2AExtension, decideA2A, verifyA2ACard, cardEndpoints } from "./a2a.js";
23
25
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,cAAc,EACd,IAAI,EACJ,MAAM,EACN,gBAAgB,EAChB,SAAS,EACT,QAAQ,EACR,UAAU,EACV,KAAK,EACL,OAAO,GAER,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,UAAU,EAAoB,MAAM,kB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AwB,EACxB,aAAa,EACb,eAAe,EACf,mCAAmC,EACnC,qBAAqB,GAEtB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAA4B,MAAM,0BAA0B,CAAC;AAE5G,qEAAqE;AACrE,yEAAyE;AACzE,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAExF,OAAO,EAAE,qBAAqB,EAAE,gCAAgC,EAAiC,MAAM,+BAA+B,CAAC;AAEvI,2EAA2E;AAC3E,0EAA0E;AAC1E,sEAAsE;AACtE,0CAA0C;AAC1C,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAA8B,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAGpF,4EAA4E;AAC5E,2EAA2E;AAC3E,sEAAsE;AACtE,wEAAwE;AACxE,oEAAoE;AACpE,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,UAAU,EACV,aAAa,EACb,WAAW,EACX,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,aAAa,EACb,wBAAwB,EACxB,uBAAuB,GACxB,MAAM,mBAAmB,CAAC;AAuB3B,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,UAAU,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAErH,OAAO,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,SAAS,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC"}
package/dist/x402.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ import type { InamClient } from "./client.js";
2
+ import type { AgentRecord, ReputationResult } from "./types.js";
3
+ import type { EvidenceLevel } from "./core/attestation.js";
4
+ /** "Verify before you pay" for x402 v2 (SPEC.md §11.2).
5
+ *
6
+ * A paid resource names its INAM ID in the 402's `extensions.inam`. Before any
7
+ * payment is signed, the gate checks that this ID proved control of the
8
+ * `payTo` address (`linked.erc8004_id`, SPEC.md §2.1), and that its
9
+ * reputation meets the caller's policy. Without that binding a server could
10
+ * borrow a reputable agent's DID while routing the money elsewhere. */
11
+ export declare const INAM_X402_EXTENSION = "inam";
12
+ export interface X402Policy {
13
+ /** Minimum `trustScore` (0-100, SPEC.md §5.3). Default 0. */
14
+ minTrustScore?: number;
15
+ /** Minimum evidence behind the history. Default "countersigned". */
16
+ minEvidence?: EvidenceLevel;
17
+ }
18
+ export interface X402Decision {
19
+ allow: boolean;
20
+ reason: string;
21
+ did?: string;
22
+ reputation?: ReputationResult;
23
+ }
24
+ /** The `extensions` entry a paid server adds to its PaymentRequired object. */
25
+ export declare function inamX402Extension(did: string): {
26
+ inam: {
27
+ info: {
28
+ did: string;
29
+ };
30
+ schema: {
31
+ $schema: string;
32
+ type: string;
33
+ properties: {
34
+ did: {
35
+ type: string;
36
+ pattern: string;
37
+ };
38
+ };
39
+ required: string[];
40
+ };
41
+ };
42
+ };
43
+ /** Pure policy check, usable without the fetch wrapper. */
44
+ export declare function decideX402(agent: AgentRecord, reputation: ReputationResult, payTo: string[], policy?: X402Policy): X402Decision;
45
+ /** Why `reputation` misses `policy`, or null if it meets it. Shared with the A2A check (§11.3). */
46
+ export declare function policyFailure(reputation: ReputationResult, policy?: X402Policy): string | null;
47
+ export declare class X402PaymentBlocked extends Error {
48
+ readonly decision: X402Decision;
49
+ constructor(decision: X402Decision);
50
+ }
51
+ /** Wraps `fetch` so x402 402 responses are checked before a payment wrapper
52
+ * sees them. Compose it *inside* the payment wrapper:
53
+ *
54
+ * const pay = wrapFetchWithPayment(withInamX402Gate(fetch, inam, policy), signer);
55
+ *
56
+ * Blocked payees throw `X402PaymentBlocked`, so nothing is signed. Allowed
57
+ * ones are passed on with `accepts` narrowed to the bound `payTo`, so the
58
+ * payment wrapper can't pick an unverified destination. */
59
+ export declare function withInamX402Gate(inner: typeof fetch, inam: InamClient, policy?: X402Policy): typeof fetch;
package/dist/x402.js ADDED
@@ -0,0 +1,97 @@
1
+ /** "Verify before you pay" for x402 v2 (SPEC.md §11.2).
2
+ *
3
+ * A paid resource names its INAM ID in the 402's `extensions.inam`. Before any
4
+ * payment is signed, the gate checks that this ID proved control of the
5
+ * `payTo` address (`linked.erc8004_id`, SPEC.md §2.1), and that its
6
+ * reputation meets the caller's policy. Without that binding a server could
7
+ * borrow a reputable agent's DID while routing the money elsewhere. */
8
+ export const INAM_X402_EXTENSION = "inam";
9
+ const EVIDENCE_RANK = { none: 0, countersigned: 1, independently_verified: 2 };
10
+ /** The `extensions` entry a paid server adds to its PaymentRequired object. */
11
+ export function inamX402Extension(did) {
12
+ return {
13
+ [INAM_X402_EXTENSION]: {
14
+ info: { did },
15
+ schema: {
16
+ $schema: "https://json-schema.org/draft/2020-12/schema",
17
+ type: "object",
18
+ properties: { did: { type: "string", pattern: "^did:key:" } },
19
+ required: ["did"],
20
+ },
21
+ },
22
+ };
23
+ }
24
+ /** Pure policy check, usable without the fetch wrapper. */
25
+ export function decideX402(agent, reputation, payTo, policy = {}) {
26
+ const base = { did: agent.id, reputation };
27
+ if (agent.revokedAt)
28
+ return { ...base, allow: false, reason: "payee INAM ID is revoked" };
29
+ const bound = agent.linked.erc8004_id?.toLowerCase();
30
+ if (!bound || !payTo.some((a) => a.toLowerCase() === bound)) {
31
+ return { ...base, allow: false, reason: "payTo is not an address this INAM ID proved control of" };
32
+ }
33
+ const failed = policyFailure(reputation, policy);
34
+ return failed ? { ...base, allow: false, reason: failed } : { ...base, allow: true, reason: "ok" };
35
+ }
36
+ /** Why `reputation` misses `policy`, or null if it meets it. Shared with the A2A check (§11.3). */
37
+ export function policyFailure(reputation, policy = {}) {
38
+ const minEvidence = policy.minEvidence ?? "countersigned";
39
+ if (EVIDENCE_RANK[reputation.evidenceLevel] < EVIDENCE_RANK[minEvidence])
40
+ return `evidence ${reputation.evidenceLevel} is below ${minEvidence}`;
41
+ if (reputation.trustScore < (policy.minTrustScore ?? 0))
42
+ return `trustScore ${reputation.trustScore} is below ${policy.minTrustScore}`;
43
+ return null;
44
+ }
45
+ export class X402PaymentBlocked extends Error {
46
+ decision;
47
+ constructor(decision) {
48
+ super(`x402 payment blocked by INAM: ${decision.reason}`);
49
+ this.decision = decision;
50
+ this.name = "X402PaymentBlocked";
51
+ }
52
+ }
53
+ const b64decode = (s) => new TextDecoder().decode(Uint8Array.from(atob(s), (c) => c.charCodeAt(0)));
54
+ const b64encode = (s) => btoa(String.fromCharCode(...new TextEncoder().encode(s)));
55
+ /** Wraps `fetch` so x402 402 responses are checked before a payment wrapper
56
+ * sees them. Compose it *inside* the payment wrapper:
57
+ *
58
+ * const pay = wrapFetchWithPayment(withInamX402Gate(fetch, inam, policy), signer);
59
+ *
60
+ * Blocked payees throw `X402PaymentBlocked`, so nothing is signed. Allowed
61
+ * ones are passed on with `accepts` narrowed to the bound `payTo`, so the
62
+ * payment wrapper can't pick an unverified destination. */
63
+ // ponytail: x402 v2 header only; v1 (body JSON, X-PAYMENT) servers get treated as having no INAM ID.
64
+ export function withInamX402Gate(inner, inam, policy = {}) {
65
+ return async (input, init) => {
66
+ const res = await inner(input, init);
67
+ if (res.status !== 402)
68
+ return res;
69
+ const header = res.headers.get("payment-required");
70
+ let required;
71
+ try {
72
+ required = JSON.parse(b64decode(header ?? ""));
73
+ }
74
+ catch {
75
+ throw new X402PaymentBlocked({ allow: false, reason: "no readable PAYMENT-REQUIRED header" });
76
+ }
77
+ const did = required.extensions?.[INAM_X402_EXTENSION]?.info?.did;
78
+ if (typeof did !== "string")
79
+ throw new X402PaymentBlocked({ allow: false, reason: "payee names no INAM ID" });
80
+ let agent, reputation;
81
+ try {
82
+ [agent, reputation] = await Promise.all([inam.getAgent(did), inam.getReputation(did)]);
83
+ }
84
+ catch {
85
+ throw new X402PaymentBlocked({ allow: false, did, reason: "payee INAM ID not found in the registry" });
86
+ }
87
+ const accepts = required.accepts ?? [];
88
+ const decision = decideX402(agent, reputation, accepts.map((a) => a.payTo), policy);
89
+ if (!decision.allow)
90
+ throw new X402PaymentBlocked(decision);
91
+ const bound = agent.linked.erc8004_id.toLowerCase();
92
+ const headers = new Headers(res.headers);
93
+ headers.set("payment-required", b64encode(JSON.stringify({ ...required, accepts: accepts.filter((a) => a.payTo.toLowerCase() === bound) })));
94
+ return new Response(res.body, { status: 402, statusText: res.statusText, headers });
95
+ };
96
+ }
97
+ //# sourceMappingURL=x402.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"x402.js","sourceRoot":"","sources":["../src/x402.ts"],"names":[],"mappings":"AAIA;;;;;;uEAMuE;AAEvE,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC;AAgB1C,MAAM,aAAa,GAAkC,EAAE,IAAI,EAAE,CAAC,EAAE,aAAa,EAAE,CAAC,EAAE,sBAAsB,EAAE,CAAC,EAAE,CAAC;AAE9G,+EAA+E;AAC/E,MAAM,UAAU,iBAAiB,CAAC,GAAW;IAC3C,OAAO;QACL,CAAC,mBAAmB,CAAC,EAAE;YACrB,IAAI,EAAE,EAAE,GAAG,EAAE;YACb,MAAM,EAAE;gBACN,OAAO,EAAE,8CAA8C;gBACvD,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,EAAE;gBAC7D,QAAQ,EAAE,CAAC,KAAK,CAAC;aAClB;SACF;KACF,CAAC;AACJ,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,UAAU,CAAC,KAAkB,EAAE,UAA4B,EAAE,KAAe,EAAE,SAAqB,EAAE;IACnH,MAAM,IAAI,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,UAAU,EAAE,CAAC;IAC3C,IAAI,KAAK,CAAC,SAAS;QAAE,OAAO,EAAE,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,0BAA0B,EAAE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;QAC7I,OAAO,IAAI,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,CAAC,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC;IACtF,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inamprotocol",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
4
4
  "description": "Reference TypeScript/JavaScript SDK for the INAM Protocol — agent identity, execution receipts, jobs, and reputation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",