@portal-hq/utils 0.2.16 → 0.2.18

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.
@@ -28,7 +28,6 @@ const convert = (value, from = 'ETH', countryCode) => __awaiter(void 0, void 0,
28
28
  });
29
29
  exports.convert = convert;
30
30
  const convertCryptoToLocal = (value, from = 'ETH', countryCode) => __awaiter(void 0, void 0, void 0, function* () {
31
- console.log("to local");
32
31
  const country = countries_1.countryCodes[countryCode || 'US'];
33
32
  if (!country) {
34
33
  throw new Error(`Unable to find country name for country code: ${countryCode}.`);
@@ -41,12 +40,10 @@ const convertCryptoToLocal = (value, from = 'ETH', countryCode) => __awaiter(voi
41
40
  const response = yield fetch(url);
42
41
  const data = yield response.json();
43
42
  const convertedValue = Math.round((value * data[to]) * 10000) / 10000;
44
- console.log("Converted value: ", convertedValue);
45
43
  return convertedValue;
46
44
  });
47
45
  exports.convertCryptoToLocal = convertCryptoToLocal;
48
46
  const convertLocalToCrypto = (value, to = 'ETH', countryCode) => __awaiter(void 0, void 0, void 0, function* () {
49
- console.log("to Crypto");
50
47
  const country = countries_1.countryCodes[countryCode || 'US'];
51
48
  if (!country) {
52
49
  throw new Error(`Unable to find country name for country code: ${countryCode}.`);
@@ -58,9 +55,7 @@ const convertLocalToCrypto = (value, to = 'ETH', countryCode) => __awaiter(void
58
55
  const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${to}`;
59
56
  const response = yield fetch(url);
60
57
  const data = yield response.json();
61
- console.log(data);
62
58
  const convertedValue = Math.round((value / (1 / data[to]) + Number.EPSILON) * 10000) / 10000;
63
- console.log("Converted value: ", convertedValue);
64
59
  return convertedValue;
65
60
  });
66
61
  exports.convertLocalToCrypto = convertLocalToCrypto;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class MpcSigningError extends Error {
4
4
  constructor(message) {
5
- super(`[Portal] An error occurred in the MPC signing process: ${message}`);
5
+ super(`[Portal] An error occurred in the MPC signing process: ${message}.`);
6
6
  // Set the prototype explicitly.
7
7
  Object.setPrototypeOf(this, MpcSigningError.prototype);
8
8
  }
@@ -9,23 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.parseRequest = void 0;
12
+ exports.parseRequest = exports.hexStringToString = exports.hexStringToNumber = exports.hexStringToByteArray = void 0;
13
13
  const currency_1 = require("../currency");
14
- const hexStringToString = (hexInput) => {
15
- // Get the proper hex string
16
- const byteArray = Array.from(hexStringToByteArray(hexInput.replace('0x', '')));
17
- // Create an array of characters
18
- const stringArray = byteArray.reduce((acc, charCode) => {
19
- // Add the character matching the charCode
20
- acc.push(String.fromCharCode(charCode));
21
- // Return the accumulator
22
- return acc;
23
- }, []);
24
- // Join the array of characters
25
- const string = stringArray.join('');
26
- // Return the final string
27
- return string;
28
- };
29
14
  const hexStringToByteArray = (hexString) => {
30
15
  // Verify the length of the string
31
16
  if (hexString.length % 2 !== 0) {
@@ -45,21 +30,37 @@ const hexStringToByteArray = (hexString) => {
45
30
  // Return a byte array
46
31
  return byteArray;
47
32
  };
33
+ exports.hexStringToByteArray = hexStringToByteArray;
48
34
  const hexStringToNumber = (hexBalance) => {
49
35
  const balanceNumber = Number(hexBalance);
50
- console.log(`balanceNumber`, balanceNumber);
51
- const balance = Math.round((balanceNumber / 1e18) * 1000000) / 1000000;
36
+ const balance = Math.round((balanceNumber / 1e18) * 100000) / 100000;
52
37
  return balance;
53
38
  };
39
+ exports.hexStringToNumber = hexStringToNumber;
40
+ const hexStringToString = (hexInput) => {
41
+ // Get the proper hex string
42
+ const byteArray = Array.from((0, exports.hexStringToByteArray)(hexInput.replace('0x', '')));
43
+ // Create an array of characters
44
+ const stringArray = byteArray.reduce((acc, charCode) => {
45
+ // Add the character matching the charCode
46
+ acc.push(String.fromCharCode(charCode));
47
+ // Return the accumulator
48
+ return acc;
49
+ }, []);
50
+ // Join the array of characters
51
+ const string = stringArray.join('');
52
+ // Return the final string
53
+ return string;
54
+ };
55
+ exports.hexStringToString = hexStringToString;
54
56
  const parseRequest = (method, params) => __awaiter(void 0, void 0, void 0, function* () {
55
57
  let requestData;
56
- console.log(`Request: `, JSON.stringify({ method, params }));
57
58
  const txParam0 = Array.isArray(params) ? params[0] : params;
58
59
  switch (method) {
59
60
  case 'eth_sendTransaction':
60
61
  case 'eth_signTransaction':
61
62
  const payload = txParam0;
62
- const value = hexStringToNumber(payload.value);
63
+ const value = (0, exports.hexStringToNumber)(payload.value);
63
64
  requestData = {
64
65
  data: payload.data,
65
66
  from: payload.from,
@@ -74,7 +75,7 @@ const parseRequest = (method, params) => __awaiter(void 0, void 0, void 0, funct
74
75
  case 'eth_sign':
75
76
  case 'personal_sign':
76
77
  requestData = {
77
- message: hexStringToString(txParam0),
78
+ message: (0, exports.hexStringToString)(txParam0),
78
79
  };
79
80
  return {
80
81
  method,
@@ -87,7 +88,6 @@ const parseRequest = (method, params) => __awaiter(void 0, void 0, void 0, funct
87
88
  message: 'NO_DATA_PROVIDED',
88
89
  },
89
90
  };
90
- break;
91
91
  }
92
92
  });
93
93
  exports.parseRequest = parseRequest;
@@ -16,15 +16,28 @@ class HttpRequester {
16
16
  ? baseUrl
17
17
  : `https://${baseUrl}`;
18
18
  }
19
+ buildHeaders(headers = {}) {
20
+ return Object.assign({ 'Content-Type': 'application/json' }, headers);
21
+ }
22
+ delete(path, options) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ const requestOptions = {
25
+ method: 'DELETE',
26
+ url: `${this.baseUrl}${path}`,
27
+ };
28
+ requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {});
29
+ const request = new index_1.HttpRequest(requestOptions);
30
+ const response = (yield request.send());
31
+ return response;
32
+ });
33
+ }
19
34
  get(path, options) {
20
35
  return __awaiter(this, void 0, void 0, function* () {
21
36
  const requestOptions = {
22
37
  method: 'GET',
23
38
  url: `${this.baseUrl}${path}`,
24
39
  };
25
- if (options && options.headers) {
26
- requestOptions.headers = this.buildHeaders(options.headers);
27
- }
40
+ requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {});
28
41
  const request = new index_1.HttpRequest(requestOptions);
29
42
  const response = (yield request.send());
30
43
  return response;
@@ -45,8 +58,5 @@ class HttpRequester {
45
58
  return response;
46
59
  });
47
60
  }
48
- buildHeaders(headers) {
49
- return Object.assign({ 'Content-Type': 'application/json' }, headers);
50
- }
51
61
  }
