@lightsparkdev/core 1.2.2 → 1.2.4

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.
@@ -229,15 +229,8 @@ class Requester {
229
229
  signingNodeId: string | undefined,
230
230
  /* eslint-disable-next-line @typescript-eslint/no-explicit-any -- LIG-3400 */
231
231
  ): Promise<Uint8Array> {
232
- let TextEncoderImpl;
233
- if (typeof TextEncoder === "undefined") {
234
- TextEncoderImpl = (await import("text-encoding")).TextEncoder;
235
- } else {
236
- TextEncoderImpl = TextEncoder;
237
- }
238
-
239
232
  if (!signingNodeId) {
240
- return new TextEncoderImpl().encode(JSON.stringify(queryPayload));
233
+ return new TextEncoder().encode(JSON.stringify(queryPayload));
241
234
  }
242
235
 
243
236
  const query = queryPayload.query;
@@ -262,9 +255,7 @@ class Requester {
262
255
  );
263
256
  }
264
257
 
265
- const encodedPayload = new TextEncoderImpl().encode(
266
- JSON.stringify(payload),
267
- );
258
+ const encodedPayload = new TextEncoder().encode(JSON.stringify(payload));
268
259
 
269
260
  const signedPayload = await key.sign(encodedPayload);
270
261
 
@@ -15,10 +15,11 @@ export const CurrencyUnit = {
15
15
  BITCOIN: "BITCOIN",
16
16
  SATOSHI: "SATOSHI",
17
17
  MILLISATOSHI: "MILLISATOSHI",
18
- USD: "USD",
19
18
  NANOBITCOIN: "NANOBITCOIN",
20
19
  MICROBITCOIN: "MICROBITCOIN",
21
20
  MILLIBITCOIN: "MILLIBITCOIN",
21
+ USD: "USD",
22
+ MXN: "MXN",
22
23
 
23
24
  Bitcoin: "BITCOIN",
24
25
  Microbitcoin: "MICROBITCOIN",
@@ -27,6 +28,7 @@ export const CurrencyUnit = {
27
28
  Nanobitcoin: "NANOBITCOIN",
28
29
  Satoshi: "SATOSHI",
29
30
  Usd: "USD",
31
+ Mxn: "MXN",
30
32
  } as const;
31
33
 
32
34
  export type CurrencyUnitType = (typeof CurrencyUnit)[keyof typeof CurrencyUnit];
@@ -39,6 +41,37 @@ export type SDKCurrencyAmountType = {
39
41
  preferredCurrencyValueApprox: number;
40
42
  };
41
43
 
44
+ const standardUnitConversionObj = {
45
+ [CurrencyUnit.BITCOIN]: (v: number, unitsPerBtc = 1) => v / unitsPerBtc,
46
+ [CurrencyUnit.MICROBITCOIN]: (v: number, unitsPerBtc = 1) =>
47
+ (v / unitsPerBtc) * 1_000_000,
48
+ [CurrencyUnit.MILLIBITCOIN]: (v: number, unitsPerBtc = 1) =>
49
+ (v / unitsPerBtc) * 1_000,
50
+ [CurrencyUnit.MILLISATOSHI]: (v: number, unitsPerBtc = 1) =>
51
+ (v / unitsPerBtc) * 100_000_000_000,
52
+ [CurrencyUnit.NANOBITCOIN]: (v: number, unitsPerBtc = 1) =>
53
+ (v / unitsPerBtc) * 1_000_000_000,
54
+ [CurrencyUnit.SATOSHI]: (v: number, unitsPerBtc = 1) =>
55
+ (v / unitsPerBtc) * 100_000_000,
56
+ /* Converting between two different fiat types is not currently supported */
57
+ [CurrencyUnit.USD]: (v: number) => v,
58
+ [CurrencyUnit.MXN]: (v: number) => v,
59
+ };
60
+
61
+ /* Round without decimals since we're returning cents: */
62
+ const toBitcoinConversion = (v: number, unitsPerBtc = 1) =>
63
+ round(v * unitsPerBtc);
64
+ const toMicrobitcoinConversion = (v: number, unitsPerBtc = 1) =>
65
+ round((v / 1_000_000) * unitsPerBtc);
66
+ const toMillibitcoinConversion = (v: number, unitsPerBtc = 1) =>
67
+ round((v / 1_000) * unitsPerBtc);
68
+ const toMillisatoshiConversion = (v: number, unitsPerBtc = 1) =>
69
+ round((v / 100_000_000_000) * unitsPerBtc);
70
+ const toNanobitcoinConversion = (v: number, unitsPerBtc = 1) =>
71
+ round((v / 1_000_000_000) * unitsPerBtc);
72
+ const toSatoshiConversion = (v: number, unitsPerBtc = 1) =>
73
+ round((v / 100_000_000) * unitsPerBtc);
74
+
42
75
  const CONVERSION_MAP = {
43
76
  [CurrencyUnit.BITCOIN]: {
44
77
  [CurrencyUnit.BITCOIN]: (v: number) => v,
@@ -47,9 +80,8 @@ const CONVERSION_MAP = {
47
80
  [CurrencyUnit.MILLISATOSHI]: (v: number) => v * 100_000_000_000,
48
81
  [CurrencyUnit.NANOBITCOIN]: (v: number) => v * 1_000_000_000,
49
82
  [CurrencyUnit.SATOSHI]: (v: number) => v * 100_000_000,
50
- [CurrencyUnit.USD]: (v: number, centsPerBtc = 1) =>
51
- /* Round without decimals since we're returning cents: */
52
- round(v * centsPerBtc, 2),
83
+ [CurrencyUnit.USD]: toBitcoinConversion,
84
+ [CurrencyUnit.MXN]: toBitcoinConversion,
53
85
  },
54
86
  [CurrencyUnit.MICROBITCOIN]: {
55
87
  [CurrencyUnit.BITCOIN]: (v: number) => v / 1_000_000,
@@ -58,9 +90,8 @@ const CONVERSION_MAP = {
58
90
  [CurrencyUnit.MILLISATOSHI]: (v: number) => v * 100_000,
59
91
  [CurrencyUnit.NANOBITCOIN]: (v: number) => v * 1000,
60
92
  [CurrencyUnit.SATOSHI]: (v: number) => v * 100,
61
- [CurrencyUnit.USD]: (v: number, centsPerBtc = 1) =>
62
- /* Round without decimals since we're returning cents: */
63
- round((v / 1_000_000) * centsPerBtc),
93
+ [CurrencyUnit.USD]: toMicrobitcoinConversion,
94
+ [CurrencyUnit.MXN]: toMicrobitcoinConversion,
64
95
  },
65
96
  [CurrencyUnit.MILLIBITCOIN]: {
66
97
  [CurrencyUnit.BITCOIN]: (v: number) => v / 1_000,
@@ -69,9 +100,8 @@ const CONVERSION_MAP = {
69
100
  [CurrencyUnit.MILLISATOSHI]: (v: number) => v * 100_000_000,
70
101
  [CurrencyUnit.NANOBITCOIN]: (v: number) => v * 1_000_000,
71
102
  [CurrencyUnit.SATOSHI]: (v: number) => v * 100_000,
72
- [CurrencyUnit.USD]: (v: number, centsPerBtc = 1) =>
73
- /* Round without decimals since we're returning cents: */
74
- round((v / 1_000) * centsPerBtc),
103
+ [CurrencyUnit.USD]: toMillibitcoinConversion,
104
+ [CurrencyUnit.MXN]: toMillibitcoinConversion,
75
105
  },
76
106
  [CurrencyUnit.MILLISATOSHI]: {
77
107
  [CurrencyUnit.BITCOIN]: (v: number) => v / 100_000_000_000,
@@ -80,9 +110,8 @@ const CONVERSION_MAP = {
80
110
  [CurrencyUnit.MILLISATOSHI]: (v: number) => v,
81
111
  [CurrencyUnit.NANOBITCOIN]: (v: number) => v / 100,
82
112
  [CurrencyUnit.SATOSHI]: (v: number) => v / 1000,
83
- [CurrencyUnit.USD]: (v: number, centsPerBtc = 1) =>
84
- /* Round without decimals since we're returning cents: */
85
- round((v / 100_000_000_000) * centsPerBtc),
113
+ [CurrencyUnit.USD]: toMillisatoshiConversion,
114
+ [CurrencyUnit.MXN]: toMillisatoshiConversion,
86
115
  },
87
116
  [CurrencyUnit.NANOBITCOIN]: {
88
117
  [CurrencyUnit.BITCOIN]: (v: number) => v / 1_000_000_000,
@@ -91,9 +120,8 @@ const CONVERSION_MAP = {
91
120
  [CurrencyUnit.MILLISATOSHI]: (v: number) => v * 100,
92
121
  [CurrencyUnit.NANOBITCOIN]: (v: number) => v,
93
122
  [CurrencyUnit.SATOSHI]: (v: number) => v / 10,
94
- [CurrencyUnit.USD]: (v: number, centsPerBtc = 1) =>
95
- /* Round without decimals since we're returning cents: */
96
- round((v / 1_000_000_000) * centsPerBtc),
123
+ [CurrencyUnit.USD]: toNanobitcoinConversion,
124
+ [CurrencyUnit.MXN]: toNanobitcoinConversion,
97
125
  },
98
126
  [CurrencyUnit.SATOSHI]: {
99
127
  [CurrencyUnit.BITCOIN]: (v: number) => v / 100_000_000,
@@ -102,31 +130,22 @@ const CONVERSION_MAP = {
102
130
  [CurrencyUnit.MILLISATOSHI]: (v: number) => v * 1000,
103
131
  [CurrencyUnit.NANOBITCOIN]: (v: number) => v * 10,
104
132
  [CurrencyUnit.SATOSHI]: (v: number) => v,
105
- [CurrencyUnit.USD]: (v: number, centsPerBtc = 1) =>
106
- /* Round without decimals since we're returning cents: */
107
- round((v / 100_000_000) * centsPerBtc),
108
- },
109
- [CurrencyUnit.USD]: {
110
- [CurrencyUnit.BITCOIN]: (v: number, centsPerBtc = 1) => v / centsPerBtc,
111
- [CurrencyUnit.MICROBITCOIN]: (v: number, centsPerBtc = 1) =>
112
- (v / centsPerBtc) * 1_000_000,
113
- [CurrencyUnit.MILLIBITCOIN]: (v: number, centsPerBtc = 1) =>
114
- (v / centsPerBtc) * 1_000,
115
- [CurrencyUnit.MILLISATOSHI]: (v: number, centsPerBtc = 1) =>
116
- (v / centsPerBtc) * 100_000_000_000,
117
- [CurrencyUnit.NANOBITCOIN]: (v: number, centsPerBtc = 1) =>
118
- (v / centsPerBtc) * 1_000_000_000,
119
- [CurrencyUnit.SATOSHI]: (v: number, centsPerBtc = 1) =>
120
- (v / centsPerBtc) * 100_000_000,
121
- [CurrencyUnit.USD]: (v: number) => v,
133
+ [CurrencyUnit.USD]: toSatoshiConversion,
134
+ [CurrencyUnit.MXN]: toSatoshiConversion,
122
135
  },
136
+ [CurrencyUnit.USD]: standardUnitConversionObj,
137
+ [CurrencyUnit.MXN]: standardUnitConversionObj,
123
138
  };
124
139
 
125
140
  export function convertCurrencyAmountValue(
126
141
  fromUnit: CurrencyUnitType,
127
142
  toUnit: CurrencyUnitType,
128
143
  amount: number,
129
- centsPerBtc = 1,
144
+ /* Currency values are expected to always be provided in whole numbers of their smallest unit
145
+ e.g. $50.11 would be 5011. unitsPerBtc is the approximate value of one BTC in smallest
146
+ units to provide value estimates where needed where a backend value is not available, eg
147
+ previewing the approximate value of an amount to send. */
148
+ unitsPerBtc = 1,
130
149
  ): number {
131
150
  if (
132
151
  fromUnit === CurrencyUnit.FUTURE_VALUE ||
@@ -147,7 +166,7 @@ export function convertCurrencyAmountValue(
147
166
  );
148
167
  }
149
168
 
150
- return conversionFn(amount, centsPerBtc);
169
+ return conversionFn(amount, unitsPerBtc);
151
170
  }
152
171
 
153
172
  export const convertCurrencyAmount = (
@@ -178,6 +197,7 @@ export type CurrencyMap = {
178
197
  [CurrencyUnit.MILLIBITCOIN]: number;
179
198
  [CurrencyUnit.NANOBITCOIN]: number;
180
199
  [CurrencyUnit.USD]: number;
200
+ [CurrencyUnit.MXN]: number;
181
201
  [CurrencyUnit.FUTURE_VALUE]: number;
182
202
  formatted: {
183
203
  sats: string;
@@ -190,6 +210,7 @@ export type CurrencyMap = {
190
210
  [CurrencyUnit.MICROBITCOIN]: string;
191
211
  [CurrencyUnit.NANOBITCOIN]: string;
192
212
  [CurrencyUnit.USD]: string;
213
+ [CurrencyUnit.MXN]: string;
193
214
  [CurrencyUnit.FUTURE_VALUE]: string;
194
215
  };
195
216
  isZero: boolean;
@@ -199,7 +220,7 @@ export type CurrencyMap = {
199
220
  type: "CurrencyMap";
200
221
  };
201
222
 
202
- export type CurrencyAmountObj = {
223
+ export type DeprecatedCurrencyAmountObj = {
203
224
  /* Technically the generated graphql schema has value as `any` but it's always a number.
204
225
  * We are intentionally widening the type here to allow for more forgiving input: */
205
226
  value?: number | string | null;
@@ -208,18 +229,60 @@ export type CurrencyAmountObj = {
208
229
  __typename?: "CurrencyAmount" | undefined;
209
230
  };
210
231
 
232
+ export type CurrencyAmountObj = {
233
+ /* Technically the generated graphql schema has value as `any` but it's always a number.
234
+ * We are intentionally widening the type here to allow for more forgiving input: */
235
+ original_value?: number | string | null;
236
+ /* assume satoshi if not provided */
237
+ original_unit?: CurrencyUnitType;
238
+ __typename?: "CurrencyAmount" | undefined;
239
+ };
240
+
241
+ export type CurrencyAmountPreferenceObj = {
242
+ /* Technically the generated graphql schema has value as `any` but it's always a number.
243
+ * We are intentionally widening the type here to allow for more forgiving input: */
244
+ preferred_currency_unit?: CurrencyUnitType;
245
+ /* assume satoshi if not provided */
246
+ preferred_currency_value_rounded?: number | string | null;
247
+ __typename?: "CurrencyAmount" | undefined;
248
+ };
249
+
211
250
  export type CurrencyAmountArg =
251
+ | DeprecatedCurrencyAmountObj
212
252
  | CurrencyAmountObj
253
+ | CurrencyAmountPreferenceObj
213
254
  | SDKCurrencyAmountType
214
255
  | undefined
215
256
  | null;
216
257
 
217
- export function isCurrencyAmountObj(arg: unknown): arg is CurrencyAmountObj {
258
+ export function isDeprecatedCurrencyAmountObj(
259
+ arg: unknown,
260
+ ): arg is DeprecatedCurrencyAmountObj {
218
261
  return (
219
262
  typeof arg === "object" && arg !== null && "value" in arg && "unit" in arg
220
263
  );
221
264
  }
222
265
 
266
+ export function isCurrencyAmountObj(arg: unknown): arg is CurrencyAmountObj {
267
+ return (
268
+ typeof arg === "object" &&
269
+ arg !== null &&
270
+ "original_value" in arg &&
271
+ "original_unit" in arg
272
+ );
273
+ }
274
+
275
+ export function isCurrencyAmountPreferenceObj(
276
+ arg: unknown,
277
+ ): arg is CurrencyAmountPreferenceObj {
278
+ return (
279
+ typeof arg === "object" &&
280
+ arg !== null &&
281
+ "preferred_currency_unit" in arg &&
282
+ "preferred_currency_value_rounded" in arg
283
+ );
284
+ }
285
+
223
286
  export function isSDKCurrencyAmount(
224
287
  arg: unknown,
225
288
  ): arg is SDKCurrencyAmountType {
@@ -248,7 +311,13 @@ function getCurrencyAmount(currencyAmountArg: CurrencyAmountArg) {
248
311
  if (isSDKCurrencyAmount(currencyAmountArg)) {
249
312
  value = currencyAmountArg.originalValue;
250
313
  unit = currencyAmountArg.originalUnit;
314
+ } else if (isCurrencyAmountPreferenceObj(currencyAmountArg)) {
315
+ value = asNumber(currencyAmountArg.preferred_currency_value_rounded);
316
+ unit = currencyAmountArg.preferred_currency_unit;
251
317
  } else if (isCurrencyAmountObj(currencyAmountArg)) {
318
+ value = asNumber(currencyAmountArg.original_value);
319
+ unit = currencyAmountArg.original_unit;
320
+ } else if (isDeprecatedCurrencyAmountObj(currencyAmountArg)) {
252
321
  value = asNumber(currencyAmountArg.value);
253
322
  unit = currencyAmountArg.unit;
254
323
  }
@@ -261,24 +330,26 @@ function getCurrencyAmount(currencyAmountArg: CurrencyAmountArg) {
261
330
 
262
331
  export function mapCurrencyAmount(
263
332
  currencyAmountArg: CurrencyAmountArg,
264
- centsPerBtc = 1,
333
+ unitsPerBtc = 1,
265
334
  ): CurrencyMap {
266
335
  const { value, unit } = getCurrencyAmount(currencyAmountArg);
267
336
 
268
337
  const convert = convertCurrencyAmountValue;
269
- const sats = convert(unit, CurrencyUnit.SATOSHI, value, centsPerBtc);
270
- const btc = convert(unit, CurrencyUnit.BITCOIN, value, centsPerBtc);
271
- const msats = convert(unit, CurrencyUnit.MILLISATOSHI, value, centsPerBtc);
272
- const usd = convert(unit, CurrencyUnit.USD, value, centsPerBtc);
273
- const mibtc = convert(unit, CurrencyUnit.MICROBITCOIN, value, centsPerBtc);
274
- const mlbtc = convert(unit, CurrencyUnit.MILLIBITCOIN, value, centsPerBtc);
275
- const nbtc = convert(unit, CurrencyUnit.NANOBITCOIN, value, centsPerBtc);
338
+ const sats = convert(unit, CurrencyUnit.SATOSHI, value, unitsPerBtc);
339
+ const btc = convert(unit, CurrencyUnit.BITCOIN, value, unitsPerBtc);
340
+ const msats = convert(unit, CurrencyUnit.MILLISATOSHI, value, unitsPerBtc);
341
+ const usd = convert(unit, CurrencyUnit.USD, value, unitsPerBtc);
342
+ const mxn = convert(unit, CurrencyUnit.MXN, value, unitsPerBtc);
343
+ const mibtc = convert(unit, CurrencyUnit.MICROBITCOIN, value, unitsPerBtc);
344
+ const mlbtc = convert(unit, CurrencyUnit.MILLIBITCOIN, value, unitsPerBtc);
345
+ const nbtc = convert(unit, CurrencyUnit.NANOBITCOIN, value, unitsPerBtc);
276
346
 
277
347
  const mapWithCurrencyUnits = {
278
348
  [CurrencyUnit.BITCOIN]: btc,
279
349
  [CurrencyUnit.SATOSHI]: sats,
280
350
  [CurrencyUnit.MILLISATOSHI]: msats,
281
351
  [CurrencyUnit.USD]: usd,
352
+ [CurrencyUnit.MXN]: mxn,
282
353
  [CurrencyUnit.MICROBITCOIN]: mibtc,
283
354
  [CurrencyUnit.MILLIBITCOIN]: mlbtc,
284
355
  [CurrencyUnit.NANOBITCOIN]: nbtc,
@@ -312,6 +383,10 @@ export function mapCurrencyAmount(
312
383
  value: usd,
313
384
  unit: CurrencyUnit.USD,
314
385
  }),
386
+ [CurrencyUnit.MXN]: formatCurrencyStr({
387
+ value: mxn,
388
+ unit: CurrencyUnit.MXN,
389
+ }),
315
390
  [CurrencyUnit.FUTURE_VALUE]: "-",
316
391
  },
317
392
  };