trustbase-seller-backend 0.7.0 → 0.7.2

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 (655) hide show
  1. package/.claude/skills/wechatpay-payment-integration/SKILL.md +165 -0
  2. package/.claude/skills/wechatpay-payment-integration/references/APIv3/346/216/245/345/217/243/345/212/250/346/200/201/346/216/222/351/232/234.md +234 -0
  3. package/.claude/skills/wechatpay-payment-integration/references/wechatpay-dev-cli/344/275/277/347/224/250/350/257/264/346/230/216.md +74 -0
  4. package/.claude/skills/wechatpay-payment-integration/references//345/237/272/347/241/200/346/246/202/345/277/265/345/217/212/344/270/232/345/212/241/344/273/213/347/273/215.md +109 -0
  5. package/.claude/skills/wechatpay-payment-integration/references//345/246/202/344/275/225/347/220/206/350/247/243/347/224/250/346/210/267/351/227/256/351/242/230.md +55 -0
  6. package/.claude/skills/wechatpay-payment-integration/references//346/216/245/345/205/245/350/264/250/351/207/217/346/243/200/346/237/245/346/270/205/345/215/225.md +70 -0
  7. package/.claude/skills/wechatpay-payment-integration/references//346/226/207/346/241/243/346/243/200/347/264/242/344/270/216/351/227/256/347/255/224.md +99 -0
  8. package/.claude/skills/wechatpay-payment-integration/scripts/bash/extract_and_sign.sh +231 -0
  9. package/.claude/skills/wechatpay-payment-integration/scripts/powershell/extract_and_sign.ps1 +199 -0
  10. package/.claude/skills/wechatpay-payment-integration/scripts/wechatpay-resource-sync.py +646 -0
  11. package/MERCHANT-EDITION.md +56 -56
  12. package/README.md +99 -99
  13. package/api/__tests__/test-utils.d.ts +32 -32
  14. package/api/__tests__/test-utils.js +89 -89
  15. package/api/db/case-library-types.d.ts +46 -46
  16. package/api/db/case-library-types.js +79 -79
  17. package/api/db/case-library.db.d.ts +4 -4
  18. package/api/db/case-library.db.js +70 -70
  19. package/api/email.d.ts +55 -55
  20. package/api/email.js +305 -305
  21. package/api/index.d.ts +21 -21
  22. package/api/index.js +40 -40
  23. package/api/integration-helpers.d.ts +152 -152
  24. package/api/integration-helpers.js +354 -354
  25. package/api/lib/case-summary.d.ts +9 -9
  26. package/api/lib/case-summary.js +29 -29
  27. package/api/middleware/p2p-auth.d.ts +139 -139
  28. package/api/middleware/p2p-auth.js +506 -506
  29. package/api/middleware/tool-policy.d.ts +46 -46
  30. package/api/middleware/tool-policy.js +155 -155
  31. package/api/oauth-adapters.d.ts +191 -191
  32. package/api/oauth-adapters.js +455 -455
  33. package/api/oauth-bridge.d.ts +55 -55
  34. package/api/oauth-bridge.js +615 -615
  35. package/api/oauth-router.d.ts +35 -35
  36. package/api/oauth-router.js +53 -53
  37. package/api/p2p-mcp.d.ts +68 -68
  38. package/api/p2p-mcp.js +285 -285
  39. package/api/payment-store.d.ts +55 -55
  40. package/api/payment-store.js +122 -122
  41. package/api/payment.d.ts +13 -13
  42. package/api/payment.js +215 -215
  43. package/api/pwa-stats.d.ts +66 -66
  44. package/api/pwa-stats.js +259 -259
  45. package/api/riskcontrol-adapters.d.ts +113 -113
  46. package/api/riskcontrol-adapters.js +209 -209
  47. package/api/riskcontrol-event-whitelist.d.ts +80 -80
  48. package/api/riskcontrol-event-whitelist.js +180 -180
  49. package/api/riskcontrol-rules.d.ts +104 -104
  50. package/api/riskcontrol-rules.js +381 -381
  51. package/api/riskcontrol-scoring.d.ts +71 -71
  52. package/api/riskcontrol-scoring.js +324 -324
  53. package/api/riskcontrol-service.d.ts +59 -59
  54. package/api/riskcontrol-service.js +200 -200
  55. package/api/riskcontrol-stream.d.ts +111 -111
  56. package/api/riskcontrol-stream.js +229 -229
  57. package/api/riskcontrol-v5-e2e-test.d.ts +14 -14
  58. package/api/riskcontrol-v5-e2e-test.js +203 -203
  59. package/api/riskcontrol-v5-router.d.ts +47 -47
  60. package/api/riskcontrol-v5-router.js +223 -223
  61. package/api/riskcontrol-webhook.d.ts +87 -87
  62. package/api/riskcontrol-webhook.js +282 -282
  63. package/api/routes/case-library.d.ts +3 -3
  64. package/api/routes/case-library.js +86 -86
  65. package/api/seller-earnings.d.ts +22 -22
  66. package/api/seller-earnings.js +244 -244
  67. package/api/seller-orders.d.ts +24 -24
  68. package/api/seller-orders.js +393 -393
  69. package/api/seller-products-chain.d.ts +30 -30
  70. package/api/seller-products-chain.js +303 -303
  71. package/api/seller-products.d.ts +29 -29
  72. package/api/seller-products.js +496 -444
  73. package/api/seller-reputation.d.ts +20 -20
  74. package/api/seller-reputation.js +202 -202
  75. package/api/seller-router.d.ts +32 -32
  76. package/api/seller-router.js +52 -52
  77. package/api/seller-settings.d.ts +22 -22
  78. package/api/seller-settings.js +270 -270
  79. package/api/shipping-store.d.ts +43 -43
  80. package/api/shipping-store.js +112 -112
  81. package/api/shipping.d.ts +15 -15
  82. package/api/shipping.js +238 -238
  83. package/api/social-endpoint-verify.d.ts +7 -7
  84. package/api/social-endpoint-verify.js +151 -151
  85. package/api/social-posts.d.ts +64 -64
  86. package/api/social-posts.js +510 -510
  87. package/api/social-router.d.ts +35 -35
  88. package/api/social-router.js +55 -55
  89. package/api/social-shares.d.ts +39 -39
  90. package/api/social-shares.js +364 -364
  91. package/api/social-whitelist.d.ts +40 -40
  92. package/api/social-whitelist.js +251 -251
  93. package/api/storefront-orders.d.ts +22 -22
  94. package/api/storefront-orders.js +344 -344
  95. package/api/storefront-products.d.ts +20 -20
  96. package/api/storefront-products.js +176 -176
  97. package/api/storefront-router.d.ts +32 -32
  98. package/api/storefront-router.js +46 -46
  99. package/api/storefront-user.d.ts +22 -22
  100. package/api/storefront-user.js +325 -325
  101. package/api/test-db-helper.d.ts +28 -28
  102. package/api/test-db-helper.js +106 -106
  103. package/api/traffic.d.ts +21 -21
  104. package/api/traffic.js +111 -111
  105. package/api/wc-credentials.d.ts +33 -33
  106. package/api/wc-credentials.js +273 -273
  107. package/api/wc-tracker-receiver.d.ts +46 -46
  108. package/api/wc-tracker-receiver.js +257 -257
  109. package/api/wc-v3.d.ts +39 -39
  110. package/api/wc-v3.js +665 -665
  111. package/audit-page-scripts.js +61 -61
  112. package/bin/tb-doctor.js +32 -32
  113. package/check.d.ts +1 -1
  114. package/check.js +18 -18
  115. package/config/export-import.d.ts +107 -107
  116. package/config/export-import.js +233 -233
  117. package/config/manager.d.ts +72 -72
  118. package/config/manager.js +245 -245
  119. package/config/profile.d.ts +41 -41
  120. package/config/profile.js +110 -110
  121. package/core/address.js +23 -23
  122. package/core/advanced-performance.d.ts +148 -148
  123. package/core/advanced-performance.js +323 -323
  124. package/core/alerting.d.ts +90 -90
  125. package/core/alerting.js +362 -362
  126. package/core/app-store.d.ts +76 -76
  127. package/core/app-store.js +206 -206
  128. package/core/boot-env.js +39 -39
  129. package/core/business-scope-schema.d.ts +59 -59
  130. package/core/business-scope-schema.js +119 -119
  131. package/core/cache.d.ts +52 -52
  132. package/core/cache.js +129 -129
  133. package/core/chain-listener.d.ts +46 -46
  134. package/core/chain-listener.js +253 -253
  135. package/core/chain-reject.js +107 -107
  136. package/core/chain-signer.js +268 -268
  137. package/core/chain-write-log.js +94 -0
  138. package/core/cleanup.d.ts +41 -41
  139. package/core/cleanup.js +143 -143
  140. package/core/cluster.d.ts +77 -77
  141. package/core/cluster.js +214 -214
  142. package/core/community-review.d.ts +81 -81
  143. package/core/community-review.js +200 -200
  144. package/core/connection-pool.d.ts +71 -71
  145. package/core/connection-pool.js +172 -172
  146. package/core/consistency.d.ts +129 -129
  147. package/core/consistency.js +339 -339
  148. package/core/content-ai.d.ts +302 -302
  149. package/core/content-ai.js +343 -343
  150. package/core/content-backfill.d.ts +54 -54
  151. package/core/content-backfill.js +173 -173
  152. package/core/conversion-rate-sync.d.ts +96 -96
  153. package/core/conversion-rate-sync.js +194 -194
  154. package/core/coverage-backup.d.ts +86 -86
  155. package/core/coverage-backup.js +264 -264
  156. package/core/db-installer.d.ts +107 -107
  157. package/core/db-installer.js +406 -406
  158. package/core/device-analyzer.d.ts +95 -95
  159. package/core/device-analyzer.js +234 -234
  160. package/core/discovery.d.ts +164 -164
  161. package/core/discovery.js +362 -362
  162. package/core/distributed.d.ts +171 -171
  163. package/core/distributed.js +421 -421
  164. package/core/doctor.js +249 -249
  165. package/core/enforcement.d.ts +56 -56
  166. package/core/enforcement.js +114 -114
  167. package/core/error-handler.d.ts +130 -130
  168. package/core/error-handler.js +290 -290
  169. package/core/federated-learning.d.ts +123 -123
  170. package/core/federated-learning.js +337 -337
  171. package/core/fingerprint.d.ts +94 -94
  172. package/core/fingerprint.js +312 -312
  173. package/core/geoip.d.ts +66 -66
  174. package/core/geoip.js +173 -173
  175. package/core/gossip.d.ts +140 -140
  176. package/core/gossip.js +371 -371
  177. package/core/governance.d.ts +146 -146
  178. package/core/governance.js +350 -350
  179. package/core/indexer-enhancements.d.ts +163 -163
  180. package/core/indexer-enhancements.js +428 -428
  181. package/core/intrusion-detection.d.ts +78 -78
  182. package/core/intrusion-detection.js +321 -321
  183. package/core/lifecycle.d.ts +117 -117
  184. package/core/lifecycle.js +295 -295
  185. package/core/location.d.ts +98 -98
  186. package/core/location.js +218 -218
  187. package/core/maxmind-integration.d.ts +67 -67
  188. package/core/maxmind-integration.js +244 -244
  189. package/core/migration.d.ts +57 -57
  190. package/core/migration.js +219 -219
  191. package/core/monitoring.d.ts +152 -152
  192. package/core/monitoring.js +405 -405
  193. package/core/offsite-backup.d.ts +70 -70
  194. package/core/offsite-backup.js +340 -340
  195. package/core/order-intent-store.js +83 -83
  196. package/core/order-intent.js +273 -273
  197. package/core/paths.js +116 -116
  198. package/core/performance.d.ts +172 -172
  199. package/core/performance.js +385 -385
  200. package/core/product-flag.d.ts +363 -363
  201. package/core/product-flag.js +464 -464
  202. package/core/protection.d.ts +138 -138
  203. package/core/protection.js +326 -326
  204. package/core/region-adapter.d.ts +151 -151
  205. package/core/region-adapter.js +504 -504
  206. package/core/region-configs.d.ts +11 -11
  207. package/core/region-configs.js +906 -906
  208. package/core/review-antifraud.d.ts +79 -79
  209. package/core/review-antifraud.js +192 -192
  210. package/core/rules-updater.d.ts +88 -88
  211. package/core/rules-updater.js +260 -260
  212. package/core/runtime-data.js +203 -203
  213. package/core/security.d.ts +143 -143
  214. package/core/security.js +389 -389
  215. package/core/seller-identity.js +78 -78
  216. package/core/session-manager.d.ts +92 -92
  217. package/core/session-manager.js +261 -261
  218. package/core/state-sync.d.ts +120 -120
  219. package/core/state-sync.js +286 -286
  220. package/core/testing.d.ts +96 -96
  221. package/core/testing.js +360 -360
  222. package/core/trust-score.d.ts +50 -50
  223. package/core/trust-score.js +187 -187
  224. package/core/trustworthiness.d.ts +139 -139
  225. package/core/trustworthiness.js +374 -374
  226. package/core/types.d.ts +257 -257
  227. package/core/types.js +8 -8
  228. package/core/upgrade-manager.d.ts +113 -113
  229. package/core/upgrade-manager.js +263 -263
  230. package/core/upgrader.d.ts +106 -106
  231. package/core/upgrader.js +386 -386
  232. package/core/websocket.d.ts +77 -77
  233. package/core/websocket.js +291 -291
  234. package/council/hidden-weight.d.ts +44 -44
  235. package/council/hidden-weight.js +134 -134
  236. package/db/email-migrations.sql +31 -31
  237. package/db/oauth-migrate.d.ts +38 -38
  238. package/db/oauth-migrate.js +169 -169
  239. package/db/oauth-migrations.sql +92 -92
  240. package/db/social-migrate.d.ts +11 -11
  241. package/db/social-migrate.js +8 -8
  242. package/db/social-migrations.sql +93 -93
  243. package/db/store-migrate.d.ts +32 -32
  244. package/db/store-migrate.js +150 -150
  245. package/db/store-migrations.sql +241 -241
  246. package/debug.d.ts +1 -1
  247. package/debug.js +17 -17
  248. package/docs/DEPLOYMENT.md +42 -42
  249. package/event-listener.d.ts +92 -92
  250. package/event-listener.js +320 -320
  251. package/event-whitelist.d.ts +78 -78
  252. package/event-whitelist.js +532 -532
  253. package/index.d.ts +44 -44
  254. package/index.js +360 -360
  255. package/init-node.d.ts +8 -8
  256. package/init-node.js +212 -212
  257. package/mcp/ai-tools.d.ts +1 -1
  258. package/mcp/ai-tools.js +222 -222
  259. package/mcp/auto-dispatch-tools.d.ts +1 -1
  260. package/mcp/auto-dispatch-tools.js +296 -296
  261. package/mcp/business-scope-tools.d.ts +10 -10
  262. package/mcp/business-scope-tools.js +178 -178
  263. package/mcp/buyer-tools.d.ts +66 -66
  264. package/mcp/buyer-tools.js +415 -415
  265. package/mcp/interfaces.d.ts +1057 -1057
  266. package/mcp/interfaces.js +12 -12
  267. package/mcp/m2m-chat-tool.d.ts +49 -49
  268. package/mcp/m2m-chat-tool.js +411 -411
  269. package/mcp/migrations.d.ts +29 -29
  270. package/mcp/migrations.js +103 -103
  271. package/mcp/peer-tools.d.ts +162 -162
  272. package/mcp/peer-tools.js +523 -523
  273. package/mcp/plugin-management-tools.d.ts +1 -1
  274. package/mcp/plugin-management-tools.js +279 -279
  275. package/mcp/product-type-tools.d.ts +1 -1
  276. package/mcp/product-type-tools.js +412 -412
  277. package/mcp/security-tools.d.ts +1 -1
  278. package/mcp/security-tools.js +281 -281
  279. package/mcp/seller-query-tools.d.ts +1 -1
  280. package/mcp/seller-query-tools.js +162 -162
  281. package/mcp/server.d.ts +10 -10
  282. package/mcp/server.js +67 -67
  283. package/mcp/standalone-server.d.ts +33 -33
  284. package/mcp/standalone-server.js +231 -231
  285. package/mcp/tools-integration.d.ts +30 -30
  286. package/mcp/tools-integration.js +287 -287
  287. package/mcp/web-panel.d.ts +17 -17
  288. package/mcp/web-panel.js +965 -965
  289. package/middleware/api-key-auth.d.ts +63 -63
  290. package/middleware/api-key-auth.js +280 -274
  291. package/middleware/scope-auth.d.ts +53 -53
  292. package/middleware/scope-auth.js +152 -152
  293. package/middleware/security.d.ts +47 -47
  294. package/middleware/security.js +216 -216
  295. package/middleware/wallet-auth.d.ts +53 -53
  296. package/middleware/wallet-auth.js +157 -157
  297. package/oauth/chain-revocation-sink.d.ts +23 -23
  298. package/oauth/chain-revocation-sink.js +46 -46
  299. package/oauth/gossip-sync.d.ts +24 -24
  300. package/oauth/gossip-sync.js +61 -61
  301. package/oauth/issuer-keys.d.ts +30 -30
  302. package/oauth/issuer-keys.js +116 -116
  303. package/oauth/peer-resolver.d.ts +24 -24
  304. package/oauth/peer-resolver.js +80 -80
  305. package/oauth/revocation-store.d.ts +57 -57
  306. package/oauth/revocation-store.js +64 -64
  307. package/oauth/token-v2.d.ts +100 -100
  308. package/oauth/token-v2.js +166 -166
  309. package/oracle/c5-glue.js +795 -795
  310. package/oracle/ops-console.js +654 -654
  311. package/oracle/oracle-sync.d.ts +28 -28
  312. package/oracle/oracle-sync.js +444 -444
  313. package/oracle/registry.d.ts +29 -29
  314. package/oracle/registry.js +61 -61
  315. package/p2p/availability.d.ts +187 -187
  316. package/p2p/availability.js +573 -573
  317. package/p2p/bootstrap.d.ts +41 -41
  318. package/p2p/bootstrap.js +145 -145
  319. package/p2p/buyer-client.d.ts +97 -97
  320. package/p2p/buyer-client.js +198 -198
  321. package/p2p/cdn-ddns.d.ts +207 -207
  322. package/p2p/cdn-ddns.js +499 -499
  323. package/p2p/cluster-sync.d.ts +114 -114
  324. package/p2p/cluster-sync.js +329 -329
  325. package/p2p/consistency-check.d.ts +29 -29
  326. package/p2p/consistency-check.js +167 -167
  327. package/p2p/endpoint-registry.d.ts +40 -40
  328. package/p2p/endpoint-registry.js +127 -127
  329. package/p2p/ipv6.d.ts +119 -119
  330. package/p2p/ipv6.js +424 -424
  331. package/p2p/protocol.d.ts +99 -99
  332. package/p2p/protocol.js +329 -329
  333. package/p2p/reliability.d.ts +55 -55
  334. package/p2p/reliability.js +239 -239
  335. package/p2p/seller-service.d.ts +52 -52
  336. package/p2p/seller-service.js +275 -275
  337. package/p2p/signing.d.ts +30 -30
  338. package/p2p/signing.js +94 -94
  339. package/package.json +73 -73
  340. package/payment/api.d.ts +8 -8
  341. package/payment/api.js +140 -140
  342. package/payment/gateway.d.ts +113 -113
  343. package/payment/gateway.js +230 -230
  344. package/payments/alipay/index.d.ts +17 -17
  345. package/payments/alipay/index.js +54 -54
  346. package/payments/base.d.ts +12 -12
  347. package/payments/base.js +27 -27
  348. package/payments/index.d.ts +17 -17
  349. package/payments/index.js +65 -65
  350. package/payments/template/index.d.ts +17 -17
  351. package/payments/template/index.js +56 -56
  352. package/payments/types.d.ts +41 -41
  353. package/payments/types.js +2 -2
  354. package/payments/wechat/index.d.ts +17 -17
  355. package/payments/wechat/index.js +53 -53
  356. package/payments/wechat/profitsharing.js +231 -231
  357. package/peer/chain-signer.d.ts +73 -73
  358. package/peer/chain-signer.js +148 -148
  359. package/peer/config.d.ts +70 -70
  360. package/peer/config.js +60 -60
  361. package/peer/index.d.ts +11 -11
  362. package/peer/index.js +45 -45
  363. package/peer/key-store.d.ts +65 -65
  364. package/peer/key-store.js +162 -162
  365. package/peer/proto-codec.d.ts +102 -102
  366. package/peer/proto-codec.js +422 -422
  367. package/peer/registrar.d.ts +87 -87
  368. package/peer/registrar.js +270 -270
  369. package/peer/registry.d.ts +30 -30
  370. package/peer/registry.js +142 -142
  371. package/plugins/auth.d.ts +192 -192
  372. package/plugins/auth.js +818 -818
  373. package/plugins/manager.d.ts +36 -36
  374. package/plugins/manager.js +82 -82
  375. package/plugins/oauth.d.ts +248 -248
  376. package/plugins/oauth.js +806 -806
  377. package/plugins/p2p-message.d.ts +132 -132
  378. package/plugins/p2p-message.js +349 -349
  379. package/plugins/postgresql.d.ts +53 -53
  380. package/plugins/postgresql.js +596 -596
  381. package/plugins/relationship.d.ts +140 -140
  382. package/plugins/relationship.js +290 -290
  383. package/plugins/sqlite.d.ts +199 -199
  384. package/plugins/sqlite.js +1532 -1532
  385. package/port-manager/disguise/ssh.d.ts +18 -18
  386. package/port-manager/disguise/ssh.js +103 -103
  387. package/port-manager/port-manager.d.ts +64 -64
  388. package/port-manager/port-manager.js +163 -163
  389. package/port-manager/port-scanner.d.ts +21 -21
  390. package/port-manager/port-scanner.js +186 -186
  391. package/port-manager/types.d.ts +50 -50
  392. package/port-manager/types.js +5 -5
  393. package/proto/amino/amino.proto +83 -83
  394. package/proto/cosmos/base/v1beta1/coin.proto +61 -61
  395. package/proto/cosmos/msg/v1/msg.proto +30 -30
  396. package/proto/cosmos_proto/cosmos.proto +111 -111
  397. package/proto/gogoproto/gogo.proto +145 -145
  398. package/proto/google/protobuf/descriptor.proto +1218 -1218
  399. package/proto/trustchain/marketplace/v1/product.proto +20 -20
  400. package/proto/trustchain/order/v1/tx.proto +156 -156
  401. package/proto/trustchain/reward/v1/tx.proto +62 -62
  402. package/proto/trustchain/serviceprovider/v1/paymentfact.proto +32 -32
  403. package/proto/trustchain/serviceprovider/v1/serviceprovider.proto +38 -38
  404. package/proto/trustchain/serviceprovider/v1/tx.proto +106 -106
  405. package/proto/trustchain/subchain/v1/params.proto +26 -26
  406. package/proto/trustchain/subchain/v1/tx.proto +33 -33
  407. package/proto/trustchain/verification/v1/params.proto +46 -46
  408. package/proto/trustchain/verification/v1/tx.proto +126 -126
  409. package/proto/trustchain/verification/v1/verification.proto +105 -105
  410. package/relationship-chain-api.d.ts +12 -12
  411. package/relationship-chain-api.js +138 -138
  412. package/relationship-chain.d.ts +89 -89
  413. package/relationship-chain.js +200 -200
  414. package/routes/agent-pay.js +13 -13
  415. package/routes/agent-serviceprovider.js +183 -183
  416. package/routes/ai.d.ts +53 -53
  417. package/routes/ai.js +289 -289
  418. package/routes/api-keys.d.ts +11 -11
  419. package/routes/api-keys.js +162 -162
  420. package/routes/app-store.d.ts +11 -11
  421. package/routes/app-store.js +206 -206
  422. package/routes/auth-management.d.ts +16 -16
  423. package/routes/auth-management.js +256 -256
  424. package/routes/auth.d.ts +8 -8
  425. package/routes/auth.js +845 -845
  426. package/routes/brand.d.ts +2 -2
  427. package/routes/brand.js +108 -108
  428. package/routes/buyer-shop.js +470 -470
  429. package/routes/chain-registry.d.ts +10 -10
  430. package/routes/chain-registry.js +50 -50
  431. package/routes/chain-writes-insurance.d.ts +16 -16
  432. package/routes/chain-writes-insurance.js +58 -58
  433. package/routes/chain-writes-pledge.d.ts +33 -33
  434. package/routes/chain-writes-pledge.js +204 -204
  435. package/routes/chain-writes-products.d.ts +65 -65
  436. package/routes/chain-writes-products.js +438 -392
  437. package/routes/chain-writes.d.ts +77 -77
  438. package/routes/chain-writes.js +669 -661
  439. package/routes/chain.d.ts +9 -9
  440. package/routes/chain.js +44 -44
  441. package/routes/compliance.d.ts +2 -2
  442. package/routes/compliance.js +52 -52
  443. package/routes/council.d.ts +2 -2
  444. package/routes/council.js +476 -476
  445. package/routes/crosschain.d.ts +10 -10
  446. package/routes/crosschain.js +38 -38
  447. package/routes/device.d.ts +2 -2
  448. package/routes/device.js +72 -72
  449. package/routes/devreward.d.ts +53 -53
  450. package/routes/devreward.js +111 -111
  451. package/routes/economy.d.ts +49 -49
  452. package/routes/economy.js +269 -269
  453. package/routes/flowback.d.ts +31 -31
  454. package/routes/flowback.js +81 -81
  455. package/routes/insurance.d.ts +9 -9
  456. package/routes/insurance.js +69 -69
  457. package/routes/m2m.d.ts +9 -9
  458. package/routes/m2m.js +29 -29
  459. package/routes/marketplace.d.ts +14 -14
  460. package/routes/marketplace.js +395 -395
  461. package/routes/node-config.d.ts +5 -5
  462. package/routes/node-config.js +499 -499
  463. package/routes/oauth-revocation.d.ts +41 -41
  464. package/routes/oauth-revocation.js +98 -98
  465. package/routes/oauth.d.ts +16 -16
  466. package/routes/oauth.js +699 -699
  467. package/routes/onboard.html +1483 -1483
  468. package/routes/onboard.js +944 -944
  469. package/routes/order.d.ts +7 -7
  470. package/routes/order.js +591 -591
  471. package/routes/p2p-api.d.ts +32 -32
  472. package/routes/p2p-api.js +268 -268
  473. package/routes/p2p-message.d.ts +19 -19
  474. package/routes/p2p-message.js +236 -236
  475. package/routes/peer.d.ts +21 -21
  476. package/routes/peer.js +210 -210
  477. package/routes/plugins.d.ts +3 -3
  478. package/routes/plugins.js +137 -137
  479. package/routes/port-manager.d.ts +5 -5
  480. package/routes/port-manager.js +211 -211
  481. package/routes/relationship.d.ts +6 -6
  482. package/routes/relationship.js +313 -313
  483. package/routes/relay.d.ts +2 -2
  484. package/routes/relay.js +181 -181
  485. package/routes/reputation.d.ts +9 -9
  486. package/routes/reputation.js +140 -140
  487. package/routes/reward.d.ts +2 -2
  488. package/routes/reward.js +129 -129
  489. package/routes/security-monitoring.d.ts +11 -11
  490. package/routes/security-monitoring.js +372 -372
  491. package/routes/shop.d.ts +7 -7
  492. package/routes/shop.js +163 -163
  493. package/routes/social.d.ts +21 -21
  494. package/routes/social.js +94 -94
  495. package/routes/sp-admin.js +32 -32
  496. package/routes/sp-applyment.js +285 -285
  497. package/routes/sp-commission.js +119 -119
  498. package/routes/sp-notify.js +424 -424
  499. package/routes/sp-tunnel.js +354 -354
  500. package/routes/subchain.d.ts +17 -17
  501. package/routes/subchain.js +105 -105
  502. package/routes/trustcoin.d.ts +16 -16
  503. package/routes/trustcoin.js +148 -148
  504. package/routes/twofa.d.ts +6 -6
  505. package/routes/twofa.js +104 -104
  506. package/routes/verification.d.ts +7 -7
  507. package/routes/verification.js +233 -233
  508. package/routes/wallet.d.ts +3 -3
  509. package/routes/wallet.js +763 -763
  510. package/run-auth.d.ts +7 -7
  511. package/run-auth.js +81 -81
  512. package/run-p2p-message.d.ts +7 -7
  513. package/run-p2p-message.js +81 -81
  514. package/run-relationship.d.ts +11 -11
  515. package/run-relationship.js +181 -181
  516. package/run-unified.d.ts +12 -12
  517. package/run-unified.js +3 -0
  518. package/run.d.ts +11 -11
  519. package/run.js +888 -888
  520. package/search/ranking.d.ts +54 -54
  521. package/search/ranking.js +66 -66
  522. package/shipping/base.d.ts +14 -14
  523. package/shipping/base.js +38 -38
  524. package/shipping/index.d.ts +18 -18
  525. package/shipping/index.js +71 -71
  526. package/shipping/sf-express/index.d.ts +22 -22
  527. package/shipping/sf-express/index.js +70 -70
  528. package/shipping/template/index.d.ts +19 -19
  529. package/shipping/template/index.js +73 -73
  530. package/shipping/types.d.ts +70 -70
  531. package/shipping/types.js +5 -5
  532. package/shipping/virtual/index.d.ts +18 -18
  533. package/shipping/virtual/index.js +53 -53
  534. package/shipping/yto/index.d.ts +17 -17
  535. package/shipping/yto/index.js +58 -58
  536. package/shipping/zto/index.d.ts +17 -17
  537. package/shipping/zto/index.js +58 -58
  538. package/skills-lock.json +11 -11
  539. package/src/db/email-migrations.sql +31 -31
  540. package/src/db/oauth-migrations.sql +92 -92
  541. package/src/db/social-migrations.sql +93 -93
  542. package/src/db/store-migrations.sql +241 -241
  543. package/test-agent-pay-lifecycle.js +186 -186
  544. package/test-agent-pay-wechat.js +291 -291
  545. package/test-api-key-auth-exempt.js +121 -0
  546. package/test-api-key-revoke.js +167 -167
  547. package/test-auth-register-ratelimit.js +132 -132
  548. package/test-auth-tv2.d.ts +1 -1
  549. package/test-auth-tv2.js +173 -173
  550. package/test-buyer-shop-ps.js +238 -238
  551. package/test-buyer-shop.js +334 -334
  552. package/test-canonsign-cross.d.ts +1 -1
  553. package/test-canonsign-cross.js +31 -31
  554. package/test-chain-signer.js +188 -188
  555. package/test-chain-write-log.js +76 -0
  556. package/test-chain-writes-audit-integration.js +36 -0
  557. package/test-env-truthy.js +50 -50
  558. package/test-laneC.js +85 -85
  559. package/test-onboard-applyment.js +823 -823
  560. package/test-onboard-backup.js +208 -208
  561. package/test-onboard-contract.js +94 -94
  562. package/test-ops-console-boundary.js +147 -147
  563. package/test-oracle-glue.js +354 -354
  564. package/test-oracle-pool-refill.js +171 -171
  565. package/test-order-create-v394.js +358 -358
  566. package/test-order-intent.js +245 -245
  567. package/test-order-lifecycle-v394.js +528 -528
  568. package/test-p2p-message.d.ts +12 -12
  569. package/test-p2p-message.js +105 -105
  570. package/test-product-type-chain.js +73 -0
  571. package/test-product-type-write.js +211 -0
  572. package/test-product-type.js +173 -173
  573. package/test-relationship-chain.d.ts +12 -12
  574. package/test-relationship-chain.js +102 -102
  575. package/test-runtime-data-path.js +217 -217
  576. package/test-seller-identity.js +74 -74
  577. package/test-signing-paths.js +104 -104
  578. package/test-sp-admin.js +289 -289
  579. package/test-sp-applyment.js +583 -583
  580. package/test-sp-gateway.js +435 -435
  581. package/test-sp-tunnel.js +311 -311
  582. package/test-token-v2.d.ts +1 -1
  583. package/test-token-v2.js +170 -170
  584. package/test-wallet-auth-nonce.d.ts +11 -11
  585. package/test-wallet-auth-nonce.js +125 -125
  586. package/test-wallet-submatch.d.ts +12 -12
  587. package/test-wallet-submatch.js +85 -85
  588. package/tls/self-signed.d.ts +11 -11
  589. package/tls/self-signed.js +62 -62
  590. package/tools/brand.d.ts +10 -10
  591. package/tools/brand.js +74 -74
  592. package/tools/chainregistry.d.ts +17 -17
  593. package/tools/chainregistry.js +155 -155
  594. package/tools/compliance.d.ts +13 -13
  595. package/tools/compliance.js +44 -44
  596. package/tools/council.d.ts +16 -16
  597. package/tools/council.js +74 -74
  598. package/tools/device.d.ts +12 -12
  599. package/tools/device.js +44 -44
  600. package/tools/devreward.d.ts +14 -14
  601. package/tools/devreward.js +175 -175
  602. package/tools/economy.d.ts +13 -13
  603. package/tools/economy.js +168 -168
  604. package/tools/flowback.d.ts +13 -13
  605. package/tools/flowback.js +132 -132
  606. package/tools/insurance.d.ts +12 -12
  607. package/tools/insurance.js +44 -44
  608. package/tools/marketplace.d.ts +11 -11
  609. package/tools/marketplace.js +112 -112
  610. package/tools/mock-tunnel-merchant.js +25 -25
  611. package/tools/order.d.ts +11 -11
  612. package/tools/order.js +87 -87
  613. package/tools/recovery.d.ts +13 -13
  614. package/tools/recovery.js +151 -151
  615. package/tools/relay.d.ts +14 -14
  616. package/tools/relay.js +129 -129
  617. package/tools/reputation.d.ts +19 -19
  618. package/tools/reputation.js +49 -49
  619. package/tools/reputation_seller_credit.d.ts +15 -15
  620. package/tools/reputation_seller_credit.js +99 -99
  621. package/tools/reward.d.ts +13 -13
  622. package/tools/reward.js +70 -70
  623. package/tools/shop.d.ts +13 -13
  624. package/tools/shop.js +82 -82
  625. package/tools/tunnel-e2e-check.sh +20 -20
  626. package/tools/verification.d.ts +11 -11
  627. package/tools/verification.js +83 -83
  628. package/types/store.d.ts +254 -254
  629. package/types/store.js +18 -18
  630. package/utils/aes-gcm.d.ts +62 -62
  631. package/utils/aes-gcm.js +178 -178
  632. package/utils/api-response.d.ts +50 -50
  633. package/utils/api-response.js +64 -64
  634. package/utils/base32.d.ts +4 -4
  635. package/utils/base32.js +19 -19
  636. package/utils/crypto.d.ts +76 -76
  637. package/utils/crypto.js +163 -163
  638. package/utils/encryption.d.ts +80 -80
  639. package/utils/encryption.js +246 -246
  640. package/utils/env.js +20 -20
  641. package/utils/jwt.d.ts +109 -109
  642. package/utils/jwt.js +274 -274
  643. package/utils/qrcode-reader.d.ts +21 -21
  644. package/utils/qrcode.d.ts +25 -25
  645. package/utils/qrcode.js +91 -91
  646. package/utils/totp.d.ts +2 -2
  647. package/utils/totp.js +57 -57
  648. package/utils/two-factor-auth.d.ts +15 -15
  649. package/utils/two-factor-auth.js +149 -149
  650. package/verifyer/hsm.d.ts +69 -69
  651. package/verifyer/hsm.js +481 -481
  652. package/verifyer/index.d.ts +80 -80
  653. package/verifyer/index.js +371 -371
  654. package/ws/peer-monitor-broadcaster.d.ts +23 -23
  655. package/ws/peer-monitor-broadcaster.js +55 -55
