"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getBytes = getBytes;

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _bytebuffer() {
  const data = _interopRequireDefault(require("bytebuffer"));

  _bytebuffer = function _bytebuffer() {
    return data;
  };

  return data;
}

var _assets = _interopRequireDefault(require("./assets"));

var _assetUtil = require("../utils/asset-util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

let Buffer;

if (typeof Buffer === 'undefined') {
  Buffer = require('buffer').Buffer;
}

function getBytes(_x, _x2, _x3) {
  return _getBytes.apply(this, arguments);
}

function _getBytes() {
  _getBytes = _asyncToGenerator(function* (transaction, skipSignature, skipSecondSignature) {
    let assetSize = 0;
    const assetBytes = yield getAssetBytes(transaction);
    if (assetBytes) assetSize = assetBytes.byteLength;
    const size = 1 + // type (int)
    4 + // timestamp (int)
    8 + // nethash 8
    32 + // senderPublicKey (int)
    32 + // requesterPublicKey (long)
    8 + // recipientId (long)
    8 + // amount (long)
    64 + // message
    64; // args or unused

    const bb = new (_bytebuffer().default)(size + assetSize, true); // const bb = new ByteBuffer(1, true);

    bb.writeByte(transaction.type); // +1

    bb.writeInt(transaction.timestamp); // +4

    bb.writeString(transaction.nethash); // +8
    // +32

    const senderPublicKeyBuffer = Buffer.from(transaction.senderPublicKey, 'hex');

    for (let i = 0; i < senderPublicKeyBuffer.length; i++) {
      bb.writeByte(senderPublicKeyBuffer[i]);
    } // +32


    if (transaction.requester_public_key) {
      // wxm block database
      const requesterPublicKey = Buffer.from(transaction.requester_public_key, 'hex'); // wxm block database

      for (let i = 0; i < requesterPublicKey.length; i++) {
        bb.writeByte(requesterPublicKey[i]);
      }
    } // +8


    if (transaction.recipientId) {
      bb.writeString(transaction.recipientId);
    } else {
      for (let i = 0; i < 8; i++) {
        bb.writeByte(0);
      }
    } // +8


    bb.writeString(transaction.amount); // +64

    if (transaction.message) bb.writeString(transaction.message); // +64

    if (transaction.args) {
      const args = transaction.args;

      for (let i = 0; i < args.length; ++i) {
        bb.writeString(args[i]);
      }
    }

    if (assetSize > 0) {
      for (let i = 0; i < assetSize; i++) {
        bb.writeByte(assetBytes[i]);
      }
    } // +64 验证的时候要减去


    if (!skipSignature && transaction.signature) {
      const signatureBuffer = Buffer.from(transaction.signature, 'hex');

      for (let i = 0; i < signatureBuffer.length; i++) {
        bb.writeByte(signatureBuffer[i]);
      }
    } // +64 验证的时候要再次减去


    if (!skipSecondSignature && transaction.sign_signature) {
      // wxm block database
      const signSignatureBuffer = Buffer.from(transaction.sign_signature, 'hex'); // wxm block database

      for (let i = 0; i < signSignatureBuffer.length; i++) {
        bb.writeByte(signSignatureBuffer[i]);
      }
    }

    bb.flip();
    return new Uint8Array(bb.toArrayBuffer());
  });
  return _getBytes.apply(this, arguments);
}

function getAssetBytes(_x4) {
  return _getAssetBytes.apply(this, arguments);
}

function _getAssetBytes() {
  _getAssetBytes = _asyncToGenerator(function* (transaction) {
    const _yield$getAsset = yield (0, _assetUtil.getAsset)(transaction),
          trsName = _yield$getAsset.trsName,
          asset = _yield$getAsset.asset;

    if (asset && typeof _assets.default[trsName] === 'function') {
      return yield _assets.default[trsName](asset);
    }

    return null;
  });
  return _getAssetBytes.apply(this, arguments);
}