@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
255 lines (228 loc) • 9.39 kB
JavaScript
import _typeof from "@babel/runtime-corejs3/helpers/typeof";
import _fromPairs from "ramda/src/fromPairs";
import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
import _Promise from "@babel/runtime-corejs3/core-js-stable/promise";
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
import AsyncInit from '../utils/async-init';
import MemoryAccount from './memory';
import { assertedType } from '../utils/crypto';
import AccountBase, { isAccountBase } from './base';
/**
* AccountMultiple stamp
*
* The purpose of this stamp is to wrap up implementations of
* {@link module:@aeternity/aepp-sdk/es/account/base--AccountBase} objects and provide a
* common interface to all of them. List are a substantial part of
* {@link module:@aeternity/aepp-sdk/es/ae/wallet--Wallet}s.
* @function
* @alias module:@aeternity/aepp-sdk/es/accounts/multiple
* @rtype Stamp
* @param {Object} [options={}] - Initializer object
* @param {Array} [options.accounts] - Accounts array
* @param {String} [options.address] - Address of account to select
* @return {Object} AccountMultiple instance
* @example
* const accounts = await AccountMultiple({ accounts: [ MemmoryAccount({ keypair: 'keypair_object' }) ] })
* await accounts.addAccount(account, { select: true }) // Add account and make it selected
* accounts.removeAccount(address) // Remove account
* accounts.selectAccount(address) // Select account
* accounts.addresses() // Get available accounts
*/
export default AccountBase.compose(AsyncInit, {
init: function init(_ref) {
var _this = this;
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var _ref$accounts, accounts, address;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_ref$accounts = _ref.accounts, accounts = _ref$accounts === void 0 ? [] : _ref$accounts, address = _ref.address;
_context2.t0 = _fromPairs;
_context2.next = 4;
return _Promise.all(_mapInstanceProperty(accounts).call(accounts, /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(a) {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return a.address();
case 2:
_context.t0 = _context.sent;
_context.t1 = a;
return _context.abrupt("return", [_context.t0, _context.t1]);
case 5:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function (_x) {
return _ref2.apply(this, arguments);
};
}()));
case 4:
_context2.t1 = _context2.sent;
_this.accounts = (0, _context2.t0)(_context2.t1);
if (!address) address = _Object$keys(_this.accounts)[0];
assertedType(address, 'ak');
_this.selectedAddress = address;
case 9:
case "end":
return _context2.stop();
}
}
}, _callee2);
}))();
},
props: {
accounts: {}
},
deepProps: {
selectedAddress: null
},
methods: {
address: function address() {
var _arguments = arguments,
_this2 = this;
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
var _ref3, _ref3$onAccount, onAccount;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_ref3 = _arguments.length > 0 && _arguments[0] !== undefined ? _arguments[0] : {}, _ref3$onAccount = _ref3.onAccount, onAccount = _ref3$onAccount === void 0 ? _this2.selectedAddress : _ref3$onAccount;
return _context3.abrupt("return", _this2._resolveAccount(onAccount).address());
case 2:
case "end":
return _context3.stop();
}
}
}, _callee3);
}))();
},
sign: function sign(data) {
var _arguments2 = arguments,
_this3 = this;
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
var _ref4, _ref4$onAccount, onAccount;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_ref4 = _arguments2.length > 1 && _arguments2[1] !== undefined ? _arguments2[1] : {}, _ref4$onAccount = _ref4.onAccount, onAccount = _ref4$onAccount === void 0 ? _this3.selectedAddress : _ref4$onAccount;
return _context4.abrupt("return", _this3._resolveAccount(onAccount).sign(data));
case 2:
case "end":
return _context4.stop();
}
}
}, _callee4);
}))();
},
/**
* Get accounts addresses
* @alias module:@aeternity/aepp-sdk/es/accounts/multiple
* @function
* @rtype () => String[]
* @return {String[]}
* @example addresses()
*/
addresses: function addresses() {
return _Object$keys(this.accounts);
},
/**
* Add specific account
* @alias module:@aeternity/aepp-sdk/es/accounts/multiple
* @function
* @category async
* @rtype (account: Account, { select: Boolean }) => void
* @param {Object} account - Account instance
* @param {Object} [options={}] - Options
* @param {Boolean} [options.select] - Select account
* @return {void}
* @example addAccount(account)
*/
addAccount: function addAccount(account) {
var _arguments3 = arguments,
_this4 = this;
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
var _ref5, select, address;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_ref5 = _arguments3.length > 1 && _arguments3[1] !== undefined ? _arguments3[1] : {}, select = _ref5.select;
_context5.next = 3;
return account.address();
case 3:
address = _context5.sent;
_this4.accounts[address] = account;
if (select) _this4.selectAccount(address);
case 6:
case "end":
return _context5.stop();
}
}
}, _callee5);
}))();
},
/**
* Remove specific account
* @alias module:@aeternity/aepp-sdk/es/accounts/multiple
* @function
* @rtype (address: String) => void
* @param {String} address - Address of account to remove
* @return {void}
* @example removeAccount(address)
*/
removeAccount: function removeAccount(address) {
if (!this.accounts[address]) {
console.warn("removeAccount: Account for ".concat(address, " not available"));
return;
}
delete this.accounts[address];
if (this.selectedAddress === address) this.selectedAddress = null;
},
/**
* Select specific account
* @alias module:@aeternity/aepp-sdk/es/account/selector
* @instance
* @rtype (address: String) => void
* @param {String} address - Address of account to select
* @example selectAccount('ak_xxxxxxxx')
*/
selectAccount: function selectAccount(address) {
assertedType(address, 'ak');
if (!this.accounts[address]) throw new Error("Account for ".concat(address, " not available"));
this.selectedAddress = address;
},
/**
* Resolves an account
* @param account can be account address (should exist in sdk instance), MemoryAccount or keypair
* @returns {AccountBase}
* @private
*/
_resolveAccount: function _resolveAccount(account) {
var _context6;
switch (_typeof(account)) {
case 'string':
assertedType(account, 'ak');
if (!this.accounts[account]) throw new Error("Account for ".concat(account, " not available"));
return this.accounts[account];
case 'object':
return isAccountBase(account) ? account : MemoryAccount({
keypair: account
});
default:
throw new Error(_concatInstanceProperty(_context6 = "Unknown account type: ".concat(_typeof(account), " (account: ")).call(_context6, account, ")"));
}
}
}
});
//# sourceMappingURL=multiple.js.map