@claw-network/node 0.2.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 (190) hide show
  1. package/README.md +49 -0
  2. package/dist/api/api-key-store.d.ts +74 -0
  3. package/dist/api/api-key-store.d.ts.map +1 -0
  4. package/dist/api/api-key-store.js +170 -0
  5. package/dist/api/api-key-store.js.map +1 -0
  6. package/dist/api/auth.d.ts +30 -0
  7. package/dist/api/auth.d.ts.map +1 -0
  8. package/dist/api/auth.js +115 -0
  9. package/dist/api/auth.js.map +1 -0
  10. package/dist/api/legacy.d.ts +26 -0
  11. package/dist/api/legacy.d.ts.map +1 -0
  12. package/dist/api/legacy.js +281 -0
  13. package/dist/api/legacy.js.map +1 -0
  14. package/dist/api/middleware.d.ts +35 -0
  15. package/dist/api/middleware.d.ts.map +1 -0
  16. package/dist/api/middleware.js +75 -0
  17. package/dist/api/middleware.js.map +1 -0
  18. package/dist/api/response.d.ts +85 -0
  19. package/dist/api/response.d.ts.map +1 -0
  20. package/dist/api/response.js +185 -0
  21. package/dist/api/response.js.map +1 -0
  22. package/dist/api/router.d.ts +45 -0
  23. package/dist/api/router.d.ts.map +1 -0
  24. package/dist/api/router.js +183 -0
  25. package/dist/api/router.js.map +1 -0
  26. package/dist/api/routes/admin.d.ts +11 -0
  27. package/dist/api/routes/admin.d.ts.map +1 -0
  28. package/dist/api/routes/admin.js +124 -0
  29. package/dist/api/routes/admin.js.map +1 -0
  30. package/dist/api/routes/contracts.d.ts +7 -0
  31. package/dist/api/routes/contracts.d.ts.map +1 -0
  32. package/dist/api/routes/contracts.js +665 -0
  33. package/dist/api/routes/contracts.js.map +1 -0
  34. package/dist/api/routes/dao.d.ts +7 -0
  35. package/dist/api/routes/dao.d.ts.map +1 -0
  36. package/dist/api/routes/dao.js +549 -0
  37. package/dist/api/routes/dao.js.map +1 -0
  38. package/dist/api/routes/dev.d.ts +9 -0
  39. package/dist/api/routes/dev.d.ts.map +1 -0
  40. package/dist/api/routes/dev.js +273 -0
  41. package/dist/api/routes/dev.js.map +1 -0
  42. package/dist/api/routes/escrows.d.ts +7 -0
  43. package/dist/api/routes/escrows.d.ts.map +1 -0
  44. package/dist/api/routes/escrows.js +454 -0
  45. package/dist/api/routes/escrows.js.map +1 -0
  46. package/dist/api/routes/identities.d.ts +7 -0
  47. package/dist/api/routes/identities.d.ts.map +1 -0
  48. package/dist/api/routes/identities.js +245 -0
  49. package/dist/api/routes/identities.js.map +1 -0
  50. package/dist/api/routes/markets-capabilities.d.ts +7 -0
  51. package/dist/api/routes/markets-capabilities.d.ts.map +1 -0
  52. package/dist/api/routes/markets-capabilities.js +477 -0
  53. package/dist/api/routes/markets-capabilities.js.map +1 -0
  54. package/dist/api/routes/markets-disputes.d.ts +7 -0
  55. package/dist/api/routes/markets-disputes.d.ts.map +1 -0
  56. package/dist/api/routes/markets-disputes.js +102 -0
  57. package/dist/api/routes/markets-disputes.js.map +1 -0
  58. package/dist/api/routes/markets-info.d.ts +7 -0
  59. package/dist/api/routes/markets-info.d.ts.map +1 -0
  60. package/dist/api/routes/markets-info.js +523 -0
  61. package/dist/api/routes/markets-info.js.map +1 -0
  62. package/dist/api/routes/markets-search.d.ts +7 -0
  63. package/dist/api/routes/markets-search.d.ts.map +1 -0
  64. package/dist/api/routes/markets-search.js +38 -0
  65. package/dist/api/routes/markets-search.js.map +1 -0
  66. package/dist/api/routes/markets-tasks.d.ts +7 -0
  67. package/dist/api/routes/markets-tasks.d.ts.map +1 -0
  68. package/dist/api/routes/markets-tasks.js +539 -0
  69. package/dist/api/routes/markets-tasks.js.map +1 -0
  70. package/dist/api/routes/node.d.ts +7 -0
  71. package/dist/api/routes/node.d.ts.map +1 -0
  72. package/dist/api/routes/node.js +53 -0
  73. package/dist/api/routes/node.js.map +1 -0
  74. package/dist/api/routes/nonce.d.ts +10 -0
  75. package/dist/api/routes/nonce.d.ts.map +1 -0
  76. package/dist/api/routes/nonce.js +65 -0
  77. package/dist/api/routes/nonce.js.map +1 -0
  78. package/dist/api/routes/reputations.d.ts +7 -0
  79. package/dist/api/routes/reputations.d.ts.map +1 -0
  80. package/dist/api/routes/reputations.js +243 -0
  81. package/dist/api/routes/reputations.js.map +1 -0
  82. package/dist/api/routes/transfers.d.ts +7 -0
  83. package/dist/api/routes/transfers.d.ts.map +1 -0
  84. package/dist/api/routes/transfers.js +88 -0
  85. package/dist/api/routes/transfers.js.map +1 -0
  86. package/dist/api/routes/wallets.d.ts +7 -0
  87. package/dist/api/routes/wallets.d.ts.map +1 -0
  88. package/dist/api/routes/wallets.js +132 -0
  89. package/dist/api/routes/wallets.js.map +1 -0
  90. package/dist/api/schemas/common.d.ts +45 -0
  91. package/dist/api/schemas/common.d.ts.map +1 -0
  92. package/dist/api/schemas/common.js +30 -0
  93. package/dist/api/schemas/common.js.map +1 -0
  94. package/dist/api/schemas/contracts.d.ts +284 -0
  95. package/dist/api/schemas/contracts.d.ts.map +1 -0
  96. package/dist/api/schemas/contracts.js +79 -0
  97. package/dist/api/schemas/contracts.js.map +1 -0
  98. package/dist/api/schemas/dao.d.ts +271 -0
  99. package/dist/api/schemas/dao.d.ts.map +1 -0
  100. package/dist/api/schemas/dao.js +78 -0
  101. package/dist/api/schemas/dao.js.map +1 -0
  102. package/dist/api/schemas/identity.d.ts +75 -0
  103. package/dist/api/schemas/identity.d.ts.map +1 -0
  104. package/dist/api/schemas/identity.js +32 -0
  105. package/dist/api/schemas/identity.js.map +1 -0
  106. package/dist/api/schemas/markets.d.ts +822 -0
  107. package/dist/api/schemas/markets.d.ts.map +1 -0
  108. package/dist/api/schemas/markets.js +246 -0
  109. package/dist/api/schemas/markets.js.map +1 -0
  110. package/dist/api/schemas/wallet.d.ts +163 -0
  111. package/dist/api/schemas/wallet.d.ts.map +1 -0
  112. package/dist/api/schemas/wallet.js +54 -0
  113. package/dist/api/schemas/wallet.js.map +1 -0
  114. package/dist/api/server.d.ts +45 -0
  115. package/dist/api/server.d.ts.map +1 -0
  116. package/dist/api/server.js +131 -0
  117. package/dist/api/server.js.map +1 -0
  118. package/dist/api/types.d.ts +69 -0
  119. package/dist/api/types.d.ts.map +1 -0
  120. package/dist/api/types.js +196 -0
  121. package/dist/api/types.js.map +1 -0
  122. package/dist/daemon.d.ts +11 -0
  123. package/dist/daemon.d.ts.map +1 -0
  124. package/dist/daemon.js +248 -0
  125. package/dist/daemon.js.map +1 -0
  126. package/dist/index.d.ts +137 -0
  127. package/dist/index.d.ts.map +1 -0
  128. package/dist/index.js +795 -0
  129. package/dist/index.js.map +1 -0
  130. package/dist/indexer/index.d.ts +10 -0
  131. package/dist/indexer/index.d.ts.map +1 -0
  132. package/dist/indexer/index.js +7 -0
  133. package/dist/indexer/index.js.map +1 -0
  134. package/dist/indexer/indexer.d.ts +60 -0
  135. package/dist/indexer/indexer.d.ts.map +1 -0
  136. package/dist/indexer/indexer.js +408 -0
  137. package/dist/indexer/indexer.js.map +1 -0
  138. package/dist/indexer/query.d.ts +141 -0
  139. package/dist/indexer/query.d.ts.map +1 -0
  140. package/dist/indexer/query.js +244 -0
  141. package/dist/indexer/query.js.map +1 -0
  142. package/dist/indexer/store.d.ts +95 -0
  143. package/dist/indexer/store.d.ts.map +1 -0
  144. package/dist/indexer/store.js +250 -0
  145. package/dist/indexer/store.js.map +1 -0
  146. package/dist/logger.d.ts +13 -0
  147. package/dist/logger.d.ts.map +1 -0
  148. package/dist/logger.js +37 -0
  149. package/dist/logger.js.map +1 -0
  150. package/dist/p2p/sync.d.ts +105 -0
  151. package/dist/p2p/sync.d.ts.map +1 -0
  152. package/dist/p2p/sync.js +875 -0
  153. package/dist/p2p/sync.js.map +1 -0
  154. package/dist/policy/liquidity-policy.d.ts +17 -0
  155. package/dist/policy/liquidity-policy.d.ts.map +1 -0
  156. package/dist/policy/liquidity-policy.js +112 -0
  157. package/dist/policy/liquidity-policy.js.map +1 -0
  158. package/dist/services/chain-config.d.ts +226 -0
  159. package/dist/services/chain-config.d.ts.map +1 -0
  160. package/dist/services/chain-config.js +105 -0
  161. package/dist/services/chain-config.js.map +1 -0
  162. package/dist/services/contract-provider.d.ts +44 -0
  163. package/dist/services/contract-provider.d.ts.map +1 -0
  164. package/dist/services/contract-provider.js +167 -0
  165. package/dist/services/contract-provider.js.map +1 -0
  166. package/dist/services/contracts-service.d.ts +192 -0
  167. package/dist/services/contracts-service.d.ts.map +1 -0
  168. package/dist/services/contracts-service.js +336 -0
  169. package/dist/services/contracts-service.js.map +1 -0
  170. package/dist/services/dao-service.d.ts +245 -0
  171. package/dist/services/dao-service.d.ts.map +1 -0
  172. package/dist/services/dao-service.js +389 -0
  173. package/dist/services/dao-service.js.map +1 -0
  174. package/dist/services/identity-service.d.ts +150 -0
  175. package/dist/services/identity-service.d.ts.map +1 -0
  176. package/dist/services/identity-service.js +286 -0
  177. package/dist/services/identity-service.js.map +1 -0
  178. package/dist/services/index.d.ts +20 -0
  179. package/dist/services/index.d.ts.map +1 -0
  180. package/dist/services/index.js +15 -0
  181. package/dist/services/index.js.map +1 -0
  182. package/dist/services/reputation-service.d.ts +128 -0
  183. package/dist/services/reputation-service.d.ts.map +1 -0
  184. package/dist/services/reputation-service.js +204 -0
  185. package/dist/services/reputation-service.js.map +1 -0
  186. package/dist/services/wallet-service.d.ts +201 -0
  187. package/dist/services/wallet-service.d.ts.map +1 -0
  188. package/dist/services/wallet-service.js +402 -0
  189. package/dist/services/wallet-service.js.map +1 -0
  190. package/package.json +66 -0
