@witnium/witniumchain 0.25.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -46,7 +46,7 @@ spec differs from the sha256 recorded in `specs/sources.json`, or when
46
46
  `src/generated/` is stale. The accounts service is a separate private repo, so
47
47
  CI cannot read it: `npm run openapi:verify:accounts` (with
48
48
  `WITNIUMCHAIN_ACCOUNTS_REPO` pointing at a clone) checks the recorded commit
49
- and the current `origin/main` there, and runs ahead of every `npm publish`.
49
+ and the tip of the branch it tracks there, and runs ahead of every `npm publish`.
50
50
  `npm run openapi:refresh` re-vendors both specs and regenerates the types.
51
51
 
52
52
  ## Install
@@ -63,7 +63,7 @@ throw at call time.
63
63
 
64
64
  | Credential | Header / Cookie | Used by |
65
65
  |---|---|---|
66
- | `sessionCookie` | `Cookie: wac_session=…` | `/v1/auth/logout`, `/v1/account/*`, `/v1/billing/*`, `/v1/keys/*`, `/v1/contracts/{pause,unpause}`, `/v1/oauth/sessions*` |
66
+ | `sessionCookie` | `Cookie: wac_session=…` | `/v1/auth/logout`, `/v1/account/*`, `/v1/billing/*`, `/v1/orgs/me*`, `/v1/keys/*`, `/v1/contracts/{pause,unpause}`, `/v1/oauth/sessions*` |
67
67
  | `accessToken` | `Authorization: Bearer <JWT>` | `/v1/users/me/delegated-keys/*`, `/v1/sign`, **all chain-api reads** |
68
68
  | `apiKey` | `Authorization: Bearer wcak_…` | `/v1/sign`, `/v1/api-key`, v5 witness `propose`/`sign`/`finalize` (via `WitniumchainApiKeyClient`) |
69
69
  | `adminToken` | `Authorization: Bearer <ADMIN_TOKEN>` | `/v1/admin/*` |
@@ -132,20 +132,101 @@ const server = new WitniumchainApiKeyClient({
132
132
  });
133
133
 
134
134
  const me = await server.me(); // its contract and signing key
