stated-protocol-parser 1.0.0 → 1.0.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.
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Browser-compatible hash utilities using Web Crypto API
3
+ * Async operations for client-side use
4
+ */
5
+ /**
6
+ * Compute SHA-256 hash of a string and return it as URL-safe base64
7
+ * Works in both browser and Node.js environments
8
+ * @param input - The string or buffer to hash
9
+ * @returns URL-safe base64 encoded hash
10
+ */
11
+ export declare const sha256: (input: string | Uint8Array) => Promise<string>;
12
+ /**
13
+ * Verify that content matches a given hash
14
+ * @param content - The content to verify
15
+ * @param hash - The expected hash
16
+ * @returns True if the hash matches
17
+ */
18
+ export declare const verify: (content: string | Uint8Array, hash: string) => Promise<boolean>;
19
+ /**
20
+ * Convert URL-safe base64 back to standard base64
21
+ * @param urlSafe - URL-safe base64 string
22
+ * @returns Standard base64 string with padding
23
+ */
24
+ export declare const fromUrlSafeBase64: (urlSafe: string) => string;
25
+ /**
26
+ * Convert standard base64 to URL-safe base64
27
+ * @param base64 - Standard base64 string
28
+ * @returns URL-safe base64 string without padding
29
+ */
30
+ export declare const toUrlSafeBase64: (base64: string) => string;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ /**
3
+ * Browser-compatible hash utilities using Web Crypto API
4
+ * Async operations for client-side use
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.toUrlSafeBase64 = exports.fromUrlSafeBase64 = exports.verify = exports.sha256 = void 0;
8
+ /**
9
+ * Compute SHA-256 hash of a string and return it as URL-safe base64
10
+ * Works in both browser and Node.js environments
11
+ * @param input - The string or buffer to hash
12
+ * @returns URL-safe base64 encoded hash
13
+ */
14
+ const sha256 = async (input) => {
15
+ let data;
16
+ if (typeof input === 'string') {
17
+ const encoder = new TextEncoder();
18
+ data = encoder.encode(input);
19
+ }
20
+ else {
21
+ data = input;
22
+ }
23
+ // Use Web Crypto API (available in both modern browsers and Node.js 15+)
24
+ const hashBuffer = await crypto.subtle.digest('SHA-256', data);
25
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
26
+ // Convert to base64
27
+ const base64 = btoa(String.fromCharCode(...hashArray));
28
+ // Make URL-safe: remove padding and replace + with - and / with _
29
+ const urlSafe = base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
30
+ return urlSafe;
31
+ };
32
+ exports.sha256 = sha256;
33
+ /**
34
+ * Verify that content matches a given hash
35
+ * @param content - The content to verify
36
+ * @param hash - The expected hash
37
+ * @returns True if the hash matches
38
+ */
39
+ const verify = async (content, hash) => {
40
+ const computed = await (0, exports.sha256)(content);
41
+ return computed === hash;
42
+ };
43
+ exports.verify = verify;
44
+ /**
45
+ * Convert URL-safe base64 back to standard base64
46
+ * @param urlSafe - URL-safe base64 string
47
+ * @returns Standard base64 string with padding
48
+ */
49
+ const fromUrlSafeBase64 = (urlSafe) => {
50
+ const base64 = urlSafe.replace(/-/g, '+').replace(/_/g, '/');
51
+ // Add padding if needed
52
+ const padding = '='.repeat((4 - (base64.length % 4)) % 4);
53
+ return base64 + padding;
54
+ };
55
+ exports.fromUrlSafeBase64 = fromUrlSafeBase64;
56
+ /**
57
+ * Convert standard base64 to URL-safe base64
58
+ * @param base64 - Standard base64 string
59
+ * @returns URL-safe base64 string without padding
60
+ */
61
+ const toUrlSafeBase64 = (base64) => {
62
+ return base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
63
+ };
64
+ exports.toUrlSafeBase64 = toUrlSafeBase64;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Node.js-specific hash utilities using native crypto module
3
+ * Synchronous operations for server-side use
4
+ */
5
+ import crypto from 'crypto';
6
+ /**
7
+ * Compute SHA-256 hash and return as URL-safe base64 (Node.js)
8
+ * @param input - String or buffer to hash
9
+ * @returns URL-safe base64 encoded hash
10
+ */
11
+ export declare const sha256: (input: crypto.BinaryLike) => string;
12
+ /**
13
+ * Verify that content matches a given hash
14
+ * @param content - Content to verify
15
+ * @param hash - Expected hash
16
+ * @returns True if hash matches
17
+ */
18
+ export declare const verify: (content: crypto.BinaryLike, hash: string) => boolean;
19
+ /**
20
+ * Convert URL-safe base64 back to standard base64
21
+ * @param urlSafe - URL-safe base64 string
22
+ * @returns Standard base64 string with padding
23
+ */
24
+ export declare const fromUrlSafeBase64: (urlSafe: string) => string;
25
+ /**
26
+ * Convert standard base64 to URL-safe base64
27
+ * @param base64 - Standard base64 string
28
+ * @returns URL-safe base64 string without padding
29
+ */
30
+ export declare const toUrlSafeBase64: (base64: string) => string;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ /**
3
+ * Node.js-specific hash utilities using native crypto module
4
+ * Synchronous operations for server-side use
5
+ */
6
+ var __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.toUrlSafeBase64 = exports.fromUrlSafeBase64 = exports.verify = exports.sha256 = void 0;
11
+ const crypto_1 = __importDefault(require("crypto"));
12
+ /**
13
+ * Compute SHA-256 hash and return as URL-safe base64 (Node.js)
14
+ * @param input - String or buffer to hash
15
+ * @returns URL-safe base64 encoded hash
16
+ */
17
+ const sha256 = (input) => {
18
+ const base64 = crypto_1.default.createHash('sha256').update(input).digest('base64');
19
+ const urlSafe = base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
20
+ return urlSafe;
21
+ };
22
+ exports.sha256 = sha256;
23
+ /**
24
+ * Verify that content matches a given hash
25
+ * @param content - Content to verify
26
+ * @param hash - Expected hash
27
+ * @returns True if hash matches
28
+ */
29
+ const verify = (content, hash) => {
30
+ return hash === (0, exports.sha256)(content);
31
+ };
32
+ exports.verify = verify;
33
+ /**
34
+ * Convert URL-safe base64 back to standard base64
35
+ * @param urlSafe - URL-safe base64 string
36
+ * @returns Standard base64 string with padding
37
+ */
38
+ const fromUrlSafeBase64 = (urlSafe) => {
39
+ const base64 = urlSafe.replace(/-/g, '+').replace(/_/g, '/');
40
+ const padding = '='.repeat((4 - (base64.length % 4)) % 4);
41
+ return base64 + padding;
42
+ };
43
+ exports.fromUrlSafeBase64 = fromUrlSafeBase64;
44
+ /**
45
+ * Convert standard base64 to URL-safe base64
46
+ * @param base64 - Standard base64 string
47
+ * @returns URL-safe base64 string without padding
48
+ */
49
+ const toUrlSafeBase64 = (base64) => {
50
+ return base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
51
+ };
52
+ exports.toUrlSafeBase64 = toUrlSafeBase64;
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from './types';
3
3
  export * from './constants';
