@lightsparkdev/core 1.0.4 → 1.0.5

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @lightsparkdev/core
2
2
 
3
+ ## 1.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - ca58c08: Update createSha256Hash with option to return as hex string and accept string data
8
+
3
9
  ## 1.0.4
4
10
 
5
11
  ### Patch Changes
@@ -46,16 +46,40 @@ var isBrowser = typeof window !== "undefined" && typeof window.document !== "und
46
46
  var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
47
47
  var isTest = isNode && process.env.NODE_ENV === "test";
48
48
 
49
+ // src/utils/hex.ts
50
+ var bytesToHex = (bytes) => {
51
+ return bytes.reduce((acc, byte) => {
52
+ return acc += ("0" + byte.toString(16)).slice(-2);
53
+ }, "");
54
+ };
55
+ var hexToBytes = (hex) => {
56
+ const bytes = [];
57
+ for (let c = 0; c < hex.length; c += 2) {
58
+ bytes.push(parseInt(hex.substr(c, 2), 16));
59
+ }
60
+ return Uint8Array.from(bytes);
61
+ };
62
+
49
63
  // src/utils/createHash.ts
50
- var createSha256Hash = async (data) => {
64
+ async function createSha256Hash(data, asHex) {
51
65
  if (isBrowser) {
52
- return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
66
+ const source = typeof data === "string" ? new TextEncoder().encode(data) : data;
67
+ const buffer = await window.crypto.subtle.digest("SHA-256", source);
68
+ const arr = new Uint8Array(buffer);
69
+ if (asHex) {
70
+ return bytesToHex(arr);
71
+ }
72
+ return arr;
53
73
  } else {
54
74
  const { createHash } = await import("crypto");
75
+ if (asHex) {
76
+ const hexStr = createHash("sha256").update(data).digest("hex");
77
+ return hexStr;
78
+ }
55
79
  const buffer = createHash("sha256").update(data).digest();
56
80
  return new Uint8Array(buffer);
57
81
  }
58
- };
82
+ }
59
83
 
60
84
  // src/LightsparkException.ts
