@totemsdk/edge-adapters 0.2.1 → 0.3.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.
Files changed (59) hide show
  1. package/dist/artifact-cache.js +9 -12
  2. package/dist/cjs/artifact-cache.d.ts +36 -0
  3. package/dist/cjs/artifact-cache.js +82 -0
  4. package/dist/cjs/identity.d.ts +19 -0
  5. package/dist/cjs/identity.js +61 -0
  6. package/dist/cjs/index.d.ts +25 -0
  7. package/dist/cjs/index.js +41 -0
  8. package/dist/cjs/liquidity.d.ts +15 -0
  9. package/dist/cjs/liquidity.js +45 -0
  10. package/dist/cjs/location.d.ts +48 -0
  11. package/dist/cjs/location.js +208 -0
  12. package/dist/cjs/lookup.d.ts +13 -0
  13. package/dist/cjs/lookup.js +96 -0
  14. package/dist/cjs/manifest.d.ts +11 -0
  15. package/dist/cjs/manifest.js +36 -0
  16. package/dist/cjs/negotiation-transport.d.ts +50 -0
  17. package/dist/cjs/negotiation-transport.js +122 -0
  18. package/dist/cjs/omnia.d.ts +7 -0
  19. package/dist/cjs/omnia.js +46 -0
  20. package/dist/cjs/package.json +1 -0
  21. package/dist/cjs/payment-l1.d.ts +38 -0
  22. package/dist/cjs/payment-l1.js +46 -0
  23. package/dist/cjs/payment-l2.d.ts +39 -0
  24. package/dist/cjs/payment-l2.js +54 -0
  25. package/dist/cjs/policy.d.ts +13 -0
  26. package/dist/cjs/policy.js +95 -0
  27. package/dist/cjs/proof.d.ts +48 -0
  28. package/dist/cjs/proof.js +88 -0
  29. package/dist/cjs/pubsub.d.ts +11 -0
  30. package/dist/cjs/pubsub.js +30 -0
  31. package/dist/cjs/purchase-authority.d.ts +42 -0
  32. package/dist/cjs/purchase-authority.js +79 -0
  33. package/dist/cjs/purchase-lookup.d.ts +44 -0
  34. package/dist/cjs/purchase-lookup.js +60 -0
  35. package/dist/cjs/purchase-payment.d.ts +78 -0
  36. package/dist/cjs/purchase-payment.js +108 -0
  37. package/dist/cjs/sqlite-commerce-store.d.ts +69 -0
  38. package/dist/cjs/sqlite-commerce-store.js +416 -0
  39. package/dist/cjs/stream.d.ts +7 -0
  40. package/dist/cjs/stream.js +26 -0
  41. package/dist/identity.js +4 -7
  42. package/dist/index.js +18 -41
  43. package/dist/liquidity.js +1 -4
  44. package/dist/location.js +10 -13
  45. package/dist/lookup.js +3 -6
  46. package/dist/manifest.js +5 -7
  47. package/dist/negotiation-transport.js +2 -6
  48. package/dist/omnia.js +1 -4
  49. package/dist/payment-l1.js +1 -4
  50. package/dist/payment-l2.js +5 -8
  51. package/dist/policy.js +1 -4
  52. package/dist/proof.js +7 -10
  53. package/dist/pubsub.js +1 -4
  54. package/dist/purchase-authority.js +1 -4
  55. package/dist/purchase-lookup.js +1 -4
  56. package/dist/purchase-payment.js +4 -7
  57. package/dist/sqlite-commerce-store.js +4 -9
  58. package/dist/stream.js +1 -4
  59. package/package.json +28 -22
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPolicyPortAdapter = createPolicyPortAdapter;
4
+ function isMiddleware(p) {
5
+ return 'evaluate' in p && typeof p.evaluate === 'function';
6
+ }
7
+ /**
8
+ * Wraps an AgentPolicy or PolicyMiddleware as an EdgePolicyPort.
9
+ *
10
+ * When the EdgePolicyPort receives a full `proposal` object, it delegates
11
+ * directly to the policy without lossy reconstruction. When only flat
12
+ * `action`/`subject` params are provided, it builds a minimal AgentProposal
13
+ * (legacy path).
14
+ */
15
+ function createPolicyPortAdapter(policy) {
16
+ return {
17
+ async check(params) {
18
+ try {
19
+ // Prefer full proposal when available
20
+ if (params.proposal) {
21
+ const proposal = {
22
+ id: params.proposal.id,
23
+ agentId: params.proposal.agentId,
24
+ intent: {
25
+ type: params.proposal.intent.type,
26
+ amount: params.proposal.intent.amount,
27
+ tokenId: params.proposal.intent.tokenId,
28
+ recipient: params.proposal.intent.recipient,
29
+ reason: params.proposal.intent.reason,
30
+ risk: params.proposal.intent.risk,
31
+ },
32
+ explanation: params.proposal.explanation,
33
+ confidence: params.proposal.confidence,
34
+ createdAt: params.proposal.createdAt,
35
+ };
36
+ if (isMiddleware(policy)) {
37
+ const result = await policy.evaluate(proposal);
38
+ return {
39
+ ok: true,
40
+ data: {
41
+ allowed: result.outcome === 'approved',
42
+ reason: result.reason,
43
+ },
44
+ };
45
+ }
46
+ const allowed = await policy.canAutoApprove(proposal);
47
+ return {
48
+ ok: true,
49
+ data: {
50
+ allowed,
51
+ reason: allowed ? undefined : 'Action requires user approval per configured policy',
52
+ },
53
+ };
54
+ }
55
+ // Legacy path: build minimal proposal from flat params
56
+ const intentType = params.action.startsWith('pay') ? 'payment' : 'receipt';
57
+ const proposal = {
58
+ id: `edge-policy-${params.action}-${params.subject}-${Date.now()}`,
59
+ agentId: 'edge-policy-adapter',
60
+ intent: {
61
+ type: intentType,
62
+ recipient: params.subject,
63
+ reason: params.action,
64
+ risk: params.context?.['risk'] ?? 'low',
65
+ metadata: params.context,
66
+ },
67
+ explanation: `Policy check for action "${params.action}" by subject "${params.subject}"`,
68
+ confidence: 1,
69
+ createdAt: Date.now(),
70
+ };
71
+ if (isMiddleware(policy)) {
72
+ const result = await policy.evaluate(proposal);
73
+ return {
74
+ ok: true,
75
+ data: {
76
+ allowed: result.outcome === 'approved',
77
+ reason: result.reason,
78
+ },
79
+ };
80
+ }
81
+ const allowed = await policy.canAutoApprove(proposal);
82
+ return {
83
+ ok: true,
84
+ data: {
85
+ allowed,
86
+ reason: allowed ? undefined : 'Action requires user approval per configured policy',
87
+ },
88
+ };
89
+ }
90
+ catch (err) {
91
+ return { ok: false, error: err instanceof Error ? err.message : String(err) };
92
+ }
93
+ },
94
+ };
95
+ }
@@ -0,0 +1,48 @@
1
+ import type { ProofProvider, ProofKind } from '@totemsdk/proof';
2
+ import type { EdgeProofPort } from '@totemsdk/edge';
3
+ export interface ProofPortConfig {
4
+ provider: ProofProvider;
5
+ /** Issuer address or identifier stamped onto created proofs. */
6
+ issuer: string;
7
+ /** Default proof kind when callers don't specify one via context. */
8
+ defaultKind?: ProofKind;
9
+ /**
10
+ * 32-byte WOTS seed. Required for signing; without it only unsigned
11
+ * proofs are returned, which MUST NOT be presented as completed proofs.
12
+ */
13
+ seed?: Uint8Array;
14
+ /**
15
+ * TreeKey index for direct signing (used when no leaseProvider is given).
16
+ * Ignored when leaseProvider is set.
17
+ */
18
+ keyIndex?: number;
19
+ /**
20
+ * WOTS lease provider for coordinated key-index reservation.
21
+ * When set, keyIndex is ignored and the index is reserved via the provider.
22
+ */
23
+ leaseProvider?: {
24
+ reserveKeyUse(params: {
25
+ treeId: string;
26
+ ttlMs?: number;
27
+ payloadHash?: string;
28
+ }): Promise<{
29
+ reservationId: string;
30
+ indices: {
31
+ addressIndex: number;
32
+ l1: number;
33
+ l2: number;
34
+ };
35
+ }>;
36
+ commitKeyUse(reservationId: string, txId: string): Promise<void>;
37
+ burnReservation(reservationId: string, reason: string): Promise<void>;
38
+ };
39
+ leaseTreeId?: string;
40
+ }
41
+ /**
42
+ * Wraps a ProofProvider (e.g. proof-integritas) as an EdgeProofPort.
43
+ *
44
+ * When config.seed is provided the returned proof is a SignedProof;
45
+ * without seed only an UnsignedProof is returned and MUST NOT be
46
+ * presented as a completed proof.
47
+ */
48
+ export declare function createProofPortAdapter(config: ProofPortConfig): EdgeProofPort;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createProofPortAdapter = createProofPortAdapter;
4
+ const proof_1 = require("@totemsdk/proof");
5
+ function rawPayloadEvidence(rawPayload) {
6
+ if (!rawPayload || rawPayload.length === 0)
7
+ return undefined;
8
+ const hash = (0, proof_1.toHex)((0, proof_1.sha3_256)(rawPayload));
9
+ return { id: 'payload:' + hash, kind: 'raw-payload', hash };
10
+ }
11
+ /**
12
+ * Wraps a ProofProvider (e.g. proof-integritas) as an EdgeProofPort.
13
+ *
14
+ * When config.seed is provided the returned proof is a SignedProof;
15
+ * without seed only an UnsignedProof is returned and MUST NOT be
16
+ * presented as a completed proof.
17
+ */
18
+ function createProofPortAdapter(config) {
19
+ const { provider, issuer, defaultKind = 'attestation' } = config;
20
+ return {
21
+ async createProof(params) {
22
+ try {
23
+ const kind = params.context?.['kind'] ?? defaultKind;
24
+ const rawPayload = params.context?.['rawPayload'];
25
+ const evidence = rawPayloadEvidence(rawPayload);
26
+ const unsigned = (0, proof_1.createProof)({
27
+ kind,
28
+ subject: { id: params.subject, kind: 'edge-subject' },
29
+ issuer,
30
+ payload: {
31
+ claims: params.claims,
32
+ ...(rawPayload !== undefined ? { rawPayloadHash: (0, proof_1.toHex)((0, proof_1.sha3_256)(rawPayload)) } : {}),
33
+ ...(params.context ?? {}),
34
+ },
35
+ ...(evidence !== undefined ? { evidence: [evidence] } : {}),
36
+ });
37
+ // Sign if seed is available
38
+ let proof;
39
+ let proofId;
40
+ if (config.seed) {
41
+ if (config.leaseProvider) {
42
+ proof = await (0, proof_1.signWithLease)(unsigned, config.seed, config.leaseProvider, {
43
+ treeId: config.leaseTreeId,
44
+ });
45
+ }
46
+ else {
47
+ proof = (0, proof_1.signProof)(unsigned, config.seed, config.keyIndex ?? 0);
48
+ }
49
+ proofId = proof.proofId;
50
+ }
51
+ else {
52
+ proof = unsigned;
53
+ proofId = unsigned.proofId;
54
+ }
55
+ // Stamp the proof's canonical hash if the provider supports it.
56
+ if (provider.stampHash) {
57
+ await provider.stampHash({ hash: proofId });
58
+ }
59
+ return { ok: true, data: { proofId, proof } };
60
+ }
61
+ catch (err) {
62
+ return { ok: false, error: err instanceof Error ? err.message : String(err) };
63
+ }
64
+ },
65
+ async verifyProof(params) {
66
+ try {
67
+ const proof = params.proof;
68
+ if (provider.verifyProof) {
69
+ const result = await provider.verifyProof(proof);
70
+ return { ok: true, data: { valid: result.valid, reason: result.reason } };
71
+ }
72
+ if (provider.checkProof) {
73
+ const result = await provider.checkProof(proof);
74
+ const valid = result.ok && !result.error;
75
+ return { ok: true, data: { valid, reason: result.error } };
76
+ }
77
+ return {
78
+ ok: false,
79
+ error: 'Provider does not support proof verification',
80
+ errorCode: 'NO_VERIFY_CAPABILITY',
81
+ };
82
+ }
83
+ catch (err) {
84
+ return { ok: false, error: err instanceof Error ? err.message : String(err) };
85
+ }
86
+ },
87
+ };
88
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * EdgePubSubPort adapter — wraps @totemsdk/pubsub-transport's IPubSubTransport
3
+ * as a first-class EdgePubSubPort for EdgeRuntimePorts.pubsub.
4
+ *
5
+ * This is the lightweight counterpart to @totemsdk/edge-mqtt — it provides
6
+ * the raw pub/sub port without the rule engine, sensor bridge, proof
7
+ * publisher, command handler, or MachinePay credit enforcement.
8
+ */
9
+ import type { EdgePubSubPort } from '@totemsdk/edge';
10
+ import type { IPubSubTransport } from '@totemsdk/pubsub-transport';
11
+ export declare function createPubSubPortAdapter(transport: IPubSubTransport): EdgePubSubPort;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ /**
3
+ * EdgePubSubPort adapter — wraps @totemsdk/pubsub-transport's IPubSubTransport
4
+ * as a first-class EdgePubSubPort for EdgeRuntimePorts.pubsub.
5
+ *
6
+ * This is the lightweight counterpart to @totemsdk/edge-mqtt — it provides
7
+ * the raw pub/sub port without the rule engine, sensor bridge, proof
8
+ * publisher, command handler, or MachinePay credit enforcement.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.createPubSubPortAdapter = createPubSubPortAdapter;
12
+ function createPubSubPortAdapter(transport) {
13
+ return {
14
+ async connect() {
15
+ await transport.connect();
16
+ },
17
+ async disconnect() {
18
+ await transport.disconnect();
19
+ },
20
+ async subscribe(topic) {
21
+ return transport.subscribe(topic);
22
+ },
23
+ async publish(topic, payload) {
24
+ await transport.publish(topic, payload);
25
+ },
26
+ onMessage(handler) {
27
+ return transport.onMessage(handler);
28
+ },
29
+ };
30
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * purchase-authority.ts — AuthorityPort adapter over @totemsdk/agent-policy.
3
+ *
4
+ * Maps a machine-commerce TradeAgreement + PurchaseIntent to an AgentProposal
5
+ * and evaluates it through a ComposablePolicy (or any AgentPolicy).
6
+ *
7
+ * Architectural boundary preserved:
8
+ * TradeProposal = machine ↔ machine bargaining (peer-to-peer)
9
+ * AgentProposal = agent → trusted local authority (wallet/policy)
10
+ *
11
+ * The machine negotiation strategy never receives a private key. This adapter
12
+ * is the trusted boundary that decides whether the local authority approves
13
+ * the economic commitment.
14
+ */
15
+ import type { AgentPolicy, PolicyMiddleware } from '@totemsdk/agent-policy';
16
+ import type { EdgeOperationResult } from '@totemsdk/edge';
17
+ import type { PurchaseIntent, TradeAgreement } from '@totemsdk/edge';
18
+ type PolicyLike = AgentPolicy | PolicyMiddleware;
19
+ export interface PurchaseAuthorityAdapterConfig {
20
+ /** The local policy (ComposablePolicy, AgentPolicy, or PolicyMiddleware). */
21
+ policy: PolicyLike;
22
+ /** Agent identifier used in the AgentProposal (e.g. 'edge-purchase-agent'). */
23
+ agentId?: string;
24
+ /** Optional risk level for the proposal. */
25
+ risk?: 'low' | 'medium' | 'high';
26
+ }
27
+ /**
28
+ * Create an AuthorityPort adapter over an agent-policy.
29
+ *
30
+ * approve() builds an AgentProposal from the agreement + intent and evaluates
31
+ * it through the policy. Returns `{ allowed, reason }`.
32
+ */
33
+ export declare function createPurchaseAuthorityAdapter(config: PurchaseAuthorityAdapterConfig): {
34
+ approve(params: {
35
+ agreement: TradeAgreement;
36
+ intent: PurchaseIntent;
37
+ }): Promise<EdgeOperationResult<{
38
+ allowed: boolean;
39
+ reason?: string;
40
+ }>>;
41
+ };
42
+ export {};
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ /**
3
+ * purchase-authority.ts — AuthorityPort adapter over @totemsdk/agent-policy.
4
+ *
5
+ * Maps a machine-commerce TradeAgreement + PurchaseIntent to an AgentProposal
6
+ * and evaluates it through a ComposablePolicy (or any AgentPolicy).
7
+ *
8
+ * Architectural boundary preserved:
9
+ * TradeProposal = machine ↔ machine bargaining (peer-to-peer)
10
+ * AgentProposal = agent → trusted local authority (wallet/policy)
11
+ *
12
+ * The machine negotiation strategy never receives a private key. This adapter
13
+ * is the trusted boundary that decides whether the local authority approves
14
+ * the economic commitment.
15
+ */
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.createPurchaseAuthorityAdapter = createPurchaseAuthorityAdapter;
18
+ function isMiddleware(p) {
19
+ return 'evaluate' in p && typeof p.evaluate === 'function';
20
+ }
21
+ /**
22
+ * Create an AuthorityPort adapter over an agent-policy.
23
+ *
24
+ * approve() builds an AgentProposal from the agreement + intent and evaluates
25
+ * it through the policy. Returns `{ allowed, reason }`.
26
+ */
27
+ function createPurchaseAuthorityAdapter(config) {
28
+ const { policy } = config;
29
+ const agentId = config.agentId ?? 'edge-purchase-agent';
30
+ const risk = config.risk ?? 'medium';
31
+ return {
32
+ async approve(params) {
33
+ try {
34
+ const { agreement, intent } = params;
35
+ const proposal = {
36
+ id: `edge-purchase:${agreement.agreementId}`,
37
+ agentId,
38
+ intent: {
39
+ type: 'payment',
40
+ amount: agreement.terms.price,
41
+ tokenId: agreement.terms.tokenId,
42
+ recipient: agreement.seller,
43
+ reason: `purchase ${intent.resource} from ${agreement.seller}`,
44
+ risk,
45
+ metadata: {
46
+ agreementId: agreement.agreementId,
47
+ negotiationId: agreement.negotiationId,
48
+ resource: intent.resource,
49
+ },
50
+ },
51
+ explanation: `Machine purchase of ${intent.resource} for ${agreement.terms.price} ${agreement.terms.tokenId ?? '0x00'} from ${agreement.seller}`,
52
+ confidence: 1,
53
+ createdAt: Date.now(),
54
+ };
55
+ if (isMiddleware(policy)) {
56
+ const result = await policy.evaluate(proposal);
57
+ return {
58
+ ok: true,
59
+ data: {
60
+ allowed: result.outcome === 'approved',
61
+ reason: result.reason,
62
+ },
63
+ };
64
+ }
65
+ const allowed = await policy.canAutoApprove(proposal);
66
+ return {
67
+ ok: true,
68
+ data: {
69
+ allowed,
70
+ reason: allowed ? undefined : 'Purchase requires user approval per configured policy',
71
+ },
72
+ };
73
+ }
74
+ catch (err) {
75
+ return { ok: false, error: err instanceof Error ? err.message : String(err) };
76
+ }
77
+ },
78
+ };
79
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * purchase-lookup.ts — PurchaseLookupPort adapter over @totemsdk/lookup-client.
3
+ *
4
+ * Maps a machine-commerce `resource` (e.g. "compute", "storage") to a
5
+ * capability query on the Lookup Node, returning signed manifests for the
6
+ * buyer's discovery flow.
7
+ *
8
+ * Discovery is publication, not authority: the buyer MUST still verify each
9
+ * returned manifest's signature, identity binding, expiry, and capabilities
10
+ * before negotiation/payment. This adapter only fetches candidates.
11
+ */
12
+ import type { LookupClient } from '@totemsdk/lookup-client';
13
+ import type { EdgeOperationResult } from '@totemsdk/edge';
14
+ export interface PurchaseLookupAdapterConfig {
15
+ client: LookupClient;
16
+ /** Optional provider address filter (authorAddress for apps). */
17
+ provider?: string;
18
+ /** Optional max price filter (agents). */
19
+ maxPricePerCall?: number;
20
+ /** Optional max latency filter (agents). */
21
+ maxLatencyMs?: number;
22
+ /** Optional result limit. */
23
+ limit?: number;
24
+ }
25
+ /**
26
+ * Create a PurchaseLookupPort adapter over a LookupClient.
27
+ *
28
+ * `resource` is treated as a capability name for agent manifests (the
29
+ * machine-commerce case). If the resource matches a known app category, it
30
+ * falls back to app queries. Returns `{ id, manifest: Uint8Array, nodeId }`
31
+ * candidates for the buyer's manifest-verification pipeline.
32
+ */
33
+ export declare function createPurchaseLookupAdapter(config: PurchaseLookupAdapterConfig): {
34
+ query(params: {
35
+ resource: string;
36
+ provider?: string;
37
+ }): Promise<EdgeOperationResult<{
38
+ results: Array<{
39
+ id: string;
40
+ manifest: Uint8Array;
41
+ nodeId: string;
42
+ }>;
43
+ }>>;
44
+ };
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ /**
3
+ * purchase-lookup.ts — PurchaseLookupPort adapter over @totemsdk/lookup-client.
4
+ *
5
+ * Maps a machine-commerce `resource` (e.g. "compute", "storage") to a
6
+ * capability query on the Lookup Node, returning signed manifests for the
7
+ * buyer's discovery flow.
8
+ *
9
+ * Discovery is publication, not authority: the buyer MUST still verify each
10
+ * returned manifest's signature, identity binding, expiry, and capabilities
11
+ * before negotiation/payment. This adapter only fetches candidates.
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.createPurchaseLookupAdapter = createPurchaseLookupAdapter;
15
+ /**
16
+ * Create a PurchaseLookupPort adapter over a LookupClient.
17
+ *
18
+ * `resource` is treated as a capability name for agent manifests (the
19
+ * machine-commerce case). If the resource matches a known app category, it
20
+ * falls back to app queries. Returns `{ id, manifest: Uint8Array, nodeId }`
21
+ * candidates for the buyer's manifest-verification pipeline.
22
+ */
23
+ function createPurchaseLookupAdapter(config) {
24
+ const { client } = config;
25
+ return {
26
+ async query(params) {
27
+ try {
28
+ const provider = params.provider ?? config.provider;
29
+ const limit = config.limit ?? 20;
30
+ // Agent capability query (machine-commerce primary path).
31
+ const agents = await client.queryAgents({
32
+ capabilityName: params.resource,
33
+ maxPricePerCall: config.maxPricePerCall,
34
+ maxLatencyMs: config.maxLatencyMs,
35
+ limit,
36
+ });
37
+ const results = agents.map((a) => ({
38
+ id: a.capabilityId,
39
+ manifest: a.manifest,
40
+ nodeId: a.nodeId,
41
+ }));
42
+ // If no agent results and a provider is specified, also try app query
43
+ // filtered by author address (provider = operator).
44
+ if (results.length === 0 && provider) {
45
+ const apps = await client.queryApps({
46
+ authorAddress: provider,
47
+ limit,
48
+ });
49
+ for (const a of apps) {
50
+ results.push({ id: a.appId, manifest: a.manifest, nodeId: a.nodeId });
51
+ }
52
+ }
53
+ return { ok: true, data: { results } };
54
+ }
55
+ catch (err) {
56
+ return { ok: false, error: err instanceof Error ? err.message : String(err) };
57
+ }
58
+ },
59
+ };
60
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * purchase-payment.ts — PurchasePaymentPort adapter with an atomic claim.
3
+ *
4
+ * Wraps any EdgePaymentPort (L1 Minima, L2 Omnia, hosted) and adds a stable
5
+ * idempotency-key claim so a retry after a lost response never double-pays.
6
+ *
7
+ * The idempotency key is the stable purchase operation identity
8
+ * (`purchaseId:agreementId:payment`) supplied by the purchasing layer. The
9
+ * wrapper records an atomic pending marker (claim) *before* it invokes the
10
+ * underlying payment port; only the caller that wins the claim pays. A retry
11
+ * with the same key returns the prior result without re-invoking the port, and
12
+ * a concurrent duplicate never double-pays — it either replays the completed
13
+ * outcome or surfaces `PAYMENT_STATE_UNKNOWN` for an in-flight claim.
14
+ *
15
+ * Final exactly-once still depends on the payment system's own reconciliation:
16
+ * if the process dies between the port accepting the payment and the outcome
17
+ * being recorded, the key stays `pending`, and retries return
18
+ * `PAYMENT_STATE_UNKNOWN` rather than risking a second payment. The owner
19
+ * reconciles that window against the payment ledger.
20
+ */
21
+ import type { EdgeOperationResult } from '@totemsdk/edge';
22
+ import { type CasStore, type StorageAdapterWithCapabilities, type WriteAckMode } from '@totemsdk/storage';
23
+ export interface PaymentResult {
24
+ txpowId?: string;
25
+ }
26
+ export interface PaymentPortLike {
27
+ pay(params: {
28
+ recipient: string;
29
+ amount: string;
30
+ tokenId?: string;
31
+ memo?: string;
32
+ idempotencyKey?: string;
33
+ }): Promise<EdgeOperationResult<PaymentResult>>;
34
+ }
35
+ /**
36
+ * Idempotency claim record. `pending` is written atomically before the port
37
+ * call; `completed` records the outcome. A `pending` key is never re-claimed
38
+ * automatically — that is the double-pay guard.
39
+ */
40
+ export type PurchasePaymentClaimRecord = {
41
+ phase: 'pending';
42
+ claimedAt: number;
43
+ } | {
44
+ phase: 'completed';
45
+ result: EdgeOperationResult<PaymentResult>;
46
+ completedAt: number;
47
+ };
48
+ /** The durable idempotency store must support conditional (CAS) writes. */
49
+ export type PurchasePaymentStore = StorageAdapterWithCapabilities & CasStore;
50
+ export interface PurchasePaymentAdapterConfig {
51
+ /** The underlying payment port (L1/L2/hosted). */
52
+ port: PaymentPortLike;
53
+ /**
54
+ * Optional claim store. When omitted, a dev in-memory store is used (no
55
+ * crash guarantees and no cross-instance dedup). A durable CAS-capable
56
+ * store (e.g. a `@totemsdk/storage` FileStore/SqliteStore) makes retries
57
+ * safe across restarts and processes. No silent downgrade: a supplied
58
+ * store is asserted CAS-capable and `durably-acknowledged` unless
59
+ * `requireAckMode` opts into a weaker acknowledgment.
60
+ */
61
+ store?: PurchasePaymentStore;
62
+ /** Key namespace prefix; default `totem_payment:v1:`. */
63
+ namespace?: string;
64
+ /** Required write acknowledgment for a supplied store; default `durably-acknowledged`. */
65
+ requireAckMode?: WriteAckMode;
66
+ }
67
+ /**
68
+ * Create a PurchasePaymentPort with an atomic idempotency-key claim.
69
+ */
70
+ export declare function createPurchasePaymentAdapter(config: PurchasePaymentAdapterConfig): {
71
+ pay(params: {
72
+ recipient: string;
73
+ amount: string;
74
+ tokenId?: string;
75
+ memo?: string;
76
+ idempotencyKey?: string;
77
+ }): Promise<EdgeOperationResult<PaymentResult>>;
78
+ };