135
- const { intentId, payloadToSign } = await server.proposeWitnessV5(me.contractAddress, {
135
+ const proposed = await server.proposeWitnessV5(me.contractAddress, {
136
136
  dataId: sha256Hex,
137
137
  requiredSigners: [me.publicKey],
138
+ // no scope: the SDK generates one, so this is a new witness (see "Witness scopes")
138
139
  });
139
- const { signature } = await server.sign({ delegatedKeyId: me.delegatedKeyId, payload: Buffer.from(payloadToSign, 'utf8').toString('hex') });
140
- await server.signWitnessV5(me.contractAddress, intentId, { signerPubKey: me.publicKey, signature });
141
- const { witnessId } = await server.finalizeWitnessV5(me.contractAddress, intentId);
140
+ if (proposed.outcome === 'created') {
141
+ const { intentId, payloadToSign } = proposed;
142
+ const { signature } = await server.sign({ delegatedKeyId: me.delegatedKeyId, payload: Buffer.from(payloadToSign, 'utf8').toString('hex') });
143
+ await server.signWitnessV5(me.contractAddress, intentId, { signerPubKey: me.publicKey, signature });
144
+ const { witnessId } = await server.finalizeWitnessV5(me.contractAddress, intentId);
145
+ }
146
+ ```
147
+
148
+ ### Witness scopes
149
+
150
+ A v5 propose runs in a **witness scope**: a scope holds at most one witness
151
+ per content hash (`dataId`), ever, shared by everyone who writes to the
152
+ contract (colleagues, API keys, MCP connections of the org).
153
+
154
+ - Same scope and `dataId`: the same witness, never a second one. Once it
155
+ is mined, a repeat gets `outcome: "existing"` with that witness and is
156
+ not charged. While your attempt is still in flight, a retry with the
157
+ identical body gets `outcome: "created"` with the same intent (sign and
158
+ finalize it as usual).
159
+ - Same scope, another `dataId`: another witness in that scope.
160
+ - Duplicates on purpose: a new scope.
161
+
162
+ The scope is the SHA-256 of a key you choose, as 64 hex characters; Witnium
163
+ never sees the key. Pass it as `scope` in the propose body:
164
+
165
+ ```ts
166
+ import { witnessScopeFor, WitnessProposeError, WitnessScopeConflictError } from '@witnium/witniumchain';
167
+
168
+ const scope = await witnessScopeFor(`daily-report:${today}`); // once per day; or a project id, a record id
169
+ try {
170
+ const proposed = await server.proposeWitnessV5(me.contractAddress, { dataId, requiredSigners: [me.publicKey], scope });
171
+ if (proposed.outcome === 'existing') {
172
+ // Already witnessed: proposed.witnessId, made at proposed.chainTimestamp (unix seconds). Nothing to sign.
173
+ } else {
174
+ // 'created': sign proposed.payloadToSign and finalize proposed.intentId, as above.
175
+ }
176
+ } catch (err) {
177
+ if (err instanceof WitnessScopeConflictError) {
178
+ // err.code: 'scope_details_mismatch' (this dataId is in the scope with another
179
+ // userTimestamp, revealAfter or drandRound; err.witnessId once mined),
180
+ // 'scope_in_progress' (an attempt you cannot join is in flight: retry later,
181
+ // with the identical body), or 'scope_registry_mismatch'.
182
+ } else if (err instanceof WitnessProposeError) {
183
+ // Any other failure: an API error, a network error or timeout, a malformed
184
+ // answer (err.status 0, err.cause). Retry with { ...body, scope: err.scope }:
185
+ // the retry can never create or charge a second witness.
186
+ }
187
+ throw err;
188
+ }
142
189
  ```
143
190
 
191
+ Without a `scope`, the SDK generates 32 random bytes before the request
192
+ (`randomWitnessScope()`), keeps them for its own retry, and returns them as
193
+ `scope` in the result and in every `WitnessProposeError`. Store the scope
194
+ before you rely on a retry: it is what makes the retry safe.
195
+
196
+ The details of a repeat must match: the same scope and `dataId` with another
197
+ `userTimestamp`, `revealAfter` or `drandRound` is a 409
198
+ `scope_details_mismatch`, never a second witness. A caller whose timestamp
199
+ naturally varies puts it in the scope key. A failed, cancelled or expired
200
+ attempt frees the scope; a revoked witness still occupies it.
201
+
202
+ **Deprecated: the `idempotencyKey` argument.** `proposeWitnessV5(addr, body,
203
+ idempotencyKey)` still sends the old `Idempotency-Key` header for one more
204
+ release of accounts, then the header path is removed. Its answer has no
205
+ `outcome` or `scope`, and passing both a key and a `scope` throws. Move to
206
+ `scope: await witnessScopeFor(key)` (or the key itself when it is already
207
+ 64 hex). The rule changes with it: a key was one request per caller (another
208
+ body under the same key was a 409); a scope is one witness per `dataId` for
209
+ the whole contract. The SDK no longer derives a key from the request body:
210
+ before this release, two proposes of the same content without a key were
211
+ merged into one witness forever; now each is a new witness unless you give
212
+ them the same scope.
213
+
144
214
  Org admins manage keys with the dashboard session: `client.apiKeys.create({ label })`
145
215
  (after a step-up; the secret is returned once), `client.apiKeys.list()`,
146
216
  `client.apiKeys.revoke(id)`.
147
217
 
148
- ### Sysadmin — create an organisation
218
+ ### Org admins: the organization and its team
219
+
220
+ With the dashboard's session cookie, an org admin manages their own org;
221
+ the org comes from the session, never from a parameter.
222
+
223
+ ```ts
224
+ await client.organization.rename('Witnium Newsroom'); // display name; the billing name stays in the billing portal
225
+ await client.orgUsers.invite({ email: 'teammate@example.com', role: 'member' });
226
+ const { members } = await client.orgUsers.list();
227
+ ```
228
+
229
+ ### Sysadmin — create an organization
149
230
 
150
231
  ```ts
151
232
  const sys = new WitniumchainClient({
@@ -239,7 +320,9 @@ const intent = await sdk.proposeWitness('0x…', {
239
320
 
240
321
  ## Errors
241
322
 
242
- All non-2xx responses surface as `WitniumchainApiError`:
323
+ All non-2xx responses surface as `WitniumchainApiError` (a scoped v5
324
+ propose throws its subclasses `WitnessProposeError` and
325
+ `WitnessScopeConflictError`, see [Witness scopes](#witness-scopes)):
243
326
 
244
327
  ```ts
245
328
  import { WitniumchainApiError } from '@witnium/witniumchain';