@btps/sdk 0.0.0-stage → 1.1.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 +149 -0
- package/LICENSE +190 -0
- package/NOTICE +25 -0
- package/README.md +261 -2
- package/dist/client/btpsAgent.d.ts +20 -0
- package/dist/client/btpsAgent.js +172 -0
- package/dist/client/btpsClient.d.ts +202 -0
- package/dist/client/btpsClient.js +602 -0
- package/dist/client/btpsTransporter.d.ts +52 -0
- package/dist/client/btpsTransporter.js +249 -0
- package/dist/client/constants.ts/index.d.ts +11 -0
- package/dist/client/constants.ts/index.js +20 -0
- package/dist/client/index.d.ts +12 -0
- package/dist/client/index.js +27 -0
- package/dist/client/libs/schema.d.ts +27604 -0
- package/dist/client/libs/schema.js +56 -0
- package/dist/client/libs/timeouts.d.ts +22 -0
- package/dist/client/libs/timeouts.js +19 -0
- package/dist/client/types/index.d.ts +155 -0
- package/dist/client/types/index.js +7 -0
- package/dist/core/authentication/BtpsAuthentication.d.ts +128 -0
- package/dist/core/authentication/BtpsAuthentication.js +293 -0
- package/dist/core/authentication/index.d.ts +11 -0
- package/dist/core/authentication/index.js +12 -0
- package/dist/core/authentication/keyProof.d.ts +18 -0
- package/dist/core/authentication/keyProof.js +16 -0
- package/dist/core/authentication/storage/InMemoryAuthSessionStore.d.ts +55 -0
- package/dist/core/authentication/storage/InMemoryAuthSessionStore.js +62 -0
- package/dist/core/authentication/storage/InMemoryTokenStore.d.ts +63 -0
- package/dist/core/authentication/storage/InMemoryTokenStore.js +63 -0
- package/dist/core/authentication/types.d.ts +135 -0
- package/dist/core/authentication/types.js +7 -0
- package/dist/core/crypto/decryptVerify.d.ts +9 -0
- package/dist/core/crypto/decryptVerify.js +45 -0
- package/dist/core/crypto/decrypter-interface.d.ts +25 -0
- package/dist/core/crypto/decrypter-interface.js +7 -0
- package/dist/core/crypto/index.d.ts +77 -0
- package/dist/core/crypto/index.js +210 -0
- package/dist/core/crypto/jcs.d.ts +28 -0
- package/dist/core/crypto/jcs.js +60 -0
- package/dist/core/crypto/keygen.d.ts +9 -0
- package/dist/core/crypto/keygen.js +35 -0
- package/dist/core/crypto/pem-decrypter.d.ts +26 -0
- package/dist/core/crypto/pem-decrypter.js +52 -0
- package/dist/core/crypto/pem-signer.d.ts +21 -0
- package/dist/core/crypto/pem-signer.js +34 -0
- package/dist/core/crypto/signEncrypt.d.ts +34 -0
- package/dist/core/crypto/signEncrypt.js +75 -0
- package/dist/core/crypto/signer-interface.d.ts +22 -0
- package/dist/core/crypto/signer-interface.js +7 -0
- package/dist/core/crypto/types.d.ts +79 -0
- package/dist/core/crypto/types.js +7 -0
- package/dist/core/delegation/BtpsDelegator.d.ts +102 -0
- package/dist/core/delegation/BtpsDelegator.js +118 -0
- package/dist/core/delegation/index.d.ts +7 -0
- package/dist/core/delegation/index.js +8 -0
- package/dist/core/error/constant.d.ts +66 -0
- package/dist/core/error/constant.js +194 -0
- package/dist/core/error/index.d.ts +35 -0
- package/dist/core/error/index.js +52 -0
- package/dist/core/error/types.d.ts +16 -0
- package/dist/core/error/types.js +7 -0
- package/dist/core/server/constants/btps-protocol.d.ts +17 -0
- package/dist/core/server/constants/btps-protocol.js +25 -0
- package/dist/core/server/constants/control.d.ts +7 -0
- package/dist/core/server/constants/control.js +11 -0
- package/dist/core/server/constants/currency.d.ts +7 -0
- package/dist/core/server/constants/currency.js +332 -0
- package/dist/core/server/constants/index.d.ts +26 -0
- package/dist/core/server/constants/index.js +61 -0
- package/dist/core/server/constants/limits.d.ts +50 -0
- package/dist/core/server/constants/limits.js +50 -0
- package/dist/core/server/helpers/BtpsMessageQueue.d.ts +13 -0
- package/dist/core/server/helpers/BtpsMessageQueue.js +12 -0
- package/dist/core/server/helpers/InMemoryQueue.d.ts +16 -0
- package/dist/core/server/helpers/InMemoryQueue.js +35 -0
- package/dist/core/server/helpers/index.d.ts +8 -0
- package/dist/core/server/helpers/index.js +9 -0
- package/dist/core/server/schemas/artifacts/agentSchema.d.ts +4565 -0
- package/dist/core/server/schemas/artifacts/agentSchema.js +51 -0
- package/dist/core/server/schemas/artifacts/artifacts.d.ts +4920 -0
- package/dist/core/server/schemas/artifacts/artifacts.js +21 -0
- package/dist/core/server/schemas/artifacts/controlSchema.d.ts +23 -0
- package/dist/core/server/schemas/artifacts/controlSchema.js +18 -0
- package/dist/core/server/schemas/artifacts/identityLookupSchema.d.ts +32 -0
- package/dist/core/server/schemas/artifacts/identityLookupSchema.js +21 -0
- package/dist/core/server/schemas/artifacts/transporterSchema.d.ts +553 -0
- package/dist/core/server/schemas/artifacts/transporterSchema.js +60 -0
- package/dist/core/server/schemas/btpsDocsSchema.d.ts +4519 -0
- package/dist/core/server/schemas/btpsDocsSchema.js +272 -0
- package/dist/core/server/schemas/helpers.d.ts +1612 -0
- package/dist/core/server/schemas/helpers.js +87 -0
- package/dist/core/server/schemas/responseSchema.d.ts +6918 -0
- package/dist/core/server/schemas/responseSchema.js +86 -0
- package/dist/core/server/schemas/schema.d.ts +4916 -0
- package/dist/core/server/schemas/schema.js +70 -0
- package/dist/core/server/schemas/shared.d.ts +21 -0
- package/dist/core/server/schemas/shared.js +18 -0
- package/dist/core/server/types.d.ts +172 -0
- package/dist/core/server/types.js +7 -0
- package/dist/core/storage/AbstractIdentityStore.d.ts +17 -0
- package/dist/core/storage/AbstractIdentityStore.js +13 -0
- package/dist/core/storage/AbstractStorageStore.d.ts +16 -0
- package/dist/core/storage/AbstractStorageStore.js +17 -0
- package/dist/core/storage/JsonIdentityStore.d.ts +29 -0
- package/dist/core/storage/JsonIdentityStore.js +39 -0
- package/dist/core/storage/JsonStorageStore.d.ts +59 -0
- package/dist/core/storage/JsonStorageStore.js +110 -0
- package/dist/core/storage/index.d.ts +11 -0
- package/dist/core/storage/index.js +18 -0
- package/dist/core/storage/types.d.ts +28 -0
- package/dist/core/storage/types.js +7 -0
- package/dist/core/trust/index.d.ts +10 -0
- package/dist/core/trust/index.js +15 -0
- package/dist/core/trust/storage/AbstractTrustStore.d.ts +23 -0
- package/dist/core/trust/storage/AbstractTrustStore.js +13 -0
- package/dist/core/trust/storage/JsonTrustStore.d.ts +33 -0
- package/dist/core/trust/storage/JsonTrustStore.js +42 -0
- package/dist/core/trust/trustUtils.d.ts +78 -0
- package/dist/core/trust/trustUtils.js +41 -0
- package/dist/core/trust/types.d.ts +53 -0
- package/dist/core/trust/types.js +7 -0
- package/dist/core/utils/identity.d.ts +20 -0
- package/dist/core/utils/identity.js +22 -0
- package/dist/core/utils/index.d.ts +70 -0
- package/dist/core/utils/index.js +124 -0
- package/dist/core/utils/types.d.ts +10 -0
- package/dist/core/utils/types.js +7 -0
- package/dist/core/utils/validation.d.ts +14 -0
- package/dist/core/utils/validation.js +13 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/server/btpsServer.d.ts +292 -0
- package/dist/server/btpsServer.js +827 -0
- package/dist/server/index.d.ts +11 -0
- package/dist/server/index.js +15 -0
- package/dist/server/libs/abstractRateLimiter.d.ts +10 -0
- package/dist/server/libs/abstractRateLimiter.js +15 -0
- package/dist/server/libs/artifactSizeGuard.d.ts +27 -0
- package/dist/server/libs/artifactSizeGuard.js +44 -0
- package/dist/server/libs/artifactTimeWindow.d.ts +24 -0
- package/dist/server/libs/artifactTimeWindow.js +30 -0
- package/dist/server/libs/btpsFactory.d.ts +29 -0
- package/dist/server/libs/btpsFactory.js +49 -0
- package/dist/server/libs/btpsLightweightFactory.d.ts +35 -0
- package/dist/server/libs/btpsLightweightFactory.js +37 -0
- package/dist/server/libs/btpsRegistry.d.ts +14 -0
- package/dist/server/libs/btpsRegistry.js +16 -0
- package/dist/server/libs/btpsSimpleMetricsTracker.d.ts +13 -0
- package/dist/server/libs/btpsSimpleMetricsTracker.js +21 -0
- package/dist/server/libs/btpsSimpleRateLimiter.d.ts +28 -0
- package/dist/server/libs/btpsSimpleRateLimiter.js +46 -0
- package/dist/server/libs/connectionTimeout.d.ts +16 -0
- package/dist/server/libs/connectionTimeout.js +14 -0
- package/dist/server/libs/defaultMiddleware.d.ts +8 -0
- package/dist/server/libs/defaultMiddleware.js +128 -0
- package/dist/server/libs/inFlightLimit.d.ts +18 -0
- package/dist/server/libs/inFlightLimit.js +17 -0
- package/dist/server/libs/index.d.ts +18 -0
- package/dist/server/libs/index.js +34 -0
- package/dist/server/libs/middlewareManager.d.ts +59 -0
- package/dist/server/libs/middlewareManager.js +126 -0
- package/dist/server/libs/type.d.ts +22 -0
- package/dist/server/libs/type.js +7 -0
- package/dist/server/types.d.ts +236 -0
- package/dist/server/types.js +7 -0
- package/package.json +126 -4
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { BTPErrorException } from '../error/index.js';
|
|
8
|
+
import { BTPAgentArtifact, BTPArtifact, BTPDelegation, BTPDocType, BTPServerResponse, BTPTransporterArtifact } from '../server/types.js';
|
|
9
|
+
import type { Signer } from './signer-interface.js';
|
|
10
|
+
import type { Decrypter } from './decrypter-interface.js';
|
|
11
|
+
export type EncryptionMode = 'none' | 'standardEncrypt' | '2faEncrypt';
|
|
12
|
+
export type EncryptionAlgorithmType = 'aes-256-gcm';
|
|
13
|
+
export type BTPEncryption = {
|
|
14
|
+
algorithm: EncryptionAlgorithmType;
|
|
15
|
+
encryptedKey: string;
|
|
16
|
+
iv: string;
|
|
17
|
+
type: EncryptionMode;
|
|
18
|
+
authTag: string;
|
|
19
|
+
};
|
|
20
|
+
export type SignatureAlgorithmHash = 'sha256';
|
|
21
|
+
export type BTPSignature = {
|
|
22
|
+
algorithmHash: SignatureAlgorithmHash;
|
|
23
|
+
value: string;
|
|
24
|
+
fingerprint: string;
|
|
25
|
+
};
|
|
26
|
+
export type PemKeys = {
|
|
27
|
+
publicKey: string;
|
|
28
|
+
privateKey: string;
|
|
29
|
+
};
|
|
30
|
+
/** A key that signs: a PEM key pair, or a Signer holding the private key elsewhere (a KMS, a device). */
|
|
31
|
+
export type BTPSigningKey = PemKeys | Signer;
|
|
32
|
+
/** A key that decrypts: a PEM private key, or a Decrypter holding it elsewhere (a KMS, a device). */
|
|
33
|
+
export type BTPDecryptionKey = string | Decrypter;
|
|
34
|
+
export type BTPCryptoOptions = {
|
|
35
|
+
signature: {
|
|
36
|
+
algorithmHash: SignatureAlgorithmHash;
|
|
37
|
+
};
|
|
38
|
+
encryption?: {
|
|
39
|
+
algorithm: EncryptionAlgorithmType;
|
|
40
|
+
mode: EncryptionMode;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
type WithGenericDocument<TArtifact extends BTPArtifact, T> = Omit<TArtifact, 'document'> & {
|
|
44
|
+
document: T;
|
|
45
|
+
};
|
|
46
|
+
export type BTPCryptoArtifact<T = BTPDocType> = WithGenericDocument<BTPAgentArtifact, T> | WithGenericDocument<BTPTransporterArtifact, T>;
|
|
47
|
+
export interface BTPCryptoResponse<T = BTPDocType> {
|
|
48
|
+
payload?: BTPCryptoArtifact<T> | BTPServerResponse<T>;
|
|
49
|
+
error?: BTPErrorException;
|
|
50
|
+
}
|
|
51
|
+
export interface VerifyEncryptedPayload<T = unknown | string> {
|
|
52
|
+
document?: T;
|
|
53
|
+
signature: BTPSignature;
|
|
54
|
+
encryption: BTPEncryption | null;
|
|
55
|
+
delegation?: BTPDelegation;
|
|
56
|
+
[key: string]: unknown;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Configuration for key generation
|
|
60
|
+
*/
|
|
61
|
+
export interface BTPKeyConfig {
|
|
62
|
+
/** RSA key size in bits */
|
|
63
|
+
keySize?: number;
|
|
64
|
+
/** Key format (default: 'pem') */
|
|
65
|
+
format?: 'pem';
|
|
66
|
+
/** Public key encoding type */
|
|
67
|
+
publicKeyEncoding?: 'spki';
|
|
68
|
+
/** Private key encoding type */
|
|
69
|
+
privateKeyEncoding?: 'pkcs8';
|
|
70
|
+
}
|
|
71
|
+
export interface BTPKeyPair {
|
|
72
|
+
/** Public key in PEM format */
|
|
73
|
+
publicKey: string;
|
|
74
|
+
/** Private key in PEM format */
|
|
75
|
+
privateKey: string;
|
|
76
|
+
/** Public key fingerprint */
|
|
77
|
+
fingerprint: string;
|
|
78
|
+
}
|
|
79
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
import type { BTPTransporterArtifact, BTPDelegation, BTPAttestation } from '../server/types.js';
|
|
8
|
+
import type { BTPSigningKey, PemKeys } from '../crypto/types.js';
|
|
9
|
+
import type { Signer } from '../crypto/signer-interface.js';
|
|
10
|
+
/**
|
|
11
|
+
* Delegator configuration options
|
|
12
|
+
*/
|
|
13
|
+
export type BtpsDelegatorOptions = {
|
|
14
|
+
identity: string;
|
|
15
|
+
autoInit?: boolean;
|
|
16
|
+
} & ({
|
|
17
|
+
privateKey: PemKeys['privateKey'];
|
|
18
|
+
} | {
|
|
19
|
+
signer: Signer;
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* Production-grade Delegator for BTPS artifacts
|
|
23
|
+
* Handles delegation and attestation logic for SaaS and user-managed keys
|
|
24
|
+
*/
|
|
25
|
+
export declare class BtpsDelegator {
|
|
26
|
+
private readonly identity;
|
|
27
|
+
private readonly key;
|
|
28
|
+
private publicKey?;
|
|
29
|
+
private selector;
|
|
30
|
+
private isInitialized;
|
|
31
|
+
private ready?;
|
|
32
|
+
/**
|
|
33
|
+
* Initialize the delegator
|
|
34
|
+
* @param options - The delegator options
|
|
35
|
+
*/
|
|
36
|
+
constructor(options: BtpsDelegatorOptions);
|
|
37
|
+
/**
|
|
38
|
+
* Initialize the delegator
|
|
39
|
+
* This method is called automatically when the delegator is created
|
|
40
|
+
* It verifies the delegator identity and sets the public key
|
|
41
|
+
* @throws BTPErrorException if the delegator identity verification fails
|
|
42
|
+
*/
|
|
43
|
+
init(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Verify the delegator identity
|
|
46
|
+
* @returns {isValid: boolean, publicKey?: string} - The result of the verification
|
|
47
|
+
*/
|
|
48
|
+
protected verifyDelegatorIdentity(): Promise<{
|
|
49
|
+
isValid: boolean;
|
|
50
|
+
publicKey?: string;
|
|
51
|
+
}>;
|
|
52
|
+
/**
|
|
53
|
+
* Verify that a Signer signs with the private key matching the public key
|
|
54
|
+
* @param signer - The Signer holding the private key
|
|
55
|
+
* @param publicKeyPem - The public key in PEM format
|
|
56
|
+
* @returns {Promise<boolean>} - True if the keys match, false otherwise
|
|
57
|
+
*/
|
|
58
|
+
protected isSignerMatching(signer: Signer, publicKeyPem: string): Promise<boolean>;
|
|
59
|
+
/**
|
|
60
|
+
* Verify if the private key matches the public key
|
|
61
|
+
* @param privateKeyPem - The private key in PEM format
|
|
62
|
+
* @param publicKeyPem - The public key in PEM format
|
|
63
|
+
* @returns {boolean} - True if the keys match, false otherwise
|
|
64
|
+
*/
|
|
65
|
+
protected isKeyPairMatching(privateKeyPem: string, publicKeyPem: string): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Decorate a BTPTransporterArtifact with delegation and attestation as needed
|
|
68
|
+
* @param artifact The artifact to delegate
|
|
69
|
+
* @param context Delegation context (user type, id, identity)
|
|
70
|
+
* @returns The artifact with delegation (and attestation if required)
|
|
71
|
+
*/
|
|
72
|
+
delegateArtifact(agentId: string, agentPubKey: string, artifact: BTPTransporterArtifact, onBehalfOf?: {
|
|
73
|
+
identity: string;
|
|
74
|
+
keyPair: BTPSigningKey;
|
|
75
|
+
}): Promise<BTPTransporterArtifact & {
|
|
76
|
+
delegation: BTPDelegation;
|
|
77
|
+
}>;
|
|
78
|
+
/**
|
|
79
|
+
* The delegator's own signing key, as createDelegation and createAttestation take it
|
|
80
|
+
*/
|
|
81
|
+
private ownKey;
|
|
82
|
+
/**
|
|
83
|
+
* Create and sign a delegation
|
|
84
|
+
*/
|
|
85
|
+
protected createDelegation(params: {
|
|
86
|
+
artifact: BTPTransporterArtifact;
|
|
87
|
+
delegatorIdentity: string;
|
|
88
|
+
delegatorKey: BTPSigningKey;
|
|
89
|
+
agentId: string;
|
|
90
|
+
agentPubKey: string;
|
|
91
|
+
selector: string;
|
|
92
|
+
}): Promise<BTPDelegation>;
|
|
93
|
+
/**
|
|
94
|
+
* Create and sign an attestation
|
|
95
|
+
*/
|
|
96
|
+
protected createAttestation(params: {
|
|
97
|
+
delegation: Omit<BTPDelegation, 'attestation'>;
|
|
98
|
+
attestorIdentity: string;
|
|
99
|
+
attestorKey: BTPSigningKey;
|
|
100
|
+
selector: string;
|
|
101
|
+
}): Promise<BTPAttestation>;
|
|
102
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { signBtpPayload, signBtpPayloadWithSigner } from "../crypto/index.js";
|
|
9
|
+
import { getHostAndSelector, resolvePublicKey } from "../utils/index.js";
|
|
10
|
+
import { createSign, createVerify } from "crypto";
|
|
11
|
+
import { BTP_ERROR_SIG_VERIFICATION, BTPErrorException } from "../error/index.js";
|
|
12
|
+
class BtpsDelegator {
|
|
13
|
+
identity;
|
|
14
|
+
key;
|
|
15
|
+
publicKey;
|
|
16
|
+
selector = "btps1";
|
|
17
|
+
isInitialized = !1;
|
|
18
|
+
ready;
|
|
19
|
+
constructor(options) {
|
|
20
|
+
this.identity = options.identity, this.key = "signer" in options ? options.signer : options.privateKey, options.autoInit !== !1 && (this.ready = this.init(), this.ready.catch(() => {
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
async init() {
|
|
24
|
+
const { publicKey, isValid } = await this.verifyDelegatorIdentity();
|
|
25
|
+
if (!publicKey || !isValid)
|
|
26
|
+
throw new BTPErrorException(BTP_ERROR_SIG_VERIFICATION, {
|
|
27
|
+
cause: "Delegator identity verification failed",
|
|
28
|
+
meta: {
|
|
29
|
+
identity: this.identity,
|
|
30
|
+
publicKey,
|
|
31
|
+
isValid
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
this.publicKey = publicKey, this.isInitialized = !0;
|
|
35
|
+
}
|
|
36
|
+
async verifyDelegatorIdentity() {
|
|
37
|
+
const dnsHostAndSelector = await getHostAndSelector(this.identity);
|
|
38
|
+
if (!dnsHostAndSelector) return { isValid: !1, publicKey: void 0 };
|
|
39
|
+
this.selector = dnsHostAndSelector.selector;
|
|
40
|
+
const publicKey = await resolvePublicKey(this.identity, this.selector);
|
|
41
|
+
return publicKey ? { isValid: typeof this.key == "string" ? this.isKeyPairMatching(this.key, publicKey) : await this.isSignerMatching(this.key, publicKey), publicKey } : { isValid: !1, publicKey: void 0 };
|
|
42
|
+
}
|
|
43
|
+
async isSignerMatching(signer, publicKeyPem) {
|
|
44
|
+
const message = Buffer.from("btps-key-validation"), signature = await signer.sign(message);
|
|
45
|
+
return createVerify("sha256").update(message).end().verify(publicKeyPem, signature);
|
|
46
|
+
}
|
|
47
|
+
isKeyPairMatching(privateKeyPem, publicKeyPem) {
|
|
48
|
+
const message = "btps-key-validation", sign = createSign("sha256").update(message).end().sign(privateKeyPem);
|
|
49
|
+
return createVerify("sha256").update(message).end().verify(publicKeyPem, sign);
|
|
50
|
+
}
|
|
51
|
+
async delegateArtifact(agentId, agentPubKey, artifact, onBehalfOf) {
|
|
52
|
+
if (this.ready && await this.ready, !this.isInitialized || !this.publicKey)
|
|
53
|
+
throw new BTPErrorException({ message: "Delegator not initialized" });
|
|
54
|
+
let selector = this.selector;
|
|
55
|
+
if (onBehalfOf) {
|
|
56
|
+
const dnsHostAndSelector = await getHostAndSelector(onBehalfOf.identity);
|
|
57
|
+
if (!dnsHostAndSelector)
|
|
58
|
+
throw new BTPErrorException(BTP_ERROR_SIG_VERIFICATION, {
|
|
59
|
+
cause: "Delegator identity verification failed"
|
|
60
|
+
});
|
|
61
|
+
selector = dnsHostAndSelector.selector;
|
|
62
|
+
}
|
|
63
|
+
const delegation = await this.createDelegation({
|
|
64
|
+
artifact,
|
|
65
|
+
delegatorIdentity: onBehalfOf?.identity ?? this.identity,
|
|
66
|
+
delegatorKey: onBehalfOf?.keyPair ?? this.ownKey(this.publicKey),
|
|
67
|
+
agentId,
|
|
68
|
+
agentPubKey,
|
|
69
|
+
selector
|
|
70
|
+
});
|
|
71
|
+
if (onBehalfOf) {
|
|
72
|
+
const attestation = await this.createAttestation({
|
|
73
|
+
delegation,
|
|
74
|
+
attestorIdentity: this.identity,
|
|
75
|
+
attestorKey: this.ownKey(this.publicKey),
|
|
76
|
+
selector: this.selector
|
|
77
|
+
});
|
|
78
|
+
delegation.attestation = attestation;
|
|
79
|
+
}
|
|
80
|
+
return { ...artifact, delegation };
|
|
81
|
+
}
|
|
82
|
+
ownKey(publicKey) {
|
|
83
|
+
return typeof this.key == "string" ? { privateKey: this.key, publicKey } : this.key;
|
|
84
|
+
}
|
|
85
|
+
async createDelegation(params) {
|
|
86
|
+
const issuedAt = ( new Date()).toISOString(), delegationPayload = {
|
|
87
|
+
...params.artifact,
|
|
88
|
+
delegation: {
|
|
89
|
+
agentId: params.agentId,
|
|
90
|
+
agentPubKey: params.agentPubKey,
|
|
91
|
+
signedBy: params.delegatorIdentity,
|
|
92
|
+
issuedAt,
|
|
93
|
+
selector: params.selector
|
|
94
|
+
}
|
|
95
|
+
}, signature = "sign" in params.delegatorKey ? await signBtpPayloadWithSigner(delegationPayload, params.delegatorKey) : signBtpPayload(delegationPayload, params.delegatorKey);
|
|
96
|
+
return {
|
|
97
|
+
...delegationPayload.delegation,
|
|
98
|
+
signature
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async createAttestation(params) {
|
|
102
|
+
const issuedAt = ( new Date()).toISOString(), attestationPayload = {
|
|
103
|
+
...params.delegation,
|
|
104
|
+
attestation: {
|
|
105
|
+
signedBy: params.attestorIdentity,
|
|
106
|
+
issuedAt,
|
|
107
|
+
selector: params.selector
|
|
108
|
+
}
|
|
109
|
+
}, signature = "sign" in params.attestorKey ? await signBtpPayloadWithSigner(attestationPayload, params.attestorKey) : signBtpPayload(attestationPayload, params.attestorKey);
|
|
110
|
+
return {
|
|
111
|
+
...attestationPayload.attestation,
|
|
112
|
+
signature
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export {
|
|
117
|
+
BtpsDelegator
|
|
118
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { BTPError } from './types.js';
|
|
8
|
+
export declare const BTP_ERROR_SOCKET_CLOSED: BTPError;
|
|
9
|
+
export declare const BTP_ERROR_IDENTITY: BTPError;
|
|
10
|
+
export declare const BTP_ERROR_IDENTITY_NOT_FOUND: BTPError;
|
|
11
|
+
export declare const BTP_ERROR_TRUST_NON_EXISTENT: BTPError;
|
|
12
|
+
export declare const BTPS_ERROR_ACTION_TYPE: BTPError;
|
|
13
|
+
export declare const BTP_ERROR_TRUST_BLOCKED: BTPError;
|
|
14
|
+
export declare const BTP_ERROR_TRUST_NOT_ALLOWED: BTPError;
|
|
15
|
+
/**
|
|
16
|
+
* An expired trust record gets its own distinct response `message` on the
|
|
17
|
+
* wire, separate from BTP_ERROR_TRUST_NOT_ALLOWED. This distinction lives
|
|
18
|
+
* only in the response body text - sendBtpsError's `typeof error.code ===
|
|
19
|
+
* 'number' ? error.code : 500` fallback means the numeric `status.code` is
|
|
20
|
+
* 500 for this and every other application error today.
|
|
21
|
+
*/
|
|
22
|
+
export declare const BTP_ERROR_TRUST_EXPIRED: BTPError;
|
|
23
|
+
/**
|
|
24
|
+
* The precise, server-side-only reason for a revoked trust record.
|
|
25
|
+
* On the wire this collapses onto BTP_ERROR_TRUST_NOT_ALLOWED -
|
|
26
|
+
* same response `message`, same `status.code` - so a revoked sender cannot
|
|
27
|
+
* distinguish their refusal from a never-trusted one.
|
|
28
|
+
*/
|
|
29
|
+
export declare const BTP_ERROR_TRUST_REVOKED: BTPError;
|
|
30
|
+
export declare const BTP_ERROR_TRUST_ALREADY_ACTIVE: BTPError;
|
|
31
|
+
export declare const BTP_ERROR_EVENT_UNRELATED: BTPError;
|
|
32
|
+
export declare const BTP_ERROR_RESOLVE_DNS: BTPError;
|
|
33
|
+
export declare const BTP_ERROR_RESOLVE_PUBKEY: BTPError;
|
|
34
|
+
export declare const BTP_ERROR_RATE_LIMITER: BTPError;
|
|
35
|
+
export declare const BTP_ERROR_INVALID_ACTION: BTPError;
|
|
36
|
+
export declare const BTP_ERROR_INVALID_JSON: {
|
|
37
|
+
code: string;
|
|
38
|
+
message: string;
|
|
39
|
+
status: number;
|
|
40
|
+
};
|
|
41
|
+
export declare const BTP_ERROR_VALIDATION: {
|
|
42
|
+
code: string;
|
|
43
|
+
message: string;
|
|
44
|
+
status: number;
|
|
45
|
+
};
|
|
46
|
+
export declare const BTP_ERROR_SIG_MISMATCH: BTPError;
|
|
47
|
+
export declare const BTP_ERROR_SIG_VERIFICATION: BTPError;
|
|
48
|
+
export declare const BTP_ERROR_CANONICAL_JSON: BTPError;
|
|
49
|
+
export declare const BTP_ERROR_DELEGATION_SIG_VERIFICATION: BTPError;
|
|
50
|
+
export declare const BTP_ERROR_ATTESTATION_VERIFICATION: BTPError;
|
|
51
|
+
export declare const BTP_ERROR_DELEGATION_INVALID: BTPError;
|
|
52
|
+
export declare const BTP_ERROR_UNSUPPORTED_ENCRYPT: BTPError;
|
|
53
|
+
export declare const BTP_ERROR_DECRYPTION_UNINTENDED: BTPError;
|
|
54
|
+
export declare const BTP_ERROR_UNKNOWN: BTPError;
|
|
55
|
+
export declare const BTP_ERROR_AGENT_ID: BTPError;
|
|
56
|
+
export declare const BTP_ERROR_KEY_ROTATION_PROOF: BTPError;
|
|
57
|
+
export declare const BTP_ERROR_ARTIFACT_TIME_WINDOW: BTPError;
|
|
58
|
+
export declare const BTP_ERROR_ARTIFACT_TOO_LARGE: BTPError;
|
|
59
|
+
export declare const BTP_ERROR_VERSION_UNSUPPORTED: BTPError;
|
|
60
|
+
export declare const BTP_ERROR_PRIVACY_TYPE: BTPError;
|
|
61
|
+
export declare const BTP_ERROR_ARTIFACT_NOT_ACCEPTED: BTPError;
|
|
62
|
+
export declare const BTP_ERROR_SOCKET_TIMEOUT: BTPError;
|
|
63
|
+
export declare const BTP_ERROR_AUTHENTICATION_INVALID: BTPError;
|
|
64
|
+
export declare const BTP_ERROR_TIMEOUT: BTPError;
|
|
65
|
+
export declare const BTP_ERROR_CONNECTION_CLOSED: BTPError;
|
|
66
|
+
export declare const BTP_ERROR_CONNECTION_ENDED: BTPError;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const BTP_ERROR_SOCKET_CLOSED = {
|
|
9
|
+
code: "BTP_ERROR_SOCKET_CLOSED",
|
|
10
|
+
message: "Socket already closed"
|
|
11
|
+
}, BTP_ERROR_IDENTITY = {
|
|
12
|
+
code: "BTP_ERROR_IDENTITY",
|
|
13
|
+
message: "BTPS identity is expected in the format username$domain.",
|
|
14
|
+
status: 400
|
|
15
|
+
}, BTP_ERROR_IDENTITY_NOT_FOUND = {
|
|
16
|
+
code: "BTP_ERROR_IDENTITY_NOT_FOUND",
|
|
17
|
+
message: "Identity not found",
|
|
18
|
+
status: 404
|
|
19
|
+
}, BTP_ERROR_TRUST_NON_EXISTENT = {
|
|
20
|
+
code: "BTP_ERROR_TRUST_NON_EXISTENT",
|
|
21
|
+
message: "BTPS trust record does not exist",
|
|
22
|
+
status: 403
|
|
23
|
+
}, BTPS_ERROR_ACTION_TYPE = {
|
|
24
|
+
code: "BTPS_ERROR_ACTION_TYPE",
|
|
25
|
+
message: "BTPS action type is not valid",
|
|
26
|
+
status: 400
|
|
27
|
+
}, BTP_ERROR_TRUST_BLOCKED = {
|
|
28
|
+
code: "BTP_ERROR_TRUST_BLOCKED",
|
|
29
|
+
message: "BTPS trust request is not allowed. Contact receiver",
|
|
30
|
+
status: 403
|
|
31
|
+
}, BTP_ERROR_TRUST_NOT_ALLOWED = {
|
|
32
|
+
code: "BTP_ERROR_TRUST_NOT_ALLOWED",
|
|
33
|
+
message: "BTPS trust request is not allowed at this time. Contact receiver",
|
|
34
|
+
status: 403
|
|
35
|
+
}, BTP_ERROR_TRUST_EXPIRED = {
|
|
36
|
+
code: "BTP_ERROR_TRUST_EXPIRED",
|
|
37
|
+
message: "BTPS trust record has expired",
|
|
38
|
+
status: 403
|
|
39
|
+
}, BTP_ERROR_TRUST_REVOKED = {
|
|
40
|
+
code: "BTP_ERROR_TRUST_REVOKED",
|
|
41
|
+
message: "BTPS trust record has been revoked",
|
|
42
|
+
status: 403
|
|
43
|
+
}, BTP_ERROR_TRUST_ALREADY_ACTIVE = {
|
|
44
|
+
code: "BTP_ERROR_TRUST_ALREADY_ACTIVE",
|
|
45
|
+
message: "BTPS trust record already exist. Request invalid",
|
|
46
|
+
status: 409
|
|
47
|
+
}, BTP_ERROR_EVENT_UNRELATED = {
|
|
48
|
+
code: "BTP_ERROR_EVENT_UNRELATED",
|
|
49
|
+
message: "Event does not relate to an artifact exchanged between its parties",
|
|
50
|
+
status: 400
|
|
51
|
+
}, BTP_ERROR_RESOLVE_DNS = {
|
|
52
|
+
code: "BTP_ERROR_RESOLVE_DNS",
|
|
53
|
+
message: "No valid DNS record found"
|
|
54
|
+
}, BTP_ERROR_RESOLVE_PUBKEY = {
|
|
55
|
+
code: "BTP_ERROR_RESOLVE_PUBKEY",
|
|
56
|
+
message: "No valid public-key found",
|
|
57
|
+
status: 401
|
|
58
|
+
}, BTP_ERROR_RATE_LIMITER = {
|
|
59
|
+
code: "BTP_ERROR_RATE_LIMITER",
|
|
60
|
+
message: "Too many request than its allowed",
|
|
61
|
+
status: 429
|
|
62
|
+
}, BTP_ERROR_INVALID_ACTION = {
|
|
63
|
+
code: "BTP_ERROR_INVALID_ACTION",
|
|
64
|
+
message: "Invalid action",
|
|
65
|
+
status: 400
|
|
66
|
+
}, BTP_ERROR_INVALID_JSON = {
|
|
67
|
+
code: "BTP_ERROR_INVALID_JSON",
|
|
68
|
+
message: "Invalid JSON format",
|
|
69
|
+
status: 400
|
|
70
|
+
}, BTP_ERROR_VALIDATION = {
|
|
71
|
+
code: "BTP_ERROR_VALIDATION",
|
|
72
|
+
message: "BTPS artifact validation failed",
|
|
73
|
+
status: 400
|
|
74
|
+
}, BTP_ERROR_SIG_MISMATCH = {
|
|
75
|
+
code: "BTP_ERROR_SIG_MISMATCH",
|
|
76
|
+
message: "fingerprint mis-match",
|
|
77
|
+
status: 401
|
|
78
|
+
}, BTP_ERROR_SIG_VERIFICATION = {
|
|
79
|
+
code: "BTP_ERROR_SIG_VERIFICATION",
|
|
80
|
+
message: "Signature verification failed",
|
|
81
|
+
status: 401
|
|
82
|
+
}, BTP_ERROR_CANONICAL_JSON = {
|
|
83
|
+
code: "BTP_ERROR_CANONICAL_JSON",
|
|
84
|
+
message: "Payload cannot be canonicalized (RFC 8785)",
|
|
85
|
+
status: 400
|
|
86
|
+
}, BTP_ERROR_DELEGATION_SIG_VERIFICATION = {
|
|
87
|
+
code: "BTP_ERROR_DELEGATION_SIG_VERIFICATION",
|
|
88
|
+
message: "Delegation signature verification failed",
|
|
89
|
+
status: 401
|
|
90
|
+
}, BTP_ERROR_ATTESTATION_VERIFICATION = {
|
|
91
|
+
code: "BTP_ERROR_ATTESTATION_VERIFICATION",
|
|
92
|
+
message: "Attestation verification failed",
|
|
93
|
+
status: 401
|
|
94
|
+
}, BTP_ERROR_DELEGATION_INVALID = {
|
|
95
|
+
code: "BTP_ERROR_DELEGATION_INVALID",
|
|
96
|
+
message: "Delegation is invalid",
|
|
97
|
+
status: 401
|
|
98
|
+
}, BTP_ERROR_UNSUPPORTED_ENCRYPT = {
|
|
99
|
+
code: "BTP_ERROR_UNSUPPORTED_ENCRYPT",
|
|
100
|
+
message: "Unsupported encryption algorithm",
|
|
101
|
+
status: 400
|
|
102
|
+
}, BTP_ERROR_DECRYPTION_UNINTENDED = {
|
|
103
|
+
code: "BTP_ERROR_DECRYPTION_UNINTENDED",
|
|
104
|
+
message: "Decryption failed: Message was not intended for this receiver."
|
|
105
|
+
}, BTP_ERROR_UNKNOWN = {
|
|
106
|
+
code: "BTP_UNKNOWN_ERROR",
|
|
107
|
+
message: "Unknown error",
|
|
108
|
+
status: 500
|
|
109
|
+
}, BTP_ERROR_AGENT_ID = {
|
|
110
|
+
code: "BTP_ERROR_AGENT_ID",
|
|
111
|
+
message: "An agent id names a registered device, not an identity",
|
|
112
|
+
status: 403
|
|
113
|
+
}, BTP_ERROR_KEY_ROTATION_PROOF = {
|
|
114
|
+
code: "BTP_ERROR_KEY_ROTATION_PROOF",
|
|
115
|
+
message: "A new device key needs a proof signed by that key",
|
|
116
|
+
status: 401
|
|
117
|
+
}, BTP_ERROR_ARTIFACT_TIME_WINDOW = {
|
|
118
|
+
code: "BTP_ERROR_ARTIFACT_TIME_WINDOW",
|
|
119
|
+
message: "Artifact was not issued within the time window of this server",
|
|
120
|
+
status: 400
|
|
121
|
+
}, BTP_ERROR_ARTIFACT_TOO_LARGE = {
|
|
122
|
+
code: "BTP_ERROR_ARTIFACT_TOO_LARGE",
|
|
123
|
+
message: "Artifact exceeds the size limit of this server",
|
|
124
|
+
status: 413
|
|
125
|
+
}, BTP_ERROR_VERSION_UNSUPPORTED = {
|
|
126
|
+
code: "BTP_ERROR_VERSION_UNSUPPORTED",
|
|
127
|
+
message: "This server does not support the artifact protocol version",
|
|
128
|
+
status: 400
|
|
129
|
+
}, BTP_ERROR_PRIVACY_TYPE = {
|
|
130
|
+
code: "BTP_ERROR_PRIVACY_TYPE",
|
|
131
|
+
message: "Artifact does not use the encryption its trust requires",
|
|
132
|
+
status: 400
|
|
133
|
+
}, BTP_ERROR_ARTIFACT_NOT_ACCEPTED = {
|
|
134
|
+
code: "BTP_ERROR_ARTIFACT_NOT_ACCEPTED",
|
|
135
|
+
message: "This server does not accept this kind of artifact",
|
|
136
|
+
status: 503
|
|
137
|
+
}, BTP_ERROR_SOCKET_TIMEOUT = {
|
|
138
|
+
code: "BTP_ERROR_SOCKET_TIMEOUT",
|
|
139
|
+
message: "Socket timeout. Connection closed",
|
|
140
|
+
status: 408
|
|
141
|
+
}, BTP_ERROR_AUTHENTICATION_INVALID = {
|
|
142
|
+
code: "BTP_ERROR_AUTHENTICATION_INVALID",
|
|
143
|
+
message: "Authentication failed",
|
|
144
|
+
status: 401
|
|
145
|
+
}, BTP_ERROR_TIMEOUT = {
|
|
146
|
+
code: "BTP_ERROR_TIMEOUT",
|
|
147
|
+
message: "Timeout"
|
|
148
|
+
}, BTP_ERROR_CONNECTION_CLOSED = {
|
|
149
|
+
code: "BTP_ERROR_CONNECTION_CLOSED",
|
|
150
|
+
message: "Connection closed"
|
|
151
|
+
}, BTP_ERROR_CONNECTION_ENDED = {
|
|
152
|
+
code: "BTP_ERROR_CONNECTION_ENDED",
|
|
153
|
+
message: "Connection ended"
|
|
154
|
+
};
|
|
155
|
+
export {
|
|
156
|
+
BTPS_ERROR_ACTION_TYPE,
|
|
157
|
+
BTP_ERROR_AGENT_ID,
|
|
158
|
+
BTP_ERROR_ARTIFACT_NOT_ACCEPTED,
|
|
159
|
+
BTP_ERROR_ARTIFACT_TIME_WINDOW,
|
|
160
|
+
BTP_ERROR_ARTIFACT_TOO_LARGE,
|
|
161
|
+
BTP_ERROR_ATTESTATION_VERIFICATION,
|
|
162
|
+
BTP_ERROR_AUTHENTICATION_INVALID,
|
|
163
|
+
BTP_ERROR_CANONICAL_JSON,
|
|
164
|
+
BTP_ERROR_CONNECTION_CLOSED,
|
|
165
|
+
BTP_ERROR_CONNECTION_ENDED,
|
|
166
|
+
BTP_ERROR_DECRYPTION_UNINTENDED,
|
|
167
|
+
BTP_ERROR_DELEGATION_INVALID,
|
|
168
|
+
BTP_ERROR_DELEGATION_SIG_VERIFICATION,
|
|
169
|
+
BTP_ERROR_EVENT_UNRELATED,
|
|
170
|
+
BTP_ERROR_IDENTITY,
|
|
171
|
+
BTP_ERROR_IDENTITY_NOT_FOUND,
|
|
172
|
+
BTP_ERROR_INVALID_ACTION,
|
|
173
|
+
BTP_ERROR_INVALID_JSON,
|
|
174
|
+
BTP_ERROR_KEY_ROTATION_PROOF,
|
|
175
|
+
BTP_ERROR_PRIVACY_TYPE,
|
|
176
|
+
BTP_ERROR_RATE_LIMITER,
|
|
177
|
+
BTP_ERROR_RESOLVE_DNS,
|
|
178
|
+
BTP_ERROR_RESOLVE_PUBKEY,
|
|
179
|
+
BTP_ERROR_SIG_MISMATCH,
|
|
180
|
+
BTP_ERROR_SIG_VERIFICATION,
|
|
181
|
+
BTP_ERROR_SOCKET_CLOSED,
|
|
182
|
+
BTP_ERROR_SOCKET_TIMEOUT,
|
|
183
|
+
BTP_ERROR_TIMEOUT,
|
|
184
|
+
BTP_ERROR_TRUST_ALREADY_ACTIVE,
|
|
185
|
+
BTP_ERROR_TRUST_BLOCKED,
|
|
186
|
+
BTP_ERROR_TRUST_EXPIRED,
|
|
187
|
+
BTP_ERROR_TRUST_NON_EXISTENT,
|
|
188
|
+
BTP_ERROR_TRUST_NOT_ALLOWED,
|
|
189
|
+
BTP_ERROR_TRUST_REVOKED,
|
|
190
|
+
BTP_ERROR_UNKNOWN,
|
|
191
|
+
BTP_ERROR_UNSUPPORTED_ENCRYPT,
|
|
192
|
+
BTP_ERROR_VALIDATION,
|
|
193
|
+
BTP_ERROR_VERSION_UNSUPPORTED
|
|
194
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { BTPError } from './types.js';
|
|
8
|
+
export * from './constant.js';
|
|
9
|
+
export * from './types.js';
|
|
10
|
+
export declare class BTPErrorException extends Error {
|
|
11
|
+
readonly code?: string | number;
|
|
12
|
+
readonly status?: number;
|
|
13
|
+
readonly cause?: unknown;
|
|
14
|
+
readonly meta?: Record<string, unknown>;
|
|
15
|
+
constructor(btpError: BTPError, options?: {
|
|
16
|
+
cause?: unknown;
|
|
17
|
+
meta?: Record<string, unknown>;
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* Optional JSON representation for logs or APIs
|
|
21
|
+
*/
|
|
22
|
+
toJSON(): {
|
|
23
|
+
cause?: {} | undefined;
|
|
24
|
+
meta?: Record<string, unknown> | undefined;
|
|
25
|
+
message: string;
|
|
26
|
+
stack: string | undefined;
|
|
27
|
+
status?: number | undefined;
|
|
28
|
+
name: string;
|
|
29
|
+
code: string | number | undefined;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export declare const transformToBTPErrorException: (err: unknown, options?: {
|
|
33
|
+
cause?: unknown;
|
|
34
|
+
meta?: Record<string, unknown>;
|
|
35
|
+
}) => BTPErrorException;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { BTP_ERROR_UNKNOWN } from "./constant.js";
|
|
9
|
+
export * from "./constant.js";
|
|
10
|
+
export * from "./types.js";
|
|
11
|
+
class BTPErrorException extends Error {
|
|
12
|
+
code;
|
|
13
|
+
status;
|
|
14
|
+
cause;
|
|
15
|
+
meta;
|
|
16
|
+
constructor(btpError, options) {
|
|
17
|
+
super(btpError.message), this.name = "BTPErrorException", this.code = btpError.code, this.status = btpError.status, this.cause = options?.cause, this.meta = options?.meta, Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace && Error.captureStackTrace(this, BTPErrorException);
|
|
18
|
+
}
|
|
19
|
+
toJSON() {
|
|
20
|
+
return {
|
|
21
|
+
name: this.name,
|
|
22
|
+
code: this.code,
|
|
23
|
+
...this.status ? { status: this.status } : {},
|
|
24
|
+
message: this.message,
|
|
25
|
+
stack: this.stack,
|
|
26
|
+
...this.meta ? { meta: this.meta } : {},
|
|
27
|
+
...this.cause ? { cause: this.cause } : {}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const transformToBTPErrorException = (err, options) => {
|
|
32
|
+
if (err instanceof BTPErrorException)
|
|
33
|
+
return err;
|
|
34
|
+
if (err instanceof Error) {
|
|
35
|
+
const code = err?.code;
|
|
36
|
+
return new BTPErrorException(
|
|
37
|
+
{ message: err?.message ?? err.name, code: code ?? BTP_ERROR_UNKNOWN.code },
|
|
38
|
+
{
|
|
39
|
+
cause: options?.cause ?? err.cause,
|
|
40
|
+
meta: { ...err, ...options?.meta ?? {} }
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return new BTPErrorException(BTP_ERROR_UNKNOWN, {
|
|
45
|
+
cause: options?.cause ?? JSON.stringify(err),
|
|
46
|
+
meta: options?.meta ?? { err }
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
export {
|
|
50
|
+
BTPErrorException,
|
|
51
|
+
transformToBTPErrorException
|
|
52
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
export type BTPError = {
|
|
8
|
+
code?: string | number;
|
|
9
|
+
message: string;
|
|
10
|
+
/** The numeric status a server answers this error with (4xx: do not resend as is; 429 and 5xx: resend later) */
|
|
11
|
+
status?: number;
|
|
12
|
+
};
|
|
13
|
+
export type BTPErrorResponse<T = unknown> = {
|
|
14
|
+
data: T;
|
|
15
|
+
errors: BTPError[];
|
|
16
|
+
};
|