@@ -1,54 +1,54 @@
1
- /**
2
- * Scope Auth Middleware — 数据租客原则的核心执行层 (PRD-NEW §27.6/§27.7)
3
- *
4
- * 用户是房东,应用是租客。requireScope 是「检查租客有没有这把钥匙」的门卫。
5
- *
6
- * Token 优先级:
7
- * 1. 第三方 OAuth token (OAuthPlugin.validateAccessToken) → 严格 scope 强制
8
- * 2. 第一方登录 token (AuthPlugin.verifyToken) → 隐式全权限(用户自己的 session)
9
- *
10
- * 第一方 = 隐式全权限:用户自己登录操作自己的数据,不受 scope 限制。
11
- * 第三方 = 严格 scope:App 只能访问用户授权的 scope(逐项 toggle 的子集)。
12
- */
13
- import { Request, Response, NextFunction } from 'express';
14
- import type { OAuthPlugin } from '../plugins/oauth';
15
- import type { AuthPlugin } from '../plugins/auth';
16
- import type { IssuerKeyCache } from '../oauth/issuer-keys';
17
- import type { RevocationStore } from '../oauth/revocation-store';
18
- export interface OAuthUser {
19
- userId: string;
20
- address: string;
21
- scopes: string[];
22
- isOAuth: boolean;
23
- clientId?: string;
24
- tokenV2?: boolean;
25
- }
26
- /** R2-1: tv2 离线验证依赖(注入即启用;不注入则 tv2 token 一律 401) */
27
- export interface ScopeAuthV2Deps {
28
- issuerKeys: IssuerKeyCache;
29
- revocations: RevocationStore;
30
- }
31
- declare module 'express-serve-static-core' {
32
- interface Request {
33
- oauthUser?: OAuthUser;
34
- walletAddress?: string;
35
- }
36
- }
37
- /**
38
- * 写操作钱包挑战(R2-1 设计 §4 + R2-2 预研方案 B):写/高危操作必须叠加 walletAuth,
39
- * 且签名者 === token 当事人(tv2=sub;第一方=用户地址)。token 丢了没有用户私钥
40
- * 照样用不了写操作。
41
- * 用法:router.post('/write-op', walletAuth(), requireScope('x:write'), requireWalletSubMatch(), handler)
42
- * (walletAuth 在前,requireScope 在后。)
43
- *
44
- * 管辖范围(R2-2 方案 B,预研实现,待 kimi 拍板):
45
- * - tv2 token(tokenV2=true)→ 强制 walletAuth + signer===sub(R2-1 已有)
46
- * - 第一方登录 token(isOAuth=false)→ 强制 walletAuth + signer===address
47
- * 收口"隐式全权限":第三方 App 即使拿到第一方 token 也做不了写操作。
48
- * 不做 scope 检查——第一方默认 scopes 只有 profile/email,加检查会破坏
49
- * 所有依赖第一方全权限的现有端点(orders/marketplace 等)。
50
- * - v1 第三方 OAuth token(isOAuth=true 非 tv2)→ 保持豁免(本就有严格 scope 制)
51
- */
52
- export declare function requireWalletSubMatch(): (req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>;
53
- export declare function createScopeAuth(oauthPlugin: OAuthPlugin, authPlugin: AuthPlugin, v2Deps?: ScopeAuthV2Deps): (scope: string) => (req: Request, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
1
+ /**
2
+ * Scope Auth Middleware — 数据租客原则的核心执行层 (PRD-NEW §27.6/§27.7)
3
+ *
4
+ * 用户是房东,应用是租客。requireScope 是「检查租客有没有这把钥匙」的门卫。
5
+ *
6
+ * Token 优先级:
7
+ * 1. 第三方 OAuth token (OAuthPlugin.validateAccessToken) → 严格 scope 强制
8
+ * 2. 第一方登录 token (AuthPlugin.verifyToken) → 隐式全权限(用户自己的 session)
9
+ *
10
+ * 第一方 = 隐式全权限:用户自己登录操作自己的数据,不受 scope 限制。
11
+ * 第三方 = 严格 scope:App 只能访问用户授权的 scope(逐项 toggle 的子集)。
12
+ */
13
+ import { Request, Response, NextFunction } from 'express';
14
+ import type { OAuthPlugin } from '../plugins/oauth';
15
+ import type { AuthPlugin } from '../plugins/auth';
16
+ import type { IssuerKeyCache } from '../oauth/issuer-keys';
17
+ import type { RevocationStore } from '../oauth/revocation-store';
18
+ export interface OAuthUser {
19
+ userId: string;
20
+ address: string;
21
+ scopes: string[];
22
+ isOAuth: boolean;
23
+ clientId?: string;
24
+ tokenV2?: boolean;
25
+ }
26
+ /** R2-1: tv2 离线验证依赖(注入即启用;不注入则 tv2 token 一律 401) */
27
+ export interface ScopeAuthV2Deps {
28
+ issuerKeys: IssuerKeyCache;
29
+ revocations: RevocationStore;
30
+ }
31
+ declare module 'express-serve-static-core' {
32
+ interface Request {
33
+ oauthUser?: OAuthUser;
34
+ walletAddress?: string;
35
+ }
36
+ }
37
+ /**
38
+ * 写操作钱包挑战(R2-1 设计 §4 + R2-2 预研方案 B):写/高危操作必须叠加 walletAuth,
39
+ * 且签名者 === token 当事人(tv2=sub;第一方=用户地址)。token 丢了没有用户私钥
40
+ * 照样用不了写操作。
41
+ * 用法:router.post('/write-op', walletAuth(), requireScope('x:write'), requireWalletSubMatch(), handler)
42
+ * (walletAuth 在前,requireScope 在后。)
43
+ *
44
+ * 管辖范围(R2-2 方案 B,预研实现,待 kimi 拍板):
45
+ * - tv2 token(tokenV2=true)→ 强制 walletAuth + signer===sub(R2-1 已有)
46
+ * - 第一方登录 token(isOAuth=false)→ 强制 walletAuth + signer===address
47
+ * 收口"隐式全权限":第三方 App 即使拿到第一方 token 也做不了写操作。
48
+ * 不做 scope 检查——第一方默认 scopes 只有 profile/email,加检查会破坏
49
+ * 所有依赖第一方全权限的现有端点(orders/marketplace 等)。
50
+ * - v1 第三方 OAuth token(isOAuth=true 非 tv2)→ 保持豁免(本就有严格 scope 制)
51
+ */
52
+ export declare function requireWalletSubMatch(): (req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>;
53
+ export declare function createScopeAuth(oauthPlugin: OAuthPlugin, authPlugin: AuthPlugin, v2Deps?: ScopeAuthV2Deps): (scope: string) => (req: Request, res: Response, next: NextFunction) => Promise<void | Response<any, Record<string, any>>>;
54
54
  //# sourceMappingURL=scope-auth.d.ts.map
@@ -1,153 +1,153 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requireWalletSubMatch = requireWalletSubMatch;
4
- exports.createScopeAuth = createScopeAuth;
5
- const token_v2_1 = require("../oauth/token-v2");
6
- /**
7
- * 写操作钱包挑战(R2-1 设计 §4 + R2-2 预研方案 B):写/高危操作必须叠加 walletAuth,
8
- * 且签名者 === token 当事人(tv2=sub;第一方=用户地址)。token 丢了没有用户私钥
9
- * 照样用不了写操作。
10
- * 用法:router.post('/write-op', walletAuth(), requireScope('x:write'), requireWalletSubMatch(), handler)
11
- * (walletAuth 在前,requireScope 在后。)
12
- *
13
- * 管辖范围(R2-2 方案 B,预研实现,待 kimi 拍板):
14
- * - tv2 token(tokenV2=true)→ 强制 walletAuth + signer===sub(R2-1 已有)
15
- * - 第一方登录 token(isOAuth=false)→ 强制 walletAuth + signer===address
16
- * 收口"隐式全权限":第三方 App 即使拿到第一方 token 也做不了写操作。
17
- * 不做 scope 检查——第一方默认 scopes 只有 profile/email,加检查会破坏
18
- * 所有依赖第一方全权限的现有端点(orders/marketplace 等)。
19
- * - v1 第三方 OAuth token(isOAuth=true 非 tv2)→ 保持豁免(本就有严格 scope 制)
20
- */
21
- function requireWalletSubMatch() {
22
- return (req, res, next) => {
23
- const ou = req.oauthUser;
24
- if (!ou)
25
- return next();
26
- // 豁免: v1 第三方 token (严格 scope 制已管), 不在本中间件管辖
27
- if (ou.isOAuth && !ou.tokenV2)
28
- return next();
29
- if (!req.walletAddress) {
30
- return res.status(401).json({
31
- error: 'wallet-challenge-required',
32
- message: ou.tokenV2 ? 'write op with tv2 token requires walletAuth headers' : 'write op with first-party token requires walletAuth headers',
33
- });
34
- }
35
- if (req.walletAddress !== ou.address) {
36
- return res.status(403).json({ error: 'wallet-sub-mismatch', message: 'signer must equal token subject' });
37
- }
38
- next();
39
- };
40
- }
41
- function createScopeAuth(oauthPlugin, authPlugin, v2Deps) {
42
- /**
43
- * requireScope(scope) — 路由级中间件,校验 token 是否持有指定 scope。
44
- *
45
- * 第一方 token 隐式通过(用户自己 = 全权限)。
46
- * 第三方 token 必须 scopes.includes(scope),否则 403 insufficient_scope。
47
- */
48
- return function requireScope(scope) {
49
- return async (req, res, next) => {
50
- const authHeader = req.headers.authorization;
51
- if (!authHeader || !authHeader.startsWith('Bearer ')) {
52
- return res.status(401).json({ error: 'missing_token', message: 'Authorization Bearer token required' });
53
- }
54
- const token = authHeader.substring(7);
55
- // 0. tv2 结构化令牌 → 跨索引器离线验证(R2-1)
56
- // 不查本地用户表(B 节点没有该用户行),sub 即身份。
57
- if (token.startsWith(token_v2_1.TOKEN_V2_PREFIX)) {
58
- if (!v2Deps) {
59
- return res.status(401).json({ error: 'token-v2-not-enabled' });
60
- }
61
- let issuerPubkey;
62
- const parsed = (0, token_v2_1.parseTokenV2)(token);
63
- if (!parsed) {
64
- return res.status(401).json({ error: 'invalid_token', message: 'malformed tv2 token' });
65
- }
66
- try {
67
- issuerPubkey = await v2Deps.issuerKeys.get(parsed.payload.iss);
68
- }
69
- catch {
70
- return res.status(503).json({ error: 'issuer-key-unavailable', message: 'chain query failed and no cached key' });
71
- }
72
- if (!issuerPubkey) {
73
- return res.status(401).json({ error: 'unknown-issuer' });
74
- }
75
- const verdict = await (0, token_v2_1.verifyTokenV2Offline)(token, issuerPubkey, v2Deps.revocations);
76
- if (!verdict.ok) {
77
- return res.status(401).json({ error: 'invalid_token', reason: verdict.reason });
78
- }
79
- const p = verdict.payload;
80
- if (!p.scopes.includes(scope)) {
81
- return res.status(403).json({ error: 'insufficient_scope', required: scope, granted: p.scopes });
82
- }
83
- req.oauthUser = {
84
- userId: p.sub,
85
- address: p.sub,
86
- scopes: p.scopes,
87
- isOAuth: true,
88
- clientId: p.client_id,
89
- tokenV2: true,
90
- };
91
- res.on('finish', () => {
92
- oauthPlugin.logAccess({
93
- appName: '', clientId: p.client_id,
94
- userId: p.sub, userAddress: p.sub,
95
- endpoint: req.path, method: req.method,
96
- scopeUsed: scope, responseStatus: res.statusCode,
97
- }).catch(() => { });
98
- });
99
- return next();
100
- }
101
- // 1. 先试第三方 OAuth token
102
- const oauthInfo = await oauthPlugin.validateAccessToken(token);
103
- if (oauthInfo) {
104
- // 第三方 → 严格 scope 强制
105
- if (!oauthInfo.scopes.includes(scope)) {
106
- return res.status(403).json({ error: 'insufficient_scope', required: scope, granted: oauthInfo.scopes });
107
- }
108
- // 解析 address
109
- const user = await authPlugin.getUser(oauthInfo.userId);
110
- if (!user) {
111
- return res.status(401).json({ error: 'invalid_token', message: 'User not found' });
112
- }
113
- req.oauthUser = {
114
- userId: oauthInfo.userId,
115
- address: user.address,
116
- scopes: oauthInfo.scopes,
117
- isOAuth: true,
118
- clientId: oauthInfo.clientId,
119
- };
120
- // 访问日志(PRD §27.9):仅第三方 OAuth token 记录,第一方不记
121
- const appName = ''; // app_name 需要 JOIN 查,这里只记 endpoint/scope
122
- res.on('finish', () => {
123
- oauthPlugin.logAccess({
124
- appName, clientId: oauthInfo.clientId,
125
- userId: oauthInfo.userId, userAddress: user.address,
126
- endpoint: req.path, method: req.method,
127
- scopeUsed: scope, responseStatus: res.statusCode,
128
- }).catch(() => { });
129
- });
130
- return next();
131
- }
132
- // 2. 后试第一方登录 token
133
- const sessionInfo = await authPlugin.verifyToken(token);
134
- if (sessionInfo) {
135
- // 第一方 → 隐式全权限(用户自己的 session)
136
- const user = await authPlugin.getUser(sessionInfo.userId);
137
- if (!user) {
138
- return res.status(401).json({ error: 'invalid_token', message: 'User not found' });
139
- }
140
- req.oauthUser = {
141
- userId: sessionInfo.userId,
142
- address: user.address,
143
- scopes: sessionInfo.scopes,
144
- isOAuth: false,
145
- };
146
- return next();
147
- }
148
- // 3. 都不匹配 → 无效 token
149
- return res.status(401).json({ error: 'invalid_token', message: 'Token not recognized or expired' });
150
- };
151
- };
152
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireWalletSubMatch = requireWalletSubMatch;
4
+ exports.createScopeAuth = createScopeAuth;
5
+ const token_v2_1 = require("../oauth/token-v2");
6
+ /**
7
+ * 写操作钱包挑战(R2-1 设计 §4 + R2-2 预研方案 B):写/高危操作必须叠加 walletAuth,
8
+ * 且签名者 === token 当事人(tv2=sub;第一方=用户地址)。token 丢了没有用户私钥
9
+ * 照样用不了写操作。
10
+ * 用法:router.post('/write-op', walletAuth(), requireScope('x:write'), requireWalletSubMatch(), handler)
11
+ * (walletAuth 在前,requireScope 在后。)
12
+ *
13
+ * 管辖范围(R2-2 方案 B,预研实现,待 kimi 拍板):
14
+ * - tv2 token(tokenV2=true)→ 强制 walletAuth + signer===sub(R2-1 已有)
15
+ * - 第一方登录 token(isOAuth=false)→ 强制 walletAuth + signer===address
16
+ * 收口"隐式全权限":第三方 App 即使拿到第一方 token 也做不了写操作。
17
+ * 不做 scope 检查——第一方默认 scopes 只有 profile/email,加检查会破坏
18
+ * 所有依赖第一方全权限的现有端点(orders/marketplace 等)。
19
+ * - v1 第三方 OAuth token(isOAuth=true 非 tv2)→ 保持豁免(本就有严格 scope 制)
20
+ */
21
+ function requireWalletSubMatch() {
22
+ return (req, res, next) => {
23
+ const ou = req.oauthUser;
24
+ if (!ou)
25
+ return next();
26
+ // 豁免: v1 第三方 token (严格 scope 制已管), 不在本中间件管辖
27
+ if (ou.isOAuth && !ou.tokenV2)
28
+ return next();
29
+ if (!req.walletAddress) {
30
+ return res.status(401).json({
31
+ error: 'wallet-challenge-required',
32
+ message: ou.tokenV2 ? 'write op with tv2 token requires walletAuth headers' : 'write op with first-party token requires walletAuth headers',
33
+ });
34
+ }
35
+ if (req.walletAddress !== ou.address) {
36
+ return res.status(403).json({ error: 'wallet-sub-mismatch', message: 'signer must equal token subject' });
37
+ }
38
+ next();
39
+ };
40
+ }
41
+ function createScopeAuth(oauthPlugin, authPlugin, v2Deps) {
42
+ /**
43
+ * requireScope(scope) — 路由级中间件,校验 token 是否持有指定 scope。
44
+ *
45
+ * 第一方 token 隐式通过(用户自己 = 全权限)。
46
+ * 第三方 token 必须 scopes.includes(scope),否则 403 insufficient_scope。
47
+ */
48
+ return function requireScope(scope) {
49
+ return async (req, res, next) => {
50
+ const authHeader = req.headers.authorization;
51
+ if (!authHeader || !authHeader.startsWith('Bearer ')) {
52
+ return res.status(401).json({ error: 'missing_token', message: 'Authorization Bearer token required' });
53
+ }
54
+ const token = authHeader.substring(7);
55
+ // 0. tv2 结构化令牌 → 跨索引器离线验证(R2-1)
56
+ // 不查本地用户表(B 节点没有该用户行),sub 即身份。
57
+ if (token.startsWith(token_v2_1.TOKEN_V2_PREFIX)) {
58
+ if (!v2Deps) {
59
+ return res.status(401).json({ error: 'token-v2-not-enabled' });
60
+ }
61
+ let issuerPubkey;
62
+ const parsed = (0, token_v2_1.parseTokenV2)(token);
63
+ if (!parsed) {
64
+ return res.status(401).json({ error: 'invalid_token', message: 'malformed tv2 token' });
65
+ }
66
+ try {
67
+ issuerPubkey = await v2Deps.issuerKeys.get(parsed.payload.iss);
68
+ }
69
+ catch {
70
+ return res.status(503).json({ error: 'issuer-key-unavailable', message: 'chain query failed and no cached key' });
71
+ }
72
+ if (!issuerPubkey) {
73
+ return res.status(401).json({ error: 'unknown-issuer' });
74
+ }
75
+ const verdict = await (0, token_v2_1.verifyTokenV2Offline)(token, issuerPubkey, v2Deps.revocations);
76
+ if (!verdict.ok) {
77
+ return res.status(401).json({ error: 'invalid_token', reason: verdict.reason });
78
+ }
79
+ const p = verdict.payload;
80
+ if (!p.scopes.includes(scope)) {
81
+ return res.status(403).json({ error: 'insufficient_scope', required: scope, granted: p.scopes });
82
+ }
83
+ req.oauthUser = {
84
+ userId: p.sub,
85
+ address: p.sub,
86
+ scopes: p.scopes,
87
+ isOAuth: true,
88
+ clientId: p.client_id,
89
+ tokenV2: true,
90
+ };
91
+ res.on('finish', () => {
92
+ oauthPlugin.logAccess({
93
+ appName: '', clientId: p.client_id,
94
+ userId: p.sub, userAddress: p.sub,
95
+ endpoint: req.path, method: req.method,
96
+ scopeUsed: scope, responseStatus: res.statusCode,
97
+ }).catch(() => { });
98
+ });
99
+ return next();
100
+ }
101
+ // 1. 先试第三方 OAuth token
102
+ const oauthInfo = await oauthPlugin.validateAccessToken(token);
103
+ if (oauthInfo) {
104
+ // 第三方 → 严格 scope 强制
105
+ if (!oauthInfo.scopes.includes(scope)) {
106
+ return res.status(403).json({ error: 'insufficient_scope', required: scope, granted: oauthInfo.scopes });
107
+ }
108
+ // 解析 address
109
+ const user = await authPlugin.getUser(oauthInfo.userId);
110
+ if (!user) {
111
+ return res.status(401).json({ error: 'invalid_token', message: 'User not found' });
112
+ }
113
+ req.oauthUser = {
114
+ userId: oauthInfo.userId,
115
+ address: user.address,
116
+ scopes: oauthInfo.scopes,
117
+ isOAuth: true,
118
+ clientId: oauthInfo.clientId,
119
+ };
120
+ // 访问日志(PRD §27.9):仅第三方 OAuth token 记录,第一方不记
121
+ const appName = ''; // app_name 需要 JOIN 查,这里只记 endpoint/scope
122
+ res.on('finish', () => {
123
+ oauthPlugin.logAccess({
124
+ appName, clientId: oauthInfo.clientId,
125
+ userId: oauthInfo.userId, userAddress: user.address,
126
+ endpoint: req.path, method: req.method,
127
+ scopeUsed: scope, responseStatus: res.statusCode,
128
+ }).catch(() => { });
129
+ });
130
+ return next();
131
+ }
132
+ // 2. 后试第一方登录 token
133
+ const sessionInfo = await authPlugin.verifyToken(token);
134
+ if (sessionInfo) {
135
+ // 第一方 → 隐式全权限(用户自己的 session)
136
+ const user = await authPlugin.getUser(sessionInfo.userId);
137
+ if (!user) {
138
+ return res.status(401).json({ error: 'invalid_token', message: 'User not found' });
139
+ }
140
+ req.oauthUser = {
141
+ userId: sessionInfo.userId,
142
+ address: user.address,
143
+ scopes: sessionInfo.scopes,
144
+ isOAuth: false,
145
+ };
146
+ return next();
147
+ }
148
+ // 3. 都不匹配 → 无效 token
149
+ return res.status(401).json({ error: 'invalid_token', message: 'Token not recognized or expired' });
150
+ };
151
+ };
152
+ }
153
153
  //# sourceMappingURL=scope-auth.js.map
@@ -1,48 +1,48 @@
1
- /**
2
- * Security Middleware - 安全中间件
3
- *
4
- * 功能:
5
- * 1. IP 白名单验证
6
- * 2. CORS 限制
7
- * 3. 请求日志审计
8
- * 4. 安全头部
9
- */
10
- import { Request, Response, NextFunction } from 'express';
11
- /**
12
- * IP 白名单中间件
13
- */
14
- export declare function ipWhitelist(allowedIPs: string[]): (req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>;
15
- /**
16
- * 动态 CORS 配置
17
- */
18
- export declare function createCorsOptions(allowedOrigins: string[]): {
19
- origin: (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => void;
20
- credentials: boolean;
21
- methods: string[];
22
- allowedHeaders: string[];
23
- };
24
- /**
25
- * 审计日志中间件
26
- */
27
- export declare function auditLog(): (req: Request, res: Response, next: NextFunction) => void;
28
- /**
29
- * 安全头部中间件
30
- */
31
- export declare function securityHeaders(): (req: Request, res: Response, next: NextFunction) => void;
32
- /**
33
- * 请求体大小限制
34
- */
35
- export declare const requestSizeLimit: {
36
- json: {
37
- limit: string;
38
- };
39
- urlencoded: {
40
- limit: string;
41
- extended: boolean;
42
- };
43
- };
44
- /**
45
- * 验证必需的环境变量
46
- */
47
- export declare function validateEnv(): void;
1
+ /**
2
+ * Security Middleware - 安全中间件
3
+ *
4
+ * 功能:
5
+ * 1. IP 白名单验证
6
+ * 2. CORS 限制
7
+ * 3. 请求日志审计
8
+ * 4. 安全头部
9
+ */
10
+ import { Request, Response, NextFunction } from 'express';
11
+ /**
12
+ * IP 白名单中间件
13
+ */
14
+ export declare function ipWhitelist(allowedIPs: string[]): (req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>;
15
+ /**
16
+ * 动态 CORS 配置
17
+ */
18
+ export declare function createCorsOptions(allowedOrigins: string[]): {
19
+ origin: (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => void;
20
+ credentials: boolean;
21
+ methods: string[];
22
+ allowedHeaders: string[];
23
+ };
24
+ /**
25
+ * 审计日志中间件
26
+ */
27
+ export declare function auditLog(): (req: Request, res: Response, next: NextFunction) => void;
28
+ /**
29
+ * 安全头部中间件
30
+ */
31
+ export declare function securityHeaders(): (req: Request, res: Response, next: NextFunction) => void;
32
+ /**
33
+ * 请求体大小限制
34
+ */
35
+ export declare const requestSizeLimit: {
36
+ json: {
37
+ limit: string;
38
+ };
39
+ urlencoded: {
40
+ limit: string;
41
+ extended: boolean;
42
+ };
43
+ };
44
+ /**
45
+ * 验证必需的环境变量
46
+ */
47
+ export declare function validateEnv(): void;
48
48
  //# sourceMappingURL=security.d.ts.map