@coffre/client 0.14.6 → 0.14.7

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.
@@ -0,0 +1,57 @@
1
+ //#region src/credential.d.ts
2
+ /** What a cipher's encrypt gives: the IV coffre drew, 12 bytes, the ciphertext, and the 16-byte tag, which the app stores in its own format. */
3
+ type Sealed = {
4
+ iv: Uint8Array;
5
+ ciphertext: Uint8Array;
6
+ tag: Uint8Array;
7
+ };
8
+ type ProxiedCredential = {
9
+ /** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
10
+ url: string;
11
+ /** `fetch`, at a path under the credential's upstream, with the handle the proxy takes. */
12
+ fetch(path: string, init?: RequestInit): Promise<Response>;
13
+ /**
14
+ * A presigned GET (or HEAD) of `path` under an AWS-signed credential's
15
+ * upstream, `/bucket/key` with each segment encoded as in a URL: coffre
16
+ * signs it with the real key pair, for `expiresIn` seconds (15 minutes
17
+ * unless set, an hour at most), and the URL is the upstream's own, for a
18
+ * browser or anyone to use until then.
19
+ */
20
+ presign(path: string, options?: {
21
+ method?: 'GET' | 'HEAD';
22
+ expiresIn?: number;
23
+ query?: Record<string, string>;
24
+ }): Promise<{
25
+ url: string;
26
+ expiresAt: string;
27
+ }>;
28
+ /**
29
+ * A signature of `message` (a string signs as its UTF-8) by a signer's
30
+ * key, which stays in coffre: a private key's, what Node's
31
+ * `crypto.sign(undefined, message, key)` gives, base64url, each one use
32
+ * in coffre's log; an HMAC secret's, as its `hmac()` writes it. A message
33
+ * past 1 MiB goes raw, its bytes as the request's body, which coffre
34
+ * signs as they come, never held: an `hmac()` signer's whose `maxBytes`
35
+ * takes it, SumSub's document upload say.
36
+ */
37
+ sign(message: Uint8Array | string): Promise<string>;
38
+ /**
39
+ * Whether `signature`, as its header carried it (`sha256=…`), is a
40
+ * verifier's HMAC of `message`: an inbound webhook's body, the bytes as
41
+ * they came, never the JSON parsed and written again. The secret stays
42
+ * in coffre, which compares in constant time and says only yes or no.
43
+ */
44
+ verify(message: Uint8Array | string, signature: string): Promise<boolean>;
45
+ /**
46
+ * `plaintext` (a string as its UTF-8) encrypted with a cipher's AES-256-GCM key, which stays in coffre, under `aad`
47
+ * if given: under an IV coffre draws for each, never the app's.
48
+ */
49
+ encrypt(plaintext: Uint8Array | string, aad?: Uint8Array | string): Promise<Sealed>;
50
+ /** The plaintext `sealed` holds under the cipher's key and `aad`, or null when it does not decrypt: whatever part is wrong, coffre says no more. */
51
+ decrypt(sealed: Sealed, aad?: Uint8Array | string): Promise<Uint8Array | null>;
52
+ };
53
+ type Env = Record<string, string | undefined>;
54
+ /** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
55
+ declare function credential(name: string, env?: Env, fetcher?: (input: string, init?: RequestInit) => Promise<Response>): ProxiedCredential;
56
+ //#endregion
57
+ export { Sealed as n, credential as r, ProxiedCredential as t };
@@ -1,43 +1,2 @@
1
- //#region src/credential.d.ts
2
- export type ProxiedCredential = {
3
- /** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
4
- url: string;
5
- /** `fetch`, at a path under the credential's upstream, with the handle the proxy takes. */
6
- fetch(path: string, init?: RequestInit): Promise<Response>;
7
- /**
8
- * A presigned GET (or HEAD) of `path` under an AWS-signed credential's
9
- * upstream, `/bucket/key` with each segment encoded as in a URL: coffre
10
- * signs it with the real key pair, for `expiresIn` seconds (15 minutes
11
- * unless set, an hour at most), and the URL is the upstream's own, for a
12
- * browser or anyone to use until then.
13
- */
14
- presign(path: string, options?: {
15
- method?: 'GET' | 'HEAD';
16
- expiresIn?: number;
17
- query?: Record<string, string>;
18
- }): Promise<{
19
- url: string;
20
- expiresAt: string;
21
- }>;
22
- /**
23
- * A signature of `message` (a string signs as its UTF-8) by a signer's
24
- * key, which stays in coffre: a private key's, what Node's
25
- * `crypto.sign(undefined, message, key)` gives, base64url, each one use
26
- * in coffre's log; an HMAC secret's, as its `hmac()` writes it. A message
27
- * past 1 MiB goes raw, its bytes as the request's body, which coffre
28
- * signs as they come, never held: an `hmac()` signer's whose `maxBytes`
29
- * takes it, SumSub's document upload say.
30
- */
31
- sign(message: Uint8Array | string): Promise<string>;
32
- /**
33
- * Whether `signature`, as its header carried it (`sha256=…`), is a
34
- * verifier's HMAC of `message`: an inbound webhook's body, the bytes as
35
- * they came, never the JSON parsed and written again. The secret stays
36
- * in coffre, which compares in constant time and says only yes or no.
37
- */
38
- verify(message: Uint8Array | string, signature: string): Promise<boolean>;
39
- };
40
- type Env = Record<string, string | undefined>;
41
- /** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
42
- export declare function credential(name: string, env?: Env, fetcher?: (input: string, init?: RequestInit) => Promise<Response>): ProxiedCredential;
43
- //#endregion
1
+ import { n as Sealed, r as credential, t as ProxiedCredential } from "./credential-DyAuH0om.js";
2
+ export { ProxiedCredential, Sealed, credential };
@@ -1,4 +1,4 @@
1
- import "./dpop.js";
1
+ import { fromBase64url } from "./dpop.js";
2
2
  //#region src/credential.ts
3
3
  /**
4
4
  * A credential through coffre's credential proxy, for an app `coffre run`
@@ -103,6 +103,38 @@ function credential(name, env = globalThis.process?.env ?? {}, fetcher = (input,
103
103
  } });
104
104
  if (!answer.ok || typeof body.valid !== "boolean") throw new Error(`coffre verified nothing: ${body.message ?? answer.status}`);
105
105
  return body.valid;
106
+ },
107
+ encrypt: async (plaintext, aad) => {
108
+ const { answer, body } = await compute({ encrypt: {
109
+ credential: name,
110
+ plaintext: bytesOf(plaintext),
111
+ ...aad === void 0 ? {} : { aad: bytesOf(aad) }
112
+ } });
113
+ const [iv, ciphertext, tag] = [
114
+ body.iv,
115
+ body.ciphertext,
116
+ body.tag
117
+ ].map((part) => typeof part === "string" ? fromBase64url(part) : null);
118
+ if (!answer.ok || !iv || !ciphertext || !tag) throw new Error(`coffre encrypted nothing: ${body.message ?? answer.status}`);
119
+ return {
120
+ iv,
121
+ ciphertext,
122
+ tag
123
+ };
124
+ },
125
+ decrypt: async ({ iv, ciphertext, tag }, aad) => {
126
+ const { answer, body } = await compute({ decrypt: {
127
+ credential: name,
128
+ iv: bytesOf(iv),
129
+ ciphertext: bytesOf(ciphertext),
130
+ tag: bytesOf(tag),
131
+ ...aad === void 0 ? {} : { aad: bytesOf(aad) }
132
+ } });
133
+ if (!answer.ok || body.plaintext !== null && typeof body.plaintext !== "string") throw new Error(`coffre decrypted nothing: ${body.message ?? answer.status}`);
134
+ if (body.plaintext === null) return null;
135
+ const plaintext = fromBase64url(body.plaintext);
136
+ if (plaintext === null) throw new Error("coffre decrypted nothing: its answer is no base64url");
137
+ return plaintext;
106
138
  }
107
139
  };
108
140
  }
@@ -1,5 +1,5 @@
1
1
  import { t as CoffreError } from "./src-BCKSeCER.js";
2
- import { base64url, bodyDigest, bodyLine, contentDigest, createProof, declaredDigest, generateProofKey, thumbprint } from "./dpop.js";
2
+ import { base64url, bodyDigest, bodyLine, contentDigest, createProof, declaredDigest, fromBase64url, generateProofKey, thumbprint } from "./dpop.js";
3
3
  //#region src/workload-proofs.ts
4
4
  /** `<audience>#jkt=<thumbprint>`: an audience only this key can use (section 4.2). */
