77import type { DecdnClient } from "./client.js" ;
88import { DecdnError } from "./errors.js" ;
99import { parseWithBigInts , sendOk } from "./http.js" ;
10+ import { integer } from "./pointer.js" ;
11+
12+ /**
13+ * The content a registered capability pays for: `"any"`, or only the
14+ * content of `namespaces`. A namespace scope carries the pool owner's EIP-712
15+ * `CapabilityScope(address signer,bytes32 poolId,uint256[] namespaces)`
16+ * signature (domain `decdn-capability-scope`, version `1`, the chain id, and
17+ * the PaymentPool as verifying contract), which the SDK passes through.
18+ */
19+ export type CapabilityScope =
20+ | "any"
21+ | {
22+ /** Namespace ids, each at least 1; at least one. */
23+ namespaces : readonly ( bigint | number ) [ ] ;
24+ /** `0x` + the 65-byte signature, in hex. */
25+ signature : string ;
26+ } ;
1027
1128/** A capability the gateway holds. */
1229export interface RegisteredCapability {
1330 /** The spending cap, in USDC base units. */
1431 cap : bigint ;
1532 /** Unix-seconds expiry. */
1633 expiry : bigint ;
34+ /** The namespaces a namespace-scoped capability pays for; absent when it pays for any content. */
35+ namespaces ?: bigint [ ] ;
1736 pool : string ;
1837 signer : string ;
1938 /** Whether a pull stopped for funding on it. */
@@ -24,6 +43,8 @@ export interface RegisteredCapability {
2443export interface Registration {
2544 /** Unix-seconds expiry. */
2645 expiry : bigint ;
46+ /** The namespaces a namespace-scoped capability pays for; absent when it pays for any content. */
47+ namespaces ?: bigint [ ] ;
2748 pool : string ;
2849 signer : string ;
2950}
@@ -35,7 +56,7 @@ export class LoopbackApi {
3556 private async call (
3657 method : string ,
3758 path : string ,
38- body ?: unknown ,
59+ body ?: string ,
3960 ) : Promise < string > {
4061 if ( this . client . context !== "loopback" ) {
4162 throw new DecdnError (
@@ -45,7 +66,7 @@ export class LoopbackApi {
4566 }
4667 const init : RequestInit = { method } ;
4768 if ( body !== undefined ) {
48- init . body = JSON . stringify ( body ) ;
69+ init . body = body ;
4970 init . headers = { "content-type" : "application/json" } ;
5071 }
5172 const response = await sendOk (
@@ -69,18 +90,34 @@ export class LoopbackApi {
6990 }
7091
7192 /**
72- * Register `capability`, a `dcap1:` issued to a key the gateway holds. A key
73- * holds one capability per pool. `CONFLICT` when the gateway does not hold
74- * the signer's key, or that key already holds a capability from that pool.
93+ * Register `capability`, a `dcap1:` issued to a key the gateway holds, to
94+ * pay for the content `scope` names. A key holds one capability per pool.
95+ * `CONFLICT` when the gateway does not hold the signer's key, or that key
96+ * already holds a capability from that pool; `UNPROCESSABLE` for a scope
97+ * the gateway refuses, among them a namespace scope whose signature does
98+ * not recover the pool's owner.
7599 */
76100 async registerCapability ( args : {
77101 capability : string ;
102+ scope : CapabilityScope ;
78103 } ) : Promise < Registration > {
79- const text = await this . call ( "PUT" , "/_capability" , {
80- capability : args . capability ,
81- } ) ;
82- const parsed = parseWithBigInts ( text , [ "expiry" ] ) as Registration ;
83- return { signer : parsed . signer , pool : parsed . pool , expiry : parsed . expiry } ;
104+ const text = await this . call (
105+ "PUT" ,
106+ "/_capability" ,
107+ `{"capability":${ JSON . stringify ( args . capability ) } ,"scope":${ scopeJson ( args . scope ) } }` ,
108+ ) ;
109+ const parsed = parseWithBigInts ( text , [
110+ "expiry" ,
111+ "namespaces" ,
112+ ] ) as Registration ;
113+ const registration : Registration = {
114+ signer : parsed . signer ,
115+ pool : parsed . pool ,
116+ expiry : parsed . expiry ,
117+ } ;
118+ if ( parsed . namespaces !== undefined )
119+ registration . namespaces = parsed . namespaces ;
120+ return registration ;
84121 }
85122
86123 /**
@@ -89,6 +126,27 @@ export class LoopbackApi {
89126 */
90127 async capabilities ( ) : Promise < RegisteredCapability [ ] > {
91128 const text = await this . call ( "GET" , "/_capability" ) ;
92- return parseWithBigInts ( text , [ "cap" , "expiry" ] ) as RegisteredCapability [ ] ;
129+ return parseWithBigInts ( text , [
130+ "cap" ,
131+ "expiry" ,
132+ "namespaces" ,
133+ ] ) as RegisteredCapability [ ] ;
134+ }
135+ }
136+
137+ /** `scope` as the JSON `PUT /_capability` carries, its namespace ids as integers. */
138+ function scopeJson ( scope : CapabilityScope ) : string {
139+ if ( scope === "any" ) return '"any"' ;
140+ if (
141+ typeof scope !== "object" ||
142+ scope === null ||
143+ ! Array . isArray ( scope . namespaces )
144+ ) {
145+ throw new DecdnError (
146+ "CONFIG" ,
147+ 'a capability scope is "any" or { namespaces, signature }' ,
148+ ) ;
93149 }
150+ const ids = scope . namespaces . map ( ( n ) => integer ( n , "a namespace id" ) ) ;
151+ return `{"namespaces":[${ ids . join ( "," ) } ],"signature":${ JSON . stringify ( scope . signature ) } }` ;
94152}
0 commit comments