Socket
Socket
Sign inDemoInstall

web3-core-method

Package Overview
Dependencies
51
Maintainers
2
Versions
137
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0-beta.46 to 1.0.0-beta.47

330

dist/web3-core-method.esm.js

@@ -1,2 +0,1 @@

import { WebsocketProvider, IpcProvider, EthereumProvider } from 'web3-providers';
import EventEmitter from 'eventemitter3';

@@ -9,3 +8,2 @@ import isString from 'lodash/isString';

import * as Utils from 'web3-utils';
import isObject from 'lodash/isObject';
import isFunction from 'lodash/isFunction';

@@ -25,3 +23,3 @@

if (receipt && receipt.blockHash) {
const validationResult = this.transactionReceiptValidator.validate(receipt, method.parameters);
const validationResult = this.transactionReceiptValidator.validate(receipt, method);
if (validationResult === true) {

@@ -39,3 +37,3 @@ this.handleSuccessState(receipt, method, promiEvent);

if (receipt && receipt.blockHash) {
const validationResult = this.transactionReceiptValidator.validate(receipt, method.parameters);
const validationResult = this.transactionReceiptValidator.validate(receipt, method);
if (validationResult === true) {

@@ -107,5 +105,5 @@ this.confirmationsCounter++;

class TransactionReceiptValidator {
validate(receipt, methodParameters) {
validate(receipt, method) {
const receiptJSON = JSON.stringify(receipt, null, 2);
if (!this.isValidGasUsage(receipt, methodParameters)) {
if (!this.isValidGasUsage(receipt, method)) {
return new Error(`Transaction ran out of gas. Please provide more gas:\n${receiptJSON}`);

@@ -119,10 +117,6 @@ }

isValidReceiptStatus(receipt) {
return receipt.status === true || receipt.status === '0x1' || typeof receipt.status === 'undefined';
return receipt.status === true || typeof receipt.status === 'undefined' || receipt.status === null;
}
isValidGasUsage(receipt, methodParameters) {
let gasProvided = null;
if (isObject(methodParameters[0]) && methodParameters[0].gas) {
gasProvided = methodParameters[0].gas;
}
return !receipt.outOfGas && (!gasProvided || gasProvided !== receipt.gasUsed);
isValidGasUsage(receipt, method) {
return !receipt.outOfGas && method.utils.hexToNumber(method.parameters[0].gas) !== receipt.gasUsed;
}

@@ -140,3 +134,3 @@ }

watch(moduleInstance) {
if (this.isSocketConnection(moduleInstance.currentProvider)) {
if (moduleInstance.currentProvider.constructor.name !== 'HttpProvider') {
this.confirmationSubscription = this.subscriptionsFactory.createNewHeadsSubscription(moduleInstance).subscribe(() => {

@@ -162,5 +156,2 @@ this.emit('newHead');

}
isSocketConnection(provider) {
return provider instanceof WebsocketProvider || provider instanceof IpcProvider || provider instanceof EthereumProvider;
}
}

@@ -197,46 +188,2 @@

class AbstractSigner {
constructor(accounts) {
this.accounts = accounts;
}
getWallet(from) {
const account = this.accounts.wallet[from];
if (account) {
return account;
}
return null;
}
}
class MessageSigner extends AbstractSigner {
constructor(accounts) {
super(accounts);
}
sign(data, address) {
const wallet = this.getWallet(address);
if (wallet && wallet.privateKey) {
return this.accounts.sign(data, wallet.privateKey).signature;
}
throw new Error('Wallet or privateKey in wallet is not set!');
}
}
class TransactionSigner extends AbstractSigner {
constructor(accounts) {
super(accounts);
}
async sign(transaction) {
const wallet = this.getWallet(transaction.from);
if (wallet && wallet.privateKey) {
delete transaction.from;
try {
return await this.accounts.signTransaction(transaction, wallet.privateKey);
} catch (error) {
throw error;
}
}
throw new Error('Wallet or privateKey in wallet is not set!');
}
}
class AbstractMethod {

@@ -302,5 +249,2 @@ constructor(rpcMethod, parametersAmount, utils, formatters$$1) {

}
hasWallets() {
return this.accounts && this.accounts.wallet.length > 0;
}
}

@@ -321,8 +265,10 @@

try {
const response = await moduleInstance.currentProvider.send(this.rpcMethod, this.parameters);
const mappedResponse = this.afterExecution(response);
let response = await moduleInstance.currentProvider.send(this.rpcMethod, this.parameters);
if (response) {
response = this.afterExecution(response);
}
if (this.callback) {
this.callback(false, mappedResponse);
this.callback(false, response);
}
return mappedResponse;
return response;
} catch (error) {

@@ -343,3 +289,3 @@ if (this.callback) {

if (response !== null) {
return this.formatters.outputTransactionFormatter(response);
return this.formatters.outputTransactionReceiptFormatter(response);
}

@@ -350,5 +296,41 @@ return response;

class AbstractSendMethod extends AbstractMethod {
constructor(rpcMethod, parametersAmount, utils, formatters$$1, transactionConfirmationWorkflow) {
super(rpcMethod, parametersAmount, utils, formatters$$1);
this.transactionConfirmationWorkflow = transactionConfirmationWorkflow;
}
static get Type() {
return 'SEND';
}
execute(moduleInstance, promiEvent) {
this.beforeExecution(moduleInstance);
if (this.parameters.length !== this.parametersAmount) {
throw new Error(`Invalid Arguments length: expected: ${this.parametersAmount}, given: ${this.parameters.length}`);
}
moduleInstance.currentProvider.send(this.rpcMethod, this.parameters).then(response => {
this.transactionConfirmationWorkflow.execute(this, moduleInstance, response, promiEvent);
if (this.callback) {
this.callback(false, response);
}
promiEvent.emit('transactionHash', response);
}).catch(error => {
if (this.callback) {
this.callback(error, null);
}
promiEvent.reject(error);
promiEvent.emit('error', error);
promiEvent.removeAllListeners();
});
return promiEvent;
}
}
class SendRawTransactionMethod extends AbstractSendMethod {
constructor(utils, formatters$$1, transactionConfirmationWorkflow) {
super('eth_sendRawTransaction', 1, utils, formatters$$1, transactionConfirmationWorkflow);
}
}
class ModuleFactory {
constructor(accounts, subscriptionsFactory, utils, formatters$$1) {
this.accounts = accounts || {};
constructor(subscriptionsFactory, utils, formatters$$1) {
this.subscriptionsFactory = subscriptionsFactory;

@@ -361,8 +343,2 @@ this.formatters = formatters$$1;

}
createTransactionSigner() {
return new TransactionSigner(this.accounts);
}
createMessageSigner() {
return new MessageSigner(this.accounts);
}
createTransactionConfirmationWorkflow() {

@@ -377,4 +353,33 @@ return new TransactionConfirmationWorkflow(this.createTransactionReceiptValidator(), this.createNewHeadsWatcher(), new GetTransactionReceiptMethod(this.utils, this.formatters));

}
createSendRawTransactionMethod() {
return new SendRawTransactionMethod(this.utils, this.formatters, this.createTransactionConfirmationWorkflow());
}
}
class GetTransactionCountMethod extends AbstractCallMethod {
constructor(utils, formatters$$1) {
super('eth_getTransactionCount', 2, utils, formatters$$1);
}
beforeExecution(moduleInstance) {
this.parameters[0] = this.formatters.inputAddressFormatter(this.parameters[0]);
if (isFunction(this.parameters[1])) {
this.callback = this.parameters[1];
this.parameters[1] = moduleInstance.defaultBlock;
}
this.parameters[1] = this.formatters.inputDefaultBlockNumberFormatter(this.parameters[1], moduleInstance);
}
afterExecution(response) {
return this.utils.hexToNumber(response);
}
}
class ChainIdMethod extends AbstractCallMethod {
constructor(utils, formatters$$1) {
super('eth_chainId', 0, utils, formatters$$1);
}
afterExecution(response) {
return this.utils.hexToNumber(response);
}
}
class AbstractMethodFactory {

@@ -403,9 +408,7 @@ constructor(methodModuleFactory, utils, formatters$$1) {

case 'CALL':
if (method.name === 'SignMethod') {
return new method(this.utils, this.formatters, this.methodModuleFactory.accounts, this.methodModuleFactory.createMessageSigner());
}
return new method(this.utils, this.formatters);
case 'SEND':
if (method.name === 'SendTransactionMethod') {
return new method(this.utils, this.formatters, this.methodModuleFactory.createTransactionConfirmationWorkflow(), this.methodModuleFactory.accounts, this.methodModuleFactory.createTransactionSigner());
const transactionConfirmationWorkflow = this.methodModuleFactory.createTransactionConfirmationWorkflow();
return new method(this.utils, this.formatters, transactionConfirmationWorkflow, new SendRawTransactionMethod(this.utils, this.formatters, transactionConfirmationWorkflow), new ChainIdMethod(this.utils, this.formatters), new GetTransactionCountMethod(this.utils, this.formatters));
}

@@ -535,19 +538,2 @@ return new method(this.utils, this.formatters, this.methodModuleFactory.createTransactionConfirmationWorkflow());

class GetTransactionCountMethod extends AbstractCallMethod {
constructor(utils, formatters$$1) {
super('eth_getTransactionCount', 2, utils, formatters$$1);
}
beforeExecution(moduleInstance) {
this.parameters[0] = this.formatters.inputAddressFormatter(this.parameters[0]);
if (isFunction(this.parameters[1])) {
this.callback = this.parameters[1];
this.parameters[1] = moduleInstance.defaultBlock;
}
this.parameters[1] = this.formatters.inputDefaultBlockNumberFormatter(this.parameters[1], moduleInstance);
}
afterExecution(response) {
return this.utils.hexToNumber(response);
}
}
class RequestAccountsMethod extends AbstractCallMethod {

@@ -607,7 +593,7 @@ constructor() {

constructor(utils, formatters$$1) {
super('eth_getTransactionByBlockNumberAndIndex', 1, utils, formatters$$1);
super('eth_getBlockTransactionCountByNumber', 1, utils, formatters$$1);
}
beforeExecution(moduleInstance) {
if (this.isHash(this.parameters[0])) {
this.rpcMethod = 'eth_getTransactionByBlockHashAndIndex';
this.rpcMethod = 'eth_getBlockTransactionCountByHash';
}

@@ -661,39 +647,2 @@ this.parameters[0] = this.formatters.inputBlockNumberFormatter(this.parameters[0]);

class AbstractSendMethod extends AbstractMethod {
constructor(rpcMethod, parametersAmount, utils, formatters$$1, transactionConfirmationWorkflow) {
super(rpcMethod, parametersAmount, utils, formatters$$1);
this.transactionConfirmationWorkflow = transactionConfirmationWorkflow;
}
static get Type() {
return 'SEND';
}
execute(moduleInstance, promiEvent) {
this.beforeExecution(moduleInstance);
if (this.parameters.length !== this.parametersAmount) {
throw new Error(`Invalid Arguments length: expected: ${this.parametersAmount}, given: ${this.parameters.length}`);
}
moduleInstance.currentProvider.send(this.rpcMethod, this.parameters).then(response => {
this.transactionConfirmationWorkflow.execute(this, moduleInstance, response, promiEvent);
if (this.callback) {
this.callback(false, response);
}
promiEvent.emit('transactionHash', response);
}).catch(error => {
if (this.callback) {
this.callback(error, null);
}
promiEvent.reject(error);
promiEvent.emit('error', error);
promiEvent.removeAllListeners();
});
return promiEvent;
}
}
class SendRawTransactionMethod extends AbstractSendMethod {
constructor(utils, formatters$$1, transactionConfirmationWorkflow) {
super('eth_sendRawTransaction', 1, utils, formatters$$1, transactionConfirmationWorkflow);
}
}
class SignTransactionMethod extends AbstractCallMethod {

@@ -709,6 +658,7 @@ constructor(utils, formatters$$1) {

class SendTransactionMethod extends AbstractSendMethod {
constructor(utils, formatters$$1, transactionConfirmationWorkflow, accounts, transactionSigner) {
constructor(utils, formatters$$1, transactionConfirmationWorkflow, sendRawTransactionMethod, chainIdMethod, getTransactionCountMethod) {
super('eth_sendTransaction', 1, utils, formatters$$1, transactionConfirmationWorkflow);
this.accounts = accounts;
this.transactionSigner = transactionSigner;
this.sendRawTransactionMethod = sendRawTransactionMethod;
this.chainIdMethod = chainIdMethod;
this.getTransactionCountMethod = getTransactionCountMethod;
}

@@ -719,23 +669,30 @@ beforeExecution(moduleInstance) {

execute(moduleInstance, promiEvent) {
if (!this.isGasLimitDefined()) {
if (this.hasDefaultGasLimit(moduleInstance)) {
this.parameters[0]['gas'] = moduleInstance.defaultGas;
if (!this.parameters[0].gas && moduleInstance.defaultGas) {
this.parameters[0]['gas'] = moduleInstance.defaultGas;
}
if (!this.parameters[0].gasPrice) {
if (!moduleInstance.defaultGasPrice) {
moduleInstance.currentProvider.send('eth_gasPrice', []).then(gasPrice => {
this.parameters[0].gasPrice = gasPrice;
this.execute(moduleInstance, promiEvent);
});
return promiEvent;
}
}
if (!this.isGasPriceDefined() && this.hasDefaultGasPrice(moduleInstance)) {
this.parameters[0]['gasPrice'] = moduleInstance.defaultGasPrice;
}
if (!this.isGasPriceDefined() && !this.hasDefaultGasPrice(moduleInstance)) {
moduleInstance.currentProvider.send('eth_gasPrice', []).then(gasPrice => {
this.parameters[0]['gasPrice'] = gasPrice;
this.execute(moduleInstance, promiEvent);
});
return promiEvent;
if (this.hasAccounts(moduleInstance) && this.isDefaultSigner(moduleInstance)) {
if (moduleInstance.accounts.wallet[this.parameters[0].from]) {
this.sendRawTransaction(moduleInstance.accounts.wallet[this.parameters[0].from].privateKey, promiEvent, moduleInstance).catch(error => {
if (this.callback) {
this.callback(error, null);
}
promiEvent.reject(error);
promiEvent.emit('error', error);
promiEvent.removeAllListeners();
});
return promiEvent;
}
}
if (this.hasWallets()) {
this.rpcMethod = 'eth_sendRawTransaction';
this.transactionSigner.sign(this.parameters[0]).then(response => {
this.parameters = [response.rawTransaction];
super.execute(moduleInstance, promiEvent);
}).catch(error => {
if (this.hasCustomSigner(moduleInstance)) {
this.sendRawTransaction(null, promiEvent, moduleInstance).catch(error => {
if (this.callback) {

@@ -753,13 +710,23 @@ this.callback(error, null);

}
hasDefaultGasPrice(moduleInstance) {
return moduleInstance.defaultGasPrice !== null && typeof moduleInstance.defaultGasPrice !== 'undefined';
async sendRawTransaction(privateKey, promiEvent, moduleInstance) {
if (!this.parameters[0].chainId) {
this.parameters[0].chainId = await this.chainIdMethod.execute(moduleInstance);
}
if (!this.parameters[0].nonce && this.parameters[0].nonce !== 0) {
this.getTransactionCountMethod.parameters = [this.parameters[0].from];
this.parameters[0].nonce = await this.getTransactionCountMethod.execute(moduleInstance);
}
const response = await moduleInstance.transactionSigner.sign(this.parameters[0], privateKey);
this.sendRawTransactionMethod.parameters = [response.rawTransaction];
this.sendRawTransactionMethod.callback = this.callback;
this.sendRawTransactionMethod.execute(moduleInstance, promiEvent);
}
isGasPriceDefined() {
return isObject(this.parameters[0]) && typeof this.parameters[0].gasPrice !== 'undefined';
isDefaultSigner(moduleInstance) {
return moduleInstance.transactionSigner.constructor.name === 'TransactionSigner';
}
hasDefaultGasLimit(moduleInstance) {
return moduleInstance.defaultGas !== null && typeof moduleInstance.defaultGas !== 'undefined';
hasAccounts(moduleInstance) {
return moduleInstance.accounts && moduleInstance.accounts.accountsIndex > 0;
}
isGasLimitDefined() {
return isObject(this.parameters[0]) && typeof this.parameters[0].gas !== 'undefined';
hasCustomSigner(moduleInstance) {
return moduleInstance.transactionSigner.constructor.name !== 'TransactionSigner';
}

@@ -783,18 +750,19 @@ }

class SignMethod extends AbstractCallMethod {
constructor(utils, formatters$$1, accounts, messageSigner) {
constructor(utils, formatters$$1) {
super('eth_sign', 2, utils, formatters$$1);
this.accounts = accounts;
this.messageSigner = messageSigner;
}
execute(moduleInstance) {
if (this.hasWallets()) {
this.beforeExecution(moduleInstance);
return this.signOnClient();
if (this.hasAccount(moduleInstance)) {
return this.signLocally(moduleInstance);
}
return super.execute(moduleInstance);
}
signOnClient() {
let signedMessage;
async signLocally(moduleInstance) {
try {
signedMessage = this.afterExecution(this.messageSigner.sign(this.parameters[0], this.parameters[1]));
this.beforeExecution(moduleInstance);
let signedMessage = moduleInstance.accounts.sign(this.parameters[0], moduleInstance.accounts.wallet[this.parameters[1]].address);
if (this.callback) {
this.callback(false, signedMessage);
}
return signedMessage;
} catch (error) {

@@ -806,6 +774,2 @@ if (this.callback) {

}
if (this.callback) {
this.callback(false, signedMessage);
}
return Promise.resolve(signedMessage);
}

@@ -816,2 +780,5 @@ beforeExecution(moduleInstance) {

}
hasAccount(moduleInstance) {
return moduleInstance.accounts && moduleInstance.accounts.accountsIndex > 0 && moduleInstance.accounts.wallet[this.parameters[1]];
}
}

@@ -880,3 +847,2 @@

this.parameters[0] = this.formatters.inputSignFormatter(this.parameters[0]);
this.parameters[1] = this.formatters.inputAddressFormatter(this.parameters[1]);
}

@@ -1083,6 +1049,6 @@ }

const MethodModuleFactory = accounts => {
return new ModuleFactory(accounts, new SubscriptionsFactory(), Utils, formatters);
const MethodModuleFactory = () => {
return new ModuleFactory(new SubscriptionsFactory(), Utils, formatters);
};
export { MethodModuleFactory, AbstractMethod, AbstractMethodFactory, GetProtocolVersionMethod, VersionMethod, ListeningMethod, PeerCountMethod, GetNodeInfoMethod, GetCoinbaseMethod, IsMiningMethod, GetHashrateMethod, IsSyncingMethod, GetGasPriceMethod, SubmitWorkMethod, GetWorkMethod, GetAccountsMethod, GetBalanceMethod, GetTransactionCountMethod, RequestAccountsMethod, GetBlockNumberMethod, GetBlockMethod, GetUncleMethod, GetBlockTransactionCountMethod, GetBlockUncleCountMethod, GetTransactionMethod, GetTransactionFromBlockMethod, GetTransactionReceiptMethod as GetTransactionReceipt, SendRawTransactionMethod, SignTransactionMethod, SendTransactionMethod, GetCodeMethod, SignMethod, CallMethod, GetStorageAtMethod, EstimateGasMethod, GetPastLogsMethod, EcRecoverMethod, ImportRawKeyMethod, ListAccountsMethod, LockAccountMethod, NewAccountMethod, PersonalSendTransactionMethod, PersonalSignMethod, PersonalSignTransactionMethod, UnlockAccountMethod, AddPrivateKeyMethod, AddSymKeyMethod, DeleteKeyPairMethod, DeleteMessageFilterMethod, DeleteSymKeyMethod, GenerateSymKeyFromPasswordMethod, GetFilterMessagesMethod, GetInfoMethod, GetPrivateKeyMethod, GetPublicKeyMethod, GetSymKeyMethod, HasKeyPairMethod, HasSymKeyMethod, MarkTrustedPeerMethod, NewKeyPairMethod, NewMessageFilterMethod, NewSymKeyMethod, PostMethod, SetMaxMessageSizeMethod, SetMinPoWMethod, ShhVersionMethod };
export { MethodModuleFactory, AbstractMethod, AbstractMethodFactory, GetProtocolVersionMethod, VersionMethod, ListeningMethod, PeerCountMethod, ChainIdMethod, GetNodeInfoMethod, GetCoinbaseMethod, IsMiningMethod, GetHashrateMethod, IsSyncingMethod, GetGasPriceMethod, SubmitWorkMethod, GetWorkMethod, GetAccountsMethod, GetBalanceMethod, GetTransactionCountMethod, RequestAccountsMethod, GetBlockNumberMethod, GetBlockMethod, GetUncleMethod, GetBlockTransactionCountMethod, GetBlockUncleCountMethod, GetTransactionMethod, GetTransactionFromBlockMethod, GetTransactionReceiptMethod as GetTransactionReceipt, SendRawTransactionMethod, SignTransactionMethod, SendTransactionMethod, GetCodeMethod, SignMethod, CallMethod, GetStorageAtMethod, EstimateGasMethod, GetPastLogsMethod, EcRecoverMethod, ImportRawKeyMethod, ListAccountsMethod, LockAccountMethod, NewAccountMethod, PersonalSendTransactionMethod, PersonalSignMethod, PersonalSignTransactionMethod, UnlockAccountMethod, AddPrivateKeyMethod, AddSymKeyMethod, DeleteKeyPairMethod, DeleteMessageFilterMethod, DeleteSymKeyMethod, GenerateSymKeyFromPasswordMethod, GetFilterMessagesMethod, GetInfoMethod, GetPrivateKeyMethod, GetPublicKeyMethod, GetSymKeyMethod, HasKeyPairMethod, HasSymKeyMethod, MarkTrustedPeerMethod, NewKeyPairMethod, NewMessageFilterMethod, NewSymKeyMethod, PostMethod, SetMaxMessageSizeMethod, SetMinPoWMethod, ShhVersionMethod };
{
"name": "web3-core-method",
"namespace": "ethereum",
"version": "1.0.0-beta.46",
"version": "1.0.0-beta.47",
"description": "Handles the JSON-RPC methods. This package is internally used by web3.",

@@ -23,11 +23,11 @@ "repository": "https://github.com/ethereum/web3.js/tree/1.0/packages/web3-core-method",

"lodash": "^4.17.11",
"web3-core": "1.0.0-beta.46",
"web3-core-helpers": "1.0.0-beta.46",
"web3-core-promievent": "1.0.0-beta.46",
"web3-core-subscriptions": "1.0.0-beta.46",
"web3-utils": "1.0.0-beta.46"
"web3-core": "1.0.0-beta.47",
"web3-core-helpers": "1.0.0-beta.47",
"web3-core-promievent": "1.0.0-beta.47",
"web3-core-subscriptions": "1.0.0-beta.47",
"web3-utils": "1.0.0-beta.47"
},
"devDependencies": {
"dtslint": "^0.4.2",
"web3-providers": "1.0.0-beta.46"
"web3-providers": "1.0.0-beta.47"
},

@@ -34,0 +34,0 @@ "files": [

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc