@parity/product-sdk-bulletin 0.1.0 → 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.
- package/dist/index.d.ts +445 -396
- package/dist/index.js +306 -1119
- package/dist/index.js.map +1 -1
- package/package.json +8 -7
- package/src/authorization.ts +300 -1
- package/src/cid.ts +115 -239
- package/src/client.ts +238 -179
- package/src/errors.ts +65 -138
- package/src/index.ts +85 -20
- package/src/lazy-signer.ts +113 -0
- package/src/networks.ts +49 -0
- package/src/query.ts +165 -31
- package/src/resolve-query.ts +9 -3
- package/src/types.ts +26 -117
- package/src/verify.ts +384 -0
- package/src/gateway.ts +0 -209
- package/src/resolve-signer.ts +0 -66
- package/src/upload.ts +0 -344
package/src/authorization.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { createLogger } from "@parity/product-sdk-logger";
|
|
2
|
+
import { submitAndWatch, type TxStatus, type WaitFor } from "@parity/product-sdk-tx";
|
|
3
|
+
import type { PolkadotSigner } from "polkadot-api";
|
|
2
4
|
import { Enum } from "polkadot-api";
|
|
3
5
|
|
|
4
|
-
import { BulletinAuthorizationError } from "./errors.js";
|
|
6
|
+
import { BulletinAuthorizationError, ProductBulletinError } from "./errors.js";
|
|
5
7
|
import type { AuthorizationStatus, BulletinApi } from "./types.js";
|
|
6
8
|
|
|
7
9
|
const log = createLogger("bulletin");
|
|
@@ -80,6 +82,144 @@ export async function checkAuthorization(
|
|
|
80
82
|
return status;
|
|
81
83
|
}
|
|
82
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Options for {@link authorizeAccount}.
|
|
87
|
+
*/
|
|
88
|
+
export interface AuthorizeAccountOptions {
|
|
89
|
+
/**
|
|
90
|
+
* Wrap the extrinsic in `Sudo.sudo(...)` before submission. Default: `false`.
|
|
91
|
+
*
|
|
92
|
+
* Use `true` on networks where granting bulletin storage authorization
|
|
93
|
+
* requires sudo permissions (most production / managed test networks).
|
|
94
|
+
* Use `false` (default) when the account self-authorizes — typical for
|
|
95
|
+
* local development chains.
|
|
96
|
+
*/
|
|
97
|
+
viaSudo?: boolean;
|
|
98
|
+
/** When to resolve: `"best-block"` (default) or `"finalized"`. */
|
|
99
|
+
waitFor?: WaitFor;
|
|
100
|
+
/** Timeout in ms. Default: 300_000 (5 min). */
|
|
101
|
+
timeoutMs?: number;
|
|
102
|
+
/** Lifecycle status callback for UI progress. */
|
|
103
|
+
onStatus?: (status: TxStatus) => void;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Grant an account authorization to store data on the Bulletin Chain.
|
|
108
|
+
*
|
|
109
|
+
* Submits a `TransactionStorage.authorize_account` extrinsic, optionally
|
|
110
|
+
* wrapped in `Sudo.sudo(...)` for networks that require sudo to grant
|
|
111
|
+
* authorization. Mirrors the call shape of {@link upload} — top-level
|
|
112
|
+
* function, takes an explicit signer, returns a block hash on success.
|
|
113
|
+
*
|
|
114
|
+
* Pair with {@link checkAuthorization} for a typical "check, grant if
|
|
115
|
+
* insufficient, then upload" flow.
|
|
116
|
+
*
|
|
117
|
+
* ## Additive semantics — call once per authorization need
|
|
118
|
+
*
|
|
119
|
+
* `authorize_account` is **additive** within an unexpired authorization window
|
|
120
|
+
* for `AuthorizationScope::Account` (see `pallet-bulletin-transaction-storage`,
|
|
121
|
+
* `fn authorize`). Each successful call **adds** to the existing
|
|
122
|
+
* `transactions_allowance` and `bytes_allowance` rather than overwriting them.
|
|
123
|
+
*
|
|
124
|
+
* Implications for callers:
|
|
125
|
+
*
|
|
126
|
+
* - Calling this function twice with `(100, 1MB)` while the previous
|
|
127
|
+
* authorization is still active leaves the account with quota for `200`
|
|
128
|
+
* transactions and `2MB` — likely unintended.
|
|
129
|
+
* - **This function does NOT use `withRetry`.** Retrying a successful-but-
|
|
130
|
+
* acknowledgment-lost submission would double-grant the quota. Callers
|
|
131
|
+
* needing retry should wrap this function and use {@link checkAuthorization}
|
|
132
|
+
* to verify the post-state before retrying.
|
|
133
|
+
* - To "reset" a quota, let the existing authorization expire
|
|
134
|
+
* (`AuthorizationPeriod` blocks). The next call after expiry creates a fresh
|
|
135
|
+
* authorization rather than adding.
|
|
136
|
+
*
|
|
137
|
+
* Note: `AuthorizationScope::Preimage` uses `set` semantics in the same
|
|
138
|
+
* pallet. This helper is for account-scope authorization only.
|
|
139
|
+
*
|
|
140
|
+
* @param api - Typed Bulletin Chain API instance.
|
|
141
|
+
* @param who - SS58-encoded account to authorize.
|
|
142
|
+
* @param transactions - Number of transactions to **add** to the account's allowance.
|
|
143
|
+
* @param bytes - Byte budget to **add** to the account's allowance.
|
|
144
|
+
* @param signer - Signer for the extrinsic. On `viaSudo: true` this must be the sudo key.
|
|
145
|
+
* @param options - Optional `viaSudo` flag plus standard submission controls.
|
|
146
|
+
* @returns Block hash where the extrinsic was included.
|
|
147
|
+
* @throws {ProductBulletinError} If `viaSudo: true` is requested but the chain has no `Sudo` pallet.
|
|
148
|
+
*
|
|
149
|
+
* @example Direct (account self-authorizes — local dev)
|
|
150
|
+
* ```ts
|
|
151
|
+
* import { authorizeAccount } from "@parity/product-sdk-bulletin";
|
|
152
|
+
*
|
|
153
|
+
* await authorizeAccount(api, address, 100, 100n * 1024n * 1024n, signer);
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @example Sudo-wrapped (managed test network)
|
|
157
|
+
* ```ts
|
|
158
|
+
* await authorizeAccount(api, userAddress, 100, 1_000_000n, sudoSigner, {
|
|
159
|
+
* viaSudo: true,
|
|
160
|
+
* });
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* @see {@link checkAuthorization} for the read counterpart.
|
|
164
|
+
* @see {@link BulletinClient.authorizeAccount} for the client method equivalent.
|
|
165
|
+
*/
|
|
166
|
+
export async function authorizeAccount(
|
|
167
|
+
api: BulletinApi,
|
|
168
|
+
who: string,
|
|
169
|
+
transactions: number,
|
|
170
|
+
bytes: bigint,
|
|
171
|
+
signer: PolkadotSigner,
|
|
172
|
+
options: AuthorizeAccountOptions = {},
|
|
173
|
+
): Promise<{ blockHash: string }> {
|
|
174
|
+
const { viaSudo = false, waitFor, timeoutMs, onStatus } = options;
|
|
175
|
+
|
|
176
|
+
// Single `as any` cast for the whole function. `BulletinApi` is upstream-
|
|
177
|
+
// typed as `TypedApi<any>` (see types.ts), so member access is loose by
|
|
178
|
+
// design. Same pattern as upload.ts:57 — narrowing it requires retyping
|
|
179
|
+
// BulletinApi against a bundled descriptor (out of scope here).
|
|
180
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
181
|
+
const apiTx = (api as any).tx;
|
|
182
|
+
|
|
183
|
+
log.info("authorizeAccount: building extrinsic", {
|
|
184
|
+
who,
|
|
185
|
+
transactions,
|
|
186
|
+
bytes: bytes.toString(),
|
|
187
|
+
viaSudo,
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
if (viaSudo && !apiTx.Sudo?.sudo) {
|
|
191
|
+
throw new ProductBulletinError(
|
|
192
|
+
"viaSudo: true requires the Sudo pallet, which is not available on this network. " +
|
|
193
|
+
"On production networks (Polkadot, Kusama), authorize_account requires governance or a different mechanism.",
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const authorizeTx = apiTx.TransactionStorage.authorize_account({
|
|
198
|
+
who,
|
|
199
|
+
transactions,
|
|
200
|
+
bytes,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const txToSubmit = viaSudo ? apiTx.Sudo.sudo({ call: authorizeTx.decodedCall }) : authorizeTx;
|
|
204
|
+
|
|
205
|
+
// NOTE: Intentionally NOT using `withRetry` here. `authorize_account` is
|
|
206
|
+
// additive (see JSDoc above), so a retry after a successful-but-lost
|
|
207
|
+
// submission would double-grant the quota. Caller-side retry must verify
|
|
208
|
+
// post-state via `checkAuthorization` first.
|
|
209
|
+
const result = await submitAndWatch(txToSubmit, signer, {
|
|
210
|
+
waitFor,
|
|
211
|
+
timeoutMs,
|
|
212
|
+
onStatus,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
log.info("authorizeAccount: included in block", {
|
|
216
|
+
who,
|
|
217
|
+
blockHash: result.block.hash,
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
return { blockHash: result.block.hash };
|
|
221
|
+
}
|
|
222
|
+
|
|
83
223
|
if (import.meta.vitest) {
|
|
84
224
|
const { describe, test, expect, vi } = import.meta.vitest;
|
|
85
225
|
|
|
@@ -188,4 +328,163 @@ if (import.meta.vitest) {
|
|
|
188
328
|
expect(arg.value).toBe("5GrwvaEF...");
|
|
189
329
|
});
|
|
190
330
|
});
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Mock factory for `authorizeAccount` tests.
|
|
334
|
+
*
|
|
335
|
+
* Mirrors the mocking style used in `upload.ts` — we don't mock
|
|
336
|
+
* `submitAndWatch` (the SDK helper) directly. Instead we let it call
|
|
337
|
+
* through to a fake `signSubmitAndWatch` on the api, which emits the
|
|
338
|
+
* lifecycle events `submitAndWatch` listens for.
|
|
339
|
+
*/
|
|
340
|
+
function createMockApiForAuthorize(blockHash = "0xblockhash") {
|
|
341
|
+
const fakeTx = {
|
|
342
|
+
decodedCall: { fakeCall: true } as unknown,
|
|
343
|
+
signSubmitAndWatch: vi.fn().mockReturnValue({
|
|
344
|
+
subscribe: (handlers: { next: (e: unknown) => void }) => {
|
|
345
|
+
queueMicrotask(() => {
|
|
346
|
+
handlers.next({ type: "signed", txHash: "0xtxhash" });
|
|
347
|
+
handlers.next({
|
|
348
|
+
type: "txBestBlocksState",
|
|
349
|
+
txHash: "0xtxhash",
|
|
350
|
+
found: true,
|
|
351
|
+
ok: true,
|
|
352
|
+
block: { hash: blockHash, number: 1, index: 0 },
|
|
353
|
+
events: [],
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
return { unsubscribe: vi.fn() };
|
|
357
|
+
},
|
|
358
|
+
}),
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
return {
|
|
362
|
+
api: {
|
|
363
|
+
tx: {
|
|
364
|
+
TransactionStorage: {
|
|
365
|
+
authorize_account: vi.fn().mockReturnValue(fakeTx),
|
|
366
|
+
},
|
|
367
|
+
Sudo: {
|
|
368
|
+
sudo: vi.fn().mockReturnValue(fakeTx),
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
},
|
|
372
|
+
fakeTx,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const mockSigner = {} as PolkadotSigner;
|
|
377
|
+
|
|
378
|
+
describe("authorizeAccount", () => {
|
|
379
|
+
test("direct path: calls TransactionStorage.authorize_account with the right params", async () => {
|
|
380
|
+
const { api } = createMockApiForAuthorize();
|
|
381
|
+
|
|
382
|
+
await authorizeAccount(
|
|
383
|
+
api as unknown as BulletinApi,
|
|
384
|
+
"5GrwvaEF...",
|
|
385
|
+
100,
|
|
386
|
+
1_000_000n,
|
|
387
|
+
mockSigner,
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
expect(api.tx.TransactionStorage.authorize_account).toHaveBeenCalledOnce();
|
|
391
|
+
const arg = api.tx.TransactionStorage.authorize_account.mock.calls[0][0];
|
|
392
|
+
expect(arg.who).toBe("5GrwvaEF...");
|
|
393
|
+
expect(arg.transactions).toBe(100);
|
|
394
|
+
expect(arg.bytes).toBe(1_000_000n);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
test("direct path: does NOT call Sudo.sudo when viaSudo is false (default)", async () => {
|
|
398
|
+
const { api } = createMockApiForAuthorize();
|
|
399
|
+
|
|
400
|
+
await authorizeAccount(
|
|
401
|
+
api as unknown as BulletinApi,
|
|
402
|
+
"5GrwvaEF...",
|
|
403
|
+
10,
|
|
404
|
+
100n,
|
|
405
|
+
mockSigner,
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
expect(api.tx.Sudo.sudo).not.toHaveBeenCalled();
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
test("direct path: returns the block hash from submission", async () => {
|
|
412
|
+
const { api } = createMockApiForAuthorize("0xdeadbeef");
|
|
413
|
+
|
|
414
|
+
const result = await authorizeAccount(
|
|
415
|
+
api as unknown as BulletinApi,
|
|
416
|
+
"5GrwvaEF...",
|
|
417
|
+
10,
|
|
418
|
+
100n,
|
|
419
|
+
mockSigner,
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
expect(result).toEqual({ blockHash: "0xdeadbeef" });
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
test("sudo path: wraps the authorize_account call inside Sudo.sudo", async () => {
|
|
426
|
+
const { api, fakeTx } = createMockApiForAuthorize();
|
|
427
|
+
|
|
428
|
+
await authorizeAccount(
|
|
429
|
+
api as unknown as BulletinApi,
|
|
430
|
+
"5GrwvaEF...",
|
|
431
|
+
10,
|
|
432
|
+
100n,
|
|
433
|
+
mockSigner,
|
|
434
|
+
{ viaSudo: true },
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
expect(api.tx.Sudo.sudo).toHaveBeenCalledOnce();
|
|
438
|
+
const sudoArg = api.tx.Sudo.sudo.mock.calls[0][0];
|
|
439
|
+
expect(sudoArg.call).toBe(fakeTx.decodedCall);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
test("sudo path: still returns the block hash from the sudo extrinsic", async () => {
|
|
443
|
+
const { api } = createMockApiForAuthorize("0xsudoblock");
|
|
444
|
+
|
|
445
|
+
const result = await authorizeAccount(
|
|
446
|
+
api as unknown as BulletinApi,
|
|
447
|
+
"5GrwvaEF...",
|
|
448
|
+
10,
|
|
449
|
+
100n,
|
|
450
|
+
mockSigner,
|
|
451
|
+
{ viaSudo: true },
|
|
452
|
+
);
|
|
453
|
+
|
|
454
|
+
expect(result).toEqual({ blockHash: "0xsudoblock" });
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
test("throws ProductBulletinError when viaSudo is true but the chain lacks a Sudo pallet", async () => {
|
|
458
|
+
const apiWithoutSudo = {
|
|
459
|
+
tx: {
|
|
460
|
+
TransactionStorage: {
|
|
461
|
+
authorize_account: vi.fn().mockReturnValue({
|
|
462
|
+
decodedCall: {} as unknown,
|
|
463
|
+
signSubmitAndWatch: vi.fn(),
|
|
464
|
+
}),
|
|
465
|
+
},
|
|
466
|
+
// Sudo intentionally absent — represents production Polkadot/Kusama
|
|
467
|
+
},
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
const err = await authorizeAccount(
|
|
471
|
+
apiWithoutSudo as unknown as BulletinApi,
|
|
472
|
+
"5GrwvaEF...",
|
|
473
|
+
10,
|
|
474
|
+
100n,
|
|
475
|
+
mockSigner,
|
|
476
|
+
{ viaSudo: true },
|
|
477
|
+
).catch((e: unknown) => e);
|
|
478
|
+
|
|
479
|
+
expect(err).toBeInstanceOf(ProductBulletinError);
|
|
480
|
+
expect((err as Error).message).toMatch(/Sudo pallet/i);
|
|
481
|
+
// Verify we did NOT proceed to submit anything
|
|
482
|
+
expect(apiWithoutSudo.tx.TransactionStorage.authorize_account).not.toHaveBeenCalled();
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
// Note: submission-failure propagation is tested upstream in
|
|
486
|
+
// @parity/product-sdk-tx's own suite. Re-testing it here would just
|
|
487
|
+
// re-test submitAndWatch's error path through a brittle mock, which
|
|
488
|
+
// depends on internal event-shape details outside this file's contract.
|
|
489
|
+
});
|
|
191
490
|
}
|