61
85
  var LightsparkException = class extends Error {
@@ -162,20 +186,6 @@ var isErrorMsg = (e, msg) => {
162
186
  return false;
163
187
  };
164
188
 
165
- // src/utils/hex.ts
166
- var bytesToHex = (bytes) => {
167
- return bytes.reduce((acc, byte) => {
168
- return acc += ("0" + byte.toString(16)).slice(-2);
169
- }, "");
170
- };
171
- var hexToBytes = (hex) => {
172
- const bytes = [];
173
- for (let c = 0; c < hex.length; c += 2) {
174
- bytes.push(parseInt(hex.substr(c, 2), 16));
175
- }
176
- return Uint8Array.from(bytes);
177
- };
178
-
179
189
  // ../../node_modules/lodash-es/_freeGlobal.js
180
190
  var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
181
191
  var freeGlobal_default = freeGlobal;
@@ -311,14 +321,14 @@ export {
311
321
  isBrowser,
312
322
  isNode,
313
323
  isTest,
324
+ bytesToHex,
325
+ hexToBytes,
314
326
  createSha256Hash,
315
327
  convertCurrencyAmount,
316
328
  isError,
317
329
  isErrorWithMessage,
318
330
  getErrorMsg,
319
331
  isErrorMsg,
320
- bytesToHex,
321
- hexToBytes,
322
332
  sleep,
323
333
  pollUntil,
324
334
  isType
package/dist/index.cjs CHANGED
@@ -372,16 +372,40 @@ var isBrowser = typeof window !== "undefined" && typeof window.document !== "und
372
372
  var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
373
373
  var isTest = isNode && process.env.NODE_ENV === "test";
374
374
 
375
+ // src/utils/hex.ts
376
+ var bytesToHex = (bytes) => {
377
+ return bytes.reduce((acc, byte) => {
378
+ return acc += ("0" + byte.toString(16)).slice(-2);
379
+ }, "");
380
+ };
381
+ var hexToBytes = (hex) => {
382
+ const bytes = [];
383
+ for (let c = 0; c < hex.length; c += 2) {
384
+ bytes.push(parseInt(hex.substr(c, 2), 16));
385
+ }
386
+ return Uint8Array.from(bytes);
387
+ };
388
+
375
389
  // src/utils/createHash.ts
376
- var createSha256Hash = async (data) => {
390
+ async function createSha256Hash(data, asHex) {
377
391
  if (isBrowser) {
378
- return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
392
+ const source = typeof data === "string" ? new TextEncoder().encode(data) : data;
393
+ const buffer = await window.crypto.subtle.digest("SHA-256", source);
394
+ const arr = new Uint8Array(buffer);
395
+ if (asHex) {
396
+ return bytesToHex(arr);
397
+ }
398
+ return arr;
379
399
  } else {
380
400
  const { createHash } = await import("crypto");
401
+ if (asHex) {
402
+ const hexStr = createHash("sha256").update(data).digest("hex");
403
+ return hexStr;
404
+ }
381
405
  const buffer = createHash("sha256").update(data).digest();
382
406
  return new Uint8Array(buffer);
383
407
  }
384
- };
408
+ }
385
409
 
386
410
  // src/utils/currency.ts
387
411
  var CONVERSION_MAP = {
@@ -474,20 +498,6 @@ var isErrorMsg = (e, msg) => {
474
498
  return false;
475
499
  };
476
500
 
477
- // src/utils/hex.ts
478
- var bytesToHex = (bytes) => {
479
- return bytes.reduce((acc, byte) => {
480
- return acc += ("0" + byte.toString(16)).slice(-2);
481
- }, "");
482
- };
483
- var hexToBytes = (hex) => {
484
- const bytes = [];
485
- for (let c = 0; c < hex.length; c += 2) {
486
- bytes.push(parseInt(hex.substr(c, 2), 16));
487
- }
488
- return Uint8Array.from(bytes);
489
- };
490
-
491
501
  // ../../node_modules/lodash-es/_freeGlobal.js
492
502
  var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
493
503
  var freeGlobal_default = freeGlobal;
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ import {
17
17
  pollUntil,
18
18
  sleep,
19
19
  urlsafe_b64decode
20
- } from "./chunk-J6VQYCQJ.js";
20
+ } from "./chunk-5P2KZ44N.js";
21
21
 
22
22
  // src/auth/LightsparkAuthException.ts
23
23
  var LightsparkAuthException = class extends LightsparkException_default {
@@ -98,16 +98,40 @@ var isBrowser = typeof window !== "undefined" && typeof window.document !== "und
98
98
  var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
99
99
  var isTest = isNode && process.env.NODE_ENV === "test";
100
100
 
101
+ // src/utils/hex.ts
102
+ var bytesToHex = (bytes) => {
103
+ return bytes.reduce((acc, byte) => {
104
+ return acc += ("0" + byte.toString(16)).slice(-2);
105
+ }, "");
106
+ };
107
+ var hexToBytes = (hex) => {
108
+ const bytes = [];
109
+ for (let c = 0; c < hex.length; c += 2) {
110
+ bytes.push(parseInt(hex.substr(c, 2), 16));
111
+ }
112
+ return Uint8Array.from(bytes);
113
+ };
114
+
101
115
  // src/utils/createHash.ts
102
- var createSha256Hash = async (data) => {
116
+ async function createSha256Hash(data, asHex) {
103
117
  if (isBrowser) {
104
- return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
118
+ const source = typeof data === "string" ? new TextEncoder().encode(data) : data;
119
+ const buffer = await window.crypto.subtle.digest("SHA-256", source);
120
+ const arr = new Uint8Array(buffer);
121
+ if (asHex) {
122
+ return bytesToHex(arr);
123
+ }
124
+ return arr;
105
125
  } else {
106
126
  const { createHash } = await import("crypto");
127
+ if (asHex) {
128
+ const hexStr = createHash("sha256").update(data).digest("hex");
129
+ return hexStr;
130
+ }
107
131
  const buffer = createHash("sha256").update(data).digest();
108
132
  return new Uint8Array(buffer);
109
133
  }
110
- };
134
+ }
111
135
 
112
136
  // src/LightsparkException.ts
113
137
  var LightsparkException = class extends Error {
@@ -214,20 +238,6 @@ var isErrorMsg = (e, msg) => {
214
238
  return false;
215
239
  };
216
240
 
217
- // src/utils/hex.ts
218
- var bytesToHex = (bytes) => {
219
- return bytes.reduce((acc, byte) => {
220
- return acc += ("0" + byte.toString(16)).slice(-2);
221
- }, "");
222
- };
223
- var hexToBytes = (hex) => {
224
- const bytes = [];
225
- for (let c = 0; c < hex.length; c += 2) {
226
- bytes.push(parseInt(hex.substr(c, 2), 16));
227
- }
228
- return Uint8Array.from(bytes);
229
- };
230
-
231
241
  // ../../node_modules/lodash-es/_freeGlobal.js
232
242
  var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
233
243
  var freeGlobal_default = freeGlobal;
@@ -2,7 +2,9 @@ declare const b64decode: (encoded: string) => Uint8Array;
2
2
  declare const urlsafe_b64decode: (encoded: string) => Uint8Array;
3
3
  declare const b64encode: (data: ArrayBuffer) => string;
4
4
 
5
- declare const createSha256Hash: (data: Uint8Array) => Promise<Uint8Array>;
5
+ type SourceData = Uint8Array | string;
6
+ declare function createSha256Hash(data: SourceData): Promise<Uint8Array>;
7
+ declare function createSha256Hash(data: SourceData, asHex: true): Promise<string>;
6
8
 
7
9
  /** Represents the value and unit for an amount of currency. **/
8
10
  type CurrencyAmount = {
@@ -16,7 +16,7 @@ import {
16
16
  pollUntil,
17
17
  sleep,
18
18
  urlsafe_b64decode
19
- } from "../chunk-J6VQYCQJ.js";
19
+ } from "../chunk-5P2KZ44N.js";
20
20
  export {
21
21
  b64decode,
22
22
  b64encode,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightsparkdev/core",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Lightspark JS SDK",
5
5
  "author": "Lightspark Inc.",
6
6
  "keywords": [
@@ -1,13 +1,34 @@
1
1
  import { isBrowser } from "./environment.js";
2
+ import { bytesToHex } from "./hex.js";
2
3
 
3
- export const createSha256Hash = async (
4
- data: Uint8Array,
5
- ): Promise<Uint8Array> => {
4
+ type SourceData = Uint8Array | string;
5
+
6
+ export async function createSha256Hash(data: SourceData): Promise<Uint8Array>;
7
+ export async function createSha256Hash(
8
+ data: SourceData,
9
+ asHex: true,
10
+ ): Promise<string>;
11
+
12
+ export async function createSha256Hash(
13
+ data: SourceData,
14
+ asHex?: boolean,
15
+ ): Promise<Uint8Array | string> {
6
16
  if (isBrowser) {
7
- return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
17
+ const source =
18
+ typeof data === "string" ? new TextEncoder().encode(data) : data;
19
+ const buffer = await window.crypto.subtle.digest("SHA-256", source);
20
+ const arr = new Uint8Array(buffer);
21
+ if (asHex) {
22
+ return bytesToHex(arr);
23
+ }
24
+ return arr;
8
25
  } else {
9
26
  const { createHash } = await import("crypto");
27
+ if (asHex) {
28
+ const hexStr = createHash("sha256").update(data).digest("hex");
29
+ return hexStr;
30
+ }
10
31
  const buffer = createHash("sha256").update(data).digest();
11
32
  return new Uint8Array(buffer);
12
33
  }
13
- };
34
+ }