src/index.js
function sign(transaction, privateKey, toObject) {
  if (typeof transaction !== 'object' || transaction === null) { throw new Error(`[ethjs-signer] transaction input must be a type 'object', got '${typeof(transaction)}'`); }
  if (typeof privateKey !== 'string') { throw new Error('[ethjs-signer] private key input must be a string'); }
  if (!privateKey.match(/^(0x)[0-9a-fA-F]{64}$/)) { throw new Error('[ethjs-signer] invalid private key value, private key must be a prefixed hexified 32 byte string (i.e. "0x..." 64 chars long).'); }

  const raw = [];

  transactionFields.forEach((fieldInfo) => {
    var value = new Buffer(0); // eslint-disable-line

    // shim for field name gas
    const txKey = (fieldInfo.name === 'gasLimit' && transaction.gas) ? 'gas' : fieldInfo.name;

    if (typeof transaction[txKey] !== 'undefined') {
      if (fieldInfo.number === true) {
        value = bnToBuffer(numberToBN(transaction[txKey]));
      } else {
        value = new Buffer(padToEven(stripHexPrefix(transaction[txKey])), 'hex');
      }
    }

    // Fixed-width field
    if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
      throw new Error(`[ethjs-signer] while signing raw transaction, invalid '${fieldInfo.name}', invalid length should be '${fieldInfo.length}' got '${value.length}'`);
    }

    // Variable-width (with a maximum)
    if (fieldInfo.maxLength) {
      value = stripZeros(value);
      if (value.length > fieldInfo.maxLength) {
        throw new Error(`[ethjs-signer] while signing raw transaction, invalid '${fieldInfo.name}' length, the max length is '${fieldInfo.maxLength}', got '${value.length}'`);
      }
    }

    raw.push(value);
  });

  // private key is not stored in memory
  const signature = secp256k1.keyFromPrivate(new Buffer(privateKey.slice(2), 'hex'))
                    .sign((new Buffer(keccak256(rlp.encode(raw)), 'hex')), { canonical: true });

  raw.push(new Buffer([27 + signature.recoveryParam]));
  raw.push(bnToBuffer(signature.r));
  raw.push(bnToBuffer(signature.s));

  return toObject ? raw : `0x${rlp.encode(raw).toString('hex')}`;
}
function sign(transaction, privateKey, toObject) {
  if (typeof transaction !== 'object' || transaction === null) { throw new Error(`[ethjs-signer] transaction input must be a type 'object', got '${typeof(transaction)}'`); }
  if (typeof privateKey !== 'string') { throw new Error('[ethjs-signer] private key input must be a string'); }
  if (!privateKey.match(/^(0x)[0-9a-fA-F]{64}$/)) { throw new Error('[ethjs-signer] invalid private key value, private key must be a prefixed hexified 32 byte string (i.e. "0x..." 64 chars long).'); }

  const raw = [];

  transactionFields.forEach((fieldInfo) => {
    var value = new Buffer(0); // eslint-disable-line

    // shim for field name gas
    const txKey = (fieldInfo.name === 'gasLimit' && transaction.gas) ? 'gas' : fieldInfo.name;

    if (typeof transaction[txKey] !== 'undefined') {
      if (fieldInfo.number === true) {
        value = bnToBuffer(numberToBN(transaction[txKey]));
      } else {
        value = new Buffer(padToEven(stripHexPrefix(transaction[txKey])), 'hex');
      }
    }

    // Fixed-width field
    if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
      throw new Error(`[ethjs-signer] while signing raw transaction, invalid '${fieldInfo.name}', invalid length should be '${fieldInfo.length}' got '${value.length}'`);
    }

    // Variable-width (with a maximum)
    if (fieldInfo.maxLength) {
      value = stripZeros(value);
      if (value.length > fieldInfo.maxLength) {
        throw new Error(`[ethjs-signer] while signing raw transaction, invalid '${fieldInfo.name}' length, the max length is '${fieldInfo.maxLength}', got '${value.length}'`);
      }
    }

    raw.push(value);
  });

  // private key is not stored in memory
  const signature = secp256k1.keyFromPrivate(new Buffer(privateKey.slice(2), 'hex'))
                    .sign((new Buffer(keccak256(rlp.encode(raw)), 'hex')), { canonical: true });

  raw.push(new Buffer([27 + signature.recoveryParam]));
  raw.push(bnToBuffer(signature.r));
  raw.push(bnToBuffer(signature.s));

  return toObject ? raw : `0x${rlp.encode(raw).toString('hex')}`;
}
function sign ( transaction , privateKey , toObject ) { if ( typeof transaction !== 'object' || transaction === null ) { throw new Error ( ` ${ typeof ( transaction ) } ` ) ; } if ( typeof privateKey !== 'string' ) { throw new Error ( '[ethjs-signer] private key input must be a string' ) ; } if ( ! privateKey . match ( / ^(0x)[0-9a-fA-F]{64}$ / ) ) { throw new Error ( '[ethjs-signer] invalid private key value, private key must be a prefixed hexified 32 byte string (i.e. "0x..." 64 chars long).' ) ; } const raw = [ ] ; transactionFields . forEach ( ( fieldInfo ) => { var value = new Buffer ( 0 ) ; // eslint-disable-line // shim for field name gas const txKey = ( fieldInfo . name === 'gasLimit' && transaction . gas ) ? 'gas' : fieldInfo . name ; if ( typeof transaction [ txKey ] !== 'undefined' ) { if ( fieldInfo . number === true ) { value = bnToBuffer ( numberToBN ( transaction [ txKey ] ) ) ; } else { value = new Buffer ( padToEven ( stripHexPrefix ( transaction [ txKey ] ) ) , 'hex' ) ; } } // Fixed-width field if ( fieldInfo . length && value . length !== fieldInfo . length && value . length > 0 ) { throw new Error ( ` ${ fieldInfo . name } ${ fieldInfo . length } ${ value . length } ` ) ; } // Variable-width (with a maximum) if ( fieldInfo . maxLength ) { value = stripZeros ( value ) ; if ( value . length > fieldInfo . maxLength ) { throw new Error ( ` ${ fieldInfo . name } ${ fieldInfo . maxLength } ${ value . length } ` ) ; } } raw . push ( value ) ; } ) ; // private key is not stored in memory const signature = secp256k1 . keyFromPrivate ( new Buffer ( privateKey . slice ( 2 ) , 'hex' ) ) . sign ( ( new Buffer ( keccak256 ( rlp . encode ( raw ) ) , 'hex' ) ) , { canonical : true } ) ; raw . push ( new Buffer ( [ 27 + signature . recoveryParam ] ) ) ; raw . push ( bnToBuffer ( signature . r ) ) ; raw . push ( bnToBuffer ( signature . s ) ) ; return toObject ? raw : ` ${ rlp . encode ( raw ) . toString ( 'hex' ) } ` ; }
Will sign a raw transaction and return it either as a serlized hex string or raw tx object.

@method sign
@param {Object} transaction a valid transaction object
@param {String} privateKey a valid 32 byte prefixed hex string private key
@param {Boolean} toObject **Optional**
@returns {String|Object} output either a serilized hex string or signed tx object
Will sign a raw transaction and return it either as a serlized hex string or raw tx object .