How to use ethereumjs-tx - 10 common examples

To help you get started, we’ve selected a few ethereumjs-tx 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 ETCDEVTeam / emerald-wallet / packages / core / src / blockchains / ethereum / EthereumTx.ts View on Github external
public static fromRaw (hex: string, chainId: any): ITransaction {
    if (chainId === 62 || chainId === 61) {
      // Because ethereumjs-tx doesn't support MORDEN and ETC
      const custom = Common.forCustomChain(1, {
        chainId
      }, 'byzantium');
      return new EthereumTx(new EthTx(hex, { common: custom }));
    }
    return new EthereumTx(new EthTx(hex, { chain: chainId }));
  }
  public internalTx: EthTx;
github MyEtherWallet / MyEtherWallet / src / wallets / hardware / ledger / index.js View on Github external
const txSigner = async tx => {
      tx = new Transaction(tx, {
        common: commonGenerator(store.state.network)
      });
      const networkId = tx.getChainId();
      tx.raw[6] = networkId;
      tx.raw[7] = Buffer.from([]);
      tx.raw[8] = Buffer.from([]);
      const tokenInfo = byContractAddress('0x' + tx.to.toString('hex'));
      if (tokenInfo) await this.ledger.provideERC20TokenInformation(tokenInfo);
      const result = await this.ledger.signTransaction(
        accountPath,
        tx.serialize().toString('hex')
      );

      // EIP155 support. check/recalc signature v value.
      let v = result.v;
      const rv = parseInt(v, 16);
github pubkey / eth-crypto / dist / lib / sign-transaction.js View on Github external
function signTransaction(rawTx, privateKey) {

    // check if privateKey->address matches rawTx.from
    var publicKey = (0, _publicKeyByPrivateKey2['default'])(privateKey);
    var address = (0, _publicKey.toAddress)(publicKey);
    if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey');

    var privateKeyBuffer = new Buffer(privateKey.replace(/^.{2}/g, ''), 'hex');

    var tx = new _ethereumjsTx.Transaction(rawTx);
    tx.sign(privateKeyBuffer);
    var serializedTx = tx.serialize().toString('hex');
    return serializedTx;
}
github pubkey / eth-crypto / dist / es / sign-transaction.js View on Github external
export default function signTransaction(rawTx, privateKey) {

    // check if privateKey->address matches rawTx.from
    var publicKey = publicKeyByPrivateKey(privateKey);
    var address = addressByPublicKey(publicKey);
    if (address != rawTx.from) throw new Error('EthCrypto.signTransaction(): rawTx.from does not match the address of the privateKey');

    var privateKeyBuffer = new Buffer(privateKey.replace(/^.{2}/g, ''), 'hex');

    var tx = new Transaction(rawTx);
    tx.sign(privateKeyBuffer);
    var serializedTx = tx.serialize().toString('hex');
    return serializedTx;
}
github ETCDEVTeam / emerald-js / packages / eth-account / src / EthAccount.ts View on Github external
signTx(txData: EthTxData): string {
      const tx = new EthereumTx(txData);
      // tx.gasLimit = txData.gasLimit;
      // tx.gasPrice = txData.gasPrice;
      // tx.nonce = txData.nonce;
      // tx.value = txData.value;
      // tx.data = txData.data;
      // tx.chainId = txData.chainId;
      tx.sign(this.wallet.getPrivateKey());
      return `0x${tx.serialize().toString('hex')}`;
    }
}
github MyCryptoHQ / MyCrypto / common / v2 / services / WalletService / deterministic / safe-t.ts View on Github external
result => {
          if (!result.success) {
            return reject(Error(result.error));
          }

          // TODO: Explain what's going on here? Add tests? Adapted from:
          // https://github.com/kvhnuke/etherwallet/blob/v3.10.2.6/app/scripts/uiFuncs.js#L24
          const txToSerialize: TxData = {
            ...strTx,
            v: addHexPrefix(new BN(result.v).toString(16)),
            r: addHexPrefix(result.r.toString()),
            s: addHexPrefix(result.s)
          };
          const eTx = new EthTx(txToSerialize);
          const serializedTx = eTx.serialize();
          resolve(serializedTx);
        }
      );
github MyCryptoHQ / MyCrypto / common / v2 / services / WalletService / deterministic / trezor.ts View on Github external
// check the returned signature_v and recalc signature_v if it needed
        // see also https://github.com/trezor/trezor-mcu/pull/399
        if (Number(res.payload.v) <= 1) {
          //  for larger chainId, only signature_v returned. simply recalc signature_v
          res.payload.v += 2 * chainId + 35;
        }

        // TODO: Explain what's going on here? Add tests? Adapted from:
        // https://github.com/kvhnuke/etherwallet/blob/v3.10.2.6/app/scripts/uiFuncs.js#L24
        const txToSerialize: TxData = {
          ...strTx,
          v: res.payload.v,
          r: res.payload.r,
          s: res.payload.s
        };
        const eTx = new EthTx(txToSerialize);
        const serializedTx = eTx.serialize();
        resolve(serializedTx);
      });
    });
github MyCryptoHQ / MyCrypto / common / v2 / services / WalletService / deterministic / ledger.ts View on Github external
// (rv !== cv) : for v is truncated byte case
          // (rv & cv): make cv to truncated byte
          // (rv & cv) !== rv: signature v bit needed
          cv += 1; // add signature v bit.
        }
        v = cv.toString(16);
      }

      const txToSerialize: TxData = {
        ...txFields,
        v: addHexPrefix(v),
        r: addHexPrefix(result.r),
        s: addHexPrefix(result.s)
      };

      return new EthTx(txToSerialize).serialize();
    } catch (err) {
      throw Error(err + '. Check to make sure contract data is on');
    }
  }
github MetaMask / eth-simple-keyring / test / index.js View on Github external
it('returns a signed tx object', async () => {
      await keyring.deserialize([ privateKey ])

      const txParams = {
        from: address,
        nonce: '0x00',
        gasPrice: '0x09184e72a000',
        gasLimit: '0x2710',
        to: address,
        value: '0x1000',
      }
      const tx = new EthereumTx(txParams)

      const signed = await keyring.signTransaction(address, tx)
      assert.ok(signed.raw, 'has a raw signature')
    })
  })
github MyEtherWallet / MyEtherWallet / src / wallets / hybrid / WalletConnect / index.js View on Github external
.then(signed => {
              console.log(signed);
              const _tx = new Transaction(signed);
              const signedChainId = calculateChainIdFromV(_tx.v);
              if (signedChainId !== networkId)
                throw new Error(
                  'Invalid networkId signature returned. Expected: ' +
                    networkId +
                    ', Got: ' +
                    signedChainId,
                  'InvalidNetworkId'
                );
              resolve(getSignTransactionObject(_tx));
            })
            .catch(reject);

ethereumjs-tx

A simple module for creating, manipulating and signing Ethereum transactions

MPL-2.0
Latest version published 4 years ago

Package Health Score

58 / 100
Full package analysis

Popular ethereumjs-tx functions