@bsv/create-dpp-app 0.0.0-stage → 0.1.0-beta.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 (178) hide show
  1. package/LICENSE +203 -0
  2. package/NOTICE +2 -0
  3. package/PATENTS +12 -0
  4. package/README.md +93 -2
  5. package/dist/index.js +125 -0
  6. package/package.json +57 -4
  7. package/template/Dockerfile +37 -0
  8. package/template/README.md +389 -0
  9. package/template/_dockerignore +6 -0
  10. package/template/_env.example +122 -0
  11. package/template/_github/workflows/ci.yml +23 -0
  12. package/template/_gitignore +8 -0
  13. package/template/_prettierrc +6 -0
  14. package/template/apps/api/API.md +119 -0
  15. package/template/apps/api/package.json +39 -0
  16. package/template/apps/api/src/auth.ts +55 -0
  17. package/template/apps/api/src/bound-profiles.ts +590 -0
  18. package/template/apps/api/src/claims.ts +204 -0
  19. package/template/apps/api/src/cli.ts +99 -0
  20. package/template/apps/api/src/config.ts +180 -0
  21. package/template/apps/api/src/custody.ts +358 -0
  22. package/template/apps/api/src/dev-index.ts +120 -0
  23. package/template/apps/api/src/env-file.ts +55 -0
  24. package/template/apps/api/src/env.ts +14 -0
  25. package/template/apps/api/src/epcis/exchange.ts +434 -0
  26. package/template/apps/api/src/epcis/mongo-storage.ts +381 -0
  27. package/template/apps/api/src/epcis/routes.ts +364 -0
  28. package/template/apps/api/src/epcis/sources.ts +142 -0
  29. package/template/apps/api/src/go-live.ts +226 -0
  30. package/template/apps/api/src/http-error.ts +15 -0
  31. package/template/apps/api/src/identifiers.ts +57 -0
  32. package/template/apps/api/src/index-client.ts +261 -0
  33. package/template/apps/api/src/index-projects.ts +53 -0
  34. package/template/apps/api/src/lifecycle-authority.ts +49 -0
  35. package/template/apps/api/src/lifecycle.ts +485 -0
  36. package/template/apps/api/src/main.ts +58 -0
  37. package/template/apps/api/src/parties.ts +68 -0
  38. package/template/apps/api/src/platform.ts +176 -0
  39. package/template/apps/api/src/portability/commands.ts +224 -0
  40. package/template/apps/api/src/portability/export.ts +708 -0
  41. package/template/apps/api/src/portability/package.ts +270 -0
  42. package/template/apps/api/src/portability/restore.ts +863 -0
  43. package/template/apps/api/src/profiles.ts +125 -0
  44. package/template/apps/api/src/reader.ts +265 -0
  45. package/template/apps/api/src/resolver.ts +159 -0
  46. package/template/apps/api/src/sector-examples.ts +390 -0
  47. package/template/apps/api/src/server.ts +613 -0
  48. package/template/apps/api/src/store.ts +426 -0
  49. package/template/apps/api/src/validation-engine.ts +172 -0
  50. package/template/apps/api/src/wallet.ts +359 -0
  51. package/template/apps/api/src/worker.ts +113 -0
  52. package/template/apps/api/src/writer.ts +603 -0
  53. package/template/apps/api/test/bound-http.test.ts +496 -0
  54. package/template/apps/api/test/bound-profiles.test.ts +547 -0
  55. package/template/apps/api/test/config.test.ts +160 -0
  56. package/template/apps/api/test/custody-journal.test.ts +415 -0
  57. package/template/apps/api/test/custody-recovery-profile.test.ts +224 -0
  58. package/template/apps/api/test/custom-profile.test.ts +167 -0
  59. package/template/apps/api/test/env-file.test.ts +53 -0
  60. package/template/apps/api/test/epcis-fixtures.ts +270 -0
  61. package/template/apps/api/test/epcis-http.test.ts +1179 -0
  62. package/template/apps/api/test/epcis-mongo-storage.test.ts +313 -0
  63. package/template/apps/api/test/epcis-separation.test.ts +253 -0
  64. package/template/apps/api/test/epcis-web.test.ts +230 -0
  65. package/template/apps/api/test/exit-exercise.test.ts +1038 -0
  66. package/template/apps/api/test/external-index.test.ts +32 -0
  67. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/background.ttl +33 -0
  68. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/binding.json +141 -0
  69. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/context.jsonld +36 -0
  70. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/product.schema.json +44 -0
  71. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/shapes.ttl +112 -0
  72. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/conflicting-gtin.json +44 -0
  73. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/conflicting-serial.json +44 -0
  74. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/granularity-mismatch.json +44 -0
  75. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/item-gtin-13.json +43 -0
  76. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/item.json +42 -0
  77. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/missing-required-property.json +43 -0
  78. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/model-as-subject.json +44 -0
  79. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/no-passport-identifier.json +43 -0
  80. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/other-product.json +44 -0
  81. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/part-not-dishwasher-safe.json +45 -0
  82. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/altered-artefact.json +19 -0
  83. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/altered-background.ttl +33 -0
  84. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/conflicting-binding.json +141 -0
  85. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/conflicting-second-registration.json +19 -0
  86. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/registration/untrusted-binding.json +13 -0
  87. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/unlisted-context.json +47 -0
  88. package/template/apps/api/test/fixtures/custom-profile/example-bottle@1/vectors/wrong-binding-digest.json +44 -0
  89. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/binding.json +88 -0
  90. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/product.schema.json +25 -0
  91. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/item.json +26 -0
  92. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/missing-required-property.json +27 -0
  93. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/no-passport-identifier.json +27 -0
  94. package/template/apps/api/test/fixtures/custom-profile/example-notebook@1/vectors/other-product.json +28 -0
  95. package/template/apps/api/test/go-live.test.ts +179 -0
  96. package/template/apps/api/test/gs1-global-setup.ts +33 -0
  97. package/template/apps/api/test/gs1-test-env.ts +5 -0
  98. package/template/apps/api/test/identifier-host.test.ts +256 -0
  99. package/template/apps/api/test/index-projects.test.ts +81 -0
  100. package/template/apps/api/test/lifecycle-claims.test.ts +546 -0
  101. package/template/apps/api/test/lifecycle-fixtures.ts +167 -0
  102. package/template/apps/api/test/lifecycle.test.ts +147 -0
  103. package/template/apps/api/test/manifest-notice.test.ts +36 -0
  104. package/template/apps/api/test/network-guard.ts +113 -0
  105. package/template/apps/api/test/portability.test.ts +655 -0
  106. package/template/apps/api/test/profile-bundle-resolver.test.ts +198 -0
  107. package/template/apps/api/test/retired-profiles.test.ts +216 -0
  108. package/template/apps/api/test/sector-cutover.test.ts +95 -0
  109. package/template/apps/api/test/sector-forms.test.ts +420 -0
  110. package/template/apps/api/test/unjoined-index.test.ts +62 -0
  111. package/template/apps/api/test/validation-engine.test.ts +328 -0
  112. package/template/apps/api/test/wallet-monitor.test.ts +38 -0
  113. package/template/apps/api/test/writer-journal.test.ts +327 -0
  114. package/template/apps/api/tsconfig.json +9 -0
  115. package/template/apps/api/vitest.config.ts +12 -0
  116. package/template/apps/web/components.json +21 -0
  117. package/template/apps/web/index.html +12 -0
  118. package/template/apps/web/package.json +33 -0
  119. package/template/apps/web/src/app.tsx +55 -0
  120. package/template/apps/web/src/components/bits.tsx +188 -0
  121. package/template/apps/web/src/components/bound/bound-passport-form.tsx +368 -0
  122. package/template/apps/web/src/components/bound/field-view.tsx +237 -0
  123. package/template/apps/web/src/components/bound/pieces.tsx +169 -0
  124. package/template/apps/web/src/components/bound/readings.tsx +152 -0
  125. package/template/apps/web/src/components/bound/structured-value.tsx +114 -0
  126. package/template/apps/web/src/components/claim-dialog.tsx +344 -0
  127. package/template/apps/web/src/components/epcis-events.tsx +119 -0
  128. package/template/apps/web/src/components/layout.tsx +135 -0
  129. package/template/apps/web/src/components/lifecycle-claims.tsx +199 -0
  130. package/template/apps/web/src/components/passport-form.tsx +244 -0
  131. package/template/apps/web/src/components/read-passport.tsx +49 -0
  132. package/template/apps/web/src/components/reading.tsx +150 -0
  133. package/template/apps/web/src/components/ui/alert.tsx +65 -0
  134. package/template/apps/web/src/components/ui/badge.tsx +47 -0
  135. package/template/apps/web/src/components/ui/button.tsx +63 -0
  136. package/template/apps/web/src/components/ui/card.tsx +91 -0
  137. package/template/apps/web/src/components/ui/dialog.tsx +156 -0
  138. package/template/apps/web/src/components/ui/input.tsx +20 -0
  139. package/template/apps/web/src/components/ui/label.tsx +21 -0
  140. package/template/apps/web/src/components/ui/select.tsx +189 -0
  141. package/template/apps/web/src/components/ui/separator.tsx +25 -0
  142. package/template/apps/web/src/components/ui/table.tsx +115 -0
  143. package/template/apps/web/src/components/ui/tabs.tsx +90 -0
  144. package/template/apps/web/src/components/ui/textarea.tsx +17 -0
  145. package/template/apps/web/src/index.css +81 -0
  146. package/template/apps/web/src/lib/api.ts +396 -0
  147. package/template/apps/web/src/lib/auth.ts +10 -0
  148. package/template/apps/web/src/lib/bound/battery-form.ts +269 -0
  149. package/template/apps/web/src/lib/bound/display.ts +122 -0
  150. package/template/apps/web/src/lib/bound/fields.ts +128 -0
  151. package/template/apps/web/src/lib/bound/form-model.ts +521 -0
  152. package/template/apps/web/src/lib/bound/forms.ts +15 -0
  153. package/template/apps/web/src/lib/bound/identity.ts +48 -0
  154. package/template/apps/web/src/lib/bound/textile-form.ts +236 -0
  155. package/template/apps/web/src/lib/bound/units.ts +37 -0
  156. package/template/apps/web/src/lib/epcis.ts +345 -0
  157. package/template/apps/web/src/lib/format.ts +87 -0
  158. package/template/apps/web/src/lib/lifecycle.ts +350 -0
  159. package/template/apps/web/src/lib/session.tsx +92 -0
  160. package/template/apps/web/src/lib/use-load.ts +46 -0
  161. package/template/apps/web/src/main.tsx +18 -0
  162. package/template/apps/web/src/pages/accept.tsx +113 -0
  163. package/template/apps/web/src/pages/brand.tsx +94 -0
  164. package/template/apps/web/src/pages/brands.tsx +104 -0
  165. package/template/apps/web/src/pages/events.tsx +388 -0
  166. package/template/apps/web/src/pages/home.tsx +51 -0
  167. package/template/apps/web/src/pages/new-passport.tsx +137 -0
  168. package/template/apps/web/src/pages/operations.tsx +169 -0
  169. package/template/apps/web/src/pages/passport.tsx +632 -0
  170. package/template/apps/web/src/pages/public-passport.tsx +64 -0
  171. package/template/apps/web/src/pages/sign-in.tsx +93 -0
  172. package/template/apps/web/src/pages/verify.tsx +30 -0
  173. package/template/apps/web/src/pages/wallet.tsx +141 -0
  174. package/template/apps/web/tsconfig.json +29 -0
  175. package/template/apps/web/vite.config.ts +23 -0
  176. package/template/deploy/compose.yml +38 -0
  177. package/template/package.json +48 -0
  178. package/template/tsconfig.base.json +13 -0
