@capxul/sdk 0.1.0-alpha.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.
@@ -0,0 +1,116 @@
1
+ // src/errors.ts
2
+ var CapxulError = class extends Error {
3
+ code;
4
+ details;
5
+ operationId;
6
+ correlationId;
7
+ retryable;
8
+ constructor(init) {
9
+ super(
10
+ init.message,
11
+ init.cause !== void 0 ? { cause: init.cause } : void 0
12
+ );
13
+ this.name = "CapxulError";
14
+ this.code = init.code;
15
+ this.details = init.details;
16
+ this.operationId = init.operationId;
17
+ this.correlationId = init.correlationId;
18
+ this.retryable = init.retryable;
19
+ }
20
+ };
21
+
22
+ // ../config/src/timing.ts
23
+ var WEBHOOK_FRESHNESS_WINDOW_MS = 5 * 60 * 1e3;
24
+
25
+ // ../config/src/org-roles.ts
26
+ function roleKeyFromLabel(label) {
27
+ const bytes = new TextEncoder().encode(label);
28
+ const hex = Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
29
+ return "0x" + hex.padEnd(64, "0");
30
+ }
31
+ roleKeyFromLabel("OWNER");
32
+ roleKeyFromLabel("FINANCE_MANAGER");
33
+ roleKeyFromLabel("TEAM_LEAD");
34
+
35
+ // src/webhooks.ts
36
+ async function verifyWebhook(request, secret, options) {
37
+ if (!secret) {
38
+ throw new CapxulError({
39
+ code: "INVALID_INPUT",
40
+ message: "A webhook signing secret is required."
41
+ });
42
+ }
43
+ const timestamp = request.headers.get("x-capxul-timestamp");
44
+ const signature = request.headers.get("x-capxul-signature");
45
+ if (!timestamp || !signature) {
46
+ return { valid: false, reason: "invalid_signature" };
47
+ }
48
+ const timestampMs = Number(timestamp);
49
+ if (!Number.isFinite(timestampMs)) {
50
+ return { valid: false, reason: "malformed" };
51
+ }
52
+ const freshnessWindowMs = options?.freshnessWindowMs ?? WEBHOOK_FRESHNESS_WINDOW_MS;
53
+ if (Math.abs(Date.now() - timestampMs) > freshnessWindowMs) {
54
+ return { valid: false, reason: "replay" };
55
+ }
56
+ const body = await request.text();
57
+ const signatureHex = signature.startsWith("sha256=") ? signature.slice("sha256=".length) : signature;
58
+ if (!/^[a-f0-9]{64}$/i.test(signatureHex)) {
59
+ return { valid: false, reason: "invalid_signature" };
60
+ }
61
+ const validSignature = await verifyHmacSha256(
62
+ secret,
63
+ `${timestamp}.${body}`,
64
+ signatureHex
65
+ );
66
+ if (!validSignature) {
67
+ return { valid: false, reason: "invalid_signature" };
68
+ }
69
+ const parsed = parseWebhookEvent(body);
70
+ if (!parsed) {
71
+ return { valid: false, reason: "malformed" };
72
+ }
73
+ return { valid: true, event: parsed };
74
+ }
75
+ async function verifyHmacSha256(secret, message, signatureHex) {
76
+ const encoder = new TextEncoder();
77
+ const key = await crypto.subtle.importKey(
78
+ "raw",
79
+ encoder.encode(secret),
80
+ { name: "HMAC", hash: "SHA-256" },
81
+ false,
82
+ ["verify"]
83
+ );
84
+ return await crypto.subtle.verify(
85
+ "HMAC",
86
+ key,
87
+ hexToArrayBuffer(signatureHex),
88
+ encoder.encode(message)
89
+ );
90
+ }
91
+ function hexToArrayBuffer(hex) {
92
+ const buffer = new ArrayBuffer(hex.length / 2);
93
+ const bytes = new Uint8Array(buffer);
94
+ for (let i = 0; i < bytes.length; i++) {
95
+ bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
96
+ }
97
+ return buffer;
98
+ }
99
+ function parseWebhookEvent(body) {
100
+ try {
101
+ const parsed = JSON.parse(body);
102
+ if (!isWebhookEvent(parsed)) return null;
103
+ return parsed;
104
+ } catch {
105
+ return null;
106
+ }
107
+ }
108
+ function isWebhookEvent(value) {
109
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
110
+ return false;
111
+ }
112
+ const candidate = value;
113
+ return typeof candidate.id === "string" && typeof candidate.type === "string" && typeof candidate.createdAt === "string" && (candidate.operationId === void 0 || typeof candidate.operationId === "string") && (candidate.correlationId === void 0 || typeof candidate.correlationId === "string") && !!candidate.data && typeof candidate.data === "object" && !Array.isArray(candidate.data);
114
+ }
115
+
116
+ export { verifyWebhook };
package/package.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "@capxul/sdk",
3
+ "version": "0.1.0-alpha.0",
4
+ "description": "Headless TypeScript client for Capxul's /v1/* HTTP contract.",
5
+ "license": "UNLICENSED",
6
+ "private": false,
7
+ "type": "module",
8
+ "homepage": "https://capxul.com",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Xelmar-tech/Capxul.git",
12
+ "directory": "packages/sdk"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/Xelmar-tech/Capxul/issues"
16
+ },
17
+ "author": "Capxul (Xelmar Tech Ltd.)",
18
+ "keywords": [
19
+ "capxul",
20
+ "sdk",
21
+ "stablecoin",
22
+ "payments",
23
+ "smart-account",
24
+ "typescript"
25
+ ],
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "import": "./dist/index.js",
30
+ "require": "./dist/index.cjs"
31
+ },
32
+ "./client": {
33
+ "types": "./dist/client.d.ts",
34
+ "import": "./dist/client.js",
35
+ "require": "./dist/client.cjs"
36
+ },
37
+ "./errors": {
38
+ "types": "./dist/errors.d.ts",
39
+ "import": "./dist/errors.js",
40
+ "require": "./dist/errors.cjs"
41
+ },
42
+ "./webhooks": {
43
+ "types": "./dist/webhooks.d.ts",
44
+ "import": "./dist/webhooks.js",
45
+ "require": "./dist/webhooks.cjs"
46
+ }
47
+ },
48
+ "files": [
49
+ "dist",
50
+ "README.md",
51
+ "LICENSE",
52
+ "CHANGELOG.md"
53
+ ],
54
+ "publishConfig": {
55
+ "access": "public"
56
+ },
57
+ "dependencies": {
58
+ "xstate": "^5",
59
+ "@repo/api-contract": "0.0.0",
60
+ "@repo/backend": "0.0.0",
61
+ "@repo/config": "0.0.0",
62
+ "@repo/observability": "0.0.0",
63
+ "@repo/platform-kernel": "0.0.0"
64
+ },
65
+ "peerDependencies": {
66
+ "permissionless": ">=0.3.4 <0.4.0",
67
+ "viem": ">=2.0.0"
68
+ },
69
+ "devDependencies": {
70
+ "@types/node": "^22.15.3",
71
+ "permissionless": "0.3.4",
72
+ "tsup": "^8.5.1",
73
+ "typescript": "5.9.2",
74
+ "viem": "2.47.10",
75
+ "vitest": "^4.1.2",
76
+ "zod": "^4.3.6",
77
+ "@repo/typescript-config": "0.0.0"
78
+ },
79
+ "scripts": {
80
+ "build": "tsup",
81
+ "check-types": "tsc --noEmit",
82
+ "snapshot:api": "tsx scripts/snapshot-convex-api.ts",
83
+ "test": "vitest run",
84
+ "test:types": "vitest run --typecheck"
85
+ },
86
+ "main": "./dist/index.cjs",
87
+ "module": "./dist/index.js",
88
+ "types": "./dist/index.d.ts"
89
+ }