52
62
  exports.default = HttpRequester;
@@ -24,7 +24,6 @@ export const convert = (value, from = 'ETH', countryCode) => __awaiter(void 0, v
24
24
  return convertedValue;
25
25
  });
26
26
  export const convertCryptoToLocal = (value, from = 'ETH', countryCode) => __awaiter(void 0, void 0, void 0, function* () {
27
- console.log("to local");
28
27
  const country = countryCodes[countryCode || 'US'];
29
28
  if (!country) {
30
29
  throw new Error(`Unable to find country name for country code: ${countryCode}.`);
@@ -37,11 +36,9 @@ export const convertCryptoToLocal = (value, from = 'ETH', countryCode) => __awai
37
36
  const response = yield fetch(url);
38
37
  const data = yield response.json();
39
38
  const convertedValue = Math.round((value * data[to]) * 10000) / 10000;
40
- console.log("Converted value: ", convertedValue);
41
39
  return convertedValue;
42
40
  });
43
41
  export const convertLocalToCrypto = (value, to = 'ETH', countryCode) => __awaiter(void 0, void 0, void 0, function* () {
44
- console.log("to Crypto");
45
42
  const country = countryCodes[countryCode || 'US'];
46
43
  if (!country) {
47
44
  throw new Error(`Unable to find country name for country code: ${countryCode}.`);
@@ -53,8 +50,6 @@ export const convertLocalToCrypto = (value, to = 'ETH', countryCode) => __awaite
53
50
  const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${to}`;
54
51
  const response = yield fetch(url);
55
52
  const data = yield response.json();
56
- console.log(data);
57
53
  const convertedValue = Math.round((value / (1 / data[to]) + Number.EPSILON) * 10000) / 10000;
58
- console.log("Converted value: ", convertedValue);
59
54
  return convertedValue;
60
55
  });
@@ -1,6 +1,6 @@
1
1
  class MpcSigningError extends Error {
2
2
  constructor(message) {
3
- super(`[Portal] An error occurred in the MPC signing process: ${message}`);
3
+ super(`[Portal] An error occurred in the MPC signing process: ${message}.`);
4
4
  // Set the prototype explicitly.
5
5
  Object.setPrototypeOf(this, MpcSigningError.prototype);
6
6
  }
@@ -8,22 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { convert } from '../currency';
11
- const hexStringToString = (hexInput) => {
12
- // Get the proper hex string
13
- const byteArray = Array.from(hexStringToByteArray(hexInput.replace('0x', '')));
14
- // Create an array of characters
15
- const stringArray = byteArray.reduce((acc, charCode) => {
16
- // Add the character matching the charCode
17
- acc.push(String.fromCharCode(charCode));
18
- // Return the accumulator
19
- return acc;
20
- }, []);
21
- // Join the array of characters
22
- const string = stringArray.join('');
23
- // Return the final string
24
- return string;
25
- };
26
- const hexStringToByteArray = (hexString) => {
11
+ export const hexStringToByteArray = (hexString) => {
27
12
  // Verify the length of the string
28
13
  if (hexString.length % 2 !== 0) {
29
14
  throw Error('[Portal] hexStringToByteArray() detected invalid input lenth for hexString.');
@@ -42,15 +27,28 @@ const hexStringToByteArray = (hexString) => {
42
27
  // Return a byte array
43
28
  return byteArray;
44
29
  };
45
- const hexStringToNumber = (hexBalance) => {
30
+ export const hexStringToNumber = (hexBalance) => {
46
31
  const balanceNumber = Number(hexBalance);
47
- console.log(`balanceNumber`, balanceNumber);
48
- const balance = Math.round((balanceNumber / 1e18) * 1000000) / 1000000;
32
+ const balance = Math.round((balanceNumber / 1e18) * 100000) / 100000;
49
33
  return balance;
50
34
  };
35
+ export const hexStringToString = (hexInput) => {
36
+ // Get the proper hex string
37
+ const byteArray = Array.from(hexStringToByteArray(hexInput.replace('0x', '')));
38
+ // Create an array of characters
39
+ const stringArray = byteArray.reduce((acc, charCode) => {
40
+ // Add the character matching the charCode
41
+ acc.push(String.fromCharCode(charCode));
42
+ // Return the accumulator
43
+ return acc;
44
+ }, []);
45
+ // Join the array of characters
46
+ const string = stringArray.join('');
47
+ // Return the final string
48
+ return string;
49
+ };
51
50
  export const parseRequest = (method, params) => __awaiter(void 0, void 0, void 0, function* () {
52
51
  let requestData;
53
- console.log(`Request: `, JSON.stringify({ method, params }));
54
52
  const txParam0 = Array.isArray(params) ? params[0] : params;
55
53
  switch (method) {
56
54
  case 'eth_sendTransaction':
@@ -84,6 +82,5 @@ export const parseRequest = (method, params) => __awaiter(void 0, void 0, void 0
84
82
  message: 'NO_DATA_PROVIDED',
85
83
  },
86
84
  };
87
- break;
88
85
  }
89
86
  });
@@ -14,15 +14,28 @@ class HttpRequester {
14
14
  ? baseUrl
15
15
  : `https://${baseUrl}`;
16
16
  }
17
+ buildHeaders(headers = {}) {
18
+ return Object.assign({ 'Content-Type': 'application/json' }, headers);
19
+ }
20
+ delete(path, options) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ const requestOptions = {
23
+ method: 'DELETE',
24
+ url: `${this.baseUrl}${path}`,
25
+ };
26
+ requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {});
27
+ const request = new HttpRequest(requestOptions);
28
+ const response = (yield request.send());
29
+ return response;
30
+ });
31
+ }
17
32
  get(path, options) {
18
33
  return __awaiter(this, void 0, void 0, function* () {
19
34
  const requestOptions = {
20
35
  method: 'GET',
21
36
  url: `${this.baseUrl}${path}`,
22
37
  };
23
- if (options && options.headers) {
24
- requestOptions.headers = this.buildHeaders(options.headers);
25
- }
38
+ requestOptions.headers = this.buildHeaders(options && options.headers ? options.headers : {});
26
39
  const request = new HttpRequest(requestOptions);
27
40
  const response = (yield request.send());
28
41
  return response;
@@ -43,8 +56,5 @@ class HttpRequester {
43
56
  return response;
44
57
  });
45
58
  }
46
- buildHeaders(headers) {
47
- return Object.assign({ 'Content-Type': 'application/json' }, headers);
48
- }
49
59
  }
