@t402/tezos 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +161 -0
  2. package/dist/exact-direct/client/index.d.cts +92 -0
  3. package/dist/exact-direct/client/index.d.ts +92 -0
  4. package/dist/exact-direct/client/index.js +204 -0
  5. package/dist/exact-direct/client/index.js.map +1 -0
  6. package/dist/exact-direct/client/index.mjs +176 -0
  7. package/dist/exact-direct/client/index.mjs.map +1 -0
  8. package/dist/exact-direct/facilitator/index.d.cts +110 -0
  9. package/dist/exact-direct/facilitator/index.d.ts +110 -0
  10. package/dist/exact-direct/facilitator/index.js +331 -0
  11. package/dist/exact-direct/facilitator/index.js.map +1 -0
  12. package/dist/exact-direct/facilitator/index.mjs +303 -0
  13. package/dist/exact-direct/facilitator/index.mjs.map +1 -0
  14. package/dist/exact-direct/server/index.d.cts +109 -0
  15. package/dist/exact-direct/server/index.d.ts +109 -0
  16. package/dist/exact-direct/server/index.js +226 -0
  17. package/dist/exact-direct/server/index.js.map +1 -0
  18. package/dist/exact-direct/server/index.mjs +198 -0
  19. package/dist/exact-direct/server/index.mjs.map +1 -0
  20. package/dist/index.d.cts +124 -0
  21. package/dist/index.d.ts +124 -0
  22. package/dist/index.js +228 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/index.mjs +170 -0
  25. package/dist/index.mjs.map +1 -0
  26. package/dist/types-DQMtUOa_.d.cts +125 -0
  27. package/dist/types-DQMtUOa_.d.ts +125 -0
  28. package/package.json +100 -0
  29. package/src/constants.ts +53 -0
  30. package/src/exact-direct/client/index.ts +13 -0
  31. package/src/exact-direct/client/register.ts +71 -0
  32. package/src/exact-direct/client/scheme.ts +177 -0
  33. package/src/exact-direct/facilitator/index.ts +13 -0
  34. package/src/exact-direct/facilitator/register.ts +74 -0
  35. package/src/exact-direct/facilitator/scheme.ts +311 -0
  36. package/src/exact-direct/server/index.ts +13 -0
  37. package/src/exact-direct/server/register.ts +64 -0
  38. package/src/exact-direct/server/scheme.ts +205 -0
  39. package/src/index.ts +32 -0
  40. package/src/tokens.ts +86 -0
  41. package/src/types.ts +160 -0
  42. package/src/utils.ts +128 -0
