@pacspace-io/sdk 0.9.0 → 0.15.2
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/README.md +53 -132
- package/dist/blinding-file.d.ts +13 -0
- package/dist/blinding-file.d.ts.map +1 -0
- package/dist/blinding-guard.d.ts +7 -0
- package/dist/blinding-guard.d.ts.map +1 -0
- package/dist/blinding.d.ts +32 -0
- package/dist/blinding.d.ts.map +1 -0
- package/dist/client.d.ts +28 -4
- package/dist/client.d.ts.map +1 -1
- package/dist/fingerprint.d.ts +26 -5
- package/dist/fingerprint.d.ts.map +1 -1
- package/dist/index.d.ts +35 -35
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7366 -186
- package/dist/index.js.map +7 -1
- package/dist/inputs.txt +132 -0
- package/dist/records-crypto-libraries.json +42 -0
- package/dist/records-crypto-types/proof/hiding-commitment-v1.d.ts +19 -0
- package/dist/records-crypto-types/proof/jcs.d.ts +4 -0
- package/dist/records-crypto-types/proof/layout-document-v1.d.ts +72 -0
- package/dist/records-crypto-types/proof/ledger-head-v1.d.ts +22 -0
- package/dist/records-crypto-types/proof/merkle-v2.d.ts +5 -0
- package/dist/records-crypto-types/proof/payload-ref-v1.d.ts +17 -0
- package/dist/records-crypto-types/proof/seal-input.d.ts +2 -0
- package/dist/records-crypto-types/proof/semantic-receipt-v2.d.ts +89 -0
- package/dist/records-crypto-types/proof/semantic-receipt-v3.d.ts +20 -0
- package/dist/records-crypto-types/proof/slot-encoding-v1.d.ts +169 -0
- package/dist/records-crypto-types/records/flag-threads-v1.d.ts +53 -0
- package/dist/records-crypto-types/records/labels.d.ts +37 -0
- package/dist/records-crypto-types/records/reader-crypto-v1.d.ts +198 -0
- package/dist/records-crypto-types/records/reader-link-v1.d.ts +164 -0
- package/dist/records-crypto-types/records/shared-record-ack-v2.d.ts +72 -0
- package/dist/records-crypto-types/records/signed-acknowledgment-v1.d.ts +38 -0
- package/dist/records-crypto-types/records/signed-acknowledgment-v2.d.ts +12 -0
- package/dist/records-crypto-types/records/trail-index-v1.d.ts +89 -0
- package/dist/records-crypto-types/records/trail-rule.d.ts +54 -0
- package/dist/records-crypto-types/records-crypto/asymmetric/ack-flag-notes-v1.d.ts +57 -0
- package/dist/records-crypto-types/records-crypto/asymmetric/flag-notes-v1.d.ts +55 -0
- package/dist/records-crypto-types/records-crypto/asymmetric/index.d.ts +4 -0
- package/dist/records-crypto-types/records-crypto/asymmetric/reader-wraps-v1.d.ts +83 -0
- package/dist/records-crypto-types/records-crypto/asymmetric/records-asymmetric-v1.d.ts +201 -0
- package/dist/records-crypto-types/records-crypto/base64url.d.ts +3 -0
- package/dist/records-crypto-types/records-crypto/blinding-file.d.ts +16 -0
- package/dist/records-crypto-types/records-crypto/blinding-value.d.ts +10 -0
- package/dist/records-crypto-types/records-crypto/bundle-entry.d.ts +2 -0
- package/dist/records-crypto-types/records-crypto/disclosure-envelope-v1.d.ts +42 -0
- package/dist/records-crypto-types/records-crypto/held-file.d.ts +17 -0
- package/dist/records-crypto-types/records-crypto/hmac-node.d.ts +8 -0
- package/dist/records-crypto-types/records-crypto/hmac-web.d.ts +8 -0
- package/dist/records-crypto-types/records-crypto/index.d.ts +13 -0
- package/dist/records-crypto-types/records-crypto/platform-crypto.d.ts +9 -0
- package/dist/records-crypto-types/records-crypto/record-disclosure-v1.d.ts +171 -0
- package/dist/records-crypto-types/records-crypto/record-hash-v2.d.ts +49 -0
- package/dist/records-crypto-types/records-crypto/record-hash-v3.d.ts +36 -0
- package/dist/records-crypto-types/records-crypto/records-encryption-v1.d.ts +185 -0
- package/dist/records-crypto-types/records-crypto/wrap-shapes-v1.d.ts +23 -0
- package/dist/records-crypto.d.ts +1 -0
- package/dist/records-crypto.js +16105 -0
- package/dist/records-crypto.js.map +7 -0
- package/dist/records-encryption/base64url.d.ts +5 -0
- package/dist/records-encryption/base64url.d.ts.map +1 -0
- package/dist/records-encryption/encrypted-records.d.ts +338 -0
- package/dist/records-encryption/encrypted-records.d.ts.map +1 -0
- package/dist/records-encryption/index.d.ts +14 -0
- package/dist/records-encryption/index.d.ts.map +1 -0
- package/dist/records-encryption/keys.d.ts +40 -0
- package/dist/records-encryption/keys.d.ts.map +1 -0
- package/dist/records-encryption/loader.d.ts +15 -0
- package/dist/records-encryption/loader.d.ts.map +1 -0
- package/dist/records-encryption/pin-store-file.d.ts +8 -0
- package/dist/records-encryption/pin-store-file.d.ts.map +1 -0
- package/dist/records-encryption/pin-store.d.ts +17 -0
- package/dist/records-encryption/pin-store.d.ts.map +1 -0
- package/dist/records-encryption/transport.d.ts +23 -0
- package/dist/records-encryption/transport.d.ts.map +1 -0
- package/dist/records-encryption/words.d.ts +40 -0
- package/dist/records-encryption/words.d.ts.map +1 -0
- package/dist/records-encryption/workspace-secret.d.ts +9 -0
- package/dist/records-encryption/workspace-secret.d.ts.map +1 -0
- package/dist/resources/records.d.ts +112 -2
- package/dist/resources/records.d.ts.map +1 -1
- package/dist/types/common.d.ts +0 -8
- package/dist/types/common.d.ts.map +1 -1
- package/dist/types/config.d.ts +7 -5
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/records-encryption.d.ts +136 -0
- package/dist/types/records-encryption.d.ts.map +1 -0
- package/dist/types/records.d.ts +171 -20
- package/dist/types/records.d.ts.map +1 -1
- package/dist/types/verify.d.ts +84 -0
- package/dist/types/verify.d.ts.map +1 -0
- package/dist/webhooks/index.d.ts +1 -1
- package/dist/webhooks/index.d.ts.map +1 -1
- package/dist/webhooks/index.js +229 -5
- package/dist/webhooks/index.js.map +7 -1
- package/dist/webhooks/types.d.ts +15 -7
- package/dist/webhooks/types.d.ts.map +1 -1
- package/dist/workspace-secret-file.d.ts +18 -0
- package/dist/workspace-secret-file.d.ts.map +1 -0
- package/package.json +16 -13
- package/dist/client.js +0 -553
- package/dist/client.js.map +0 -1
- package/dist/errors/index.js +0 -223
- package/dist/errors/index.js.map +0 -1
- package/dist/fingerprint.js +0 -132
- package/dist/fingerprint.js.map +0 -1
- package/dist/resources/balance.d.ts +0 -384
- package/dist/resources/balance.d.ts.map +0 -1
- package/dist/resources/balance.js +0 -1120
- package/dist/resources/balance.js.map +0 -1
- package/dist/resources/customers.d.ts +0 -33
- package/dist/resources/customers.d.ts.map +0 -1
- package/dist/resources/customers.js +0 -26
- package/dist/resources/customers.js.map +0 -1
- package/dist/resources/internal/batch-transport.d.ts +0 -65
- package/dist/resources/internal/batch-transport.d.ts.map +0 -1
- package/dist/resources/internal/batch-transport.js +0 -42
- package/dist/resources/internal/batch-transport.js.map +0 -1
- package/dist/resources/receipts.d.ts +0 -46
- package/dist/resources/receipts.d.ts.map +0 -1
- package/dist/resources/receipts.js +0 -133
- package/dist/resources/receipts.js.map +0 -1
- package/dist/resources/records.js +0 -232
- package/dist/resources/records.js.map +0 -1
- package/dist/resources/submission.d.ts +0 -79
- package/dist/resources/submission.d.ts.map +0 -1
- package/dist/resources/submission.js +0 -398
- package/dist/resources/submission.js.map +0 -1
- package/dist/types/balance.d.ts +0 -966
- package/dist/types/balance.d.ts.map +0 -1
- package/dist/types/balance.js +0 -3
- package/dist/types/balance.js.map +0 -1
- package/dist/types/common.js +0 -3
- package/dist/types/common.js.map +0 -1
- package/dist/types/config.js +0 -3
- package/dist/types/config.js.map +0 -1
- package/dist/types/records.js +0 -5
- package/dist/types/records.js.map +0 -1
- package/dist/types/submission.d.ts +0 -125
- package/dist/types/submission.d.ts.map +0 -1
- package/dist/types/submission.js +0 -3
- package/dist/types/submission.js.map +0 -1
- package/dist/utils/polling.d.ts +0 -32
- package/dist/utils/polling.d.ts.map +0 -1
- package/dist/utils/polling.js +0 -52
- package/dist/utils/polling.js.map +0 -1
- package/dist/utils/retry.js +0 -21
- package/dist/utils/retry.js.map +0 -1
- package/dist/webhooks/types.js +0 -3
- package/dist/webhooks/types.js.map +0 -1
- package/dist/webhooks/verify.js +0 -191
- package/dist/webhooks/verify.js.map +0 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Developer-friendly alias: a content fingerprint uniquely identifies a
|
|
3
|
+
* single item's content.
|
|
4
|
+
*/
|
|
5
|
+
export type ContentFingerprint = string;
|
|
6
|
+
/**
|
|
7
|
+
* Summary of verified records.
|
|
8
|
+
*
|
|
9
|
+
* @deprecated Never returned by the verify endpoint since the move to
|
|
10
|
+
* proof-only verification (ADR 0008). Retained for compile compatibility.
|
|
11
|
+
*/
|
|
12
|
+
export interface VerifySummary {
|
|
13
|
+
recordCount: number;
|
|
14
|
+
netChange: number;
|
|
15
|
+
startingBalance: number;
|
|
16
|
+
endingBalance: number;
|
|
17
|
+
firstRecordAt: string | null;
|
|
18
|
+
lastRecordAt: string | null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A single record in the verify response.
|
|
22
|
+
*
|
|
23
|
+
* @deprecated Never returned by the verify endpoint since the move to
|
|
24
|
+
* proof-only verification (ADR 0008). Retained for compile compatibility.
|
|
25
|
+
*/
|
|
26
|
+
export interface VerifyRecord {
|
|
27
|
+
amount: number;
|
|
28
|
+
reason: string | null;
|
|
29
|
+
referenceId: string | null;
|
|
30
|
+
status: string;
|
|
31
|
+
time: string;
|
|
32
|
+
/** Content fingerprint: recompute from the original data to confirm nothing changed. */
|
|
33
|
+
itemFingerprint: ContentFingerprint | null;
|
|
34
|
+
}
|
|
35
|
+
/** Verification metadata from the verify endpoint. */
|
|
36
|
+
export interface VerifyVerification {
|
|
37
|
+
verificationReference: string | null;
|
|
38
|
+
verificationExplorerUrl: string | null;
|
|
39
|
+
batchId: string | null;
|
|
40
|
+
note: string;
|
|
41
|
+
}
|
|
42
|
+
export type VerifySource = 'db' | 'chain' | 'both';
|
|
43
|
+
export interface VerifySourceResult {
|
|
44
|
+
source: 'db' | 'chain';
|
|
45
|
+
available: boolean;
|
|
46
|
+
verified: boolean | null;
|
|
47
|
+
reason: string;
|
|
48
|
+
checkedAt: string;
|
|
49
|
+
proofRoot: string;
|
|
50
|
+
verificationReference: string | null;
|
|
51
|
+
chainId: number | null;
|
|
52
|
+
contractAddress: string | null;
|
|
53
|
+
observedProofRoot: string | null;
|
|
54
|
+
algorithm: string;
|
|
55
|
+
cache: 'none' | 'hit' | 'write';
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Response from the verify endpoint (no auth required).
|
|
59
|
+
* Anyone with the proof root can call this to independently confirm a proof
|
|
60
|
+
* exists. The response is proof-only: validity, timestamps, and chain
|
|
61
|
+
* references. Record contents are never returned (ADR 0008).
|
|
62
|
+
*/
|
|
63
|
+
export interface VerifyResponse {
|
|
64
|
+
verified: boolean;
|
|
65
|
+
processing?: boolean;
|
|
66
|
+
proofRoot: string;
|
|
67
|
+
chainId?: number;
|
|
68
|
+
contractAddress?: string | null;
|
|
69
|
+
recordedAt?: string;
|
|
70
|
+
environment?: string;
|
|
71
|
+
/** @deprecated Never returned; the verify endpoint is proof-only (ADR 0008). */
|
|
72
|
+
summary?: VerifySummary;
|
|
73
|
+
/** @deprecated Never returned; the verify endpoint is proof-only (ADR 0008). */
|
|
74
|
+
records?: VerifyRecord[];
|
|
75
|
+
verification?: VerifyVerification;
|
|
76
|
+
requestedSource?: VerifySource;
|
|
77
|
+
overallVerified?: boolean;
|
|
78
|
+
dbVerification?: VerifySourceResult;
|
|
79
|
+
chainVerification?: VerifySourceResult;
|
|
80
|
+
itemsRootAlgorithm?: string;
|
|
81
|
+
sourceConsistency?: 'consistent' | 'mismatch' | 'insufficient_data';
|
|
82
|
+
message: string;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=verify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../../src/types/verify.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAExC;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB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IAC7B,QAAQ,EAAE,OAAO,CAAC;IAClB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,eAAe,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gFAAgF;IAChF,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,gFAAgF;IAChF,OAAO,CAAC,EAAE,YAAY,EAAE,CAAC;IACzB,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAClC,eAAe,CAAC,EAAE,YAAY,CAAC;IAC/B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,cAAc,CAAC,EAAE,kBAAkB,CAAC;IACpC,iBAAiB,CAAC,EAAE,kBAAkB,CAAC;IACvC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,iBAAiB,CAAC,EAAE,YAAY,GAAG,UAAU,GAAG,mBAAmB,CAAC;IACpE,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
package/dist/webhooks/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { Webhooks, type VerifyOptions } from './verify';
|
|
2
|
-
export type { KnownWebhookEventType, WebhookEventType, WebhookEvent, WebhookEventMap, WebhookHeaders, DeltaVerifiedPayload, DeltaFailedPayload, } from './types';
|
|
2
|
+
export type { KnownWebhookEventType, WebhookEventType, WebhookEvent, WebhookEventMap, WebhookHeaders, DeltaVerifiedPayload, DeltaFailedPayload, RecordClosedPayload, } from './types';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/webhooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AACxD,YAAY,EACV,qBAAqB,EACrB,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,kBAAkB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/webhooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AACxD,YAAY,EACV,qBAAqB,EACrB,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,SAAS,CAAC"}
|
package/dist/webhooks/index.js
CHANGED
|
@@ -1,6 +1,230 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/webhooks/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Webhooks: () => Webhooks
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/webhooks/verify.ts
|
|
28
|
+
var import_crypto = require("crypto");
|
|
29
|
+
|
|
30
|
+
// src/errors/index.ts
|
|
31
|
+
var PacSpaceError = class extends Error {
|
|
32
|
+
constructor(message, statusCode, code, requestPath) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.name = "PacSpaceError";
|
|
35
|
+
this.statusCode = statusCode;
|
|
36
|
+
this.code = code;
|
|
37
|
+
this.requestPath = requestPath;
|
|
38
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var WebhookVerificationError = class extends PacSpaceError {
|
|
42
|
+
constructor(message = "Webhook signature verification failed") {
|
|
43
|
+
super(message, 0, "WEBHOOK_VERIFICATION_FAILED");
|
|
44
|
+
this.name = "WebhookVerificationError";
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/webhooks/verify.ts
|
|
49
|
+
var Webhooks = class {
|
|
50
|
+
/**
|
|
51
|
+
* @param secret - Your webhook signing secret (from the PacSpace dashboard).
|
|
52
|
+
*/
|
|
53
|
+
constructor(secret) {
|
|
54
|
+
if (!secret) {
|
|
55
|
+
throw new WebhookVerificationError(
|
|
56
|
+
"Webhook secret is required. Get it from your PacSpace dashboard."
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
this.secret = secret;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Verify a webhook payload and return the typed event.
|
|
63
|
+
*
|
|
64
|
+
* @param signature - The `X-PacSpace-Signature` header value.
|
|
65
|
+
* @param timestamp - The `X-PacSpace-Timestamp` header value.
|
|
66
|
+
* @param rawBody - The raw request body as a string (before JSON parsing).
|
|
67
|
+
* @param options - Verification options (timestamp tolerance).
|
|
68
|
+
* @returns The parsed and verified webhook event.
|
|
69
|
+
* @throws WebhookVerificationError if verification fails.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const event = pac.webhooks.verify(
|
|
74
|
+
* req.headers['x-pacspace-signature'],
|
|
75
|
+
* req.headers['x-pacspace-timestamp'],
|
|
76
|
+
* req.rawBody,
|
|
77
|
+
* );
|
|
78
|
+
*
|
|
79
|
+
* if (event.event === 'delta.verified') {
|
|
80
|
+
* console.log(event.data.receiptId);
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
verify(signature, timestamp, rawBody, options) {
|
|
85
|
+
if (!signature || !timestamp || !rawBody) {
|
|
86
|
+
throw new WebhookVerificationError(
|
|
87
|
+
"Missing required parameters: signature, timestamp, and rawBody are all required."
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
this.validateTimestamp(timestamp, options?.tolerance ?? 300);
|
|
91
|
+
const signedContent = `${timestamp}.${rawBody}`;
|
|
92
|
+
const expected = Buffer.from(
|
|
93
|
+
"v1=" + (0, import_crypto.createHmac)("sha256", this.secret).update(signedContent).digest("hex")
|
|
94
|
+
);
|
|
95
|
+
const matched = signature.split(",").map((part) => part.trim()).filter((part) => part.startsWith("v1=")).some((candidate) => {
|
|
96
|
+
const received = Buffer.from(candidate);
|
|
97
|
+
return received.length === expected.length && (0, import_crypto.timingSafeEqual)(expected, received);
|
|
98
|
+
});
|
|
99
|
+
if (!matched) {
|
|
100
|
+
throw new WebhookVerificationError(
|
|
101
|
+
"Signature mismatch. Ensure you are using the correct webhook secret and the raw (unparsed) request body."
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
return JSON.parse(rawBody);
|
|
106
|
+
} catch {
|
|
107
|
+
throw new WebhookVerificationError(
|
|
108
|
+
"Failed to parse webhook payload as JSON."
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Verify using a headers object (convenience method).
|
|
114
|
+
*
|
|
115
|
+
* Extracts `x-pacspace-signature` and `x-pacspace-timestamp` from headers.
|
|
116
|
+
*
|
|
117
|
+
* @param headers - Headers object (Express req.headers or similar).
|
|
118
|
+
* @param rawBody - The raw request body as a string.
|
|
119
|
+
* @param options - Verification options.
|
|
120
|
+
* @returns The parsed and verified webhook event.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const event = pac.webhooks.verifyFromHeaders(req.headers, req.rawBody);
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
verifyFromHeaders(headers, rawBody, options) {
|
|
128
|
+
const signature = this.getHeader(headers, "x-pacspace-signature");
|
|
129
|
+
const timestamp = this.getHeader(headers, "x-pacspace-timestamp");
|
|
130
|
+
if (!signature) {
|
|
131
|
+
throw new WebhookVerificationError(
|
|
132
|
+
"Missing X-PacSpace-Signature header."
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
if (!timestamp) {
|
|
136
|
+
throw new WebhookVerificationError(
|
|
137
|
+
"Missing X-PacSpace-Timestamp header."
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return this.verify(signature, timestamp, rawBody, options);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Create an Express/Connect-compatible middleware for webhook verification.
|
|
144
|
+
*
|
|
145
|
+
* The middleware:
|
|
146
|
+
* 1. Verifies the HMAC signature
|
|
147
|
+
* 2. Validates the timestamp
|
|
148
|
+
* 3. Attaches the parsed event to `req.pacspaceEvent`
|
|
149
|
+
* 4. Calls `next()` on success, or responds with 401 on failure
|
|
150
|
+
*
|
|
151
|
+
* **Important:** Ensure raw body parsing is enabled before this middleware.
|
|
152
|
+
*
|
|
153
|
+
* @param options - Verification options.
|
|
154
|
+
* @returns Express middleware function.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* // Capture raw body (required for signature verification)
|
|
159
|
+
* app.use('/webhooks', express.json({
|
|
160
|
+
* verify: (req, _res, buf) => { req.rawBody = buf.toString(); }
|
|
161
|
+
* }));
|
|
162
|
+
*
|
|
163
|
+
* app.post('/webhooks/pacspace', pac.webhooks.middleware(), (req, res) => {
|
|
164
|
+
* const event = req.pacspaceEvent;
|
|
165
|
+
* console.log(`Received ${event.event}:`, event.data);
|
|
166
|
+
* res.status(200).json({ received: true });
|
|
167
|
+
* });
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
middleware(options) {
|
|
171
|
+
return (req, res, next) => {
|
|
172
|
+
try {
|
|
173
|
+
const rawBody = req.rawBody || JSON.stringify(req.body);
|
|
174
|
+
const event = this.verifyFromHeaders(req.headers, rawBody, options);
|
|
175
|
+
req.pacspaceEvent = event;
|
|
176
|
+
next();
|
|
177
|
+
} catch (err) {
|
|
178
|
+
if (err instanceof WebhookVerificationError) {
|
|
179
|
+
res.status(401).json({ error: err.message });
|
|
180
|
+
} else {
|
|
181
|
+
next(err);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Validate that the timestamp is within the tolerance window.
|
|
188
|
+
*/
|
|
189
|
+
validateTimestamp(timestamp, toleranceSeconds) {
|
|
190
|
+
const webhookTimeSeconds = this.parseTimestampToSeconds(timestamp);
|
|
191
|
+
if (Number.isNaN(webhookTimeSeconds)) {
|
|
192
|
+
throw new WebhookVerificationError(
|
|
193
|
+
`Invalid timestamp: "${timestamp}". Expected Unix epoch in seconds or milliseconds.`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
const currentTime = Math.floor(Date.now() / 1e3);
|
|
197
|
+
const age = Math.abs(currentTime - webhookTimeSeconds);
|
|
198
|
+
if (age > toleranceSeconds) {
|
|
199
|
+
throw new WebhookVerificationError(
|
|
200
|
+
`Timestamp too old (${age}s > ${toleranceSeconds}s tolerance). This may be a replay attack, or your server clock is skewed.`
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Convert epoch timestamp string to seconds.
|
|
206
|
+
* Accepts both second and millisecond precision.
|
|
207
|
+
*/
|
|
208
|
+
parseTimestampToSeconds(timestamp) {
|
|
209
|
+
const raw = Number.parseInt(timestamp, 10);
|
|
210
|
+
if (!Number.isFinite(raw)) {
|
|
211
|
+
return Number.NaN;
|
|
212
|
+
}
|
|
213
|
+
if (Math.abs(raw) >= 1e12) {
|
|
214
|
+
return Math.floor(raw / 1e3);
|
|
215
|
+
}
|
|
216
|
+
return raw;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Extract a header value from a headers object (handles arrays).
|
|
220
|
+
*/
|
|
221
|
+
getHeader(headers, key) {
|
|
222
|
+
const value = headers[key] ?? headers[key.toLowerCase()];
|
|
223
|
+
return Array.isArray(value) ? value[0] : value;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
227
|
+
0 && (module.exports = {
|
|
228
|
+
Webhooks
|
|
229
|
+
});
|
|
230
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/webhooks/index.ts", "../../src/webhooks/verify.ts", "../../src/errors/index.ts"],
|
|
4
|
+
"sourcesContent": ["export { Webhooks, type VerifyOptions } from './verify';\nexport type {\n KnownWebhookEventType,\n WebhookEventType,\n WebhookEvent,\n WebhookEventMap,\n WebhookHeaders,\n DeltaVerifiedPayload,\n DeltaFailedPayload,\n RecordClosedPayload,\n} from './types';\n", "import { createHmac, timingSafeEqual } from 'crypto';\nimport { WebhookVerificationError } from '../errors';\nimport type {\n WebhookEvent,\n WebhookHeaders,\n} from './types';\n\n/**\n * Options for webhook verification.\n */\nexport interface VerifyOptions {\n /**\n * Maximum age (seconds) of the timestamp before rejecting.\n * Protects against replay attacks.\n * @default 300 (5 minutes)\n */\n tolerance?: number;\n}\n\n/**\n * PacSpace Webhook verification and middleware.\n *\n * Provides HMAC-SHA256 signature verification for webhook payloads\n * using the algorithm documented at:\n * https://docs.pacspace.io/webhooks/signature-verification\n */\nexport class Webhooks {\n private readonly secret: string;\n\n /**\n * @param secret - Your webhook signing secret (from the PacSpace dashboard).\n */\n constructor(secret: string) {\n if (!secret) {\n throw new WebhookVerificationError(\n 'Webhook secret is required. Get it from your PacSpace dashboard.',\n );\n }\n this.secret = secret;\n }\n\n /**\n * Verify a webhook payload and return the typed event.\n *\n * @param signature - The `X-PacSpace-Signature` header value.\n * @param timestamp - The `X-PacSpace-Timestamp` header value.\n * @param rawBody - The raw request body as a string (before JSON parsing).\n * @param options - Verification options (timestamp tolerance).\n * @returns The parsed and verified webhook event.\n * @throws WebhookVerificationError if verification fails.\n *\n * @example\n * ```typescript\n * const event = pac.webhooks.verify(\n * req.headers['x-pacspace-signature'],\n * req.headers['x-pacspace-timestamp'],\n * req.rawBody,\n * );\n *\n * if (event.event === 'delta.verified') {\n * console.log(event.data.receiptId);\n * }\n * ```\n */\n verify(\n signature: string,\n timestamp: string,\n rawBody: string,\n options?: VerifyOptions,\n ): WebhookEvent {\n if (!signature || !timestamp || !rawBody) {\n throw new WebhookVerificationError(\n 'Missing required parameters: signature, timestamp, and rawBody are all required.',\n );\n }\n\n // Validate timestamp to prevent replay attacks\n this.validateTimestamp(timestamp, options?.tolerance ?? 300);\n\n // Reconstruct the signed content: \"{timestamp}.{rawBody}\"\n const signedContent = `${timestamp}.${rawBody}`;\n\n // Compute HMAC-SHA256\n const expected = Buffer.from(\n 'v1=' +\n createHmac('sha256', this.secret).update(signedContent).digest('hex'),\n );\n\n // The header is one `v1=<hex>`, or, for 24 hours after a secret rotation\n // with overlap, two of them comma separated (the current secret's first,\n // then the previous secret's). The event is genuine when any one of them\n // matches this secret; each comparison is constant time.\n const matched = signature\n .split(',')\n .map((part) => part.trim())\n .filter((part) => part.startsWith('v1='))\n .some((candidate) => {\n const received = Buffer.from(candidate);\n return (\n received.length === expected.length &&\n timingSafeEqual(expected, received)\n );\n });\n\n if (!matched) {\n throw new WebhookVerificationError(\n 'Signature mismatch. Ensure you are using the correct webhook secret and the raw (unparsed) request body.',\n );\n }\n\n // Parse and return the event\n try {\n return JSON.parse(rawBody) as WebhookEvent;\n } catch {\n throw new WebhookVerificationError(\n 'Failed to parse webhook payload as JSON.',\n );\n }\n }\n\n /**\n * Verify using a headers object (convenience method).\n *\n * Extracts `x-pacspace-signature` and `x-pacspace-timestamp` from headers.\n *\n * @param headers - Headers object (Express req.headers or similar).\n * @param rawBody - The raw request body as a string.\n * @param options - Verification options.\n * @returns The parsed and verified webhook event.\n *\n * @example\n * ```typescript\n * const event = pac.webhooks.verifyFromHeaders(req.headers, req.rawBody);\n * ```\n */\n verifyFromHeaders(\n headers: WebhookHeaders | Record<string, string | string[] | undefined>,\n rawBody: string,\n options?: VerifyOptions,\n ): WebhookEvent {\n const signature = this.getHeader(headers, 'x-pacspace-signature');\n const timestamp = this.getHeader(headers, 'x-pacspace-timestamp');\n\n if (!signature) {\n throw new WebhookVerificationError(\n 'Missing X-PacSpace-Signature header.',\n );\n }\n if (!timestamp) {\n throw new WebhookVerificationError(\n 'Missing X-PacSpace-Timestamp header.',\n );\n }\n\n return this.verify(signature, timestamp, rawBody, options);\n }\n\n /**\n * Create an Express/Connect-compatible middleware for webhook verification.\n *\n * The middleware:\n * 1. Verifies the HMAC signature\n * 2. Validates the timestamp\n * 3. Attaches the parsed event to `req.pacspaceEvent`\n * 4. Calls `next()` on success, or responds with 401 on failure\n *\n * **Important:** Ensure raw body parsing is enabled before this middleware.\n *\n * @param options - Verification options.\n * @returns Express middleware function.\n *\n * @example\n * ```typescript\n * // Capture raw body (required for signature verification)\n * app.use('/webhooks', express.json({\n * verify: (req, _res, buf) => { req.rawBody = buf.toString(); }\n * }));\n *\n * app.post('/webhooks/pacspace', pac.webhooks.middleware(), (req, res) => {\n * const event = req.pacspaceEvent;\n * console.log(`Received ${event.event}:`, event.data);\n * res.status(200).json({ received: true });\n * });\n * ```\n */\n middleware(options?: VerifyOptions) {\n return (req: any, res: any, next: any) => {\n try {\n const rawBody = req.rawBody || JSON.stringify(req.body);\n const event = this.verifyFromHeaders(req.headers, rawBody, options);\n req.pacspaceEvent = event;\n next();\n } catch (err) {\n if (err instanceof WebhookVerificationError) {\n res.status(401).json({ error: err.message });\n } else {\n next(err);\n }\n }\n };\n }\n\n /**\n * Validate that the timestamp is within the tolerance window.\n */\n private validateTimestamp(timestamp: string, toleranceSeconds: number): void {\n const webhookTimeSeconds = this.parseTimestampToSeconds(timestamp);\n if (Number.isNaN(webhookTimeSeconds)) {\n throw new WebhookVerificationError(\n `Invalid timestamp: \"${timestamp}\". Expected Unix epoch in seconds or milliseconds.`,\n );\n }\n\n const currentTime = Math.floor(Date.now() / 1000);\n const age = Math.abs(currentTime - webhookTimeSeconds);\n\n if (age > toleranceSeconds) {\n throw new WebhookVerificationError(\n `Timestamp too old (${age}s > ${toleranceSeconds}s tolerance). This may be a replay attack, or your server clock is skewed.`,\n );\n }\n }\n\n /**\n * Convert epoch timestamp string to seconds.\n * Accepts both second and millisecond precision.\n */\n private parseTimestampToSeconds(timestamp: string): number {\n const raw = Number.parseInt(timestamp, 10);\n if (!Number.isFinite(raw)) {\n return Number.NaN;\n }\n // 13+ digits => milliseconds in current webhook workers.\n if (Math.abs(raw) >= 1_000_000_000_000) {\n return Math.floor(raw / 1000);\n }\n return raw;\n }\n\n /**\n * Extract a header value from a headers object (handles arrays).\n */\n private getHeader(\n headers: Record<string, string | string[] | undefined>,\n key: string,\n ): string | undefined {\n const value = headers[key] ?? headers[key.toLowerCase()];\n return Array.isArray(value) ? value[0] : value;\n }\n}\n", "/**\n * Base error class for all PacSpace SDK errors.\n * Extends native Error with structured API error data.\n */\nexport class PacSpaceError extends Error {\n /** HTTP status code from the API response. */\n readonly statusCode: number;\n /** Machine-readable error code (e.g., 'INSUFFICIENT_CREDITS'). */\n readonly code: string;\n /** The original API request path that triggered this error. */\n readonly requestPath?: string;\n\n constructor(\n message: string,\n statusCode: number,\n code: string,\n requestPath?: string,\n ) {\n super(message);\n this.name = 'PacSpaceError';\n this.statusCode = statusCode;\n this.code = code;\n this.requestPath = requestPath;\n\n // Ensure instanceof works correctly in TS\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/**\n * Thrown when the API key is missing, invalid, or disabled (HTTP 401).\n */\nexport class InvalidApiKeyError extends PacSpaceError {\n constructor(message = 'Invalid or missing API key', requestPath?: string) {\n super(message, 401, 'INVALID_API_KEY', requestPath);\n this.name = 'InvalidApiKeyError';\n }\n}\n\n/**\n * Thrown when the tenant has insufficient credits for the operation (HTTP 402).\n */\nexport class InsufficientCreditsError extends PacSpaceError {\n constructor(\n message = 'Insufficient credits for this operation',\n requestPath?: string,\n ) {\n super(message, 402, 'INSUFFICIENT_CREDITS', requestPath);\n this.name = 'InsufficientCreditsError';\n }\n}\n\n/**\n * Thrown when the requested resource is not found (HTTP 404).\n */\nexport class NotFoundError extends PacSpaceError {\n constructor(message = 'Resource not found', requestPath?: string) {\n super(message, 404, 'NOT_FOUND', requestPath);\n this.name = 'NotFoundError';\n }\n}\n\n/**\n * Thrown when a receipt cannot be found (HTTP 404).\n */\nexport class ReceiptNotFoundError extends PacSpaceError {\n constructor(message = 'Receipt not found', requestPath?: string) {\n super(message, 404, 'RECEIPT_NOT_FOUND', requestPath);\n this.name = 'ReceiptNotFoundError';\n }\n}\n\n/**\n * Thrown when a receipt exists but is not ready yet (HTTP 409).\n */\nexport class ReceiptNotReadyError extends PacSpaceError {\n constructor(message = 'Receipt is not ready yet', requestPath?: string) {\n super(message, 409, 'RECEIPT_NOT_READY', requestPath);\n this.name = 'ReceiptNotReadyError';\n }\n}\n\n/**\n * Thrown when no contract is deployed for the tenant (HTTP 412).\n */\nexport class ContractNotDeployedError extends PacSpaceError {\n constructor(\n message = 'No contract deployed. Provision an environment first.',\n requestPath?: string,\n ) {\n super(message, 412, 'CONTRACT_NOT_DEPLOYED', requestPath);\n this.name = 'ContractNotDeployedError';\n }\n}\n\n/**\n * Thrown when the API rate limit is exceeded (HTTP 429).\n */\nexport class RateLimitError extends PacSpaceError {\n /** Seconds to wait before retrying, if provided by the API. */\n readonly retryAfter: number | null;\n\n constructor(\n message = 'Rate limit exceeded',\n retryAfter: number | null = null,\n requestPath?: string,\n ) {\n super(message, 429, 'RATE_LIMITED', requestPath);\n this.name = 'RateLimitError';\n this.retryAfter = retryAfter;\n }\n}\n\n/**\n * Thrown when a customer's submission cadence window has not elapsed (HTTP 429).\n * This error is terminal for the current request and should not be auto-retried.\n */\nexport class CadenceLimitError extends PacSpaceError {\n /** Milliseconds remaining before the next allowed submission, if provided. */\n readonly retryAfterMs: number | null;\n /** Customer ID associated with the cadence block, when provided. */\n readonly customerId: string | null;\n\n constructor(\n message = 'Submission cadence limit reached',\n retryAfterSeconds: number | null = null,\n customerId: string | null = null,\n requestPath?: string,\n ) {\n super(message, 429, 'CADENCE_LIMIT', requestPath);\n this.name = 'CadenceLimitError';\n this.retryAfterMs =\n retryAfterSeconds && Number.isFinite(retryAfterSeconds)\n ? Math.max(0, Math.floor(retryAfterSeconds * 1000))\n : null;\n this.customerId = customerId;\n }\n}\n\n/**\n * Thrown when the API is temporarily unavailable (HTTP 503).\n */\nexport class ServiceUnavailableError extends PacSpaceError {\n /** Seconds to wait before retrying, if provided by the API. */\n readonly retryAfter: number | null;\n\n constructor(\n message = 'Service temporarily unavailable',\n retryAfter: number | null = null,\n requestPath?: string,\n ) {\n super(message, 503, 'SERVICE_UNAVAILABLE', requestPath);\n this.name = 'ServiceUnavailableError';\n this.retryAfter = retryAfter;\n }\n}\n\n/**\n * Thrown when the API returns a validation error (HTTP 400).\n */\nexport class ValidationError extends PacSpaceError {\n constructor(message = 'Invalid request data', requestPath?: string) {\n super(message, 400, 'VALIDATION_ERROR', requestPath);\n this.name = 'ValidationError';\n }\n}\n\n/**\n * Thrown when a request mixes mutually-exclusive scope fields (HTTP 400).\n */\nexport class InvalidScopeCombinationError extends PacSpaceError {\n constructor(\n message = 'Choose either a checkpoint or a time scope for this request.',\n requestPath?: string,\n ) {\n super(message, 400, 'INVALID_SCOPE_COMBINATION', requestPath);\n this.name = 'InvalidScopeCombinationError';\n }\n}\n\n/**\n * Thrown when the requested scope exceeds the maximum supported window (HTTP 400).\n */\nexport class ScopeTooWideError extends PacSpaceError {\n constructor(\n message = 'Scope window is too wide for a single request.',\n requestPath?: string,\n ) {\n super(message, 400, 'SCOPE_TOO_WIDE', requestPath);\n this.name = 'ScopeTooWideError';\n }\n}\n\n/**\n * Thrown when a polling operation exceeds the configured timeout.\n */\nexport class TimeoutError extends PacSpaceError {\n constructor(\n message = 'Operation timed out waiting for verification',\n requestPath?: string,\n ) {\n super(message, 0, 'TIMEOUT', requestPath);\n this.name = 'TimeoutError';\n }\n}\n\n/**\n * Thrown when the webhook signature is invalid.\n */\nexport class WebhookVerificationError extends PacSpaceError {\n constructor(message = 'Webhook signature verification failed') {\n super(message, 0, 'WEBHOOK_VERIFICATION_FAILED');\n this.name = 'WebhookVerificationError';\n }\n}\n\n/**\n * Map an HTTP status code + API response to the appropriate typed error.\n * @internal\n */\nexport function mapApiError(\n statusCode: number,\n message: string,\n requestPath?: string,\n headers?: Headers,\n apiError?: {\n code?: string;\n retryAfterSeconds?: number | null;\n customerId?: string | null;\n },\n): PacSpaceError {\n const headerRetryAfter = headers?.get('retry-after');\n const parsedHeaderRetryAfter = headerRetryAfter\n ? Number.parseInt(headerRetryAfter, 10)\n : null;\n const retryAfterSeconds =\n apiError?.retryAfterSeconds ??\n (Number.isFinite(parsedHeaderRetryAfter)\n ? parsedHeaderRetryAfter\n : null);\n\n switch (statusCode) {\n case 400:\n if (apiError?.code === 'INVALID_SCOPE_COMBINATION') {\n return new InvalidScopeCombinationError(message, requestPath);\n }\n if (apiError?.code === 'SCOPE_TOO_WIDE') {\n return new ScopeTooWideError(message, requestPath);\n }\n if (apiError?.code) {\n return new PacSpaceError(message, statusCode, apiError.code, requestPath);\n }\n return new ValidationError(message, requestPath);\n case 401:\n return new InvalidApiKeyError(message, requestPath);\n case 402:\n return new InsufficientCreditsError(message, requestPath);\n case 404:\n if (apiError?.code === 'RECEIPT_NOT_FOUND') {\n return new ReceiptNotFoundError(message, requestPath);\n }\n return new NotFoundError(message, requestPath);\n case 409:\n if (apiError?.code === 'RECEIPT_NOT_READY') {\n return new ReceiptNotReadyError(message, requestPath);\n }\n return new PacSpaceError(message, statusCode, apiError?.code || 'CONFLICT', requestPath);\n case 412:\n return new ContractNotDeployedError(message, requestPath);\n case 429: {\n if (apiError?.code === 'CADENCE_LIMIT') {\n return new CadenceLimitError(\n message,\n retryAfterSeconds,\n apiError.customerId ?? null,\n requestPath,\n );\n }\n return new RateLimitError(\n message,\n retryAfterSeconds,\n requestPath,\n );\n }\n case 503: {\n return new ServiceUnavailableError(\n message,\n retryAfterSeconds,\n requestPath,\n );\n }\n default:\n return new PacSpaceError(\n message,\n statusCode,\n apiError?.code || 'API_ERROR',\n requestPath,\n );\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAA4C;;;ACIrC,IAAM,gBAAN,cAA4B,MAAM;AAAA,EAQvC,YACE,SACA,YACA,MACA,aACA;AACA,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,OAAO;AACZ,SAAK,cAAc;AAGnB,WAAO,eAAe,MAAM,WAAW,SAAS;AAAA,EAClD;AACF;AAsLO,IAAM,2BAAN,cAAuC,cAAc;AAAA,EAC1D,YAAY,UAAU,yCAAyC;AAC7D,UAAM,SAAS,GAAG,6BAA6B;AAC/C,SAAK,OAAO;AAAA,EACd;AACF;;;AD5LO,IAAM,WAAN,MAAe;AAAA;AAAA;AAAA;AAAA,EAMpB,YAAY,QAAgB;AAC1B,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI;AAAA,QACR;AA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D,UAAM,MAAM,OAAO,SAAS,WAAW,EAAE;AACzC,QAAI,CAAC,OAAO,SAAS,GAAG,GAAG;AACzB,aAAO,OAAO;AAAA,IAChB;AAEA,QAAI,KAAK,IAAI,GAAG,KAAK,MAAmB;AACtC,aAAO,KAAK,MAAM,MAAM,GAAI;AAAA,IAC9B;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKQ,UACN,SACA,KACoB;AACpB,UAAM,QAAQ,QAAQ,GAAG,KAAK,QAAQ,IAAI,YAAY,CAAC;AACvD,WAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,CAAC,IAAI;AAAA,EAC3C;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/webhooks/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Known webhook events currently emitted by production workers.
|
|
3
3
|
*/
|
|
4
|
-
export type KnownWebhookEventType = 'delta.verified' | 'delta.failed' | '
|
|
4
|
+
export type KnownWebhookEventType = 'delta.verified' | 'delta.failed' | 'record.committed' | 'record.failed' | 'record.closed';
|
|
5
5
|
/**
|
|
6
6
|
* Event type union with forward-compat support for unknown future events.
|
|
7
7
|
*/
|
|
@@ -101,11 +101,19 @@ export interface RecordFailedPayload {
|
|
|
101
101
|
code: string;
|
|
102
102
|
sentence: string;
|
|
103
103
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
104
|
+
/**
|
|
105
|
+
* A record was closed, by your code (`records.close`) or by PacSpace after
|
|
106
|
+
* the source's timeout. A close writes no entry. `entityId` is the wire id
|
|
107
|
+
* of an encrypted record; `lastSeq` is the wire's number, counted from 0.
|
|
108
|
+
*/
|
|
109
|
+
export interface RecordClosedPayload {
|
|
110
|
+
recordType: string;
|
|
111
|
+
entityId: string;
|
|
112
|
+
closedBy: 'code' | 'timeout';
|
|
113
|
+
lastSeq: number;
|
|
114
|
+
lastSeal: string | null;
|
|
115
|
+
/** The day of the close, YYYY-MM-DD (UTC). */
|
|
116
|
+
closedOn: string;
|
|
109
117
|
}
|
|
110
118
|
/**
|
|
111
119
|
* Map from event type to its payload shape.
|
|
@@ -113,9 +121,9 @@ export interface CustomerCreatedPayload {
|
|
|
113
121
|
export interface WebhookEventMap {
|
|
114
122
|
'delta.verified': DeltaVerifiedPayload;
|
|
115
123
|
'delta.failed': DeltaFailedPayload;
|
|
116
|
-
'customer.created': CustomerCreatedPayload;
|
|
117
124
|
'record.committed': RecordCommittedPayload;
|
|
118
125
|
'record.failed': RecordFailedPayload;
|
|
126
|
+
'record.closed': RecordClosedPayload;
|
|
119
127
|
}
|
|
120
128
|
/**
|
|
121
129
|
* A typed webhook event.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/webhooks/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAC7B,gBAAgB,GAChB,cAAc,GACd,kBAAkB,GAClB,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/webhooks/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAC7B,gBAAgB,GAChB,cAAc,GACd,kBAAkB,GAClB,eAAe,GACf,eAAe,CAAC;AAEpB;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,qBAAqB,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAMrE,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,MAAM,EAAE,UAAU,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,cAAc,EAAE,MAAM,CAAC;IAEvB,KAAK,EAAE;QACL,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;QAC1B,MAAM,EAAE,MAAM,GAAG,IAAI,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jB,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,8CAA8C;IAC9C,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,gBAAgB,EAAE,oBAAoB,CAAC;IACvC,cAAc,EAAE,kBAAkB,CAAC;IACnC,kBAAkB,EAAE,sBAAsB,CAAC;IAC3C,eAAe,EAAE,mBAAmB,CAAC;IACrC,eAAe,EAAE,mBAAmB,CAAC;CACtC;AAED;;GAEG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,gBAAgB,GAAG,gBAAgB;IACzE,sBAAsB;IACtB,KAAK,EAAE,CAAC,CAAC;IACT,uDAAuD;IACvD,SAAS,EAAE,MAAM,CAAC;IAClB,2BAA2B;IAC3B,IAAI,EAAE,CAAC,SAAS,qBAAqB,GAAG,eAAe,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;CACtE;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,sBAAsB,EAAE,MAAM,CAAC;IAC/B;;;OAGG;IACH,sBAAsB,EAAE,MAAM,CAAC;IAC/B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACnC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type WorkspaceSecretEnvironment = 'sandbox' | 'production';
|
|
2
|
+
/**
|
|
3
|
+
* The name the dashboard downloads a workspace secret file under. Sandbox's
|
|
4
|
+
* says so, so the two environments' files never share a name (EN1, D-20):
|
|
5
|
+
* `pacspace-workspace-secret-sandbox-v1.json`, and `pacspace-workspace-secret-v1.json`
|
|
6
|
+
* for Production.
|
|
7
|
+
*/
|
|
8
|
+
export declare function workspaceSecretFileName(version: number, environment?: WorkspaceSecretEnvironment): string;
|
|
9
|
+
/**
|
|
10
|
+
* Read a workspace secret file, for `records: { secret }`. The file's name
|
|
11
|
+
* doesn't matter: Sandbox's `pacspace-workspace-secret-sandbox-vN.json` and
|
|
12
|
+
* the SDK's own `pacspace-workspace-secret-vN.json` read the same. Anything
|
|
13
|
+
* that isn't a pacspace-workspace-secret/v1 file is refused
|
|
14
|
+
* (RECORDS_SECRET_INVALID). Node only. The text is returned and kept
|
|
15
|
+
* nowhere else.
|
|
16
|
+
*/
|
|
17
|
+
export declare function readWorkspaceSecretFile(path: string): Promise<string>;
|
|
18
|
+
//# sourceMappingURL=workspace-secret-file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace-secret-file.d.ts","sourceRoot":"","sources":["../src/workspace-secret-file.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,0BAA0B,GAAG,SAAS,GAAG,YAAY,CAAC;AAElE;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,GAAE,0BAAyC,GAAG,MAAM,CAGvH;AAOD;;;;;;;GAOG;AACH,wBAAsB,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAmB3E"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pacspace-io/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Official PacSpace
|
|
3
|
+
"version": "0.15.2",
|
|
4
|
+
"description": "Official PacSpace API SDK: TypeScript client for records, with the record check bundled",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"exports": {
|
|
@@ -12,43 +12,46 @@
|
|
|
12
12
|
"./webhooks": {
|
|
13
13
|
"types": "./dist/webhooks/index.d.ts",
|
|
14
14
|
"default": "./dist/webhooks/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./records-crypto": {
|
|
17
|
+
"types": "./dist/records-crypto.d.ts",
|
|
18
|
+
"default": "./dist/records-crypto.js"
|
|
15
19
|
}
|
|
16
20
|
},
|
|
17
21
|
"files": [
|
|
18
22
|
"dist"
|
|
19
23
|
],
|
|
20
24
|
"scripts": {
|
|
21
|
-
"build": "
|
|
22
|
-
"dev": "tsc --watch",
|
|
25
|
+
"build": "node scripts/build.mjs",
|
|
23
26
|
"clean": "rm -rf dist",
|
|
24
27
|
"lint": "eslint \"src/**/*.{ts,tsx}\"",
|
|
25
28
|
"test": "vitest run",
|
|
26
29
|
"test:watch": "vitest"
|
|
27
30
|
},
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
},
|
|
31
|
-
"peerDependenciesMeta": {
|
|
32
|
-
"@pacspace/shared": {
|
|
33
|
-
"optional": true
|
|
34
|
-
}
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"ethers": "^6.13.4"
|
|
35
33
|
},
|
|
36
34
|
"devDependencies": {
|
|
37
35
|
"@pacspace/shared": "^1.0.0",
|
|
36
|
+
"esbuild": "^0.25.11",
|
|
38
37
|
"typescript": "^5.3.0",
|
|
39
38
|
"vitest": "^4.1.10"
|
|
40
39
|
},
|
|
41
40
|
"engines": {
|
|
42
|
-
"node": ">=
|
|
41
|
+
"node": ">=20.19.0"
|
|
43
42
|
},
|
|
44
43
|
"keywords": [
|
|
45
44
|
"pacspace",
|
|
46
|
-
"balance-api",
|
|
47
45
|
"usage-based-billing",
|
|
48
46
|
"billing-verification",
|
|
49
47
|
"sdk"
|
|
50
48
|
],
|
|
51
49
|
"license": "MIT",
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/PacSpace/sdk-js.git",
|
|
53
|
+
"directory": "packages/sdk"
|
|
54
|
+
},
|
|
52
55
|
"publishConfig": {
|
|
53
56
|
"access": "public"
|
|
54
57
|
}
|