@toon-protocol/sdk 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/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/identity.ts","../src/errors.ts","../src/handler-context.ts","../src/handler-registry.ts","../src/pricing-validator.ts","../src/verification-pipeline.ts","../src/payment-handler-bridge.ts","../src/event-storage-handler.ts","../src/create-node.ts","../src/skill-descriptor.ts"],"sourcesContent":["/**\n * Unified identity module for @toon-protocol/sdk.\n *\n * Derives both a Nostr pubkey (x-only Schnorr, BIP-340) and an EVM address\n * (Keccak-256) from a single secp256k1 private key, following the NIP-06\n * derivation standard.\n *\n * Both Nostr and EVM use the secp256k1 elliptic curve, so a single 12-word\n * seed phrase can recover the complete identity across both layers.\n */\n\nimport {\n generateMnemonic as _generateMnemonic,\n validateMnemonic,\n mnemonicToSeedSync,\n} from '@scure/bip39';\nimport { wordlist } from '@scure/bip39/wordlists/english.js';\nimport { HDKey } from '@scure/bip32';\nimport { getPublicKey } from 'nostr-tools/pure';\nimport { secp256k1 } from '@noble/curves/secp256k1.js';\nimport { keccak_256 } from '@noble/hashes/sha3.js';\nimport { bytesToHex } from '@noble/hashes/utils.js';\nimport { IdentityError } from './errors.js';\n\n/**\n * Represents a node's complete identity: secret key, Nostr pubkey, and EVM address.\n */\nexport interface NodeIdentity {\n /** The 32-byte secp256k1 secret key. */\n secretKey: Uint8Array;\n /** The x-only Schnorr public key (32 bytes, 64 lowercase hex characters). */\n pubkey: string;\n /** The EIP-55 checksummed EVM address (0x-prefixed, 42 characters). */\n evmAddress: string;\n}\n\n/**\n * Options for mnemonic-based key derivation.\n */\nexport interface FromMnemonicOptions {\n /** Key index in the NIP-06 derivation path. Defaults to 0. */\n accountIndex?: number;\n}\n\n/**\n * Generates a valid 12-word BIP-39 mnemonic using 128-bit entropy.\n *\n * @returns A space-separated string of 12 BIP-39 English words.\n */\nexport function generateMnemonic(): string {\n return _generateMnemonic(wordlist, 128);\n}\n\n/**\n * Derives a complete NodeIdentity from a BIP-39 mnemonic phrase.\n *\n * Uses the NIP-06 derivation path: m/44'/1237'/0'/0/{accountIndex}\n *\n * @param mnemonic - A valid BIP-39 mnemonic (12 or 24 words).\n * @param options - Optional derivation options (accountIndex defaults to 0).\n * @returns The derived NodeIdentity with secretKey, pubkey, and evmAddress.\n * @throws {IdentityError} If the mnemonic is invalid.\n */\nexport function fromMnemonic(\n mnemonic: string,\n options?: FromMnemonicOptions\n): NodeIdentity {\n if (!validateMnemonic(mnemonic, wordlist)) {\n throw new IdentityError(\n `Invalid BIP-39 mnemonic: the provided words do not form a valid mnemonic phrase`\n );\n }\n\n const accountIndex = options?.accountIndex ?? 0;\n\n if (\n !Number.isInteger(accountIndex) ||\n accountIndex < 0 ||\n accountIndex > MAX_BIP32_INDEX\n ) {\n throw new IdentityError(\n `Invalid accountIndex: expected a non-negative integer (0 to ${MAX_BIP32_INDEX}), got ${String(accountIndex)}`\n );\n }\n\n const path = `m/44'/1237'/0'/0/${accountIndex}`;\n\n let seed: Uint8Array | undefined;\n try {\n seed = mnemonicToSeedSync(mnemonic);\n const hdKey = HDKey.fromMasterSeed(seed).derive(path);\n\n if (!hdKey.privateKey) {\n throw new IdentityError(`Failed to derive private key at path ${path}`);\n }\n\n const secretKey = hdKey.privateKey;\n return deriveIdentity(secretKey);\n } catch (error: unknown) {\n if (error instanceof IdentityError) {\n throw error;\n }\n throw new IdentityError(\n `Key derivation failed at path ${path}: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n } finally {\n // Best-effort zeroing of the intermediate seed to reduce the window\n // during which sensitive material remains in memory. This is not a\n // guarantee (JS has no secure-erase primitive), but it limits exposure.\n if (seed) {\n seed.fill(0);\n }\n }\n}\n\n/**\n * Derives a complete NodeIdentity from an existing 32-byte secret key.\n *\n * @param secretKey - A 32-byte secp256k1 secret key.\n * @returns The derived NodeIdentity with secretKey, pubkey, and evmAddress.\n * @throws {IdentityError} If the secret key is not exactly 32 bytes.\n */\nexport function fromSecretKey(secretKey: Uint8Array): NodeIdentity {\n if (!(secretKey instanceof Uint8Array)) {\n throw new IdentityError(\n `Invalid secret key: expected Uint8Array, got ${secretKey === null ? 'null' : typeof secretKey}`\n );\n }\n\n if (secretKey.length !== 32) {\n throw new IdentityError(\n `Invalid secret key: expected 32 bytes, got ${secretKey.length} bytes`\n );\n }\n\n try {\n return deriveIdentity(secretKey);\n } catch (error: unknown) {\n if (error instanceof IdentityError) {\n throw error;\n }\n throw new IdentityError(\n `Invalid secret key: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Maximum valid BIP-32 non-hardened child index (2^31 - 1).\n * Values at or above 2^31 are reserved for hardened derivation.\n */\nconst MAX_BIP32_INDEX = 0x7fffffff;\n\n/**\n * Shared helper that computes pubkey and evmAddress from a 32-byte secret key.\n * Returns a defensive copy of the secret key to prevent external mutation.\n */\nfunction deriveIdentity(secretKey: Uint8Array): NodeIdentity {\n // Compute x-only Schnorr pubkey (32 bytes hex) via nostr-tools\n const pubkey = getPublicKey(secretKey);\n\n // Compute EVM address from the uncompressed secp256k1 public key\n const evmAddress = computeEvmAddress(secretKey);\n\n // Return a defensive copy of the secret key to prevent external mutation\n // from affecting the identity or vice versa\n return { secretKey: new Uint8Array(secretKey), pubkey, evmAddress };\n}\n\n/**\n * Computes an EIP-55 checksummed EVM address from a secp256k1 private key.\n *\n * Steps:\n * 1. Compute the full uncompressed public key (65 bytes: 0x04 + 64 bytes X,Y)\n * 2. Strip the 0x04 prefix to get 64 bytes\n * 3. Hash with Keccak-256\n * 4. Take the last 20 bytes\n * 5. Format as 0x-prefixed hex with EIP-55 checksum\n */\nfunction computeEvmAddress(secretKey: Uint8Array): string {\n // Get the uncompressed public key (65 bytes: 0x04 prefix + 64 bytes X,Y)\n const uncompressedPubkey = secp256k1.getPublicKey(secretKey, false);\n\n // Strip the 0x04 prefix (first byte), leaving 64 bytes of X,Y coordinates\n const pubkeyWithoutPrefix = uncompressedPubkey.slice(1);\n\n // Hash with Keccak-256\n const hash = keccak_256(pubkeyWithoutPrefix);\n\n // Take last 20 bytes\n const addressBytes = hash.slice(-20);\n const addressHex = bytesToHex(addressBytes);\n\n // Apply EIP-55 mixed-case checksum\n return toChecksumAddress(addressHex);\n}\n\n/**\n * Applies EIP-55 mixed-case checksum encoding to an Ethereum address.\n *\n * EIP-55 rules:\n * - Hash the lowercase hex address (without 0x) with Keccak-256\n * - For each hex character in the address:\n * - If the corresponding nibble in the hash is >= 8, uppercase it\n * - Otherwise, lowercase it\n *\n * @param addressHex - The 40-character lowercase hex address (without 0x prefix).\n * @returns The checksummed address with 0x prefix.\n */\nfunction toChecksumAddress(addressHex: string): string {\n const lower = addressHex.toLowerCase();\n const hash = bytesToHex(keccak_256(new TextEncoder().encode(lower)));\n\n let checksummed = '0x';\n for (let i = 0; i < 40; i++) {\n const char = lower[i];\n const hashChar = hash[i];\n if (char === undefined || hashChar === undefined) {\n throw new IdentityError(\n `Unexpected undefined at index ${i} during checksum computation`\n );\n }\n const hashNibble = parseInt(hashChar, 16);\n checksummed += hashNibble >= 8 ? char.toUpperCase() : char;\n }\n\n return checksummed;\n}\n","/**\n * SDK-specific error classes for @toon-protocol/sdk.\n * All errors extend ToonError from @toon-protocol/core for a consistent error hierarchy.\n */\n\nimport { ToonError } from '@toon-protocol/core';\n\n/**\n * Error thrown when identity operations fail.\n * Used for invalid mnemonics, invalid secret keys, and key derivation failures.\n */\nexport class IdentityError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'IDENTITY_ERROR', cause);\n this.name = 'IdentityError';\n }\n}\n\n/**\n * Error thrown when node lifecycle operations fail.\n * Used for start/stop failures, configuration errors, etc.\n */\nexport class NodeError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'NODE_ERROR', cause);\n this.name = 'NodeError';\n }\n}\n\n/**\n * Error thrown when handler dispatch operations fail.\n * Used for handler registration conflicts, missing handlers, and dispatch errors.\n */\nexport class HandlerError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'HANDLER_ERROR', cause);\n this.name = 'HandlerError';\n }\n}\n\n/**\n * Error thrown when Schnorr signature verification fails.\n * Used for invalid signatures, malformed events, and verification pipeline errors.\n */\nexport class VerificationError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'VERIFICATION_ERROR', cause);\n this.name = 'VerificationError';\n }\n}\n\n/**\n * Error thrown when payment validation fails.\n * Used for pricing calculation errors, insufficient payment, and pricing policy violations.\n */\nexport class PricingError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'PRICING_ERROR', cause);\n this.name = 'PricingError';\n }\n}\n","/**\n * Handler context for @toon-protocol/sdk.\n *\n * Provides a context object passed to kind-based handlers with methods\n * for accessing TOON data, shallow-parsed metadata, and accept/reject actions.\n */\n\nimport { createHash } from 'crypto';\nimport type { NostrEvent } from 'nostr-tools/pure';\nimport type { ToonRoutingMeta } from '@toon-protocol/core/toon';\nimport type {\n HandlePacketAcceptResponse,\n HandlePacketRejectResponse,\n} from '@toon-protocol/core';\n\n// Re-export core response types so SDK consumers don't need to import from core\nexport type { HandlePacketAcceptResponse, HandlePacketRejectResponse };\n\n/**\n * The handler context passed to each kind-based handler.\n */\nexport interface HandlerContext {\n /** Raw TOON string (base64-encoded). */\n readonly toon: string;\n /** Event kind from shallow parse. */\n readonly kind: number;\n /** Event pubkey from shallow parse. */\n readonly pubkey: string;\n /** Payment amount in the ILP packet. */\n readonly amount: bigint;\n /** ILP destination address. */\n readonly destination: string;\n /** Lazy-decode the TOON payload into a full NostrEvent. */\n decode(): NostrEvent;\n /** Accept the packet with optional metadata. */\n accept(metadata?: Record<string, unknown>): HandlePacketAcceptResponse;\n /** Reject the packet with an ILP error code and message. */\n reject(code: string, message: string): HandlePacketRejectResponse;\n}\n\nexport interface CreateHandlerContextOptions {\n toon: string;\n meta: ToonRoutingMeta;\n amount: bigint;\n destination: string;\n toonDecoder: (toon: string) => NostrEvent;\n}\n\n/**\n * Creates a HandlerContext from the given options.\n */\nexport function createHandlerContext(\n options: CreateHandlerContextOptions\n): HandlerContext {\n let cachedEvent: NostrEvent | undefined;\n\n return {\n get toon() {\n return options.toon;\n },\n get kind() {\n return options.meta.kind;\n },\n get pubkey() {\n return options.meta.pubkey;\n },\n get amount() {\n return options.amount;\n },\n get destination() {\n return options.destination;\n },\n decode(): NostrEvent {\n if (!cachedEvent) {\n cachedEvent = options.toonDecoder(options.toon);\n }\n return cachedEvent;\n },\n accept(metadata?: Record<string, unknown>): HandlePacketAcceptResponse {\n // Compute fulfillment = SHA-256(raw_toon_bytes).\n // Matches the connector's PaymentHandlerAdapter.computeFulfillmentFromData().\n // The sender uses condition = SHA-256(SHA-256(raw_toon_bytes)) and the\n // connector validates SHA-256(fulfillment) == condition.\n const toonBytes = Buffer.from(options.toon, 'base64');\n const fulfillment = createHash('sha256').update(toonBytes).digest().toString('base64');\n return {\n accept: true,\n fulfillment,\n ...(metadata ? { metadata } : {}),\n };\n },\n reject(code: string, message: string): HandlePacketRejectResponse {\n return {\n accept: false,\n code,\n message,\n };\n },\n };\n}\n","/**\n * Handler registry for @toon-protocol/sdk.\n *\n * Maps event kinds to handler functions for dispatching incoming ILP packets.\n */\n\nimport { JOB_REQUEST_KIND_BASE } from '@toon-protocol/core';\nimport type {\n HandlerContext,\n HandlePacketAcceptResponse,\n HandlePacketRejectResponse,\n} from './handler-context.js';\n\nexport type HandlerResponse =\n | HandlePacketAcceptResponse\n | HandlePacketRejectResponse;\n\nexport type Handler = (ctx: HandlerContext) => Promise<HandlerResponse>;\n\n/**\n * Registry that maps Nostr event kinds to handler functions.\n */\nexport class HandlerRegistry {\n private handlers = new Map<number, Handler>();\n private defaultHandler: Handler | undefined;\n\n /**\n * Register a handler for a specific event kind.\n * Replaces any existing handler for that kind.\n */\n on(kind: number, handler: Handler): this {\n this.handlers.set(kind, handler);\n return this;\n }\n\n /**\n * Register a default handler for unrecognized kinds.\n */\n onDefault(handler: Handler): this {\n this.defaultHandler = handler;\n return this;\n }\n\n /**\n * Returns all registered kind numbers, sorted ascending.\n */\n getRegisteredKinds(): number[] {\n return [...this.handlers.keys()].sort((a, b) => a - b);\n }\n\n /**\n * Returns registered kinds in the DVM request range (5000-5999), sorted ascending.\n * Uses JOB_REQUEST_KIND_BASE (5000) as the range start.\n */\n getDvmKinds(): number[] {\n const dvmRangeStart = JOB_REQUEST_KIND_BASE;\n const dvmRangeEnd = JOB_REQUEST_KIND_BASE + 999;\n return this.getRegisteredKinds().filter(\n (k) => k >= dvmRangeStart && k <= dvmRangeEnd\n );\n }\n\n /**\n * Dispatch a context to the appropriate handler based on kind.\n */\n async dispatch(ctx: HandlerContext): Promise<HandlerResponse> {\n const handler = this.handlers.get(ctx.kind);\n if (handler) {\n return handler(ctx);\n }\n if (this.defaultHandler) {\n return this.defaultHandler(ctx);\n }\n return {\n accept: false,\n code: 'F00',\n message: `No handler registered for kind ${ctx.kind}`,\n };\n }\n}\n","/**\n * Pricing validator for @toon-protocol/sdk.\n *\n * Validates that incoming ILP payments meet the required price based on\n * TOON payload size and configured price per byte.\n */\n\nimport type { ToonRoutingMeta } from '@toon-protocol/core/toon';\n\nexport interface PricingValidatorConfig {\n basePricePerByte?: bigint;\n ownPubkey: string;\n kindPricing?: Record<number, bigint>;\n}\n\nexport interface PricingValidationResult {\n accepted: boolean;\n rejection?: {\n accept: false;\n code: string;\n message: string;\n metadata?: Record<string, string>;\n };\n}\n\n/**\n * Creates a pricing validator that checks payment amounts against TOON size.\n */\nexport function createPricingValidator(config: PricingValidatorConfig) {\n const basePricePerByte = config.basePricePerByte ?? 10n;\n\n return {\n validate(meta: ToonRoutingMeta, amount: bigint): PricingValidationResult {\n // Self-write bypass\n if (meta.pubkey === config.ownPubkey) {\n return { accepted: true };\n }\n\n // Kind-specific pricing override (use Object.hasOwn for prototype-safe lookup)\n const kindOverride =\n config.kindPricing && Object.hasOwn(config.kindPricing, meta.kind)\n ? config.kindPricing[meta.kind]\n : undefined;\n const pricePerByte = kindOverride ?? basePricePerByte;\n\n const requiredAmount = BigInt(meta.rawBytes.length) * pricePerByte;\n\n if (amount < requiredAmount) {\n return {\n accepted: false,\n rejection: {\n accept: false,\n code: 'F04',\n message: 'Insufficient payment',\n metadata: {\n required: requiredAmount.toString(),\n received: amount.toString(),\n },\n },\n };\n }\n\n return { accepted: true };\n },\n };\n}\n","/**\n * Verification pipeline for @toon-protocol/sdk.\n *\n * Verifies Schnorr signatures on incoming TOON payloads before dispatching\n * to handlers. In dev mode, verification is skipped.\n */\n\nimport type { ToonRoutingMeta } from '@toon-protocol/core/toon';\nimport { schnorr } from '@noble/curves/secp256k1.js';\nimport { hexToBytes } from '@noble/hashes/utils.js';\n\nexport interface VerificationResult {\n verified: boolean;\n rejection?: {\n accept: false;\n code: string;\n message: string;\n };\n}\n\nexport interface VerificationPipelineConfig {\n devMode: boolean;\n}\n\n/**\n * Creates a verification pipeline that checks Schnorr signatures.\n */\nexport function createVerificationPipeline(config: VerificationPipelineConfig) {\n return {\n async verify(\n meta: ToonRoutingMeta,\n _toonData: string\n ): Promise<VerificationResult> {\n if (config.devMode) {\n return { verified: true };\n }\n\n try {\n const sigBytes = hexToBytes(meta.sig);\n const msgBytes = hexToBytes(meta.id);\n const pubkeyBytes = hexToBytes(meta.pubkey);\n const valid = schnorr.verify(sigBytes, msgBytes, pubkeyBytes);\n if (!valid) {\n return {\n verified: false,\n rejection: {\n accept: false,\n code: 'F06',\n message: 'Invalid Schnorr signature',\n },\n };\n }\n return { verified: true };\n } catch {\n return {\n verified: false,\n rejection: {\n accept: false,\n code: 'F06',\n message: 'Signature verification failed',\n },\n };\n }\n },\n };\n}\n","/**\n * Payment handler bridge for @toon-protocol/sdk.\n *\n * Bridges between ILP packet handling and the handler registry,\n * using isTransit to distinguish fire-and-forget from await semantics.\n */\n\nimport type { HandlerRegistry } from './handler-registry.js';\nimport { createHandlerContext } from './handler-context.js';\nimport type { ToonRoutingMeta } from '@toon-protocol/core/toon';\n\nexport interface PaymentHandlerBridgeConfig {\n registry: HandlerRegistry;\n devMode: boolean;\n ownPubkey: string;\n basePricePerByte: bigint;\n}\n\nexport interface PaymentRequest {\n paymentId: string;\n destination: string;\n amount: string;\n data: string;\n isTransit: boolean;\n}\n\nexport interface PaymentResponse {\n accept: boolean;\n fulfillment?: string;\n code?: string;\n message?: string;\n data?: string;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Creates a payment handler bridge that connects ILP packets to the handler registry.\n */\nexport function createPaymentHandlerBridge(config: PaymentHandlerBridgeConfig) {\n const { registry } = config;\n\n return {\n async handlePayment(request: PaymentRequest): Promise<PaymentResponse> {\n let amount: bigint;\n try {\n amount = BigInt(request.amount);\n } catch {\n return {\n accept: false,\n code: 'T00',\n message: 'Invalid payment amount',\n };\n }\n\n const ctx = createHandlerContext({\n toon: request.data,\n meta: {\n kind: 0,\n pubkey: '',\n id: '',\n sig: '',\n rawBytes: new Uint8Array(0),\n } as ToonRoutingMeta,\n amount,\n destination: request.destination,\n toonDecoder: () => {\n throw new Error('decode not available in bridge');\n },\n });\n\n if (request.isTransit) {\n void registry.dispatch(ctx).catch((err: unknown) => {\n // Log only the error message, not the full error object (which may contain payload data)\n const errMsg = err instanceof Error ? err.message : 'Unknown error';\n console.error('Transit handler error:', errMsg);\n });\n return { accept: true };\n }\n\n try {\n return await registry.dispatch(ctx);\n } catch (err: unknown) {\n // Log only the error message, not the full error object (which may contain payload data)\n const errMsg = err instanceof Error ? err.message : 'Unknown error';\n console.error('Handler error:', errMsg);\n return {\n accept: false,\n code: 'T00',\n message: 'Internal error',\n };\n }\n },\n };\n}\n","/**\n * Event storage handler stub for @toon-protocol/sdk.\n *\n * This is a stub that throws. The real implementation lives in\n * `@toon-protocol/town` -- see `createEventStorageHandler` from that package.\n *\n * The SDK is the framework; Town is the relay implementation. SDK consumers\n * building relay functionality should use `@toon-protocol/town` directly.\n */\n\n/**\n * Creates an event storage handler.\n *\n * **Stub** -- throws \"not yet implemented\". See `@toon-protocol/town` for the\n * real relay implementation of this handler.\n *\n * @see {@link https://github.com/ALLiDoizCode/toon/tree/main/packages/town | @toon-protocol/town}\n */\nexport function createEventStorageHandler(_config: unknown): unknown {\n throw new Error(\n 'createEventStorageHandler is not yet implemented in @toon-protocol/sdk. ' +\n 'Use @toon-protocol/town for the relay implementation.'\n );\n}\n","/**\n * createNode() composition for @toon-protocol/sdk.\n *\n * Wires the full ILP packet processing pipeline:\n * shallow TOON parse -> Schnorr signature verification -> pricing validation -> handler dispatch\n *\n * Provides start() / stop() lifecycle management by delegating to\n * the core ToonNode composition (embedded mode) or manual HTTP\n * composition (standalone mode).\n *\n * ## Deployment Modes\n *\n * - **Embedded** (`connector`): Pass an EmbeddableConnectorLike directly.\n * Zero-latency packet delivery via direct function calls.\n * - **Standalone** (`connectorUrl` + `handlerPort`): Connect to an external\n * connector via HTTP. The SDK starts an HTTP server to receive packets.\n */\n\nimport { createServer, type Server as HttpServer } from 'node:http';\nimport type { NostrEvent } from 'nostr-tools/pure';\nimport type {\n EmbeddableConnectorLike,\n HandlePacketRequest,\n HandlePacketResponse,\n ConnectorChannelClient,\n} from '@toon-protocol/core';\nimport type {\n KnownPeer,\n BootstrapResult,\n BootstrapEventListener,\n} from '@toon-protocol/core';\nimport type { SettlementConfig } from '@toon-protocol/core';\nimport {\n createToonNode,\n BootstrapService,\n createDiscoveryTracker,\n createHttpIlpClient,\n createHttpConnectorAdmin,\n createHttpChannelClient,\n resolveChainConfig,\n buildIlpPrepare,\n buildJobFeedbackEvent,\n buildJobResultEvent,\n parseJobResult,\n} from '@toon-protocol/core';\nimport type { DvmJobStatus, IlpSendResult } from '@toon-protocol/core';\nimport type {\n IlpClient,\n ConnectorAdminClient,\n DiscoveryTracker,\n} from '@toon-protocol/core';\nimport {\n shallowParseToon,\n decodeEventFromToon,\n encodeEventToToon,\n} from '@toon-protocol/core/toon';\n\nimport type { SkillDescriptor } from '@toon-protocol/core';\nimport { fromSecretKey } from './identity.js';\nimport { HandlerRegistry, type Handler } from './handler-registry.js';\nimport { createHandlerContext } from './handler-context.js';\nimport { createVerificationPipeline } from './verification-pipeline.js';\nimport { createPricingValidator } from './pricing-validator.js';\nimport {\n buildSkillDescriptor,\n type BuildSkillDescriptorConfig,\n} from './skill-descriptor.js';\nimport { NodeError } from './errors.js';\n\n/**\n * Maximum base64-encoded payload size (in characters) accepted by the pipeline.\n * Defense-in-depth against DoS via oversized payloads. 1MB of base64 decodes\n * to ~750KB of raw TOON data, which is far beyond any legitimate Nostr event.\n * The pay-per-byte pricing model provides an additional economic disincentive.\n */\nconst MAX_PAYLOAD_BASE64_LENGTH = 1_048_576;\n\n/**\n * Configuration for creating a ServiceNode via createNode().\n *\n * Supports three deployment modes:\n * - **Default** (no connector args): Auto-creates an embedded ConnectorNode via\n * dynamic import. Requires `@toon-protocol/connector` as peer dependency.\n * - **Embedded** (`connector`): Pass a pre-configured EmbeddableConnectorLike.\n * - **Standalone** (`connectorUrl` + `handlerPort`): Connect to an external\n * connector via HTTP. The SDK starts an HTTP server to receive packets.\n *\n * Provide `connector` OR (`connectorUrl` + `handlerPort`), or neither (auto-create).\n */\nexport interface NodeConfig {\n /** 32-byte secp256k1 secret key */\n secretKey: Uint8Array;\n\n /** Chain preset name (default: 'anvil'). See resolveChainConfig(). */\n chain?: string;\n\n // --- Connector (optional — defaults to auto-created embedded connector) ---\n\n /**\n * Embedded connector instance for zero-latency mode.\n * If neither `connector` nor `connectorUrl` is provided, an embedded\n * ConnectorNode is auto-created (requires @toon-protocol/connector peer dep).\n */\n connector?: EmbeddableConnectorLike;\n /**\n * External connector admin URL for standalone mode (e.g., \"http://localhost:8081\").\n * Must be provided with `handlerPort`.\n */\n connectorUrl?: string;\n /**\n * Port for the HTTP server that receives ILP packets from the external connector.\n * Must be provided with `connectorUrl`.\n */\n handlerPort?: number;\n\n /**\n * BTP server port for the auto-created embedded connector (default: 3000).\n * Only used when neither `connector` nor `connectorUrl` is provided.\n */\n btpServerPort?: number;\n\n /**\n * EVM private key for settlement infrastructure.\n * Only used when auto-creating an embedded connector.\n * If not set, the identity's secp256k1 key is used.\n */\n settlementPrivateKey?: string;\n\n // --- Network ---\n\n /** ILP address (default: 'g.toon.local') */\n ilpAddress?: string;\n /** BTP endpoint URL advertised in kind:10032 announcements */\n btpEndpoint?: string;\n /** Asset code (default: 'USD') */\n assetCode?: string;\n /** Asset scale (default: 6) */\n assetScale?: number;\n /**\n * Base price per byte for pricing validation (default: 10n).\n *\n * Amounts are in USDC micro-units (6 decimals) for production.\n * Default 10n = 10 micro-USDC per byte = $0.00001/byte.\n * A 1KB event costs 10,240 micro-USDC = ~$0.01.\n */\n basePricePerByte?: bigint;\n /** Dev mode skips signature verification (default: false) */\n devMode?: boolean;\n /** TOON encoder function */\n toonEncoder?: (event: NostrEvent) => Uint8Array;\n /** TOON decoder function */\n toonDecoder?: (bytes: Uint8Array) => NostrEvent;\n /** Initial known peers for bootstrap */\n knownPeers?: KnownPeer[];\n /** Relay WebSocket URL */\n relayUrl?: string;\n /** Settlement info for peer registration */\n settlementInfo?: SettlementConfig;\n /** Enable ArDrive peer lookup */\n ardriveEnabled?: boolean;\n /** Per-kind pricing overrides */\n kindPricing?: Record<number, bigint>;\n /** Config-based handler registration (alternative to post-creation .on()) */\n handlers?: Record<number, Handler>;\n /** Config-based default handler (alternative to post-creation .onDefault()) */\n defaultHandler?: Handler;\n /**\n * Optional skill descriptor configuration overrides.\n * When DVM handlers are registered, these values override the auto-derived\n * defaults in the skill descriptor. See buildSkillDescriptor().\n */\n skillConfig?: Omit<\n BuildSkillDescriptorConfig,\n 'basePricePerByte' | 'kindPricing'\n >;\n}\n\n/**\n * Result returned by ServiceNode.start().\n */\nexport interface StartResult {\n /** Number of peers successfully bootstrapped */\n peerCount: number;\n /** Number of payment channels opened */\n channelCount: number;\n /** Detailed results from the bootstrap phase */\n bootstrapResults: BootstrapResult[];\n}\n\n/**\n * Result returned by ServiceNode.publishEvent().\n */\nexport interface PublishEventResult {\n success: boolean;\n eventId: string;\n fulfillment?: string;\n code?: string;\n message?: string;\n}\n\n/**\n * A fully wired TOON node with lifecycle management.\n */\nexport interface ServiceNode {\n /** Nostr x-only public key (32 bytes, 64 hex chars) */\n readonly pubkey: string;\n /** EVM address derived from the same secp256k1 key */\n readonly evmAddress: string;\n /** Pass-through to the underlying connector (null in standalone mode) */\n readonly connector: EmbeddableConnectorLike | null;\n /** Channel client (null if connector lacks channel support or in standalone mode) */\n readonly channelClient: ConnectorChannelClient | null;\n /** Register a handler for a specific event kind (builder pattern) */\n on(kind: number, handler: Handler): ServiceNode;\n /** Register a lifecycle event listener */\n on(event: 'bootstrap', listener: BootstrapEventListener): ServiceNode;\n /** Register a default handler for unrecognized kinds (builder pattern) */\n onDefault(handler: Handler): ServiceNode;\n /** Start the node: wire packet handler, run bootstrap, start discovery */\n start(): Promise<StartResult>;\n /** Stop the node: clean up lifecycle state */\n stop(): Promise<void>;\n /** Initiate peering with a discovered peer (register + settlement) */\n peerWith(pubkey: string): Promise<void>;\n /**\n * Publish a Nostr event to a remote peer via the embedded connector.\n *\n * TOON-encodes the event, computes payment amount, and sends as an\n * ILP PREPARE packet via the runtime client.\n *\n * @param event - The Nostr event to publish\n * @param options - Must include destination ILP address\n * @returns Result with success/failure info and event ID\n */\n publishEvent(\n event: NostrEvent,\n options?: { destination: string }\n ): Promise<PublishEventResult>;\n\n /**\n * Publish a Kind 7000 DVM job feedback event via ILP PREPARE.\n *\n * Builds a signed Kind 7000 feedback event with NIP-90 tags (e, p, status)\n * and delegates to publishEvent() for TOON encoding and ILP delivery.\n * Standard relay write fee applies: basePricePerByte * toonData.length.\n *\n * @param requestEventId - 64-char hex event ID of the original Kind 5xxx request\n * @param customerPubkey - 64-char hex pubkey of the customer who posted the request\n * @param status - Job status value ('processing', 'error', 'success', 'partial')\n * @param content - Optional status details or error message\n * @param options - Must include destination ILP address\n * @returns Result with success/failure info and event ID\n */\n publishFeedback(\n requestEventId: string,\n customerPubkey: string,\n status: DvmJobStatus,\n content?: string,\n options?: { destination: string }\n ): Promise<PublishEventResult>;\n\n /**\n * Publish a Kind 6xxx DVM job result event via ILP PREPARE.\n *\n * Builds a signed Kind 6xxx result event with NIP-90 tags (e, p, amount)\n * and delegates to publishEvent() for TOON encoding and ILP delivery.\n * Standard relay write fee applies: basePricePerByte * toonData.length.\n *\n * @param requestEventId - 64-char hex event ID of the original Kind 5xxx request\n * @param customerPubkey - 64-char hex pubkey of the customer who posted the request\n * @param amount - Compute cost in USDC micro-units as string\n * @param content - Result data (text, URL, etc.)\n * @param options - Must include destination; optional kind (default: 6100)\n * @returns Result with success/failure info and event ID\n */\n publishResult(\n requestEventId: string,\n customerPubkey: string,\n amount: string,\n content: string,\n options?: { destination: string; kind?: number }\n ): Promise<PublishEventResult>;\n\n /**\n * Returns the computed skill descriptor for this node's DVM capabilities.\n * Returns `undefined` if no DVM handlers (kinds 5000-5999) are registered.\n * The descriptor is computed from the handler registry and node config.\n */\n getSkillDescriptor(): SkillDescriptor | undefined;\n\n /**\n * Send an ILP payment to a provider for compute settlement.\n *\n * Extracts the compute cost from the result event's `amount` tag via\n * parseJobResult(), optionally validates against the original bid amount\n * (E5-R005 bid validation), and sends a pure ILP value transfer\n * (empty data field) to the provider's ILP address.\n *\n * This is a payment-only operation -- no TOON encoding, no relay write.\n * The payment routes through the ILP mesh using the same infrastructure\n * as relay write fees.\n *\n * @param resultEvent - Kind 6xxx result event with amount tag\n * @param providerIlpAddress - Provider's ILP address from kind:10035\n * @param options - Optional originalBid for bid validation (E5-R005)\n * @returns ILP send result with accepted/rejected status\n */\n settleCompute(\n resultEvent: NostrEvent,\n providerIlpAddress: string,\n options?: { originalBid?: string }\n ): Promise<IlpSendResult>;\n}\n\n/**\n * Creates a fully wired ServiceNode from configuration.\n *\n * The returned node has the full ILP packet processing pipeline wired in the\n * correct order:\n * 1. Shallow TOON parse (extract routing metadata)\n * 2. Schnorr signature verification (reject with F06 if invalid)\n * 3. Pricing validation (reject with F04 if underpaid)\n * 4. Handler dispatch (route to kind-specific or default handler)\n *\n * Supports two deployment modes:\n * - **Embedded** (`connector`): Uses createToonNode for zero-latency\n * packet delivery via direct function calls.\n * - **Standalone** (`connectorUrl` + `handlerPort`): Starts an HTTP server\n * to receive packets and uses HTTP clients for the connector API.\n *\n * Handlers can be registered via config or post-creation .on()/.onDefault().\n */\nexport function createNode(config: NodeConfig): ServiceNode {\n // 0. Validate connector mode\n const hasConnector = config.connector !== undefined;\n const hasConnectorUrl = config.connectorUrl !== undefined;\n\n if (hasConnector && hasConnectorUrl) {\n throw new NodeError(\n 'NodeConfig: provide either connector or connectorUrl, not both'\n );\n }\n if (hasConnectorUrl && config.handlerPort === undefined) {\n throw new NodeError(\n 'NodeConfig: handlerPort is required when using connectorUrl (standalone mode)'\n );\n }\n const autoCreateConnector = !hasConnector && !hasConnectorUrl;\n const embeddedMode = hasConnector || autoCreateConnector;\n\n // 0b. Resolve chain config and auto-populate settlementInfo if not set\n const chainConfig = resolveChainConfig(config.chain);\n let effectiveSettlementInfo = config.settlementInfo;\n if (!effectiveSettlementInfo) {\n const chainKey = `evm:base:${chainConfig.chainId}`;\n const supportedChains = [chainKey];\n const preferredTokens: Record<string, string> = {\n [chainKey]: chainConfig.usdcAddress,\n };\n const tokenNetworks: Record<string, string> | undefined =\n chainConfig.tokenNetworkAddress\n ? { [chainKey]: chainConfig.tokenNetworkAddress }\n : undefined;\n\n effectiveSettlementInfo = {\n supportedChains,\n preferredTokens,\n ...(tokenNetworks && { tokenNetworks }),\n };\n }\n\n // 1. Derive identity from secretKey\n let identity;\n try {\n identity = fromSecretKey(config.secretKey);\n } catch (error: unknown) {\n throw new NodeError(\n `Invalid secretKey: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n const { pubkey, evmAddress } = identity;\n\n // 2. Create handler registry\n const registry = new HandlerRegistry();\n\n // 3. Register config-based handlers (with kind validation matching node.on())\n if (config.handlers) {\n for (const [kind, handler] of Object.entries(config.handlers)) {\n const kindNum = Number(kind);\n if (!Number.isInteger(kindNum) || kindNum < 0) {\n throw new NodeError(\n `Invalid event kind in handlers config: expected a non-negative integer, got '${kind}'`\n );\n }\n registry.on(kindNum, handler);\n }\n }\n\n // 4. Register config-based default handler\n if (config.defaultHandler) {\n registry.onDefault(config.defaultHandler);\n }\n\n // 5. Create verification pipeline\n const verifier = createVerificationPipeline({\n devMode: config.devMode ?? false,\n });\n\n // 6. Create pricing validator\n const pricer = createPricingValidator({\n basePricePerByte: config.basePricePerByte ?? 10n,\n ownPubkey: pubkey,\n kindPricing: config.kindPricing,\n });\n\n // 7. Set up TOON codec with defaults\n const encoder = config.toonEncoder ?? encodeEventToToon;\n const decoder = config.toonDecoder ?? decodeEventFromToon;\n\n // Context decoder: converts base64 TOON string to NostrEvent\n const contextDecoder = (toon: string): NostrEvent => {\n const bytes = Buffer.from(toon, 'base64');\n return decoder(bytes);\n };\n\n // Mutable ref so the packet handler closure can access the tracker after it's created.\n const trackerRef: { current?: { processEvent(event: NostrEvent): void } } =\n {};\n\n // 8. Build the pipelined packet handler (Option A: directly on HandlePacketRequest)\n const pipelinedHandler = async (\n request: HandlePacketRequest\n ): Promise<HandlePacketResponse> => {\n // Step 0: Reject oversized payloads before allocating memory (DoS mitigation)\n if (request.data.length > MAX_PAYLOAD_BASE64_LENGTH) {\n return {\n accept: false,\n code: 'F06',\n message: `Payload too large: ${request.data.length} bytes exceeds maximum ${MAX_PAYLOAD_BASE64_LENGTH}`,\n };\n }\n\n // Step 1: Shallow TOON parse\n const toonBytes = Buffer.from(request.data, 'base64');\n let meta;\n try {\n meta = shallowParseToon(toonBytes);\n } catch {\n // Corrupted TOON data cannot be parsed -- reject as invalid payload\n return {\n accept: false,\n code: 'F06',\n message: 'Invalid TOON payload: failed to parse routing metadata',\n };\n }\n\n // Dev mode: log packet details\n // Sanitize user-controlled fields (amount, destination) to prevent log injection\n // via newlines or control characters. meta.kind (integer) and meta.pubkey (validated\n // hex) are safe; request.data preview is base64 (safe character set).\n if (config.devMode ?? false) {\n const toonPreview =\n request.data.length > 80\n ? request.data.substring(0, 80) + '...'\n : request.data;\n // eslint-disable-next-line no-control-regex\n const sanitize = (s: string) => s.replace(/[\\x00-\\x1f\\x7f]/g, '');\n console.log(\n '[toon:dev]',\n `kind=${meta.kind}`,\n `pubkey=${meta.pubkey.substring(0, 16)}...`,\n `amount=${sanitize(request.amount)}`,\n `dest=${sanitize(request.destination)}`,\n `toon=${toonPreview}`\n );\n }\n\n // Step 2: Verify signature\n const verifyResult = await verifier.verify(meta, request.data);\n if (!verifyResult.verified) {\n // VerificationResult.rejection is always set when verified=false per\n // createVerificationPipeline contract. Guard defensively anyway.\n if (verifyResult.rejection) {\n return verifyResult.rejection;\n }\n return { accept: false, code: 'F06', message: 'Verification failed' };\n }\n\n // Step 3: Validate pricing (skip in dev mode)\n let amount: bigint;\n try {\n amount = BigInt(request.amount);\n } catch {\n if (config.devMode ?? false) {\n amount = 0n;\n } else {\n return {\n accept: false,\n code: 'T00',\n message: 'Invalid payment amount',\n };\n }\n }\n if (!(config.devMode ?? false)) {\n const priceResult = pricer.validate(meta, amount);\n if (!priceResult.accepted) {\n // PricingValidationResult.rejection is always set when accepted=false per\n // createPricingValidator contract. Guard defensively anyway.\n if (priceResult.rejection) {\n return priceResult.rejection;\n }\n return {\n accept: false,\n code: 'F04',\n message: 'Pricing validation failed',\n };\n }\n }\n\n // Step 4: Build HandlerContext with real metadata\n const ctx = createHandlerContext({\n toon: request.data,\n meta,\n amount,\n destination: request.destination,\n toonDecoder: contextDecoder,\n });\n\n // Step 5: Dispatch to handler (T00 error boundary)\n try {\n const result = await registry.dispatch(ctx);\n\n // Feed accepted kind:10032 events to discovery tracker for peer discovery.\n // Uses late-binding reference since the tracker is created after this handler.\n if (result.accept && meta.kind === 10032 && trackerRef.current) {\n const decoded = ctx.decode();\n if (decoded) {\n trackerRef.current.processEvent(decoded);\n }\n }\n\n return result;\n } catch (err: unknown) {\n // Log only the error message, not the full error object (which may contain payload data)\n const errMsg = err instanceof Error ? err.message : 'Unknown error';\n console.error('Handler dispatch failed:', errMsg);\n return { accept: false, code: 'T00', message: 'Internal error' };\n }\n };\n\n // ILP info shared between both modes\n const ilpInfo = {\n ilpAddress: config.ilpAddress ?? 'g.toon.local',\n btpEndpoint: config.btpEndpoint ?? '',\n assetCode: config.assetCode ?? 'USD',\n assetScale: config.assetScale ?? 6,\n };\n\n // 9. Branch: embedded mode vs standalone mode\n let ilpClient: IlpClient;\n let adminClient: ConnectorAdminClient;\n let channelClient: ConnectorChannelClient | null = null;\n let bootstrapServiceInstance: BootstrapService;\n let discoveryTrackerInstance: DiscoveryTracker;\n let httpServer: HttpServer | null = null;\n\n // Lifecycle delegates (start/stop differ per mode)\n let doStart: () => Promise<{\n bootstrapResults: BootstrapResult[];\n peerCount: number;\n channelCount: number;\n }>;\n let doStop: () => Promise<void>;\n\n // Track auto-created connector for cleanup\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let autoCreatedConnector: any = null;\n\n if (embeddedMode && hasConnector) {\n // --- EMBEDDED MODE (user-provided connector): delegate to createToonNode ---\n const toonNode = createToonNode({\n connector: config.connector as NonNullable<typeof config.connector>,\n handlePacket: pipelinedHandler,\n secretKey: config.secretKey,\n ilpInfo,\n toonEncoder: encoder,\n toonDecoder: decoder,\n relayUrl: config.relayUrl,\n knownPeers: config.knownPeers,\n settlementInfo: effectiveSettlementInfo,\n basePricePerByte: config.basePricePerByte,\n ardriveEnabled: config.ardriveEnabled,\n });\n\n ilpClient = toonNode.ilpClient;\n channelClient = toonNode.channelClient;\n bootstrapServiceInstance = toonNode.bootstrapService;\n discoveryTrackerInstance = toonNode.discoveryTracker;\n adminClient = {\n addPeer: () => Promise.resolve(),\n removePeer: () => Promise.resolve(),\n };\n\n trackerRef.current = discoveryTrackerInstance;\n\n doStart = async () => toonNode.start();\n doStop = async () => toonNode.stop();\n } else if (autoCreateConnector) {\n // --- AUTO-CREATE EMBEDDED MODE: deferred ConnectorNode creation in doStart() ---\n\n // Create placeholder bootstrap/discovery instances (wired during start)\n bootstrapServiceInstance = new BootstrapService(\n {\n knownPeers: config.knownPeers ?? [],\n ardriveEnabled: config.ardriveEnabled ?? false,\n defaultRelayUrl: config.relayUrl ?? '',\n settlementInfo: effectiveSettlementInfo,\n ownIlpAddress: ilpInfo.ilpAddress,\n toonEncoder: encoder,\n toonDecoder: decoder,\n basePricePerByte: config.basePricePerByte ?? 10n,\n },\n config.secretKey,\n ilpInfo\n );\n\n discoveryTrackerInstance = createDiscoveryTracker({\n secretKey: config.secretKey,\n settlementInfo: effectiveSettlementInfo,\n });\n\n // Placeholder clients (replaced during start)\n ilpClient = {\n sendIlpPacket: () =>\n Promise.reject(new NodeError('Node not started. Call start() first.')),\n };\n adminClient = {\n addPeer: () => Promise.resolve(),\n removePeer: () => Promise.resolve(),\n };\n\n trackerRef.current = discoveryTrackerInstance;\n\n doStart = async () => {\n // Dynamic import to keep @toon-protocol/connector as optional peer dep\n let ConnectorNodeClass: typeof import('@toon-protocol/connector').ConnectorNode;\n let createConnectorLogger: typeof import('@toon-protocol/connector').createLogger;\n try {\n const mod = await import('@toon-protocol/connector');\n ConnectorNodeClass = mod.ConnectorNode;\n createConnectorLogger = mod.createLogger;\n } catch {\n throw new NodeError(\n 'Auto-create connector requires @toon-protocol/connector as a peer dependency. ' +\n 'Install it with: pnpm add @toon-protocol/connector'\n );\n }\n\n const nodeId = `toon-${pubkey.slice(0, 16)}`;\n const btpServerPort = config.btpServerPort ?? 3000;\n const connectorLogger = createConnectorLogger(nodeId, 'warn');\n\n // Derive settlement private key from identity if not explicitly set\n let settlementPrivateKey = config.settlementPrivateKey;\n if (!settlementPrivateKey) {\n const keyBuffer = Buffer.from(config.secretKey);\n settlementPrivateKey = `0x${keyBuffer.toString('hex')}`;\n keyBuffer.fill(0);\n }\n\n const hasSettlementAddresses =\n chainConfig.registryAddress && chainConfig.tokenNetworkAddress;\n\n autoCreatedConnector = new ConnectorNodeClass(\n {\n nodeId,\n btpServerPort,\n environment: 'development' as const,\n deploymentMode: 'embedded' as const,\n peers: [],\n routes: [],\n localDelivery: { enabled: false },\n ...(hasSettlementAddresses && {\n settlementInfra: {\n enabled: true,\n rpcUrl: chainConfig.rpcUrl,\n registryAddress: chainConfig.registryAddress,\n tokenAddress: chainConfig.usdcAddress,\n privateKey: settlementPrivateKey,\n },\n }),\n },\n connectorLogger\n );\n\n await autoCreatedConnector.start();\n\n // Now wire the real connector into createToonNode\n const connector = autoCreatedConnector as unknown as EmbeddableConnectorLike;\n const toonNode = createToonNode({\n connector,\n handlePacket: pipelinedHandler,\n secretKey: config.secretKey,\n ilpInfo,\n toonEncoder: encoder,\n toonDecoder: decoder,\n relayUrl: config.relayUrl,\n knownPeers: config.knownPeers,\n settlementInfo: effectiveSettlementInfo,\n basePricePerByte: config.basePricePerByte,\n ardriveEnabled: config.ardriveEnabled,\n });\n\n // Replace placeholder clients with real ones\n ilpClient = toonNode.ilpClient;\n channelClient = toonNode.channelClient;\n\n // Wire bootstrap and discovery with the real admin client\n bootstrapServiceInstance.setIlpClient(toonNode.ilpClient);\n bootstrapServiceInstance.setConnectorAdmin({\n addPeer: () => Promise.resolve(),\n removePeer: () => Promise.resolve(),\n });\n if (toonNode.channelClient) {\n bootstrapServiceInstance.setChannelClient(toonNode.channelClient);\n }\n\n discoveryTrackerInstance.setConnectorAdmin({\n addPeer: () => Promise.resolve(),\n removePeer: () => Promise.resolve(),\n });\n if (toonNode.channelClient) {\n discoveryTrackerInstance.setChannelClient(toonNode.channelClient);\n }\n\n trackerRef.current = discoveryTrackerInstance;\n\n // Add self-route\n autoCreatedConnector.addRoute({\n prefix: ilpInfo.ilpAddress,\n nextHop: nodeId,\n priority: 100,\n });\n\n // Start the toonNode (wires packet handler, runs bootstrap)\n const result = await toonNode.start();\n return result;\n };\n\n doStop = async () => {\n if (autoCreatedConnector) {\n await autoCreatedConnector.stop();\n autoCreatedConnector = null;\n }\n };\n } else {\n // --- STANDALONE MODE: manual composition with HTTP clients ---\n const connectorUrl = config.connectorUrl as string;\n const handlerPort = config.handlerPort as number;\n\n ilpClient = createHttpIlpClient(connectorUrl);\n adminClient = createHttpConnectorAdmin(connectorUrl, '');\n\n // Channel client via HTTP if settlement is configured\n if (effectiveSettlementInfo) {\n channelClient = createHttpChannelClient(connectorUrl);\n }\n\n // Create BootstrapService\n bootstrapServiceInstance = new BootstrapService(\n {\n knownPeers: config.knownPeers ?? [],\n ardriveEnabled: config.ardriveEnabled ?? false,\n defaultRelayUrl: config.relayUrl ?? '',\n settlementInfo: effectiveSettlementInfo,\n ownIlpAddress: ilpInfo.ilpAddress,\n toonEncoder: encoder,\n toonDecoder: decoder,\n basePricePerByte: config.basePricePerByte ?? 10n,\n },\n config.secretKey,\n ilpInfo\n );\n\n bootstrapServiceInstance.setIlpClient(ilpClient);\n bootstrapServiceInstance.setConnectorAdmin(adminClient);\n if (channelClient) {\n bootstrapServiceInstance.setChannelClient(channelClient);\n }\n\n // Create DiscoveryTracker\n discoveryTrackerInstance = createDiscoveryTracker({\n secretKey: config.secretKey,\n settlementInfo: effectiveSettlementInfo,\n });\n discoveryTrackerInstance.setConnectorAdmin(adminClient);\n if (channelClient) {\n discoveryTrackerInstance.setChannelClient(channelClient);\n }\n\n trackerRef.current = discoveryTrackerInstance;\n\n doStart = async () => {\n // Start HTTP server for receiving ILP packets\n httpServer = createServer(async (req, res) => {\n if (req.method === 'GET' && req.url === '/health') {\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ status: 'healthy', pubkey }));\n return;\n }\n\n if (req.method === 'POST' && req.url === '/handle-packet') {\n let body = '';\n req.on('data', (chunk: Buffer) => {\n body += chunk.toString();\n });\n req.on('end', async () => {\n try {\n const request = JSON.parse(body) as HandlePacketRequest;\n if (\n request.amount === undefined ||\n request.amount === null ||\n request.destination === undefined ||\n request.destination === null ||\n request.data === undefined ||\n request.data === null\n ) {\n res.writeHead(400, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n accept: false,\n code: 'F00',\n message: 'Missing required fields',\n })\n );\n return;\n }\n const result = await pipelinedHandler(request);\n res.writeHead(result.accept ? 200 : 400, {\n 'Content-Type': 'application/json',\n });\n res.end(JSON.stringify(result));\n } catch (err: unknown) {\n console.error('[SDK] handle-packet error:', err);\n res.writeHead(500, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n accept: false,\n code: 'T00',\n message: 'Internal server error',\n })\n );\n }\n });\n return;\n }\n\n res.writeHead(404);\n res.end();\n });\n\n await new Promise<void>((resolve) => {\n httpServer?.listen(handlerPort, () => resolve());\n });\n\n // Run bootstrap\n const results = await bootstrapServiceInstance.bootstrap();\n const bootstrapPeerPubkeys = results.map((r) => r.knownPeer.pubkey);\n discoveryTrackerInstance.addExcludedPubkeys(bootstrapPeerPubkeys);\n\n return {\n bootstrapResults: results,\n peerCount: results.length,\n channelCount: results.filter((r) => r.channelId).length,\n };\n };\n\n doStop = async () => {\n const server = httpServer;\n if (server) {\n await new Promise<void>((resolve) => {\n server.close(() => resolve());\n });\n httpServer = null;\n }\n };\n }\n\n // 10. Track SDK-level lifecycle state\n let started = false;\n\n // 11. Build and return ServiceNode\n const node: ServiceNode = {\n get pubkey() {\n return pubkey;\n },\n get evmAddress() {\n return evmAddress;\n },\n get connector() {\n return config.connector ?? (autoCreatedConnector as unknown as EmbeddableConnectorLike) ?? null;\n },\n get channelClient() {\n return channelClient;\n },\n\n getSkillDescriptor(): SkillDescriptor | undefined {\n return buildSkillDescriptor(registry, {\n basePricePerByte: config.basePricePerByte ?? 10n,\n kindPricing: config.kindPricing,\n ...config.skillConfig,\n });\n },\n\n on(\n kindOrEvent: number | string,\n handlerOrListener: Handler | BootstrapEventListener\n ): ServiceNode {\n if (typeof kindOrEvent === 'number') {\n // Handler registration (existing behavior)\n if (!Number.isInteger(kindOrEvent) || kindOrEvent < 0) {\n throw new NodeError(\n `Invalid event kind: expected a non-negative integer, got ${String(kindOrEvent)}`\n );\n }\n registry.on(kindOrEvent, handlerOrListener as Handler);\n } else if (kindOrEvent === 'bootstrap') {\n // Lifecycle event listener -- forward to bootstrapService AND discoveryTracker\n const listener = handlerOrListener as BootstrapEventListener;\n bootstrapServiceInstance.on(listener);\n discoveryTrackerInstance.on(listener);\n } else {\n // Sanitize event name to prevent log injection via control characters\n // eslint-disable-next-line no-control-regex\n const sanitized = String(kindOrEvent).replace(/[\\x00-\\x1f\\x7f]/g, '');\n throw new NodeError(\n `Unknown lifecycle event: '${sanitized}'. Supported: 'bootstrap'`\n );\n }\n return node;\n },\n\n onDefault(handler: Handler): ServiceNode {\n registry.onDefault(handler);\n return node;\n },\n\n async start(): Promise<StartResult> {\n if (started) {\n throw new NodeError('Node already started');\n }\n\n try {\n const result = await doStart();\n started = true;\n return {\n peerCount: result.peerCount,\n channelCount: result.channelCount,\n bootstrapResults: result.bootstrapResults,\n };\n } catch (error: unknown) {\n if (error instanceof NodeError) {\n throw error;\n }\n throw new NodeError(\n `Failed to start node: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n },\n\n async stop(): Promise<void> {\n if (!started) {\n return; // No-op if not started\n }\n\n await doStop();\n started = false;\n },\n\n async peerWith(targetPubkey: string): Promise<void> {\n if (!started) {\n throw new NodeError(\n 'Cannot peer: node not started. Call start() first.'\n );\n }\n // Defense-in-depth: validate pubkey format before delegating to core\n if (\n typeof targetPubkey !== 'string' ||\n targetPubkey.length !== 64 ||\n !/^[0-9a-f]{64}$/.test(targetPubkey)\n ) {\n throw new NodeError(\n 'Invalid pubkey: expected a 64-character lowercase hex string'\n );\n }\n return discoveryTrackerInstance.peerWith(targetPubkey);\n },\n\n async publishEvent(\n event: NostrEvent,\n options?: { destination: string }\n ): Promise<PublishEventResult> {\n // Guard: node must be started\n if (!started) {\n throw new NodeError(\n 'Cannot publish: node not started. Call start() first.'\n );\n }\n\n // Guard: destination is required\n if (!options?.destination) {\n throw new NodeError(\n \"Cannot publish: destination is required. Pass { destination: 'g.peer.address' }.\"\n );\n }\n\n try {\n // TOON-encode the event\n const toonData = encoder(event);\n\n // Compute amount: basePricePerByte * toonData.length\n const amount =\n (config.basePricePerByte ?? 10n) * BigInt(toonData.length);\n\n // Build ILP PREPARE packet using shared construction (packet equivalence\n // with x402 rail -- the destination relay cannot distinguish between\n // packets sent via publishEvent() and the x402 /publish endpoint).\n const packet = buildIlpPrepare({\n destination: options.destination,\n amount,\n data: toonData,\n });\n\n // Send via ILP client\n const result = await ilpClient.sendIlpPacket(packet);\n\n // Map IlpSendResult to PublishEventResult\n if (result.accepted) {\n return {\n success: true,\n eventId: event.id,\n fulfillment: result.fulfillment ?? '',\n };\n }\n\n return {\n success: false,\n eventId: event.id,\n code: result.code ?? 'T00',\n message: result.message ?? 'Unknown error',\n };\n } catch (error: unknown) {\n // Propagate NodeError directly\n if (error instanceof NodeError) {\n throw error;\n }\n throw new NodeError(\n `Failed to publish event: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n },\n\n async publishFeedback(\n requestEventId: string,\n customerPubkey: string,\n status: DvmJobStatus,\n content?: string,\n options?: { destination: string }\n ): Promise<PublishEventResult> {\n // Build Kind 7000 feedback event using provider's secretKey\n const feedbackEvent = buildJobFeedbackEvent(\n { requestEventId, customerPubkey, status, content },\n config.secretKey\n );\n\n // Delegate to publishEvent() for TOON encoding, pricing, and ILP delivery\n return node.publishEvent(feedbackEvent, options);\n },\n\n async publishResult(\n requestEventId: string,\n customerPubkey: string,\n amount: string,\n content: string,\n options?: { destination: string; kind?: number }\n ): Promise<PublishEventResult> {\n // Default result kind is 6100 (text generation result = 5100 + 1000)\n const resultKind = options?.kind ?? 6100;\n\n // Build Kind 6xxx result event using provider's secretKey\n const resultEvent = buildJobResultEvent(\n { kind: resultKind, requestEventId, customerPubkey, amount, content },\n config.secretKey\n );\n\n // Delegate to publishEvent() for TOON encoding, pricing, and ILP delivery\n return node.publishEvent(resultEvent, options);\n },\n\n async settleCompute(\n resultEvent: NostrEvent,\n providerIlpAddress: string,\n options?: { originalBid?: string }\n ): Promise<IlpSendResult> {\n // Guard: node must be started\n if (!started) {\n throw new NodeError(\n 'Cannot settle compute: node not started. Call start() first.'\n );\n }\n\n // Guard: providerIlpAddress must be a non-empty, non-whitespace string\n if (\n !providerIlpAddress ||\n typeof providerIlpAddress !== 'string' ||\n providerIlpAddress.trim() === ''\n ) {\n throw new NodeError(\n \"Cannot settle compute: providerIlpAddress is required. Resolve it from the provider's kind:10035 service discovery event.\"\n );\n }\n\n // Extract amount from result event via parseJobResult()\n const parsed = parseJobResult(resultEvent);\n if (!parsed) {\n throw new NodeError(\n 'Cannot settle compute: failed to parse result event. Ensure the event is a valid Kind 6xxx with an amount tag.'\n );\n }\n\n const computeAmount = parsed.amount;\n\n // Validate computeAmount is a valid non-negative numeric string before any\n // further processing. Without this, a non-numeric amount would propagate to\n // ilpClient.sendIlpPacket() and cause a BootstrapError instead of a clear\n // NodeError. Negative amounts would cause undefined behavior in the ILP layer.\n let amountBigInt: bigint;\n try {\n amountBigInt = BigInt(computeAmount);\n } catch {\n throw new NodeError(\n `Cannot settle compute: result event amount ('${computeAmount}') is not a valid numeric string.`\n );\n }\n if (amountBigInt < 0n) {\n throw new NodeError(\n `Cannot settle compute: result event amount ('${computeAmount}') must be non-negative.`\n );\n }\n\n // E5-R005 bid validation: if originalBid is provided, validate amount <= bid\n if (options?.originalBid !== undefined) {\n let bidBigInt: bigint;\n try {\n bidBigInt = BigInt(options.originalBid);\n } catch {\n throw new NodeError(\n `Cannot settle compute: originalBid ('${options.originalBid}') is not a valid numeric string.`\n );\n }\n if (amountBigInt > bidBigInt) {\n throw new NodeError(\n `Cannot settle compute: result amount (${computeAmount}) exceeds original bid (${options.originalBid}). Potential provider overcharge.`\n );\n }\n }\n\n // Send pure ILP value transfer (empty data = no event payload)\n // This is a payment-only operation -- no TOON encoding, no relay write.\n return ilpClient.sendIlpPacket({\n destination: providerIlpAddress,\n amount: computeAmount,\n data: '',\n });\n },\n };\n\n return node;\n}\n","/**\n * Skill descriptor builder for DVM service discovery.\n *\n * Computes a SkillDescriptor from the handler registry and node configuration.\n * The descriptor is embedded in kind:10035 events to advertise DVM capabilities.\n *\n * Returns `undefined` when no DVM handlers are registered (backward compatible\n * with pre-DVM kind:10035 events).\n */\n\nimport type { SkillDescriptor } from '@toon-protocol/core';\nimport type { HandlerRegistry } from './handler-registry.js';\n\n/** Configuration for building a skill descriptor. */\nexport interface BuildSkillDescriptorConfig {\n /** Base price per byte in USDC micro-units (default: 10n). */\n basePricePerByte?: bigint;\n /** Per-kind pricing overrides (kind number -> price in USDC micro-units). */\n kindPricing?: Record<number, bigint>;\n /** Service name override (default: 'toon-dvm'). */\n name?: string;\n /** Schema version override (default: '1.0'). */\n version?: string;\n /** Feature list override. */\n features?: string[];\n /** JSON Schema draft-07 object for job request parameters. */\n inputSchema?: Record<string, unknown>;\n /** Available AI models. */\n models?: string[];\n}\n\n/**\n * Builds a SkillDescriptor from the handler registry and configuration.\n *\n * Returns `undefined` when the registry has no DVM handlers (kinds 5000-5999).\n * When DVM kinds exist, auto-populates the descriptor:\n * - `kinds` from `registry.getDvmKinds()`\n * - `pricing` from `config.kindPricing` overrides or `config.basePricePerByte` fallback\n * - `name`, `version`, `features`, `inputSchema`, `models` from config or defaults\n *\n * @param registry - The handler registry to read DVM kinds from.\n * @param config - Node configuration for pricing and optional overrides.\n * @returns A SkillDescriptor, or undefined if no DVM handlers are registered.\n */\nexport function buildSkillDescriptor(\n registry: HandlerRegistry,\n config: BuildSkillDescriptorConfig = {}\n): SkillDescriptor | undefined {\n const dvmKinds = registry.getDvmKinds();\n if (dvmKinds.length === 0) {\n return undefined;\n }\n\n const basePricePerByte = config.basePricePerByte ?? 10n;\n const kindPricing = config.kindPricing ?? {};\n\n // Derive pricing: per-kind overrides or basePricePerByte fallback\n const pricing: Record<string, string> = {};\n for (const kind of dvmKinds) {\n if (Object.hasOwn(kindPricing, kind)) {\n pricing[String(kind)] = String(kindPricing[kind]);\n } else {\n pricing[String(kind)] = String(basePricePerByte);\n }\n }\n\n const descriptor: SkillDescriptor = {\n name: config.name ?? 'toon-dvm',\n version: config.version ?? '1.0',\n kinds: dvmKinds,\n features: config.features ?? [],\n inputSchema: config.inputSchema ?? { type: 'object', properties: {} },\n pricing,\n };\n\n if (config.models !== undefined) {\n descriptor.models = config.models;\n }\n\n return descriptor;\n}\n"],"mappings":";AAWA;AAAA,EACE,oBAAoB;AAAA,EACpB;AAAA,EACA;AAAA,OACK;AACP,SAAS,gBAAgB;AACzB,SAAS,aAAa;AACtB,SAAS,oBAAoB;AAC7B,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAC3B,SAAS,kBAAkB;;;AChB3B,SAAS,iBAAiB;AAMnB,IAAM,gBAAN,cAA4B,UAAU;AAAA,EAC3C,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,kBAAkB,KAAK;AACtC,SAAK,OAAO;AAAA,EACd;AACF;AAMO,IAAM,YAAN,cAAwB,UAAU;AAAA,EACvC,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,cAAc,KAAK;AAClC,SAAK,OAAO;AAAA,EACd;AACF;AAMO,IAAM,eAAN,cAA2B,UAAU;AAAA,EAC1C,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,iBAAiB,KAAK;AACrC,SAAK,OAAO;AAAA,EACd;AACF;AAMO,IAAM,oBAAN,cAAgC,UAAU;AAAA,EAC/C,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,sBAAsB,KAAK;AAC1C,SAAK,OAAO;AAAA,EACd;AACF;AAMO,IAAM,eAAN,cAA2B,UAAU;AAAA,EAC1C,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,iBAAiB,KAAK;AACrC,SAAK,OAAO;AAAA,EACd;AACF;;;ADXO,SAAS,mBAA2B;AACzC,SAAO,kBAAkB,UAAU,GAAG;AACxC;AAYO,SAAS,aACd,UACA,S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D,OAAO,uBAAuB,YAC9B,mBAAmB,KAAK,MAAM,IAC9B;AACA,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAGA,YAAM,SAAS,eAAe,WAAW;AACzC,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAEA,YAAM,gBAAgB,OAAO;AAM7B,UAAI;AACJ,UAAI;AACF,uBAAe,OAAO,aAAa;AAAA,MACrC,QAAQ;AACN,cAAM,IAAI;AAAA,UACR,gDAAgD,aAAa;AAAA,QAC/D;AAAA,MACF;AACA,UAAI,eAAe,IAAI;AACrB,cAAM,IAAI;AAAA,UACR,gDAAgD,aAAa;AAAA,QAC/D;AAAA,MACF;AAGA,UAAI,SAAS,gBAAgB,QAAW;AACtC,YAAI;AACJ,YAAI;AACF,sBAAY,OAAO,QAAQ,WAAW;AAAA,QACxC,QAAQ;AACN,gBAAM,IAAI;AAAA,YACR,wCAAwC,QAAQ,WAAW;AAAA,UAC7D;AAAA,QACF;AACA,YAAI,eAAe,WAAW;AAC5B,gBAAM,IAAI;AAAA,YACR,yCAAyC,aAAa,2BAA2B,QAAQ,WAAW;AAAA,UACtG;AAAA,QACF;AAAA,MACF;AAIA,aAAO,UAAU,cAAc;AAAA,QAC7B,aAAa;AAAA,QACb,QAAQ;AAAA,QACR,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/identity.ts","../src/errors.ts","../src/handler-context.ts","../src/handler-registry.ts","../src/pricing-validator.ts","../src/verification-pipeline.ts","../src/payment-handler-bridge.ts","../src/event-storage-handler.ts","../src/create-node.ts","../src/skill-descriptor.ts","../src/workflow-orchestrator.ts","../src/swarm-coordinator.ts","../src/prefix-claim-handler.ts","../src/arweave/arweave-dvm-handler.ts","../src/arweave/turbo-adapter.ts","../src/arweave/chunk-manager.ts","../src/arweave/chunked-upload.ts"],"sourcesContent":["/**\n * Unified identity module for @toon-protocol/sdk.\n *\n * Derives both a Nostr pubkey (x-only Schnorr, BIP-340) and an EVM address\n * (Keccak-256) from a single secp256k1 private key, following the NIP-06\n * derivation standard.\n *\n * Both Nostr and EVM use the secp256k1 elliptic curve, so a single 12-word\n * seed phrase can recover the complete identity across both layers.\n */\n\nimport {\n generateMnemonic as _generateMnemonic,\n validateMnemonic,\n mnemonicToSeedSync,\n} from '@scure/bip39';\nimport { wordlist } from '@scure/bip39/wordlists/english.js';\nimport { HDKey } from '@scure/bip32';\nimport { getPublicKey } from 'nostr-tools/pure';\nimport { secp256k1 } from '@noble/curves/secp256k1.js';\nimport { keccak_256 } from '@noble/hashes/sha3.js';\nimport { bytesToHex } from '@noble/hashes/utils.js';\nimport { IdentityError } from './errors.js';\n\n/**\n * Represents a node's complete identity: secret key, Nostr pubkey, and EVM address.\n */\nexport interface NodeIdentity {\n /** The 32-byte secp256k1 secret key. */\n secretKey: Uint8Array;\n /** The x-only Schnorr public key (32 bytes, 64 lowercase hex characters). */\n pubkey: string;\n /** The EIP-55 checksummed EVM address (0x-prefixed, 42 characters). */\n evmAddress: string;\n}\n\n/**\n * Options for mnemonic-based key derivation.\n */\nexport interface FromMnemonicOptions {\n /** Key index in the NIP-06 derivation path. Defaults to 0. */\n accountIndex?: number;\n}\n\n/**\n * Generates a valid 12-word BIP-39 mnemonic using 128-bit entropy.\n *\n * @returns A space-separated string of 12 BIP-39 English words.\n */\nexport function generateMnemonic(): string {\n return _generateMnemonic(wordlist, 128);\n}\n\n/**\n * Derives a complete NodeIdentity from a BIP-39 mnemonic phrase.\n *\n * Uses the NIP-06 derivation path: m/44'/1237'/0'/0/{accountIndex}\n *\n * @param mnemonic - A valid BIP-39 mnemonic (12 or 24 words).\n * @param options - Optional derivation options (accountIndex defaults to 0).\n * @returns The derived NodeIdentity with secretKey, pubkey, and evmAddress.\n * @throws {IdentityError} If the mnemonic is invalid.\n */\nexport function fromMnemonic(\n mnemonic: string,\n options?: FromMnemonicOptions\n): NodeIdentity {\n if (!validateMnemonic(mnemonic, wordlist)) {\n throw new IdentityError(\n `Invalid BIP-39 mnemonic: the provided words do not form a valid mnemonic phrase`\n );\n }\n\n const accountIndex = options?.accountIndex ?? 0;\n\n if (\n !Number.isInteger(accountIndex) ||\n accountIndex < 0 ||\n accountIndex > MAX_BIP32_INDEX\n ) {\n throw new IdentityError(\n `Invalid accountIndex: expected a non-negative integer (0 to ${MAX_BIP32_INDEX}), got ${String(accountIndex)}`\n );\n }\n\n const path = `m/44'/1237'/0'/0/${accountIndex}`;\n\n let seed: Uint8Array | undefined;\n try {\n seed = mnemonicToSeedSync(mnemonic);\n const hdKey = HDKey.fromMasterSeed(seed).derive(path);\n\n if (!hdKey.privateKey) {\n throw new IdentityError(`Failed to derive private key at path ${path}`);\n }\n\n const secretKey = hdKey.privateKey;\n return deriveIdentity(secretKey);\n } catch (error: unknown) {\n if (error instanceof IdentityError) {\n throw error;\n }\n throw new IdentityError(\n `Key derivation failed at path ${path}: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n } finally {\n // Best-effort zeroing of the intermediate seed to reduce the window\n // during which sensitive material remains in memory. This is not a\n // guarantee (JS has no secure-erase primitive), but it limits exposure.\n if (seed) {\n seed.fill(0);\n }\n }\n}\n\n/**\n * Derives a complete NodeIdentity from an existing 32-byte secret key.\n *\n * @param secretKey - A 32-byte secp256k1 secret key.\n * @returns The derived NodeIdentity with secretKey, pubkey, and evmAddress.\n * @throws {IdentityError} If the secret key is not exactly 32 bytes.\n */\nexport function fromSecretKey(secretKey: Uint8Array): NodeIdentity {\n if (!(secretKey instanceof Uint8Array)) {\n throw new IdentityError(\n `Invalid secret key: expected Uint8Array, got ${secretKey === null ? 'null' : typeof secretKey}`\n );\n }\n\n if (secretKey.length !== 32) {\n throw new IdentityError(\n `Invalid secret key: expected 32 bytes, got ${secretKey.length} bytes`\n );\n }\n\n try {\n return deriveIdentity(secretKey);\n } catch (error: unknown) {\n if (error instanceof IdentityError) {\n throw error;\n }\n throw new IdentityError(\n `Invalid secret key: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n}\n\n/**\n * Maximum valid BIP-32 non-hardened child index (2^31 - 1).\n * Values at or above 2^31 are reserved for hardened derivation.\n */\nconst MAX_BIP32_INDEX = 0x7fffffff;\n\n/**\n * Shared helper that computes pubkey and evmAddress from a 32-byte secret key.\n * Returns a defensive copy of the secret key to prevent external mutation.\n */\nfunction deriveIdentity(secretKey: Uint8Array): NodeIdentity {\n // Compute x-only Schnorr pubkey (32 bytes hex) via nostr-tools\n const pubkey = getPublicKey(secretKey);\n\n // Compute EVM address from the uncompressed secp256k1 public key\n const evmAddress = computeEvmAddress(secretKey);\n\n // Return a defensive copy of the secret key to prevent external mutation\n // from affecting the identity or vice versa\n return { secretKey: new Uint8Array(secretKey), pubkey, evmAddress };\n}\n\n/**\n * Computes an EIP-55 checksummed EVM address from a secp256k1 private key.\n *\n * Steps:\n * 1. Compute the full uncompressed public key (65 bytes: 0x04 + 64 bytes X,Y)\n * 2. Strip the 0x04 prefix to get 64 bytes\n * 3. Hash with Keccak-256\n * 4. Take the last 20 bytes\n * 5. Format as 0x-prefixed hex with EIP-55 checksum\n */\nfunction computeEvmAddress(secretKey: Uint8Array): string {\n // Get the uncompressed public key (65 bytes: 0x04 prefix + 64 bytes X,Y)\n const uncompressedPubkey = secp256k1.getPublicKey(secretKey, false);\n\n // Strip the 0x04 prefix (first byte), leaving 64 bytes of X,Y coordinates\n const pubkeyWithoutPrefix = uncompressedPubkey.slice(1);\n\n // Hash with Keccak-256\n const hash = keccak_256(pubkeyWithoutPrefix);\n\n // Take last 20 bytes\n const addressBytes = hash.slice(-20);\n const addressHex = bytesToHex(addressBytes);\n\n // Apply EIP-55 mixed-case checksum\n return toChecksumAddress(addressHex);\n}\n\n/**\n * Applies EIP-55 mixed-case checksum encoding to an Ethereum address.\n *\n * EIP-55 rules:\n * - Hash the lowercase hex address (without 0x) with Keccak-256\n * - For each hex character in the address:\n * - If the corresponding nibble in the hash is >= 8, uppercase it\n * - Otherwise, lowercase it\n *\n * @param addressHex - The 40-character lowercase hex address (without 0x prefix).\n * @returns The checksummed address with 0x prefix.\n */\nfunction toChecksumAddress(addressHex: string): string {\n const lower = addressHex.toLowerCase();\n const hash = bytesToHex(keccak_256(new TextEncoder().encode(lower)));\n\n let checksummed = '0x';\n for (let i = 0; i < 40; i++) {\n const char = lower[i];\n const hashChar = hash[i];\n if (char === undefined || hashChar === undefined) {\n throw new IdentityError(\n `Unexpected undefined at index ${i} during checksum computation`\n );\n }\n const hashNibble = parseInt(hashChar, 16);\n checksummed += hashNibble >= 8 ? char.toUpperCase() : char;\n }\n\n return checksummed;\n}\n","/**\n * SDK-specific error classes for @toon-protocol/sdk.\n * All errors extend ToonError from @toon-protocol/core for a consistent error hierarchy.\n */\n\nimport { ToonError } from '@toon-protocol/core';\n\n/**\n * Error thrown when identity operations fail.\n * Used for invalid mnemonics, invalid secret keys, and key derivation failures.\n */\nexport class IdentityError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'IDENTITY_ERROR', cause);\n this.name = 'IdentityError';\n }\n}\n\n/**\n * Error thrown when node lifecycle operations fail.\n * Used for start/stop failures, configuration errors, etc.\n */\nexport class NodeError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'NODE_ERROR', cause);\n this.name = 'NodeError';\n }\n}\n\n/**\n * Error thrown when handler dispatch operations fail.\n * Used for handler registration conflicts, missing handlers, and dispatch errors.\n */\nexport class HandlerError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'HANDLER_ERROR', cause);\n this.name = 'HandlerError';\n }\n}\n\n/**\n * Error thrown when Schnorr signature verification fails.\n * Used for invalid signatures, malformed events, and verification pipeline errors.\n */\nexport class VerificationError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'VERIFICATION_ERROR', cause);\n this.name = 'VerificationError';\n }\n}\n\n/**\n * Error thrown when payment validation fails.\n * Used for pricing calculation errors, insufficient payment, and pricing policy violations.\n */\nexport class PricingError extends ToonError {\n constructor(message: string, cause?: Error) {\n super(message, 'PRICING_ERROR', cause);\n this.name = 'PricingError';\n }\n}\n","/**\n * Handler context for @toon-protocol/sdk.\n *\n * Provides a context object passed to kind-based handlers with methods\n * for accessing TOON data, shallow-parsed metadata, and accept/reject actions.\n */\n\nimport type { NostrEvent } from 'nostr-tools/pure';\nimport type { ToonRoutingMeta } from '@toon-protocol/core/toon';\nimport type {\n HandlePacketAcceptResponse,\n HandlePacketRejectResponse,\n} from '@toon-protocol/core';\n\n// Re-export core response types so SDK consumers don't need to import from core\nexport type { HandlePacketAcceptResponse, HandlePacketRejectResponse };\n\n/**\n * The handler context passed to each kind-based handler.\n */\nexport interface HandlerContext {\n /** Raw TOON string (base64-encoded). */\n readonly toon: string;\n /** Event kind from shallow parse. */\n readonly kind: number;\n /** Event pubkey from shallow parse. */\n readonly pubkey: string;\n /** Payment amount in the ILP packet. */\n readonly amount: bigint;\n /** ILP destination address. */\n readonly destination: string;\n /** Lazy-decode the TOON payload into a full NostrEvent. */\n decode(): NostrEvent;\n /** Accept the packet with optional metadata. */\n accept(metadata?: Record<string, unknown>): HandlePacketAcceptResponse;\n /** Reject the packet with an ILP error code and message. */\n reject(code: string, message: string): HandlePacketRejectResponse;\n}\n\nexport interface CreateHandlerContextOptions {\n toon: string;\n meta: ToonRoutingMeta;\n amount: bigint;\n destination: string;\n toonDecoder: (toon: string) => NostrEvent;\n}\n\n/**\n * Creates a HandlerContext from the given options.\n */\nexport function createHandlerContext(\n options: CreateHandlerContextOptions\n): HandlerContext {\n let cachedEvent: NostrEvent | undefined;\n\n return {\n get toon() {\n return options.toon;\n },\n get kind() {\n return options.meta.kind;\n },\n get pubkey() {\n return options.meta.pubkey;\n },\n get amount() {\n return options.amount;\n },\n get destination() {\n return options.destination;\n },\n decode(): NostrEvent {\n if (!cachedEvent) {\n cachedEvent = options.toonDecoder(options.toon);\n }\n return cachedEvent;\n },\n accept(metadata?: Record<string, unknown>): HandlePacketAcceptResponse {\n return {\n accept: true,\n ...(metadata ? { metadata } : {}),\n };\n },\n reject(code: string, message: string): HandlePacketRejectResponse {\n return {\n accept: false,\n code,\n message,\n };\n },\n };\n}\n","/**\n * Handler registry for @toon-protocol/sdk.\n *\n * Maps event kinds to handler functions for dispatching incoming ILP packets.\n */\n\nimport { JOB_REQUEST_KIND_BASE } from '@toon-protocol/core';\nimport type {\n HandlerContext,\n HandlePacketAcceptResponse,\n HandlePacketRejectResponse,\n} from './handler-context.js';\n\nexport type HandlerResponse =\n | HandlePacketAcceptResponse\n | HandlePacketRejectResponse;\n\nexport type Handler = (ctx: HandlerContext) => Promise<HandlerResponse>;\n\n/**\n * Registry that maps Nostr event kinds to handler functions.\n */\nexport class HandlerRegistry {\n private handlers = new Map<number, Handler>();\n private defaultHandler: Handler | undefined;\n\n /**\n * Register a handler for a specific event kind.\n * Replaces any existing handler for that kind.\n */\n on(kind: number, handler: Handler): this {\n this.handlers.set(kind, handler);\n return this;\n }\n\n /**\n * Register a default handler for unrecognized kinds.\n */\n onDefault(handler: Handler): this {\n this.defaultHandler = handler;\n return this;\n }\n\n /**\n * Returns all registered kind numbers, sorted ascending.\n */\n getRegisteredKinds(): number[] {\n return [...this.handlers.keys()].sort((a, b) => a - b);\n }\n\n /**\n * Returns registered kinds in the DVM request range (5000-5999), sorted ascending.\n * Uses JOB_REQUEST_KIND_BASE (5000) as the range start.\n */\n getDvmKinds(): number[] {\n const dvmRangeStart = JOB_REQUEST_KIND_BASE;\n const dvmRangeEnd = JOB_REQUEST_KIND_BASE + 999;\n return this.getRegisteredKinds().filter(\n (k) => k >= dvmRangeStart && k <= dvmRangeEnd\n );\n }\n\n /**\n * Dispatch a context to the appropriate handler based on kind.\n */\n async dispatch(ctx: HandlerContext): Promise<HandlerResponse> {\n const handler = this.handlers.get(ctx.kind);\n if (handler) {\n return handler(ctx);\n }\n if (this.defaultHandler) {\n return this.defaultHandler(ctx);\n }\n return {\n accept: false,\n code: 'F00',\n message: `No handler registered for kind ${ctx.kind}`,\n };\n }\n}\n","/**\n * Pricing validator for @toon-protocol/sdk.\n *\n * Validates that incoming ILP payments meet the required price based on\n * TOON payload size and configured price per byte.\n */\n\nimport type { ToonRoutingMeta } from '@toon-protocol/core/toon';\n\nexport interface PricingValidatorConfig {\n basePricePerByte?: bigint;\n ownPubkey: string;\n kindPricing?: Record<number, bigint>;\n}\n\nexport interface PricingValidationResult {\n accepted: boolean;\n rejection?: {\n accept: false;\n code: string;\n message: string;\n metadata?: Record<string, string>;\n };\n}\n\n/**\n * Creates a pricing validator that checks payment amounts against TOON size.\n */\nexport function createPricingValidator(config: PricingValidatorConfig) {\n const basePricePerByte = config.basePricePerByte ?? 10n;\n\n return {\n validate(meta: ToonRoutingMeta, amount: bigint): PricingValidationResult {\n // Self-write bypass\n if (meta.pubkey === config.ownPubkey) {\n return { accepted: true };\n }\n\n // Kind-specific pricing override (use Object.hasOwn for prototype-safe lookup)\n const kindOverride =\n config.kindPricing && Object.hasOwn(config.kindPricing, meta.kind)\n ? config.kindPricing[meta.kind]\n : undefined;\n const pricePerByte = kindOverride ?? basePricePerByte;\n\n const requiredAmount = BigInt(meta.rawBytes.length) * pricePerByte;\n\n if (amount < requiredAmount) {\n return {\n accepted: false,\n rejection: {\n accept: false,\n code: 'F04',\n message: 'Insufficient payment',\n metadata: {\n required: requiredAmount.toString(),\n received: amount.toString(),\n },\n },\n };\n }\n\n return { accepted: true };\n },\n };\n}\n","/**\n * Verification pipeline for @toon-protocol/sdk.\n *\n * Verifies Schnorr signatures on incoming TOON payloads before dispatching\n * to handlers. In dev mode, verification is skipped.\n */\n\nimport type { ToonRoutingMeta } from '@toon-protocol/core/toon';\nimport { schnorr } from '@noble/curves/secp256k1.js';\nimport { hexToBytes } from '@noble/hashes/utils.js';\n\nexport interface VerificationResult {\n verified: boolean;\n rejection?: {\n accept: false;\n code: string;\n message: string;\n };\n}\n\nexport interface VerificationPipelineConfig {\n devMode: boolean;\n}\n\n/**\n * Creates a verification pipeline that checks Schnorr signatures.\n */\nexport function createVerificationPipeline(config: VerificationPipelineConfig) {\n return {\n async verify(\n meta: ToonRoutingMeta,\n _toonData: string\n ): Promise<VerificationResult> {\n if (config.devMode) {\n return { verified: true };\n }\n\n try {\n const sigBytes = hexToBytes(meta.sig);\n const msgBytes = hexToBytes(meta.id);\n const pubkeyBytes = hexToBytes(meta.pubkey);\n const valid = schnorr.verify(sigBytes, msgBytes, pubkeyBytes);\n if (!valid) {\n return {\n verified: false,\n rejection: {\n accept: false,\n code: 'F06',\n message: 'Invalid Schnorr signature',\n },\n };\n }\n return { verified: true };\n } catch {\n return {\n verified: false,\n rejection: {\n accept: false,\n code: 'F06',\n message: 'Signature verification failed',\n },\n };\n }\n },\n };\n}\n","/**\n * Payment handler bridge for @toon-protocol/sdk.\n *\n * Bridges between ILP packet handling and the handler registry,\n * using isTransit to distinguish fire-and-forget from await semantics.\n */\n\nimport type { HandlerRegistry } from './handler-registry.js';\nimport { createHandlerContext } from './handler-context.js';\nimport type { ToonRoutingMeta } from '@toon-protocol/core/toon';\n\nexport interface PaymentHandlerBridgeConfig {\n registry: HandlerRegistry;\n devMode: boolean;\n ownPubkey: string;\n basePricePerByte: bigint;\n}\n\nexport interface PaymentRequest {\n paymentId: string;\n destination: string;\n amount: string;\n data: string;\n isTransit: boolean;\n}\n\nexport interface PaymentResponse {\n accept: boolean;\n code?: string;\n message?: string;\n data?: string;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Creates a payment handler bridge that connects ILP packets to the handler registry.\n */\nexport function createPaymentHandlerBridge(config: PaymentHandlerBridgeConfig) {\n const { registry } = config;\n\n return {\n async handlePayment(request: PaymentRequest): Promise<PaymentResponse> {\n let amount: bigint;\n try {\n amount = BigInt(request.amount);\n } catch {\n return {\n accept: false,\n code: 'T00',\n message: 'Invalid payment amount',\n };\n }\n\n const ctx = createHandlerContext({\n toon: request.data,\n meta: {\n kind: 0,\n pubkey: '',\n id: '',\n sig: '',\n rawBytes: new Uint8Array(0),\n } as ToonRoutingMeta,\n amount,\n destination: request.destination,\n toonDecoder: () => {\n throw new Error('decode not available in bridge');\n },\n });\n\n if (request.isTransit) {\n void registry.dispatch(ctx).catch((err: unknown) => {\n // Log only the error message, not the full error object (which may contain payload data)\n const errMsg = err instanceof Error ? err.message : 'Unknown error';\n console.error('Transit handler error:', errMsg);\n });\n return { accept: true };\n }\n\n try {\n return await registry.dispatch(ctx);\n } catch (err: unknown) {\n // Log only the error message, not the full error object (which may contain payload data)\n const errMsg = err instanceof Error ? err.message : 'Unknown error';\n console.error('Handler error:', errMsg);\n return {\n accept: false,\n code: 'T00',\n message: 'Internal error',\n };\n }\n },\n };\n}\n","/**\n * Event storage handler stub for @toon-protocol/sdk.\n *\n * This is a stub that throws. The real implementation lives in\n * `@toon-protocol/town` -- see `createEventStorageHandler` from that package.\n *\n * The SDK is the framework; Town is the relay implementation. SDK consumers\n * building relay functionality should use `@toon-protocol/town` directly.\n */\n\n/**\n * Creates an event storage handler.\n *\n * **Stub** -- throws \"not yet implemented\". See `@toon-protocol/town` for the\n * real relay implementation of this handler.\n *\n * @see {@link https://github.com/ALLiDoizCode/toon/tree/main/packages/town | @toon-protocol/town}\n */\nexport function createEventStorageHandler(_config: unknown): unknown {\n throw new Error(\n 'createEventStorageHandler is not yet implemented in @toon-protocol/sdk. ' +\n 'Use @toon-protocol/town for the relay implementation.'\n );\n}\n","/**\n * createNode() composition for @toon-protocol/sdk.\n *\n * Wires the full ILP packet processing pipeline:\n * shallow TOON parse -> Schnorr signature verification -> pricing validation -> handler dispatch\n *\n * Provides start() / stop() lifecycle management by delegating to\n * the core ToonNode composition (embedded mode) or manual HTTP\n * composition (standalone mode).\n *\n * ## Deployment Modes\n *\n * - **Embedded** (`connector`): Pass an EmbeddableConnectorLike directly.\n * Zero-latency packet delivery via direct function calls.\n * - **Standalone** (`connectorUrl` + `handlerPort`): Connect to an external\n * connector via HTTP. The SDK starts an HTTP server to receive packets.\n */\n\nimport { createServer, type Server as HttpServer } from 'node:http';\nimport type { NostrEvent } from 'nostr-tools/pure';\nimport type {\n EmbeddableConnectorLike,\n HandlePacketRequest,\n HandlePacketResponse,\n ConnectorChannelClient,\n} from '@toon-protocol/core';\nimport type {\n KnownPeer,\n BootstrapResult,\n BootstrapEventListener,\n} from '@toon-protocol/core';\nimport type { SettlementConfig } from '@toon-protocol/core';\nimport {\n createToonNode,\n BootstrapService,\n createDiscoveryTracker,\n createHttpIlpClient,\n createHttpConnectorAdmin,\n createHttpChannelClient,\n resolveChainConfig,\n buildIlpPrepare,\n buildJobFeedbackEvent,\n buildJobResultEvent,\n parseJobResult,\n deriveChildAddress,\n checkAddressCollision,\n AddressRegistry,\n ILP_ROOT_PREFIX,\n calculateRouteAmount,\n resolveRouteFees,\n buildPrefixClaimEvent,\n validatePrefix,\n} from '@toon-protocol/core';\nimport type { ChainProviderConfigEntry } from '@toon-protocol/core';\nimport type { DvmJobStatus, IlpSendResult } from '@toon-protocol/core';\nimport type {\n IlpClient,\n ConnectorAdminClient,\n DiscoveryTracker,\n} from '@toon-protocol/core';\nimport {\n shallowParseToon,\n decodeEventFromToon,\n encodeEventToToon,\n} from '@toon-protocol/core/toon';\n\nimport type { SkillDescriptor } from '@toon-protocol/core';\nimport { fromSecretKey } from './identity.js';\nimport { HandlerRegistry, type Handler } from './handler-registry.js';\nimport { createHandlerContext } from './handler-context.js';\nimport { createVerificationPipeline } from './verification-pipeline.js';\nimport { createPricingValidator } from './pricing-validator.js';\nimport {\n buildSkillDescriptor,\n type BuildSkillDescriptorConfig,\n} from './skill-descriptor.js';\nimport { NodeError } from './errors.js';\n\n/**\n * Maximum base64-encoded payload size (in characters) accepted by the pipeline.\n * Defense-in-depth against DoS via oversized payloads. 1MB of base64 decodes\n * to ~750KB of raw TOON data, which is far beyond any legitimate Nostr event.\n * The pay-per-byte pricing model provides an additional economic disincentive.\n */\nconst MAX_PAYLOAD_BASE64_LENGTH = 1_048_576;\n\n/**\n * Configuration for creating a ServiceNode via createNode().\n *\n * Supports three deployment modes:\n * - **Default** (no connector args): Auto-creates an embedded ConnectorNode via\n * dynamic import. Requires `@toon-protocol/connector` as peer dependency.\n * - **Embedded** (`connector`): Pass a pre-configured EmbeddableConnectorLike.\n * - **Standalone** (`connectorUrl` + `handlerPort`): Connect to an external\n * connector via HTTP. The SDK starts an HTTP server to receive packets.\n *\n * Provide `connector` OR (`connectorUrl` + `handlerPort`), or neither (auto-create).\n */\nexport interface NodeConfig {\n /** 32-byte secp256k1 secret key */\n secretKey: Uint8Array;\n\n /** Chain preset name (default: 'anvil'). See resolveChainConfig(). */\n chain?: string;\n\n // --- Connector (optional — defaults to auto-created embedded connector) ---\n\n /**\n * Embedded connector instance for zero-latency mode.\n * If neither `connector` nor `connectorUrl` is provided, an embedded\n * ConnectorNode is auto-created (requires @toon-protocol/connector peer dep).\n */\n connector?: EmbeddableConnectorLike;\n /**\n * External connector admin URL for standalone mode (e.g., \"http://localhost:8081\").\n * Must be provided with `handlerPort`.\n */\n connectorUrl?: string;\n /**\n * Port for the HTTP server that receives ILP packets from the external connector.\n * Must be provided with `connectorUrl`.\n */\n handlerPort?: number;\n\n /**\n * BTP server port for the auto-created embedded connector (default: 3000).\n * Only used when neither `connector` nor `connectorUrl` is provided.\n */\n btpServerPort?: number;\n\n /**\n * EVM private key for settlement infrastructure.\n * Only used when auto-creating an embedded connector.\n * If not set, the identity's secp256k1 key is used.\n */\n settlementPrivateKey?: string;\n\n // --- Network ---\n\n /**\n * Upstream peer's ILP address prefix for address derivation.\n * When set, the node derives its ILP address as\n * `deriveChildAddress(upstreamPrefix, pubkey)` and ignores `ilpAddress`.\n */\n upstreamPrefix?: string;\n /**\n * Multiple upstream peer prefixes for multi-peered nodes (Story 7.3).\n * When set, derives one ILP address per upstream prefix. Takes priority\n * over `upstreamPrefix` (singular) when both are set.\n */\n upstreamPrefixes?: string[];\n /** ILP address (default: derived from pubkey under ILP_ROOT_PREFIX) */\n ilpAddress?: string;\n /** BTP endpoint URL advertised in kind:10032 announcements */\n btpEndpoint?: string;\n /** Asset code (default: 'USD') */\n assetCode?: string;\n /** Asset scale (default: 6) */\n assetScale?: number;\n /**\n * Base price per byte for pricing validation (default: 10n).\n *\n * Amounts are in USDC micro-units (6 decimals) for production.\n * Default 10n = 10 micro-USDC per byte = $0.00001/byte.\n * A 1KB event costs 10,240 micro-USDC = ~$0.01.\n */\n basePricePerByte?: bigint;\n /**\n * Routing fee per byte charged by this node as an intermediary (default: 0n = free routing).\n * Advertised in kind:10032 peer info events as a non-negative integer string.\n */\n feePerByte?: bigint;\n /** Dev mode skips signature verification (default: false) */\n devMode?: boolean;\n /** TOON encoder function */\n toonEncoder?: (event: NostrEvent) => Uint8Array;\n /** TOON decoder function */\n toonDecoder?: (bytes: Uint8Array) => NostrEvent;\n /** Initial known peers for bootstrap */\n knownPeers?: KnownPeer[];\n /** Relay WebSocket URL */\n relayUrl?: string;\n /** Settlement info for peer registration */\n settlementInfo?: SettlementConfig;\n /** Enable ArDrive peer lookup */\n ardriveEnabled?: boolean;\n /** Per-kind pricing overrides */\n kindPricing?: Record<number, bigint>;\n /**\n * Multi-chain provider configuration for the embedded connector.\n * When provided, passed directly to ConnectorNode as `chainProviders`.\n * Takes priority over the legacy `settlementInfra` auto-configuration.\n * Only used when auto-creating an embedded connector.\n */\n chainProviders?: ChainProviderConfigEntry[];\n /**\n * NIP-59 transport privacy configuration for the embedded connector.\n * When enabled, per-packet claims are wrapped in three-layer encryption.\n * Only used when auto-creating an embedded connector.\n */\n nip59?: { enabled: boolean };\n /**\n * Per-peer NIP-59 public keys for claim encryption.\n * Map of peer ID to compressed secp256k1 public key (hex, 66 chars).\n * Applied to peer configs when registering peers with the connector.\n * Only used when auto-creating an embedded connector.\n */\n peerNip59PublicKeys?: Record<string, string>;\n /** Config-based handler registration (alternative to post-creation .on()) */\n handlers?: Record<number, Handler>;\n /** Config-based default handler (alternative to post-creation .onDefault()) */\n defaultHandler?: Handler;\n /**\n * Optional skill descriptor configuration overrides.\n * When DVM handlers are registered, these values override the auto-derived\n * defaults in the skill descriptor. See buildSkillDescriptor().\n */\n skillConfig?: Omit<\n BuildSkillDescriptorConfig,\n 'basePricePerByte' | 'kindPricing'\n >;\n}\n\n/**\n * Result returned by ServiceNode.start().\n */\nexport interface StartResult {\n /** Number of peers successfully bootstrapped */\n peerCount: number;\n /** Number of payment channels opened */\n channelCount: number;\n /** Detailed results from the bootstrap phase */\n bootstrapResults: BootstrapResult[];\n}\n\n/**\n * Result returned by ServiceNode.publishEvent().\n */\nexport interface PublishEventResult {\n success: boolean;\n eventId: string;\n /** Response data from the ILP FULFILL packet (e.g., Arweave tx ID for kind:5094). */\n data?: string;\n code?: string;\n message?: string;\n}\n\n/**\n * A fully wired TOON node with lifecycle management.\n */\nexport interface ServiceNode {\n /** Nostr x-only public key (32 bytes, 64 hex chars) */\n readonly pubkey: string;\n /** EVM address derived from the same secp256k1 key */\n readonly evmAddress: string;\n /** Pass-through to the underlying connector (null in standalone mode) */\n readonly connector: EmbeddableConnectorLike | null;\n /** Channel client (null if connector lacks channel support or in standalone mode) */\n readonly channelClient: ConnectorChannelClient | null;\n /** Register a handler for a specific event kind (builder pattern) */\n on(kind: number, handler: Handler): ServiceNode;\n /** Register a lifecycle event listener */\n on(event: 'bootstrap', listener: BootstrapEventListener): ServiceNode;\n /** Register a default handler for unrecognized kinds (builder pattern) */\n onDefault(handler: Handler): ServiceNode;\n /** Start the node: wire packet handler, run bootstrap, start discovery */\n start(): Promise<StartResult>;\n /** Stop the node: clean up lifecycle state */\n stop(): Promise<void>;\n /** Initiate peering with a discovered peer (register + settlement) */\n peerWith(pubkey: string): Promise<void>;\n /**\n * Publish a Nostr event to a remote peer via the embedded connector.\n *\n * TOON-encodes the event, computes payment amount, and sends as an\n * ILP PREPARE packet via the runtime client.\n *\n * @param event - The Nostr event to publish\n * @param options - Must include destination ILP address. Optional `amount`\n * overrides the default basePricePerByte * toonData.length calculation\n * (prepaid model: send exact destination amount). Optional `bid` is a\n * client-side safety cap: if the destination amount exceeds `bid`, the\n * SDK throws before sending any ILP packet.\n * @returns Result with success/failure info and event ID\n */\n publishEvent(\n event: NostrEvent,\n options?: { destination: string; amount?: bigint; bid?: bigint }\n ): Promise<PublishEventResult>;\n\n /**\n * Publish a Kind 7000 DVM job feedback event via ILP PREPARE.\n *\n * Builds a signed Kind 7000 feedback event with NIP-90 tags (e, p, status)\n * and delegates to publishEvent() for TOON encoding and ILP delivery.\n * Standard relay write fee applies: basePricePerByte * toonData.length.\n *\n * @param requestEventId - 64-char hex event ID of the original Kind 5xxx request\n * @param customerPubkey - 64-char hex pubkey of the customer who posted the request\n * @param status - Job status value ('processing', 'error', 'success', 'partial')\n * @param content - Optional status details or error message\n * @param options - Must include destination ILP address\n * @returns Result with success/failure info and event ID\n */\n publishFeedback(\n requestEventId: string,\n customerPubkey: string,\n status: DvmJobStatus,\n content?: string,\n options?: { destination: string }\n ): Promise<PublishEventResult>;\n\n /**\n * Publish a Kind 6xxx DVM job result event via ILP PREPARE.\n *\n * Builds a signed Kind 6xxx result event with NIP-90 tags (e, p, amount)\n * and delegates to publishEvent() for TOON encoding and ILP delivery.\n * Standard relay write fee applies: basePricePerByte * toonData.length.\n *\n * @param requestEventId - 64-char hex event ID of the original Kind 5xxx request\n * @param customerPubkey - 64-char hex pubkey of the customer who posted the request\n * @param amount - Compute cost in USDC micro-units as string\n * @param content - Result data (text, URL, etc.)\n * @param options - Must include destination; optional kind (default: 6100)\n * @returns Result with success/failure info and event ID\n */\n publishResult(\n requestEventId: string,\n customerPubkey: string,\n amount: string,\n content: string,\n options?: { destination: string; kind?: number }\n ): Promise<PublishEventResult>;\n\n /**\n * Returns the computed skill descriptor for this node's DVM capabilities.\n * Returns `undefined` if no DVM handlers (kinds 5000-5999) are registered.\n * The descriptor is computed from the handler registry and node config.\n */\n getSkillDescriptor(): SkillDescriptor | undefined;\n\n /**\n * Send an ILP payment to a provider for compute settlement.\n *\n * Extracts the compute cost from the result event's `amount` tag via\n * parseJobResult(), optionally validates against the original bid amount\n * (E5-R005 bid validation), and sends a pure ILP value transfer\n * (empty data field) to the provider's ILP address.\n *\n * This is a payment-only operation -- no TOON encoding, no relay write.\n * The payment routes through the ILP mesh using the same infrastructure\n * as relay write fees.\n *\n * @deprecated Use publishEvent() with amount option instead. Prepaid model:\n * send job request + payment in one packet via\n * `publishEvent(event, { destination, amount: computePrice })`.\n *\n * @param resultEvent - Kind 6xxx result event with amount tag\n * @param providerIlpAddress - Provider's ILP address from kind:10035\n * @param options - Optional originalBid for bid validation (E5-R005)\n * @returns ILP send result with accepted/rejected status\n */\n settleCompute(\n resultEvent: NostrEvent,\n providerIlpAddress: string,\n options?: { originalBid?: string }\n ): Promise<IlpSendResult>;\n\n /**\n * Add a new upstream peer, deriving and registering its ILP address.\n *\n * Updates the AddressRegistry, registers a self-route in the embedded\n * connector, and triggers kind:10032 republication via BootstrapService.\n *\n * @param upstreamPrefix - The upstream peer's ILP address prefix\n */\n addUpstreamPeer(upstreamPrefix: string): void;\n\n /**\n * Remove an upstream peer, unregistering its derived ILP address.\n *\n * Updates the AddressRegistry, removes the self-route from the embedded\n * connector, and triggers kind:10032 republication via BootstrapService.\n *\n * @param upstreamPrefix - The upstream prefix to remove\n */\n removeUpstreamPeer(upstreamPrefix: string): void;\n\n /**\n * Claim a prefix from an upstream peer by sending a prefix claim event\n * with payment via ILP.\n *\n * Builds a Kind 10034 prefix claim event, reads the upstream's prefix\n * pricing from its kind:10032 advertisement, and calls publishEvent()\n * with the appropriate amount.\n *\n * @param prefix - The prefix string to claim (e.g., 'useast')\n * @param upstreamDestination - ILP address of the upstream node\n * @param options - Optional prefixPrice override (defaults to upstream's prefixPricing.basePrice from discovery)\n * @returns Result with success/failure info and event ID\n */\n claimPrefix(\n prefix: string,\n upstreamDestination: string,\n options?: { prefixPrice?: bigint }\n ): Promise<PublishEventResult>;\n}\n\n/**\n * Creates a fully wired ServiceNode from configuration.\n *\n * The returned node has the full ILP packet processing pipeline wired in the\n * correct order:\n * 1. Shallow TOON parse (extract routing metadata)\n * 2. Schnorr signature verification (reject with F06 if invalid)\n * 3. Pricing validation (reject with F04 if underpaid)\n * 4. Handler dispatch (route to kind-specific or default handler)\n *\n * Supports two deployment modes:\n * - **Embedded** (`connector`): Uses createToonNode for zero-latency\n * packet delivery via direct function calls.\n * - **Standalone** (`connectorUrl` + `handlerPort`): Starts an HTTP server\n * to receive packets and uses HTTP clients for the connector API.\n *\n * Handlers can be registered via config or post-creation .on()/.onDefault().\n */\nexport function createNode(config: NodeConfig): ServiceNode {\n // 0. Validate connector mode\n const hasConnector = config.connector !== undefined;\n const hasConnectorUrl = config.connectorUrl !== undefined;\n\n if (hasConnector && hasConnectorUrl) {\n throw new NodeError(\n 'NodeConfig: provide either connector or connectorUrl, not both'\n );\n }\n if (hasConnectorUrl && config.handlerPort === undefined) {\n throw new NodeError(\n 'NodeConfig: handlerPort is required when using connectorUrl (standalone mode)'\n );\n }\n const autoCreateConnector = !hasConnector && !hasConnectorUrl;\n const embeddedMode = hasConnector || autoCreateConnector;\n\n // 0b. Resolve chain config and auto-populate settlementInfo if not set\n const chainConfig = resolveChainConfig(config.chain);\n let effectiveSettlementInfo = config.settlementInfo;\n if (!effectiveSettlementInfo) {\n const chainKey = `evm:base:${chainConfig.chainId}`;\n const supportedChains = [chainKey];\n const preferredTokens: Record<string, string> = {\n [chainKey]: chainConfig.usdcAddress,\n };\n const tokenNetworks: Record<string, string> | undefined =\n chainConfig.tokenNetworkAddress\n ? { [chainKey]: chainConfig.tokenNetworkAddress }\n : undefined;\n\n effectiveSettlementInfo = {\n supportedChains,\n preferredTokens,\n ...(tokenNetworks && { tokenNetworks }),\n };\n }\n\n // 1. Derive identity from secretKey\n let identity;\n try {\n identity = fromSecretKey(config.secretKey);\n } catch (error: unknown) {\n throw new NodeError(\n `Invalid secretKey: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n const { pubkey, evmAddress } = identity;\n\n // 2. Create handler registry\n const registry = new HandlerRegistry();\n\n // 3. Register config-based handlers (with kind validation matching node.on())\n if (config.handlers) {\n for (const [kind, handler] of Object.entries(config.handlers)) {\n const kindNum = Number(kind);\n if (!Number.isInteger(kindNum) || kindNum < 0) {\n throw new NodeError(\n `Invalid event kind in handlers config: expected a non-negative integer, got '${kind}'`\n );\n }\n registry.on(kindNum, handler);\n }\n }\n\n // 4. Register config-based default handler\n if (config.defaultHandler) {\n registry.onDefault(config.defaultHandler);\n }\n\n // 5. Create verification pipeline\n const verifier = createVerificationPipeline({\n devMode: config.devMode ?? false,\n });\n\n // 6. Create pricing validator\n const pricer = createPricingValidator({\n basePricePerByte: config.basePricePerByte ?? 10n,\n ownPubkey: pubkey,\n kindPricing: config.kindPricing,\n });\n\n // 7. Set up TOON codec with defaults\n const encoder = config.toonEncoder ?? encodeEventToToon;\n const decoder = config.toonDecoder ?? decodeEventFromToon;\n\n // Context decoder: converts base64 TOON string to NostrEvent\n const contextDecoder = (toon: string): NostrEvent => {\n const bytes = Buffer.from(toon, 'base64');\n return decoder(bytes);\n };\n\n // Mutable ref so the packet handler closure can access the tracker after it's created.\n const trackerRef: { current?: { processEvent(event: NostrEvent): void } } =\n {};\n\n // 8. Build the pipelined packet handler (Option A: directly on HandlePacketRequest)\n const pipelinedHandler = async (\n request: HandlePacketRequest\n ): Promise<HandlePacketResponse> => {\n // Step 0: Reject oversized payloads before allocating memory (DoS mitigation)\n if (request.data.length > MAX_PAYLOAD_BASE64_LENGTH) {\n return {\n accept: false,\n code: 'F06',\n message: `Payload too large: ${request.data.length} bytes exceeds maximum ${MAX_PAYLOAD_BASE64_LENGTH}`,\n };\n }\n\n // Step 1: Shallow TOON parse\n const toonBytes = Buffer.from(request.data, 'base64');\n let meta;\n try {\n meta = shallowParseToon(toonBytes);\n } catch {\n // Corrupted TOON data cannot be parsed -- reject as invalid payload\n return {\n accept: false,\n code: 'F06',\n message: 'Invalid TOON payload: failed to parse routing metadata',\n };\n }\n\n // Dev mode: log packet details\n // Sanitize user-controlled fields (amount, destination) to prevent log injection\n // via newlines or control characters. meta.kind (integer) and meta.pubkey (validated\n // hex) are safe; request.data preview is base64 (safe character set).\n if (config.devMode ?? false) {\n const toonPreview =\n request.data.length > 80\n ? request.data.substring(0, 80) + '...'\n : request.data;\n // eslint-disable-next-line no-control-regex\n const sanitize = (s: string) => s.replace(/[\\x00-\\x1f\\x7f]/g, '');\n console.log(\n '[toon:dev]',\n `kind=${meta.kind}`,\n `pubkey=${meta.pubkey.substring(0, 16)}...`,\n `amount=${sanitize(request.amount)}`,\n `dest=${sanitize(request.destination)}`,\n `toon=${toonPreview}`\n );\n }\n\n // Step 2: Verify signature\n const verifyResult = await verifier.verify(meta, request.data);\n if (!verifyResult.verified) {\n // VerificationResult.rejection is always set when verified=false per\n // createVerificationPipeline contract. Guard defensively anyway.\n if (verifyResult.rejection) {\n return verifyResult.rejection;\n }\n return { accept: false, code: 'F06', message: 'Verification failed' };\n }\n\n // Step 3: Validate pricing (skip in dev mode)\n let amount: bigint;\n try {\n amount = BigInt(request.amount);\n } catch {\n if (config.devMode ?? false) {\n amount = 0n;\n } else {\n return {\n accept: false,\n code: 'T00',\n message: 'Invalid payment amount',\n };\n }\n }\n if (!(config.devMode ?? false)) {\n const priceResult = pricer.validate(meta, amount);\n if (!priceResult.accepted) {\n // PricingValidationResult.rejection is always set when accepted=false per\n // createPricingValidator contract. Guard defensively anyway.\n if (priceResult.rejection) {\n return priceResult.rejection;\n }\n return {\n accept: false,\n code: 'F04',\n message: 'Pricing validation failed',\n };\n }\n }\n\n // Step 4: Build HandlerContext with real metadata\n const ctx = createHandlerContext({\n toon: request.data,\n meta,\n amount,\n destination: request.destination,\n toonDecoder: contextDecoder,\n });\n\n // Step 5: Dispatch to handler (T00 error boundary)\n try {\n const result = await registry.dispatch(ctx);\n\n // Feed accepted kind:10032 events to discovery tracker for peer discovery.\n // Uses late-binding reference since the tracker is created after this handler.\n if (result.accept && meta.kind === 10032 && trackerRef.current) {\n const decoded = ctx.decode();\n if (decoded) {\n trackerRef.current.processEvent(decoded);\n }\n }\n\n return result;\n } catch (err: unknown) {\n // Log only the error message, not the full error object (which may contain payload data)\n const errMsg = err instanceof Error ? err.message : 'Unknown error';\n console.error('Handler dispatch failed:', errMsg);\n return { accept: false, code: 'T00', message: 'Internal error' };\n }\n };\n\n // ILP info shared between both modes\n // Address resolution priority (Story 7.3 extends with upstreamPrefixes):\n // 1. upstreamPrefixes set -> derive one address per prefix, primary = first\n // 2. upstreamPrefix set -> derive single address (ignore ilpAddress)\n // 3. ilpAddress set -> use directly (includes genesis node with 'g.toon')\n // 4. neither set -> derive from ILP_ROOT_PREFIX + pubkey (default)\n let resolvedIlpAddress: string;\n let resolvedIlpAddresses: string[];\n\n if (\n config.upstreamPrefixes !== undefined &&\n config.upstreamPrefixes.length > 0\n ) {\n // upstreamPrefixes takes priority over upstreamPrefix (singular)\n if (config.upstreamPrefix !== undefined) {\n console.warn(\n '[toon:warn] Both upstreamPrefixes and upstreamPrefix set; upstreamPrefixes takes priority'\n );\n }\n resolvedIlpAddresses = config.upstreamPrefixes.map((p) =>\n deriveChildAddress(p, pubkey)\n );\n // Task 4.4: Check for truncation collisions across upstream prefixes\n for (let i = 0; i < resolvedIlpAddresses.length; i++) {\n const others = resolvedIlpAddresses.filter((_, j) => j !== i);\n checkAddressCollision(resolvedIlpAddresses[i] as string, others);\n }\n resolvedIlpAddress = resolvedIlpAddresses[0] as string;\n } else if (config.upstreamPrefix !== undefined) {\n resolvedIlpAddress = deriveChildAddress(config.upstreamPrefix, pubkey);\n resolvedIlpAddresses = [resolvedIlpAddress];\n } else if (config.ilpAddress !== undefined) {\n resolvedIlpAddress = config.ilpAddress;\n resolvedIlpAddresses = [resolvedIlpAddress];\n } else {\n resolvedIlpAddress = deriveChildAddress(ILP_ROOT_PREFIX, pubkey);\n resolvedIlpAddresses = [resolvedIlpAddress];\n }\n\n // Task 6.2: Initialize AddressRegistry with initial upstream prefixes\n const addressRegistry = new AddressRegistry();\n if (\n config.upstreamPrefixes !== undefined &&\n config.upstreamPrefixes.length > 0\n ) {\n for (let i = 0; i < config.upstreamPrefixes.length; i++) {\n addressRegistry.addAddress(\n config.upstreamPrefixes[i] as string,\n resolvedIlpAddresses[i] as string\n );\n }\n } else if (config.upstreamPrefix !== undefined) {\n addressRegistry.addAddress(config.upstreamPrefix, resolvedIlpAddress);\n } else if (config.ilpAddress !== undefined) {\n addressRegistry.addAddress('explicit', resolvedIlpAddress);\n } else {\n addressRegistry.addAddress(ILP_ROOT_PREFIX, resolvedIlpAddress);\n }\n\n const ilpInfo = {\n ilpAddress: resolvedIlpAddress,\n ilpAddresses: resolvedIlpAddresses,\n btpEndpoint: config.btpEndpoint ?? '',\n assetCode: config.assetCode ?? 'USD',\n assetScale: config.assetScale ?? 6,\n feePerByte: String(config.feePerByte ?? 0n),\n };\n\n // 9. Branch: embedded mode vs standalone mode\n let ilpClient: IlpClient;\n let adminClient: ConnectorAdminClient;\n let channelClient: ConnectorChannelClient | null = null;\n let bootstrapServiceInstance: BootstrapService;\n let discoveryTrackerInstance: DiscoveryTracker;\n let httpServer: HttpServer | null = null;\n\n // Lifecycle delegates (start/stop differ per mode)\n let doStart: () => Promise<{\n bootstrapResults: BootstrapResult[];\n peerCount: number;\n channelCount: number;\n }>;\n let doStop: () => Promise<void>;\n\n // Track auto-created connector for cleanup\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let autoCreatedConnector: any = null;\n\n if (embeddedMode && hasConnector) {\n // --- EMBEDDED MODE (user-provided connector): delegate to createToonNode ---\n const toonNode = createToonNode({\n connector: config.connector as NonNullable<typeof config.connector>,\n handlePacket: pipelinedHandler,\n secretKey: config.secretKey,\n ilpInfo,\n toonEncoder: encoder,\n toonDecoder: decoder,\n relayUrl: config.relayUrl,\n knownPeers: config.knownPeers,\n settlementInfo: effectiveSettlementInfo,\n basePricePerByte: config.basePricePerByte,\n ardriveEnabled: config.ardriveEnabled,\n });\n\n ilpClient = toonNode.ilpClient;\n channelClient = toonNode.channelClient;\n bootstrapServiceInstance = toonNode.bootstrapService;\n discoveryTrackerInstance = toonNode.discoveryTracker;\n adminClient = {\n addPeer: () => Promise.resolve(),\n removePeer: () => Promise.resolve(),\n };\n\n trackerRef.current = discoveryTrackerInstance;\n\n doStart = async () => toonNode.start();\n doStop = async () => toonNode.stop();\n } else if (autoCreateConnector) {\n // --- AUTO-CREATE EMBEDDED MODE: deferred ConnectorNode creation in doStart() ---\n\n // Create placeholder bootstrap/discovery instances (wired during start)\n bootstrapServiceInstance = new BootstrapService(\n {\n knownPeers: config.knownPeers ?? [],\n ardriveEnabled: config.ardriveEnabled ?? false,\n defaultRelayUrl: config.relayUrl ?? '',\n settlementInfo: effectiveSettlementInfo,\n ownIlpAddress: ilpInfo.ilpAddress,\n toonEncoder: encoder,\n toonDecoder: decoder,\n basePricePerByte: config.basePricePerByte ?? 10n,\n },\n config.secretKey,\n ilpInfo\n );\n\n discoveryTrackerInstance = createDiscoveryTracker({\n secretKey: config.secretKey,\n settlementInfo: effectiveSettlementInfo,\n });\n\n // Placeholder clients (replaced during start)\n ilpClient = {\n sendIlpPacket: () =>\n Promise.reject(new NodeError('Node not started. Call start() first.')),\n };\n adminClient = {\n addPeer: () => Promise.resolve(),\n removePeer: () => Promise.resolve(),\n };\n\n trackerRef.current = discoveryTrackerInstance;\n\n doStart = async () => {\n // Dynamic import to keep @toon-protocol/connector as optional peer dep\n // eslint-disable-next-line @typescript-eslint/consistent-type-imports\n let ConnectorNodeClass: typeof import('@toon-protocol/connector').ConnectorNode;\n // eslint-disable-next-line @typescript-eslint/consistent-type-imports\n let createConnectorLogger: typeof import('@toon-protocol/connector').createLogger;\n try {\n const mod = await import('@toon-protocol/connector');\n ConnectorNodeClass = mod.ConnectorNode;\n createConnectorLogger = mod.createLogger;\n } catch {\n throw new NodeError(\n 'Auto-create connector requires @toon-protocol/connector as a peer dependency. ' +\n 'Install it with: pnpm add @toon-protocol/connector'\n );\n }\n\n const nodeId = `toon-${pubkey.slice(0, 16)}`;\n const btpServerPort = config.btpServerPort ?? 3000;\n const connectorLogger = createConnectorLogger(nodeId, 'warn');\n\n // Derive settlement private key from identity if not explicitly set\n let settlementPrivateKey = config.settlementPrivateKey;\n if (!settlementPrivateKey) {\n const keyBuffer = Buffer.from(config.secretKey);\n settlementPrivateKey = `0x${keyBuffer.toString('hex')}`;\n keyBuffer.fill(0);\n }\n\n const hasSettlementAddresses =\n chainConfig.registryAddress && chainConfig.tokenNetworkAddress;\n\n // Build connector config: chainProviders takes priority over settlementInfra\n const hasChainProviders =\n config.chainProviders !== undefined && config.chainProviders.length > 0;\n\n autoCreatedConnector = new ConnectorNodeClass(\n {\n nodeId,\n btpServerPort,\n environment: 'development' as const,\n deploymentMode: 'embedded' as const,\n peers: [],\n routes: [],\n localDelivery: { enabled: false },\n // Multi-chain: use chainProviders when provided\n ...(hasChainProviders && {\n chainProviders: config.chainProviders,\n }),\n // Legacy: fall back to settlementInfra when chainProviders absent\n ...(!hasChainProviders &&\n hasSettlementAddresses && {\n settlementInfra: {\n enabled: true,\n rpcUrl: chainConfig.rpcUrl,\n registryAddress: chainConfig.registryAddress,\n tokenAddress: chainConfig.usdcAddress,\n privateKey: settlementPrivateKey,\n },\n }),\n // NIP-59 transport privacy\n ...(config.nip59 && { nip59: config.nip59 }),\n },\n connectorLogger\n );\n\n await autoCreatedConnector.start();\n\n // Now wire the real connector into createToonNode\n const connector =\n autoCreatedConnector as unknown as EmbeddableConnectorLike;\n const toonNode = createToonNode({\n connector,\n handlePacket: pipelinedHandler,\n secretKey: config.secretKey,\n ilpInfo,\n toonEncoder: encoder,\n toonDecoder: decoder,\n relayUrl: config.relayUrl,\n knownPeers: config.knownPeers,\n settlementInfo: effectiveSettlementInfo,\n basePricePerByte: config.basePricePerByte,\n ardriveEnabled: config.ardriveEnabled,\n });\n\n // Replace placeholder clients with real ones\n ilpClient = toonNode.ilpClient;\n channelClient = toonNode.channelClient;\n\n // Wire bootstrap and discovery with the real admin client\n bootstrapServiceInstance.setIlpClient(toonNode.ilpClient);\n bootstrapServiceInstance.setConnectorAdmin({\n addPeer: () => Promise.resolve(),\n removePeer: () => Promise.resolve(),\n });\n if (toonNode.channelClient) {\n bootstrapServiceInstance.setChannelClient(toonNode.channelClient);\n }\n\n discoveryTrackerInstance.setConnectorAdmin({\n addPeer: () => Promise.resolve(),\n removePeer: () => Promise.resolve(),\n });\n if (toonNode.channelClient) {\n discoveryTrackerInstance.setChannelClient(toonNode.channelClient);\n }\n\n trackerRef.current = discoveryTrackerInstance;\n\n // Add self-routes for all addresses (Task 5.3)\n for (const addr of ilpInfo.ilpAddresses) {\n autoCreatedConnector.addRoute({\n prefix: addr,\n nextHop: nodeId,\n priority: 100,\n });\n }\n\n // Start the toonNode (wires packet handler, runs bootstrap)\n const result = await toonNode.start();\n return result;\n };\n\n doStop = async () => {\n if (autoCreatedConnector) {\n await autoCreatedConnector.stop();\n autoCreatedConnector = null;\n }\n };\n } else {\n // --- STANDALONE MODE: manual composition with HTTP clients ---\n const connectorUrl = config.connectorUrl as string;\n const handlerPort = config.handlerPort as number;\n\n ilpClient = createHttpIlpClient(connectorUrl);\n adminClient = createHttpConnectorAdmin(connectorUrl, '');\n\n // Channel client via HTTP if settlement is configured\n if (effectiveSettlementInfo) {\n channelClient = createHttpChannelClient(connectorUrl);\n }\n\n // Create BootstrapService\n bootstrapServiceInstance = new BootstrapService(\n {\n knownPeers: config.knownPeers ?? [],\n ardriveEnabled: config.ardriveEnabled ?? false,\n defaultRelayUrl: config.relayUrl ?? '',\n settlementInfo: effectiveSettlementInfo,\n ownIlpAddress: ilpInfo.ilpAddress,\n toonEncoder: encoder,\n toonDecoder: decoder,\n basePricePerByte: config.basePricePerByte ?? 10n,\n },\n config.secretKey,\n ilpInfo\n );\n\n bootstrapServiceInstance.setIlpClient(ilpClient);\n bootstrapServiceInstance.setConnectorAdmin(adminClient);\n if (channelClient) {\n bootstrapServiceInstance.setChannelClient(channelClient);\n }\n\n // Create DiscoveryTracker\n discoveryTrackerInstance = createDiscoveryTracker({\n secretKey: config.secretKey,\n settlementInfo: effectiveSettlementInfo,\n });\n discoveryTrackerInstance.setConnectorAdmin(adminClient);\n if (channelClient) {\n discoveryTrackerInstance.setChannelClient(channelClient);\n }\n\n trackerRef.current = discoveryTrackerInstance;\n\n doStart = async () => {\n // Start HTTP server for receiving ILP packets\n httpServer = createServer(async (req, res) => {\n if (req.method === 'GET' && req.url === '/health') {\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ status: 'healthy', pubkey }));\n return;\n }\n\n if (req.method === 'POST' && req.url === '/handle-packet') {\n let body = '';\n req.on('data', (chunk: Buffer) => {\n body += chunk.toString();\n });\n req.on('end', async () => {\n try {\n const request = JSON.parse(body) as HandlePacketRequest;\n if (\n request.amount === undefined ||\n request.amount === null ||\n request.destination === undefined ||\n request.destination === null ||\n request.data === undefined ||\n request.data === null\n ) {\n res.writeHead(400, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n accept: false,\n code: 'F00',\n message: 'Missing required fields',\n })\n );\n return;\n }\n const result = await pipelinedHandler(request);\n res.writeHead(result.accept ? 200 : 400, {\n 'Content-Type': 'application/json',\n });\n res.end(JSON.stringify(result));\n } catch (err: unknown) {\n console.error('[SDK] handle-packet error:', err);\n res.writeHead(500, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n accept: false,\n code: 'T00',\n message: 'Internal server error',\n })\n );\n }\n });\n return;\n }\n\n res.writeHead(404);\n res.end();\n });\n\n await new Promise<void>((resolve) => {\n httpServer?.listen(handlerPort, () => resolve());\n });\n\n // Run bootstrap\n const results = await bootstrapServiceInstance.bootstrap();\n const bootstrapPeerPubkeys = results.map((r) => r.knownPeer.pubkey);\n discoveryTrackerInstance.addExcludedPubkeys(bootstrapPeerPubkeys);\n\n return {\n bootstrapResults: results,\n peerCount: results.length,\n channelCount: results.filter((r) => r.channelId).length,\n };\n };\n\n doStop = async () => {\n const server = httpServer;\n if (server) {\n await new Promise<void>((resolve) => {\n server.close(() => resolve());\n });\n httpServer = null;\n }\n };\n }\n\n // 10. Track SDK-level lifecycle state\n let started = false;\n let lastBootstrapResults: BootstrapResult[] = [];\n\n // 11. Build and return ServiceNode\n const node: ServiceNode = {\n get pubkey() {\n return pubkey;\n },\n get evmAddress() {\n return evmAddress;\n },\n get connector() {\n return (\n config.connector ??\n (autoCreatedConnector as unknown as EmbeddableConnectorLike) ??\n null\n );\n },\n get channelClient() {\n return channelClient;\n },\n\n getSkillDescriptor(): SkillDescriptor | undefined {\n return buildSkillDescriptor(registry, {\n basePricePerByte: config.basePricePerByte ?? 10n,\n kindPricing: config.kindPricing,\n ...config.skillConfig,\n });\n },\n\n on(\n kindOrEvent: number | string,\n handlerOrListener: Handler | BootstrapEventListener\n ): ServiceNode {\n if (typeof kindOrEvent === 'number') {\n // Handler registration (existing behavior)\n if (!Number.isInteger(kindOrEvent) || kindOrEvent < 0) {\n throw new NodeError(\n `Invalid event kind: expected a non-negative integer, got ${String(kindOrEvent)}`\n );\n }\n registry.on(kindOrEvent, handlerOrListener as Handler);\n } else if (kindOrEvent === 'bootstrap') {\n // Lifecycle event listener -- forward to bootstrapService AND discoveryTracker\n const listener = handlerOrListener as BootstrapEventListener;\n bootstrapServiceInstance.on(listener);\n discoveryTrackerInstance.on(listener);\n } else {\n // Sanitize event name to prevent log injection via control characters\n // eslint-disable-next-line no-control-regex\n const sanitized = String(kindOrEvent).replace(/[\\x00-\\x1f\\x7f]/g, '');\n throw new NodeError(\n `Unknown lifecycle event: '${sanitized}'. Supported: 'bootstrap'`\n );\n }\n return node;\n },\n\n onDefault(handler: Handler): ServiceNode {\n registry.onDefault(handler);\n return node;\n },\n\n async start(): Promise<StartResult> {\n if (started) {\n throw new NodeError('Node already started');\n }\n\n try {\n const result = await doStart();\n started = true;\n lastBootstrapResults = result.bootstrapResults;\n return {\n peerCount: result.peerCount,\n channelCount: result.channelCount,\n bootstrapResults: result.bootstrapResults,\n };\n } catch (error: unknown) {\n if (error instanceof NodeError) {\n throw error;\n }\n throw new NodeError(\n `Failed to start node: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n },\n\n async stop(): Promise<void> {\n if (!started) {\n return; // No-op if not started\n }\n\n await doStop();\n started = false;\n },\n\n async peerWith(targetPubkey: string): Promise<void> {\n if (!started) {\n throw new NodeError(\n 'Cannot peer: node not started. Call start() first.'\n );\n }\n // Defense-in-depth: validate pubkey format before delegating to core\n if (\n typeof targetPubkey !== 'string' ||\n targetPubkey.length !== 64 ||\n !/^[0-9a-f]{64}$/.test(targetPubkey)\n ) {\n throw new NodeError(\n 'Invalid pubkey: expected a 64-character lowercase hex string'\n );\n }\n return discoveryTrackerInstance.peerWith(targetPubkey);\n },\n\n async publishEvent(\n event: NostrEvent,\n options?: { destination: string; amount?: bigint; bid?: bigint }\n ): Promise<PublishEventResult> {\n // Guard: node must be started\n if (!started) {\n throw new NodeError(\n 'Cannot publish: node not started. Call start() first.'\n );\n }\n\n // Guard: destination is required\n if (!options?.destination) {\n throw new NodeError(\n \"Cannot publish: destination is required. Pass { destination: 'g.peer.address' }.\"\n );\n }\n\n try {\n // TOON-encode the event\n const toonData = encoder(event);\n\n // Resolve intermediary routing fees from discovered peers\n const { hopFees, warnings } = resolveRouteFees({\n destination: options.destination,\n ownIlpAddress: ilpInfo.ilpAddress,\n discoveredPeers: discoveryTrackerInstance.getAllDiscoveredPeers(),\n });\n\n // Log warnings about unknown intermediaries\n for (const warning of warnings) {\n console.warn(`[publishEvent] ${warning}`);\n }\n\n // Compute destination amount: use override or default basePricePerByte * bytes\n const destinationAmount =\n options.amount ??\n (config.basePricePerByte ?? 10n) * BigInt(toonData.length);\n\n // Bid safety cap: if bid is provided, reject if destination amount exceeds bid\n if (options.bid !== undefined && destinationAmount > options.bid) {\n throw new NodeError(\n `Cannot publish: destination amount ${destinationAmount} exceeds bid safety cap ${options.bid}`\n );\n }\n\n // Compute total amount: destination amount + intermediary route fees\n let amount: bigint;\n if (options.amount !== undefined) {\n // When amount override is provided, add only hop fees (no basePricePerByte)\n const hopFeesTotal = calculateRouteAmount({\n basePricePerByte: 0n,\n packetByteLength: toonData.length,\n hopFees,\n });\n amount = options.amount + hopFeesTotal;\n } else {\n // Default: basePricePerByte * bytes + hop fees\n amount = calculateRouteAmount({\n basePricePerByte: config.basePricePerByte ?? 10n,\n packetByteLength: toonData.length,\n hopFees,\n });\n }\n\n // Build ILP PREPARE packet using shared construction (packet equivalence\n // with x402 rail -- the destination relay cannot distinguish between\n // packets sent via publishEvent() and the x402 /publish endpoint).\n const packet = buildIlpPrepare({\n destination: options.destination,\n amount,\n data: toonData,\n });\n\n // Send via ILP client\n const result = await ilpClient.sendIlpPacket(packet);\n\n // Map IlpSendResult to PublishEventResult\n if (result.accepted) {\n return {\n success: true,\n eventId: event.id,\n ...(result.data !== undefined ? { data: result.data } : {}),\n };\n }\n\n return {\n success: false,\n eventId: event.id,\n code: result.code ?? 'T00',\n message: result.message ?? 'Unknown error',\n };\n } catch (error: unknown) {\n // Propagate NodeError directly\n if (error instanceof NodeError) {\n throw error;\n }\n throw new NodeError(\n `Failed to publish event: ${error instanceof Error ? error.message : String(error)}`,\n error instanceof Error ? error : undefined\n );\n }\n },\n\n async publishFeedback(\n requestEventId: string,\n customerPubkey: string,\n status: DvmJobStatus,\n content?: string,\n options?: { destination: string }\n ): Promise<PublishEventResult> {\n // Build Kind 7000 feedback event using provider's secretKey\n const feedbackEvent = buildJobFeedbackEvent(\n { requestEventId, customerPubkey, status, content },\n config.secretKey\n );\n\n // Delegate to publishEvent() for TOON encoding, pricing, and ILP delivery\n return node.publishEvent(feedbackEvent, options);\n },\n\n async publishResult(\n requestEventId: string,\n customerPubkey: string,\n amount: string,\n content: string,\n options?: { destination: string; kind?: number }\n ): Promise<PublishEventResult> {\n // Default result kind is 6100 (text generation result = 5100 + 1000)\n const resultKind = options?.kind ?? 6100;\n\n // Build Kind 6xxx result event using provider's secretKey\n const resultEvent = buildJobResultEvent(\n { kind: resultKind, requestEventId, customerPubkey, amount, content },\n config.secretKey\n );\n\n // Delegate to publishEvent() for TOON encoding, pricing, and ILP delivery\n return node.publishEvent(resultEvent, options);\n },\n\n async settleCompute(\n resultEvent: NostrEvent,\n providerIlpAddress: string,\n options?: { originalBid?: string }\n ): Promise<IlpSendResult> {\n // Deprecation warning\n console.warn(\n '[settleCompute] DEPRECATED: Use publishEvent() with { amount } option instead. The prepaid model sends job request + payment in one packet.'\n );\n\n // Guard: node must be started\n if (!started) {\n throw new NodeError(\n 'Cannot settle compute: node not started. Call start() first.'\n );\n }\n\n // Guard: providerIlpAddress must be a non-empty, non-whitespace string\n if (\n !providerIlpAddress ||\n typeof providerIlpAddress !== 'string' ||\n providerIlpAddress.trim() === ''\n ) {\n throw new NodeError(\n \"Cannot settle compute: providerIlpAddress is required. Resolve it from the provider's kind:10035 service discovery event.\"\n );\n }\n\n // Extract amount from result event via parseJobResult()\n const parsed = parseJobResult(resultEvent);\n if (!parsed) {\n throw new NodeError(\n 'Cannot settle compute: failed to parse result event. Ensure the event is a valid Kind 6xxx with an amount tag.'\n );\n }\n\n const computeAmount = parsed.amount;\n\n // Validate computeAmount is a valid non-negative numeric string before any\n // further processing. Without this, a non-numeric amount would propagate to\n // ilpClient.sendIlpPacket() and cause a BootstrapError instead of a clear\n // NodeError. Negative amounts would cause undefined behavior in the ILP layer.\n let amountBigInt: bigint;\n try {\n amountBigInt = BigInt(computeAmount);\n } catch {\n throw new NodeError(\n `Cannot settle compute: result event amount ('${computeAmount}') is not a valid numeric string.`\n );\n }\n if (amountBigInt < 0n) {\n throw new NodeError(\n `Cannot settle compute: result event amount ('${computeAmount}') must be non-negative.`\n );\n }\n\n // E5-R005 bid validation: if originalBid is provided, validate amount <= bid\n if (options?.originalBid !== undefined) {\n let bidBigInt: bigint;\n try {\n bidBigInt = BigInt(options.originalBid);\n } catch {\n throw new NodeError(\n `Cannot settle compute: originalBid ('${options.originalBid}') is not a valid numeric string.`\n );\n }\n if (amountBigInt > bidBigInt) {\n throw new NodeError(\n `Cannot settle compute: result amount (${computeAmount}) exceeds original bid (${options.originalBid}). Potential provider overcharge.`\n );\n }\n }\n\n // Send pure ILP value transfer (empty data = no event payload)\n // This is a payment-only operation -- no TOON encoding, no relay write.\n return ilpClient.sendIlpPacket({\n destination: providerIlpAddress,\n amount: computeAmount,\n data: '',\n });\n },\n\n // Task 6.3: Address lifecycle management for multi-peered nodes\n addUpstreamPeer(upstreamPrefix: string): void {\n const derivedAddress = deriveChildAddress(upstreamPrefix, pubkey);\n\n // Check for collision against existing addresses\n checkAddressCollision(derivedAddress, addressRegistry.getAddresses());\n\n // Update registry\n addressRegistry.addAddress(upstreamPrefix, derivedAddress);\n\n // Update ilpInfo for kind:10032 republication\n ilpInfo.ilpAddresses = addressRegistry.getAddresses();\n ilpInfo.ilpAddress =\n addressRegistry.getPrimaryAddress() ?? ilpInfo.ilpAddress;\n\n // Register self-route in embedded connector (if available)\n if (autoCreatedConnector) {\n const nodeId = `toon-${pubkey.slice(0, 16)}`;\n autoCreatedConnector.addRoute({\n prefix: derivedAddress,\n nextHop: nodeId,\n priority: 100,\n });\n }\n\n // Trigger kind:10032 republication to propagate updated address list\n if (started && lastBootstrapResults.length > 0) {\n void bootstrapServiceInstance.republish(lastBootstrapResults);\n }\n },\n\n removeUpstreamPeer(upstreamPrefix: string): void {\n // Guard: cannot remove the last address -- a node must always have at least one\n // ILP address (AC #3: empty ilpAddresses array is rejected at construction time).\n if (\n addressRegistry.hasPrefix(upstreamPrefix) &&\n addressRegistry.size <= 1\n ) {\n throw new NodeError(\n 'Cannot remove last upstream peer: a node must have at least one ILP address'\n );\n }\n\n const removedAddress = addressRegistry.removeAddress(upstreamPrefix);\n if (removedAddress === undefined) {\n return; // No-op if prefix not found\n }\n\n // Update ilpInfo for kind:10032 republication\n ilpInfo.ilpAddresses = addressRegistry.getAddresses();\n const newPrimary = addressRegistry.getPrimaryAddress();\n if (newPrimary !== undefined) {\n ilpInfo.ilpAddress = newPrimary;\n }\n\n // Remove self-route from embedded connector (if available)\n if (autoCreatedConnector && autoCreatedConnector.removeRoute) {\n autoCreatedConnector.removeRoute(removedAddress);\n }\n\n // Trigger kind:10032 republication to propagate updated address list\n if (started && lastBootstrapResults.length > 0) {\n void bootstrapServiceInstance.republish(lastBootstrapResults);\n }\n },\n\n async claimPrefix(\n prefix: string,\n upstreamDestination: string,\n options?: { prefixPrice?: bigint }\n ): Promise<PublishEventResult> {\n // Guard: node must be started\n if (!started) {\n throw new NodeError(\n 'Cannot claim prefix: node not started. Call start() first.'\n );\n }\n\n // Validate prefix format before sending payment (fail-fast, like bid safety cap)\n const prefixValidation = validatePrefix(prefix);\n if (!prefixValidation.valid) {\n throw new NodeError(\n `Cannot claim prefix: ${prefixValidation.reason ?? 'invalid prefix'}`\n );\n }\n\n // Determine the amount: explicit override, or look up upstream's prefixPricing\n let claimAmount = options?.prefixPrice;\n if (claimAmount === undefined) {\n // Look up from discovered peers\n const peers = discoveryTrackerInstance.getAllDiscoveredPeers();\n const upstreamPeer = peers.find(\n (p) =>\n p.peerInfo.ilpAddress === upstreamDestination ||\n (p.peerInfo.ilpAddresses &&\n p.peerInfo.ilpAddresses.includes(upstreamDestination))\n );\n if (upstreamPeer?.peerInfo.prefixPricing?.basePrice) {\n claimAmount = BigInt(upstreamPeer.peerInfo.prefixPricing.basePrice);\n } else {\n throw new NodeError(\n 'Cannot claim prefix: no amount provided and upstream peer prefix pricing not found in discovery'\n );\n }\n }\n\n // Build the prefix claim event\n const claimEvent = buildPrefixClaimEvent(\n { requestedPrefix: prefix },\n config.secretKey\n );\n\n // Delegate to publishEvent with the amount override\n return node.publishEvent(claimEvent, {\n destination: upstreamDestination,\n amount: claimAmount,\n });\n },\n };\n\n return node;\n}\n","/**\n * Skill descriptor builder for DVM service discovery.\n *\n * Computes a SkillDescriptor from the handler registry and node configuration.\n * The descriptor is embedded in kind:10035 events to advertise DVM capabilities.\n *\n * Returns `undefined` when no DVM handlers are registered (backward compatible\n * with pre-DVM kind:10035 events).\n */\n\nimport { ToonError } from '@toon-protocol/core';\nimport type { SkillDescriptor, ReputationScore } from '@toon-protocol/core';\nimport type { HandlerRegistry } from './handler-registry.js';\n\n/** Regex for 64-char lowercase hex string. */\nconst HEX_64_REGEX = /^[0-9a-f]{64}$/;\n\n/** Configuration for building a skill descriptor. */\nexport interface BuildSkillDescriptorConfig {\n /** Base price per byte in USDC micro-units (default: 10n). */\n basePricePerByte?: bigint;\n /** Per-kind pricing overrides (kind number -> price in USDC micro-units). */\n kindPricing?: Record<number, bigint>;\n /** Service name override (default: 'toon-dvm'). */\n name?: string;\n /** Schema version override (default: '1.0'). */\n version?: string;\n /** Feature list override. */\n features?: string[];\n /** JSON Schema draft-07 object for job request parameters. */\n inputSchema?: Record<string, unknown>;\n /** Available AI models. */\n models?: string[];\n /** Pre-computed reputation score for the skill descriptor. */\n reputation?: ReputationScore;\n /** TEE attestation metadata for the skill descriptor. */\n attestation?: {\n /** 64-char hex event ID of the provider's latest kind:10033 attestation event. */\n eventId: string;\n /** Enclave image hash for the TEE environment. */\n enclaveImageHash: string;\n };\n}\n\n/**\n * Builds a SkillDescriptor from the handler registry and configuration.\n *\n * Returns `undefined` when the registry has no DVM handlers (kinds 5000-5999).\n * When DVM kinds exist, auto-populates the descriptor:\n * - `kinds` from `registry.getDvmKinds()`\n * - `pricing` from `config.kindPricing` overrides or `config.basePricePerByte` fallback\n * - `name`, `version`, `features`, `inputSchema`, `models` from config or defaults\n *\n * @param registry - The handler registry to read DVM kinds from.\n * @param config - Node configuration for pricing and optional overrides.\n * @returns A SkillDescriptor, or undefined if no DVM handlers are registered.\n */\nexport function buildSkillDescriptor(\n registry: HandlerRegistry,\n config: BuildSkillDescriptorConfig = {}\n): SkillDescriptor | undefined {\n const dvmKinds = registry.getDvmKinds();\n if (dvmKinds.length === 0) {\n return undefined;\n }\n\n // Validate attestation eventId if provided\n if (config.attestation !== undefined) {\n if (!HEX_64_REGEX.test(config.attestation.eventId)) {\n throw new ToonError(\n 'Skill descriptor attestation eventId must be a 64-character lowercase hex string',\n 'DVM_SKILL_INVALID_ATTESTATION_EVENT_ID'\n );\n }\n }\n\n const basePricePerByte = config.basePricePerByte ?? 10n;\n const kindPricing = config.kindPricing ?? {};\n\n // Derive pricing: per-kind overrides or basePricePerByte fallback\n const pricing: Record<string, string> = {};\n for (const kind of dvmKinds) {\n if (Object.hasOwn(kindPricing, kind)) {\n pricing[String(kind)] = String(kindPricing[kind]);\n } else {\n pricing[String(kind)] = String(basePricePerByte);\n }\n }\n\n const descriptor: SkillDescriptor = {\n name: config.name ?? 'toon-dvm',\n version: config.version ?? '1.0',\n kinds: dvmKinds,\n features: config.features ?? [],\n inputSchema: config.inputSchema ?? { type: 'object', properties: {} },\n pricing,\n };\n\n if (config.models !== undefined) {\n descriptor.models = config.models;\n }\n\n if (config.reputation !== undefined) {\n descriptor.reputation = config.reputation;\n }\n\n if (config.attestation !== undefined) {\n descriptor.attestation = {\n eventId: config.attestation.eventId,\n enclaveImageHash: config.attestation.enclaveImageHash,\n };\n }\n\n return descriptor;\n}\n","/**\n * Workflow Orchestrator for multi-step DVM pipelines (Story 6.1).\n *\n * Manages the lifecycle of a workflow chain: creates step job requests,\n * detects step completion/failure, advances to the next step, handles\n * timeouts, and settles compute payments per step.\n *\n * The orchestrator uses the TOON relay as the orchestration layer --\n * there is no separate workflow engine. Step completion is detected\n * via relay event subscriptions (Kind 6xxx results, Kind 7000 feedback).\n *\n * State machine states:\n * - `pending`: Workflow created but not yet started\n * - `step_N_running`: Step N's job request published, waiting for result\n * - `step_N_failed`: Step N failed (error or timeout), workflow aborted\n * - `completed`: All steps finished successfully\n *\n * Settlement: Each step settles individually via settleCompute().\n * The orchestrator validates sum(step_amounts) <= total_bid before settlement.\n *\n * Forward-compatible with Epic 7 prepaid protocol: settlement logic is\n * isolated in handleStepResult() so swapping to prepaid per-step payment\n * requires changes only in that method.\n */\n\nimport type { NostrEvent } from 'nostr-tools/pure';\nimport { buildJobRequestEvent, parseJobFeedback } from '@toon-protocol/core';\nimport type { ParsedWorkflowDefinition } from '@toon-protocol/core';\nimport type { ServiceNode } from './create-node.js';\n\n/**\n * Workflow state type. Uses template literal types for step-indexed states.\n * Note: Timeout is represented as `step_N_failed` (timeout is a failure mode,\n * not a separate terminal state). The timeout cause is communicated in the\n * customer notification content.\n */\nexport type WorkflowState =\n | 'pending'\n | `step_${number}_running`\n | `step_${number}_failed`\n | 'completed';\n\n/**\n * Event store interface for workflow state persistence.\n * Minimal interface -- only store() and query() are needed.\n */\nexport interface WorkflowEventStore {\n store(event: NostrEvent): Promise<void>;\n query(filter: { kinds?: number[]; '#e'?: string[] }): Promise<NostrEvent[]>;\n}\n\n/**\n * Configuration options for the WorkflowOrchestrator.\n */\nexport interface WorkflowOrchestratorOptions {\n /** Secret key for signing step job request events (32-byte Uint8Array). */\n secretKey?: Uint8Array;\n /** Per-step timeout in milliseconds (default: 300000 = 5 minutes). */\n stepTimeoutMs?: number;\n /** Injectable time source for deterministic testing. */\n now?: () => number;\n /**\n * Injectable timer factory for deterministic testing.\n * Defaults to global setTimeout. Inject a custom implementation\n * to control timer advancement in tests without vi.useFakeTimers().\n */\n setTimer?: (\n callback: () => void,\n ms: number\n ) => ReturnType<typeof setTimeout>;\n /**\n * Injectable timer cancellation for deterministic testing.\n * Defaults to global clearTimeout. Must pair with setTimer.\n */\n clearTimer?: (handle: ReturnType<typeof setTimeout>) => void;\n /** Optional event store for workflow state persistence. */\n eventStore?: WorkflowEventStore;\n /** Default destination ILP address for publishing events. */\n destination?: string;\n /** Workflow definition event ID (for referencing in notifications). */\n workflowEventId?: string;\n /** Customer pubkey (for directing notifications). */\n customerPubkey?: string;\n}\n\n/**\n * Per-step state tracking.\n */\ninterface StepState {\n /** Index of this step (0-based). */\n index: number;\n /** The job request event ID published for this step. */\n requestEventId?: string;\n /** The result event received for this step. */\n resultEvent?: NostrEvent;\n /** Whether this step has been settled. */\n settled: boolean;\n}\n\n/**\n * Orchestrates a multi-step DVM workflow chain.\n *\n * Each instance manages a single workflow. For concurrent workflows,\n * create multiple WorkflowOrchestrator instances sharing the same\n * ServiceNode.\n */\nexport class WorkflowOrchestrator {\n private readonly node: ServiceNode;\n private readonly options: Required<\n Pick<WorkflowOrchestratorOptions, 'stepTimeoutMs'>\n > &\n WorkflowOrchestratorOptions;\n\n private state: WorkflowState = 'pending';\n private definition: ParsedWorkflowDefinition | null = null;\n private workflowEventId: string = '0'.repeat(64);\n private customerPubkey: string = '0'.repeat(64);\n private currentStepIndex = 0;\n private stepStates: StepState[] = [];\n private timeoutHandle: ReturnType<typeof setTimeout> | null = null;\n private processedResultIds = new Set<string>();\n\n constructor(node: ServiceNode, options?: WorkflowOrchestratorOptions) {\n this.node = node;\n this.options = {\n stepTimeoutMs: 300_000, // 5 minutes default\n ...options,\n };\n if (options?.workflowEventId) {\n this.workflowEventId = options.workflowEventId;\n }\n if (options?.customerPubkey) {\n this.customerPubkey = options.customerPubkey;\n }\n }\n\n /**\n * Returns the current workflow state.\n */\n getState(): WorkflowState {\n return this.state;\n }\n\n /**\n * Returns per-step state tracking data (for testing/debugging).\n */\n getStepStates(): readonly Readonly<StepState>[] {\n return this.stepStates;\n }\n\n /**\n * Starts a workflow: parses the definition, creates and publishes\n * the Kind 5xxx job request for step 1, sets state to step_1_running.\n */\n async startWorkflow(definition: ParsedWorkflowDefinition): Promise<void> {\n // Guard: prevent re-entrance -- calling startWorkflow on an already-started\n // orchestrator would silently overwrite state without cleaning up timeouts.\n if (this.state !== 'pending') {\n throw new Error(\n 'WorkflowOrchestrator.startWorkflow() called on a non-pending orchestrator. ' +\n 'Create a new WorkflowOrchestrator instance for each workflow.'\n );\n }\n\n // Guard: validate steps non-empty (defense-in-depth -- the builder validates\n // this too, but the orchestrator may receive ParsedWorkflowDefinition from\n // sources other than the builder, e.g. deserialized from EventStore).\n if (!definition.steps || definition.steps.length === 0) {\n throw new Error(\n 'WorkflowOrchestrator.startWorkflow() requires at least one step in the definition.'\n );\n }\n\n this.definition = definition;\n\n // Initialize step states\n this.stepStates = definition.steps.map((_, index) => ({\n index,\n settled: false,\n }));\n\n // Store the workflow definition event if eventStore is available\n if (this.options.eventStore) {\n // Create a synthetic event representing the workflow state.\n // Use a hash-like ID derived from pubkey + timestamp to avoid collisions\n // across concurrent workflows.\n const now = this.options.now ? this.options.now() : Date.now();\n const syntheticId = `wf${now.toString(16).padStart(16, '0')}${this.node.pubkey.slice(0, 48)}`;\n const stateEvent: NostrEvent = {\n id: syntheticId.padEnd(64, '0').slice(0, 64),\n pubkey: this.node.pubkey,\n created_at: Math.floor(now / 1000),\n kind: 10040,\n content: JSON.stringify({\n state: 'started',\n definition,\n }),\n tags: [],\n sig: '0'.repeat(128),\n };\n await this.options.eventStore.store(stateEvent);\n }\n\n // Create and publish step 1\n await this.publishStepRequest(\n 0,\n definition.initialInput.data,\n definition.initialInput.type\n );\n\n // Set state\n this.state = 'step_1_running';\n this.currentStepIndex = 0;\n\n // Start timeout for step 1\n this.startStepTimeout();\n }\n\n /**\n * Handles a Kind 6xxx result event for the current step.\n *\n * If the current step matches, extracts the result content and either:\n * - Advances to the next step (publishes Kind 5xxx for step N+1)\n * - Marks workflow as completed (if this was the final step)\n *\n * Idempotent: re-processing a result for an already-advanced step is a no-op.\n */\n async handleStepResult(resultEvent: NostrEvent): Promise<void> {\n if (!this.definition) return;\n\n // Validate result event kind is in the 6000-6999 range (Kind 6xxx).\n // Without this check, any event kind could be injected as a step result.\n if (resultEvent.kind < 6000 || resultEvent.kind > 6999) return;\n\n // Idempotency: skip already-processed result events\n if (this.processedResultIds.has(resultEvent.id)) return;\n\n // Idempotency: ignore if we've already moved past this step\n const currentStep = this.stepStates[this.currentStepIndex];\n if (!currentStep) return;\n if (currentStep.resultEvent) return; // Already processed\n\n // Ignore if workflow is in a terminal state\n if (this.state === 'completed' || this.state.endsWith('_failed')) return;\n\n // NOTE: The orchestrator trusts that the caller pre-filters result events\n // to match the current step's request (via relay subscription with `e` tag\n // filter on currentStep.requestEventId). Defense-in-depth validation of the\n // `e` tag could be added here but would require callers to ensure result\n // events always reference the correct request ID.\n\n // Mark this result as processed\n this.processedResultIds.add(resultEvent.id);\n\n // Clear timeout\n this.clearStepTimeout();\n\n // Record the result\n currentStep.resultEvent = resultEvent;\n\n // Settle compute for this step\n await this.settleStep(this.currentStepIndex, resultEvent);\n\n // Store step result in eventStore if available\n if (this.options.eventStore) {\n await this.options.eventStore.store(resultEvent);\n }\n\n // Check if this was the final step\n const isLastStep =\n this.currentStepIndex === this.definition.steps.length - 1;\n\n if (isLastStep) {\n // Workflow complete\n this.state = 'completed';\n\n // Notify customer with Kind 7000 success\n await this.publishWorkflowNotification(\n 'success',\n 'Workflow completed successfully'\n );\n } else {\n // Advance to next step\n const nextIndex = this.currentStepIndex + 1;\n const resultContent = resultEvent.content;\n\n await this.publishStepRequest(nextIndex, resultContent, 'text');\n\n this.currentStepIndex = nextIndex;\n this.state = `step_${nextIndex + 1}_running`;\n\n // Start timeout for next step\n this.startStepTimeout();\n }\n }\n\n /**\n * Handles a Kind 7000 feedback event for the current step.\n *\n * If the feedback status is 'error', marks the workflow as failed\n * and notifies the customer.\n */\n async handleStepFeedback(feedbackEvent: NostrEvent): Promise<void> {\n if (!this.definition) return;\n\n // Ignore if workflow is in a terminal state\n if (this.state === 'completed' || this.state.endsWith('_failed')) return;\n\n const parsed = parseJobFeedback(feedbackEvent);\n if (!parsed) return;\n\n if (parsed.status === 'error') {\n // Clear timeout\n this.clearStepTimeout();\n\n // Mark failed\n this.state = `step_${this.currentStepIndex + 1}_failed`;\n\n // Store failure in eventStore if available\n if (this.options.eventStore) {\n await this.options.eventStore.store(feedbackEvent);\n }\n\n // Notify customer\n await this.publishWorkflowNotification(\n 'error',\n `Workflow failed at step ${this.currentStepIndex + 1}: ${parsed.content || 'Unknown error'}`\n );\n }\n }\n\n /**\n * Cleans up resources (timeout handles).\n */\n destroy(): void {\n this.clearStepTimeout();\n }\n\n // ---------- Private Methods ----------\n\n /**\n * Publishes a Kind 5xxx job request for the given step.\n */\n private async publishStepRequest(\n stepIndex: number,\n inputData: string,\n inputType: string\n ): Promise<void> {\n if (!this.definition) return;\n\n const step = this.definition.steps[stepIndex];\n if (!step) return;\n\n // Compute bid for this step\n const stepBid = this.getStepBid(stepIndex);\n\n // Build the job request event\n const jobRequest = buildJobRequestEvent(\n {\n kind: step.kind,\n input: { data: inputData, type: inputType },\n bid: stepBid,\n output: 'text/plain',\n content: step.description,\n targetProvider: step.targetProvider,\n },\n this.getSecretKey()\n );\n\n // Publish via the node's publishEvent (which handles TOON encoding + ILP)\n const destination = this.options.destination ?? 'g.toon.local';\n await this.node.publishEvent(jobRequest, { destination });\n\n // Record the request event ID\n const stepState = this.stepStates[stepIndex];\n if (stepState) {\n stepState.requestEventId = jobRequest.id;\n }\n }\n\n /**\n * Settles compute payment for a completed step.\n */\n private async settleStep(\n stepIndex: number,\n resultEvent: NostrEvent\n ): Promise<void> {\n if (!this.definition) return;\n\n const stepState = this.stepStates[stepIndex];\n if (!stepState || stepState.settled) return;\n\n const step = this.definition.steps[stepIndex];\n if (!step) return;\n\n // Determine provider ILP address from result event pubkey or fallback.\n // In production, this would be resolved from the provider's kind:10035\n // service discovery event. For now, derive from the destination prefix.\n const providerIlpAddress = this.options.destination ?? 'g.toon.provider';\n\n try {\n await this.node.settleCompute(resultEvent, providerIlpAddress, {\n originalBid: this.getStepBid(stepIndex),\n });\n stepState.settled = true;\n } catch (_err: unknown) {\n // Settlement failure does not block workflow advancement.\n // The orchestrator prioritizes workflow progress over payment atomicity.\n // In production, the caller should monitor stepState.settled per step\n // and reconcile unsettled steps out-of-band.\n }\n }\n\n /**\n * Returns the bid allocation for a specific step.\n * Uses explicit bidAllocation if set, otherwise proportional split.\n */\n private getStepBid(stepIndex: number): string {\n if (!this.definition) return '0';\n\n const step = this.definition.steps[stepIndex];\n if (!step) return '0';\n\n // Use explicit allocation if available\n if (step.bidAllocation !== undefined) {\n return step.bidAllocation;\n }\n\n // Proportional split -- wrap BigInt conversion in try/catch since\n // totalBid may come from deserialized EventStore data that was not\n // validated by the builder.\n try {\n const totalBid = BigInt(this.definition.totalBid);\n const stepCount = BigInt(this.definition.steps.length);\n const perStep = totalBid / stepCount;\n return perStep.toString();\n } catch {\n return '0';\n }\n }\n\n /**\n * Publishes a Kind 7000 workflow notification to the customer.\n */\n private async publishWorkflowNotification(\n status: 'success' | 'error',\n content: string\n ): Promise<void> {\n const destination = this.options.destination ?? 'g.toon.local';\n try {\n await this.node.publishFeedback(\n this.workflowEventId,\n this.customerPubkey,\n status,\n content,\n { destination }\n );\n } catch (_err: unknown) {\n // Best-effort notification -- don't fail the workflow.\n // Notification delivery is not guaranteed; callers should\n // check orchestrator.getState() for authoritative status.\n }\n }\n\n /**\n * Starts a timeout timer for the current step.\n */\n private startStepTimeout(): void {\n this.clearStepTimeout();\n\n const timeoutMs = this.options.stepTimeoutMs;\n const timerFn = this.options.setTimer ?? setTimeout;\n\n this.timeoutHandle = timerFn(() => {\n // Mark as failed due to timeout\n this.state = `step_${this.currentStepIndex + 1}_failed`;\n\n // Notify customer\n void this.publishWorkflowNotification(\n 'error',\n `Workflow timed out at step ${this.currentStepIndex + 1} after ${timeoutMs}ms`\n );\n }, timeoutMs);\n }\n\n /**\n * Returns the secret key for signing step events.\n * Falls back to a deterministic key derived from the node pubkey\n * (for testing -- production should always pass secretKey in options).\n */\n private getSecretKey(): Uint8Array {\n if (this.options.secretKey) {\n return this.options.secretKey;\n }\n // Fallback: use a fixed test key (this path should not be hit in production)\n throw new Error(\n 'WorkflowOrchestrator requires secretKey in options to sign step events'\n );\n }\n\n /**\n * Clears the current step timeout.\n */\n private clearStepTimeout(): void {\n if (this.timeoutHandle !== null) {\n const cancelFn = this.options.clearTimer ?? clearTimeout;\n cancelFn(this.timeoutHandle);\n this.timeoutHandle = null;\n }\n }\n}\n","/**\n * Swarm Coordinator for competitive DVM bidding (Story 6.2).\n *\n * Manages the lifecycle of a competitive swarm: collects provider\n * submissions, handles timeout-based judging deadlines, validates\n * winner selection, and settles compute payment to the winner only.\n *\n * The coordinator uses the TOON relay as the orchestration layer --\n * there is no separate swarm engine. Submission arrival is detected\n * via relay event subscriptions (Kind 6xxx results).\n *\n * State machine states:\n * - `collecting`: Waiting for provider submissions (Kind 6xxx results)\n * - `judging`: Timeout or max submissions reached; customer selects winner\n * - `settled`: Winner paid via settleCompute(); swarm complete\n * - `failed`: No submissions within timeout or error\n *\n * Settlement: Only the winning provider receives compute payment.\n * Losing providers paid relay write fees (sunk cost) but receive\n * no compute payment -- this is by design to incentivize quality.\n *\n * Forward-compatible with Epic 7 prepaid protocol: settlement logic\n * is isolated in selectWinner() so swapping to prepaid per-winner\n * payment requires changes only in that method.\n */\n\nimport type { NostrEvent } from 'nostr-tools/pure';\nimport {\n ToonError,\n parseSwarmRequest,\n parseSwarmSelection,\n} from '@toon-protocol/core';\nimport type { ServiceNode } from './create-node.js';\nimport type { WorkflowEventStore } from './workflow-orchestrator.js';\n\n/**\n * Swarm state type.\n * - `collecting`: Waiting for provider submissions.\n * - `judging`: Timeout or max submissions reached; awaiting winner selection.\n * - `settled`: Winner paid; swarm complete.\n * - `failed`: No submissions within timeout or error.\n */\nexport type SwarmState = 'collecting' | 'judging' | 'settled' | 'failed';\n\n/**\n * Configuration options for the SwarmCoordinator.\n */\nexport interface SwarmCoordinatorOptions {\n /** Secret key for signing events (32-byte Uint8Array). */\n secretKey?: Uint8Array;\n /** Swarm collection timeout in milliseconds (default: 600000 = 10 minutes). */\n timeoutMs?: number;\n /** Injectable time source for deterministic testing. */\n now?: () => number;\n /**\n * Injectable timer factory for deterministic testing.\n * Defaults to global setTimeout. Inject a custom implementation\n * to control timer advancement in tests without vi.useFakeTimers().\n */\n setTimer?: (\n callback: () => void,\n ms: number\n ) => ReturnType<typeof setTimeout>;\n /**\n * Injectable timer cancellation for deterministic testing.\n * Defaults to global clearTimeout. Must pair with setTimer.\n */\n clearTimer?: (handle: ReturnType<typeof setTimeout>) => void;\n /** Optional event store for swarm state persistence. */\n eventStore?: WorkflowEventStore;\n /** Default destination ILP address for publishing events. */\n destination?: string;\n}\n\n/**\n * Coordinates a competitive DVM swarm.\n *\n * Each instance manages a single swarm. For concurrent swarms,\n * create multiple SwarmCoordinator instances sharing the same\n * ServiceNode.\n */\nexport class SwarmCoordinator {\n private readonly node: ServiceNode;\n private readonly options: Required<\n Pick<SwarmCoordinatorOptions, 'timeoutMs'>\n > &\n SwarmCoordinatorOptions;\n\n private state: SwarmState = 'collecting';\n private swarmRequestId = '';\n private customerPubkey = '';\n private maxProviders = 0;\n private submissions: NostrEvent[] = [];\n private timeoutHandle: ReturnType<typeof setTimeout> | null = null;\n private started = false;\n private settlementSucceeded = false;\n private submissionIds = new Set<string>();\n\n constructor(node: ServiceNode, options?: SwarmCoordinatorOptions) {\n this.node = node;\n this.options = {\n timeoutMs: 600_000, // 10 minutes default\n ...options,\n };\n }\n\n /**\n * Returns the current swarm state.\n */\n getState(): SwarmState {\n return this.state;\n }\n\n /**\n * Returns whether settlement was successfully completed.\n * Only meaningful when state is 'settled' (returns true).\n * If settlement fails, the state remains 'judging' and this returns false.\n */\n isSettlementSucceeded(): boolean {\n return this.settlementSucceeded;\n }\n\n /**\n * Returns the collected eligible submissions.\n */\n getSubmissions(): readonly NostrEvent[] {\n return this.submissions;\n }\n\n /**\n * Starts a swarm: parses the swarm request, initializes collection,\n * and starts the timeout timer.\n */\n async startSwarm(swarmRequest: NostrEvent): Promise<void> {\n const parsed = parseSwarmRequest(swarmRequest);\n if (!parsed) {\n throw new ToonError(\n 'Invalid swarm request event: missing swarm tag or invalid Kind 5xxx',\n 'DVM_INVALID_KIND'\n );\n }\n\n this.swarmRequestId = swarmRequest.id;\n this.customerPubkey = swarmRequest.pubkey;\n this.maxProviders = parsed.maxProviders;\n this.state = 'collecting';\n this.submissions = [];\n this.submissionIds = new Set<string>();\n this.started = true;\n\n // Store the swarm request in EventStore if available\n if (this.options.eventStore) {\n await this.options.eventStore.store(swarmRequest);\n }\n\n // Start timeout timer\n this.startTimeout();\n }\n\n /**\n * Handles a Kind 6xxx result event (provider submission).\n *\n * Validates the `e` tag references the swarm request, adds to the\n * submissions list, and checks if max providers is reached.\n *\n * Late submissions (after timeout or max reached) are ignored.\n * Submissions before startSwarm() are silently ignored.\n */\n async handleSubmission(resultEvent: NostrEvent): Promise<void> {\n if (!this.started) return;\n\n // Only accept submissions during collecting state\n if (this.state !== 'collecting') return;\n\n // Validate result event kind is in the 6000-6999 range\n if (resultEvent.kind < 6000 || resultEvent.kind > 6999) return;\n\n // Validate e tag references our swarm request\n const eTag = resultEvent.tags.find((t: string[]) => t[0] === 'e');\n if (!eTag) return;\n const requestEventId = eTag[1];\n if (requestEventId === undefined || requestEventId !== this.swarmRequestId)\n return;\n\n // Deduplicate by event ID -- relay normally handles dedup, but the\n // coordinator must also guard against duplicate submissions reaching\n // the maxProviders threshold with the same event repeated.\n if (this.submissionIds.has(resultEvent.id)) return;\n this.submissionIds.add(resultEvent.id);\n\n // Add to submissions\n this.submissions.push(resultEvent);\n\n // Store in EventStore if available\n if (this.options.eventStore) {\n await this.options.eventStore.store(resultEvent);\n }\n\n // Check if max providers reached\n if (this.submissions.length >= this.maxProviders) {\n this.clearTimeout();\n this.state = 'judging';\n }\n }\n\n /**\n * Selects the winner and settles compute payment.\n *\n * Validates:\n * - Swarm is in `judging` state (not `collecting`, `settled`, or `failed`)\n * - Selection references a submission in the collected set\n * - Swarm has not already been settled (idempotency guard)\n *\n * @throws ToonError with code DVM_SWARM_ALREADY_SETTLED if already settled\n * @throws ToonError with code DVM_SWARM_INVALID_SELECTION if winner not in submissions\n */\n async selectWinner(selectionEvent: NostrEvent): Promise<void> {\n // Idempotency guard: reject if already settled\n if (this.state === 'settled') {\n throw new ToonError(\n 'Swarm has already been settled; duplicate selection rejected',\n 'DVM_SWARM_ALREADY_SETTLED'\n );\n }\n\n // Must be in judging state\n if (this.state !== 'judging') {\n throw new ToonError(\n `Cannot select winner in state '${this.state}'; swarm must be in 'judging' state`,\n 'DVM_SWARM_INVALID_SELECTION'\n );\n }\n\n // Parse the selection event to extract the winner\n // Validate that the selection event is from the swarm's customer\n if (selectionEvent.pubkey !== this.customerPubkey) {\n throw new ToonError(\n 'Selection event pubkey does not match the swarm request customer pubkey',\n 'DVM_SWARM_INVALID_SELECTION'\n );\n }\n\n const parsed = parseSwarmSelection(selectionEvent);\n if (!parsed) {\n throw new ToonError(\n 'Invalid swarm selection event: missing winner tag or invalid Kind 7000',\n 'DVM_SWARM_INVALID_SELECTION'\n );\n }\n\n // Validate that the selection references this swarm (not a different one)\n if (parsed.swarmRequestEventId !== this.swarmRequestId) {\n throw new ToonError(\n `Selection event references swarm '${parsed.swarmRequestEventId}' but this swarm is '${this.swarmRequestId}'`,\n 'DVM_SWARM_INVALID_SELECTION'\n );\n }\n\n // Validate the winner references a collected submission\n const winnerSubmission = this.submissions.find(\n (s) => s.id === parsed.winnerResultEventId\n );\n if (!winnerSubmission) {\n throw new ToonError(\n `Winner result event ID '${parsed.winnerResultEventId}' not found in collected submissions`,\n 'DVM_SWARM_INVALID_SELECTION'\n );\n }\n\n // Settle compute payment to the winning provider only\n const providerIlpAddress = this.options.destination ?? 'g.toon.provider';\n try {\n await this.node.settleCompute(winnerSubmission, providerIlpAddress);\n this.settlementSucceeded = true;\n } catch (_err: unknown) {\n // Settlement failed -- remain in 'judging' state so the caller can\n // retry selectWinner() with the same or different selection event.\n // Do NOT transition to 'settled' because no payment was made.\n this.settlementSucceeded = false;\n throw new ToonError(\n 'Settlement failed for winning provider; swarm remains in judging state for retry',\n 'DVM_SWARM_SETTLEMENT_FAILED'\n );\n }\n\n // Store selection in EventStore if available\n if (this.options.eventStore) {\n await this.options.eventStore.store(selectionEvent);\n }\n\n this.state = 'settled';\n }\n\n /**\n * Cleans up resources (timeout handles).\n */\n destroy(): void {\n this.clearTimeout();\n }\n\n // ---------- Private Methods ----------\n\n /**\n * Starts the collection timeout timer.\n */\n private startTimeout(): void {\n this.clearTimeout();\n\n const timeoutMs = this.options.timeoutMs;\n const timerFn = this.options.setTimer ?? setTimeout;\n\n this.timeoutHandle = timerFn(() => {\n if (this.state !== 'collecting') return;\n\n if (this.submissions.length === 0) {\n // No submissions -- transition to failed\n this.state = 'failed';\n\n // Publish \"no submissions\" Kind 7000 feedback to customer\n void this.publishNoSubmissionsFeedback();\n } else {\n // Some submissions collected -- transition to judging\n this.state = 'judging';\n }\n }, timeoutMs);\n }\n\n /**\n * Publishes a Kind 7000 feedback event indicating no submissions were received.\n */\n private async publishNoSubmissionsFeedback(): Promise<void> {\n const destination = this.options.destination ?? 'g.toon.local';\n try {\n await this.node.publishFeedback(\n this.swarmRequestId,\n this.customerPubkey,\n 'error',\n 'Swarm timed out with no submissions',\n { destination }\n );\n } catch (_err: unknown) {\n // Best-effort notification -- don't fail the state transition.\n }\n }\n\n /**\n * Clears the current timeout timer.\n */\n private clearTimeout(): void {\n if (this.timeoutHandle !== null) {\n const cancelFn = this.options.clearTimer ?? clearTimeout;\n cancelFn(this.timeoutHandle);\n this.timeoutHandle = null;\n }\n }\n}\n","/**\n * Prefix claim handler for the TOON prefix marketplace.\n *\n * Creates a handler that processes kind 10034 prefix claim events,\n * validates payment and prefix availability, and publishes grant\n * confirmations. Lives in SDK (not core) because it depends on\n * the SDK's Handler type and HandlerContext interface.\n */\n\nimport type { NostrEvent } from 'nostr-tools/pure';\nimport {\n parsePrefixClaimEvent,\n buildPrefixGrantEvent,\n validatePrefix,\n} from '@toon-protocol/core';\nimport type { Handler, HandlerResponse } from './handler-registry.js';\nimport type { HandlerContext } from './handler-context.js';\n\n/**\n * Options for creating a prefix claim handler.\n */\nexport interface PrefixClaimHandlerOptions {\n /** Pricing configuration for prefix claims. */\n prefixPricing: { basePrice: bigint };\n /** Secret key for signing grant events. */\n secretKey: Uint8Array;\n /** Returns the current map of claimed prefixes (prefix -> claimer pubkey). */\n getClaimedPrefixes: () => Map<string, string>;\n /**\n * Atomically claim a prefix for a pubkey. Returns true if the claim\n * succeeded, false if the prefix was already taken. This is the\n * serialization point for race condition defense.\n */\n claimPrefix: (prefix: string, claimerPubkey: string) => boolean;\n /** Publish the grant event (e.g., to the local relay). */\n publishGrant: (grantEvent: NostrEvent) => Promise<void>;\n /** ILP address prefix to include in the grant event (default: 'g.toon'). */\n ilpAddressPrefix?: string;\n}\n\n/**\n * Creates a handler for kind 10034 prefix claim events.\n *\n * The handler validates payment, prefix availability, and prefix format,\n * then atomically claims the prefix and publishes a grant confirmation.\n *\n * @param options - Handler configuration\n * @returns A Handler function for kind 10034 events\n */\nexport function createPrefixClaimHandler(\n options: PrefixClaimHandlerOptions\n): Handler {\n return async (ctx: HandlerContext): Promise<HandlerResponse> => {\n // 1. Parse the incoming event as PrefixClaimContent\n const event = ctx.decode();\n const claim = parsePrefixClaimEvent(event);\n if (!claim) {\n return ctx.reject('F06', 'Invalid prefix claim event: malformed content');\n }\n\n // 2. Validate the prefix format\n const validation = validatePrefix(claim.requestedPrefix);\n if (!validation.valid) {\n return ctx.reject(\n 'F06',\n `Invalid prefix: ${validation.reason ?? 'unknown validation error'}`\n );\n }\n\n // 3. Check payment is sufficient\n if (ctx.amount < options.prefixPricing.basePrice) {\n return ctx.reject(\n 'F06',\n `Insufficient payment: received ${ctx.amount}, required ${options.prefixPricing.basePrice}`\n );\n }\n\n // 4. Check prefix availability\n const claimedPrefixes = options.getClaimedPrefixes();\n if (claimedPrefixes.has(claim.requestedPrefix)) {\n return ctx.reject(\n 'F06',\n `PREFIX_TAKEN: prefix \"${claim.requestedPrefix}\" is already claimed`\n );\n }\n\n // 5. Atomically claim the prefix (serialization point for race conditions)\n const claimed = options.claimPrefix(claim.requestedPrefix, event.pubkey);\n if (!claimed) {\n return ctx.reject(\n 'F06',\n `PREFIX_TAKEN: prefix \"${claim.requestedPrefix}\" is already claimed`\n );\n }\n\n // 6. Build and publish grant event\n const prefix = options.ilpAddressPrefix ?? 'g.toon';\n const ilpAddress = `${prefix}.${claim.requestedPrefix}`;\n const grantEvent = buildPrefixGrantEvent(\n {\n grantedPrefix: claim.requestedPrefix,\n claimerPubkey: event.pubkey,\n ilpAddress,\n },\n options.secretKey\n );\n await options.publishGrant(grantEvent);\n\n // 7. Accept the ILP packet\n return ctx.accept();\n };\n}\n","/**\n * Arweave DVM handler for kind:5094 blob storage requests.\n *\n * Creates a handler function that:\n * 1. Parses the kind:5094 event from the HandlerContext\n * 2. For single-packet uploads: uploads directly to Arweave via the adapter\n * 3. For chunked uploads: accumulates via ChunkManager, uploads on completion\n *\n * The handler does NOT validate pricing -- the SDK pricing validator\n * (packages/sdk/src/pricing-validator.ts) runs BEFORE the handler is invoked.\n *\n * Returns HandlePacketAcceptResponse with `data` field containing the Arweave\n * tx ID -- NOT via ctx.accept() which only supports metadata.\n */\n\nimport { parseBlobStorageRequest } from '@toon-protocol/core';\nimport type { HandlerContext } from '../handler-context.js';\nimport type { HandlerResponse } from '../handler-registry.js';\nimport type { ArweaveUploadAdapter } from './turbo-adapter.js';\nimport type { ChunkManager } from './chunk-manager.js';\n\n/**\n * Validates and sanitizes a MIME content type string.\n * Returns sanitized type or 'application/octet-stream' for invalid values.\n *\n * Defends against header injection (CWE-113) and ensures only well-formed\n * MIME types are forwarded to the Arweave upload adapter.\n */\nconst MIME_REGEX =\n /^[a-zA-Z0-9][a-zA-Z0-9!#$&\\-^_.+]*\\/[a-zA-Z0-9][a-zA-Z0-9!#$&\\-^_.+]*$/;\nfunction sanitizeContentType(contentType: string): string {\n // Strip any parameters (e.g., \"; charset=utf-8\") and whitespace\n const base = (contentType.split(';')[0] ?? '').trim();\n if (base.length > 0 && base.length <= 255 && MIME_REGEX.test(base)) {\n return base;\n }\n return 'application/octet-stream';\n}\n\nexport interface ArweaveDvmConfig {\n /** Arweave upload adapter (wraps @ardrive/turbo-sdk). */\n turboAdapter: ArweaveUploadAdapter;\n /** Chunk state manager for multi-packet uploads. */\n chunkManager: ChunkManager;\n /** Optional default Arweave data item tags (e.g., Content-Type). */\n arweaveTags?: Record<string, string>;\n}\n\n/**\n * Creates an Arweave DVM handler for kind:5094 blob storage requests.\n *\n * @param config - Handler configuration with adapter and chunk manager.\n * @returns A handler function compatible with HandlerRegistry.on().\n */\nexport function createArweaveDvmHandler(\n config: ArweaveDvmConfig\n): (ctx: HandlerContext) => Promise<HandlerResponse> {\n const { turboAdapter, chunkManager, arweaveTags } = config;\n\n return async (ctx: HandlerContext): Promise<HandlerResponse> => {\n // Decode the TOON payload into a full NostrEvent\n const event = ctx.decode();\n\n // Parse the kind:5094 event\n const parsed = parseBlobStorageRequest(event);\n if (!parsed) {\n return {\n accept: false,\n code: 'F00',\n message: 'Malformed kind:5094 blob storage request',\n };\n }\n\n // Build tags for Arweave upload.\n // Event-level Content-Type takes precedence over config-level arweaveTags.\n // Sanitize to prevent header injection (CWE-113) from user-controlled input.\n const uploadTags: Record<string, string> = {\n ...arweaveTags,\n 'Content-Type': sanitizeContentType(parsed.contentType),\n };\n\n // Check if this is a chunked upload\n if (\n parsed.uploadId !== undefined &&\n parsed.chunkIndex !== undefined &&\n parsed.totalChunks !== undefined\n ) {\n try {\n const result = chunkManager.addChunk(\n parsed.uploadId,\n parsed.chunkIndex,\n parsed.totalChunks,\n parsed.blobData\n );\n\n if (!result.complete) {\n // Intermediate chunk -- acknowledge receipt\n return {\n accept: true,\n data: Buffer.from(`ack:${parsed.chunkIndex}`).toString('base64'),\n };\n }\n\n // All chunks received -- upload assembled blob\n // assembled is guaranteed to be present when complete is true\n const assembled = result.assembled ?? Buffer.alloc(0);\n const { txId } = await turboAdapter.upload(assembled, uploadTags);\n\n // Base64-encode txId for ILP FULFILL data field (connector validates base64)\n return {\n accept: true,\n data: Buffer.from(txId).toString('base64'),\n };\n } catch (error) {\n // Log full error internally but return generic message to client (CWE-209)\n const internalMsg =\n error instanceof Error ? error.message : 'Unknown chunk error';\n // Only surface safe, expected error categories to callers\n const safeMsg = internalMsg.includes('Duplicate chunkIndex')\n ? 'Duplicate chunk rejected'\n : internalMsg.includes('Max active uploads')\n ? 'Server busy, too many concurrent uploads'\n : internalMsg.includes('exceeds max bytes')\n ? 'Upload size limit exceeded'\n : 'Chunk processing error';\n return {\n accept: false,\n code: 'F00',\n message: safeMsg,\n };\n }\n }\n\n // Single-packet upload\n try {\n const { txId } = await turboAdapter.upload(parsed.blobData, uploadTags);\n // Base64-encode txId for ILP FULFILL data field (connector validates base64)\n return {\n accept: true,\n data: Buffer.from(txId).toString('base64'),\n };\n } catch {\n // Generic message to avoid leaking Arweave SDK internals (CWE-209)\n return {\n accept: false,\n code: 'T00',\n message: 'Arweave upload failed',\n };\n }\n };\n}\n","/**\n * Arweave upload adapter wrapping @ardrive/turbo-sdk.\n *\n * This is the ONLY file that imports @ardrive/turbo-sdk, isolating the\n * external dependency behind an interface (risk E8-R002).\n */\n\nimport { Readable } from 'node:stream';\n\n/**\n * Interface for uploading data to Arweave.\n * Implementations wrap a specific Arweave upload SDK.\n */\nexport interface ArweaveUploadAdapter {\n upload(\n data: Buffer,\n tags?: Record<string, string>\n ): Promise<{ txId: string }>;\n}\n\n/**\n * Turbo SDK upload adapter using @ardrive/turbo-sdk.\n *\n * - Dev/free tier (<=100KB): pass no turboClient, uses TurboFactory.unauthenticated()\n * - Prod (paid, uncapped): pass a TurboAuthenticatedClient from TurboFactory.authenticated()\n *\n * The turboClient is typed as `unknown` to avoid importing @ardrive/turbo-sdk types\n * in this interface. The actual TurboAuthenticatedClient or TurboUnauthenticatedClient\n * is duck-typed at runtime.\n */\nexport class TurboUploadAdapter implements ArweaveUploadAdapter {\n private turboClient: unknown;\n private initialized = false;\n\n constructor(turboClient?: unknown) {\n if (turboClient) {\n this.turboClient = turboClient;\n this.initialized = true;\n }\n }\n\n private async getClient(): Promise<unknown> {\n if (!this.initialized) {\n // Lazy-import to avoid loading @ardrive/turbo-sdk unless actually used\n const { TurboFactory } = await import('@ardrive/turbo-sdk/node');\n const Arweave = (await import('arweave')).default;\n const arweave = Arweave.init({});\n const jwk = await arweave.crypto.generateJWK();\n this.turboClient = TurboFactory.authenticated({ privateKey: jwk });\n this.initialized = true;\n }\n return this.turboClient;\n }\n\n async upload(\n data: Buffer,\n tags?: Record<string, string>\n ): Promise<{ txId: string }> {\n const client = (await this.getClient()) as {\n uploadFile(params: {\n fileStreamFactory: () => Readable;\n fileSizeFactory: () => number;\n dataItemOpts?: { tags: { name: string; value: string }[] };\n }): Promise<{ id: string }>;\n };\n\n // Convert Record<string, string> to tag array format\n const tagArray: { name: string; value: string }[] = [];\n if (tags) {\n for (const [name, value] of Object.entries(tags)) {\n tagArray.push({ name, value });\n }\n }\n\n const result = await client.uploadFile({\n fileStreamFactory: () => Readable.from(data),\n fileSizeFactory: () => data.length,\n ...(tagArray.length > 0 ? { dataItemOpts: { tags: tagArray } } : {}),\n });\n\n return { txId: result.id };\n }\n}\n","/**\n * Chunk state manager for multi-packet blob uploads.\n *\n * Accumulates chunks for a given uploadId, assembles them in order\n * when all chunks have arrived, and enforces timeout and memory caps.\n */\n\nexport interface ChunkManagerConfig {\n /** Timeout in milliseconds before partial uploads are discarded (default: 300_000 = 5 min). */\n timeoutMs?: number;\n /** Maximum number of concurrent active uploads (default: 100). */\n maxActiveUploads?: number;\n /** Maximum total bytes accumulated per upload before rejection (default: 50MB). */\n maxBytesPerUpload?: number;\n}\n\nexport interface AddChunkResult {\n /** Whether all chunks have been received and the blob is fully assembled. */\n complete: boolean;\n /** The assembled blob (only present when complete is true). */\n assembled?: Buffer;\n /** Error message if the chunk was rejected. */\n error?: string;\n}\n\ninterface UploadState {\n chunks: Map<number, Buffer>;\n totalChunks: number;\n timer: ReturnType<typeof setTimeout>;\n}\n\n/**\n * Manages chunked upload state for the Arweave DVM handler.\n */\nexport class ChunkManager {\n private uploads = new Map<string, UploadState>();\n private readonly timeoutMs: number;\n private readonly maxActiveUploads: number;\n private readonly maxBytesPerUpload: number;\n\n constructor(config: ChunkManagerConfig = {}) {\n this.timeoutMs = config.timeoutMs ?? 300_000;\n this.maxActiveUploads = config.maxActiveUploads ?? 100;\n this.maxBytesPerUpload = config.maxBytesPerUpload ?? 50 * 1024 * 1024; // 50MB\n }\n\n /**\n * Add a chunk for a given uploadId.\n *\n * @param uploadId - Unique identifier for the upload session.\n * @param chunkIndex - Zero-based index of this chunk.\n * @param totalChunks - Total number of chunks expected.\n * @param data - The chunk data.\n * @returns Result indicating completion status or error.\n * @throws Error if memory cap reached or duplicate chunk.\n */\n addChunk(\n uploadId: string,\n chunkIndex: number,\n totalChunks: number,\n data: Buffer\n ): AddChunkResult {\n let state = this.uploads.get(uploadId);\n\n if (!state) {\n // Check memory cap before creating new upload\n if (this.uploads.size >= this.maxActiveUploads) {\n throw new Error(\n `Max active uploads reached (${this.maxActiveUploads}). Cannot accept new uploadId.`\n );\n }\n\n // Create new upload state with timeout\n const timer = setTimeout(() => {\n this.cleanup(uploadId);\n }, this.timeoutMs);\n\n state = {\n chunks: new Map(),\n totalChunks,\n timer,\n };\n this.uploads.set(uploadId, state);\n }\n\n // Validate chunkIndex is within bounds\n if (chunkIndex < 0 || chunkIndex >= state.totalChunks) {\n throw new Error(\n `chunkIndex ${chunkIndex} out of bounds for totalChunks ${state.totalChunks} (uploadId ${uploadId})`\n );\n }\n\n // Check for duplicate chunk\n if (state.chunks.has(chunkIndex)) {\n throw new Error(\n `Duplicate chunkIndex ${chunkIndex} for uploadId ${uploadId}`\n );\n }\n\n // Check per-upload byte limit before storing\n let currentBytes = 0;\n for (const chunk of state.chunks.values()) {\n currentBytes += chunk.length;\n }\n if (currentBytes + data.length > this.maxBytesPerUpload) {\n this.cleanup(uploadId);\n throw new Error(\n `Upload ${uploadId} exceeds max bytes per upload (${this.maxBytesPerUpload})`\n );\n }\n\n // Store chunk\n state.chunks.set(chunkIndex, data);\n\n // Check if all chunks received\n if (state.chunks.size === state.totalChunks) {\n // Assemble in correct order (0, 1, 2, ...)\n const orderedChunks: Buffer[] = [];\n for (let i = 0; i < state.totalChunks; i++) {\n const chunk = state.chunks.get(i);\n if (!chunk) {\n throw new Error(`Missing chunk ${i} for upload`);\n }\n orderedChunks.push(chunk);\n }\n const assembled = Buffer.concat(orderedChunks);\n\n // Clean up state\n this.cleanup(uploadId);\n\n return { complete: true, assembled };\n }\n\n return { complete: false };\n }\n\n /**\n * Check if an upload is complete (all chunks received).\n * Returns false if the uploadId is unknown (cleaned up or never started).\n */\n isComplete(uploadId: string): boolean {\n const state = this.uploads.get(uploadId);\n if (!state) return false;\n return state.chunks.size === state.totalChunks;\n }\n\n /**\n * Clean up state for a given uploadId, clearing the timeout timer.\n */\n cleanup(uploadId: string): void {\n const state = this.uploads.get(uploadId);\n if (state) {\n clearTimeout(state.timer);\n this.uploads.delete(uploadId);\n }\n }\n\n /**\n * Clean up all active uploads and their timers.\n * Call this on node shutdown to prevent timer leaks.\n */\n destroyAll(): void {\n for (const [, state] of this.uploads) {\n clearTimeout(state.timer);\n }\n this.uploads.clear();\n }\n}\n","/**\n * Client-side helpers for uploading blobs to an Arweave DVM provider.\n *\n * - uploadBlob(): single-packet upload for blobs under the chunk threshold\n * - uploadBlobChunked(): multi-packet upload that splits large blobs into chunks\n *\n * Both use publishEvent() with the amount override (Story 7.6, D7-007).\n */\n\nimport { randomUUID } from 'node:crypto';\nimport type { NostrEvent } from 'nostr-tools/pure';\nimport { buildBlobStorageRequest } from '@toon-protocol/core';\nimport type { PublishEventResult } from '../create-node.js';\n\n/**\n * Minimal interface for publishing events. Matches ServiceNode.publishEvent().\n */\nexport interface PublishableNode {\n publishEvent(\n event: NostrEvent,\n options?: { destination: string; amount?: bigint }\n ): Promise<\n Pick<PublishEventResult, 'success' | 'eventId' | 'data' | 'message'>\n >;\n}\n\n/**\n * Options for single-packet blob upload.\n */\nexport interface UploadBlobOptions {\n /** MIME type of the blob (default: 'application/octet-stream'). */\n contentType?: string;\n /** Secret key for signing the event. */\n secretKey: Uint8Array;\n /** Price per byte in USDC micro-units for amount calculation. */\n pricePerByte?: bigint;\n}\n\n/**\n * Options for chunked blob upload.\n */\nexport interface UploadBlobChunkedOptions {\n /** Chunk size in bytes (default: 500_000, under 512KB threshold). */\n chunkSize?: number;\n /** MIME type of the blob (default: 'application/octet-stream'). */\n contentType?: string;\n /** Secret key for signing events. */\n secretKey: Uint8Array;\n /** Price per byte in USDC micro-units for amount calculation. */\n pricePerByte?: bigint;\n}\n\n/**\n * Upload a blob to an Arweave DVM provider in a single ILP packet.\n *\n * Uses publishEvent() with amount override (D7-007) to send the blob\n * and payment in one ILP PREPARE.\n *\n * @param node - The ServiceNode (or compatible) to publish through.\n * @param blob - The raw blob data to upload.\n * @param destination - The provider's ILP address.\n * @param options - Upload options including secretKey for signing.\n * @returns The Arweave transaction ID from the FULFILL data field.\n */\nexport async function uploadBlob(\n node: PublishableNode,\n blob: Buffer,\n destination: string,\n options: UploadBlobOptions\n): Promise<string> {\n const contentType = options.contentType ?? 'application/octet-stream';\n const pricePerByte = options.pricePerByte ?? 10n;\n const amount = BigInt(blob.length) * pricePerByte;\n\n const event = buildBlobStorageRequest(\n {\n blobData: blob,\n contentType,\n bid: amount.toString(),\n },\n options.secretKey\n );\n\n const result = await node.publishEvent(event, { destination, amount });\n\n if (!result.success) {\n throw new Error(`Blob upload failed: ${result.message ?? 'unknown error'}`);\n }\n\n // The tx ID is returned base64-encoded in the FULFILL data field.\n // Decode it to get the raw Arweave tx ID string.\n if (result.data) {\n return Buffer.from(result.data, 'base64').toString('utf-8');\n }\n return result.eventId;\n}\n\n/**\n * Upload a large blob to an Arweave DVM provider using chunked uploads.\n *\n * Splits the blob into chunks, each sent as a separate kind:5094 ILP PREPARE\n * with uploadId, chunkIndex, and totalChunks params. Each chunk carries its\n * own payment. The final chunk's FULFILL data contains the Arweave tx ID.\n *\n * @param node - The ServiceNode (or compatible) to publish through.\n * @param blob - The raw blob data to upload.\n * @param destination - The provider's ILP address.\n * @param options - Chunked upload options including secretKey for signing.\n * @returns The Arweave transaction ID from the final chunk's FULFILL data.\n */\nexport async function uploadBlobChunked(\n node: PublishableNode,\n blob: Buffer,\n destination: string,\n options: UploadBlobChunkedOptions\n): Promise<string> {\n if (blob.length === 0) {\n throw new Error('Cannot upload empty blob via chunked upload');\n }\n\n const chunkSize = options.chunkSize ?? 500_000;\n const contentType = options.contentType ?? 'application/octet-stream';\n const pricePerByte = options.pricePerByte ?? 10n;\n const uploadId = randomUUID();\n\n // Split blob into chunks\n const totalChunks = Math.ceil(blob.length / chunkSize);\n let lastResult: PublishEventResult | undefined;\n\n for (let i = 0; i < totalChunks; i++) {\n const start = i * chunkSize;\n const end = Math.min(start + chunkSize, blob.length);\n const chunkData = blob.subarray(start, end);\n const amount = BigInt(chunkData.length) * pricePerByte;\n\n const event = buildBlobStorageRequest(\n {\n blobData: Buffer.from(chunkData),\n contentType,\n bid: amount.toString(),\n params: [\n { key: 'uploadId', value: uploadId },\n { key: 'chunkIndex', value: String(i) },\n { key: 'totalChunks', value: String(totalChunks) },\n ],\n },\n options.secretKey\n );\n\n lastResult = await node.publishEvent(event, { destination, amount });\n\n if (!lastResult.success) {\n throw new Error(\n `Chunk ${i}/${totalChunks} upload failed: ${lastResult.message ?? 'unknown error'}`\n );\n }\n }\n\n // The final chunk's FULFILL data contains the Arweave tx ID (base64-encoded)\n // lastResult is guaranteed to be set since totalChunks >= 1\n if (!lastResult) {\n throw new Error('No chunks were uploaded');\n }\n if (lastResult.data) {\n return Buffer.from(lastResult.data, 'base64').toString('utf-8');\n }\n return lastResult.eventId;\n}\n"],"mappings":";;;AAWA;AAAA,EACE,oBAAoB;AAAA,EACpB;AAAA,EACA;AAAA,OACK;AACP,SAAS,gBAAgB;AACzB,SAAS,aAAa;AACtB,SAAS,oBAAoB;AAC7B,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB;AAC3B,SAAS,kBAAkB;;;AChB3B,SAAS,iBAAiB;AAMnB,IAAM,gBAAN,cAA4B,UAAU;AAAA,EAC3C,YAAY,SAAiB,OAAe;AAC1C,UAAM,SAAS,kBAAkB,KAAK;AAC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