web3-core#AbstractProvider TypeScript Examples
The following examples show how to use
web3-core#AbstractProvider.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: signer.ts From v3-client with Apache License 2.0 | 6 votes |
/**
* Sign a message with `personal_sign`.
*/
protected async ethSignPersonalInternal(
signer: string,
message: string,
): Promise<string> {
let provider = this.web3.currentProvider;
if (provider === null) {
throw new Error('Cannot sign since Web3 currentProvider is null');
}
if (typeof provider === 'string') {
throw new Error('Cannot sign since Web3 currentProvider is a string');
}
provider = provider as AbstractProvider;
const sendAsync: (param: JsonRpcPayload) => Promise<JsonRpcResponse> = (
promisify(provider.sendAsync || provider.send).bind(provider)
);
const rpcMethod = 'personal_sign';
const response = await sendAsync({
method: rpcMethod,
params: [signer, message],
jsonrpc: '2.0',
id: Date.now(),
});
if (response.error) {
throw new Error((response.error as unknown as { message: string }).message);
}
// Note: Using createTypedSignature() fixes the signature `v` value.
return createTypedSignature(response.result, SignatureTypes.PERSONAL);
}
Example #2
Source File: signer.ts From v3-client with Apache License 2.0 | 5 votes |
protected async ethSignTypedDataInternal(
signer: string,
data: {},
signingMethod: SigningMethod,
): Promise<string> {
let rpcMethod: string;
let rpcData: {};
let provider = this.web3.currentProvider;
if (provider === null) {
throw new Error('Cannot sign since Web3 currentProvider is null');
}
if (typeof provider === 'string') {
throw new Error('Cannot sign since Web3 currentProvider is a string');
}
provider = provider as AbstractProvider;
let sendAsync: (param: JsonRpcPayload) => Promise<JsonRpcResponse>;
switch (signingMethod) {
case SigningMethod.TypedData:
sendAsync = promisify(provider.send!).bind(provider);
rpcMethod = 'eth_signTypedData';
rpcData = data;
break;
case SigningMethod.MetaMask:
sendAsync = promisify(provider.sendAsync).bind(provider);
rpcMethod = 'eth_signTypedData_v3';
rpcData = JSON.stringify(data);
break;
case SigningMethod.MetaMaskLatest:
sendAsync = promisify(provider.sendAsync).bind(provider);
rpcMethod = 'eth_signTypedData_v4';
rpcData = JSON.stringify(data);
break;
case SigningMethod.CoinbaseWallet:
sendAsync = promisify(provider.sendAsync).bind(provider);
rpcMethod = 'eth_signTypedData_v4';
rpcData = data;
break;
default:
throw new Error(`Invalid signing method ${signingMethod}`);
}
const response = await sendAsync({
method: rpcMethod,
params: [signer, rpcData],
jsonrpc: '2.0',
id: Date.now(),
});
if (response.error) {
throw new Error((response.error as unknown as { message: string }).message);
}
return `0x${stripHexPrefix(response.result)}0${SignatureTypes.NO_PREPEND}`;
}
Example #3
Source File: essentialsweb3provider.ts From Elastos.Essentials.App with MIT License | 5 votes |
// Concurrency: 1
export class EssentialsWeb3Provider implements AbstractProvider {
constructor(private rpcApiUrl: string) {
}
private callJsonRPC(payload): Promise<any> {
// eslint-disable-next-line @typescript-eslint/no-misused-promises, no-async-promise-executor
return callJsonRPCQueue.add(() => {
return new Promise((resolve, reject) => {
var request = new XMLHttpRequest();
request.open('POST', this.rpcApiUrl, true);
request.setRequestHeader('Content-Type', 'application/json');
request.timeout = 5000;
request.onreadystatechange = function () {
if (request.readyState === 4 && request.timeout !== 1) {
var result = request.responseText;
try {
//Logger.log("global", "Ethereum JSON RPC call result:", result, "for payload:", payload);
result = JSON.parse(result);
resolve(result);
} catch (e) {
Logger.error("global", "Ethereum response: JSON parse error");
reject("Invalid JSON response returned by the JSON RPC: " + e);
}
}
};
request.ontimeout = function () {
reject("Timeout");
};
request.onerror = function (error) {
console.error("RPC call error");
reject(error);
}
try {
request.send(JSON.stringify(payload));
} catch (error) {
reject("Connection error");
}
});
});
}
// Mandatory method: sendAsync()
async sendAsync(payload: JsonRpcPayload, callback: (error: Error, result?: JsonRpcResponse) => void) {
//Logger.log("global", "Essentials Web3 provider sendAsync payload", payload);
switch (payload.method) {
// All methods not handled above are sent through JSON RPC API to the user-defined node url.
default:
try {
let result = await this.callJsonRPC(payload);
callback(null, result);
}
catch (e) {
Logger.error("global", "callJsonRPC catched");
callback(e);
}
}
}
}
Example #4
Source File: EIP712Signer.ts From multisig-react with MIT License | 5 votes |
getEIP712Signer =
(version?: string) =>
async (txArgs): Promise<string> => {
const web3 = getWeb3()
const typedData = await generateTypedDataFrom(txArgs)
let method = 'eth_signTypedData_v3'
if (version === 'v4') {
method = 'eth_signTypedData_v4'
}
if (!version) {
method = 'eth_signTypedData'
}
const jsonTypedData = JSON.stringify(typedData)
const signedTypedData = {
jsonrpc: '2.0',
method,
params: version === 'v3' || version === 'v4' ? [txArgs.sender, jsonTypedData] : [jsonTypedData, txArgs.sender],
from: txArgs.sender,
id: new Date().getTime(),
}
return new Promise((resolve, reject) => {
const provider = web3.currentProvider as AbstractProvider
provider.sendAsync(signedTypedData, (err, signature) => {
if (err) {
reject(err)
return
}
if (signature?.result == null) {
reject(new Error(EIP712_NOT_SUPPORTED_ERROR_MSG))
return
}
const sig = adjustV('eth_signTypedData', signature.result)
resolve(sig.replace(EMPTY_DATA, ''))
})
})
}
Example #5
Source File: sdk.ts From magic-js with MIT License | 5 votes |
/**
* Contains a Web3-compliant provider. Pass this module to your Web3/Ethers
* instance for automatic compatibility with Ethereum methods.
*/
public readonly rpcProvider: RPCProviderModule & AbstractProvider;