mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-19 15:42:41 +00:00
c0cd668c64
* Layed out components for custom nodes. * Outline of custom nodes. Still missing various features and error handling. * Persist custom nodes to local storage. * Make custom nodes removable. * Add latest block functions, call it when switching nodes. * Initialize correct node, move node utils into utils file. * Fix names * Send headers along with rpc requests. * Remove custom network options for now. * PR feedback. * One last log. * Fix tests. * Headers in batch too. * Switch to node when you add it. * Reduce hackery. * Clean up linter and tsc. * Fix latest block hex conversion. * Unit tests. * Fix missing property. * Fix Modal title typing.
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { randomBytes } from 'crypto';
|
|
import { JsonRpcResponse, RPCRequest } from './types';
|
|
|
|
export default class RPCClient {
|
|
public endpoint: string;
|
|
public headers: object;
|
|
constructor(endpoint: string, headers: object = {}) {
|
|
this.endpoint = endpoint;
|
|
this.headers = headers;
|
|
}
|
|
|
|
public id(): string {
|
|
return randomBytes(16).toString('hex');
|
|
}
|
|
|
|
public decorateRequest = (req: RPCRequest) => ({
|
|
...req,
|
|
id: this.id(),
|
|
jsonrpc: '2.0'
|
|
});
|
|
|
|
public call = (request: RPCRequest | any): Promise<JsonRpcResponse> => {
|
|
return fetch(this.endpoint, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...this.headers,
|
|
},
|
|
body: JSON.stringify(this.decorateRequest(request))
|
|
}).then(r => r.json());
|
|
};
|
|
|
|
public batch = (requests: RPCRequest[] | any): Promise<JsonRpcResponse[]> => {
|
|
return fetch(this.endpoint, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...this.headers,
|
|
},
|
|
body: JSON.stringify(requests.map(this.decorateRequest))
|
|
}).then(r => r.json());
|
|
};
|
|
}
|