@toon-protocol/sdk 2.1.0 → 3.0.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.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { ToonError, HandlePacketAcceptResponse, HandlePacketRejectResponse, ReputationScore, SkillDescriptor, EmbeddableConnectorLike, KnownPeer, SettlementConfig, ChainProviderConfigEntry, ConnectorChannelClient, BootstrapEventListener, BootstrapResult, DvmJobStatus, IlpSendResult, ParsedWorkflowDefinition, SwapPair } from '@toon-protocol/core';
2
- export { BootstrapEvent, BootstrapEventListener, HandlePacketAcceptResponse, HandlePacketRejectResponse, SkillDescriptor, balanceProofFieldsMina, balanceProofHashEvm, balanceProofHashSolana, bigintToBytes32BE, concatBytes, hexToBytes, minaHashToField } from '@toon-protocol/core';
2
+ export { BootstrapEvent, BootstrapEventListener, HandlePacketAcceptResponse, HandlePacketRejectResponse, SkillDescriptor, balanceProofFieldsMina, balanceProofHashEvm, balanceProofHashSolana, bigintToBytes32BE, concatBytes, coopCloseHashEvm, eip712DomainSeparatorEvm, hexToBytes, minaHashToField } from '@toon-protocol/core';
3
3
  import { NostrEvent, UnsignedEvent } from 'nostr-tools/pure';
4
4
  import { ToonRoutingMeta } from '@toon-protocol/core/toon';
5
5
  import { TransportConfig } from '@toon-protocol/connector';
6
6
  export { TransportConfig } from '@toon-protocol/connector';
7
- import { A as AccumulatedClaim } from './swap-Oub-0vqU.js';
8
- export { a as AdaptiveDeltaController, b as AdaptiveDeltaControllerConfig, D as DecryptFulfillClaimParams, E as EncryptFulfillClaimParams, c as EncryptFulfillClaimResult, I as InMemorySwapControllerStateStore, J as JsonFileSwapControllerStateStore, P as PacketObservation, d as PacketProgress, e as PacketResolution, R as RateMonitorCallback, S as StreamSwapAdaptiveController, f as StreamSwapController, g as StreamSwapParams, h as StreamSwapResult, i as SwapControllerError, j as SwapControllerState, k as SwapControllerStateStore, U as UnwrapSwapPacketFromToonParams, l as UnwrapSwapPacketParams, m as UnwrapSwapPacketResult, W as WrapSwapPacketParams, n as WrapSwapPacketResult, o as WrapSwapPacketToToonParams, p as WrapSwapPacketToToonResult, _ as __streamSwapTesting, q as decryptFulfillClaim, r as encryptFulfillClaim, s as isSwapControllerState, t as streamSwap, u as streamSwapControlled, v as swapControllerStateKey, w as unwrapSwapPacket, x as unwrapSwapPacketFromToon, y as wrapSwapPacket, z as wrapSwapPacketToToon } from './swap-Oub-0vqU.js';
7
+ import { R as ReceiptSessionStoreLike, A as AccumulatedClaim } from './swap-PFQTJZA7.js';
8
+ export { a as AdaptiveDeltaController, b as AdaptiveDeltaControllerConfig, B as BoundedReceiptSessions, D as DEFAULT_RECEIPT_SESSIONS_CAP, c as DecryptFulfillClaimParams, E as EncryptFulfillClaimParams, d as EncryptFulfillClaimResult, I as InMemorySwapControllerStateStore, J as JsonFileSwapControllerStateStore, P as PacketObservation, e as PacketProgress, f as PacketResolution, g as RateMonitorCallback, h as ReceiptAddResult, i as ReceiptChainTracker, j as ReceiptSessionState, S as STREAM_RECEIPT_SIGNING_TAG, k as STREAM_RECEIPT_VERSION, l as StreamReceipt, m as StreamReceiptChain, n as StreamReceiptFields, o as StreamSwapAdaptiveController, p as StreamSwapController, q as StreamSwapParams, r as StreamSwapResult, s as SwapControllerError, t as SwapControllerState, u as SwapControllerStateStore, U as UnwrapSwapPacketFromToonParams, v as UnwrapSwapPacketParams, w as UnwrapSwapPacketResult, W as WrapSwapPacketParams, x as WrapSwapPacketResult, y as WrapSwapPacketToToonParams, z as WrapSwapPacketToToonResult, _ as __streamSwapTesting, C as decryptFulfillClaim, F as encodeReceiptSigningPayload, G as encryptFulfillClaim, H as isSwapControllerState, K as isValidStreamNonce, L as issueSessionReceipt, M as parseStreamReceipt, N as serializeReceiptChain, O as signStreamReceipt, Q as streamSwap, T as streamSwapControlled, V as swapControllerStateKey, X as unwrapSwapPacket, Y as unwrapSwapPacketFromToon, Z as verifyStreamReceipt, $ as wrapSwapPacket, a0 as wrapSwapPacketToToon } from './swap-PFQTJZA7.js';
9
9
 
