UNPKG

@aeternity/aepp-sdk

Version:
300 lines (266 loc) 11.8 kB
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys"; import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"; import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter"; import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor"; import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors"; import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties"; import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property"; import _toConsumableArray from "@babel/runtime-corejs3/helpers/toConsumableArray"; import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty"; import _typeof from "@babel/runtime-corejs3/helpers/typeof"; function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context3; _forEachInstanceProperty(_context3 = ownKeys(Object(source), true)).call(_context3, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context4; _forEachInstanceProperty(_context4 = ownKeys(Object(source))).call(_context4, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; } import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes"; import _Object$values from "@babel/runtime-corejs3/core-js-stable/object/values"; import _Object$assign from "@babel/runtime-corejs3/core-js-stable/object/assign"; import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat"; /** * TxObject module * @module @aeternity/aepp-sdk/es/tx/tx-object * @export TxObject * @example import TxObject from '@aeternity/aepp-sdk/es/tx/tx-object' */ import stampit from '@stamp/it'; import { assertedType } from '../utils/crypto'; import { buildTx, calculateFee, unpackTx } from './builder'; import { TX_TYPE } from './builder/schema'; import { encode } from './builder/helpers'; import { isHex } from '../utils/string'; /** * Build transaction from object * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @param {String} type Transaction type * @param {Object} params Transaction params * @param {Object} [options={}] Options * @throws {Error} Arguments validation error's * @return {{ encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, params: Object, type: String }} */ var buildTransaction = function buildTransaction(type, params) { var _context; var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; if (_typeof(params) !== 'object') throw new Error('"params" should be an object'); if (typeof type !== 'string' || !_includesInstanceProperty(_context = _Object$values(TX_TYPE)).call(_context, type)) throw new Error("Unknown transaction type ".concat(type)); var fee = calculateFee(params.fee, type, { gas: params.gas, params: params, vsn: params.vsn }); var _buildTx = buildTx(_objectSpread(_objectSpread({}, params), {}, { fee: fee }), type, _objectSpread({ vsn: params.vsn }, options)), rlpEncoded = _buildTx.rlpEncoded, binary = _buildTx.binary, encodedTx = _buildTx.tx, txObject = _buildTx.txObject; return { rlpEncoded: rlpEncoded, binary: binary, encodedTx: encodedTx, params: txObject, type: type }; }; /** * Unpack transaction from RLP encoded binary or base64c string * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @param {Buffer|String} tx RLP encoded binary or base64c(rlpBinary) string * @throws {Error} Arguments validation error's * @return {{ encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, type: String, params: Object }} */ var unpackTransaction = function unpackTransaction(tx) { if (!tx) throw new Error("Invalid transaction: ".concat(tx)); if (typeof tx === 'string') { if (!assertedType(tx, 'tx', true)) throw new Error('Invalid transaction string. Tx should be `tx` prefixed base58c string'); var _unpackTx = unpackTx(tx), type = _unpackTx.txType, params = _unpackTx.tx, rlpEncoded = _unpackTx.rlpEncoded, binary = _unpackTx.binary; return { encodedTx: tx, type: type, params: params, rlpEncoded: rlpEncoded, binary: binary }; } if (Buffer.isBuffer(tx)) { var _unpackTx2 = unpackTx(tx, true), _type = _unpackTx2.txType, _params = _unpackTx2.tx, _rlpEncoded = _unpackTx2.rlpEncoded, _binary = _unpackTx2.binary; return { encodedTx: encode(tx, 'tx'), type: _type, params: _params, rlpEncoded: _rlpEncoded, binary: _binary }; } }; /** * Helper which build or unpack transaction base on constructor arguments * Need to provide one of arguments: [tx] -> unpack flow or [params, type] -> build flow * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @param {Buffer|String} [tx] Transaction rlp binary or vase64c string * @param {Object} params Transaction params * @param {String} type Transaction type * @param {Object} [options={}] Options * @throws {Error} Arguments validation error's * @return {{encodedTx: String, binary: Array<Buffer>, rlpEncoded: Buffer, type: String, params: Object}} */ var initTransaction = function initTransaction() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, tx = _ref.tx, params = _ref.params, type = _ref.type, _ref$options = _ref.options, options = _ref$options === void 0 ? {} : _ref$options; if (params && type) return buildTransaction(type, params, options); if (tx) return unpackTransaction(tx); throw new Error('Invalid TxObject arguments. Please provide one of { tx: "tx_asdasd23..." } or { type: "spendTx", params: {...} }'); }; /** * Transaction Validator Stamp * This stamp give us possibility to unpack and validate some of transaction properties, * to make sure we can post it to the chain * @function * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @rtype Stamp * @param {Object} [options={}] - Initializer object * @param {Buffer|String} [options.tx] - Rlp binary or base64c transaction * @param {Object} [options.params] - Transaction params * @param {String} [options.type] - Transaction type * @param {Object} [options.options] - Build options * @return {Object} TxObject instance * @example TxObject({ params: {...}, type: 'spendTx' }) */ export var TxObject = stampit({ init: function init() { var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, tx = _ref2.tx, params = _ref2.params, type = _ref2.type, _ref2$options = _ref2.options, options = _ref2$options === void 0 ? {} : _ref2$options; this.options = options; this.signatures = []; _Object$assign(this, initTransaction({ tx: tx, params: params, type: type, options: options })); if (this.type === TX_TYPE.signed) { var _this$params = this.params, signatures = _this$params.signatures, _this$params$encodedT = _this$params.encodedTx, txType = _this$params$encodedT.txType, _tx = _this$params$encodedT.tx; this.signatures = signatures; this.params = _tx; this.type = txType; this.isSigned = true; } }, statics: { /** * Create txObject from base64c RLP encoded transaction string with 'tx_' prefix * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @static * @param {String} tx Transaction string (tx_23fsdgsdfg...) * @return {TxObject} */ fromString: function fromString(tx) { return TxObject({ tx: tx }); }, /** * Create txObject from transaction RLP binary * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @static * @param {Buffer} tx Transaction RLP binary * @return {TxObject} */ fromRlp: function fromRlp(tx) { return TxObject({ tx: tx }); } }, methods: { /** * Rebuild transaction with new params and recalculate fee * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @param {Object} props Transaction properties for update * @param options * @return {TxObject} */ setProp: function setProp() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (_typeof(props) !== 'object') throw new Error('Props should be an object'); this.isSigned = false; this.signatures = []; _Object$assign(this, buildTransaction(this.type, _objectSpread(_objectSpread(_objectSpread({}, this.params), props), {}, { fee: null }), _objectSpread(_objectSpread({}, this.options), options))); return this; }, /** * Get signatures * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @return {Array} Array of signatures */ getSignatures: function getSignatures() { if (!this.isSigned) throw new Error('Signature not found, transaction is not signed'); return this.signatures; }, /** * Add signature * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @param {Buffer|String} signature Signature to add ( Can be: Buffer | Uint8Array | HexString ) * @return {void} */ addSignature: function addSignature(signature) { var _context2; signature = isHex(signature) ? Buffer.from(signature, 'hex') : signature; if (!Buffer.isBuffer(signature) && !(signature instanceof Uint8Array)) throw new Error('Invalid signature, signature must be of type Buffer or Uint8Array'); _Object$assign(this, buildTransaction(TX_TYPE.signed, { encodedTx: this.rlpEncoded, signatures: [_concatInstanceProperty(_context2 = []).call(_context2, _toConsumableArray(this.signatures), [signature])] })); var _this$params2 = this.params, signatures = _this$params2.signatures, _this$params2$encoded = _this$params2.encodedTx, txType = _this$params2$encoded.txType, tx = _this$params2$encoded.tx; this.signatures = signatures; this.params = tx; this.type = txType; this.isSigned = true; }, /** * Calculate fee * @alias module:@aeternity/aepp-sdk/es/tx/tx-object * @param {Object} props * @return {String} fee */ calculateMinFee: function calculateMinFee() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var params = _objectSpread(_objectSpread({}, this.params), props); return calculateFee(0, this.type, { gas: params.gas, params: params, vsn: params.vsn }); } } }); export default TxObject; //# sourceMappingURL=tx-object.js.map