@catandbox/schrodinger-web-adapter 0.1.28 → 0.1.32

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/dist/signer.d.ts CHANGED
@@ -1,2 +1,29 @@
1
- export { signRequest, sha256Hex, buildCanonicalString } from "@catandbox/schrodinger-shopify-adapter/signer";
2
- export type { SignRequestInput, SignedHeaders } from "@catandbox/schrodinger-shopify-adapter/signer";
1
+ export interface SignRequestInput {
2
+ appId: string;
3
+ keyId: string;
4
+ keySecret: string;
5
+ timestamp: number;
6
+ nonce: string;
7
+ method: string;
8
+ path: string;
9
+ queryString?: string;
10
+ rawBody?: string;
11
+ }
12
+ export interface SignedHeaders {
13
+ "X-Sch-AppId": string;
14
+ "X-Sch-KeyId": string;
15
+ "X-Sch-Timestamp": string;
16
+ "X-Sch-Nonce": string;
17
+ "Content-SHA256": string;
18
+ "X-Sch-Signature": string;
19
+ }
20
+ export declare function sha256Hex(rawBody: string): Promise<string>;
21
+ export declare function buildCanonicalString(input: {
22
+ timestamp: number;
23
+ nonce: string;
24
+ method: string;
25
+ path: string;
26
+ queryString: string;
27
+ contentSha256: string;
28
+ }): string;
29
+ export declare function signRequest(input: SignRequestInput): Promise<SignedHeaders>;
package/dist/signer.js CHANGED
@@ -1 +1,51 @@
1
- export { signRequest, sha256Hex, buildCanonicalString } from "@catandbox/schrodinger-shopify-adapter/signer";
1
+ const encoder = new TextEncoder();
2
+ function toHex(data) {
3
+ return Array.from(new Uint8Array(data), (byte) => byte.toString(16).padStart(2, "0")).join("");
4
+ }
5
+ function toBase64Url(data) {
6
+ let binary = "";
7
+ for (const byte of new Uint8Array(data)) {
8
+ binary += String.fromCharCode(byte);
9
+ }
10
+ const base64 = btoa(binary);
11
+ return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
12
+ }
13
+ export async function sha256Hex(rawBody) {
14
+ const digest = await crypto.subtle.digest("SHA-256", encoder.encode(rawBody));
15
+ return toHex(digest);
16
+ }
17
+ export function buildCanonicalString(input) {
18
+ return [
19
+ "v1",
20
+ String(input.timestamp),
21
+ input.nonce,
22
+ input.method.toUpperCase(),
23
+ input.path,
24
+ input.queryString,
25
+ input.contentSha256,
26
+ ""
27
+ ].join("\n");
28
+ }
29
+ export async function signRequest(input) {
30
+ const queryString = input.queryString ?? "";
31
+ const rawBody = input.rawBody ?? "";
32
+ const contentSha256 = await sha256Hex(rawBody);
33
+ const canonical = buildCanonicalString({
34
+ timestamp: input.timestamp,
35
+ nonce: input.nonce,
36
+ method: input.method,
37
+ path: input.path,
38
+ queryString,
39
+ contentSha256
40
+ });
41
+ const key = await crypto.subtle.importKey("raw", encoder.encode(input.keySecret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
42
+ const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(canonical));
43
+ return {
44
+ "X-Sch-AppId": input.appId,
45
+ "X-Sch-KeyId": input.keyId,
46
+ "X-Sch-Timestamp": String(input.timestamp),
47
+ "X-Sch-Nonce": input.nonce,
48
+ "Content-SHA256": contentSha256,
49
+ "X-Sch-Signature": toBase64Url(signature)
50
+ };
51
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@catandbox/schrodinger-web-adapter",
3
- "version": "0.1.28",
3
+ "version": "0.1.32",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -34,7 +34,6 @@
34
34
  "test": "vitest run"
35
35
  },
36
36
  "dependencies": {
37
- "@catandbox/schrodinger-contracts": "^0.1.0",
38
- "@catandbox/schrodinger-shopify-adapter": "^0.1.0"
37
+ "@catandbox/schrodinger-contracts": "^0.1.0"
39
38
  }
40
39
  }