@keetanetwork/anchor 0.0.113 → 0.0.115

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 (51) hide show
  1. package/lib/http-server/index.d.ts +12 -2
  2. package/lib/http-server/index.d.ts.map +1 -1
  3. package/lib/http-server/index.js +11 -2
  4. package/lib/http-server/index.js.map +1 -1
  5. package/lib/http-server/replay-guard-postgres.d.ts +41 -0
  6. package/lib/http-server/replay-guard-postgres.d.ts.map +1 -0
  7. package/lib/http-server/replay-guard-postgres.js +113 -0
  8. package/lib/http-server/replay-guard-postgres.js.map +1 -0
  9. package/lib/http-server/replay-guard-redis.d.ts +29 -0
  10. package/lib/http-server/replay-guard-redis.d.ts.map +1 -0
  11. package/lib/http-server/replay-guard-redis.js +23 -0
  12. package/lib/http-server/replay-guard-redis.js.map +1 -0
  13. package/lib/http-server/replay-guard.d.ts +2 -2
  14. package/lib/http-server/replay-guard.js.map +1 -1
  15. package/lib/http-server/request-signing.d.ts +19 -2
  16. package/lib/http-server/request-signing.d.ts.map +1 -1
  17. package/lib/http-server/request-signing.js +53 -4
  18. package/lib/http-server/request-signing.js.map +1 -1
  19. package/lib/resolver.generated.js +33 -33
  20. package/lib/utils/signing.d.ts +1 -1
  21. package/lib/utils/signing.d.ts.map +1 -1
  22. package/lib/utils/signing.js +5 -5
  23. package/lib/utils/signing.js.map +1 -1
  24. package/npm-shrinkwrap.json +2 -2
  25. package/package.json +1 -1
  26. package/services/asset-movement/common.d.ts +1 -1
  27. package/services/asset-movement/common.d.ts.map +1 -1
  28. package/services/asset-movement/common.generated.js +1322 -1284
  29. package/services/asset-movement/common.generated.js.map +1 -1
  30. package/services/asset-movement/common.js.map +1 -1
  31. package/services/asset-movement/common.server.generated.js +12267 -10065
  32. package/services/asset-movement/common.server.generated.js.map +1 -1
  33. package/services/asset-movement/lib/data/addresses/bank-account/bre-b.d.ts +4 -0
  34. package/services/asset-movement/lib/data/addresses/bank-account/bre-b.d.ts.map +1 -0
  35. package/services/asset-movement/lib/data/addresses/bank-account/bre-b.js +42 -0
  36. package/services/asset-movement/lib/data/addresses/bank-account/bre-b.js.map +1 -0
  37. package/services/asset-movement/lib/data/addresses/bank-account/co.d.ts +9 -0
  38. package/services/asset-movement/lib/data/addresses/bank-account/co.d.ts.map +1 -1
  39. package/services/asset-movement/lib/data/addresses/bank-account/co.js +15 -8
  40. package/services/asset-movement/lib/data/addresses/bank-account/co.js.map +1 -1
  41. package/services/asset-movement/lib/data/addresses/bank-account/index.generated.d.ts +2 -0
  42. package/services/asset-movement/lib/data/addresses/bank-account/index.generated.d.ts.map +1 -1
  43. package/services/asset-movement/lib/data/addresses/bank-account/index.generated.js +2 -0
  44. package/services/asset-movement/lib/data/addresses/bank-account/index.generated.js.map +1 -1
  45. package/services/asset-movement/lib/data/addresses/types.generated.d.ts +122 -2
  46. package/services/asset-movement/lib/data/addresses/types.generated.d.ts.map +1 -1
  47. package/services/asset-movement/lib/data/addresses/types.generated.js +126 -0
  48. package/services/asset-movement/lib/data/addresses/types.generated.js.map +1 -1
  49. package/services/asset-movement/lib/location.generated.js +3 -3
  50. package/services/storage/clients/contacts.generated.js +730 -538
  51. package/services/storage/clients/contacts.generated.js.map +1 -1
