"use strict";

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

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;
}

function _utils() {
  const data = _interopRequireDefault(require("@ddn/utils"));

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

  return data;
}

var _crypto = _interopRequireDefault(require("../utils/crypto"));

var _slots = _interopRequireDefault(require("../time/slots"));

var _config = require("../config");

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); }); }; }

function createDApp(_x, _x2, _x3) {
  return _createDApp.apply(this, arguments);
}

function _createDApp() {
  _createDApp = _asyncToGenerator(function* (options, secret, secondSecret) {
    const keys = _crypto.default.getKeys(secret);

    const fee = _utils().default.bignum.multiply(_config.constants.net.fees.dapp, _config.constants.fixedPoint);

    const transaction = {
      nethash: _config.config.nethash,
      type: _utils().default.assetTypes.DAPP,
      amount: '0',
      fee: `${fee}`,
      recipientId: null,
      senderPublicKey: keys.publicKey,
      timestamp: _slots.default.getTime() - _config.config.clientDriftSeconds,
      asset: {
        dapp: {
          category: options.category,
          name: options.name,
          description: options.description,
          tags: options.tags,
          type: options.type,
          link: options.link,
          icon: options.icon,
          delegates: options.delegates,
          unlock_delegates: options.unlock_delegates
        }
      }
    };
    transaction.signature = yield _crypto.default.sign(transaction, keys);

    if (secondSecret) {
      const secondKeys = _crypto.default.getKeys(secondSecret);

      transaction.sign_signature = yield _crypto.default.secondSign(transaction, secondKeys);
    }

    transaction.id = yield _crypto.default.getId(transaction);
    return transaction;
  });
  return _createDApp.apply(this, arguments);
}

function getDAppTransactionBytes(trs, skipSignature) {
  const bb = new (_bytebuffer().default)(1, true);
  bb.writeInt(trs.timestamp);
  bb.writeString(trs.fee);
  const senderPublicKeyBuffer = Buffer.from(trs.senderPublicKey, 'hex');

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

  bb.writeInt(trs.type);
  if (trs.args) bb.writeString(trs.args);

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

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

  bb.flip();
  return bb.toBuffer();
}

function createInnerTransaction(options, secret) {
  const keys = _crypto.default.getKeys(secret);

  let args = options.args;
  if (args instanceof Array) args = JSON.stringify(args);
  const trs = {
    nethash: _config.config.nethash,
    fee: options.fee,
    timestamp: _slots.default.getTime() - _config.config.clientDriftSeconds,
    senderPublicKey: keys.publicKey,
    type: options.type,
    args
  };
  trs.signature = _crypto.default.signBytes(getDAppTransactionBytes(trs), keys); // .sign?

  return trs;
}

var _default = {
  createDApp,
  createInnerTransaction
};
exports.default = _default;