@proof-holdings/sdk 0.0.0-stage → 1.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/CHANGELOG.md +54 -0
- package/LICENSE +21 -0
- package/README.md +239 -2
- package/dist/cjs/client.d.ts +48 -0
- package/dist/cjs/client.d.ts.map +1 -0
- package/dist/cjs/client.js +73 -0
- package/dist/cjs/client.js.map +1 -0
- package/dist/cjs/credentials.d.ts +46 -0
- package/dist/cjs/credentials.d.ts.map +1 -0
- package/dist/cjs/credentials.js +137 -0
- package/dist/cjs/credentials.js.map +1 -0
- package/dist/cjs/crypto/hitl-crypto.d.ts +95 -0
- package/dist/cjs/crypto/hitl-crypto.d.ts.map +1 -0
- package/dist/cjs/crypto/hitl-crypto.js +338 -0
- package/dist/cjs/crypto/hitl-crypto.js.map +1 -0
- package/dist/cjs/errors.d.ts +52 -0
- package/dist/cjs/errors.d.ts.map +1 -0
- package/dist/cjs/errors.js +121 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/http.d.ts +62 -0
- package/dist/cjs/http.d.ts.map +1 -0
- package/dist/cjs/http.js +192 -0
- package/dist/cjs/http.js.map +1 -0
- package/dist/cjs/index.d.ts +26 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +79 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/jwks/key-set-cache.d.ts +105 -0
- package/dist/cjs/jwks/key-set-cache.d.ts.map +1 -0
- package/dist/cjs/jwks/key-set-cache.js +277 -0
- package/dist/cjs/jwks/key-set-cache.js.map +1 -0
- package/dist/cjs/jwks/keyset.d.ts +139 -0
- package/dist/cjs/jwks/keyset.d.ts.map +1 -0
- package/dist/cjs/jwks/keyset.js +503 -0
- package/dist/cjs/jwks/keyset.js.map +1 -0
- package/dist/cjs/jwks/support.d.ts +9 -0
- package/dist/cjs/jwks/support.d.ts.map +1 -0
- package/dist/cjs/jwks/support.js +12 -0
- package/dist/cjs/jwks/support.js.map +1 -0
- package/dist/cjs/polling.d.ts +10 -0
- package/dist/cjs/polling.d.ts.map +1 -0
- package/dist/cjs/polling.js +74 -0
- package/dist/cjs/polling.js.map +1 -0
- package/dist/cjs/resources/auth-sessions.d.ts +41 -0
- package/dist/cjs/resources/auth-sessions.d.ts.map +1 -0
- package/dist/cjs/resources/auth-sessions.js +34 -0
- package/dist/cjs/resources/auth-sessions.js.map +1 -0
- package/dist/cjs/resources/authorizations.d.ts +23 -0
- package/dist/cjs/resources/authorizations.d.ts.map +1 -0
- package/dist/cjs/resources/authorizations.js +29 -0
- package/dist/cjs/resources/authorizations.js.map +1 -0
- package/dist/cjs/resources/circles.d.ts +38 -0
- package/dist/cjs/resources/circles.d.ts.map +1 -0
- package/dist/cjs/resources/circles.js +67 -0
- package/dist/cjs/resources/circles.js.map +1 -0
- package/dist/cjs/resources/confirmations.d.ts +111 -0
- package/dist/cjs/resources/confirmations.d.ts.map +1 -0
- package/dist/cjs/resources/confirmations.js +86 -0
- package/dist/cjs/resources/confirmations.js.map +1 -0
- package/dist/cjs/resources/delegations.d.ts +36 -0
- package/dist/cjs/resources/delegations.d.ts.map +1 -0
- package/dist/cjs/resources/delegations.js +47 -0
- package/dist/cjs/resources/delegations.js.map +1 -0
- package/dist/cjs/resources/hitl-chat-id.d.ts +42 -0
- package/dist/cjs/resources/hitl-chat-id.d.ts.map +1 -0
- package/dist/cjs/resources/hitl-chat-id.js +37 -0
- package/dist/cjs/resources/hitl-chat-id.js.map +1 -0
- package/dist/cjs/resources/hitl-keys.d.ts +13 -0
- package/dist/cjs/resources/hitl-keys.d.ts.map +1 -0
- package/dist/cjs/resources/hitl-keys.js +22 -0
- package/dist/cjs/resources/hitl-keys.js.map +1 -0
- package/dist/cjs/resources/hitl.d.ts +39 -0
- package/dist/cjs/resources/hitl.d.ts.map +1 -0
- package/dist/cjs/resources/hitl.js +58 -0
- package/dist/cjs/resources/hitl.js.map +1 -0
- package/dist/cjs/resources/me.d.ts +102 -0
- package/dist/cjs/resources/me.d.ts.map +1 -0
- package/dist/cjs/resources/me.js +85 -0
- package/dist/cjs/resources/me.js.map +1 -0
- package/dist/cjs/resources/profiles.d.ts +47 -0
- package/dist/cjs/resources/profiles.d.ts.map +1 -0
- package/dist/cjs/resources/profiles.js +70 -0
- package/dist/cjs/resources/profiles.js.map +1 -0
- package/dist/cjs/resources/proof-me.d.ts +48 -0
- package/dist/cjs/resources/proof-me.d.ts.map +1 -0
- package/dist/cjs/resources/proof-me.js +24 -0
- package/dist/cjs/resources/proof-me.js.map +1 -0
- package/dist/cjs/resources/proofs.d.ts +81 -0
- package/dist/cjs/resources/proofs.d.ts.map +1 -0
- package/dist/cjs/resources/proofs.js +437 -0
- package/dist/cjs/resources/proofs.js.map +1 -0
- package/dist/cjs/resources/public-profiles.d.ts +36 -0
- package/dist/cjs/resources/public-profiles.d.ts.map +1 -0
- package/dist/cjs/resources/public-profiles.js +49 -0
- package/dist/cjs/resources/public-profiles.js.map +1 -0
- package/dist/cjs/resources/sessions.d.ts +15 -0
- package/dist/cjs/resources/sessions.d.ts.map +1 -0
- package/dist/cjs/resources/sessions.js +26 -0
- package/dist/cjs/resources/sessions.js.map +1 -0
- package/dist/cjs/resources/templates.d.ts +25 -0
- package/dist/cjs/resources/templates.d.ts.map +1 -0
- package/dist/cjs/resources/templates.js +46 -0
- package/dist/cjs/resources/templates.js.map +1 -0
- package/dist/cjs/resources/verification-requests.d.ts +56 -0
- package/dist/cjs/resources/verification-requests.d.ts.map +1 -0
- package/dist/cjs/resources/verification-requests.js +61 -0
- package/dist/cjs/resources/verification-requests.js.map +1 -0
- package/dist/cjs/resources/verifications.d.ts +60 -0
- package/dist/cjs/resources/verifications.d.ts.map +1 -0
- package/dist/cjs/resources/verifications.js +91 -0
- package/dist/cjs/resources/verifications.js.map +1 -0
- package/dist/cjs/resources/webhook-deliveries.d.ts +15 -0
- package/dist/cjs/resources/webhook-deliveries.d.ts.map +1 -0
- package/dist/cjs/resources/webhook-deliveries.js +26 -0
- package/dist/cjs/resources/webhook-deliveries.js.map +1 -0
- package/dist/cjs/streaming.d.ts +87 -0
- package/dist/cjs/streaming.d.ts.map +1 -0
- package/dist/cjs/streaming.js +289 -0
- package/dist/cjs/streaming.js.map +1 -0
- package/dist/cjs/types.d.ts +1483 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +6 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.d.ts.map +1 -0
- package/dist/cjs/version.js +5 -0
- package/dist/cjs/version.js.map +1 -0
- package/dist/esm/client.d.ts +48 -0
- package/dist/esm/client.d.ts.map +1 -0
- package/dist/esm/client.js +69 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/credentials.d.ts +46 -0
- package/dist/esm/credentials.d.ts.map +1 -0
- package/dist/esm/credentials.js +127 -0
- package/dist/esm/credentials.js.map +1 -0
- package/dist/esm/crypto/hitl-crypto.d.ts +95 -0
- package/dist/esm/crypto/hitl-crypto.d.ts.map +1 -0
- package/dist/esm/crypto/hitl-crypto.js +321 -0
- package/dist/esm/crypto/hitl-crypto.js.map +1 -0
- package/dist/esm/errors.d.ts +52 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +106 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/http.d.ts +62 -0
- package/dist/esm/http.d.ts.map +1 -0
- package/dist/esm/http.js +187 -0
- package/dist/esm/http.js.map +1 -0
- package/dist/esm/index.d.ts +26 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +33 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/jwks/key-set-cache.d.ts +105 -0
- package/dist/esm/jwks/key-set-cache.d.ts.map +1 -0
- package/dist/esm/jwks/key-set-cache.js +268 -0
- package/dist/esm/jwks/key-set-cache.js.map +1 -0
- package/dist/esm/jwks/keyset.d.ts +139 -0
- package/dist/esm/jwks/keyset.d.ts.map +1 -0
- package/dist/esm/jwks/keyset.js +489 -0
- package/dist/esm/jwks/keyset.js.map +1 -0
- package/dist/esm/jwks/support.d.ts +9 -0
- package/dist/esm/jwks/support.d.ts.map +1 -0
- package/dist/esm/jwks/support.js +9 -0
- package/dist/esm/jwks/support.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/polling.d.ts +10 -0
- package/dist/esm/polling.d.ts.map +1 -0
- package/dist/esm/polling.js +71 -0
- package/dist/esm/polling.js.map +1 -0
- package/dist/esm/resources/auth-sessions.d.ts +41 -0
- package/dist/esm/resources/auth-sessions.d.ts.map +1 -0
- package/dist/esm/resources/auth-sessions.js +30 -0
- package/dist/esm/resources/auth-sessions.js.map +1 -0
- package/dist/esm/resources/authorizations.d.ts +23 -0
- package/dist/esm/resources/authorizations.d.ts.map +1 -0
- package/dist/esm/resources/authorizations.js +25 -0
- package/dist/esm/resources/authorizations.js.map +1 -0
- package/dist/esm/resources/circles.d.ts +38 -0
- package/dist/esm/resources/circles.d.ts.map +1 -0
- package/dist/esm/resources/circles.js +63 -0
- package/dist/esm/resources/circles.js.map +1 -0
- package/dist/esm/resources/confirmations.d.ts +111 -0
- package/dist/esm/resources/confirmations.d.ts.map +1 -0
- package/dist/esm/resources/confirmations.js +82 -0
- package/dist/esm/resources/confirmations.js.map +1 -0
- package/dist/esm/resources/delegations.d.ts +36 -0
- package/dist/esm/resources/delegations.d.ts.map +1 -0
- package/dist/esm/resources/delegations.js +43 -0
- package/dist/esm/resources/delegations.js.map +1 -0
- package/dist/esm/resources/hitl-chat-id.d.ts +42 -0
- package/dist/esm/resources/hitl-chat-id.d.ts.map +1 -0
- package/dist/esm/resources/hitl-chat-id.js +33 -0
- package/dist/esm/resources/hitl-chat-id.js.map +1 -0
- package/dist/esm/resources/hitl-keys.d.ts +13 -0
- package/dist/esm/resources/hitl-keys.d.ts.map +1 -0
- package/dist/esm/resources/hitl-keys.js +18 -0
- package/dist/esm/resources/hitl-keys.js.map +1 -0
- package/dist/esm/resources/hitl.d.ts +39 -0
- package/dist/esm/resources/hitl.d.ts.map +1 -0
- package/dist/esm/resources/hitl.js +54 -0
- package/dist/esm/resources/hitl.js.map +1 -0
- package/dist/esm/resources/me.d.ts +102 -0
- package/dist/esm/resources/me.d.ts.map +1 -0
- package/dist/esm/resources/me.js +81 -0
- package/dist/esm/resources/me.js.map +1 -0
- package/dist/esm/resources/profiles.d.ts +47 -0
- package/dist/esm/resources/profiles.d.ts.map +1 -0
- package/dist/esm/resources/profiles.js +66 -0
- package/dist/esm/resources/profiles.js.map +1 -0
- package/dist/esm/resources/proof-me.d.ts +48 -0
- package/dist/esm/resources/proof-me.d.ts.map +1 -0
- package/dist/esm/resources/proof-me.js +20 -0
- package/dist/esm/resources/proof-me.js.map +1 -0
- package/dist/esm/resources/proofs.d.ts +81 -0
- package/dist/esm/resources/proofs.d.ts.map +1 -0
- package/dist/esm/resources/proofs.js +433 -0
- package/dist/esm/resources/proofs.js.map +1 -0
- package/dist/esm/resources/public-profiles.d.ts +36 -0
- package/dist/esm/resources/public-profiles.d.ts.map +1 -0
- package/dist/esm/resources/public-profiles.js +45 -0
- package/dist/esm/resources/public-profiles.js.map +1 -0
- package/dist/esm/resources/sessions.d.ts +15 -0
- package/dist/esm/resources/sessions.d.ts.map +1 -0
- package/dist/esm/resources/sessions.js +22 -0
- package/dist/esm/resources/sessions.js.map +1 -0
- package/dist/esm/resources/templates.d.ts +25 -0
- package/dist/esm/resources/templates.d.ts.map +1 -0
- package/dist/esm/resources/templates.js +42 -0
- package/dist/esm/resources/templates.js.map +1 -0
- package/dist/esm/resources/verification-requests.d.ts +56 -0
- package/dist/esm/resources/verification-requests.d.ts.map +1 -0
- package/dist/esm/resources/verification-requests.js +57 -0
- package/dist/esm/resources/verification-requests.js.map +1 -0
- package/dist/esm/resources/verifications.d.ts +60 -0
- package/dist/esm/resources/verifications.d.ts.map +1 -0
- package/dist/esm/resources/verifications.js +87 -0
- package/dist/esm/resources/verifications.js.map +1 -0
- package/dist/esm/resources/webhook-deliveries.d.ts +15 -0
- package/dist/esm/resources/webhook-deliveries.d.ts.map +1 -0
- package/dist/esm/resources/webhook-deliveries.js +22 -0
- package/dist/esm/resources/webhook-deliveries.js.map +1 -0
- package/dist/esm/streaming.d.ts +87 -0
- package/dist/esm/streaming.d.ts.map +1 -0
- package/dist/esm/streaming.js +286 -0
- package/dist/esm/streaming.js.map +1 -0
- package/dist/esm/types.d.ts +1483 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +5 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/version.js.map +1 -0
- package/package.json +64 -4
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HITL End-to-End Encryption — Crypto Module
|
|
3
|
+
*
|
|
4
|
+
* Implements the envelope spec from docs/ENCRYPTION_ENVELOPE.md.
|
|
5
|
+
* Uses Node.js crypto module (works in Node.js 18+).
|
|
6
|
+
*
|
|
7
|
+
* Two modes:
|
|
8
|
+
* - Encrypt-only: agent has public key, encrypts messages for the approver
|
|
9
|
+
* - Encrypt+decrypt: approver has password, decrypts messages in browser
|
|
10
|
+
*/
|
|
11
|
+
import crypto from 'crypto';
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Constants
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
const ENVELOPE_VERSION = 1;
|
|
16
|
+
const MESSAGE_ALG = 'RSA-OAEP-256+AES-256-GCM';
|
|
17
|
+
const PRIVATE_KEY_ALG = 'PBKDF2-SHA256+AES-256-GCM';
|
|
18
|
+
const AES_KEY_BYTES = 32;
|
|
19
|
+
const AES_IV_BYTES = 12;
|
|
20
|
+
const AES_TAG_BYTES = 16;
|
|
21
|
+
const RSA_EK_BYTES = 512; // 4096-bit RSA
|
|
22
|
+
const PBKDF2_ITERATIONS = 600000;
|
|
23
|
+
const PBKDF2_SALT_BYTES = 16;
|
|
24
|
+
const PBKDF2_KEY_BYTES = 32;
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// Base64url helpers
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
function toBase64url(buf) {
|
|
29
|
+
return buf.toString('base64url');
|
|
30
|
+
}
|
|
31
|
+
function fromBase64url(str) {
|
|
32
|
+
return Buffer.from(str, 'base64url');
|
|
33
|
+
}
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Key Identifier
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
/** Compute kid = lowercase_hex(SHA-256(DER-encoded SPKI public key bytes)) */
|
|
38
|
+
export function computeKid(publicKeyPem) {
|
|
39
|
+
const keyObject = crypto.createPublicKey(publicKeyPem);
|
|
40
|
+
const der = keyObject.export({ type: 'spki', format: 'der' });
|
|
41
|
+
return crypto.createHash('sha256').update(der).digest('hex');
|
|
42
|
+
}
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
// RSA Key Generation
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
/** Generate an RSA-4096 key pair. Returns PEM-encoded SPKI public + PKCS#8 private. */
|
|
47
|
+
export function generateKeyPair() {
|
|
48
|
+
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
|
|
49
|
+
modulusLength: 4096,
|
|
50
|
+
publicKeyEncoding: { type: 'spki', format: 'pem' },
|
|
51
|
+
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
|
|
52
|
+
});
|
|
53
|
+
return { publicKeyPem: publicKey, privateKeyPem: privateKey };
|
|
54
|
+
}
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// PBKDF2
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
/** Derive a 256-bit key from password + salt using PBKDF2-SHA256 at 600k iterations. */
|
|
59
|
+
export function deriveKey(password, salt) {
|
|
60
|
+
return crypto.pbkdf2Sync(password, salt, PBKDF2_ITERATIONS, PBKDF2_KEY_BYTES, 'sha256');
|
|
61
|
+
}
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// AES-256-GCM
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
function aesGcmEncrypt(key, iv, plaintext, aad) {
|
|
66
|
+
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
|
|
67
|
+
cipher.setAAD(aad);
|
|
68
|
+
const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
69
|
+
const tag = cipher.getAuthTag();
|
|
70
|
+
return { ciphertext, tag };
|
|
71
|
+
}
|
|
72
|
+
function aesGcmDecrypt(key, iv, ciphertext, tag, aad) {
|
|
73
|
+
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
|
|
74
|
+
decipher.setAuthTag(tag);
|
|
75
|
+
decipher.setAAD(aad);
|
|
76
|
+
try {
|
|
77
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
throw new Error('Decryption failed');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
// RSA-OAEP-SHA256 wrap/unwrap
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
function rsaOaepWrap(publicKeyPem, plaintext) {
|
|
87
|
+
const keyObject = crypto.createPublicKey(publicKeyPem);
|
|
88
|
+
return crypto.publicEncrypt({ key: keyObject, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, plaintext);
|
|
89
|
+
}
|
|
90
|
+
function rsaOaepUnwrap(privateKeyPem, ciphertext) {
|
|
91
|
+
const keyObject = crypto.createPrivateKey(privateKeyPem);
|
|
92
|
+
try {
|
|
93
|
+
return crypto.privateDecrypt({ key: keyObject, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, ciphertext);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
throw new Error('Decryption failed');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
// Envelope Validation
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
export function validateMessageEnvelope(obj) {
|
|
103
|
+
if (!obj || typeof obj !== 'object')
|
|
104
|
+
return false;
|
|
105
|
+
const e = obj;
|
|
106
|
+
if (e.v !== 1 && e.v !== 2)
|
|
107
|
+
return false;
|
|
108
|
+
if (e.alg !== MESSAGE_ALG)
|
|
109
|
+
return false;
|
|
110
|
+
if (typeof e.ek !== 'string' || fromBase64url(e.ek).length !== RSA_EK_BYTES)
|
|
111
|
+
return false;
|
|
112
|
+
if (typeof e.iv !== 'string' || fromBase64url(e.iv).length !== AES_IV_BYTES)
|
|
113
|
+
return false;
|
|
114
|
+
if (typeof e.tag !== 'string' || fromBase64url(e.tag).length !== AES_TAG_BYTES)
|
|
115
|
+
return false;
|
|
116
|
+
if (typeof e.ct !== 'string' || !e.ct)
|
|
117
|
+
return false;
|
|
118
|
+
if (typeof e.kid !== 'string' || !/^[a-f0-9]{64}$/.test(e.kid))
|
|
119
|
+
return false;
|
|
120
|
+
// v2: per-recipient wrapped keys (authorization_id → base64url, each RSA_EK_BYTES long)
|
|
121
|
+
if (e.v === 2) {
|
|
122
|
+
if (!e.approver_ek || typeof e.approver_ek !== 'object' || Array.isArray(e.approver_ek))
|
|
123
|
+
return false;
|
|
124
|
+
for (const wrapped of Object.values(e.approver_ek)) {
|
|
125
|
+
if (typeof wrapped !== 'string' || fromBase64url(wrapped).length !== RSA_EK_BYTES)
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// Reject unknown fields (v2 additionally allows approver_ek)
|
|
130
|
+
const known = new Set(e.v === 2
|
|
131
|
+
? ['v', 'alg', 'ek', 'approver_ek', 'iv', 'ct', 'tag', 'kid']
|
|
132
|
+
: ['v', 'alg', 'ek', 'iv', 'ct', 'tag', 'kid']);
|
|
133
|
+
for (const key of Object.keys(e)) {
|
|
134
|
+
if (!known.has(key))
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
export function validatePrivateKeyEnvelope(obj) {
|
|
140
|
+
if (!obj || typeof obj !== 'object')
|
|
141
|
+
return false;
|
|
142
|
+
const e = obj;
|
|
143
|
+
if (e.v !== 1)
|
|
144
|
+
return false;
|
|
145
|
+
if (e.alg !== PRIVATE_KEY_ALG)
|
|
146
|
+
return false;
|
|
147
|
+
if (typeof e.iv !== 'string' || fromBase64url(e.iv).length !== AES_IV_BYTES)
|
|
148
|
+
return false;
|
|
149
|
+
if (typeof e.tag !== 'string' || fromBase64url(e.tag).length !== AES_TAG_BYTES)
|
|
150
|
+
return false;
|
|
151
|
+
if (typeof e.ct !== 'string' || !e.ct)
|
|
152
|
+
return false;
|
|
153
|
+
if (typeof e.salt !== 'string' || fromBase64url(e.salt).length !== PBKDF2_SALT_BYTES)
|
|
154
|
+
return false;
|
|
155
|
+
if (typeof e.kid !== 'string' || !/^[a-f0-9]{64}$/.test(e.kid))
|
|
156
|
+
return false;
|
|
157
|
+
const known = new Set(['v', 'alg', 'iv', 'ct', 'tag', 'salt', 'kid']);
|
|
158
|
+
for (const key of Object.keys(e)) {
|
|
159
|
+
if (!known.has(key))
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
// ---------------------------------------------------------------------------
|
|
165
|
+
// Message Encryption (Section 4.1)
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
/**
|
|
168
|
+
* Encrypt a confirmation message for a HITL config.
|
|
169
|
+
* AAD = `{hitlId}`
|
|
170
|
+
*/
|
|
171
|
+
export function encryptMessage(plaintext, publicKeyPem, hitlId) {
|
|
172
|
+
const aesKey = crypto.randomBytes(AES_KEY_BYTES);
|
|
173
|
+
const iv = crypto.randomBytes(AES_IV_BYTES);
|
|
174
|
+
const aad = Buffer.from(hitlId, 'utf8');
|
|
175
|
+
const plaintextBuf = Buffer.from(plaintext, 'utf8');
|
|
176
|
+
const { ciphertext, tag } = aesGcmEncrypt(aesKey, iv, plaintextBuf, aad);
|
|
177
|
+
const ek = rsaOaepWrap(publicKeyPem, aesKey);
|
|
178
|
+
const kid = computeKid(publicKeyPem);
|
|
179
|
+
return {
|
|
180
|
+
v: ENVELOPE_VERSION,
|
|
181
|
+
alg: MESSAGE_ALG,
|
|
182
|
+
ek: toBase64url(ek),
|
|
183
|
+
iv: toBase64url(iv),
|
|
184
|
+
ct: toBase64url(ciphertext),
|
|
185
|
+
tag: toBase64url(tag),
|
|
186
|
+
kid,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Encrypt a confirmation message with multi-recipient wrapping (v2).
|
|
191
|
+
* Wraps the AES content key with the owner's public key AND each approver's
|
|
192
|
+
* public key, so every enrolled approver can decrypt with their own key.
|
|
193
|
+
* AAD = `{hitlId}`.
|
|
194
|
+
*
|
|
195
|
+
* @param approverKeys - Map of authorization_id → approver PEM public key
|
|
196
|
+
*/
|
|
197
|
+
export function encryptMessageV2(plaintext, ownerPublicKeyPem, hitlId, approverKeys) {
|
|
198
|
+
const aesKey = crypto.randomBytes(AES_KEY_BYTES);
|
|
199
|
+
const iv = crypto.randomBytes(AES_IV_BYTES);
|
|
200
|
+
const aad = Buffer.from(hitlId, 'utf8');
|
|
201
|
+
const plaintextBuf = Buffer.from(plaintext, 'utf8');
|
|
202
|
+
const { ciphertext, tag } = aesGcmEncrypt(aesKey, iv, plaintextBuf, aad);
|
|
203
|
+
const ek = rsaOaepWrap(ownerPublicKeyPem, aesKey);
|
|
204
|
+
const kid = computeKid(ownerPublicKeyPem);
|
|
205
|
+
// Wrap the same AES key with each approver's public key.
|
|
206
|
+
const approver_ek = {};
|
|
207
|
+
for (const [authzId, approverPem] of Object.entries(approverKeys)) {
|
|
208
|
+
approver_ek[authzId] = toBase64url(rsaOaepWrap(approverPem, aesKey));
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
v: 2,
|
|
212
|
+
alg: MESSAGE_ALG,
|
|
213
|
+
ek: toBase64url(ek),
|
|
214
|
+
approver_ek,
|
|
215
|
+
iv: toBase64url(iv),
|
|
216
|
+
ct: toBase64url(ciphertext),
|
|
217
|
+
tag: toBase64url(tag),
|
|
218
|
+
kid,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Encrypt a message with fixed AES key and IV (for test vector generation only).
|
|
223
|
+
* @internal
|
|
224
|
+
*/
|
|
225
|
+
export function encryptMessageWithFixedInputs(plaintext, publicKeyPem, hitlId, aesKey, iv) {
|
|
226
|
+
const aad = Buffer.from(hitlId, 'utf8');
|
|
227
|
+
const plaintextBuf = Buffer.from(plaintext, 'utf8');
|
|
228
|
+
const { ciphertext, tag } = aesGcmEncrypt(aesKey, iv, plaintextBuf, aad);
|
|
229
|
+
const ek = rsaOaepWrap(publicKeyPem, aesKey);
|
|
230
|
+
const kid = computeKid(publicKeyPem);
|
|
231
|
+
return {
|
|
232
|
+
v: ENVELOPE_VERSION,
|
|
233
|
+
alg: MESSAGE_ALG,
|
|
234
|
+
ek: toBase64url(ek),
|
|
235
|
+
iv: toBase64url(iv),
|
|
236
|
+
ct: toBase64url(ciphertext),
|
|
237
|
+
tag: toBase64url(tag),
|
|
238
|
+
kid,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Decrypt a confirmation message envelope.
|
|
243
|
+
* AAD = `{hitlId}`
|
|
244
|
+
*/
|
|
245
|
+
export function decryptMessage(envelope, privateKeyPem, hitlId) {
|
|
246
|
+
if (!validateMessageEnvelope(envelope)) {
|
|
247
|
+
throw new Error('Decryption failed');
|
|
248
|
+
}
|
|
249
|
+
const ek = fromBase64url(envelope.ek);
|
|
250
|
+
const iv = fromBase64url(envelope.iv);
|
|
251
|
+
const ct = fromBase64url(envelope.ct);
|
|
252
|
+
const tag = fromBase64url(envelope.tag);
|
|
253
|
+
const aad = Buffer.from(hitlId, 'utf8');
|
|
254
|
+
const aesKey = rsaOaepUnwrap(privateKeyPem, ek);
|
|
255
|
+
const plaintext = aesGcmDecrypt(aesKey, iv, ct, tag, aad);
|
|
256
|
+
return plaintext.toString('utf8');
|
|
257
|
+
}
|
|
258
|
+
// ---------------------------------------------------------------------------
|
|
259
|
+
// Private Key Encryption (Section 4.2)
|
|
260
|
+
// ---------------------------------------------------------------------------
|
|
261
|
+
/**
|
|
262
|
+
* Encrypt an RSA private key with a user password for storage.
|
|
263
|
+
* AAD = `{hitlId}:{userId}`
|
|
264
|
+
*/
|
|
265
|
+
export function encryptPrivateKey(privateKeyPem, password, publicKeyPem, hitlId, userId) {
|
|
266
|
+
const salt = crypto.randomBytes(PBKDF2_SALT_BYTES);
|
|
267
|
+
const derivedKey = deriveKey(password, salt);
|
|
268
|
+
const iv = crypto.randomBytes(AES_IV_BYTES);
|
|
269
|
+
const aad = Buffer.from(`${hitlId}:${userId}`, 'utf8');
|
|
270
|
+
const privateKeyDer = crypto.createPrivateKey(privateKeyPem).export({ type: 'pkcs8', format: 'der' });
|
|
271
|
+
const { ciphertext, tag } = aesGcmEncrypt(derivedKey, iv, privateKeyDer, aad);
|
|
272
|
+
const kid = computeKid(publicKeyPem);
|
|
273
|
+
return {
|
|
274
|
+
v: ENVELOPE_VERSION,
|
|
275
|
+
alg: PRIVATE_KEY_ALG,
|
|
276
|
+
iv: toBase64url(iv),
|
|
277
|
+
ct: toBase64url(ciphertext),
|
|
278
|
+
tag: toBase64url(tag),
|
|
279
|
+
salt: toBase64url(salt),
|
|
280
|
+
kid,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Decrypt an RSA private key using the user's password.
|
|
285
|
+
* AAD = `{hitlId}:{userId}`
|
|
286
|
+
* Returns PEM-encoded PKCS#8 private key.
|
|
287
|
+
*/
|
|
288
|
+
export function decryptPrivateKey(envelope, password, hitlId, userId) {
|
|
289
|
+
if (!validatePrivateKeyEnvelope(envelope)) {
|
|
290
|
+
throw new Error('Decryption failed');
|
|
291
|
+
}
|
|
292
|
+
const salt = fromBase64url(envelope.salt);
|
|
293
|
+
const derivedKey = deriveKey(password, salt);
|
|
294
|
+
const iv = fromBase64url(envelope.iv);
|
|
295
|
+
const ct = fromBase64url(envelope.ct);
|
|
296
|
+
const tag = fromBase64url(envelope.tag);
|
|
297
|
+
const aad = Buffer.from(`${hitlId}:${userId}`, 'utf8');
|
|
298
|
+
const privateKeyDer = aesGcmDecrypt(derivedKey, iv, ct, tag, aad);
|
|
299
|
+
// Convert DER back to PEM
|
|
300
|
+
const keyObject = crypto.createPrivateKey({ key: privateKeyDer, format: 'der', type: 'pkcs8' });
|
|
301
|
+
return keyObject.export({ type: 'pkcs8', format: 'pem' });
|
|
302
|
+
}
|
|
303
|
+
// ---------------------------------------------------------------------------
|
|
304
|
+
// Full Key Setup (convenience)
|
|
305
|
+
// ---------------------------------------------------------------------------
|
|
306
|
+
/**
|
|
307
|
+
* Generate a new RSA-4096 key pair and encrypt the private key with a password.
|
|
308
|
+
* Returns everything needed for `PUT /api/v1/hitl/:id/keys`.
|
|
309
|
+
*/
|
|
310
|
+
export function generateEncryptionKeyPair(password, hitlId, userId) {
|
|
311
|
+
const { publicKeyPem, privateKeyPem } = generateKeyPair();
|
|
312
|
+
const encrypted = encryptPrivateKey(privateKeyPem, password, publicKeyPem, hitlId, userId);
|
|
313
|
+
const kid = computeKid(publicKeyPem);
|
|
314
|
+
return {
|
|
315
|
+
publicKeyPem,
|
|
316
|
+
encryptedPrivateKey: encrypted,
|
|
317
|
+
kdfSalt: encrypted.salt,
|
|
318
|
+
kid,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
//# sourceMappingURL=hitl-crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IAElE,0BAA0B;IAC1B,MAAM,SAAS,GAAG,MAAM,CAAC,gBAAgB,CAAC,EAAE,GAAG,EAAE,aAAa,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IAChG,OAAO,SAAS,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAW,CAAC;AACtE,CAAC;AAED,8EAA8E;AAC9E,+BAA+B;AAC/B,8EAA8E;AAE9E;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CACvC,QAAgB,EAChB,MAAc,EACd,MAAc;IAEd,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,GAAG,eAAe,EAAE,CAAC;IAC1D,MAAM,SAAS,GAAG,iBAAiB,CAAC,aAAa,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3F,MAAM,GAAG,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;IACrC,OAAO;QACL,YAAY;QACZ,mBAAmB,EAAE,SAAS;QAC9B,OAAO,EAAE,SAAS,CAAC,IAAI;QACvB,GAAG;KACJ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ApiError } from './types.js';
|
|
2
|
+
export declare class ProofError extends Error {
|
|
3
|
+
readonly code: string;
|
|
4
|
+
readonly statusCode: number;
|
|
5
|
+
readonly details?: unknown;
|
|
6
|
+
readonly requestId?: string;
|
|
7
|
+
constructor(message: string, code: string, statusCode: number, details?: unknown, requestId?: string);
|
|
8
|
+
static fromResponse(statusCode: number, error?: ApiError): ProofError;
|
|
9
|
+
}
|
|
10
|
+
export declare class ValidationError extends ProofError {
|
|
11
|
+
constructor(message: string, code: string, details?: unknown, requestId?: string);
|
|
12
|
+
}
|
|
13
|
+
export declare class AuthenticationError extends ProofError {
|
|
14
|
+
constructor(message: string, code: string, details?: unknown, requestId?: string);
|
|
15
|
+
}
|
|
16
|
+
export declare class ForbiddenError extends ProofError {
|
|
17
|
+
constructor(message: string, code: string, details?: unknown, requestId?: string);
|
|
18
|
+
}
|
|
19
|
+
export declare class NotFoundError extends ProofError {
|
|
20
|
+
constructor(message: string, code: string, details?: unknown, requestId?: string);
|
|
21
|
+
}
|
|
22
|
+
export declare class ConflictError extends ProofError {
|
|
23
|
+
constructor(message: string, code: string, details?: unknown, requestId?: string);
|
|
24
|
+
}
|
|
25
|
+
export declare class RateLimitError extends ProofError {
|
|
26
|
+
/** Seconds to wait before retrying (from error response retryAfter field) */
|
|
27
|
+
readonly retryAfter?: number;
|
|
28
|
+
/** Number of remaining attempts before lockout (auth endpoints only) */
|
|
29
|
+
readonly remainingAttempts?: number;
|
|
30
|
+
constructor(message: string, code: string, details?: unknown, requestId?: string, retryAfter?: number, remainingAttempts?: number);
|
|
31
|
+
static fromApiError(error: ApiError): RateLimitError;
|
|
32
|
+
}
|
|
33
|
+
export declare class ServerError extends ProofError {
|
|
34
|
+
constructor(message: string, code: string, statusCode?: number, details?: unknown, requestId?: string);
|
|
35
|
+
}
|
|
36
|
+
export declare class NetworkError extends ProofError {
|
|
37
|
+
constructor(message: string);
|
|
38
|
+
}
|
|
39
|
+
export declare class TimeoutError extends ProofError {
|
|
40
|
+
constructor(message?: string);
|
|
41
|
+
}
|
|
42
|
+
export declare class PollingTimeoutError extends ProofError {
|
|
43
|
+
constructor(message?: string);
|
|
44
|
+
}
|
|
45
|
+
export declare class ConfirmationDeniedError extends ProofError {
|
|
46
|
+
/** ID of the confirmation that triggered the error */
|
|
47
|
+
readonly confirmationId: string;
|
|
48
|
+
/** Non-approved terminal status that triggered the error ('denied' or, for the createAndWait gate, 'expired') */
|
|
49
|
+
readonly status: string;
|
|
50
|
+
constructor(confirmationId: string, status?: string);
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0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,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM;CAIjF;AAED,qBAAa,cAAe,SAAQ,UAAU;IAC5C,6EAA6E;IAC7E,SAAgB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpC,wEAAwE;IACxE,SAAgB,iBAAiB,CAAC,EAAE,MAAM,CAAC;gBAGzC,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,OAAO,EACjB,SAAS,CAAC,EAAE,MAAM,EAClB,UAAU,CAAC,EAAE,MAAM,EACnB,iBAAiB,CAAC,EAAE,MAAM;IAQ5B,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,GAAG,cAAc;CAMrD;AAED,qBAAa,WAAY,SAAQ,UAAU;gBAC7B,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,GAAE,MAAY,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM;CAI3G;AAED,qBAAa,YAAa,SAAQ,UAAU;gBAC9B,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,YAAa,SAAQ,UAAU;gBAC9B,OAAO,GAAE,MAA4B;CAIlD;AAED,qBAAa,mBAAoB,SAAQ,UAAU;gBACrC,OAAO,GAAE,MAAmD;CAIzE;AAED,qBAAa,uBAAwB,SAAQ,UAAU;IACrD,sDAAsD;IACtD,SAAgB,cAAc,EAAE,MAAM,CAAC;IACvC,iHAAiH;IACjH,SAAgB,MAAM,EAAE,MAAM,CAAC;gBAEnB,cAAc,EAAE,MAAM,EAAE,MAAM,GAAE,MAAiB;CAM9D"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export class ProofError extends Error {
|
|
2
|
+
constructor(message, code, statusCode, details, requestId) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'ProofError';
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.statusCode = statusCode;
|
|
7
|
+
this.details = details;
|
|
8
|
+
this.requestId = requestId;
|
|
9
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
10
|
+
}
|
|
11
|
+
static fromResponse(statusCode, error) {
|
|
12
|
+
const code = error?.code ?? `http_${statusCode}`;
|
|
13
|
+
const message = error?.message ?? `Request failed with status ${statusCode}`;
|
|
14
|
+
const details = error?.details;
|
|
15
|
+
const requestId = error?.request_id;
|
|
16
|
+
switch (statusCode) {
|
|
17
|
+
case 400: return new ValidationError(message, code, details, requestId);
|
|
18
|
+
case 401: return new AuthenticationError(message, code, details, requestId);
|
|
19
|
+
case 403: return new ForbiddenError(message, code, details, requestId);
|
|
20
|
+
case 404: return new NotFoundError(message, code, details, requestId);
|
|
21
|
+
case 409: return new ConflictError(message, code, details, requestId);
|
|
22
|
+
case 429:
|
|
23
|
+
return error
|
|
24
|
+
? RateLimitError.fromApiError(error)
|
|
25
|
+
: new RateLimitError(message, code, details, requestId);
|
|
26
|
+
default:
|
|
27
|
+
if (statusCode >= 500)
|
|
28
|
+
return new ServerError(message, code, statusCode, details, requestId);
|
|
29
|
+
return new ProofError(message, code, statusCode, details, requestId);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class ValidationError extends ProofError {
|
|
34
|
+
constructor(message, code, details, requestId) {
|
|
35
|
+
super(message, code, 400, details, requestId);
|
|
36
|
+
this.name = 'ValidationError';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export class AuthenticationError extends ProofError {
|
|
40
|
+
constructor(message, code, details, requestId) {
|
|
41
|
+
super(message, code, 401, details, requestId);
|
|
42
|
+
this.name = 'AuthenticationError';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export class ForbiddenError extends ProofError {
|
|
46
|
+
constructor(message, code, details, requestId) {
|
|
47
|
+
super(message, code, 403, details, requestId);
|
|
48
|
+
this.name = 'ForbiddenError';
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export class NotFoundError extends ProofError {
|
|
52
|
+
constructor(message, code, details, requestId) {
|
|
53
|
+
super(message, code, 404, details, requestId);
|
|
54
|
+
this.name = 'NotFoundError';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export class ConflictError extends ProofError {
|
|
58
|
+
constructor(message, code, details, requestId) {
|
|
59
|
+
super(message, code, 409, details, requestId);
|
|
60
|
+
this.name = 'ConflictError';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export class RateLimitError extends ProofError {
|
|
64
|
+
constructor(message, code, details, requestId, retryAfter, remainingAttempts) {
|
|
65
|
+
super(message, code, 429, details, requestId);
|
|
66
|
+
this.name = 'RateLimitError';
|
|
67
|
+
this.retryAfter = retryAfter;
|
|
68
|
+
this.remainingAttempts = remainingAttempts;
|
|
69
|
+
}
|
|
70
|
+
static fromApiError(error) {
|
|
71
|
+
return new RateLimitError(error.message, error.code, error.details, error.request_id, error.retryAfter, error.remaining_attempts);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export class ServerError extends ProofError {
|
|
75
|
+
constructor(message, code, statusCode = 500, details, requestId) {
|
|
76
|
+
super(message, code, statusCode, details, requestId);
|
|
77
|
+
this.name = 'ServerError';
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export class NetworkError extends ProofError {
|
|
81
|
+
constructor(message) {
|
|
82
|
+
super(message, 'network_error', 0);
|
|
83
|
+
this.name = 'NetworkError';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export class TimeoutError extends ProofError {
|
|
87
|
+
constructor(message = 'Request timed out') {
|
|
88
|
+
super(message, 'timeout', 0);
|
|
89
|
+
this.name = 'TimeoutError';
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export class PollingTimeoutError extends ProofError {
|
|
93
|
+
constructor(message = 'Polling timed out waiting for completion') {
|
|
94
|
+
super(message, 'polling_timeout', 0);
|
|
95
|
+
this.name = 'PollingTimeoutError';
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export class ConfirmationDeniedError extends ProofError {
|
|
99
|
+
constructor(confirmationId, status = 'denied') {
|
|
100
|
+
super(`Confirmation ${confirmationId} was ${status}`, 'confirmation_denied', 0);
|
|
101
|
+
this.name = 'ConfirmationDeniedError';
|
|
102
|
+
this.confirmationId = confirmationId;
|
|
103
|
+
this.status = status;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,UAAW,SAAQ,KAAK;IAMnC,YACE,OAAe,EACf,IAAY,EACZ,UAAkB,EAClB,OAAiB,EACjB,SAAkB;QAElB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,UAAkB,EAAE,KAAgB;QACtD,MAAM,IAAI,GAAG,KAAK,EAAE,IAAI,IAAI,QAAQ,UAAU,EAAE,CAAC;QACjD,MAAM,OAAO,GAAG,KAAK,EAAE,OAAO,IAAI,8BAA8B,UAAU,EAAE,CAAC;QAC7E,MAAM,OAAO,GAAG,KAAK,EAAE,OAAO,CAAC;QAC/B,MAAM,SAAS,GAAG,KAAK,EAAE,UAAU,CAAC;QAEpC,QAAQ,UAAU,EAAE,CAAC;YACnB,KAAK,GAAG,CAAC,CAAC,OAAO,IAAI,eAAe,CAAC,OAAO,EAAE,IAA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AC;QAChF,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;QACtC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export interface HttpClientConfig {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
timeout: number;
|
|
5
|
+
maxRetries: number;
|
|
6
|
+
fetch: typeof globalThis.fetch;
|
|
7
|
+
}
|
|
8
|
+
type QueryValue = string | number | boolean | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Read a response body, giving up one byte past `limit`.
|
|
11
|
+
*
|
|
12
|
+
* `response.text()` would buffer the WHOLE body and measure afterwards, which is not a ceiling
|
|
13
|
+
* at all for a reply that declares no length: a multi-gigabyte answer is fully in memory before
|
|
14
|
+
* anything refuses it. Returns null when the body is over the limit.
|
|
15
|
+
*/
|
|
16
|
+
export declare function readCapped(response: Response, limit: number): Promise<string | null>;
|
|
17
|
+
export declare class HttpClient {
|
|
18
|
+
readonly baseUrl: string;
|
|
19
|
+
private readonly apiKey;
|
|
20
|
+
private readonly timeout;
|
|
21
|
+
private readonly maxRetries;
|
|
22
|
+
private readonly _fetch;
|
|
23
|
+
constructor(config: HttpClientConfig);
|
|
24
|
+
request<T>(method: string, path: string, options?: {
|
|
25
|
+
body?: unknown;
|
|
26
|
+
query?: Record<string, QueryValue>;
|
|
27
|
+
}): Promise<T>;
|
|
28
|
+
get<T>(path: string, query?: Record<string, QueryValue>): Promise<T>;
|
|
29
|
+
post<T>(path: string, body?: unknown): Promise<T>;
|
|
30
|
+
put<T>(path: string, body?: unknown): Promise<T>;
|
|
31
|
+
del<T>(path: string): Promise<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Raw fetch with auth + user-agent headers applied. Used for SSE streaming
|
|
34
|
+
* and other non-JSON responses. Does NOT retry, does NOT parse body, does
|
|
35
|
+
* NOT apply the default request timeout — the caller owns lifecycle via
|
|
36
|
+
* AbortSignal.
|
|
37
|
+
*/
|
|
38
|
+
rawFetch(path: string, options?: {
|
|
39
|
+
headers?: Record<string, string>;
|
|
40
|
+
signal?: AbortSignal;
|
|
41
|
+
query?: Record<string, QueryValue>;
|
|
42
|
+
}): Promise<Response>;
|
|
43
|
+
/**
|
|
44
|
+
* Fetch a PUBLIC artifact by absolute url through the configured transport — no API key, no
|
|
45
|
+
* retries, redirects refused.
|
|
46
|
+
*
|
|
47
|
+
* The key is left off deliberately rather than forgotten: the issuer's status list is a shared
|
|
48
|
+
* document whose whole point is that reading it says nothing about WHICH proof is being checked
|
|
49
|
+
* (`docs/api-map.yaml`, the status-list endpoint is public). Sending a credential with it would
|
|
50
|
+
* hand the issuer the one fact the design withholds. Redirects are refused because the caller
|
|
51
|
+
* has already decided the url's origin is trusted, and a redirect would move it.
|
|
52
|
+
*/
|
|
53
|
+
fetchPublic(url: string, options: {
|
|
54
|
+
timeoutMs: number;
|
|
55
|
+
headers?: Record<string, string>;
|
|
56
|
+
}): Promise<Response>;
|
|
57
|
+
private buildUrl;
|
|
58
|
+
private backoff;
|
|
59
|
+
private sleep;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
62
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/http.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CAChC;AAED,KAAK,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC;AAExD;;;;;;GAMG;AACH,wBAAsB,UAAU,CAAC,QAAQ,EAAE,QAAQ,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