@transia/isomorphic 1.0.1

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.
Files changed (88) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +124 -0
  3. package/dist/internal/normalizeInput.d.ts +9 -0
  4. package/dist/internal/normalizeInput.d.ts.map +1 -0
  5. package/dist/internal/normalizeInput.js +13 -0
  6. package/dist/internal/normalizeInput.js.map +1 -0
  7. package/dist/internal/types.d.ts +31 -0
  8. package/dist/internal/types.d.ts.map +1 -0
  9. package/dist/internal/types.js +3 -0
  10. package/dist/internal/types.js.map +1 -0
  11. package/dist/internal/wrapCryptoCreateHash.d.ts +11 -0
  12. package/dist/internal/wrapCryptoCreateHash.d.ts.map +1 -0
  13. package/dist/internal/wrapCryptoCreateHash.js +32 -0
  14. package/dist/internal/wrapCryptoCreateHash.js.map +1 -0
  15. package/dist/internal/wrapNoble.d.ts +9 -0
  16. package/dist/internal/wrapNoble.d.ts.map +1 -0
  17. package/dist/internal/wrapNoble.js +31 -0
  18. package/dist/internal/wrapNoble.js.map +1 -0
  19. package/dist/ripemd160/browser.d.ts +5 -0
  20. package/dist/ripemd160/browser.d.ts.map +1 -0
  21. package/dist/ripemd160/browser.js +13 -0
  22. package/dist/ripemd160/browser.js.map +1 -0
  23. package/dist/ripemd160/index.d.ts +5 -0
  24. package/dist/ripemd160/index.d.ts.map +1 -0
  25. package/dist/ripemd160/index.js +13 -0
  26. package/dist/ripemd160/index.js.map +1 -0
  27. package/dist/sha256/browser.d.ts +5 -0
  28. package/dist/sha256/browser.d.ts.map +1 -0
  29. package/dist/sha256/browser.js +13 -0
  30. package/dist/sha256/browser.js.map +1 -0
  31. package/dist/sha256/index.d.ts +5 -0
  32. package/dist/sha256/index.d.ts.map +1 -0
  33. package/dist/sha256/index.js +13 -0
  34. package/dist/sha256/index.js.map +1 -0
  35. package/dist/sha512/browser.d.ts +5 -0
  36. package/dist/sha512/browser.d.ts.map +1 -0
  37. package/dist/sha512/browser.js +13 -0
  38. package/dist/sha512/browser.js.map +1 -0
  39. package/dist/sha512/index.d.ts +5 -0
  40. package/dist/sha512/index.d.ts.map +1 -0
  41. package/dist/sha512/index.js +13 -0
  42. package/dist/sha512/index.js.map +1 -0
  43. package/dist/utils/browser.d.ts +8 -0
  44. package/dist/utils/browser.d.ts.map +1 -0
  45. package/dist/utils/browser.js +56 -0
  46. package/dist/utils/browser.js.map +1 -0
  47. package/dist/utils/index.d.ts +9 -0
  48. package/dist/utils/index.d.ts.map +1 -0
  49. package/dist/utils/index.js +85 -0
  50. package/dist/utils/index.js.map +1 -0
  51. package/dist/utils/shared.d.ts +4 -0
  52. package/dist/utils/shared.d.ts.map +1 -0
  53. package/dist/utils/shared.js +24 -0
  54. package/dist/utils/shared.js.map +1 -0
  55. package/dist/utils/types.d.ts +34 -0
  56. package/dist/utils/types.d.ts.map +1 -0
  57. package/dist/utils/types.js +3 -0
  58. package/dist/utils/types.js.map +1 -0
  59. package/dist/ws/browser.d.ts +52 -0
  60. package/dist/ws/browser.d.ts.map +1 -0
  61. package/dist/ws/browser.js +71 -0
  62. package/dist/ws/browser.js.map +1 -0
  63. package/dist/ws/index.d.ts +6 -0
  64. package/dist/ws/index.d.ts.map +1 -0
  65. package/dist/ws/index.js +14 -0
  66. package/dist/ws/index.js.map +1 -0
  67. package/package.json +52 -0
  68. package/ripemd160/package.json +7 -0
  69. package/sha256/package.json +7 -0
  70. package/sha512/package.json +7 -0
  71. package/src/internal/normalizeInput.ts +11 -0
  72. package/src/internal/types.ts +33 -0
  73. package/src/internal/wrapCryptoCreateHash.ts +32 -0
  74. package/src/internal/wrapNoble.ts +28 -0
  75. package/src/ripemd160/browser.ts +8 -0
  76. package/src/ripemd160/index.ts +7 -0
  77. package/src/sha256/browser.ts +8 -0
  78. package/src/sha256/index.ts +7 -0
  79. package/src/sha512/browser.ts +8 -0
  80. package/src/sha512/index.ts +7 -0
  81. package/src/utils/browser.ts +54 -0
  82. package/src/utils/index.ts +93 -0
  83. package/src/utils/shared.ts +21 -0
  84. package/src/utils/types.ts +37 -0
  85. package/src/ws/browser.ts +105 -0
  86. package/src/ws/index.ts +10 -0
  87. package/utils/package.json +7 -0
  88. package/ws/package.json +11 -0
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.stringToHex = exports.hexToString = exports.randomBytes = exports.hexToBytes = exports.bytesToHex = void 0;
18
+ const crypto_1 = require("crypto");
19
+ const shared_1 = require("./shared");
20
+ const OriginalBuffer = Symbol('OriginalBuffer');
21
+ /**
22
+ * Converts a Node.js Buffer to a Uint8Array for uniform behavior with browser implementations.
23
+ *
24
+ * Choices:
25
+ * 1. Directly returning the Buffer:
26
+ * - Operation: Return Buffer as is (a Buffer *IS* an instanceof Uint8Array).
27
+ * - Pros: Most memory and performance efficient.
28
+ * - Cons: Violates strict Uint8Array typing and may lead to issues where Buffer-specific features are [ab]used.
29
+ *
30
+ * 2. Using `new Uint8Array(buffer)` or `Uint8Array.from(buffer)`:
31
+ * - Operation: Copies the buffer's data into a new Uint8Array.
32
+ * - Pros: Ensures data isolation; memory-safe.
33
+ * - Cons: Less performant due to data duplication.
34
+ *
35
+ * 3. Using buf.buffer slice:
36
+ * - Operation: Shares memory between Buffer and Uint8Array.
37
+ * - Pros: Performant.
38
+ * - Cons: Risks with shared memory and potential for invalid references.
39
+ *
40
+ * 4. Using buf.buffer slice and keeping a Buffer reference for ownership semantics:
41
+ * - Operation: Shares memory and associates the original Buffer with the resulting Uint8Array.
42
+ * - Pros: Performant while ensuring the original Buffer isn't garbage collected.
43
+ * - Cons: Risks with shared memory but mitigates potential for invalid references.
44
+ *
45
+ * The chosen method (4) prioritizes performance by sharing memory while ensuring buffer ownership.
46
+ *
47
+ * @param {Buffer} buffer - The Node.js Buffer to convert.
48
+ * @returns {Uint8Array} Resulting Uint8Array sharing the same memory as the Buffer and maintaining a reference to it.
49
+ */
50
+ function toUint8Array(buffer) {
51
+ const u8Array = new Uint8Array(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
52
+ u8Array[OriginalBuffer] = buffer;
53
+ return u8Array;
54
+ }
55
+ /* eslint-disable func-style -- Typed to ensure uniformity between node and browser implementations and docs */
56
+ const bytesToHex = (bytes) => {
57
+ const buf = Buffer.from(bytes);
58
+ return buf.toString('hex').toUpperCase();
59
+ };
60
+ exports.bytesToHex = bytesToHex;
61
+ const hexToBytes = (hex) => {
62
+ if (!shared_1.HEX_REGEX.test(hex)) {
63
+ throw new Error('Invalid hex string');
64
+ }
65
+ return toUint8Array(Buffer.from(hex, 'hex'));
66
+ };
67
+ exports.hexToBytes = hexToBytes;
68
+ const randomBytes = (size) => {
69
+ return toUint8Array((0, crypto_1.randomBytes)(size));
70
+ };
71
+ exports.randomBytes = randomBytes;
72
+ const hexToString = (hex, encoding = 'utf8') => {
73
+ if (!shared_1.HEX_REGEX.test(hex)) {
74
+ throw new Error('Invalid hex string');
75
+ }
76
+ return new TextDecoder(encoding).decode((0, exports.hexToBytes)(hex));
77
+ };
78
+ exports.hexToString = hexToString;
79
+ const stringToHex = (string) => {
80
+ return (0, exports.bytesToHex)(new TextEncoder().encode(string));
81
+ };
82
+ exports.stringToHex = stringToHex;
83
+ /* eslint-enable func-style */
84
+ __exportStar(require("./shared"), exports);
85
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mCAAyD;AAGzD,qCAAoC;AAEpC,MAAM,cAAc,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAA;AAe/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,SAAS,YAAY,CAAC,MAAc;IAClC,MAAM,OAAO,GAAG,IAAI,UAAU,CAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CACjB,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CACtC,CACyB,CAAA;IAC5B,OAAO,CAAC,cAAc,CAAC,GAAG,MAAM,CAAA;IAChC,OAAO,OAAO,CAAA;AAChB,CAAC;AAED,+GAA+G;AACxG,MAAM,UAAU,GAAwB,CAAC,KAAK,EAAE,EAAE;IACvD,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAC9B,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAA;AAC1C,CAAC,CAAA;AAHY,QAAA,UAAU,cAGtB;AAEM,MAAM,UAAU,GAAwB,CAAC,GAAG,EAAE,EAAE;IACrD,IAAI,CAAC,kBAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QACxB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;KACtC;IACD,OAAO,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAA;AAC9C,CAAC,CAAA;AALY,QAAA,UAAU,cAKtB;AAEM,MAAM,WAAW,GAAyB,CAAC,IAAI,EAAE,EAAE;IACxD,OAAO,YAAY,CAAC,IAAA,oBAAiB,EAAC,IAAI,CAAC,CAAC,CAAA;AAC9C,CAAC,CAAA;AAFY,QAAA,WAAW,eAEvB;AAEM,MAAM,WAAW,GAAyB,CAC/C,GAAW,EACX,QAAQ,GAAG,MAAM,EACT,EAAE;IACV,IAAI,CAAC,kBAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QACxB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;KACtC;IACD,OAAO,IAAI,WAAW,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,IAAA,kBAAU,EAAC,GAAG,CAAC,CAAC,CAAA;AAC1D,CAAC,CAAA;AARY,QAAA,WAAW,eAQvB;AAEM,MAAM,WAAW,GAAyB,CAAC,MAAc,EAAU,EAAE;IAC1E,OAAO,IAAA,kBAAU,EAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;AACrD,CAAC,CAAA;AAFY,QAAA,WAAW,eAEvB;AACD,8BAA8B;AAE9B,2CAAwB"}
@@ -0,0 +1,4 @@
1
+ export declare const HEX_REGEX: RegExp;
2
+ export declare function concat(views: Uint8Array[]): Uint8Array;
3
+ export declare function equal(buf1: Uint8Array, buf2: Uint8Array): boolean;
4
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/utils/shared.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,SAAS,QAAkB,CAAA;AAExC,wBAAgB,MAAM,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,UAAU,CAEtD;AAED,wBAAgB,KAAK,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAYjE"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.equal = exports.concat = exports.HEX_REGEX = void 0;
4
+ const utils_1 = require("@noble/hashes/utils");
5
+ exports.HEX_REGEX = /^[A-F0-9]*$/iu;
6
+ function concat(views) {
7
+ return (0, utils_1.concatBytes)(...views);
8
+ }
9
+ exports.concat = concat;
10
+ function equal(buf1, buf2) {
11
+ if (buf1.byteLength !== buf2.byteLength) {
12
+ return false;
13
+ }
14
+ const dv1 = new Int8Array(buf1);
15
+ const dv2 = new Int8Array(buf2);
16
+ for (let i = 0; i !== buf1.byteLength; i++) {
17
+ if (dv1[i] !== dv2[i]) {
18
+ return false;
19
+ }
20
+ }
21
+ return true;
22
+ }
23
+ exports.equal = equal;
24
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/utils/shared.ts"],"names":[],"mappings":";;;AAAA,+CAAiD;AAEpC,QAAA,SAAS,GAAG,eAAe,CAAA;AAExC,SAAgB,MAAM,CAAC,KAAmB;IACxC,OAAO,IAAA,mBAAW,EAAC,GAAG,KAAK,CAAC,CAAA;AAC9B,CAAC;AAFD,wBAEC;AAED,SAAgB,KAAK,CAAC,IAAgB,EAAE,IAAgB;IACtD,IAAI,IAAI,CAAC,UAAU,KAAK,IAAI,CAAC,UAAU,EAAE;QACvC,OAAO,KAAK,CAAA;KACb;IACD,MAAM,GAAG,GAAG,IAAI,SAAS,CAAC,IAAI,CAAC,CAAA;IAC/B,MAAM,GAAG,GAAG,IAAI,SAAS,CAAC,IAAI,CAAC,CAAA;IAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC,UAAU,EAAE,CAAC,EAAE,EAAE;QAC1C,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE;YACrB,OAAO,KAAK,CAAA;SACb;KACF;IACD,OAAO,IAAI,CAAA;AACb,CAAC;AAZD,sBAYC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Convert a UInt8Array to hex. The returned hex will be in all caps.
3
+ *
4
+ * @param bytes - {Uint8Array} to convert to hex
5
+ */
6
+ export declare function BytesToHexFn(bytes: Uint8Array | number[]): string;
7
+ /**
8
+ * Convert hex to a Uint8Array.
9
+ *
10
+ * @param hex - {string} to convert to a Uint8Array
11
+ */
12
+ export declare function HexToBytesFn(hex: string): Uint8Array;
13
+ /**
14
+ * Create a Uint8Array of the supplied size.
15
+ *
16
+ * @param size - number of bytes to generate
17
+ */
18
+ export declare function RandomBytesFn(size: number): Uint8Array;
19
+ /**
20
+ * Converts hex to its string equivalent. Useful to read the Domain field and some Memos.
21
+ *
22
+ * @param hex - The hex to convert to a string.
23
+ * @param encoding - The encoding to use. Defaults to 'utf8' (UTF-8). 'ascii' is also allowed.
24
+ * @returns The converted string.
25
+ */
26
+ export declare function HexToStringFn(hex: string, encoding?: string): string;
27
+ /**
28
+ * Converts a utf-8 to its hex equivalent. Useful for Memos.
29
+ *
30
+ * @param string - The string to convert to Hex.
31
+ * @returns The Hex equivalent of the string.
32
+ */
33
+ export declare function StringToHexFn(string: string): string;
34
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/utils/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,EAAE,GAAG,MAAM,CAAA;AAE1E;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAA;AAE7D;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAAA;AAE/D;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;AAE7E;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/utils/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,52 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { EventEmitter } from 'eventemitter3';
4
+ interface WSWrapperOptions {
5
+ perMessageDeflate: boolean;
6
+ handshakeTimeout: number;
7
+ protocolVersion: number;
8
+ origin: string;
9
+ maxPayload: number;
10
+ followRedirects: boolean;
11
+ maxRedirects: number;
12
+ }
13
+ /**
14
+ * Provides `EventEmitter` interface for native browser `WebSocket`,
15
+ * same, as `ws` package provides.
16
+ */
17
+ export default class WSWrapper extends EventEmitter {
18
+ static CONNECTING: number;
19
+ static OPEN: number;
20
+ static CLOSING: number;
21
+ static CLOSED: number;
22
+ private readonly ws;
23
+ /**
24
+ * Constructs a browser-safe websocket.
25
+ *
26
+ * @param url - URL to connect to.
27
+ * @param _protocols - Not used.
28
+ * @param _websocketOptions - Not used.
29
+ */
30
+ constructor(url: string, _protocols: string | string[] | WSWrapperOptions | undefined, _websocketOptions: WSWrapperOptions);
31
+ /**
32
+ * Get the ready state of the websocket.
33
+ *
34
+ * @returns The Websocket's ready state.
35
+ */
36
+ get readyState(): number;
37
+ /**
38
+ * Closes the websocket.
39
+ *
40
+ * @param code - Close code.
41
+ * @param reason - Close reason.
42
+ */
43
+ close(code?: number, reason?: Buffer): void;
44
+ /**
45
+ * Sends a message over the Websocket connection.
46
+ *
47
+ * @param message - Message to send.
48
+ */
49
+ send(message: string): void;
50
+ }
51
+ export {};
52
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../src/ws/browser.ts"],"names":[],"mappings":";;AACA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAc5C,UAAU,gBAAgB;IACxB,iBAAiB,EAAE,OAAO,CAAA;IAC1B,gBAAgB,EAAE,MAAM,CAAA;IACxB,eAAe,EAAE,MAAM,CAAA;IACvB,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,CAAA;IAClB,eAAe,EAAE,OAAO,CAAA;IACxB,YAAY,EAAE,MAAM,CAAA;CACrB;AAED;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,YAAY;IACjD,OAAc,UAAU,SAAI;IAC5B,OAAc,IAAI,SAAI;IACtB,OAAc,OAAO,SAAI;IAEzB,OAAc,MAAM,SAAI;IACxB,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAW;IAE9B;;;;;;OAMG;gBAED,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,gBAAgB,GAAG,SAAS,EAC5D,iBAAiB,EAAE,gBAAgB;IA4BrC;;;;OAIG;IACH,IAAW,UAAU,IAAI,MAAM,CAE9B;IAED;;;;;OAKG;IACI,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAMlD;;;;OAIG;IACI,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;CAGnC"}
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* eslint-disable max-classes-per-file -- Needs to be a wrapper for ws */
4
+ const eventemitter3_1 = require("eventemitter3");
5
+ /**
6
+ * Provides `EventEmitter` interface for native browser `WebSocket`,
7
+ * same, as `ws` package provides.
8
+ */
9
+ class WSWrapper extends eventemitter3_1.EventEmitter {
10
+ /**
11
+ * Constructs a browser-safe websocket.
12
+ *
13
+ * @param url - URL to connect to.
14
+ * @param _protocols - Not used.
15
+ * @param _websocketOptions - Not used.
16
+ */
17
+ constructor(url, _protocols, _websocketOptions) {
18
+ super();
19
+ this.ws = new WebSocket(url);
20
+ this.ws.onclose = (closeEvent) => {
21
+ let reason;
22
+ if (closeEvent.reason) {
23
+ const enc = new TextEncoder();
24
+ reason = enc.encode(closeEvent.reason);
25
+ }
26
+ this.emit('close', closeEvent.code, reason);
27
+ };
28
+ this.ws.onopen = () => {
29
+ this.emit('open');
30
+ };
31
+ this.ws.onerror = (error) => {
32
+ this.emit('error', error);
33
+ };
34
+ this.ws.onmessage = (message) => {
35
+ this.emit('message', message.data);
36
+ };
37
+ }
38
+ /**
39
+ * Get the ready state of the websocket.
40
+ *
41
+ * @returns The Websocket's ready state.
42
+ */
43
+ get readyState() {
44
+ return this.ws.readyState;
45
+ }
46
+ /**
47
+ * Closes the websocket.
48
+ *
49
+ * @param code - Close code.
50
+ * @param reason - Close reason.
51
+ */
52
+ close(code, reason) {
53
+ if (this.readyState === 1) {
54
+ this.ws.close(code, reason);
55
+ }
56
+ }
57
+ /**
58
+ * Sends a message over the Websocket connection.
59
+ *
60
+ * @param message - Message to send.
61
+ */
62
+ send(message) {
63
+ this.ws.send(message);
64
+ }
65
+ }
66
+ WSWrapper.CONNECTING = 0;
67
+ WSWrapper.OPEN = 1;
68
+ WSWrapper.CLOSING = 2;
69
+ WSWrapper.CLOSED = 3;
70
+ exports.default = WSWrapper;
71
+ //# sourceMappingURL=browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/ws/browser.ts"],"names":[],"mappings":";;AAAA,yEAAyE;AACzE,iDAA4C;AAwB5C;;;GAGG;AACH,MAAqB,SAAU,SAAQ,4BAAY;IAQjD;;;;;;OAMG;IACH,YACE,GAAW,EACX,UAA4D,EAC5D,iBAAmC;QAEnC,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,EAAE,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC,CAAA;QAE5B,IAAI,CAAC,EAAE,CAAC,OAAO,GAAG,CAAC,UAAsB,EAAQ,EAAE;YACjD,IAAI,MAA8B,CAAA;YAClC,IAAI,UAAU,CAAC,MAAM,EAAE;gBACrB,MAAM,GAAG,GAAG,IAAI,WAAW,EAAE,CAAA;gBAC7B,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;aACvC;YACD,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;QAC7C,CAAC,CAAA;QAED,IAAI,CAAC,EAAE,CAAC,MAAM,GAAG,GAAS,EAAE;YAC1B,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACnB,CAAC,CAAA;QAED,IAAI,CAAC,EAAE,CAAC,OAAO,GAAG,CAAC,KAAK,EAAQ,EAAE;YAChC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;QAC3B,CAAC,CAAA;QAED,IAAI,CAAC,EAAE,CAAC,SAAS,GAAG,CAAC,OAAqB,EAAQ,EAAE;YAClD,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;QACpC,CAAC,CAAA;IACH,CAAC;IAED;;;;OAIG;IACH,IAAW,UAAU;QACnB,OAAO,IAAI,CAAC,EAAE,CAAC,UAAU,CAAA;IAC3B,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,IAAa,EAAE,MAAe;QACzC,IAAI,IAAI,CAAC,UAAU,KAAK,CAAC,EAAE;YACzB,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;SAC5B;IACH,CAAC;IAED;;;;OAIG;IACI,IAAI,CAAC,OAAe;QACzB,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACvB,CAAC;;AAzEa,oBAAU,GAAG,CAAC,CAAA;AACd,cAAI,GAAG,CAAC,CAAA;AACR,iBAAO,GAAG,CAAC,CAAA;AAEX,gBAAM,GAAG,CAAC,CAAA;kBALL,SAAS"}
@@ -0,0 +1,6 @@
1
+ import WebSocket from 'ws';
2
+ export default class Socket extends WebSocket {
3
+ constructor(...args: any[]);
4
+ }
5
+ export type ClientOptions = WebSocket.ClientOptions;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ws/index.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,IAAI,CAAA;AAE1B,MAAM,CAAC,OAAO,OAAO,MAAO,SAAQ,SAAS;gBAC/B,GAAG,IAAI,OAAA;CAIpB;AAED,MAAM,MAAM,aAAa,GAAG,SAAS,CAAC,aAAa,CAAA"}
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const ws_1 = __importDefault(require("ws"));
7
+ class Socket extends ws_1.default {
8
+ constructor(...args) {
9
+ super(args[0], args[1], args[2]);
10
+ this.setMaxListeners(Infinity);
11
+ }
12
+ }
13
+ exports.default = Socket;
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ws/index.ts"],"names":[],"mappings":";;;;;AAAA,4CAA0B;AAE1B,MAAqB,MAAO,SAAQ,YAAS;IAC3C,YAAY,GAAG,IAAI;QACjB,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;QAChC,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAA;IAChC,CAAC;CACF;AALD,yBAKC"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@transia/isomorphic",
3
+ "version": "1.0.1",
4
+ "description": "A collection of isomorphic and tree-shakeable crypto hashes and utils for xrpl.js",
5
+ "keywords": [
6
+ "crypto",
7
+ "isomorphic",
8
+ "xrpl"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc --build ./tsconfig.build.json",
12
+ "test": "npm run build && jest --verbose false --silent=false ./test/*.test.ts",
13
+ "test:browser": "npm run build && karma start ./karma.config.js",
14
+ "clean": "rm -rf ./dist ./coverage ./test/testCompiledForWeb tsconfig.build.tsbuildinfo",
15
+ "lint": "eslint . --max-warnings 0",
16
+ "prepublish": "npm run lint && npm test"
17
+ },
18
+ "files": [
19
+ "dist/*",
20
+ "sha256/*",
21
+ "sha512/*",
22
+ "ripemd160/*",
23
+ "src/*",
24
+ "utils/*",
25
+ "ws/*"
26
+ ],
27
+ "directories": {
28
+ "test": "test"
29
+ },
30
+ "dependencies": {
31
+ "@noble/hashes": "^1.0.0",
32
+ "eventemitter3": "5.0.1",
33
+ "ws": "^8.17.1"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^18.18.38",
37
+ "@types/ws": "^8.5.6"
38
+ },
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git@github.com:XRPLF/xrpl.js.git"
42
+ },
43
+ "license": "ISC",
44
+ "prettier": "@xrplf/prettier-config",
45
+ "engines": {
46
+ "node": ">=18.0.0"
47
+ },
48
+ "gitHead": "cdaeed0dc8ccb726deb35aaaddcc229eb9aabe6c",
49
+ "publishConfig": {
50
+ "access": "public"
51
+ }
52
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@transia/isomorphic/ripemd160",
3
+ "private": true,
4
+ "main": "../dist/ripemd160",
5
+ "types": "../dist/ripemd160",
6
+ "browser": "../dist/ripemd160/browser.js"
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@transia/isomorphic/sha256",
3
+ "private": true,
4
+ "main": "../dist/sha256",
5
+ "types": "../dist/sha256",
6
+ "browser": "../dist/sha256/browser.js"
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@transia/isomorphic/sha512",
3
+ "private": true,
4
+ "main": "../dist/sha512",
5
+ "types": "../dist/sha512",
6
+ "browser": "../dist/sha512/browser.js"
7
+ }
@@ -0,0 +1,11 @@
1
+ import { Input } from './types'
2
+
3
+ /**
4
+ * Normalize a string, number array, or Uint8Array to a string or Uint8Array.
5
+ * Both node and noble lib functions accept these types.
6
+ *
7
+ * @param input - value to normalize
8
+ */
9
+ export default function normalizeInput(input: Input): string | Uint8Array {
10
+ return Array.isArray(input) ? new Uint8Array(input) : input
11
+ }
@@ -0,0 +1,33 @@
1
+ export type ByteEncodedString = string
2
+ export type Input = Uint8Array | number[] | ByteEncodedString
3
+
4
+ /**
5
+ * A stripped down isomorphic hash inspired by node's `crypto.Hash`
6
+ */
7
+ export interface Hash {
8
+ /**
9
+ * Updates the hash content with the given data,
10
+ *
11
+ * @param data - a byte encoded string, an array of numbers or a Uint8Array
12
+ */
13
+ update: (data: Input) => this
14
+ /**
15
+ * Calculates the digest of all the data passed to be hashed and returns a Uint8Array
16
+ */
17
+ digest: () => Uint8Array
18
+ }
19
+
20
+ export interface HashFn {
21
+ /**
22
+ * Produces a Uint8Array for the given hash contents
23
+ * Shorthand for calling `create`, `update`, and then `digest`
24
+ *
25
+ * @param data - a byte encoded string, an array of numbers or a Uint8Array
26
+ */
27
+ (data: Input): Uint8Array
28
+
29
+ /**
30
+ * Creates a new empty `Hash`.
31
+ */
32
+ create: () => Hash
33
+ }
@@ -0,0 +1,32 @@
1
+ import { createHash } from 'crypto'
2
+ import { Hash, HashFn, Input } from './types'
3
+ import normalizeInput from './normalizeInput'
4
+
5
+ /**
6
+ * Wrap createHash from node to provide an interface that is isomorphic
7
+ *
8
+ * @param type - the hash name
9
+ * @param fn - {createHash} the hash factory
10
+ */
11
+ export default function wrapCryptoCreateHash(
12
+ type: string,
13
+ fn: typeof createHash,
14
+ ): HashFn {
15
+ function hashFn(input: Input): Uint8Array {
16
+ return fn(type).update(normalizeInput(input)).digest()
17
+ }
18
+
19
+ hashFn.create = (): Hash => {
20
+ const hash = fn(type)
21
+ return {
22
+ update(input: Input): Hash {
23
+ hash.update(normalizeInput(input))
24
+ return this
25
+ },
26
+ digest(): Uint8Array {
27
+ return hash.digest()
28
+ },
29
+ }
30
+ }
31
+ return hashFn
32
+ }
@@ -0,0 +1,28 @@
1
+ import { CHash } from '@noble/hashes/utils'
2
+ import { Hash, HashFn, Input } from './types'
3
+ import normalizeInput from './normalizeInput'
4
+
5
+ /**
6
+ * Wrap a CHash object from @noble/hashes to provide a interface that is isomorphic
7
+ *
8
+ * @param chash - {CHash} hash function to wrap
9
+ */
10
+ export default function wrapNoble(chash: CHash): HashFn {
11
+ function wrapped(input: Input): Uint8Array {
12
+ return chash(normalizeInput(input))
13
+ }
14
+
15
+ wrapped.create = (): Hash => {
16
+ const hash = chash.create()
17
+ return {
18
+ update(input: Input): Hash {
19
+ hash.update(normalizeInput(input))
20
+ return this
21
+ },
22
+ digest(): Uint8Array {
23
+ return hash.digest()
24
+ },
25
+ }
26
+ }
27
+ return wrapped
28
+ }
@@ -0,0 +1,8 @@
1
+ import { ripemd160 as nobleImpl } from '@noble/hashes/ripemd160'
2
+
3
+ import wrapNoble from '../internal/wrapNoble'
4
+
5
+ /**
6
+ * Wrap noble-libs's ripemd160 implementation in HashFn
7
+ */
8
+ export const ripemd160 = wrapNoble(nobleImpl)
@@ -0,0 +1,7 @@
1
+ import { createHash } from 'crypto'
2
+ import wrapCryptoCreateHash from '../internal/wrapCryptoCreateHash'
3
+
4
+ /**
5
+ * Wrap node's native ripemd160 implementation in HashFn
6
+ */
7
+ export const ripemd160 = wrapCryptoCreateHash('ripemd160', createHash)
@@ -0,0 +1,8 @@
1
+ import { sha256 as nobleImpl } from '@noble/hashes/sha256'
2
+
3
+ import wrapNoble from '../internal/wrapNoble'
4
+
5
+ /**
6
+ * Wrap noble-libs's sha256 implementation in HashFn
7
+ */
8
+ export const sha256 = wrapNoble(nobleImpl)
@@ -0,0 +1,7 @@
1
+ import { createHash } from 'crypto'
2
+ import wrapCryptoCreateHash from '../internal/wrapCryptoCreateHash'
3
+
4
+ /**
5
+ * Wrap node's native sha256 implementation in HashFn
6
+ */
7
+ export const sha256 = wrapCryptoCreateHash('sha256', createHash)
@@ -0,0 +1,8 @@
1
+ import { sha512 as nobleImpl } from '@noble/hashes/sha512'
2
+
3
+ import wrapNoble from '../internal/wrapNoble'
4
+
5
+ /**
6
+ * Wrap noble-libs's sha512 implementation in HashFn
7
+ */
8
+ export const sha512 = wrapNoble(nobleImpl)
@@ -0,0 +1,7 @@
1
+ import { createHash } from 'crypto'
2
+ import wrapCryptoCreateHash from '../internal/wrapCryptoCreateHash'
3
+
4
+ /**
5
+ * Wrap node's native sha512 implementation in HashFn
6
+ */
7
+ export const sha512 = wrapCryptoCreateHash('sha512', createHash)
@@ -0,0 +1,54 @@
1
+ import {
2
+ bytesToHex as nobleBytesToHex,
3
+ randomBytes as nobleRandomBytes,
4
+ } from '@noble/hashes/utils'
5
+ import type {
6
+ BytesToHexFn,
7
+ HexToBytesFn,
8
+ HexToStringFn,
9
+ RandomBytesFn,
10
+ StringToHexFn,
11
+ } from './types'
12
+ import { HEX_REGEX } from './shared'
13
+
14
+ /* eslint-disable func-style -- Typed to ensure uniformity between node and browser implementations and docs */
15
+ export const bytesToHex: typeof BytesToHexFn = (bytes) => {
16
+ const hex = nobleBytesToHex(
17
+ bytes instanceof Uint8Array ? bytes : Uint8Array.from(bytes),
18
+ )
19
+ return hex.toUpperCase()
20
+ }
21
+
22
+ // A clone of hexToBytes from @noble/hashes without the length checks. This allows us to do our own checks.
23
+ export const hexToBytes: typeof HexToBytesFn = (hex): Uint8Array => {
24
+ const len = hex.length
25
+ const array = new Uint8Array(len / 2)
26
+ if (!HEX_REGEX.test(hex)) {
27
+ throw new Error('Invalid hex string')
28
+ }
29
+ for (let i = 0; i < array.length; i++) {
30
+ const j = i * 2
31
+ const hexByte = hex.slice(j, j + 2)
32
+ const byte = Number.parseInt(hexByte, 16)
33
+ if (Number.isNaN(byte) || byte < 0) {
34
+ throw new Error('Invalid byte sequence')
35
+ }
36
+ array[i] = byte
37
+ }
38
+ return array
39
+ }
40
+
41
+ export const hexToString: typeof HexToStringFn = (
42
+ hex: string,
43
+ encoding = 'utf8',
44
+ ): string => {
45
+ return new TextDecoder(encoding).decode(hexToBytes(hex))
46
+ }
47
+
48
+ export const stringToHex: typeof StringToHexFn = (string: string): string => {
49
+ return bytesToHex(new TextEncoder().encode(string))
50
+ }
51
+ /* eslint-enable func-style */
52
+
53
+ export const randomBytes: typeof RandomBytesFn = nobleRandomBytes
54
+ export * from './shared'