MyCrypto/common/libs/nodes/web3/index.ts

56 lines
1.5 KiB
TypeScript

import { IHexStrWeb3Transaction } from 'libs/transaction';
import RPCNode from '../rpc';
import Web3Client from './client';
import Web3Requests from './requests';
import { INode } from 'libs/nodes/INode';
import {
isValidSendTransaction,
isValidSignMessage,
isValidGetAccounts,
isValidGetNetVersion
} from '../../validators';
export default class Web3Node extends RPCNode {
public client: Web3Client;
public requests: Web3Requests;
constructor() {
super('web3'); // initialized with fake endpoint
this.client = new Web3Client();
this.requests = new Web3Requests();
}
public getNetVersion(): Promise<string> {
return this.client
.call(this.requests.getNetVersion())
.then(isValidGetNetVersion)
.then(({ result }) => result);
}
public sendTransaction(web3Tx: IHexStrWeb3Transaction): Promise<string> {
return this.client
.call(this.requests.sendTransaction(web3Tx))
.then(isValidSendTransaction)
.then(({ result }) => result);
}
public signMessage(msgHex: string, fromAddr: string): Promise<string> {
return this.client
.call(this.requests.signMessage(msgHex, fromAddr))
.then(isValidSignMessage)
.then(({ result }) => result);
}
public getAccounts(): Promise<string> {
return this.client
.call(this.requests.getAccounts())
.then(isValidGetAccounts)
.then(({ result }) => result);
}
}
export function isWeb3Node(nodeLib: INode | Web3Node): nodeLib is Web3Node {
return nodeLib instanceof Web3Node;
}