10
10
  /**
11
11
  * Unified identity module for @toon-protocol/sdk.
@@ -1527,6 +1527,27 @@ interface CreateSwapHandlerConfig {
1527
1527
  * satisfies the contract.
1528
1528
  */
1529
1529
  seenPacketIds?: SeenPacketIdsLike;
1530
+ /**
1531
+ * Optional dedicated receipt signing key (issue #84, rfc-0039 stream
1532
+ * receipts; rolling-swap spec §7.2). 32-byte secp256k1 secret key used to
1533
+ * BIP-340-sign the per-fulfill {@link StreamReceipt}. When OMITTED,
1534
+ * receipts are signed with `recipientSecretKey` — the maker's Nostr
1535
+ * identity key — so senders verify against the `swapPubkey` they already
1536
+ * discovered via kind:10032. Provide a separate key when the receipt
1537
+ * signer should be provisioned independently of the identity key (e.g.
1538
+ * the swap#47 coupled engine binding receipts to the chain-B claim
1539
+ * signer); senders then verify via `StreamSwapParams.receiptPubkey`.
1540
+ */
1541
+ receiptSecretKey?: Uint8Array;
1542
+ /**
1543
+ * Optional receipt session store (issue #84). Tracks per-`streamNonce`
1544
+ * `{seq, cumulativeDelivered}` so receipt totals are monotone within a
1545
+ * session. Defaults to a {@link BoundedReceiptSessions} in-memory LRU.
1546
+ * Operators that persist claims should back this with the same storage so
1547
+ * receipt state survives restarts alongside the claim stream (a lost
1548
+ * session restarts at seq 1, which senders reject as a forked chain).
1549
+ */
1550
+ receiptSessions?: ReceiptSessionStoreLike;
1530
1551
  /** Optional pino-compatible logger. Defaults to a no-op logger. */
1531
1552
  logger?: SwapHandlerLogger;
1532
1553
  }
@@ -1767,7 +1788,10 @@ interface SwapSignerConfig {
1767
1788
  */
1768
1789
  address: string;
1769
1790
  /**
1770
- * On-chain payment-channel contract address (EVM-only).
1791
+ * On-chain payment-channel contract address (EVM-only). Required for EVM
1792
+ * claims: it is the RLP `to` field AND the EIP-712 `verifyingContract` of the
1793
+ * v2 balance-proof digest (connector#324 finding #1), so it now binds the
1794
+ * signature to this exact deployment.
1771
1795
  */
1772
1796
  contractAddress?: string;
1773
1797
  /**
@@ -1775,7 +1799,9 @@ interface SwapSignerConfig {
1775
1799
  */
1776
1800
  programId?: string;
1777
1801
  /**
1778
- * EVM chain-id (decimal). Required for EVM claims — baked into RLP per EIP-155.
1802
+ * EVM chain-id (decimal). Required for EVM claims — baked into RLP per EIP-155
1803
+ * AND into the EIP-712 domain `chainId` of the v2 balance-proof digest
1804
+ * (connector#324 finding #1), so the signature is valid on this chain only.
1779
1805
  */
1780
1806
  chainId?: number;
1781
1807
  }
