<commit_before>import * as tape from 'tape'
import { Buffer } from 'buffer'
import {
  BN,
  rlp,
  zeros,
  privateToPublic,
  toBuffer,
  bufferToHex,
  unpadBuffer,
} from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { Transaction, TxData } from '../src'
import { TxsJsonEntry, VitaliksTestsDataEntry } from './types'

const txFixtures: TxsJsonEntry[] = require('./json/txs.json')
const txFixturesEip155: VitaliksTestsDataEntry[] = require('./json/ttTransactionTestEip155VitaliksTests.json')

tape('[Transaction]: Basic functions', function (t) {
  const transactions: Transaction[] = []

  t.test('should decode transactions', function (st) {
    txFixtures.slice(0, 4).forEach(function (tx: any) {
      const txData = tx.raw.map(toBuffer)
      const pt = Transaction.fromValuesArray(txData)

      st.equal(bufferToHex(unpadBuffer(toBuffer(pt.nonce))), tx.raw[0])
      st.equal(bufferToHex(toBuffer(pt.gasPrice)), tx.raw[1])
      st.equal(bufferToHex(toBuffer(pt.gasLimit)), tx.raw[2])
      st.equal(pt.to?.toString(), tx.raw[3])
      st.equal(bufferToHex(unpadBuffer(toBuffer(pt.value))), tx.raw[4])
      st.equal('0x' + pt.data.toString('hex'), tx.raw[5])
      st.equal(bufferToHex(toBuffer(pt.v)), tx.raw[6])
      st.equal(bufferToHex(toBuffer(pt.r)), tx.raw[7])
      st.equal(bufferToHex(toBuffer(pt.s)), tx.raw[8])

      transactions.push(pt)
    })
    st.end()
  })

  t.test('should serialize', function (st) {
    transactions.forEach(function (tx, i) {
      const s1 = tx.serialize()
      const s2 = rlp.encode(txFixtures[i].raw)
      st.ok(s1.equals(s2))
    })
    st.end()
  })

  t.test('should hash', function (st) {
    const tx = Transaction.fromValuesArray(
      txFixtures[3].raw.map(toBuffer),
      new Common({ chain: 'mainnet', hardfork: 'tangerineWhistle' }),
    )
    st.deepEqual(
      tx.hash(),
      Buffer.from('375a8983c9fc56d7cfd118254a80a8d7403d590a6c9e105532b67aca1efb97aa', 'hex'),
    )
    st.deepEqual(
      tx.getMessageToSign(),
      Buffer.from('61e1ec33764304dddb55348e7883d4437426f44ab3ef65e6da1e025734c03ff0', 'hex'),
    )
    st.deepEqual(
      tx.hash(),
      Buffer.from('375a8983c9fc56d7cfd118254a80a8d7403d590a6c9e105532b67aca1efb97aa', 'hex'),
    )
    st.end()
  })

  t.test('should hash with defined chainId', function (st) {
    const tx = Transaction.fromValuesArray(txFixtures[4].raw.map(toBuffer))
    st.equal(
      tx.hash().toString('hex'),
      '0f09dc98ea85b7872f4409131a790b91e7540953992886fc268b7ba5c96820e4',
    )
    st.equal(
      tx.hash().toString('hex'),
      '0f09dc98ea85b7872f4409131a790b91e7540953992886fc268b7ba5c96820e4',
    )
    st.equal(
      tx.getMessageToSign().toString('hex'),
      'f97c73fdca079da7652dbc61a46cd5aeef804008e057be3e712c43eac389aaf0',
    )
    st.end()
  })

  t.test('should verify Signatures', function (st) {
    transactions.forEach(function (tx) {
      st.equals(tx.verifySignature(), true)
    })
    st.end()
  })

  t.test('should not verify Signatures', function (st) {
    const txs: Transaction[] = []

    txFixtures.slice(0, 4).forEach(function (txFixture: any) {
      const txData = txFixture.raw.map(toBuffer)
      // set `s` to zero
      txData[8] = zeros(32)
      const tx = Transaction.fromValuesArray(txData)
      txs.push(tx)
    })

    txs.forEach(function (tx) {
      st.equals(tx.verifySignature(), false)

      st.ok(
        tx.validate(true).includes('Invalid Signature'),<commit_msg>update tx test case name<commit_after>import * as tape from 'tape'
import { Buffer } from 'buffer'
import {
  BN,
  rlp,
  zeros,
  privateToPublic,
  toBuffer,
  bufferToHex,
  unpadBuffer,
} from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { Transaction, TxData } from '../src'
import { TxsJsonEntry, VitaliksTestsDataEntry } from './types'

const txFixtures: TxsJsonEntry[] = require('./json/txs.json')
const txFixturesEip155: VitaliksTestsDataEntry[] = require('./json/ttTransactionTestEip155VitaliksTests.json')

tape('[Transaction]: Basic functions', function (t) {
  const transactions: Transaction[] = []

  t.test('should decode transactions', function (st) {
    txFixtures.slice(0, 4).forEach(function (tx: any) {
      const txData = tx.raw.map(toBuffer)
      const pt = Transaction.fromValuesArray(txData)

      st.equal(bufferToHex(unpadBuffer(toBuffer(pt.nonce))), tx.raw[0])
      st.equal(bufferToHex(toBuffer(pt.gasPrice)), tx.raw[1])
      st.equal(bufferToHex(toBuffer(pt.gasLimit)), tx.raw[2])
      st.equal(pt.to?.toString(), tx.raw[3])
      st.equal(bufferToHex(unpadBuffer(toBuffer(pt.value))), tx.raw[4])
      st.equal('0x' + pt.data.toString('hex'), tx.raw[5])
      st.equal(bufferToHex(toBuffer(pt.v)), tx.raw[6])
      st.equal(bufferToHex(toBuffer(pt.r)), tx.raw[7])
      st.equal(bufferToHex(toBuffer(pt.s)), tx.raw[8])

      transactions.push(pt)
    })
    st.end()
  })

  t.test('should serialize', function (st) {
    transactions.forEach(function (tx, i) {
      const s1 = tx.serialize()
      const s2 = rlp.encode(txFixtures[i].raw)
      st.ok(s1.equals(s2))
    })
    st.end()
  })

  t.test('should hash', function (st) {
    const tx = Transaction.fromValuesArray(
      txFixtures[3].raw.map(toBuffer),
      new Common({ chain: 'mainnet', hardfork: 'tangerineWhistle' }),
    )
    st.deepEqual(
      tx.hash(),
      Buffer.from('375a8983c9fc56d7cfd118254a80a8d7403d590a6c9e105532b67aca1efb97aa', 'hex'),
    )
    st.deepEqual(
      tx.getMessageToSign(),
      Buffer.from('61e1ec33764304dddb55348e7883d4437426f44ab3ef65e6da1e025734c03ff0', 'hex'),
    )
    st.deepEqual(
      tx.hash(),
      Buffer.from('375a8983c9fc56d7cfd118254a80a8d7403d590a6c9e105532b67aca1efb97aa', 'hex'),
    )
    st.end()
  })

  t.test('should hash with defined chainId', function (st) {
    const tx = Transaction.fromValuesArray(txFixtures[4].raw.map(toBuffer))
    st.equal(
      tx.hash().toString('hex'),
      '0f09dc98ea85b7872f4409131a790b91e7540953992886fc268b7ba5c96820e4',
    )
    st.equal(
      tx.hash().toString('hex'),
      '0f09dc98ea85b7872f4409131a790b91e7540953992886fc268b7ba5c96820e4',
    )
    st.equal(
      tx.getMessageToSign().toString('hex'),
      'f97c73fdca079da7652dbc61a46cd5aeef804008e057be3e712c43eac389aaf0',
    )
    st.end()
  })

  t.test('should verify Signatures', function (st) {
    transactions.forEach(function (tx) {
      st.equals(tx.verifySignature(), true)
    })
    st.end()
  })

  t.test('should not verify invalid signatures', function (st) {
    const txs: Transaction[] = []

    txFixtures.slice(0, 4).forEach(function (txFixture: any) {
      const txData = txFixture.raw.map(toBuffer)
      // set `s` to zero
      txData[8] = zeros(32)
      const tx = Transaction.fromValuesArray(txData)
      txs.push(tx)
    })

    txs.forEach(function (tx) {
      st.equals(tx.verifySignature(), false)

      st.ok(
        tx.validate(true).includes('Invalid Signature'),
{'fraction_non_alphanumeric': 0.14951929975606257, 'fraction_numerical': 0.080642846893385, 'mean_word_length': 3.60978835978836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4002869', 'n_tokens_mistral': 2906, 'n_tokens_neox': 2612, 'n_words': 384}