@@ -0,0 +1,176 @@
1
+ // src/constants.ts
2
+ var SCHEME_EXACT_DIRECT = "exact-direct";
3
+ var TEZOS_CAIP2_NAMESPACE = "tezos";
4
+ var TEZOS_MAINNET_CAIP2 = "tezos:NetXdQprcVkpaWU";
5
+ var TEZOS_GHOSTNET_CAIP2 = "tezos:NetXnHfVqm9iesp";
6
+ var DEFAULT_MAINNET_RPC = "https://mainnet.api.tez.ie";
7
+ var DEFAULT_GHOSTNET_RPC = "https://ghostnet.tezos.marigold.dev";
8
+ var DEFAULT_MAINNET_INDEXER = "https://api.tzkt.io";
9
+ var DEFAULT_GHOSTNET_INDEXER = "https://api.ghostnet.tzkt.io";
10
+ var NETWORK_CONFIGS = {
11
+ [TEZOS_MAINNET_CAIP2]: {
12
+ name: "Tezos Mainnet",
13
+ rpcUrl: DEFAULT_MAINNET_RPC,
14
+ indexerUrl: DEFAULT_MAINNET_INDEXER
15
+ },
16
+ [TEZOS_GHOSTNET_CAIP2]: {
17
+ name: "Tezos Ghostnet",
18
+ rpcUrl: DEFAULT_GHOSTNET_RPC,
19
+ indexerUrl: DEFAULT_GHOSTNET_INDEXER
20
+ }
21
+ };
22
+
23
+ // src/types.ts
24
+ function isValidTezosAddress(address) {
25
+ if (!address) return false;
26
+ const prefixPattern = /^(tz1|tz2|tz3|KT1)/;
27
+ if (!prefixPattern.test(address)) return false;
28
+ return address.length === 36;
29
+ }
30
+
31
+ // src/tokens.ts
32
+ var USDT_MAINNET = {
33
+ contractAddress: "KT1XnTn74bUtxHfDtBmm2bGZAQfhPbvKWR8o",
34
+ tokenId: 0,
35
+ symbol: "USDt",
36
+ name: "Tether USD",
37
+ decimals: 6
38
+ };
39
+ var TOKEN_REGISTRY = {
40
+ [TEZOS_MAINNET_CAIP2]: [USDT_MAINNET],
41
+ [TEZOS_GHOSTNET_CAIP2]: []
42
+ };
43
+ var DEFAULT_TOKENS = {
44
+ [TEZOS_MAINNET_CAIP2]: USDT_MAINNET,
45
+ [TEZOS_GHOSTNET_CAIP2]: void 0
46
+ };
47
+ function getTokenBySymbol(network, symbol) {
48
+ const tokens = TOKEN_REGISTRY[network];
49
+ if (!tokens) return void 0;
50
+ return tokens.find(
51
+ (t) => t.symbol.toLowerCase() === symbol.toLowerCase()
52
+ );
53
+ }
54
+
55
+ // src/utils.ts
56
+ function compareAddresses(addr1, addr2) {
57
+ if (!addr1 || !addr2) return false;
58
+ return addr1 === addr2;
59
+ }
60
+
61
+ // src/exact-direct/client/scheme.ts
62
+ var ExactDirectTezosClient = class {
63
+ constructor(signer, config = {}) {
64
+ this.signer = signer;
65
+ void config;
66
+ }
67
+ scheme = SCHEME_EXACT_DIRECT;
68
+ /**
69
+ * Create a payment payload by executing the transfer
70
+ */
71
+ async createPaymentPayload(t402Version, paymentRequirements) {
72
+ this.validateRequirements(paymentRequirements);
73
+ const from = await this.signer.getAddress();
74
+ const assetInfo = this.parseAssetIdentifier(paymentRequirements.asset);
75
+ if (!assetInfo) {
76
+ throw new Error(`Invalid asset identifier: ${paymentRequirements.asset}`);
77
+ }
78
+ const amount = BigInt(paymentRequirements.amount);
79
+ const balance = await this.signer.getBalance(
80
+ assetInfo.contractAddress,
81
+ assetInfo.tokenId
82
+ );
83
+ if (balance < amount) {
84
+ throw new Error(
85
+ `Insufficient balance: have ${balance}, need ${amount}`
86
+ );
87
+ }
88
+ const opHash = await this.signer.transfer(
89
+ assetInfo.contractAddress,
90
+ assetInfo.tokenId,
91
+ paymentRequirements.payTo,
92
+ amount
93
+ );
94
+ const payload = {
95
+ opHash,
96
+ from,
97
+ to: paymentRequirements.payTo,
98
+ amount: paymentRequirements.amount,
99
+ contractAddress: assetInfo.contractAddress,
100
+ tokenId: assetInfo.tokenId
101
+ };
102
+ return {
103
+ t402Version,
104
+ payload
105
+ };
106
+ }
107
+ /**
108
+ * Parse CAIP-19 asset identifier for Tezos FA2
109
+ * Format: tezos:{chainRef}/fa2:{contractAddress}/{tokenId}
110
+ */
111
+ parseAssetIdentifier(asset) {
112
+ const caipMatch = asset.match(/^tezos:[^/]+\/fa2:([^/]+)\/(\d+)$/);
113
+ if (caipMatch) {
114
+ return {
115
+ contractAddress: caipMatch[1],
116
+ tokenId: parseInt(caipMatch[2], 10)
117
+ };
118
+ }
119
+ const simpleMatch = asset.match(/^(KT1[a-zA-Z0-9]+)(?:\/(\d+))?$/);
120
+ if (simpleMatch) {
121
+ return {
122
+ contractAddress: simpleMatch[1],
123
+ tokenId: simpleMatch[2] ? parseInt(simpleMatch[2], 10) : 0
124
+ };
125
+ }
126
+ return null;
127
+ }
128
+ /**
129
+ * Validate payment requirements
130
+ */
131
+ validateRequirements(requirements) {
132
+ if (requirements.scheme !== SCHEME_EXACT_DIRECT) {
133
+ throw new Error(
134
+ `Invalid scheme: expected ${SCHEME_EXACT_DIRECT}, got ${requirements.scheme}`
135
+ );
136
+ }
137
+ if (!requirements.network.startsWith(`${TEZOS_CAIP2_NAMESPACE}:`)) {
138
+ throw new Error(`Invalid network: ${requirements.network}`);
139
+ }
140
+ if (!isValidTezosAddress(requirements.payTo)) {
141
+ throw new Error(`Invalid payTo address: ${requirements.payTo}`);
142
+ }
143
+ const amount = BigInt(requirements.amount);
144
+ if (amount <= 0n) {
145
+ throw new Error(`Invalid amount: ${requirements.amount}`);
146
+ }
147
+ const assetInfo = this.parseAssetIdentifier(requirements.asset);
148
+ if (!assetInfo) {
149
+ throw new Error(`Invalid asset: ${requirements.asset}`);
150
+ }
151
+ const tokenConfig = getTokenBySymbol(requirements.network, "USDt");
152
+ if (tokenConfig && !compareAddresses(tokenConfig.contractAddress, assetInfo.contractAddress)) {
153
+ console.warn(
154
+ `Using non-standard token: ${assetInfo.contractAddress}`
155
+ );
156
+ }
157
+ }
158
+ };
159
+
160
+ // src/exact-direct/client/register.ts
161
+ function registerExactDirectTezosClient(client, config) {
162
+ const scheme = new ExactDirectTezosClient(config.signer, config.schemeConfig);
163
+ if (config.networks && config.networks.length > 0) {
164
+ config.networks.forEach((network) => {
165
+ client.register(network, scheme);
166
+ });
167
+ } else {
168
+ client.register("tezos:*", scheme);
169
+ }
170
+ return client;
171
+ }
172
+ export {
173
+ ExactDirectTezosClient,
174
+ registerExactDirectTezosClient
175
+ };
176
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/constants.ts","../../../src/types.ts","../../../src/tokens.ts","../../../src/utils.ts","../../../src/exact-direct/client/scheme.ts","../../../src/exact-direct/client/register.ts"],"sourcesContent":["/**\n * Tezos mechanism constants\n */\n\n// Scheme identifiers\nexport const SCHEME_EXACT_DIRECT = \"exact-direct\";\n\n// CAIP-2 namespace\nexport const TEZOS_CAIP2_NAMESPACE = \"tezos\";\n\n// CAIP-2 network identifiers (derived from genesis block hash)\nexport const TEZOS_MAINNET_CAIP2 = \"tezos:NetXdQprcVkpaWU\";\nexport const TEZOS_GHOSTNET_CAIP2 = \"tezos:NetXnHfVqm9iesp\";\n\n// Default RPC endpoints\nexport const DEFAULT_MAINNET_RPC = \"https://mainnet.api.tez.ie\";\nexport const DEFAULT_GHOSTNET_RPC = \"https://ghostnet.tezos.marigold.dev\";\n\n// Default indexer API endpoints (TzKT)\nexport const DEFAULT_MAINNET_INDEXER = \"https://api.tzkt.io\";\nexport const DEFAULT_GHOSTNET_INDEXER = \"https://api.ghostnet.tzkt.io\";\n\n// FA2 token standard (TZIP-12)\nexport const FA2_TRANSFER_ENTRYPOINT = \"transfer\";\nexport const FA2_BALANCE_OF_ENTRYPOINT = \"balance_of\";\nexport const FA2_UPDATE_OPERATORS_ENTRYPOINT = \"update_operators\";\n\n// Supported networks\nexport const SUPPORTED_NETWORKS = [\n TEZOS_MAINNET_CAIP2,\n TEZOS_GHOSTNET_CAIP2,\n] as const;\n\n// Network configurations\nexport const NETWORK_CONFIGS: Record<\n string,\n {\n name: string;\n rpcUrl: string;\n indexerUrl: string;\n }\n> = {\n [TEZOS_MAINNET_CAIP2]: {\n name: \"Tezos Mainnet\",\n rpcUrl: DEFAULT_MAINNET_RPC,\n indexerUrl: DEFAULT_MAINNET_INDEXER,\n },\n [TEZOS_GHOSTNET_CAIP2]: {\n name: \"Tezos Ghostnet\",\n rpcUrl: DEFAULT_GHOSTNET_RPC,\n indexerUrl: DEFAULT_GHOSTNET_INDEXER,\n },\n};\n","/**\n * Tezos mechanism types\n */\n\nimport type { Network } from \"@t402/core/types\";\n\n/**\n * Exact-direct payment payload for Tezos\n */\nexport type ExactDirectTezosPayload = {\n /** Operation hash (o...) */\n opHash: string;\n /** Sender address (tz1/tz2/tz3...) */\n from: string;\n /** Recipient address */\n to: string;\n /** Amount in smallest units */\n amount: string;\n /** FA2 contract address (KT1...) */\n contractAddress: string;\n /** Token ID within the FA2 contract */\n tokenId: number;\n};\n\n/**\n * Tezos signer interface for client-side operations\n */\nexport interface TezosSigner {\n /** Get the signer's address */\n getAddress(): Promise<string>;\n\n /**\n * Execute an FA2 transfer\n * @param contractAddress FA2 contract address\n * @param tokenId Token ID\n * @param to Recipient address\n * @param amount Amount to transfer\n * @returns Operation hash\n */\n transfer(\n contractAddress: string,\n tokenId: number,\n to: string,\n amount: bigint,\n ): Promise<string>;\n\n /**\n * Get token balance\n * @param contractAddress FA2 contract address\n * @param tokenId Token ID\n * @param address Address to check (optional, defaults to signer address)\n * @returns Balance in smallest units\n */\n getBalance(\n contractAddress: string,\n tokenId: number,\n address?: string,\n ): Promise<bigint>;\n}\n\n/**\n * Tezos signer interface for facilitator operations\n */\nexport interface FacilitatorTezosSigner {\n /** Get facilitator addresses for a network */\n getAddresses(network: Network): string[];\n\n /**\n * Query an operation by hash\n * @param opHash Operation hash\n * @returns Operation result or null if not found\n */\n queryOperation(opHash: string): Promise<TezosOperationResult | null>;\n\n /**\n * Get token balance for an address\n * @param contractAddress FA2 contract address\n * @param tokenId Token ID\n * @param address Address to check\n * @returns Balance as string\n */\n getBalance(\n contractAddress: string,\n tokenId: number,\n address: string,\n ): Promise<string>;\n}\n\n/**\n * Tezos operation result from indexer\n */\nexport interface TezosOperationResult {\n /** Operation hash */\n hash: string;\n /** Block level */\n level: number;\n /** Timestamp */\n timestamp: string;\n /** Status: applied, failed, backtracked, skipped */\n status: \"applied\" | \"failed\" | \"backtracked\" | \"skipped\";\n /** Sender address */\n sender: {\n address: string;\n };\n /** Target contract (for contract calls) */\n target?: {\n address: string;\n };\n /** Entrypoint called */\n entrypoint?: string;\n /** Parameter value */\n parameter?: unknown;\n /** Amount transferred (in mutez for XTZ) */\n amount?: number;\n /** Errors if failed */\n errors?: Array<{\n type: string;\n message?: string;\n }>;\n}\n\n/**\n * FA2 transfer parameter structure\n */\nexport interface FA2TransferParam {\n from_: string;\n txs: Array<{\n to_: string;\n token_id: number;\n amount: string;\n }>;\n}\n\n/**\n * Check if a string is a valid Tezos address\n */\nexport function isValidTezosAddress(address: string): boolean {\n if (!address) return false;\n // tz1, tz2, tz3 for implicit accounts, KT1 for contracts\n const prefixPattern = /^(tz1|tz2|tz3|KT1)/;\n if (!prefixPattern.test(address)) return false;\n // Base58 check - length should be 36 characters\n return address.length === 36;\n}\n\n/**\n * Check if a string is a valid Tezos operation hash\n */\nexport function isValidOperationHash(opHash: string): boolean {\n if (!opHash) return false;\n // Operation hashes start with 'o' and are 51 characters\n return opHash.startsWith(\"o\") && opHash.length === 51;\n}\n\n/**\n * Check if a network is a Tezos network\n */\nexport function isTezosNetwork(network: string): boolean {\n return network.startsWith(\"tezos:\");\n}\n","/**\n * Tezos token registry\n */\n\nimport { TEZOS_MAINNET_CAIP2, TEZOS_GHOSTNET_CAIP2 } from \"./constants.js\";\n\n/**\n * Token configuration for Tezos FA2 tokens\n */\nexport interface TokenConfig {\n /** FA2 contract address (KT1...) */\n contractAddress: string;\n /** Token ID within the FA2 contract */\n tokenId: number;\n /** Token symbol */\n symbol: string;\n /** Token name */\n name: string;\n /** Token decimals */\n decimals: number;\n}\n\n/**\n * USDT on Tezos Mainnet\n */\nexport const USDT_MAINNET: TokenConfig = {\n contractAddress: \"KT1XnTn74bUtxHfDtBmm2bGZAQfhPbvKWR8o\",\n tokenId: 0,\n symbol: \"USDt\",\n name: \"Tether USD\",\n decimals: 6,\n};\n\n/**\n * Token registry by network\n */\nexport const TOKEN_REGISTRY: Record<string, TokenConfig[]> = {\n [TEZOS_MAINNET_CAIP2]: [USDT_MAINNET],\n [TEZOS_GHOSTNET_CAIP2]: [],\n};\n\n/**\n * Default token for each network\n */\nexport const DEFAULT_TOKENS: Record<string, TokenConfig | undefined> = {\n [TEZOS_MAINNET_CAIP2]: USDT_MAINNET,\n [TEZOS_GHOSTNET_CAIP2]: undefined,\n};\n\n/**\n * Get token by symbol for a network\n */\nexport function getTokenBySymbol(\n network: string,\n symbol: string,\n): TokenConfig | undefined {\n const tokens = TOKEN_REGISTRY[network];\n if (!tokens) return undefined;\n return tokens.find(\n (t) => t.symbol.toLowerCase() === symbol.toLowerCase(),\n );\n}\n\n/**\n * Get token by contract address and token ID\n */\nexport function getTokenByContract(\n network: string,\n contractAddress: string,\n tokenId: number,\n): TokenConfig | undefined {\n const tokens = TOKEN_REGISTRY[network];\n if (!tokens) return undefined;\n return tokens.find(\n (t) =>\n t.contractAddress.toLowerCase() === contractAddress.toLowerCase() &&\n t.tokenId === tokenId,\n );\n}\n\n/**\n * Get default token for a network\n */\nexport function getDefaultToken(network: string): TokenConfig | undefined {\n return DEFAULT_TOKENS[network];\n}\n","/**\n * Tezos utility functions\n */\n\nimport {\n NETWORK_CONFIGS,\n TEZOS_CAIP2_NAMESPACE,\n SUPPORTED_NETWORKS,\n} from \"./constants.js\";\n\n/**\n * Get network configuration\n */\nexport function getNetworkConfig(network: string) {\n return NETWORK_CONFIGS[network];\n}\n\n/**\n * Check if a network is supported\n */\nexport function isSupportedNetwork(network: string): boolean {\n return (SUPPORTED_NETWORKS as readonly string[]).includes(network);\n}\n\n/**\n * Parse a CAIP-2 network identifier to extract the chain reference\n */\nexport function parseNetworkId(network: string): {\n namespace: string;\n reference: string;\n} | null {\n const parts = network.split(\":\");\n if (parts.length !== 2) return null;\n return {\n namespace: parts[0],\n reference: parts[1],\n };\n}\n\n/**\n * Build a CAIP-2 network identifier\n */\nexport function buildNetworkId(reference: string): string {\n return `${TEZOS_CAIP2_NAMESPACE}:${reference}`;\n}\n\n/**\n * Get indexer URL for a network\n */\nexport function getIndexerUrl(network: string): string | undefined {\n return NETWORK_CONFIGS[network]?.indexerUrl;\n}\n\n/**\n * Get RPC URL for a network\n */\nexport function getRpcUrl(network: string): string | undefined {\n return NETWORK_CONFIGS[network]?.rpcUrl;\n}\n\n/**\n * Compare two Tezos addresses (case-insensitive for base58)\n */\nexport function compareAddresses(addr1: string, addr2: string): boolean {\n if (!addr1 || !addr2) return false;\n return addr1 === addr2;\n}\n\n/**\n * Format amount with decimals\n */\nexport function formatAmount(amount: bigint, decimals: number): string {\n const divisor = BigInt(10 ** decimals);\n const wholePart = amount / divisor;\n const fractionalPart = amount % divisor;\n const fractionalStr = fractionalPart.toString().padStart(decimals, \"0\");\n return `${wholePart}.${fractionalStr}`;\n}\n\n/**\n * Parse amount string to bigint\n */\nexport function parseAmount(amount: string, decimals: number): bigint {\n const [whole, fractional = \"\"] = amount.split(\".\");\n const paddedFractional = fractional.padEnd(decimals, \"0\").slice(0, decimals);\n return BigInt(whole + paddedFractional);\n}\n\n/**\n * Extract FA2 transfer details from operation parameter\n */\nexport function extractFA2TransferDetails(\n parameter: unknown,\n): {\n from: string;\n to: string;\n tokenId: number;\n amount: string;\n} | null {\n if (!parameter || !Array.isArray(parameter)) return null;\n\n // FA2 transfer parameter is an array of { from_: string, txs: [...] }\n const firstTransfer = parameter[0];\n if (!firstTransfer || typeof firstTransfer !== \"object\") return null;\n\n const transfer = firstTransfer as Record<string, unknown>;\n const from = transfer.from_ as string;\n const txs = transfer.txs as Array<{\n to_: string;\n token_id: number | string;\n amount: number | string;\n }>;\n\n if (!from || !Array.isArray(txs) || txs.length === 0) return null;\n\n const firstTx = txs[0];\n if (!firstTx) return null;\n\n return {\n from,\n to: firstTx.to_,\n tokenId:\n typeof firstTx.token_id === \"string\"\n ? parseInt(firstTx.token_id, 10)\n : firstTx.token_id,\n amount: String(firstTx.amount),\n };\n}\n","/**\n * Tezos Exact-Direct Client Scheme\n *\n * The client executes the FA2 transfer directly and provides\n * the operation hash as proof of payment.\n */\n\nimport type {\n SchemeNetworkClient,\n PaymentPayload,\n PaymentRequirements,\n} from \"@t402/core/types\";\nimport { SCHEME_EXACT_DIRECT, TEZOS_CAIP2_NAMESPACE } from \"../../constants.js\";\nimport type { TezosSigner, ExactDirectTezosPayload } from \"../../types.js\";\nimport { isValidTezosAddress } from \"../../types.js\";\nimport { getTokenBySymbol } from \"../../tokens.js\";\nimport { compareAddresses } from \"../../utils.js\";\n\n/**\n * Configuration for ExactDirectTezosClient\n */\nexport interface ExactDirectTezosClientConfig {\n /**\n * Whether to verify the operation was successful before returning\n * @default true\n */\n verifyOperation?: boolean;\n}\n\n/**\n * Tezos Exact-Direct Client\n *\n * Implements the client-side payment flow where the client:\n * 1. Receives payment requirements\n * 2. Executes the FA2 transfer operation\n * 3. Returns operation hash as proof\n */\nexport class ExactDirectTezosClient implements SchemeNetworkClient {\n readonly scheme = SCHEME_EXACT_DIRECT;\n\n constructor(\n private readonly signer: TezosSigner,\n config: ExactDirectTezosClientConfig = {},\n ) {\n // Config reserved for future use\n void config;\n }\n\n /**\n * Create a payment payload by executing the transfer\n */\n async createPaymentPayload(\n t402Version: number,\n paymentRequirements: PaymentRequirements,\n ): Promise<Pick<PaymentPayload, \"t402Version\" | \"payload\">> {\n // Validate requirements\n this.validateRequirements(paymentRequirements);\n\n // Get sender address\n const from = await this.signer.getAddress();\n\n // Parse asset to get contract address and token ID\n const assetInfo = this.parseAssetIdentifier(paymentRequirements.asset);\n if (!assetInfo) {\n throw new Error(`Invalid asset identifier: ${paymentRequirements.asset}`);\n }\n\n // Get amount\n const amount = BigInt(paymentRequirements.amount);\n\n // Check balance\n const balance = await this.signer.getBalance(\n assetInfo.contractAddress,\n assetInfo.tokenId,\n );\n if (balance < amount) {\n throw new Error(\n `Insufficient balance: have ${balance}, need ${amount}`,\n );\n }\n\n // Execute transfer\n const opHash = await this.signer.transfer(\n assetInfo.contractAddress,\n assetInfo.tokenId,\n paymentRequirements.payTo,\n amount,\n );\n\n // Create payload\n const payload: ExactDirectTezosPayload = {\n opHash,\n from,\n to: paymentRequirements.payTo,\n amount: paymentRequirements.amount,\n contractAddress: assetInfo.contractAddress,\n tokenId: assetInfo.tokenId,\n };\n\n return {\n t402Version,\n payload,\n };\n }\n\n /**\n * Parse CAIP-19 asset identifier for Tezos FA2\n * Format: tezos:{chainRef}/fa2:{contractAddress}/{tokenId}\n */\n private parseAssetIdentifier(\n asset: string,\n ): { contractAddress: string; tokenId: number } | null {\n // Try parsing CAIP-19 format\n const caipMatch = asset.match(/^tezos:[^/]+\\/fa2:([^/]+)\\/(\\d+)$/);\n if (caipMatch) {\n return {\n contractAddress: caipMatch[1],\n tokenId: parseInt(caipMatch[2], 10),\n };\n }\n\n // Try simple format: contractAddress/tokenId or just contractAddress (default tokenId 0)\n const simpleMatch = asset.match(/^(KT1[a-zA-Z0-9]+)(?:\\/(\\d+))?$/);\n if (simpleMatch) {\n return {\n contractAddress: simpleMatch[1],\n tokenId: simpleMatch[2] ? parseInt(simpleMatch[2], 10) : 0,\n };\n }\n\n return null;\n }\n\n /**\n * Validate payment requirements\n */\n private validateRequirements(requirements: PaymentRequirements): void {\n // Check scheme\n if (requirements.scheme !== SCHEME_EXACT_DIRECT) {\n throw new Error(\n `Invalid scheme: expected ${SCHEME_EXACT_DIRECT}, got ${requirements.scheme}`,\n );\n }\n\n // Check network\n if (!requirements.network.startsWith(`${TEZOS_CAIP2_NAMESPACE}:`)) {\n throw new Error(`Invalid network: ${requirements.network}`);\n }\n\n // Check payTo address\n if (!isValidTezosAddress(requirements.payTo)) {\n throw new Error(`Invalid payTo address: ${requirements.payTo}`);\n }\n\n // Check amount\n const amount = BigInt(requirements.amount);\n if (amount <= 0n) {\n throw new Error(`Invalid amount: ${requirements.amount}`);\n }\n\n // Check asset\n const assetInfo = this.parseAssetIdentifier(requirements.asset);\n if (!assetInfo) {\n throw new Error(`Invalid asset: ${requirements.asset}`);\n }\n\n // Verify token is supported (warn for unknown tokens)\n const tokenConfig = getTokenBySymbol(requirements.network, \"USDt\");\n if (tokenConfig && !compareAddresses(tokenConfig.contractAddress, assetInfo.contractAddress)) {\n console.warn(\n `Using non-standard token: ${assetInfo.contractAddress}`,\n );\n }\n }\n}\n\nexport default ExactDirectTezosClient;\n","/**\n * Registration function for Tezos Exact-Direct client\n */\n\nimport { t402Client } from \"@t402/core/client\";\nimport type { Network } from \"@t402/core/types\";\nimport type { TezosSigner } from \"../../types.js\";\nimport {\n ExactDirectTezosClient,\n type ExactDirectTezosClientConfig,\n} from \"./scheme.js\";\n\n/**\n * Configuration options for registering Tezos schemes to a t402Client\n */\nexport interface TezosClientConfig {\n /**\n * The Tezos signer for payment operations\n */\n signer: TezosSigner;\n\n /**\n * Optional specific networks to register\n * If not provided, registers wildcard support (tezos:*)\n */\n networks?: Network[];\n\n /**\n * Optional scheme configuration\n */\n schemeConfig?: ExactDirectTezosClientConfig;\n}\n\n/**\n * Registers Tezos exact-direct payment scheme to a t402Client instance.\n *\n * @param client - The t402Client instance to register schemes to\n * @param config - Configuration for Tezos client registration\n * @returns The client instance for chaining\n *\n * @example\n * ```typescript\n * import { registerExactDirectTezosClient } from \"@t402/tezos/exact-direct/client\";\n * import { t402Client } from \"@t402/core/client\";\n *\n * const client = new t402Client();\n * registerExactDirectTezosClient(client, {\n * signer: myTezosSigner,\n * networks: [\"tezos:NetXdQprcVkpaWU\"]\n * });\n * ```\n */\nexport function registerExactDirectTezosClient(\n client: t402Client,\n config: TezosClientConfig,\n): t402Client {\n const scheme = new ExactDirectTezosClient(config.signer, config.schemeConfig);\n\n // Register scheme\n if (config.networks && config.networks.length > 0) {\n // Register specific networks\n config.networks.forEach((network) => {\n client.register(network, scheme);\n });\n } else {\n // Register wildcard for all Tezos networks\n client.register(\"tezos:*\", scheme);\n }\n\n return client;\n}\n"],"mappings":";AAKO,IAAM,sBAAsB;AAG5B,IAAM,wBAAwB;AAG9B,IAAM,sBAAsB;AAC5B,IAAM,uBAAuB;AAG7B,IAAM,sBAAsB;AAC5B,IAAM,uBAAuB;AAG7B,IAAM,0BAA0B;AAChC,IAAM,2BAA2B;AAcjC,IAAM,kBAOT;AAAA,EACF,CAAC,mBAAmB,GAAG;AAAA,IACrB,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,YAAY;AAAA,EACd;AAAA,EACA,CAAC,oBAAoB,GAAG;AAAA,IACtB,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,YAAY;AAAA,EACd;AACF;;;ACoFO,SAAS,oBAAoB,SAA0B;AAC5D,MAAI,CAAC,QAAS,QAAO;AAErB,QAAM,gBAAgB;AACtB,MAAI,CAAC,cAAc,KAAK,OAAO,EAAG,QAAO;AAEzC,SAAO,QAAQ,WAAW;AAC5B;;;ACtHO,IAAM,eAA4B;AAAA,EACvC,iBAAiB;AAAA,EACjB,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,UAAU;AACZ;AAKO,IAAM,iBAAgD;AAAA,EAC3D,CAAC,mBAAmB,GAAG,CAAC,YAAY;AAAA,EACpC,CAAC,oBAAoB,GAAG,CAAC;AAC3B;AAKO,IAAM,iBAA0D;AAAA,EACrE,CAAC,mBAAmB,GAAG;AAAA,EACvB,CAAC,oBAAoB,GAAG;AAC1B;AAKO,SAAS,iBACd,SACA,QACyB;AACzB,QAAM,SAAS,eAAe,OAAO;AACrC,MAAI,CAAC,OAAQ,QAAO;AACpB,SAAO,OAAO;AAAA,IACZ,CAAC,MAAM,EAAE,OAAO,YAAY,MAAM,OAAO,YAAY;AAAA,EACvD;AACF;;;ACE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AS,GAAG;AAEjD,WAAO,SAAS,QAAQ,CAAC,YAAY;AACnC,aAAO,SAAS,SAAS,MAAM;AAAA,IACjC,CAAC;AAAA,EACH,OAAO;AAEL,WAAO,SAAS,WAAW,MAAM;AAAA,EACnC;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1,110 @@
1
+ import { SchemeNetworkFacilitator, Network, PaymentPayload, PaymentRequirements, VerifyResponse, SettleResponse } from '@t402/core/types';
2
+ import { F as FacilitatorTezosSigner } from '../../types-DQMtUOa_.cjs';
3
+ import { t402Facilitator } from '@t402/core/facilitator';
4
+
5
+ /**
6
+ * Tezos Exact-Direct Facilitator Scheme
7
+ *
8
+ * Verifies FA2 transfer operations and manages replay protection.
9
+ */
10
+
11
+ /**
12
+ * Configuration for ExactDirectTezosFacilitator
13
+ */
14
+ interface ExactDirectTezosFacilitatorConfig {
15
+ /**
16
+ * Maximum age of operation in seconds (default: 3600 = 1 hour)
17
+ */
18
+ maxOperationAge?: number;
19
+ /**
20
+ * Duration to cache used operation hashes (in milliseconds)
21
+ */
22
+ usedOpCacheDuration?: number;
23
+ }
24
+ /**
25
+ * Tezos Exact-Direct Facilitator
26
+ *
27
+ * Implements the facilitator-side verification and settlement.
28
+ * For exact-direct, settlement is a no-op since client already executed.
29
+ */
30
+ declare class ExactDirectTezosFacilitator implements SchemeNetworkFacilitator {
31
+ private readonly signer;
32
+ readonly scheme = "exact-direct";
33
+ readonly caipFamily = "tezos:*";
34
+ private readonly config;
35
+ private usedOps;
36
+ constructor(signer: FacilitatorTezosSigner, config?: ExactDirectTezosFacilitatorConfig);
37
+ /**
38
+ * Get extra data for a supported kind
39
+ */
40
+ getExtra(network: Network): Record<string, unknown> | undefined;
41
+ /**
42
+ * Get facilitator signer addresses for a network
43
+ */
44
+ getSigners(network: Network): string[];
45
+ /**
46
+ * Verify a payment payload
47
+ */
48
+ verify(payload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>;
49
+ /**
50
+ * Settle a payment (no-op for exact-direct since client already executed)
51
+ */
52
+ settle(payload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>;
53
+ /**
54
+ * Check if an operation has been used
55
+ */
56
+ private isOpUsed;
57
+ /**
58
+ * Mark an operation as used
59
+ */
60
+ private markOpUsed;
61
+ /**
62
+ * Start the cleanup interval for used operations
63
+ */
64
+ private startCleanupInterval;
65
+ }
66
+
67
+ /**
68
+ * Registration function for Tezos Exact-Direct facilitator
69
+ */
70
+
71
+ /**
72
+ * Configuration options for registering Tezos schemes to a t402Facilitator
73
+ */
74
+ interface TezosFacilitatorConfig {
75
+ /**
76
+ * The Tezos signer for facilitator operations (verify and settle)
77
+ */
78
+ signer: FacilitatorTezosSigner;
79
+ /**
80
+ * Optional specific networks to register
81
+ * If not provided, registers wildcard support (tezos:*)
82
+ */
83
+ networks?: Network[];
84
+ /**
85
+ * Optional scheme configuration
86
+ */
87
+ schemeConfig?: ExactDirectTezosFacilitatorConfig;
88
+ }
89
+ /**
90
+ * Registers Tezos exact-direct payment schemes to a t402Facilitator instance.
91
+ *
92
+ * @param facilitator - The t402Facilitator instance to register schemes to
93
+ * @param config - Configuration for Tezos facilitator registration
94
+ * @returns The facilitator instance for chaining
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * import { registerExactDirectTezosFacilitator } from "@t402/tezos/exact-direct/facilitator";
99
+ * import { t402Facilitator } from "@t402/core/facilitator";
100
+ *
101
+ * const facilitator = new t402Facilitator();
102
+ * registerExactDirectTezosFacilitator(facilitator, {
103
+ * signer: myTezosSigner,
104
+ * networks: ["tezos:NetXdQprcVkpaWU"]
105
+ * });
106
+ * ```
107
+ */
108
+ declare function registerExactDirectTezosFacilitator(facilitator: t402Facilitator, config: TezosFacilitatorConfig): t402Facilitator;
109
+
110
+ export { ExactDirectTezosFacilitator, type ExactDirectTezosFacilitatorConfig, type TezosFacilitatorConfig, registerExactDirectTezosFacilitator };
@@ -0,0 +1,110 @@
1
+ import { SchemeNetworkFacilitator, Network, PaymentPayload, PaymentRequirements, VerifyResponse, SettleResponse } from '@t402/core/types';
2
+ import { F as FacilitatorTezosSigner } from '../../types-DQMtUOa_.js';
3
+ import { t402Facilitator } from '@t402/core/facilitator';
4
+
5
+ /**
6
+ * Tezos Exact-Direct Facilitator Scheme
7
+ *
8
+ * Verifies FA2 transfer operations and manages replay protection.
9
+ */
10
+
11
+ /**
12
+ * Configuration for ExactDirectTezosFacilitator
13
+ */
14
+ interface ExactDirectTezosFacilitatorConfig {
15
+ /**
16
+ * Maximum age of operation in seconds (default: 3600 = 1 hour)
17
+ */
18
+ maxOperationAge?: number;
19
+ /**
20
+ * Duration to cache used operation hashes (in milliseconds)
21
+ */
22
+ usedOpCacheDuration?: number;
23
+ }
24
+ /**
25
+ * Tezos Exact-Direct Facilitator
26
+ *
27
+ * Implements the facilitator-side verification and settlement.
28
+ * For exact-direct, settlement is a no-op since client already executed.
29
+ */
30
+ declare class ExactDirectTezosFacilitator implements SchemeNetworkFacilitator {
31
+ private readonly signer;
32
+ readonly scheme = "exact-direct";
33
+ readonly caipFamily = "tezos:*";
34
+ private readonly config;
35
+ private usedOps;
36
+ constructor(signer: FacilitatorTezosSigner, config?: ExactDirectTezosFacilitatorConfig);
37
+ /**
38
+ * Get extra data for a supported kind
39
+ */
40
+ getExtra(network: Network): Record<string, unknown> | undefined;
41
+ /**
42
+ * Get facilitator signer addresses for a network
43
+ */
44
+ getSigners(network: Network): string[];
45
+ /**
46
+ * Verify a payment payload
47
+ */
48
+ verify(payload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>;
49
+ /**
50
+ * Settle a payment (no-op for exact-direct since client already executed)
51
+ */
52
+ settle(payload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>;
53
+ /**
54
+ * Check if an operation has been used
55
+ */
56
+ private isOpUsed;
57
+ /**
58
+ * Mark an operation as used
59
+ */
60
+ private markOpUsed;
61
+ /**
62
+ * Start the cleanup interval for used operations
63
+ */
64
+ private startCleanupInterval;
65
+ }
66
+
67
+ /**
68
+ * Registration function for Tezos Exact-Direct facilitator
69
+ */
70
+
71
+ /**
72
+ * Configuration options for registering Tezos schemes to a t402Facilitator
73
+ */
74
+ interface TezosFacilitatorConfig {
75
+ /**
76
+ * The Tezos signer for facilitator operations (verify and settle)
77
+ */
78
+ signer: FacilitatorTezosSigner;
79
+ /**
80
+ * Optional specific networks to register
81
+ * If not provided, registers wildcard support (tezos:*)
82
+ */
83
+ networks?: Network[];
84
+ /**
85
+ * Optional scheme configuration
86
+ */
87
+ schemeConfig?: ExactDirectTezosFacilitatorConfig;
88
+ }
89
+ /**
90
+ * Registers Tezos exact-direct payment schemes to a t402Facilitator instance.
91
+ *
92
+ * @param facilitator - The t402Facilitator instance to register schemes to
93
+ * @param config - Configuration for Tezos facilitator registration
94
+ * @returns The facilitator instance for chaining
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * import { registerExactDirectTezosFacilitator } from "@t402/tezos/exact-direct/facilitator";
99
+ * import { t402Facilitator } from "@t402/core/facilitator";
100
+ *
101
+ * const facilitator = new t402Facilitator();
102
+ * registerExactDirectTezosFacilitator(facilitator, {
103
+ * signer: myTezosSigner,
104
+ * networks: ["tezos:NetXdQprcVkpaWU"]
105
+ * });
106
+ * ```
107
+ */
108
+ declare function registerExactDirectTezosFacilitator(facilitator: t402Facilitator, config: TezosFacilitatorConfig): t402Facilitator;
109
+
110
+ export { ExactDirectTezosFacilitator, type ExactDirectTezosFacilitatorConfig, type TezosFacilitatorConfig, registerExactDirectTezosFacilitator };