@@ -1980,4 +2006,4 @@ interface StoreHealthResponse {
1980
2006
  jobsRecent: StoreJobsRecent;
1981
2007
  }
1982
2008
 
1983
- export { AccumulatedClaim, type AddChunkResult, type ApplyRateParams, type ArweaveDvmConfig, type ArweaveUploadAdapter, type BuildSettlementTxParams, type BuildSettlementTxResult, type BuildSkillDescriptorConfig, ChunkManager, type ChunkManagerConfig, type ClaimIssuer, type CreateHandlerContextOptions, type CreateSwapHandlerConfig, type FromMnemonicOptions, GiftWrapError, type Handler, type HandlerContext, HandlerError, HandlerRegistry, type HandlerResponse, IdentityError, type IssueClaimParams, type IssueClaimResult, type MinaIdentity, type MinaSignerClientLike, type NodeConfig, NodeError, type NodeIdentity, type PaymentHandlerBridgeConfig, type PaymentRequest, type PaymentResponse, type PrefixClaimHandlerOptions, PricingError, type PricingValidationResult, type PricingValidatorConfig, type PublishEventResult, type PublishableNode, type RateQuote, SWAP_HANDLER_REJECT_CODES, SWAP_HANDLER_REJECT_MESSAGES, type ServiceNode, type SettlementBundle, SettlementTxError, type SolanaIdentity, type StartResult, type StoreHealthResponse, type StoreJobsByKindEntry, type StoreJobsByStatus, type StoreJobsRecent, StreamSwapError, SwapHandlerError, type SwapHandlerLogger, type SwapSignerConfig, SwarmCoordinator, type SwarmCoordinatorOptions, type SwarmState, type ToonIdentity, TurboUploadAdapter, type UploadBlobChunkedOptions, type UploadBlobOptions, VerificationError, type VerificationPipelineConfig, type VerificationResult, type WorkflowEventStore, WorkflowOrchestrator, type WorkflowOrchestratorOptions, type WorkflowState, applyRate, base58Decode, base58Encode, buildSettlementTx, buildSkillDescriptor, createArweaveDvmHandler, createEventStorageHandler, createHandlerContext, createNode, createPaymentHandlerBridge, createPrefixClaimHandler, createPricingValidator, createSwapHandler, createVerificationPipeline, fillEvmSettlementTxGas, findSwapPair, fromMnemonic, fromMnemonicFull, fromSecretKey, generateMnemonic, generateSolanaKeypair, loadMinaSignerClient, uploadBlob, uploadBlobChunked, verifyAccumulatedClaim, verifyEd25519Signature, verifyMinaSignature };
2009
+ export { AccumulatedClaim, type AddChunkResult, type ApplyRateParams, type ArweaveDvmConfig, type ArweaveUploadAdapter, type BuildSettlementTxParams, type BuildSettlementTxResult, type BuildSkillDescriptorConfig, ChunkManager, type ChunkManagerConfig, type ClaimIssuer, type CreateHandlerContextOptions, type CreateSwapHandlerConfig, type FromMnemonicOptions, GiftWrapError, type Handler, type HandlerContext, HandlerError, HandlerRegistry, type HandlerResponse, IdentityError, type IssueClaimParams, type IssueClaimResult, type MinaIdentity, type MinaSignerClientLike, type NodeConfig, NodeError, type NodeIdentity, type PaymentHandlerBridgeConfig, type PaymentRequest, type PaymentResponse, type PrefixClaimHandlerOptions, PricingError, type PricingValidationResult, type PricingValidatorConfig, type PublishEventResult, type PublishableNode, type RateQuote, ReceiptSessionStoreLike, SWAP_HANDLER_REJECT_CODES, SWAP_HANDLER_REJECT_MESSAGES, type ServiceNode, type SettlementBundle, SettlementTxError, type SolanaIdentity, type StartResult, type StoreHealthResponse, type StoreJobsByKindEntry, type StoreJobsByStatus, type StoreJobsRecent, StreamSwapError, SwapHandlerError, type SwapHandlerLogger, type SwapSignerConfig, SwarmCoordinator, type SwarmCoordinatorOptions, type SwarmState, type ToonIdentity, TurboUploadAdapter, type UploadBlobChunkedOptions, type UploadBlobOptions, VerificationError, type VerificationPipelineConfig, type VerificationResult, type WorkflowEventStore, WorkflowOrchestrator, type WorkflowOrchestratorOptions, type WorkflowState, applyRate, base58Decode, base58Encode, buildSettlementTx, buildSkillDescriptor, createArweaveDvmHandler, createEventStorageHandler, createHandlerContext, createNode, createPaymentHandlerBridge, createPrefixClaimHandler, createPricingValidator, createSwapHandler, createVerificationPipeline, fillEvmSettlementTxGas, findSwapPair, fromMnemonic, fromMnemonicFull, fromSecretKey, generateMnemonic, generateSolanaKeypair, loadMinaSignerClient, uploadBlob, uploadBlobChunked, verifyAccumulatedClaim, verifyEd25519Signature, verifyMinaSignature };
package/dist/index.js CHANGED
@@ -1,9 +1,14 @@
1
1
  import {
2
+ BoundedReceiptSessions,
3
+ DEFAULT_RECEIPT_SESSIONS_CAP,
2
4
  GiftWrapError,
3
5
  HandlerError,
4
6
  IdentityError,
5
7
  NodeError,
6
8
  PricingError,
9
+ ReceiptChainTracker,
10
+ STREAM_RECEIPT_SIGNING_TAG,
11
+ STREAM_RECEIPT_VERSION,
7
12
  SWAP_HANDLER_REJECT_CODES,
8
13
  SWAP_HANDLER_REJECT_MESSAGES,
9
14
  SettlementTxError,
@@ -16,6 +21,7 @@ import {
16
21
  base58Encode,
17
22
  createSwapHandler,
18
23
  decryptFulfillClaim,
24
+ encodeReceiptSigningPayload,
19
25
  encryptFulfillClaim,
20
26
  findSwapPair,
21
27
  fromMnemonic,
@@ -23,13 +29,19 @@ import {
23
29
  fromSecretKey,
24
30
  generateMnemonic,
25
31
  generateSolanaKeypair,
32
+ isValidStreamNonce,
33
+ issueSessionReceipt,
34
+ parseStreamReceipt,
35
+ serializeReceiptChain,
36
+ signStreamReceipt,
26
37
  streamSwap,
27
38
  streamSwapControlled,
28
39
  unwrapSwapPacket,
29
40
  unwrapSwapPacketFromToon,
41
+ verifyStreamReceipt,
30
42
  wrapSwapPacket,
31
43
  wrapSwapPacketToToon
32
- } from "./chunk-7LBYFU4L.js";
44
+ } from "./chunk-RAZRWSJF.js";
33
45
  import "./chunk-UP2VWCW5.js";
34
46
 
35
47
  // src/handler-context.ts
@@ -2362,6 +2374,8 @@ import {
2362
2374
  bigintToBytes32BE,
2363
2375
  concatBytes,
2364
2376
  balanceProofHashEvm,
2377
+ coopCloseHashEvm,
2378
+ eip712DomainSeparatorEvm,
2365
2379
  balanceProofHashSolana,
2366
2380
  minaHashToField,
2367
2381
  balanceProofFieldsMina
@@ -2376,7 +2390,19 @@ var EVM_SETTLEMENT_FUNCTION_SELECTOR = keccak_256(
2376
2390
  var EVM_SETTLEMENT_EVENT_TOPIC = "0x" + bytesToHex(
2377
2391
  keccak_256(new TextEncoder().encode(EVM_SETTLEMENT_EVENT_SIGNATURE))
2378
2392
  );
2379
- function recoverEvmSignerAddress(claim) {
2393
+ function recoverEvmSignerAddress(claim, chainId, verifyingContract) {
2394
+ if (typeof chainId !== "number" || !Number.isInteger(chainId) || chainId <= 0) {
2395
+ throw new SettlementTxError(
2396
+ "INVALID_INPUT",
2397
+ `EVM v2 digest requires a positive integer chainId, got ${chainId}`
2398
+ );
2399
+ }
2400
+ if (!verifyingContract) {
2401
+ throw new SettlementTxError(
2402
+ "INVALID_INPUT",
2403
+ "EVM v2 digest requires verifyingContract (RollingSwapChannel address)"
2404
+ );
2405
+ }
2380
2406
  if (claim.channelId === void 0 || claim.cumulativeAmount === void 0 || claim.nonce === void 0 || claim.recipient === void 0) {
2381
2407
  throw new SettlementTxError(
2382
2408
  "MISSING_SETTLEMENT_METADATA",
@@ -2403,6 +2429,7 @@ function recoverEvmSignerAddress(claim) {
2403
2429
  try {
2404
2430
  const channelIdBytes = hexToBytes2(claim.channelId);
2405
2431
  const recipientBytes = hexToBytes2(claim.recipient);
2432
+ const verifyingContractBytes = hexToBytes2(verifyingContract);
2406
2433
  if (channelIdBytes.length !== 32) {
2407
2434
  throw new Error(
2408
2435
  `channelId must be 32 bytes (got ${channelIdBytes.length})`
@@ -2413,11 +2440,18 @@ function recoverEvmSignerAddress(claim) {
2413
2440
  `recipient must be 20 bytes (got ${recipientBytes.length})`
2414
2441
  );
2415
2442
  }
2443
+ if (verifyingContractBytes.length !== 20) {
2444
+ throw new Error(
2445
+ `verifyingContract must be 20 bytes (got ${verifyingContractBytes.length})`
2446
+ );
2447
+ }
2416
2448
  msgHash = balanceProofHashEvm(
2417
2449
  channelIdBytes,
2418
2450
  BigInt(claim.cumulativeAmount),
2419
2451
  BigInt(claim.nonce),
2420
- recipientBytes
2452
+ recipientBytes,
2453
+ BigInt(chainId),
2454
+ verifyingContractBytes
2421
2455
  );
2422
2456
  const sig = secp256k1.Signature.fromBytes(
2423
2457
  compactRS,
@@ -2719,8 +2753,8 @@ function rlpDecodeList(buf) {
2719
2753
  }
2720
2754
  return items;
2721
2755
  }
2722
- function verifyEvmClaimSignature(claim, expectedAddress) {
2723
- const recovered = recoverEvmSignerAddress(claim);
2756
+ function verifyEvmClaimSignature(claim, expectedAddress, chainId, verifyingContract) {
2757
+ const recovered = recoverEvmSignerAddress(claim, chainId, verifyingContract);
2724
2758
  const expected = expectedAddress.toLowerCase();
2725
2759
  return {
2726
2760
  valid: recovered.toLowerCase() === expected,
@@ -3086,7 +3120,9 @@ function buildSettlementTx(params) {
3086
3120
  try {
3087
3121
  const { valid, recovered } = verifyEvmClaimSignature(
3088
3122
  claim,
3089
- signer.address
3123
+ signer.address,
3124
+ signer.chainId,
3125
+ signer.contractAddress
3090
3126
  );
3091
3127
  if (!valid) {
3092
3128
  rejected.push({
@@ -3336,10 +3372,18 @@ function buildSettlementTx(params) {
3336
3372
  function verifyAccumulatedClaim(claim, signer, minaSignerClient) {
3337
3373
  const kind = chainKindOf(claim.pair.to.chain);
3338
3374
  if (kind === "evm") {
3375
+ if (typeof signer.chainId !== "number" || !Number.isInteger(signer.chainId) || signer.chainId <= 0 || !signer.contractAddress) {
3376
+ return {
3377
+ valid: false,
3378
+ reason: "INVALID_INPUT: EVM signer requires chainId (positive integer) + contractAddress for the v2 EIP-712 digest (connector#324 finding #1)"
3379
+ };
3380
+ }
3339
3381
  try {
3340
3382
  const { valid, recovered } = verifyEvmClaimSignature(
3341
3383
  claim,
3342
- signer.address
3384
+ signer.address,
3385
+ signer.chainId,
3386
+ signer.contractAddress
3343
3387
  );
3344
3388
  if (valid) return { valid: true };
3345
3389
  return {
@@ -3394,7 +3438,9 @@ function verifyAccumulatedClaim(claim, signer, minaSignerClient) {
3394
3438
  }
3395
3439
  export {
3396
3440
  AdaptiveDeltaController,
3441
+ BoundedReceiptSessions,
3397
3442
  ChunkManager,
3443
+ DEFAULT_RECEIPT_SESSIONS_CAP,
3398
3444
  GiftWrapError,
3399
3445
  HandlerError,
3400
3446
  HandlerRegistry,
@@ -3403,6 +3449,9 @@ export {
3403
3449
  JsonFileSwapControllerStateStore,
3404
3450
  NodeError,
3405
3451
  PricingError,
3452
+ ReceiptChainTracker,
3453
+ STREAM_RECEIPT_SIGNING_TAG,
3454
+ STREAM_RECEIPT_VERSION,
3406
3455
  SWAP_HANDLER_REJECT_CODES,
3407
3456
  SWAP_HANDLER_REJECT_MESSAGES,
3408
3457
  SettlementTxError,
@@ -3424,6 +3473,7 @@ export {
3424
3473
  buildSettlementTx,
3425
3474
  buildSkillDescriptor,
3426
3475
  concatBytes,
3476
+ coopCloseHashEvm,
3427
3477
  createArweaveDvmHandler,
3428
3478
  createEventStorageHandler,
3429
3479
  createHandlerContext,
@@ -3434,6 +3484,8 @@ export {
3434
3484
  createSwapHandler,
3435
3485
  createVerificationPipeline,
3436
3486
  decryptFulfillClaim,
3487
+ eip712DomainSeparatorEvm,
3488
+ encodeReceiptSigningPayload,
3437
3489
  encryptFulfillClaim,
3438
3490
  fillEvmSettlementTxGas,
3439
3491
  findSwapPair,
@@ -3444,8 +3496,13 @@ export {
3444
3496
  generateSolanaKeypair,
3445
3497
  hexToBytes2 as hexToBytes,
3446
3498
  isSwapControllerState,
3499
+ isValidStreamNonce,
3500
+ issueSessionReceipt,
3447
3501
  loadMinaSignerClient,
3448
3502
  minaHashToField,
3503
+ parseStreamReceipt,
3504
+ serializeReceiptChain,
3505
+ signStreamReceipt,
3449
3506
  streamSwap,
3450
3507
  streamSwapControlled,
3451
3508
  swapControllerStateKey,
@@ -3456,6 +3513,7 @@ export {
3456
3513
  verifyAccumulatedClaim,
3457
3514
  verifyEd25519Signature,
3458
3515
  verifyMinaSignature,
3516
+ verifyStreamReceipt,
3459
3517
  wrapSwapPacket,
3460
3518
  wrapSwapPacketToToon
3461
3519
  };