50
60
  export default HttpRequester;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portal-hq/utils",
3
- "version": "0.2.16",
3
+ "version": "0.2.18",
4
4
  "main": "lib/commonjs/index",
5
5
  "module": "lib/esm/index",
6
6
  "source": "src/index",
@@ -14,9 +14,15 @@
14
14
  "scripts": {
15
15
  "prepare": "yarn prepare:cjs && yarn prepare:esm",
16
16
  "prepare:cjs": "tsc --outDir lib/commonjs --module commonjs",
17
- "prepare:esm": "tsc --outDir lib/esm --module es2015 --target es2015"
17
+ "prepare:esm": "tsc --outDir lib/esm --module es2015 --target es2015",
18
+ "test": "jest"
18
19
  },
19
20
  "devDependencies": {
21
+ "@babel/preset-typescript": "^7.18.6",
22
+ "@types/jest": "^29.2.0",
23
+ "jest": "^29.2.1",
24
+ "jest-environment-jsdom": "^29.2.2",
25
+ "ts-jest": "^29.0.3",
20
26
  "typescript": "^4.8.4"
21
27
  }
22
28
  }
@@ -32,7 +32,6 @@ export const convertCryptoToLocal = async (
32
32
  from: string = 'ETH',
33
33
  countryCode?: string
34
34
  ): Promise<number> => {
35
- console.log("to local");
36
35
 
37
36
  const country = countryCodes[countryCode || 'US']
38
37
  if (!country) {
@@ -52,7 +51,6 @@ export const convertCryptoToLocal = async (
52
51
 
53
52
  const convertedValue =
54
53
  Math.round((value * data[to]) * 10000) / 10000
55
- console.log("Converted value: ", convertedValue);
56
54
 
57
55
  return convertedValue
58
56
  }
@@ -63,7 +61,6 @@ export const convertLocalToCrypto = async (
63
61
  to: string = 'ETH',
64
62
  countryCode: string
65
63
  ): Promise<number> => {
66
- console.log("to Crypto");
67
64
 
68
65
  const country = countryCodes[countryCode || 'US']
69
66
  if (!country) {
@@ -80,12 +77,9 @@ export const convertLocalToCrypto = async (
80
77
  const url = `https://min-api.cryptocompare.com/data/price?fsym=${from}&tsyms=${to}`
81
78
  const response = await fetch(url)
82
79
  const data = await response.json()
83
- console.log(data);
84
-
85
80
 
86
81
  const convertedValue =
87
82
  Math.round((value / (1 / data[to]) + Number.EPSILON) * 10000) / 10000
88
- console.log("Converted value: ", convertedValue);
89
83
 
90
84
  return convertedValue
91
85
  }
@@ -1,6 +1,6 @@
1
1
  class MpcSigningError extends Error {
2
2
  constructor(message: string) {
3
- super(`[Portal] An error occurred in the MPC signing process: ${message}`)
3
+ super(`[Portal] An error occurred in the MPC signing process: ${message}.`)
4
4
 
5
5
  // Set the prototype explicitly.
6
6
  Object.setPrototypeOf(this, MpcSigningError.prototype)
@@ -7,30 +7,7 @@ import {
7
7
  type SignableTransactionData,
8
8
  } from '../../types'
9
9
 
10
- const hexStringToString = (hexInput: string): string => {
11
- // Get the proper hex string
12
- const byteArray = Array.from(hexStringToByteArray(hexInput.replace('0x', '')))
13
-
14
- // Create an array of characters
15
- const stringArray: string[] = byteArray.reduce(
16
- (acc: string[], charCode: number): string[] => {
17
- // Add the character matching the charCode
18
- acc.push(String.fromCharCode(charCode))
19
-
20
- // Return the accumulator
21
- return acc
22
- },
23
- [] as string[],
24
- )
25
-
26
- // Join the array of characters
27
- const string = stringArray.join('')
28
-
29
- // Return the final string
30
- return string
31
- }
32
-
33
- const hexStringToByteArray = (hexString: string): Uint8Array => {
10
+ export const hexStringToByteArray = (hexString: string): Uint8Array => {
34
11
  // Verify the length of the string
35
12
  if (hexString.length % 2 !== 0) {
36
13
  throw Error(
@@ -57,22 +34,43 @@ const hexStringToByteArray = (hexString: string): Uint8Array => {
57
34
  return byteArray
58
35
  }
59
36
 
60
- const hexStringToNumber = (hexBalance: string): number => {
37
+ export const hexStringToNumber = (hexBalance: string): number => {
61
38
  const balanceNumber = Number(hexBalance)
62
- console.log(`balanceNumber`, balanceNumber)
63
- const balance: number = Math.round((balanceNumber / 1e18) * 1000000) / 1000000
39
+
40
+ const balance: number = Math.round((balanceNumber / 1e18) * 100000) / 100000
64
41
 
65
42
  return balance
66
43
  }
67
44
 
45
+ export const hexStringToString = (hexInput: string): string => {
46
+ // Get the proper hex string
47
+ const byteArray = Array.from(hexStringToByteArray(hexInput.replace('0x', '')))
48
+
49
+ // Create an array of characters
50
+ const stringArray: string[] = byteArray.reduce(
51
+ (acc: string[], charCode: number): string[] => {
52
+ // Add the character matching the charCode
53
+ acc.push(String.fromCharCode(charCode))
54
+
55
+ // Return the accumulator
56
+ return acc
57
+ },
58
+ [] as string[],
59
+ )
60
+
61
+ // Join the array of characters
62
+ const string = stringArray.join('')
63
+
64
+ // Return the final string
65
+ return string
66
+ }
67
+
68
68
  export const parseRequest = async (
69
69
  method: string,
70
70
  params: any,
71
71
  ): Promise<OutgoingRequest> => {
72
72
  let requestData: OutgoingRequestData
73
73
 
74
- console.log(`Request: `, JSON.stringify({ method, params }))
75
-
76
74
  const txParam0 = Array.isArray(params) ? params[0] : params
77
75
 
78
76
  switch (method) {
@@ -109,6 +107,5 @@ export const parseRequest = async (
109
107
  message: 'NO_DATA_PROVIDED',
110
108
  },
111
109
  }
112
- break
113
110
  }
114
111
  }
@@ -5,11 +5,11 @@ import {
5
5
  import { HttpError } from '../errors'
6
6
 
7
7
  class HttpRequest {
8
- private body: Record<string, any>
9
- private headers: Record<string, any>
10
- private method: ValidHttpRequestMethods
11
- private request: XMLHttpRequest
12
- private url: string
8
+ public body: Record<string, any>
9
+ public headers: Record<string, any>
10
+ public method: ValidHttpRequestMethods
11
+ public request: XMLHttpRequest
12
+ public url: string
13
13
 
14
14
  constructor({ body = {}, headers = {}, method, url }: HttpRequestOptions) {
15
15
  this.body = body
@@ -7,7 +7,7 @@ import {
7
7
  } from '../../types'
8
8
 
9
9
  class HttpRequester {
10
- private baseUrl: string
10
+ public baseUrl: string
11
11
 
12
12
  constructor({ baseUrl }: HttpRequesterOptions) {
13
13
  this.baseUrl = baseUrl.startsWith('https://')
@@ -15,15 +15,38 @@ class HttpRequester {
15
15
  : `https://${baseUrl}`
16
16
  }
17
17
 
18
+ public buildHeaders(headers: Record<string, any> = {}): Record<string, any> {
19
+ return {
20
+ 'Content-Type': 'application/json',
21
+ ...headers,
22
+ }
23
+ }
24
+
25
+ public async delete<T>(path: string, options?: HttpOptions): Promise<T> {
26
+ const requestOptions = {
27
+ method: 'DELETE',
28
+ url: `${this.baseUrl}${path}`,
29
+ } as HttpRequestOptions
30
+
31
+ requestOptions.headers = this.buildHeaders(
32
+ options && options.headers ? options.headers : {},
33
+ )
34
+
35
+ const request = new HttpRequest(requestOptions)
36
+ const response = (await request.send()) as T
37
+
38
+ return response
39
+ }
40
+
18
41
  public async get<T>(path: string, options?: HttpOptions): Promise<T> {
19
42
  const requestOptions = {
20
43
  method: 'GET',
21
44
  url: `${this.baseUrl}${path}`,
22
45
  } as HttpRequestOptions
23
46
 
24
- if (options && options.headers) {
25
- requestOptions.headers = this.buildHeaders(options.headers)
26
- }
47
+ requestOptions.headers = this.buildHeaders(
48
+ options && options.headers ? options.headers : {},
49
+ )
27
50
 
28
51
  const request = new HttpRequest(requestOptions)
29
52
  const response = (await request.send()) as T
@@ -50,13 +73,6 @@ class HttpRequester {
50
73
 
51
74
  return response
52
75
  }
53
-
54
- private buildHeaders(headers: Record<string, any>): Record<string, any> {
55
- return {
56
- 'Content-Type': 'application/json',
57
- ...headers,
58
- }
59
- }
60
76
  }
61
77
 
62
78
  export default HttpRequester