all files / ethereumjs-tx/ index.js

72.06% Statements 49/68
55% Branches 11/20
61.54% Functions 8/13
72.06% Lines 49/68
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229                   125×                                                                                         125×                             125×                                 48×   48×       48×     48×       48×               23× 23× 23×               23×       23×             48× 48×         48× 48×   15×     33×         33×           33×     33×                                     33× 33× 33× 10486347× 165×   10486182×     33×               33×                                   48×    
const ethUtil = require('ethereumjs-util')
const fees = require('ethereum-common')
const ecdsa = require('secp256k1')
const BN = ethUtil.BN
const rlp = ethUtil.rlp
 
// give browser access to Buffers
global.Buffer = Buffer
global.ethUtil = ethUtil
 
/**
 * Represents a transaction
 * @constructor
 * @param {Buffer|Array} data raw data, deserialized
 */
var Transaction = module.exports = function (data) {
  // Define Properties
  const fields = [{
    name: 'nonce',
    length: 32,
    allowLess: true,
    default: new Buffer([])
  }, {
    name: 'gasPrice',
    length: 32,
    allowLess: true,
    default: new Buffer([])
  }, {
    name: 'gasLimit',
    length: 32,
    allowLess: true,
    default: new Buffer([])
  }, {
    name: 'to',
    allowZero: true,
    length: 20,
    default: new Buffer([])
  }, {
    name: 'value',
    length: 32,
    allowLess: true,
    default: new Buffer([])
  }, {
    name: 'data',
    allowZero: true,
    default: new Buffer([])
  }, {
    name: 'v',
    length: 1,
    default: new Buffer([0x1c])
  }, {
    name: 'r',
    length: 32,
    allowLess: true,
    default: new Buffer([])
  }, {
    name: 's',
    length: 32,
    allowLess: true,
    default: new Buffer([])
  }]
 
  Object.defineProperty(this, 'from', {
    enumerable: false,
    configurable: true,
    get: function () {
      if (this._from) {
        return this._from
      }
      this._from = this.getSenderAddress()
      return this._from
    },
    set: function (v) {
      this._from = v
    }
  })
 
  ethUtil.defineProperties(this, fields, data)
}
 
/**
 * Returns the rlp encoding of the transaction
 * @method serialize
 * @return {Buffer}
 */
Transaction.prototype.serialize = function () {
  return rlp.encode(this.raw)
}
 
/**
 * Computes a sha3-256 hash of the tx
 * @method hash
 * @param {Boolean} [true] signature - whether or not to inculde the signature
 * @return {Buffer}
 */
Transaction.prototype.hash = function (signature) {
  var toHash
 
  Iif (typeof signature === 'undefined') {
    signature = true
  }
 
  Iif (signature) {
    toHash = this.raw
  } else {
    toHash = this.raw.slice(0, 6)
  }
 
  // create hash
  return ethUtil.sha3(rlp.encode(toHash))
}
 
/**
 * gets the senders address
 * @method getSenderAddress
 * @return {Buffer}
 */
Transaction.prototype.getSenderAddress = function () {
  var pubKey = this.getSenderPublicKey()
  pubKey = ecdsa.publicKeyConvert(pubKey, false)
  return ethUtil.pubToAddress(pubKey)
}
 
/**
 * gets the senders public key
 * @method getSenderPublicKey
 * @return {Buffer}
 */
Transaction.prototype.getSenderPublicKey = function () {
  Iif (!this._senderPubKey || !this._senderPubKey.length) {
    this.verifySignature()
  }
 
  return this._senderPubKey
}
 
/**
 * @method verifySignature
 * @return {Boolean}
 */
Transaction.prototype.verifySignature = function () {
  var msgHash = this.hash(false)
  var sig = {
    signature: Buffer.concat([ethUtil.pad(this.r, 32), ethUtil.pad(this.s, 32)], 64),
    recovery: ethUtil.bufferToInt(this.v) - 27
  }
 
  try {
    this._senderPubKey = ecdsa.recoverSync(msgHash, sig.signature, sig.recovery)
  } catch (e) {
    return false
  }
 
  Iif (!this._senderPubKey) {
    return false
  }
 
  // hack to force elliptic to verify
  Iif (process.browser) {
    var result = ecdsa.verifySync(msgHash, sig.signature, this._senderPubKey)
    if (!result) {
      this._senderPubKey = null
    }
  } else {
    result = !!this._senderPubKey
  }
 
  return result
}
/**
 * sign a transaction with a given a private key
 * @method sign
 * @param {Buffer} privateKey
 */
Transaction.prototype.sign = function (privateKey) {
  var msgHash = this.hash(false)
  var sig = ecdsa.signSync(msgHash, privateKey)
 
  this.r = sig.signature.slice(0, 32)
  this.s = sig.signature.slice(32, 64)
  this.v = sig.recovery + 27
}
/**
 * The amount of gas paid for the data in this tx
 * @method getDataFee
 * @return {bn.js}
 */
Transaction.prototype.getDataFee = function () {
  const data = this.raw[5]
  var cost = new BN(0)
  for (var i = 0; i < data.length; i++) {
    if (data[i] === 0) {
      cost.iaddn(fees.txDataZeroGas.v)
    } else {
      cost.iaddn(fees.txDataNonZeroGas.v)
    }
  }
  return cost
}
 
/**
 * the base amount of gas it takes to be a valid tx
 * @method getBaseFee
 * @return {bn.js}
 */
Transaction.prototype.getBaseFee = function () {
  return this.getDataFee().addn(fees.txGas.v)
}
 
/**
 * the up front amount that an account must have for this transaction to be valid
 * @method getUpfrontCost
 * @return {BN}
 */
Transaction.prototype.getUpfrontCost = function () {
  return new BN(this.gasLimit)
    .mul(new BN(this.gasPrice))
    .add(new BN(this.value))
}
 
/**
 * validates the signature and checks to see if it has enough gas
 * @method validate
 * @return {Boolean}
 */
Transaction.prototype.validate = function () {
  return this.verifySignature() && (Number(this.getBaseFee().toString()) <= ethUtil.bufferToInt(this.gasLimit))
}