@@ -0,0 +1,396 @@
1
+ // The API client: one fetch wrapper that turns the API's { error, description }
2
+ // answer into an Error, and a typed helper for every route in apps/api/API.md.
3
+ // Reads are open; writes carry the session cookie, which a same-origin fetch
4
+ // sends by itself.
5
+ import type { EpcisEventsAnswer, EpcisImportAnswer, EpcisImportRecord, EpcisImportSummary, EpcisPullPage, EpcisPullQuery, EpcisPullResult, EpcisPullState, EpcisSource, EpcisStatus } from './epcis'
6
+ import type { LifecycleClaimView, PassportClaimsReading } from './lifecycle'
7
+
8
+ export class ApiError extends Error {
9
+ readonly code: string
10
+ readonly status: number
11
+ /** Everything else the API put in its answer: for a refused product document, the validation with its findings. */
12
+ readonly details: Record<string, unknown>
13
+
14
+ constructor(message: string, code: string, status: number, details: Record<string, unknown> = {}) {
15
+ super(message)
16
+ this.name = 'ApiError'
17
+ this.code = code
18
+ this.status = status
19
+ this.details = details
20
+ }
21
+ }
22
+
23
+ export const isNotFound = (error: unknown): boolean => error instanceof ApiError && error.status === 404
24
+
25
+ async function request<T>(path: string, method: 'GET' | 'POST' = 'GET', body?: unknown): Promise<T> {
26
+ const init: RequestInit = { method, headers: { accept: 'application/json' } }
27
+ if (body !== undefined) {
28
+ init.headers = { ...init.headers, 'content-type': 'application/json' }
29
+ init.body = JSON.stringify(body)
30
+ }
31
+ return await answerOf<T>(await fetch(path, init))
32
+ }
33
+
34
+ /** The API's answer as a value, or as the error it describes. */
35
+ async function answerOf<T>(response: Response): Promise<T> {
36
+ const text = await response.text()
37
+ let parsed: unknown
38
+ try {
39
+ parsed = text === '' ? undefined : JSON.parse(text)
40
+ } catch {
41
+ parsed = undefined
42
+ }
43
+ if (!response.ok) {
44
+ const failure = (typeof parsed === 'object' && parsed !== null ? parsed : {}) as { error?: string; description?: string } & Record<string, unknown>
45
+ const { error: code, description, ...details } = failure
46
+ throw new ApiError(description ?? `the API answered HTTP ${response.status}`, code ?? `http-${response.status}`, response.status, details)
47
+ }
48
+ if (parsed === undefined) throw new ApiError('the API answered with something that is not JSON', 'not-json', response.status)
49
+ return parsed as T
50
+ }
51
+
52
+ const get = <T>(path: string): Promise<T> => request<T>(path)
53
+ const post = <T>(path: string, body: unknown = {}): Promise<T> => request<T>(path, 'POST', body)
54
+
55
+ /** A passport reference for a URL: the identifier, URL-encoded once. */
56
+ export const passportRef = (passportId: string): string => encodeURIComponent(passportId)
57
+
58
+ // ------------------------------------------------------------------ shapes
59
+
60
+ export interface Health {
61
+ ok: boolean
62
+ network: 'main' | 'test'
63
+ /** The index in use: in this process until the app has joined the configured one. */
64
+ index: { url: string; joined: boolean; inProcess: boolean; configured?: string }
65
+ publisherKey: string
66
+ wallet: 'toolbox' | 'test'
67
+ livePublishing: boolean
68
+ /** False when the validation engine cannot be used: its libraries did not load, or the validator service did not answer. A profile held by a binding then cannot be written, and its readings say "not checked". */
69
+ validationEngine: boolean
70
+ /** Where the engine runs: in the API's own process, or in the validator service its settings name. */
71
+ validationEngineKind?: 'in-process' | 'service'
72
+ identifiers: { host: string; prefix: string; demonstration: boolean }
73
+ signIn: boolean
74
+ devSession?: { name: string; brandIds: string[] }
75
+ }
76
+
77
+ export interface ProfileSummary {
78
+ /** The edition offered for new passports when this profile is historical. */
79
+ issueSuccessor?: string
80
+ id: string
81
+ title: string
82
+ description?: string
83
+ /** `manifest`: a field list this application's form is drawn from. `binding`: a data model defined elsewhere, written as one product document. */
84
+ format: 'manifest' | 'binding'
85
+ }
86
+
87
+ export interface CodeList {
88
+ closed: boolean
89
+ options: Array<{ value: string; label: string }>
90
+ }
91
+
92
+ export interface Constraints {
93
+ minimum?: number
94
+ maximum?: number
95
+ step?: number
96
+ pattern?: string
97
+ patternHint?: string
98
+ maxLength?: number
99
+ }
100
+
101
+ export interface ProfileField {
102
+ key: string
103
+ label: string
104
+ valueType: string
105
+ unit?: string
106
+ accessTier: string
107
+ obligation: 'required' | 'recommended' | 'optional'
108
+ codeList?: CodeList
109
+ constraints?: Constraints
110
+ note?: string
111
+ group?: string
112
+ /** Not sent by the API today; read when present. */
113
+ cardinality?: 'one' | 'many'
114
+ }
115
+
116
+ export interface ManifestProfile extends ProfileSummary {
117
+ format: 'manifest'
118
+ fields: ProfileField[]
119
+ publicSchema: unknown
120
+ restrictedSchema: unknown
121
+ sample: Record<string, unknown>
122
+ }
123
+
124
+ /** A profile held by a binding: its product document is split by top-level property into a public part, on chain, and a restricted part, sealed off chain. */
125
+ export interface BoundProfile extends ProfileSummary {
126
+ format: 'binding'
127
+ bindingDigest: string
128
+ steps: string[]
129
+ /** The binding's public list. Every other property is restricted. Absent when the binding makes no split: the whole document is then public. */
130
+ access?: { policy: string; public: string[] }
131
+ contexts: Array<{ path: string; uri: string }>
132
+ prefixes: Record<string, string>
133
+ engine: { available: boolean; kind?: 'in-process' | 'service'; problem?: string }
134
+ /** Complete product documents of invented data, minted for this deployment's host and prefix. */
135
+ examples: Array<{ title: string; itemReference: string; serial: string; passportId: string; product: Record<string, unknown> }>
136
+ demonstration: boolean
137
+ }
138
+
139
+ export type Profile = ManifestProfile | BoundProfile
140
+
141
+ export type CodeLists = Record<string, Array<{ value: string; label: string }>>
142
+
143
+ /** How a view of a payload came out. `not-checked` is never a pass. */
144
+ export type ViewStatus = 'pass' | 'fail' | 'not-checked'
145
+
146
+ export interface Finding {
147
+ severity: 'Violation' | 'Warning' | 'Info'
148
+ constraint: string
149
+ step: string
150
+ focusNode?: string
151
+ path?: string
152
+ message?: string
153
+ value?: string
154
+ /** A JSON pointer into the product document, when the finding could be placed. */
155
+ pointer?: string
156
+ property?: string
157
+ }
158
+
159
+ export interface ViewCheck {
160
+ view: 'public' | 'authorised' | 'whole'
161
+ status: ViewStatus
162
+ cause?: string
163
+ summary: string
164
+ steps: Array<{ step: string; status: ViewStatus; cause?: string; messages: string[] }>
165
+ findings: Finding[]
166
+ withheld: Array<{ step: string; property: string; constraint: string; message?: string }>
167
+ }
168
+
169
+ export interface PayloadCheck {
170
+ txid: string
171
+ stateIndex: number
172
+ profile: string
173
+ publicView: ViewCheck
174
+ }
175
+
176
+ export interface ProductReading {
177
+ profile: string
178
+ bindingDigest: string
179
+ notice?: string
180
+ publicProduct: Record<string, unknown>
181
+ access?: { policy: string; public: string[] }
182
+ }
183
+
184
+ /** The holder's view of a passport under a bound profile. The owner-tier check is the protocol's, and the only statement about the on-chain commitment. */
185
+ export interface AuthorisedReading {
186
+ passportId: string
187
+ profile?: string
188
+ ownerTier: { status: ViewStatus; txid?: string; detail: string }
189
+ authorisedView: ViewCheck
190
+ restrictedProduct?: Record<string, unknown>
191
+ product?: Record<string, unknown>
192
+ access?: { policy: string; public: string[] }
193
+ }
194
+
195
+ export type CheckStatus = 'pass' | 'fail' | 'unknown' | 'not-applicable'
196
+
197
+ export interface Check {
198
+ name: string
199
+ label: string
200
+ status: CheckStatus
201
+ reasonCode?: string
202
+ }
203
+
204
+ export interface LineageEntry {
205
+ op: string
206
+ txid: string
207
+ timestamp: string
208
+ controllerKey: string
209
+ actorIdentityKey: string
210
+ payloadPublic: unknown
211
+ }
212
+
213
+ export interface Verification {
214
+ passportId: string
215
+ lineage: LineageEntry[]
216
+ checks: Check[]
217
+ report: { checkedAt?: string; limits?: string[]; [key: string]: unknown }
218
+ publisherKeys: string[]
219
+ /** One entry per state whose payload declares a profile, genesis first. */
220
+ payloadChecks?: PayloadCheck[]
221
+ /** Present when the newest state carries a product document under a binding. */
222
+ product?: ProductReading
223
+ }
224
+
225
+ export type Op = 'ISSUE' | 'UPDATE' | 'TRANSFER' | 'RETIRE'
226
+
227
+ export interface StateView {
228
+ op: Op
229
+ txid: string
230
+ timestamp: string
231
+ actor: string
232
+ index: string
233
+ refusal?: string
234
+ network: string
235
+ proven: boolean
236
+ proofPushed: boolean
237
+ blockHeight?: number
238
+ }
239
+
240
+ export interface JournalSummary {
241
+ states: number
242
+ proven: number
243
+ pending: number
244
+ status: string
245
+ }
246
+
247
+ export interface PassportView {
248
+ passportId: string
249
+ brandId: string
250
+ profile: string
251
+ /** Present when the passport's profile is retired: nothing further is written for it, and this names the profile that replaces it. */
252
+ profileRetired?: { replacedBy: string }
253
+ status: 'active' | 'retired' | 'awaiting-send' | 'not-issued'
254
+ demonstration: boolean
255
+ holder: { party: string; controllerKey: string }
256
+ createdAt: string
257
+ payload: unknown
258
+ journal: JournalSummary
259
+ states: StateView[]
260
+ acceptanceRecords: unknown[]
261
+ }
262
+
263
+ export type OfferStatus = 'open' | 'accepted' | 'transferred' | 'declined' | 'withdrawn' | 'expired'
264
+
265
+ export interface OfferPreview {
266
+ requestId: string
267
+ passportId: string
268
+ terms: Record<string, unknown>
269
+ expiresAt: string
270
+ status: OfferStatus
271
+ }
272
+
273
+ export interface OfferView {
274
+ requestId: string
275
+ passportId: string
276
+ brandId: string
277
+ mechanism: 'claim-code' | 'named-recipient'
278
+ terms: Record<string, unknown>
279
+ createdAt: string
280
+ expiresAt: string
281
+ status: OfferStatus
282
+ transferTxid?: string
283
+ }
284
+
285
+ export interface AcceptResult {
286
+ offer: OfferView
287
+ record: unknown
288
+ transferTxid: string
289
+ }
290
+
291
+ export interface Brand {
292
+ id: string
293
+ name: string
294
+ identityKey: string
295
+ did: string
296
+ createdAt: string
297
+ }
298
+
299
+ export interface Me {
300
+ userId: string
301
+ email: string
302
+ name: string
303
+ brandIds: string[]
304
+ brands: Brand[]
305
+ }
306
+
307
+ export interface Duty {
308
+ passportId: string
309
+ txid: string
310
+ op: string
311
+ duty: 'announce' | 'prove' | 'push-proof'
312
+ since: string
313
+ }
314
+
315
+ export interface Operations {
316
+ duties: Duty[]
317
+ passports: Array<{ passportId: string; brandId: string; status: string; journal: JournalSummary }>
318
+ /** The validation engine's problem as it is now, where bound profiles are read from, and each profile folder or bundle that was refused at start. */
319
+ profiles?: { engine: { available: boolean; kind?: 'in-process' | 'service'; problem?: string }; source?: 'package' | 'bundle'; refused: Array<{ source: string; code: string; detail: string }> }
320
+ }
321
+
322
+ export interface WalletView {
323
+ kind: 'toolbox' | 'test'
324
+ network: string
325
+ identityKey: string
326
+ balance: number
327
+ livePublishing: boolean
328
+ }
329
+
330
+ export interface FundingAddress {
331
+ address: string
332
+ derivationPrefix: string
333
+ derivationSuffix: string
334
+ senderIdentityKey: string
335
+ }
336
+
337
+ /** What a member sends to sign an EPCIS lifecycle claim: the event they entered, one the EPCIS exchange retained, or a claim to declare in error. */
338
+ export type IssueClaimRequest = ({ event: Record<string, unknown> } | { imported: { importId: string; pointer: string } } | { correctionOf: string; reason?: 'did_not_occur' | 'incorrect_data' }) & {
339
+ assertedRole?: string
340
+ issuerParty?: string
341
+ submit?: boolean
342
+ }
343
+
344
+ // ------------------------------------------------------------------ routes
345
+
346
+ export const api = {
347
+ health: () => get<Health>('/api/health'),
348
+ profiles: () => get<ProfileSummary[]>('/api/profiles'),
349
+ profile: (id: string) => get<Profile>(`/api/profiles/${encodeURIComponent(id)}`),
350
+ profileCodes: (id: string, classes: string[]) => (classes.length === 0 ? Promise.resolve<CodeLists>({}) : get<CodeLists>(`/api/profiles/${encodeURIComponent(id)}/codes?${classes.map((name) => `class=${encodeURIComponent(name)}`).join('&')}`)),
351
+ verify: (passportId: string) => get<Verification>(`/api/verify?passportId=${encodeURIComponent(passportId)}`),
352
+ passport: (passportId: string) => get<PassportView>(`/api/passports/${passportRef(passportId)}`),
353
+ passportClaims: (passportId: string) => get<PassportClaimsReading>(`/api/passports/${passportRef(passportId)}/claims`),
354
+ offer: (requestId: string) => get<OfferPreview>(`/api/offers/${encodeURIComponent(requestId)}`),
355
+ acceptOffer: (requestId: string, claimCode: string) => post<AcceptResult>(`/api/offers/${encodeURIComponent(requestId)}/accept`, { claimCode }),
356
+ declineOffer: (requestId: string, claimCode: string) => post<OfferView>(`/api/offers/${encodeURIComponent(requestId)}/decline`, { claimCode }),
357
+
358
+ me: () => get<Me>('/api/me'),
359
+ brands: () => get<Brand[]>('/api/brands'),
360
+ createBrand: (name: string) => post<Brand>('/api/brands', { name }),
361
+ brandPassports: (brandId: string) => get<PassportView[]>(`/api/brands/${encodeURIComponent(brandId)}/passports`),
362
+ issuePassport: (brandId: string, body: { profile: string; itemReference: string; serial: string } & ({ payload: Record<string, unknown>; ownerFields: Record<string, unknown> } | { product: Record<string, unknown> })) =>
363
+ post<PassportView>(`/api/brands/${encodeURIComponent(brandId)}/passports`, body),
364
+ updatePassport: (passportId: string, body: ({ payload: Record<string, unknown>; ownerFields: Record<string, unknown> } | { product: Record<string, unknown> }) & { eventData?: Record<string, unknown>; claimCode?: string }) =>
365
+ post<PassportView>(`/api/passports/${passportRef(passportId)}/update`, body),
366
+ authorisedView: (passportId: string, claimCode?: string) => post<AuthorisedReading>(`/api/passports/${passportRef(passportId)}/authorised-view`, claimCode == null ? {} : { claimCode }),
367
+ offerPassport: (passportId: string, body: { requestId?: string; terms: { word: string; note?: string }; claimCode?: string }) =>
368
+ post<{ offer: OfferView; claimCode?: string }>(`/api/passports/${passportRef(passportId)}/offer`, body),
369
+ passportOffers: (passportId: string) => get<OfferView[]>(`/api/passports/${passportRef(passportId)}/offers`),
370
+ withdrawOffer: (requestId: string, claimCode?: string) => post<OfferView>(`/api/offers/${encodeURIComponent(requestId)}/withdraw`, claimCode == null ? {} : { claimCode }),
371
+ retirePassport: (passportId: string, body: { reason: string; claimCode?: string }) => post<PassportView>(`/api/passports/${passportRef(passportId)}/retire`, body),
372
+ issueClaim: (passportId: string, body: IssueClaimRequest) => post<LifecycleClaimView>(`/api/passports/${passportRef(passportId)}/claims`, body),
373
+
374
+ operations: () => get<Operations>('/api/operations'),
375
+ runOperations: () => post<{ done: string[] }>('/api/operations/run'),
376
+ wallet: () => get<WalletView>('/api/wallet'),
377
+ fundingAddress: () => post<FundingAddress>('/api/wallet/funding-address'),
378
+ internalize: (txid: string, funding: FundingAddress) => post<{ satoshis: number }>('/api/wallet/internalize', { txid, funding }),
379
+
380
+ // EPCIS exchange. An import or a pull retains data and changes no passport.
381
+ epcisStatus: () => get<EpcisStatus>('/api/epcis/status'),
382
+ epcisSources: () => get<EpcisSource[]>('/api/epcis/sources'),
383
+ /**
384
+ * Upload a document. The file is sent as the bytes it is, never read and written out again, so what is retained
385
+ * and later exported is exactly what was chosen. `requestId` is chosen once for a file: sending it again is the same request.
386
+ */
387
+ epcisImport: async (file: Blob, options: { requestId: string; mediaType: string; sourceId?: string; filename?: string }) => {
388
+ const query = new URLSearchParams({ requestId: options.requestId, ...(options.sourceId == null || options.sourceId === '' ? {} : { sourceId: options.sourceId }), ...(options.filename == null ? {} : { filename: options.filename }) })
389
+ return await answerOf<EpcisImportAnswer>(await fetch(`/api/epcis/imports?${query.toString()}`, { method: 'POST', headers: { accept: 'application/json', 'content-type': options.mediaType }, body: file }))
390
+ },
391
+ epcisImports: (after?: string) => get<{ imports: EpcisImportSummary[]; next?: string }>(`/api/epcis/imports?limit=200${after == null ? '' : `&after=${encodeURIComponent(after)}`}`),
392
+ epcisImportRecord: (importId: string) => get<EpcisImportRecord>(`/api/epcis/imports/${encodeURIComponent(importId)}`),
393
+ epcisPull: (sourceId: string, body: { queryId: string; query: EpcisPullQuery }) => post<EpcisPullResult>(`/api/epcis/sources/${encodeURIComponent(sourceId)}/pulls`, body),
394
+ epcisPullState: (sourceId: string, queryId: string) => get<{ pull: EpcisPullState; pages: EpcisPullPage[] }>(`/api/epcis/sources/${encodeURIComponent(sourceId)}/pulls/${encodeURIComponent(queryId)}`),
395
+ epcisEvents: (identifier: string, granularity: 'instance' | 'class' = 'instance') => get<EpcisEventsAnswer>(`/api/epcis/events?identifier=${encodeURIComponent(identifier)}&granularity=${granularity}`),
396
+ }
@@ -0,0 +1,10 @@
1
+ // The Better Auth client. The API mounts Better Auth under /api/auth on the
2
+ // same origin, so the session cookie travels with every API call by itself.
3
+ import { createAuthClient } from 'better-auth/react'
4
+ import { organizationClient } from 'better-auth/client/plugins'
5
+
6
+ export const authClient = createAuthClient({
7
+ baseURL: window.location.origin,
8
+ basePath: '/api/auth',
9
+ plugins: [organizationClient()],
10
+ })