Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let extend = function() {
const Accounts = require('web3-eth-accounts');
/**
* If signEIP712TypedData method is already added, return. No need to extend.
*/
if (Accounts.prototype.signEIP712TypedData) {
// It may have already been added by other package.
return;
}
const Account = require('eth-lib/lib/account');
const TypedData = require('./TypedData');
/**
* Extends Accounts prototype and adds signEIP712TypedData method.
*
* @param typedData TypedData instance.
* @param privateKey Private key to sign the data.
* @param callback
* @returns {{messageHash: String, v: *, r: *, s: *, signature: *}}
*/
Accounts.prototype.signEIP712TypedData = (
// It may have already been added by other package.
return;
}
const Account = require('eth-lib/lib/account');
const TypedData = require('./TypedData');
/**
* Extends Accounts prototype and adds signEIP712TypedData method.
*
* @param typedData TypedData instance.
* @param privateKey Private key to sign the data.
* @param callback
* @returns {{messageHash: String, v: *, r: *, s: *, signature: *}}
*/
Accounts.prototype.signEIP712TypedData = (
typedData,
privateKey,
callback,
) => {
let result;
try {
if (!(typedData instanceof TypedData)) {
typedData = TypedData.fromObject(typedData);
}
let signHash = typedData.getEIP712SignHash();
let signature = Account.sign(signHash, privateKey);
let vrs = Account.decodeSignature(signature);
result = {
messageHash: signHash,
r: vrs[1],
s: vrs[2],
throw error;
}
callback && callback(null, result);
return result;
};
const orgAddAccountFunctions = Accounts.prototype._addAccountFunctions;
/**
* Adds signEIP712TypedData in Accounts instance.
* @param account Account instance.
* @returns {Object} Account instance.
* @private
*/
Accounts.prototype._addAccountFunctions = function(account) {
const oAccounts = this;
account = orgAddAccountFunctions.apply(oAccounts, arguments);
account.signEIP712TypedData = function(
typeDataInstance,
callback,
version,
) {
console.log('Calling signEIP712TypedData function');
return oAccounts.signEIP712TypedData(
typeDataInstance,
account.privateKey,
callback,
version,
);
};
v: vrs[0],
r: vrs[1],
s: vrs[2],
signature: signature
};
} catch (error) {
callback && callback(error);
throw error;
}
callback && callback(null, result);
return result;
};
const org_addAccountFunctions = Accounts.prototype._addAccountFunctions;
Accounts.prototype._addAccountFunctions = function(account) {
const oAccounts = this;
account = org_addAccountFunctions.apply(oAccounts, arguments);
account.signEIP1077Transaction = function(transaction, callback, version) {
return oAccounts.signEIP1077Transaction(transaction, account.privateKey, callback, version);
};
return account;
};
module.exports = Accounts;
{ t: 'address', v: transaction.from }, //from
{ t: 'address', v: transaction.to }, //to
{ t: 'uint8', v: transaction.value }, //value
{ t: 'bytes', v: utils.soliditySha3(transaction.data) }, //dataHash
{ t: 'uint256', v: transaction.nonce }, //nonce
{ t: 'uint8', v: transaction.gasPrice }, //gasPrice
{ t: 'uint8', v: transaction.gas }, //gas
{ t: 'uint8', v: transaction.gasToken }, //gasToken
{ t: 'bytes4', v: transaction.callPrefix }, //callPrefix
{ t: 'uint8', v: transaction.operationType }, //operationType
{ t: 'bytes32', v: transaction.extraHash }
);
return txHash;
};
Accounts.prototype.signEIP1077Transaction = (transaction, privateKey, callback, version) => {
if (transaction.nonce < 0 || transaction.gas < 0 || transaction.gasPrice < 0) {
let error = new Error('Gas, gasPrice or nonce is lower than 0');
callback && callback(error);
throw error;
}
let result;
try {
let txHash = utils.toEIP1077TransactionHash(transaction, version);
let signature = Account.sign(txHash, privateKey);
let vrs = Account.decodeSignature(signature);
result = {
messageHash: txHash,
v: vrs[0],
r: vrs[1],
s: vrs[2],
messageHash: signHash,
r: vrs[1],
s: vrs[2],
v: vrs[0],
signature: signature,
};
} catch (error) {
callback && callback(error);
throw error;
}
callback && callback(null, result);
return result;
};
const orgAddAccountFunctions = Accounts.prototype._addAccountFunctions;
/**
* Adds signEIP712TypedData in Accounts instance.
* @param account Account instance.
* @returns {Object} Account instance.
* @private
*/
Accounts.prototype._addAccountFunctions = function(account) {
const oAccounts = this;
account = orgAddAccountFunctions.apply(oAccounts, arguments);
account.signEIP712TypedData = function(
typeDataInstance,
callback,
version,
) {
messageHash: txHash,
v: vrs[0],
r: vrs[1],
s: vrs[2],
signature: signature
};
} catch (error) {
callback && callback(error);
throw error;
}
callback && callback(null, result);
return result;
};
const org_addAccountFunctions = Accounts.prototype._addAccountFunctions;
Accounts.prototype._addAccountFunctions = function(account) {
const oAccounts = this;
account = org_addAccountFunctions.apply(oAccounts, arguments);
account.signEIP1077Transaction = function(transaction, callback, version) {
return oAccounts.signEIP1077Transaction(transaction, account.privateKey, callback, version);
};
return account;
};
module.exports = Accounts;