@@ -18,6 +18,12 @@ export interface RequestTarget {
18
18
  */
19
19
  export interface RequestAuthContext extends RequestTarget {
20
20
  replayGuard: KeetaAnchorReplayGuard;
21
+ /**
22
+ * Accept a signature that covers only the operation's data, as
23
+ * clients before request binding make. Such a signature names no
24
+ * anchor, method or body. Defaults to `true`.
25
+ */
26
+ acceptLegacySignatures?: boolean | undefined;
21
27
  }
22
28
  /**
23
29
  * Collapse leading slashes in a request path, as the server does before
@@ -30,14 +36,25 @@ export declare function normalizeRequestPath(path: string): string;
30
36
  */
31
37
  export declare function publicRequestURL(serverURL: string, requestURL: URL): URL;
32
38
  /**
33
- * Sign `data` for one request.
39
+ * Sign `data` for one request, in a form both kinds of anchor verify.
40
+ *
41
+ * The v2 signature covers the request (see {@link requestSignable}) and
42
+ * rides in the nonce, after {@link DUAL_NONCE_SEPARATOR}. The
43
+ * `signature` field is the legacy signature of `data` alone, made over
44
+ * that whole nonce, so an anchor that predates request binding verifies
45
+ * it unchanged, and the v2 part cannot be removed without breaking it.
34
46
  */
35
47
  export declare function signRequest(account: SignableAccount, target: RequestTarget, data: Signable): Promise<HTTPSignedField>;
36
48
  /**
37
49
  * Check that a request signature covers this request, without spending
38
50
  * its nonce. Resolves `false` when it does not.
51
+ *
52
+ * A dual signature must hold in both parts, and its v2 part names the
53
+ * request. A plain signature is accepted when it covers the request, or,
54
+ * unless `context.acceptLegacySignatures` is `false`, when it covers
55
+ * `data` alone, as clients before request binding sign.
39
56
  */
40
- export declare function verifyRequestSignature(account: VerifiableAccount, context: RequestTarget, data: Signable, signed: HTTPSignedField): Promise<boolean>;
57
+ export declare function verifyRequestSignature(account: VerifiableAccount, context: RequestTarget & Pick<RequestAuthContext, 'acceptLegacySignatures'>, data: Signable, signed: HTTPSignedField): Promise<boolean>;
41
58
  /**
42
59
  * Spend the nonce of a verified request signature. Throws
43
60
  * `KeetaAnchorUserError` when the replay guard refuses it: the nonce was
@@ -1 +1 @@
1
- {"version":3,"file":"request-signing.d.ts","sourceRoot":"","sources":["../../../src/lib/http-server/request-signing.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAWhE;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,GAAG,CAAC;IACT,IAAI,CAAC,EAAE,OAAO,CAAC;CACf;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAmB,SAAQ,aAAa;IACxD,WAAW,EAAE,sBAA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 * Joins the request nonce and the v2 request signature inside the nonce
12
+ * of a dual signature. Neither a UUID nor base64 contains it.
13
+ */
14
+ const DUAL_NONCE_SEPARATOR = '~v2~';
10
15
  /**
11
16
  * Collapse leading slashes in a request path, as the server does before
12
17
  * routing, so `//api/x` and `/api/x` name the same operation.
@@ -82,14 +87,41 @@ export function publicRequestURL(serverURL, requestURL) {
82
87
  return (new URL(`${requestURL.pathname}${requestURL.search}`, serverURL));
83
88
  }
84
89
  /**
85
- * Sign `data` for one request.
90
+ * Sign `data` for one request, in a form both kinds of anchor verify.
91
+ *
92
+ * The v2 signature covers the request (see {@link requestSignable}) and
93
+ * rides in the nonce, after {@link DUAL_NONCE_SEPARATOR}. The
94
+ * `signature` field is the legacy signature of `data` alone, made over
95
+ * that whole nonce, so an anchor that predates request binding verifies
96
+ * it unchanged, and the v2 part cannot be removed without breaking it.
86
97
  */
87
98
  export async function signRequest(account, target, data) {
88
- return (await SignData(account, requestSignable(target, data)));
99
+ const request = await SignData(account, requestSignable(target, data));
100
+ return (await SignData(account, data, `${request.nonce}${DUAL_NONCE_SEPARATOR}${request.signature}`, request.timestamp));
101
+ }
102
+ /**
103
+ * The v2 request signature carried in a dual signature's nonce, or
104
+ * `undefined` when `signed` is not a dual signature
105
+ */
106
+ function requestSignatureOf(signed) {
107
+ const separatorAt = signed.nonce.indexOf(DUAL_NONCE_SEPARATOR);
108
+ if (separatorAt < 0) {
109
+ return (undefined);
110
+ }
111
+ return ({
112
+ nonce: signed.nonce.slice(0, separatorAt),
113
+ timestamp: signed.timestamp,
114
+ signature: signed.nonce.slice(separatorAt + DUAL_NONCE_SEPARATOR.length)
115
+ });
89
116
  }
90
117
  /**
91
118
  * Check that a request signature covers this request, without spending
92
119
  * its nonce. Resolves `false` when it does not.
120
+ *
121
+ * A dual signature must hold in both parts, and its v2 part names the
122
+ * request. A plain signature is accepted when it covers the request, or,
123
+ * unless `context.acceptLegacySignatures` is `false`, when it covers
124
+ * `data` alone, as clients before request binding sign.
93
125
  */
94
126
  export async function verifyRequestSignature(account, context, data, signed) {
95
127
  let signable;
@@ -100,7 +132,22 @@ export async function verifyRequestSignature(account, context, data, signed) {
100
132
  /* A body that cannot be canonicalized cannot have been signed */
101
133
  return (false);
102
134
  }
103
- return (await VerifySignedData(account, signable, signed, { maxSkewMs: DEFAULT_MAX_SKEW_MS }));
135
+ const options = { maxSkewMs: DEFAULT_MAX_SKEW_MS };
136
+ const requestSignature = requestSignatureOf(signed);
137
+ if (requestSignature !== undefined) {
138
+ /* Both parts must hold, so a dual signature is never half valid */
139
+ if (!await VerifySignedData(account, signable, requestSignature, options)) {
140
+ return (false);
141
+ }
142
+ return (await VerifySignedData(account, data, signed, options));
143
+ }
144
+ if (await VerifySignedData(account, signable, signed, options)) {
145
+ return (true);
146
+ }
147
+ if (context.acceptLegacySignatures === false) {
148
+ return (false);
149
+ }
150
+ return (await VerifySignedData(account, data, signed, options));
104
151
  }
105
152
  /**
106
153
  * Spend the nonce of a verified request signature. Throws
@@ -109,7 +156,9 @@ export async function verifyRequestSignature(account, context, data, signed) {
109
156
  */
110
157
  export async function claimRequestNonce(account, context, signed) {
111
158
  const expiresAt = new Date(new Date(signed.timestamp).valueOf() + DEFAULT_MAX_SKEW_MS);
112
- const claimed = await context.replayGuard.claim(`request:${account.publicKeyString.get()}:${signed.nonce}`, expiresAt);
159
+ /* Both parts of a dual signature spend the one request nonce */
160
+ const nonce = requestSignatureOf(signed)?.nonce ?? signed.nonce;
161
+ const claimed = await context.replayGuard.claim(`request:${account.publicKeyString.get()}:${nonce}`, expiresAt);
113
162
  if (!claimed) {
114
163
  throw (new KeetaAnchorUserError('Signed request refused: its nonce was already used, or the server cannot accept more signed requests now'));
115
164
  }
@@ -1 +1 @@
1
- {"version":3,"file":"request-signing.js","sourceRoot":"","sources":["../../../src/lib/http-server/request-signing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAInE,OAAO,EAAE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,OAAO,EAAE,GAAG,mBAAmB,CAAC,CAAC;IACvF,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,WAAW,OAAO,CAAC,eAAe,CAAC,GAAG,EAAE,IAAI,MAAM,CAAC,KAAK,EAAE,EAAE,SAAS,CAAC,CAAC;IACvH,IAAI,CAAC,OAAO,EAAE,CAAC;QACd,MAAK,CAAC,IAAI,oBAAoB,CAAC,0GAA0G,CAAC,CAAC,CAAC;IAC7I,CAAC;AACF,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,OAA0B,EAAE,OAA2B,EAAE,IAAc,EAAE,MAAuB;IACzI,MAAM,KAAK,GAAG,MAAM,sBAAsB,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IAC3E,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,OAAM,CAAC,KAAK,CAAC,CAAC;IACf,CAAC;IAED,MAAM,iBAAiB,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAElD,OAAM,CAAC,IAAI,CAAC,CAAC;AACd,CAAC","sourcesContent":["import { lib as KeetaNetLib } from '@keetanetwork/keetanet-client';\nimport type { Signable, SignableAccount, VerifiableAccount } from '../utils/signing.js';\nimport type { HTTPSignedField } from './common.js';\nimport type { KeetaAnchorReplayGuard } from './replay-guard.js';\nimport { Buffer } from '../utils/buffer.js';\nimport { KeetaAnchorUserError } from '../error.js';\nimport { DEFAULT_MAX_SKEW_MS, SignData, VerifySignedData, canonicalizeJson } from '../utils/signing.js';\n\n/**\n * Version tag of the request signable layout. A signature made for\n * another layout never verifies.\n */\nconst REQUEST_SIGNABLE_TAG = 'keeta-anchor-request-v1';\n\n/**\n * The request a signature is made for: the operation URL as the client\n * calls it, and the body. A JSON body is given as the value passed to\n * `JSON.stringify` (its `signed` field is ignored), and a raw body as a\n * `Buffer`.\n */\nexport interface RequestTarget {\n\tmethod: string;\n\turl: URL;\n\tbody?: unknown;\n}\n\n/**\n * What a server needs to verify a signed request: the request as seen\n * at the server's public URL, and where it records spent nonces.\n */\nexport interface RequestAuthContext extends RequestTarget {\n\treplayGuard: KeetaAnchorReplayGuard;\n}\n\n/**\n * Collapse leading slashes in a request path, as the server does before\n * routing, so `//api/x` and `/api/x` name the same operation.\n */\nexport function normalizeRequestPath(path: string): string {\n\treturn(path.replace(/^\\/+/, '/'));\n}\n\n/**\n * The URL a signature covers: origin, path, and every query parameter\n * except the signature and the signer, in sorted order.\n */\nfunction canonicalRequestURL(url: URL): string {\n\tconst params = [ ...url.searchParams ].filter(function([ key ]) {\n\t\treturn(key !== 'account' && !key.startsWith('signed.'));\n\t}).sort(function([ a ], [ b ]) {\n\t\tif (a === b) {\n\t\t\treturn(0);\n\t\t}\n\n\t\treturn(a < b ? -1 : 1);\n\t});\n\n\tlet query = '';\n\tif (params.length > 0) {\n\t\tquery = `?${new URLSearchParams(params).toString()}`;\n\t}\n\n\treturn(`${url.origin}${normalizeRequestPath(url.pathname)}${query}`);\n}\n\n/**\n * Hash of the body a signature covers. A JSON body is hashed in the form\n * the server parses, without its top-level `signed` field.\n */\nfunction requestBodyDigest(body: RequestTarget['body']): string {\n\tif (body === undefined) {\n\t\treturn('');\n\t}\n\n\tlet bytes: Buffer;\n\tif (Buffer.isBuffer(body)) {\n\t\tbytes = body;\n\t} else {\n\t\t/* The server sees the body as it parses from the wire */\n\t\tconst wire = JSON.stringify(body);\n\t\tconst parsed: unknown = JSON.parse(wire);\n\t\tif (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed) && 'signed' in parsed) {\n\t\t\tconst unsigned = Object.entries(parsed).filter(function([ key ]) {\n\t\t\t\treturn(key !== 'signed');\n\t\t\t});\n\t\t\tbytes = Buffer.from(canonicalizeJson(Object.fromEntries(unsigned)), 'utf-8');\n\t\t} else {\n\t\t\tbytes = Buffer.from(canonicalizeJson(parsed), 'utf-8');\n\t\t}\n\t}\n\n\treturn(Buffer.from(KeetaNetLib.Utils.Hash.Hash(bytes)).toString('hex'));\n}\n\n/**\n * Bind `data` to one request: the method, the URL (which names the\n * anchor and the operation), and the body.\n */\nfunction requestSignable(target: RequestTarget, data: Signable): Signable {\n\treturn([\n\t\tREQUEST_SIGNABLE_TAG,\n\t\ttarget.method.toUpperCase(),\n\t\tcanonicalRequestURL(target.url),\n\t\trequestBodyDigest(target.body),\n\t\t...data\n\t]);\n}\n\n/**\n * Rebase a request URL onto the server's configured public URL, so the\n * audience never comes from the Host header.\n */\nexport function publicRequestURL(serverURL: string, requestURL: URL): URL {\n\treturn(new URL(`${requestURL.pathname}${requestURL.search}`, serverURL));\n}\n\n/**\n * Sign `data` for one request.\n */\nexport async function signRequest(account: SignableAccount, target: RequestTarget, data: Signable): Promise<HTTPSignedField> {\n\treturn(await SignData(account, requestSignable(target, data)));\n}\n\n/**\n * Check that a request signature covers this request, without spending\n * its nonce. Resolves `false` when it does not.\n */\nexport async function verifyRequestSignature(account: VerifiableAccount, context: RequestTarget, data: Signable, signed: HTTPSignedField): Promise<boolean> {\n\tlet signable: Signable;\n\ttry {\n\t\tsignable = requestSignable(context, data);\n\t} catch {\n\t\t/* A body that cannot be canonicalized cannot have been signed */\n\t\treturn(false);\n\t}\n\n\treturn(await VerifySignedData(account, signable, signed, { maxSkewMs: DEFAULT_MAX_SKEW_MS }));\n}\n\n/**\n * Spend the nonce of a verified request signature. Throws\n * `KeetaAnchorUserError` when the replay guard refuses it: the nonce was\n * already used, or the guard cannot record more claims now.\n */\nexport async function claimRequestNonce(account: VerifiableAccount, context: RequestAuthContext, signed: HTTPSignedField): Promise<void> {\n\tconst expiresAt = new Date(new Date(signed.timestamp).valueOf() + DEFAULT_MAX_SKEW_MS);\n\tconst claimed = await context.replayGuard.claim(`request:${account.publicKeyString.get()}:${signed.nonce}`, expiresAt);\n\tif (!claimed) {\n\t\tthrow(new KeetaAnchorUserError('Signed request refused: its nonce was already used, or the server cannot accept more signed requests now'));\n\t}\n}\n\n/**\n * Verify a request signature, then spend its nonce. Resolves `false`\n * when the signature does not cover this request, and throws as\n * {@link claimRequestNonce} does when the nonce is refused.\n */\nexport async function verifySignedRequest(account: VerifiableAccount, context: RequestAuthContext, data: Signable, signed: HTTPSignedField): Promise<boolean> {\n\tconst valid = await verifyRequestSignature(account, context, data, signed);\n\tif (!valid) {\n\t\treturn(false);\n\t}\n\n\tawait claimRequestNonce(account, context, signed);\n\n\treturn(true);\n}\n"]}
1
+ {"version":3,"file":"request-signing.js","sourceRoot":"","sources":["../../../src/lib/http-server/request-signing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAInE,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAExG;;;GAGG;AACH,MAAM,oBAAoB,GAAG,yBAAyB,CAAC;AAEvD;;;GAGG;AACH,MAAM,oBAAoB,GAAG,MAAM,CAAC;AA6BpC;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,IAAY;IAChD,OAAM,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;AACnC,CAAC;AAED;;;GAGG;AACH,SAAS,mBAAmB,CAAC,GAAQ;IACpC,MAAM,MAAM,GAAG,CAAE,GAAG,GAAG,CAAC,YAAY,CAAE,CAAC,MAAM,CAAC,UAAS,CAAE,GAAG,CAAE;QAC7D,OAAM,CAAC,GAAG,KAAK,SAAS,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC,IAAI,CAAC,UAAS,CAAE,CAAC,CAAE,EAAE,CAAE,CAAC,CAAE;QAC5B,IAAI,CAA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AK,EAAE,CAAC;QACZ,OAAM,CAAC,KAAK,CAAC,CAAC;IACf,CAAC;IAED,MAAM,iBAAiB,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAElD,OAAM,CAAC,IAAI,CAAC,CAAC;AACd,CAAC","sourcesContent":["import { lib as KeetaNetLib } from '@keetanetwork/keetanet-client';\nimport type { Signable, SignableAccount, VerifiableAccount } from '../utils/signing.js';\nimport type { HTTPSignedField } from './common.js';\nimport type { KeetaAnchorReplayGuard } from './replay-guard.js';\nimport { Buffer } from '../utils/buffer.js';\nimport { KeetaAnchorUserError } from '../error.js';\nimport { DEFAULT_MAX_SKEW_MS, SignData, VerifySignedData, canonicalizeJson } from '../utils/signing.js';\n\n/**\n * Version tag of the request signable layout. A signature made for\n * another layout never verifies.\n */\nconst REQUEST_SIGNABLE_TAG = 'keeta-anchor-request-v1';\n\n/**\n * Joins the request nonce and the v2 request signature inside the nonce\n * of a dual signature. Neither a UUID nor base64 contains it.\n */\nconst DUAL_NONCE_SEPARATOR = '~v2~';\n\n/**\n * The request a signature is made for: the operation URL as the client\n * calls it, and the body. A JSON body is given as the value passed to\n * `JSON.stringify` (its `signed` field is ignored), and a raw body as a\n * `Buffer`.\n */\nexport interface RequestTarget {\n\tmethod: string;\n\turl: URL;\n\tbody?: unknown;\n}\n\n/**\n * What a server needs to verify a signed request: the request as seen\n * at the server's public URL, and where it records spent nonces.\n */\nexport interface RequestAuthContext extends RequestTarget {\n\treplayGuard: KeetaAnchorReplayGuard;\n\n\t/**\n\t * Accept a signature that covers only the operation's data, as\n\t * clients before request binding make. Such a signature names no\n\t * anchor, method or body. Defaults to `true`.\n\t */\n\tacceptLegacySignatures?: boolean | undefined;\n}\n\n/**\n * Collapse leading slashes in a request path, as the server does before\n * routing, so `//api/x` and `/api/x` name the same operation.\n */\nexport function normalizeRequestPath(path: string): string {\n\treturn(path.replace(/^\\/+/, '/'));\n}\n\n/**\n * The URL a signature covers: origin, path, and every query parameter\n * except the signature and the signer, in sorted order.\n */\nfunction canonicalRequestURL(url: URL): string {\n\tconst params = [ ...url.searchParams ].filter(function([ key ]) {\n\t\treturn(key !== 'account' && !key.startsWith('signed.'));\n\t}).sort(function([ a ], [ b ]) {\n\t\tif (a === b) {\n\t\t\treturn(0);\n\t\t}\n\n\t\treturn(a < b ? -1 : 1);\n\t});\n\n\tlet query = '';\n\tif (params.length > 0) {\n\t\tquery = `?${new URLSearchParams(params).toString()}`;\n\t}\n\n\treturn(`${url.origin}${normalizeRequestPath(url.pathname)}${query}`);\n}\n\n/**\n * Hash of the body a signature covers. A JSON body is hashed in the form\n * the server parses, without its top-level `signed` field.\n */\nfunction requestBodyDigest(body: RequestTarget['body']): string {\n\tif (body === undefined) {\n\t\treturn('');\n\t}\n\n\tlet bytes: Buffer;\n\tif (Buffer.isBuffer(body)) {\n\t\tbytes = body;\n\t} else {\n\t\t/* The server sees the body as it parses from the wire */\n\t\tconst wire = JSON.stringify(body);\n\t\tconst parsed: unknown = JSON.parse(wire);\n\t\tif (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed) && 'signed' in parsed) {\n\t\t\tconst unsigned = Object.entries(parsed).filter(function([ key ]) {\n\t\t\t\treturn(key !== 'signed');\n\t\t\t});\n\t\t\tbytes = Buffer.from(canonicalizeJson(Object.fromEntries(unsigned)), 'utf-8');\n\t\t} else {\n\t\t\tbytes = Buffer.from(canonicalizeJson(parsed), 'utf-8');\n\t\t}\n\t}\n\n\treturn(Buffer.from(KeetaNetLib.Utils.Hash.Hash(bytes)).toString('hex'));\n}\n\n/**\n * Bind `data` to one request: the method, the URL (which names the\n * anchor and the operation), and the body.\n */\nfunction requestSignable(target: RequestTarget, data: Signable): Signable {\n\treturn([\n\t\tREQUEST_SIGNABLE_TAG,\n\t\ttarget.method.toUpperCase(),\n\t\tcanonicalRequestURL(target.url),\n\t\trequestBodyDigest(target.body),\n\t\t...data\n\t]);\n}\n\n/**\n * Rebase a request URL onto the server's configured public URL, so the\n * audience never comes from the Host header.\n */\nexport function publicRequestURL(serverURL: string, requestURL: URL): URL {\n\treturn(new URL(`${requestURL.pathname}${requestURL.search}`, serverURL));\n}\n\n/**\n * Sign `data` for one request, in a form both kinds of anchor verify.\n *\n * The v2 signature covers the request (see {@link requestSignable}) and\n * rides in the nonce, after {@link DUAL_NONCE_SEPARATOR}. The\n * `signature` field is the legacy signature of `data` alone, made over\n * that whole nonce, so an anchor that predates request binding verifies\n * it unchanged, and the v2 part cannot be removed without breaking it.\n */\nexport async function signRequest(account: SignableAccount, target: RequestTarget, data: Signable): Promise<HTTPSignedField> {\n\tconst request = await SignData(account, requestSignable(target, data));\n\n\treturn(await SignData(account, data, `${request.nonce}${DUAL_NONCE_SEPARATOR}${request.signature}`, request.timestamp));\n}\n\n/**\n * The v2 request signature carried in a dual signature's nonce, or\n * `undefined` when `signed` is not a dual signature\n */\nfunction requestSignatureOf(signed: HTTPSignedField): HTTPSignedField | undefined {\n\tconst separatorAt = signed.nonce.indexOf(DUAL_NONCE_SEPARATOR);\n\tif (separatorAt < 0) {\n\t\treturn(undefined);\n\t}\n\n\treturn({\n\t\tnonce: signed.nonce.slice(0, separatorAt),\n\t\ttimestamp: signed.timestamp,\n\t\tsignature: signed.nonce.slice(separatorAt + DUAL_NONCE_SEPARATOR.length)\n\t});\n}\n\n/**\n * Check that a request signature covers this request, without spending\n * its nonce. Resolves `false` when it does not.\n *\n * A dual signature must hold in both parts, and its v2 part names the\n * request. A plain signature is accepted when it covers the request, or,\n * unless `context.acceptLegacySignatures` is `false`, when it covers\n * `data` alone, as clients before request binding sign.\n */\nexport async function verifyRequestSignature(account: VerifiableAccount, context: RequestTarget & Pick<RequestAuthContext, 'acceptLegacySignatures'>, data: Signable, signed: HTTPSignedField): Promise<boolean> {\n\tlet signable: Signable;\n\ttry {\n\t\tsignable = requestSignable(context, data);\n\t} catch {\n\t\t/* A body that cannot be canonicalized cannot have been signed */\n\t\treturn(false);\n\t}\n\n\tconst options = { maxSkewMs: DEFAULT_MAX_SKEW_MS };\n\tconst requestSignature = requestSignatureOf(signed);\n\tif (requestSignature !== undefined) {\n\t\t/* Both parts must hold, so a dual signature is never half valid */\n\t\tif (!await VerifySignedData(account, signable, requestSignature, options)) {\n\t\t\treturn(false);\n\t\t}\n\n\t\treturn(await VerifySignedData(account, data, signed, options));\n\t}\n\n\tif (await VerifySignedData(account, signable, signed, options)) {\n\t\treturn(true);\n\t}\n\n\tif (context.acceptLegacySignatures === false) {\n\t\treturn(false);\n\t}\n\n\treturn(await VerifySignedData(account, data, signed, options));\n}\n\n/**\n * Spend the nonce of a verified request signature. Throws\n * `KeetaAnchorUserError` when the replay guard refuses it: the nonce was\n * already used, or the guard cannot record more claims now.\n */\nexport async function claimRequestNonce(account: VerifiableAccount, context: RequestAuthContext, signed: HTTPSignedField): Promise<void> {\n\tconst expiresAt = new Date(new Date(signed.timestamp).valueOf() + DEFAULT_MAX_SKEW_MS);\n\t/* Both parts of a dual signature spend the one request nonce */\n\tconst nonce = requestSignatureOf(signed)?.nonce ?? signed.nonce;\n\tconst claimed = await context.replayGuard.claim(`request:${account.publicKeyString.get()}:${nonce}`, expiresAt);\n\tif (!claimed) {\n\t\tthrow(new KeetaAnchorUserError('Signed request refused: its nonce was already used, or the server cannot accept more signed requests now'));\n\t}\n}\n\n/**\n * Verify a request signature, then spend its nonce. Resolves `false`\n * when the signature does not cover this request, and throws as\n * {@link claimRequestNonce} does when the nonce is refused.\n */\nexport async function verifySignedRequest(account: VerifiableAccount, context: RequestAuthContext, data: Signable, signed: HTTPSignedField): Promise<boolean> {\n\tconst valid = await verifyRequestSignature(account, context, data, signed);\n\tif (!valid) {\n\t\treturn(false);\n\t}\n\n\tawait claimRequestNonce(account, context, signed);\n\n\treturn(true);\n}\n"]}