How to use web3-eth-accounts - 10 common examples

To help you get started, we’ve selected a few web3-eth-accounts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mosaicdao / mosaic.js / src / utils / EIP712SignerExtension / extender.js View on Github external
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 = (
github mosaicdao / mosaic.js / src / utils / EIP712SignerExtension / extender.js View on Github external
// 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],
github melonproject / protocol / src / utils / environment / initTestEnvironment.ts View on Github external
// Pass in Ganache.provider but only if
    // process.env.JSON_RPC_ENDPOINT is not set
    endpoint: process.env.JSON_RPC_ENDPOINT,
    logger: testLogger,
    provider: !process.env.JSON_RPC_ENDPOINT && getGanache(),
  });
  const accounts = await environment.eth.getAccounts();

  ensure(
    keyPairs.has(accounts[0].toLowerCase()),
    `Unknown address: ${
      accounts[0]
    }. Are you running ganache with the right mnemonic: ${testMnemonic}`,
  );

  const web3Accounts = new Web3Accounts(environment.eth.currentProvider);

  const signer = (unsignedTransaction, from = new Address(accounts[0])) =>
    web3Accounts
      .signTransaction(unsignedTransaction, keyPairs.get(from.toLowerCase()))
      .then(t => t.rawTransaction);

  const enhancedEnvironment = {
    ...environment,
    wallet: {
      address: accounts[0],
      sign: signer,
    },
  };

  return enhancedEnvironment;
};
github mosaicdao / mosaic.js / src / utils / EIP712SignerExtension / extender.js View on Github external
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,
      );
    };

web3-eth-accounts

Package for managing Ethereum accounts and signing

LGPL-3.0
Latest version published 18 days ago

Package Health Score

98 / 100
Full package analysis

Similar packages