@portal-hq/utils 0.2.0-beta1
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/lib/commonjs/currency/countries/country-codes.js +250 -0
- package/lib/commonjs/currency/countries/currency-codes.js +248 -0
- package/lib/commonjs/currency/countries/index.js +10 -0
- package/lib/commonjs/currency/index.js +29 -0
- package/lib/commonjs/errors/http-error.js +10 -0
- package/lib/commonjs/errors/index.js +23 -0
- package/lib/commonjs/errors/invalid-api-key-error.js +10 -0
- package/lib/commonjs/errors/invalid-chain-id-error.js +10 -0
- package/lib/commonjs/errors/invalid-gateway-config-error.js +10 -0
- package/lib/commonjs/errors/invalid-rpc-config-error.js +10 -0
- package/lib/commonjs/errors/missing-option-error.js +10 -0
- package/lib/commonjs/errors/mpc-signing-error.js +10 -0
- package/lib/commonjs/errors/no-address-error.js +10 -0
- package/lib/commonjs/errors/provider-rpc-error.js +52 -0
- package/lib/commonjs/eth-requests/index.js +87 -0
- package/lib/commonjs/http-request/index.js +72 -0
- package/lib/commonjs/index.js +46 -0
- package/lib/commonjs/requesters/http.js +52 -0
- package/lib/commonjs/requesters/index.js +8 -0
- package/lib/esm/currency/countries/country-codes.js +248 -0
- package/lib/esm/currency/countries/currency-codes.js +246 -0
- package/lib/esm/currency/countries/index.js +2 -0
- package/lib/esm/currency/index.js +25 -0
- package/lib/esm/errors/http-error.js +8 -0
- package/lib/esm/errors/index.js +8 -0
- package/lib/esm/errors/invalid-api-key-error.js +8 -0
- package/lib/esm/errors/invalid-chain-id-error.js +8 -0
- package/lib/esm/errors/invalid-gateway-config-error.js +8 -0
- package/lib/esm/errors/invalid-rpc-config-error.js +26 -0
- package/lib/esm/errors/missing-option-error.js +8 -0
- package/lib/esm/errors/mpc-signing-error.js +8 -0
- package/lib/esm/errors/no-address-error.js +8 -0
- package/lib/esm/errors/provider-rpc-error.js +49 -0
- package/lib/esm/eth-requests/index.js +83 -0
- package/lib/esm/http-request/index.js +70 -0
- package/lib/esm/index.js +6 -0
- package/lib/esm/requesters/http.js +50 -0
- package/lib/esm/requesters/index.js +1 -0
- package/package.json +22 -0
- package/src/currency/countries/country-codes.ts +249 -0
- package/src/currency/countries/currency-codes.ts +247 -0
- package/src/currency/countries/index.ts +2 -0
- package/src/currency/index.ts +28 -0
- package/src/errors/http-error.ts +12 -0
- package/src/errors/index.ts +11 -0
- package/src/errors/invalid-api-key-error.ts +10 -0
- package/src/errors/invalid-chain-id-error.ts +10 -0
- package/src/errors/invalid-gateway-config-error.ts +10 -0
- package/src/errors/missing-option-error.ts +14 -0
- package/src/errors/mpc-signing-error.ts +10 -0
- package/src/errors/no-address-error.ts +10 -0
- package/src/errors/provider-rpc-error.ts +69 -0
- package/src/eth-requests/index.ts +109 -0
- package/src/http-request/index.ts +90 -0
- package/src/index.ts +25 -0
- package/src/requesters/http.ts +62 -0
- package/src/requesters/index.ts +1 -0
- package/types.d.ts +61 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { convert } from '../currency'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type MessageData,
|
|
5
|
+
type OutgoingRequest,
|
|
6
|
+
type OutgoingRequestData,
|
|
7
|
+
type SignableTransactionData,
|
|
8
|
+
} from '../../types'
|
|
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 => {
|
|
34
|
+
// Verify the length of the string
|
|
35
|
+
if (hexString.length % 2 !== 0) {
|
|
36
|
+
throw Error(
|
|
37
|
+
'[Portal] hexStringToByteArray() detected invalid input lenth for hexString.',
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Get the total bytes
|
|
42
|
+
const totalBytes = hexString.length / 2
|
|
43
|
+
|
|
44
|
+
// Initialize a Uint8Array with a length matching totalBytes
|
|
45
|
+
const byteArray = new Uint8Array(totalBytes)
|
|
46
|
+
|
|
47
|
+
// Loop of the resulting array
|
|
48
|
+
for (let i = 0; i < totalBytes; i++) {
|
|
49
|
+
// Get the byte at the current index
|
|
50
|
+
const substring = hexString.substring(i * 2, i * 2 + 2).toLowerCase()
|
|
51
|
+
|
|
52
|
+
// Get the integer matching the current byte and push to the array
|
|
53
|
+
byteArray[i] = parseInt(substring, 16)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Return a byte array
|
|
57
|
+
return byteArray
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const hexStringToNumber = (hexBalance: string): number => {
|
|
61
|
+
const balanceNumber = Number(hexBalance)
|
|
62
|
+
console.log(`balanceNumber`, balanceNumber)
|
|
63
|
+
const balance: number = Math.round((balanceNumber / 1e18) * 1000000) / 1000000
|
|
64
|
+
|
|
65
|
+
return balance
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const parseRequest = async (
|
|
69
|
+
method: string,
|
|
70
|
+
params: any,
|
|
71
|
+
): Promise<OutgoingRequest> => {
|
|
72
|
+
let requestData: OutgoingRequestData
|
|
73
|
+
|
|
74
|
+
console.log(`Request: `, JSON.stringify({ method, params }))
|
|
75
|
+
|
|
76
|
+
const txParam0 = Array.isArray(params) ? params[0] : params
|
|
77
|
+
const txParam1 = Array.isArray(params) ? params[1] : null
|
|
78
|
+
|
|
79
|
+
switch (method) {
|
|
80
|
+
case 'eth_sendTransaction':
|
|
81
|
+
case 'eth_signTransaction':
|
|
82
|
+
const payload = txParam0
|
|
83
|
+
const value = hexStringToNumber(payload.value)
|
|
84
|
+
|
|
85
|
+
requestData = {
|
|
86
|
+
data: payload.data,
|
|
87
|
+
from: payload.from,
|
|
88
|
+
to: payload.to,
|
|
89
|
+
value,
|
|
90
|
+
valueConverted: await convert(value),
|
|
91
|
+
} as SignableTransactionData
|
|
92
|
+
|
|
93
|
+
console.log(`requestData: `, requestData)
|
|
94
|
+
break
|
|
95
|
+
case 'eth_sign':
|
|
96
|
+
case 'personal_sign':
|
|
97
|
+
requestData = {
|
|
98
|
+
message: hexStringToString(txParam0),
|
|
99
|
+
} as MessageData
|
|
100
|
+
break
|
|
101
|
+
default:
|
|
102
|
+
break
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
method,
|
|
107
|
+
data: requestData,
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type HttpRequestOptions,
|
|
3
|
+
type ValidHttpRequestMethods,
|
|
4
|
+
} from '../../types'
|
|
5
|
+
import { HttpError } from '../errors'
|
|
6
|
+
|
|
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
|
|
13
|
+
|
|
14
|
+
constructor({ body = {}, headers = {}, method, url }: HttpRequestOptions) {
|
|
15
|
+
this.body = body
|
|
16
|
+
this.headers = headers
|
|
17
|
+
this.method = method
|
|
18
|
+
this.url = url
|
|
19
|
+
|
|
20
|
+
this.request = new XMLHttpRequest()
|
|
21
|
+
this.request.open(this.method, this.url, true)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public send<T>(): Promise<T> {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
// Sets headers on the request
|
|
27
|
+
this.prepareRequest()
|
|
28
|
+
|
|
29
|
+
// Sends the request with a formatted request body
|
|
30
|
+
const body = this.prepareBody()
|
|
31
|
+
this.request.send(body)
|
|
32
|
+
|
|
33
|
+
// Adds an event listener for when the request state changes
|
|
34
|
+
this.request.onreadystatechange = () => {
|
|
35
|
+
if (this.request.readyState === 4) {
|
|
36
|
+
// readyState 4 means the request has completed
|
|
37
|
+
if (this.request.status !== 200) {
|
|
38
|
+
// If we didn't get an 'OK' status, error out
|
|
39
|
+
reject(
|
|
40
|
+
new HttpError({
|
|
41
|
+
message: this.request.responseText,
|
|
42
|
+
status: this.request.status,
|
|
43
|
+
statusText: this.request.statusText,
|
|
44
|
+
}),
|
|
45
|
+
)
|
|
46
|
+
} else {
|
|
47
|
+
// Otherwise, resolve a parsed response object
|
|
48
|
+
resolve(this.buildResponse<T>())
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private buildResponse<T>(): T {
|
|
56
|
+
try {
|
|
57
|
+
// Try parsing the response body into a JSON object
|
|
58
|
+
const response = JSON.parse(this.request.response) as T
|
|
59
|
+
return response
|
|
60
|
+
} catch {
|
|
61
|
+
// If JSON parsing doesn't work, just return the response body however it came back
|
|
62
|
+
return this.request.response
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private prepareBody(): string {
|
|
67
|
+
if (this.headers['Content-Type'] === 'application/json') {
|
|
68
|
+
// Stringify if we're declaring this request to be a JSON request
|
|
69
|
+
return JSON.stringify(this.body)
|
|
70
|
+
} else {
|
|
71
|
+
// Otherwise, build a serialized string
|
|
72
|
+
const params = []
|
|
73
|
+
|
|
74
|
+
for (const [key, value] of Object.entries(this.body)) {
|
|
75
|
+
params.push(`${key}=${value}`)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return params.join('&')
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private prepareRequest(): void {
|
|
83
|
+
// Add all of the defined headers to the request
|
|
84
|
+
for (const [key, value] of Object.entries(this.headers)) {
|
|
85
|
+
this.request.setRequestHeader(key, value)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default HttpRequest
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export * as currency from './currency'
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
HttpError,
|
|
5
|
+
InvalidApiKeyError,
|
|
6
|
+
InvalidChainIdError,
|
|
7
|
+
InvalidGatewayConfigError,
|
|
8
|
+
MissingOptionError,
|
|
9
|
+
MpcSigningError,
|
|
10
|
+
NoAddressError,
|
|
11
|
+
ProviderRpcError,
|
|
12
|
+
RpcErrorCodes,
|
|
13
|
+
} from './errors'
|
|
14
|
+
|
|
15
|
+
export { parseRequest } from './eth-requests'
|
|
16
|
+
|
|
17
|
+
export { default as HttpRequest } from './http-request'
|
|
18
|
+
|
|
19
|
+
export { HttpRequester } from './requesters'
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
type HttpOptions,
|
|
23
|
+
type HttpRequesterOptions,
|
|
24
|
+
type HttpRequestOptions,
|
|
25
|
+
} from '../types'
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { HttpRequest } from '../index'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type HttpOptions,
|
|
5
|
+
type HttpRequestOptions,
|
|
6
|
+
type HttpRequesterOptions,
|
|
7
|
+
} from '../../types'
|
|
8
|
+
|
|
9
|
+
class HttpRequester {
|
|
10
|
+
private baseUrl: string
|
|
11
|
+
|
|
12
|
+
constructor({ baseUrl }: HttpRequesterOptions) {
|
|
13
|
+
this.baseUrl = baseUrl.startsWith('https://')
|
|
14
|
+
? baseUrl
|
|
15
|
+
: `https://${baseUrl}`
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public async get<T>(path: string, options?: HttpOptions): Promise<T> {
|
|
19
|
+
const requestOptions = {
|
|
20
|
+
method: 'GET',
|
|
21
|
+
url: `${this.baseUrl}${path}`,
|
|
22
|
+
} as HttpRequestOptions
|
|
23
|
+
|
|
24
|
+
if (options && options.headers) {
|
|
25
|
+
requestOptions.headers = this.buildHeaders(options.headers)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const request = new HttpRequest(requestOptions)
|
|
29
|
+
const response = (await request.send()) as T
|
|
30
|
+
|
|
31
|
+
return response
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public async post<T>(path: string, options?: HttpOptions): Promise<T> {
|
|
35
|
+
const requestOptions = {
|
|
36
|
+
method: 'POST',
|
|
37
|
+
url: `${this.baseUrl}${path}`,
|
|
38
|
+
} as HttpRequestOptions
|
|
39
|
+
|
|
40
|
+
requestOptions.headers = this.buildHeaders(
|
|
41
|
+
options && options.headers ? options.headers : {},
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
if (options && options.body) {
|
|
45
|
+
requestOptions.body = options.body
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const request = new HttpRequest(requestOptions)
|
|
49
|
+
const response = (await request.send()) as T
|
|
50
|
+
|
|
51
|
+
return response
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private buildHeaders(headers: Record<string, any>): Record<string, any> {
|
|
55
|
+
return {
|
|
56
|
+
'Content-Type': 'application/json',
|
|
57
|
+
...headers,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default HttpRequester
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as HttpRequester } from './http'
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type OutgoingRequestData = MessageData | SignableTransactionData
|
|
2
|
+
|
|
3
|
+
export type ValidHttpRequestMethods = 'DELETE' | 'GET' | 'POST' | 'PUT'
|
|
4
|
+
|
|
5
|
+
export type ValidRpcErrorCodes = 4001 | 4100 | 4200 | 4900 | 4901
|
|
6
|
+
|
|
7
|
+
export interface HttpErrorOptions {
|
|
8
|
+
message: string
|
|
9
|
+
status: number
|
|
10
|
+
statusText: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HttpOptions {
|
|
14
|
+
body?: Record<string, any>
|
|
15
|
+
headers?: Record<string, any>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface HttpRequesterOptions extends RequesterOptions {
|
|
19
|
+
baseUrl: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface HttpRequestOptions {
|
|
23
|
+
body?: Record<string, any>
|
|
24
|
+
headers?: Record<string, any>
|
|
25
|
+
method: ValidHttpRequestMethods
|
|
26
|
+
url: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface MessageData {
|
|
30
|
+
message: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface MissingOptionErrorOptions {
|
|
34
|
+
className: string
|
|
35
|
+
option: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface OutgoingRequest {
|
|
39
|
+
method: string
|
|
40
|
+
data: OutgoingRequestData
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface RequesterOptions {}
|
|
44
|
+
|
|
45
|
+
export interface RpcErrorDefinition {
|
|
46
|
+
description: string
|
|
47
|
+
name: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface RpcErrorOptions {
|
|
51
|
+
code: ValidProviderRpcErrorCodes
|
|
52
|
+
data?: unknown
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface SignableTransactionData {
|
|
56
|
+
data: string
|
|
57
|
+
from: string
|
|
58
|
+
to: string
|
|
59
|
+
value: number
|
|
60
|
+
valueConverted: number
|
|
61
|
+
}
|