4
4
  export * from './utils';
5
5
  export * from './v3';
6
+ export * from './hash.browser';
6
7
  export declare const buildStatement: ({ domain, author, time, tags, content, representative, supersededStatement }: Statement) => string;
7
8
  export declare const parseStatement: ({ statement: s, allowNoVersion }: {
8
9
  statement: string;
package/dist/index.js CHANGED
@@ -25,6 +25,7 @@ __exportStar(require("./types"), exports);
25
25
  __exportStar(require("./constants"), exports);
26
26
  __exportStar(require("./utils"), exports);
27
27
  __exportStar(require("./v3"), exports);
28
+ __exportStar(require("./hash.browser"), exports);
28
29
  const buildStatement = ({ domain, author, time, tags, content, representative, supersededStatement }) => {
29
30
  if (content.match(/\nPublishing domain: /))
30
31
  throw (new Error("Statement must not contain 'Publishing domain: ', as this marks the beginning of a new statement."));
package/package.json CHANGED
@@ -1,9 +1,26 @@
1
1
  {
2
2
  "name": "stated-protocol-parser",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Parser and formatter for the Stated protocol - a decentralized statement verification system",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ },
13
+ "./hash.node": {
14
+ "import": "./dist/hash.node.js",
15
+ "require": "./dist/hash.node.js",
16
+ "types": "./dist/hash.node.d.ts"
17
+ },
18
+ "./hash.browser": {
19
+ "import": "./dist/hash.browser.js",
20
+ "require": "./dist/hash.browser.js",
21
+ "types": "./dist/hash.browser.d.ts"
22
+ }
23
+ },
7
24
  "scripts": {
8
25
  "build": "tsc",
9
26
  "test": "jest",