5
5
  function keyAudience(context) {
@@ -350,35 +350,6 @@ function enrollment(code) {
350
350
  const sleep$1 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
351
351
  //#endregion
352
352
  //#region src/workload.ts
353
- /**
354
- * A workload's session with coffre (docs/design/workload-access.md, section
355
- * 9.1): it proves who it is once, gets a session bound to a key that never
356
- * leaves this process, and from then on uses its credentials through coffre,
357
- * every request signed. The app holds no secret that works anywhere else.
358
- *
359
- * import { workload, enrollment } from '@coffre/client/workload';
360
- *
361
- * const coffre = await workload({ url: 'https://secrets.acme.example', service: 'api-prod', proof: enrollment(process.env.COFFRE_ENROLL) });
362
- * const openai = new OpenAI({ apiKey: 'coffre', baseURL: coffre.url('openai'), fetch: coffre.fetch });
363
- * const charge = await coffre.credential('stripe').fetch('/v1/charges', { method: 'POST', body: new URLSearchParams({ amount: '2000' }) });
364
- *
365
- * What it does for the app, so the app does nothing:
366
- *
367
- * - a P-256 key made here, non-extractable; the key, the session's token
368
- * and the gate's nonce live in this closure only, never on disk or in
369
- * the environment;
370
- * - the exchange, retried while an enrollment code is `full` (5 minutes)
371
- * or its session awaits approval (10), and after a spent projected token,
372
- * once the kubelet writes the next;
373
- * - renewal at half-life, every 30 seconds for an enrollment session, whose
374
- * expiry is its liveness; a fresh proof, with the same key, before the
375
- * session's hard end, `notAfter`; and a new session whenever the old one
376
- * is lost (revoked, frozen, expired while the process slept);
377
- * - each request under `<url>/use/` signed whole (method, URL, query, the
378
- * headers it was given, the body), with the newest nonce, and sent again
379
- * once if the gate asks for a fresh one;
380
- * - logout on `close()`, and on SIGTERM.
381
- */
382
353
  const dispatch = (nonces) => ({
383
354
  seq: ++nonces.sent,
384
355
  at: Date.now()
@@ -797,6 +768,38 @@ function surface(core) {
797
768
  message: bytesOf(message),
798
769
  signature
799
770
  } }, "verified")).valid === true,
771
+ encrypt: async (plaintext, aad) => {
772
+ const body = await compute({ encrypt: {
773
+ credential: name,
774
+ plaintext: bytesOf(plaintext),
775
+ ...aad === void 0 ? {} : { aad: bytesOf(aad) }
776
+ } }, "encrypted");
777
+ const [iv, ciphertext, tag] = [
778
+ body.iv,
779
+ body.ciphertext,
780
+ body.tag
781
+ ].map((part) => typeof part === "string" ? fromBase64url(part) : null);
782
+ if (!iv || !ciphertext || !tag) throw new Error("coffre encrypted nothing");
783
+ return {
784
+ iv,
785
+ ciphertext,
786
+ tag
787
+ };
788
+ },
789
+ decrypt: async ({ iv, ciphertext, tag }, aad) => {
790
+ const body = await compute({ decrypt: {
791
+ credential: name,
792
+ iv: bytesOf(iv),
793
+ ciphertext: bytesOf(ciphertext),
794
+ tag: bytesOf(tag),
795
+ ...aad === void 0 ? {} : { aad: bytesOf(aad) }
796
+ } }, "decrypted");
797
+ if (body.plaintext !== null && typeof body.plaintext !== "string") throw new Error("coffre decrypted nothing");
798
+ if (body.plaintext === null) return null;
799
+ const plaintext = fromBase64url(body.plaintext);
800
+ if (plaintext === null) throw new Error("coffre decrypted nothing: its answer is no base64url");
801
+ return plaintext;
802
+ },
800
803
  presign: async (path, options = {}) => {
801
804
  const body = await compute({ presign: {
802
805
  credential: name,
@@ -1,4 +1,4 @@
1
- import { r as Workload } from "./workload-BSw5PIp4.js";
1
+ import { r as Workload } from "./workload-w5EbGZgy.js";
2
2
  import { Buffer } from "node:buffer";
3
3
  import { Duplex } from "node:stream";
4
4
  //#region src/workload-postgres.d.ts
@@ -1,4 +1,4 @@
1
- import { i as upgradeRefused, r as upgradeHeaders } from "./workload-BbOYoYzX.js";
1
+ import { i as upgradeRefused, r as upgradeHeaders } from "./workload-ClMNMVqA.js";
2
2
  import { Buffer } from "node:buffer";
3
3
  import { Duplex } from "node:stream";
4
4
  //#region src/workload-postgres.ts
@@ -1,3 +1,4 @@
1
+ import { n as Sealed } from "./credential-DyAuH0om.js";
1
2
  import { c as PublicJwk } from "./dpop-C9EOdNCi.js";
2
3
  //#region src/workload-proofs.d.ts
3
4
  /**
@@ -208,6 +209,10 @@ interface UsedCredential {
208
209
  sign(message: string | Uint8Array): Promise<string>;
209
210
  /** Whether `signature`, as its header carried it, is a verifier's HMAC of `message`, an inbound webhook's body as it came: checked without reading its secret. */
210
211
  verify(message: string | Uint8Array, signature: string): Promise<boolean>;
212
+ /** `plaintext` encrypted with a cipher's AES-256-GCM key, which stays in coffre, under `aad` if given and an IV coffre draws. */
213
+ encrypt(plaintext: string | Uint8Array, aad?: string | Uint8Array): Promise<Sealed>;
214
+ /** The plaintext `sealed` holds under the cipher's key and `aad`, or null when it does not decrypt. */
215
+ decrypt(sealed: Sealed, aad?: string | Uint8Array): Promise<Uint8Array | null>;
211
216
  /** A presigned GET or HEAD of `path` under an AWS-signed credential's upstream: the upstream's own URL, for anyone until `expiresAt`. */
212
217
  presign(path: string, options?: {
213
218
  expiresIn?: number;
@@ -1,3 +1,4 @@
1
+ import { n as Sealed } from "./credential-DyAuH0om.js";
1
2
  import { r as CoffreError } from "./index-CURF99SB.js";
2
- import { C as oidc, S as kubernetes, _ as enrollment, a as askedNonce, b as github, c as upgradeRefused, d as KUBERNETES_TOKEN, f as PlatformProof, g as azure, h as aws, i as WorkloadOptions, l as workload, m as ProofContext, n as UsedCredential, o as fromCoffre, p as Proof, r as Workload, s as upgradeHeaders, t as NarrowOptions, u as AwsCredentials, v as fly, w as vercel, x as keyAudience, y as gcp } from "./workload-BSw5PIp4.js";
3
- export { type AwsCredentials, CoffreError, KUBERNETES_TOKEN, NarrowOptions, type PlatformProof, type Proof, type ProofContext, UsedCredential, Workload, WorkloadOptions, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
3
+ import { C as oidc, S as kubernetes, _ as enrollment, a as askedNonce, b as github, c as upgradeRefused, d as KUBERNETES_TOKEN, f as PlatformProof, g as azure, h as aws, i as WorkloadOptions, l as workload, m as ProofContext, n as UsedCredential, o as fromCoffre, p as Proof, r as Workload, s as upgradeHeaders, t as NarrowOptions, u as AwsCredentials, v as fly, w as vercel, x as keyAudience, y as gcp } from "./workload-w5EbGZgy.js";
4
+ export { type AwsCredentials, CoffreError, KUBERNETES_TOKEN, NarrowOptions, type PlatformProof, type Proof, type ProofContext, type Sealed, UsedCredential, Workload, WorkloadOptions, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
package/dist/workload.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import { t as CoffreError } from "./src-BCKSeCER.js";
2
- import { a as workload, c as azure, d as gcp, f as github, g as vercel, h as oidc, i as upgradeRefused, l as enrollment, m as kubernetes, n as fromCoffre, o as KUBERNETES_TOKEN, p as keyAudience, r as upgradeHeaders, s as aws, t as askedNonce, u as fly } from "./workload-BbOYoYzX.js";
2
+ import { a as workload, c as azure, d as gcp, f as github, g as vercel, h as oidc, i as upgradeRefused, l as enrollment, m as kubernetes, n as fromCoffre, o as KUBERNETES_TOKEN, p as keyAudience, r as upgradeHeaders, s as aws, t as askedNonce, u as fly } from "./workload-ClMNMVqA.js";
3
3
  export { CoffreError, KUBERNETES_TOKEN, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/client",
3
- "version": "0.14.6",
3
+ "version": "0.14.7",
4
4
  "description": "The coffre API as typed function calls.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/credential.ts CHANGED
@@ -12,7 +12,10 @@
12
12
  * `COFFRE_HANDLE`, the handle that stands in for the environment's values;
13
13
  * the proxy injects the key, and the app never holds it.
14
14
  */
15
- import { JSON_MESSAGE_BYTES } from './dpop.ts';
15
+ import { fromBase64url, JSON_MESSAGE_BYTES } from './dpop.ts';
16
+
17
+ /** What a cipher's encrypt gives: the IV coffre drew, 12 bytes, the ciphertext, and the 16-byte tag, which the app stores in its own format. */
18
+ export type Sealed = { iv: Uint8Array; ciphertext: Uint8Array; tag: Uint8Array };
16
19
 
17
20
  export type ProxiedCredential = {
18
21
  /** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
@@ -44,6 +47,13 @@ export type ProxiedCredential = {
44
47
  * in coffre, which compares in constant time and says only yes or no.
45
48
  */
46
49
  verify(message: Uint8Array | string, signature: string): Promise<boolean>;
50
+ /**
51
+ * `plaintext` (a string as its UTF-8) encrypted with a cipher's AES-256-GCM key, which stays in coffre, under `aad`
52
+ * if given: under an IV coffre draws for each, never the app's.
53
+ */
54
+ encrypt(plaintext: Uint8Array | string, aad?: Uint8Array | string): Promise<Sealed>;
55
+ /** The plaintext `sealed` holds under the cipher's key and `aad`, or null when it does not decrypt: whatever part is wrong, coffre says no more. */
56
+ decrypt(sealed: Sealed, aad?: Uint8Array | string): Promise<Uint8Array | null>;
47
57
  };
48
58
 
49
59
  type Env = Record<string, string | undefined>;
@@ -72,7 +82,7 @@ export function credential(
72
82
  headers: { authorization: `Bearer ${handle}`, 'content-type': 'application/json' },
73
83
  body: JSON.stringify(asked),
74
84
  });
75
- return { answer, body: (await answer.json()) as { url?: string; expiresAt?: string; signature?: string; valid?: boolean; message?: string } };
85
+ return { answer, body: (await answer.json()) as { url?: string; expiresAt?: string; signature?: string; valid?: boolean; iv?: string; ciphertext?: string; tag?: string; plaintext?: string | null; message?: string } };
76
86
  };
77
87
  /** A message past what JSON takes, signed raw: its bytes as the body, with the SHA-256 that `coffre serve` streams it by, and coffre checks. */
78
88
  const raw = async (bytes: Uint8Array) => {
@@ -113,5 +123,19 @@ export function credential(
113
123
  if (!answer.ok || typeof body.valid !== 'boolean') throw new Error(`coffre verified nothing: ${body.message ?? answer.status}`);
114
124
  return body.valid;
115
125
  },
126
+ encrypt: async (plaintext, aad) => {
127
+ const { answer, body } = await compute({ encrypt: { credential: name, plaintext: bytesOf(plaintext), ...(aad === undefined ? {} : { aad: bytesOf(aad) }) } });
128
+ const [iv, ciphertext, tag] = [body.iv, body.ciphertext, body.tag].map((part) => (typeof part === 'string' ? fromBase64url(part) : null));
129
+ if (!answer.ok || !iv || !ciphertext || !tag) throw new Error(`coffre encrypted nothing: ${body.message ?? answer.status}`);
130
+ return { iv, ciphertext, tag };
131
+ },
132
+ decrypt: async ({ iv, ciphertext, tag }, aad) => {
133
+ const { answer, body } = await compute({ decrypt: { credential: name, iv: bytesOf(iv), ciphertext: bytesOf(ciphertext), tag: bytesOf(tag), ...(aad === undefined ? {} : { aad: bytesOf(aad) }) } });
134
+ if (!answer.ok || (body.plaintext !== null && typeof body.plaintext !== 'string')) throw new Error(`coffre decrypted nothing: ${body.message ?? answer.status}`);
135
+ if (body.plaintext === null) return null;
136
+ const plaintext = fromBase64url(body.plaintext);
137
+ if (plaintext === null) throw new Error('coffre decrypted nothing: its answer is no base64url');
138
+ return plaintext;
139
+ },
116
140
  };
117
141
  }
package/src/workload.ts CHANGED
@@ -27,7 +27,10 @@
27
27
  * once if the gate asks for a fresh one;
28
28
  * - logout on `close()`, and on SIGTERM.
29
29
  */
30
- import { base64url, bodyDigest, bodyLine, contentDigest, createProof, declaredDigest, generateProofKey, JSON_MESSAGE_BYTES, thumbprint, type ProofKey, type PublicJwk } from './dpop.ts';
30
+ import type { Sealed } from './credential.ts';
31
+
32
+ export type { Sealed };
33
+ import { base64url, bodyDigest, bodyLine, contentDigest, createProof, declaredDigest, fromBase64url, generateProofKey, JSON_MESSAGE_BYTES, thumbprint, type ProofKey, type PublicJwk } from './dpop.ts';
31
34
  import { CoffreError } from './index.ts';
32
35
  import type { Proof, ProofContext } from './workload-proofs.ts';
33
36
 
@@ -101,6 +104,10 @@ export interface UsedCredential {
101
104
  sign(message: string | Uint8Array): Promise<string>;
102
105
  /** Whether `signature`, as its header carried it, is a verifier's HMAC of `message`, an inbound webhook's body as it came: checked without reading its secret. */
103
106
  verify(message: string | Uint8Array, signature: string): Promise<boolean>;
107
+ /** `plaintext` encrypted with a cipher's AES-256-GCM key, which stays in coffre, under `aad` if given and an IV coffre draws. */
108
+ encrypt(plaintext: string | Uint8Array, aad?: string | Uint8Array): Promise<Sealed>;
109
+ /** The plaintext `sealed` holds under the cipher's key and `aad`, or null when it does not decrypt. */
110
+ decrypt(sealed: Sealed, aad?: string | Uint8Array): Promise<Uint8Array | null>;
104
111
  /** A presigned GET or HEAD of `path` under an AWS-signed credential's upstream: the upstream's own URL, for anyone until `expiresAt`. */
105
112
  presign(path: string, options?: { expiresIn?: number; method?: 'GET' | 'HEAD'; query?: Record<string, string> }): Promise<{ url: string; expiresAt: string }>;
106
113
  /** A short-lived child of a minter (an AWS session, a GitHub installation token), where the binding lists it. */
@@ -528,6 +535,20 @@ function surface(core: Core): Workload {
528
535
  return body.signature;
529
536
  },
530
537
  verify: async (message, signature) => (await compute({ verify: { credential: name, message: bytesOf(message), signature } }, 'verified')).valid === true,
538
+ encrypt: async (plaintext, aad) => {
539
+ const body = await compute({ encrypt: { credential: name, plaintext: bytesOf(plaintext), ...(aad === undefined ? {} : { aad: bytesOf(aad) }) } }, 'encrypted');
540
+ const [iv, ciphertext, tag] = [body.iv, body.ciphertext, body.tag].map((part) => (typeof part === 'string' ? fromBase64url(part) : null));
541
+ if (!iv || !ciphertext || !tag) throw new Error('coffre encrypted nothing');
542
+ return { iv, ciphertext, tag };
543
+ },
544
+ decrypt: async ({ iv, ciphertext, tag }, aad) => {
545
+ const body = await compute({ decrypt: { credential: name, iv: bytesOf(iv), ciphertext: bytesOf(ciphertext), tag: bytesOf(tag), ...(aad === undefined ? {} : { aad: bytesOf(aad) }) } }, 'decrypted');
546
+ if (body.plaintext !== null && typeof body.plaintext !== 'string') throw new Error('coffre decrypted nothing');
547
+ if (body.plaintext === null) return null;
548
+ const plaintext = fromBase64url(body.plaintext);
549
+ if (plaintext === null) throw new Error('coffre decrypted nothing: its answer is no base64url');
550
+ return plaintext;
551
+ },
531
552
  presign: async (path, options = {}) => {
532
553
  const body = await compute({ presign: { credential: name, path, ...options } }, 'presigned');
533
554
  if (typeof body.url !== 'string' || typeof body.expiresAt !== 'string') throw new Error('coffre presigned nothing');