stellar-plus
Version:
beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain
101 lines (100 loc) • 3.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountBase = void 0;
const tslib_1 = require("tslib");
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const axios_1 = tslib_1.__importDefault(require("axios"));
const errors_1 = require("../../../stellar-plus/account/base/errors");
const horizon_1 = require("../../../stellar-plus/horizon");
class AccountBase {
/**
*
* @args {} payload - The payload for the account. Additional parameters may be provided to enable different helpers.
* @param {string} payload.publicKey The public key of the account.
* @param {NetworkConfig=} payload.networkConfig The network config for the target network.
*
* @description - The base account is used for handling accounts with no management actions.
*/
constructor(payload) {
const { publicKey, networkConfig, horizonHandler } = payload;
this.publicKey = publicKey;
this.networkConfig = networkConfig;
this.horizonHandler = horizonHandler;
if (this.networkConfig && !this.horizonHandler) {
this.horizonHandler = new horizon_1.HorizonHandlerClient(this.networkConfig);
}
}
/**
*
* @returns {string} The public key of the account.
*
*/
getPublicKey() {
return this.publicKey;
}
/**
*
* @returns {void}
* @description - Initialize the account with the friendbot and funds it with 10.000 XLM.
*/
initializeWithFriendbot() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this.requireTestNetwork();
try {
yield axios_1.default.get(`${this.networkConfig.friendbotUrl}?addr=${encodeURIComponent(this.publicKey)}` // friendbot URL in networkConfig validated in requireTestNetwork()
);
return;
}
catch (e) {
throw errors_1.ABError.failedToCreateAccountWithFriendbotError(e);
}
});
}
/**
*
* @returns {Horizon.BalanceLine[]} A list of the account's balances.
* @description - The account's balances are retrieved from the Horizon server and provided in a list, including all assets.
*/
getBalances() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this.requireHorizonHandler();
try {
const account = yield this.horizonHandler.loadAccount(this.publicKey); // Horizon handler validated in requireHorizonHandler()
return account.balances;
}
catch (error) {
throw errors_1.ABError.failedToLoadBalances(error);
}
});
}
/**
*
* @param {Buffer} data - The data to sign.
* @param {Buffer} signature - The signature to verify.
* @returns {boolean} True if the signature is valid, false otherwise.
*/
verifySignature(data, signature) {
const keypair = stellar_sdk_1.Keypair.fromPublicKey(this.publicKey);
return keypair.verify(data, signature);
}
/**
*
* @description - Throws an error if the network is not a test network.
*/
requireTestNetwork() {
var _a;
if (!((_a = this.networkConfig) === null || _a === void 0 ? void 0 : _a.friendbotUrl)) {
throw errors_1.ABError.friendbotNotAvailableError();
}
}
/**
*
* @description - Throws an error if the horizon handler is not set
*/
requireHorizonHandler() {
if (!this.horizonHandler) {
throw errors_1.ABError.horizonHandlerNotAvailableError();
}
}
}
exports.AccountBase = AccountBase;