UNPKG

xdb-digitalbits-base

Version:
285 lines (236 loc) 9.22 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.MuxedAccount = exports.Account = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _isString = require('lodash/isString'); var _isString2 = _interopRequireDefault(_isString); var _bignumber = require('bignumber.js'); var _bignumber2 = _interopRequireDefault(_bignumber); var _digitalbitsXdr_generated = require('./generated/digitalbits-xdr_generated'); var _digitalbitsXdr_generated2 = _interopRequireDefault(_digitalbitsXdr_generated); var _strkey = require('./strkey'); var _decode_encode_muxed_account = require('./util/decode_encode_muxed_account'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Create a new Account object. * * `Account` represents a single account in the DigitalBits network and its sequence * number. Account tracks the sequence number as it is used by {@link * TransactionBuilder}. See * [Accounts](https://developers.digitalbits.io/guides/concepts/accounts.html) for * more information about how accounts work in DigitalBits. * * @constructor * * @param {string} accountId - ID of the account (ex. * `GB3KJPLFUYN5VL6R3GU3EGCGVCKFDSD7BEDX42HWG5BWFKB3KQGJJRMA`). If you * provide a muxed account address, this will throw; use {@link * MuxedAccount} instead. * @param {string} sequence - current sequence number of the account */ var Account = exports.Account = function () { function Account(accountId, sequence) { _classCallCheck(this, Account); if (_strkey.StrKey.isValidMed25519PublicKey(accountId)) { throw new Error('accountId is an M-address; use MuxedAccount instead'); } if (!_strkey.StrKey.isValidEd25519PublicKey(accountId)) { throw new Error('accountId is invalid'); } if (!(0, _isString2.default)(sequence)) { throw new Error('sequence must be of type string'); } this._accountId = accountId; this.sequence = new _bignumber2.default(sequence); } /** * Returns DigitalBits account ID, ex. * `GB3KJPLFUYN5VL6R3GU3EGCGVCKFDSD7BEDX42HWG5BWFKB3KQGJJRMA`. * @returns {string} */ _createClass(Account, [{ key: 'accountId', value: function accountId() { return this._accountId; } /** * @returns {string} sequence number for the account as a string */ }, { key: 'sequenceNumber', value: function sequenceNumber() { return this.sequence.toString(); } /** * Increments sequence number in this object by one. * @returns {void} */ }, { key: 'incrementSequenceNumber', value: function incrementSequenceNumber() { this.sequence = this.sequence.add(1); } /** * Creates a muxed "sub"account with this base address and an ID set. * * @param {string} id - the ID of the new muxed account * @return {MuxedAccount} a new instance w/ the specified parameters * * @see MuxedAccount */ }, { key: 'createSubaccount', value: function createSubaccount(id) { return new MuxedAccount(this, id); } }]); return Account; }(); /** * Represents a muxed account for transactions and operations. * * A muxed (or *multiplexed*) account (defined rigorously in * CAP-27 and briefly in * SEP-23 is one that resolves a single * DigitalBits `G...`` account to many different underlying IDs. * * For example, you may have a single DigitalBits address for accounting purposes: * GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ * * Yet would like to use it for 4 different family members: * 1: MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAAGZFQ * 2: MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAALIWQ * 3: MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAAPYHQ * 4: MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAAQLQQ * * This object makes it easy to create muxed accounts from regular accounts, * duplicate them, retrieve the underlying IDs, etc. without mucking around with * the raw XDR. * * Because muxed accounts are purely an off-chain convention, they all share the * sequence number tied to the underlying G... account. Thus, this object * *requires* an {@link Account} instance to be passed in, so that muxed * instances of an account can collectively modify the sequence number whenever * a muxed account is used as the source of a @{link Transaction} with {@link * TransactionBuilder}. * * @constructor * * @param {Account} account - the @{link Account} instance representing the * underlying G... address * @param {string} id - a stringified uint64 value that represents the * ID of the muxed account */ var MuxedAccount = exports.MuxedAccount = function () { function MuxedAccount(baseAccount, id) { _classCallCheck(this, MuxedAccount); var accountId = baseAccount.accountId(); if (!_strkey.StrKey.isValidEd25519PublicKey(accountId)) { throw new Error('accountId is invalid'); } this.account = baseAccount; this._muxedXdr = (0, _decode_encode_muxed_account.decodeAddressToMuxedAccount)(accountId, true); this._mAddress = (0, _decode_encode_muxed_account.encodeMuxedAccountToAddress)(this._muxedXdr, true); this.setId(id); } /** * Parses an M-address into a MuxedAccount object. * * @param {string} mAddress - an M-address to transform * @param {string} sequenceNum - the sequence number of the underlying {@link * Account}, to use for the underlying base account (@link * MuxedAccount.baseAccount). If you're using the SDK, you can use * `server.loadAccount` to fetch this if you don't know it. * * @return {MuxedAccount} */ _createClass(MuxedAccount, [{ key: 'baseAccount', /** * @return {Account} the underlying account object shared among all muxed * accounts with this DigitalBits address */ value: function baseAccount() { return this.account; } /** * @return {string} the M-address representing this account's (G-address, ID) */ }, { key: 'accountId', value: function accountId() { return this._mAddress; } }, { key: 'id', value: function id() { return this._id; } }, { key: 'setId', value: function setId(id) { if (!(0, _isString2.default)(id)) { throw new Error('id should be a string representing a number (uint64)'); } this._muxedXdr.med25519().id(_digitalbitsXdr_generated2.default.Uint64.fromString(id)); this._mAddress = (0, _decode_encode_muxed_account.encodeMuxedAccountToAddress)(this._muxedXdr, true); this._id = id; return this; } /** * Accesses the underlying account's sequence number. * @return {string} strigified sequence number for the underlying account */ }, { key: 'sequenceNumber', value: function sequenceNumber() { return this.account.sequenceNumber(); } /** * Increments the underlying account's sequence number by one. * @return {void} */ }, { key: 'incrementSequenceNumber', value: function incrementSequenceNumber() { return this.account.incrementSequenceNumber(); } /** * Creates another muxed "sub"account from the base with a new ID set * * @param {string} id - the ID of the new muxed account * @return {MuxedAccount} a new instance w/ the specified parameters */ }, { key: 'createSubaccount', value: function createSubaccount(id) { return new MuxedAccount(this.account, id); } /** * @return {xdr.MuxedAccount} the XDR object representing this muxed account's * G-address and uint64 ID */ }, { key: 'toXDRObject', value: function toXDRObject() { return this._muxedXdr; } }, { key: 'equals', value: function equals(otherMuxedAccount) { return this.accountId() === otherMuxedAccount.accountId(); } }], [{ key: 'fromAddress', value: function fromAddress(mAddress, sequenceNum) { var muxedAccount = (0, _decode_encode_muxed_account.decodeAddressToMuxedAccount)(mAddress, true); var gAddress = (0, _decode_encode_muxed_account.encodeMuxedAccountToAddress)(muxedAccount, false); var id = muxedAccount.med25519().id().toString(); return new MuxedAccount(new Account(gAddress, sequenceNum), id); } }]); return MuxedAccount; }();