UNPKG

@aeternity/aepp-sdk

Version:
313 lines (274 loc) 11.7 kB
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty"; import _merge from "ramda/src/merge"; import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator"; 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 _context4; _forEachInstanceProperty(_context4 = ownKeys(Object(source), true)).call(_context4, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context5; _forEachInstanceProperty(_context5 = ownKeys(Object(source))).call(_context5, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; } import _Object$freeze from "@babel/runtime-corejs3/core-js-stable/object/freeze"; 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 _regeneratorRuntime from "@babel/runtime-corejs3/regenerator"; import { ContractAPI } from '../../ae/contract'; import { TX_TYPE, PROTOCOL_VERSIONS } from '../../tx/builder/schema'; import { buildTx } from '../../tx/builder'; import { getContractAuthFan, prepareGaParams, wrapInEmptySignedTx } from './helpers'; import { assertedType, decodeBase64Check } from '../../utils/crypto'; /** * GeneralizeAccount Stamp * * Provide Generalize Account implementation * {@link module:@aeternity/aepp-sdk/es/contract/ga} clients. * @function * @alias module:@aeternity/aepp-sdk/es/contract/ga * @rtype Stamp * @param {Object} [options={}] - Initializer object * @return {Object} GeneralizeAccount instance * @example * const authContract = `` * await client.createGeneralizeAccount(authFnName, authContract, [...authFnArguments] * // Make spend using GA * const callData = 'cb_...' // encoded call data for auth contract * await client.spend(10000, receiverPub, { authData: { callData } }) * // or * await client.spend(10000, receiverPub, { authData: { source: authContract, args: [...authContractArgs] } }) // sdk will prepare callData itself */ export var GeneralizeAccount = ContractAPI.compose({ methods: { createGeneralizeAccount: createGeneralizeAccount, createMetaTx: createMetaTx, isGA: isGA } }); export default GeneralizeAccount; /** * @alias module:@aeternity/aepp-sdk/es/contract/ga * @function * Check if account is GA account * @param {String} address - Account address * @return {Boolean} */ function isGA(_x) { return _isGA.apply(this, arguments); } /** * Convert current account to GA account * @alias module:@aeternity/aepp-sdk/es/contract/ga * @function * @param {String} authFnName - Authorization function name * @param {String} source - Auth contract source code * @param {Array} [args] - init arguments * @param {Object} [options] - Options * @return {Promise<Readonly<{result: *, owner: *, createdAt: Date, address, rawTx: *, transaction: *}>>} */ function _isGA() { _isGA = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(address) { var _yield$this$getAccoun, contractId; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return this.getAccount(address); case 2: _yield$this$getAccoun = _context.sent; contractId = _yield$this$getAccoun.contractId; return _context.abrupt("return", !!contractId); case 5: case "end": return _context.stop(); } } }, _callee, this); })); return _isGA.apply(this, arguments); } function createGeneralizeAccount(_x2, _x3) { return _createGeneralizeAccount.apply(this, arguments); } /** * Create a metaTx transaction * @alias module:@aeternity/aepp-sdk/es/contract/ga * @function * @param {String} rawTransaction Inner transaction * @param {Object} authData Object with gaMeta params * @param {String} authFnName - Authorization function name * @param {Object} options - Options * @return {String} */ function _createGeneralizeAccount() { _createGeneralizeAccount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(authFnName, source) { var args, options, opt, ownerId, _yield$getContractAut, authFun, bytecode, callData, _yield$this$gaAttachT, tx, contractId, _yield$this$send, hash, rawTx, _args2 = arguments; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: args = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : []; options = _args2.length > 3 && _args2[3] !== undefined ? _args2[3] : {}; opt = _merge(this.Ae.defaults, options); _context2.next = 5; return this.address(opt); case 5: ownerId = _context2.sent; _context2.next = 8; return this.isGA(ownerId); case 8: if (!_context2.sent) { _context2.next = 10; break; } throw new Error("Account ".concat(ownerId, " is already GA")); case 10: _context2.next = 12; return getContractAuthFan(this)(source, authFnName); case 12: _yield$getContractAut = _context2.sent; authFun = _yield$getContractAut.authFun; bytecode = _yield$getContractAut.bytecode; _context2.next = 17; return this.contractEncodeCall(source, 'init', args); case 17: callData = _context2.sent; _context2.next = 20; return this.gaAttachTx(_merge(opt, { ownerId: ownerId, code: bytecode, callData: callData, authFun: authFun })); case 20: _yield$this$gaAttachT = _context2.sent; tx = _yield$this$gaAttachT.tx; contractId = _yield$this$gaAttachT.contractId; _context2.next = 25; return this.send(tx, opt); case 25: _yield$this$send = _context2.sent; hash = _yield$this$send.hash; rawTx = _yield$this$send.rawTx; return _context2.abrupt("return", _Object$freeze({ owner: ownerId, transaction: hash, rawTx: rawTx, gaContractId: contractId })); case 29: case "end": return _context2.stop(); } } }, _callee2, this); })); return _createGeneralizeAccount.apply(this, arguments); } function createMetaTx(_x4, _x5, _x6) { return _createMetaTx.apply(this, arguments); } function _createMetaTx() { _createMetaTx = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(rawTransaction, authData, authFnName) { var options, _yield$prepareGaParam, authCallData, gas, opt, rlpBinaryTx, _wrapInEmptySignedTx, rlpEncoded, _yield$this$getVmVers, abiVersion, params, _yield$this$prepareTx, fee, ttl, _buildTx, metaTxRlp, _wrapInEmptySignedTx2, tx, _args3 = arguments; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: options = _args3.length > 3 && _args3[3] !== undefined ? _args3[3] : {}; if (authData) { _context3.next = 3; break; } throw new Error('authData is required'); case 3: _context3.next = 5; return prepareGaParams(this)(authData, authFnName); case 5: _yield$prepareGaParam = _context3.sent; authCallData = _yield$prepareGaParam.authCallData; gas = _yield$prepareGaParam.gas; opt = _merge(this.Ae.defaults, options); // Get transaction rlp binary rlpBinaryTx = decodeBase64Check(assertedType(rawTransaction, 'tx')); // Wrap in SIGNED tx with empty signatures _wrapInEmptySignedTx = wrapInEmptySignedTx(rlpBinaryTx), rlpEncoded = _wrapInEmptySignedTx.rlpEncoded; // Get abi _context3.next = 13; return this.getVmVersion(TX_TYPE.contractCall); case 13: _yield$this$getVmVers = _context3.sent; abiVersion = _yield$this$getVmVers.abiVersion; _context3.t0 = _objectSpread; _context3.t1 = _objectSpread({}, opt); _context3.t2 = {}; _context3.t3 = rlpEncoded; _context3.next = 21; return this.address(opt); case 21: _context3.t4 = _context3.sent; _context3.t5 = abiVersion; _context3.t6 = authCallData; _context3.t7 = gas; _context3.t8 = { tx: _context3.t3, gaId: _context3.t4, abiVersion: _context3.t5, authData: _context3.t6, gas: _context3.t7 }; params = (0, _context3.t0)(_context3.t1, _context3.t2, _context3.t8); _context3.next = 29; return this.prepareTxParams(TX_TYPE.gaMeta, params); case 29: _yield$this$prepareTx = _context3.sent; fee = _yield$this$prepareTx.fee; ttl = _yield$this$prepareTx.ttl; // Build META tx _buildTx = buildTx(_objectSpread(_objectSpread({}, params), {}, { fee: "".concat(fee), ttl: ttl }), TX_TYPE.gaMeta, { vsn: this.getNodeInfo().consensusProtocolVersion === PROTOCOL_VERSIONS.IRIS ? 2 : 1 }), metaTxRlp = _buildTx.rlpEncoded; // Wrap in empty signed tx _wrapInEmptySignedTx2 = wrapInEmptySignedTx(metaTxRlp), tx = _wrapInEmptySignedTx2.tx; // Send tx to the chain return _context3.abrupt("return", tx); case 35: case "end": return _context3.stop(); } } }, _callee3, this); })); return _createMetaTx.apply(this, arguments); } //# sourceMappingURL=index.js.map