@@ -0,0 +1,204 @@
1
+ /**
2
+ * ReputationService — on-chain reputation operations for ClawReputation.
3
+ *
4
+ * This service handles all reputation write/read operations that hit the
5
+ * chain. Route handlers delegate to it; it calls ClawReputation.sol via
6
+ * ContractProvider and reads indexed review data from IndexerQuery.
7
+ *
8
+ * Design decisions:
9
+ * - DID → bytes32 hash via `keccak256(toUtf8Bytes(did))`.
10
+ * - Review hash is `keccak256(toUtf8Bytes(reviewId))` where
11
+ * `reviewId` is the opaque identifier from the REST layer.
12
+ * - `anchorReputation` requires ANCHOR_ROLE — the node signer
13
+ * must hold that role on the contract.
14
+ * - `recordReview` also requires ANCHOR_ROLE.
15
+ * - Scores are uint16, max 1000 (contract-enforced).
16
+ */
17
+ import { keccak256, toUtf8Bytes } from 'ethers';
18
+ import { createLogger } from '../logger.js';
19
+ // ---------------------------------------------------------------------------
20
+ // ReputationService
21
+ // ---------------------------------------------------------------------------
22
+ export class ReputationService {
23
+ contracts;
24
+ indexer;
25
+ log;
26
+ constructor(contracts, indexer, logger) {
27
+ this.contracts = contracts;
28
+ this.indexer = indexer;
29
+ this.log = logger ?? createLogger({ level: 'info' });
30
+ }
31
+ // ========================================================================
32
+ // READ operations
33
+ // ========================================================================
34
+ /**
35
+ * Fetch an agent's on-chain reputation profile.
36
+ *
37
+ * Reads `getReputation()` for the top-level score and epoch, then
38
+ * `getLatestSnapshot()` for dimensional scores and merkle root.
39
+ *
40
+ * @returns Profile, or `null` if DID has no reputation on-chain.
41
+ */
42
+ async getProfile(did) {
43
+ const didHash = this.hashDid(did);
44
+ const reputation = this.contracts.reputation;
45
+ try {
46
+ const [score, epoch] = await reputation.getReputation(didHash);
47
+ const overallScore = Number(score);
48
+ const epochNum = Number(epoch);
49
+ // If score and epoch are both 0, no reputation exists yet.
50
+ if (overallScore === 0 && epochNum === 0) {
51
+ return null;
52
+ }
53
+ const snapshot = await reputation.getLatestSnapshot(didHash);
54
+ return {
55
+ did,
56
+ score: overallScore,
57
+ epoch: epochNum,
58
+ dimensions: {
59
+ transaction: Number(snapshot.transactionScore),
60
+ fulfillment: Number(snapshot.fulfillmentScore),
61
+ quality: Number(snapshot.qualityScore),
62
+ social: Number(snapshot.socialScore),
63
+ behavior: Number(snapshot.behaviorScore),
64
+ },
65
+ merkleRoot: snapshot.merkleRoot,
66
+ timestamp: Number(snapshot.timestamp),
67
+ };
68
+ }
69
+ catch {
70
+ return null;
71
+ }
72
+ }
73
+ /**
74
+ * Get reviews for a DID from the indexer.
75
+ *
76
+ * @param subjectDid The DID whose reviews to fetch.
77
+ * @param opts Pagination options.
78
+ */
79
+ getReviews(subjectDid, opts = {}) {
80
+ if (!this.indexer)
81
+ return null;
82
+ const filter = {
83
+ subjectDid: this.hashDid(subjectDid),
84
+ limit: opts.limit,
85
+ offset: opts.offset,
86
+ };
87
+ const result = this.indexer.getReviews(filter);
88
+ const reviews = result.items.map((r) => ({
89
+ reviewHash: r.reviewHash,
90
+ reviewerDid: r.reviewerDid,
91
+ subjectDid: r.subjectDid,
92
+ relatedTxHash: r.relatedTxHash,
93
+ timestamp: r.timestamp,
94
+ }));
95
+ return {
96
+ reviews,
97
+ total: result.total,
98
+ pagination: {
99
+ limit: result.limit,
100
+ offset: result.offset,
101
+ hasMore: result.offset + result.limit < result.total,
102
+ },
103
+ };
104
+ }
105
+ /**
106
+ * Verify a review anchor on-chain.
107
+ */
108
+ async verifyReview(reviewId) {
109
+ const reviewHash = this.hashId(reviewId);
110
+ try {
111
+ const anchor = await this.contracts.reputation.verifyReview(reviewHash);
112
+ return {
113
+ reviewerDid: anchor.reviewerDIDHash,
114
+ subjectDid: anchor.subjectDIDHash,
115
+ txHash: anchor.txHash,
116
+ timestamp: Number(anchor.timestamp),
117
+ exists: anchor.exists,
118
+ };
119
+ }
120
+ catch {
121
+ return null;
122
+ }
123
+ }
124
+ // ========================================================================
125
+ // WRITE operations
126
+ // ========================================================================
127
+ /**
128
+ * Record a review on-chain.
129
+ *
130
+ * The node signer must have ANCHOR_ROLE on ClawReputation.
131
+ *
132
+ * @param reviewId Opaque review identifier (hashed to bytes32).
133
+ * @param reviewerDid Reviewer's full DID string.
134
+ * @param subjectDid Subject's full DID string.
135
+ * @param relatedTxHash Associated transaction hash (bytes32).
136
+ */
137
+ async recordReview(reviewId, reviewerDid, subjectDid, relatedTxHash) {
138
+ const reviewHash = this.hashId(reviewId);
139
+ const reviewerHash = this.hashDid(reviewerDid);
140
+ const subjectHash = this.hashDid(subjectDid);
141
+ this.log.info('Reputation recordReview: reviewer=%s subject=%s', reviewerDid, subjectDid);
142
+ const tx = await this.contracts.reputation.recordReview(reviewHash, reviewerHash, subjectHash, relatedTxHash);
143
+ const receipt = await tx.wait();
144
+ return {
145
+ txHash: receipt.hash,
146
+ reviewHash,
147
+ subjectDid,
148
+ reviewerDid,
149
+ timestamp: Date.now(),
150
+ };
151
+ }
152
+ /**
153
+ * Anchor a reputation snapshot on-chain.
154
+ *
155
+ * The node signer must have ANCHOR_ROLE on ClawReputation.
156
+ *
157
+ * @param did Agent's full DID string.
158
+ * @param overallScore 0–1000.
159
+ * @param dimensionScores [transaction, fulfillment, quality, social, behavior] — each 0–1000.
160
+ * @param merkleRoot Merkle root of all reviews in this epoch.
161
+ */
162
+ async anchorReputation(did, overallScore, dimensionScores, merkleRoot) {
163
+ const didHash = this.hashDid(did);
164
+ this.log.info('Reputation anchor: %s score=%d', did, overallScore);
165
+ const tx = await this.contracts.reputation.anchorReputation(didHash, overallScore, dimensionScores, merkleRoot);
166
+ const receipt = await tx.wait();
167
+ // Parse epoch from ReputationAnchored event
168
+ let epoch = 0;
169
+ for (const log of receipt.logs) {
170
+ try {
171
+ const parsed = this.contracts.reputation.interface.parseLog({
172
+ topics: log.topics,
173
+ data: log.data,
174
+ });
175
+ if (parsed?.name === 'ReputationAnchored') {
176
+ epoch = Number(parsed.args[1]);
177
+ break;
178
+ }
179
+ }
180
+ catch {
181
+ // Not our event — skip.
182
+ }
183
+ }
184
+ return {
185
+ txHash: receipt.hash,
186
+ did,
187
+ overallScore,
188
+ epoch,
189
+ timestamp: Date.now(),
190
+ };
191
+ }
192
+ // ========================================================================
193
+ // Helpers
194
+ // ========================================================================
195
+ /** Hash a DID string to bytes32. */
196
+ hashDid(did) {
197
+ return keccak256(toUtf8Bytes(did));
198
+ }
199
+ /** Hash an opaque identifier string to bytes32. */
200
+ hashId(id) {
201
+ return keccak256(toUtf8Bytes(id));
202
+ }
203
+ }
204
+ //# sourceMappingURL=reputation-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reputation-service.js","sourceRoot":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OAAO;YACL,MAAM,EAAE,OAAO,CAAC,IAAI;YACpB,GAAG;YACH,YAAY;YACZ,KAAK;YACL,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC;IACJ,CAAC;IAED,2EAA2E;IAC3E,UAAU;IACV,2EAA2E;IAE3E,oCAAoC;IAC5B,OAAO,CAAC,GAAW;QACzB,OAAO,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;IACrC,CAAC;IAED,mDAAmD;IAC3C,MAAM,CAAC,EAAU;QACvB,OAAO,SAAS,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;IACpC,CAAC;CACF"}
@@ -0,0 +1,201 @@
1
+ /**
2
+ * WalletService — on-chain wallet operations for ClawToken and ClawEscrow.
3
+ *
4
+ * This service is the **single point of truth** for all wallet write/read
5
+ * operations. API route handlers delegate to it; it calls the smart
6
+ * contracts via ContractProvider and reads indexed history from
7
+ * IndexerQuery.
8
+ *
9
+ * Design decisions:
10
+ * - Token amounts are **integers** (ClawToken has 0 decimals).
11
+ * - Escrow ID is an opaque string at the REST layer; it's hashed to
12
+ * `bytes32` (keccak256) before hitting the chain.
13
+ * - DID-to-address resolution goes through ClawIdentity.getController()
14
+ * when an on-chain lookup is desired, or through the local
15
+ * `addressFromDid` helper for off-chain derivation.
16
+ * - All write methods return a receipt-like object that matches the
17
+ * existing REST response shape (txHash, amount, status, timestamp).
18
+ */
19
+ import { createLogger } from '../logger.js';
20
+ import type { ContractProvider } from '../services/contract-provider.js';
21
+ import type { IndexerQuery, EscrowFilter } from '../indexer/query.js';
22
+ type Logger = ReturnType<typeof createLogger>;
23
+ export interface TransferResult {
24
+ txHash: string;
25
+ from: string;
26
+ to: string;
27
+ amount: string;
28
+ fee?: string;
29
+ status: string;
30
+ timestamp: number;
31
+ }
32
+ export interface BalanceResult {
33
+ balance: string;
34
+ available: string;
35
+ pending: string;
36
+ locked: string;
37
+ }
38
+ export interface EscrowView {
39
+ id: string;
40
+ depositor: string;
41
+ beneficiary: string;
42
+ arbiter: string;
43
+ amount: string;
44
+ status: string;
45
+ createdAt: number;
46
+ expiresAt: number;
47
+ expired: boolean;
48
+ }
49
+ export interface EscrowActionResult {
50
+ txHash: string;
51
+ amount: string;
52
+ status: string;
53
+ timestamp: number;
54
+ }
55
+ export interface EscrowCreateResult {
56
+ id: string;
57
+ amount: string;
58
+ released: string;
59
+ remaining: string;
60
+ status: string;
61
+ createdAt: number;
62
+ expiresAt?: number;
63
+ expired: boolean;
64
+ }
65
+ export interface TransactionHistoryResult {
66
+ transactions: TransferRow[];
67
+ total: number;
68
+ hasMore: boolean;
69
+ pagination: {
70
+ limit: number;
71
+ offset: number;
72
+ };
73
+ }
74
+ interface TransferRow {
75
+ txHash: string;
76
+ from: string;
77
+ to: string;
78
+ amount: string;
79
+ type: string;
80
+ status: string;
81
+ timestamp: number;
82
+ }
83
+ export declare class WalletService {
84
+ private readonly contracts;
85
+ private readonly indexer?;
86
+ private readonly log;
87
+ constructor(contracts: ContractProvider, indexer?: IndexerQuery | undefined, logger?: Logger);
88
+ /**
89
+ * Fetch on-chain Token balance for a given EVM address.
90
+ *
91
+ * ClawToken has 0 decimals, so balanceOf returns an integer directly.
92
+ */
93
+ getBalance(address: string): Promise<BalanceResult>;
94
+ /**
95
+ * Return the EVM transaction count (nonce) for a given address.
96
+ *
97
+ * This is the standard Ethereum nonce — the number of transactions
98
+ * sent from the address. Useful for clients that need to construct
99
+ * or sequence their own transactions.
100
+ */
101
+ getNonce(address: string): Promise<{
102
+ nonce: number;
103
+ address: string;
104
+ }>;
105
+ /**
106
+ * Resolve a DID `did:claw:…` to an EVM address via the on-chain
107
+ * ClawIdentity registry.
108
+ *
109
+ * @returns The controller address, or `null` if the DID is not registered.
110
+ */
111
+ resolveDidToAddress(did: string): Promise<string | null>;
112
+ /**
113
+ * Execute an on-chain Token transfer.
114
+ *
115
+ * Uses the burn+mint pattern: tokens are burned from the sender's
116
+ * per-DID address and minted to the receiver's address. This avoids
117
+ * the need for per-DID private keys or ETH gas funding — the node
118
+ * signer holds MINTER_ROLE and BURNER_ROLE.
119
+ *
120
+ * For the special case where `from` is `'faucet'` or the node signer,
121
+ * a direct `transfer()` is used instead (since the signer actually
122
+ * holds those tokens).
123
+ */
124
+ transfer(from: string, to: string, amount: bigint | number, memo?: string): Promise<TransferResult>;
125
+ /**
126
+ * Mint new Tokens to a target address.
127
+ *
128
+ * The node's signer **must** hold MINTER_ROLE on ClawToken for this to
129
+ * succeed. Typically used by the dev faucet so it can create fresh Tokens
130
+ * without requiring a pre-funded balance.
131
+ *
132
+ * @param to Target EVM address.
133
+ * @param amount Number of Tokens to mint (integer, 0 decimals).
134
+ * @param memo Optional human-readable memo (logged, not stored on-chain).
135
+ */
136
+ mint(to: string, amount: bigint | number, memo?: string): Promise<TransferResult>;
137
+ /**
138
+ * Create a new escrow.
139
+ *
140
+ * Steps:
141
+ * 1. `token.approve(escrowAddress, amount)` — allow the escrow contract
142
+ * to pull Tokens from the signer.
143
+ * 2. `escrow.createEscrow(id, beneficiary, arbiter, amount, expiresAt)`
144
+ */
145
+ createEscrow(params: {
146
+ escrowId: string;
147
+ beneficiary: string;
148
+ arbiter?: string;
149
+ amount: bigint | number;
150
+ expiresAt?: number;
151
+ }): Promise<EscrowCreateResult>;
152
+ /**
153
+ * Fund (top up) an existing escrow.
154
+ */
155
+ fundEscrow(escrowId: string, amount: bigint | number): Promise<EscrowActionResult>;
156
+ /**
157
+ * Release escrow funds to the beneficiary.
158
+ */
159
+ releaseEscrow(escrowId: string): Promise<EscrowActionResult>;
160
+ /**
161
+ * Refund escrow funds to the depositor.
162
+ */
163
+ refundEscrow(escrowId: string): Promise<EscrowActionResult>;
164
+ /**
165
+ * Expire an escrow that has passed its `expiresAt` timestamp.
166
+ */
167
+ expireEscrow(escrowId: string): Promise<EscrowActionResult>;
168
+ /**
169
+ * Open a dispute on an active escrow.
170
+ */
171
+ disputeEscrow(escrowId: string): Promise<EscrowActionResult>;
172
+ /**
173
+ * Resolve a disputed escrow (arbiter only).
174
+ *
175
+ * @param releaseToBeneficiary `true` → release to beneficiary; `false` → refund to depositor.
176
+ */
177
+ resolveEscrow(escrowId: string, releaseToBeneficiary: boolean): Promise<EscrowActionResult>;
178
+ /**
179
+ * Fetch on-chain escrow state.
180
+ */
181
+ getEscrow(escrowId: string): Promise<EscrowView | null>;
182
+ /**
183
+ * Query transfer history from the indexer.
184
+ */
185
+ getHistory(address: string, opts?: {
186
+ limit?: number;
187
+ offset?: number;
188
+ type?: string;
189
+ }): TransactionHistoryResult;
190
+ /**
191
+ * Query escrow records from the indexer.
192
+ */
193
+ getEscrows(filter?: EscrowFilter): import("../indexer/query.js").PaginatedResult<import("../indexer/query.js").EscrowRow>;
194
+ /**
195
+ * Read the stored amount for an escrow (post-tx).
196
+ * Returns 0 if the escrow can't be read.
197
+ */
198
+ private getEscrowAmount;
199
+ }
200
+ export {};
201
+ //# sourceMappingURL=wallet-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet-service.d.ts","sourceRoot":"","sources":["../../src/services/wallet-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAMtE,KAAK,MAAM,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;AAI9C,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,aAAa;IA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