stellar-plus
Version:
beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain
219 lines (218 loc) • 9.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FreighterAccountHandlerClient = void 0;
const tslib_1 = require("tslib");
const freighter_api_1 = require("@stellar/freighter-api");
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const errors_1 = require("../../../../stellar-plus/account/account-handler/freighter/errors");
const base_1 = require("../../../../stellar-plus/account/base");
class FreighterAccountHandlerClient extends base_1.AccountBase {
/**
*
* @args payload - The payload for the Freighter account handler. Additional parameters may be provided to enable different helpers.
*
* @param {NetworkConfig} payload.networkConfig The network to use.
*
* @description - The Freighter account handler is used for handling and creating new accounts by integrating with the browser extension Freighter App.
*/
constructor(payload) {
const { networkConfig } = payload;
const publicKey = '';
super(Object.assign(Object.assign({}, payload), { publicKey }));
this.networkConfig = networkConfig;
this.publicKey = '';
}
/**
*
* @returns {string} The public key of the account.
*/
getPublicKey() {
return this.publicKey;
}
/**
*
* @param {function(string): void} onPublicKeyReceived - The callback to be called with the public key if successful.
*
* @returns {Promise<void>}
*
* @description - Perform all necessary verification to connect to Freighter and trigger the connection, calling the callback with the public key if successful.
*/
connect(onPublicKeyReceived) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
yield this.loadPublicKey(onPublicKeyReceived, true);
});
}
/**
* @returns {void}
*
* @description - Disconnect from Freighter.
*/
disconnect() {
this.publicKey = '';
}
/**
*
* @param {function(string):void} onPublicKeyReceived - The callback to be called with the public key if successful.
* @param {boolean} enforceConnection - If true, it will perform all necessary verification to connect to Freighter and trigger the connection. Defaults to false.
*
* @returns {Promise<void>}
*
* @description - Get the public key from Freighter and call the callback with the public key if successful. When enforceConnection is true, it will perform all necessary verification to connect to Freighter and trigger the connection.
*
* */
loadPublicKey(onPublicKeyReceived, enforceConnection) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const isFreighterConnected = yield this.isFreighterConnected(enforceConnection, onPublicKeyReceived);
if (isFreighterConnected) {
try {
const publicKey = yield (0, freighter_api_1.getPublicKey)();
this.publicKey = publicKey;
if (onPublicKeyReceived) {
return yield onPublicKeyReceived(publicKey);
}
}
catch (e) {
throw errors_1.FAHError.failedToLoadPublicKeyError(e);
}
}
});
}
/**
*
* @param {Transaction} tx - The transaction to sign.
*
* @returns {Promise<string>
* }
* @description - Sign a transaction with Freighter and return the signed transaction. If signerpublicKey is provided, it will be used to specifically request Freighter to sign with that account.
*
*/
sign(tx) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const isFreighterConnected = yield this.isFreighterConnected(true);
if (isFreighterConnected) {
try {
const txXDR = tx.toXDR();
const signedTx = yield (0, freighter_api_1.signTransaction)(txXDR, {
networkPassphrase: this.networkConfig.networkPassphrase,
accountToSign: this.publicKey,
});
return signedTx;
}
catch (e) {
throw errors_1.FAHError.failedToSignTransactionError(e);
}
}
else {
this.connect();
throw errors_1.FAHError.freighterIsNotConnectedError();
}
});
}
/**
*
* @param {xdr.SorobanAuthorizationEntry} entry - The soroban authorization entry to sign.
* @param {number} validUntilLedgerSeq - The ledger sequence number until which the entry signature is valid.
* @param {string} networkPassphrase - The network passphrase for the network to sign the entry for.
* @description - Signs the given Soroban authorization entry with the account's secret key.
*
* @returns {xdr.SorobanAuthorizationEntry} The signed entry.
*/
signSorobanAuthEntry(entry, validUntilLedgerSeq, networkPassphrase) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const isFreighterConnected = yield this.isFreighterConnected(true);
if (isFreighterConnected) {
if (networkPassphrase !== this.networkConfig.networkPassphrase) {
throw errors_1.FAHError.cannotSignForThisNetwork(networkPassphrase, this.networkConfig.networkPassphrase);
}
try {
const signedEntryXdr = yield (0, freighter_api_1.signAuthEntry)(entry.toXDR('base64'), { accountToSign: this.publicKey });
const signedEntry = stellar_sdk_1.xdr.SorobanAuthorizationEntry.fromXDR(signedEntryXdr, 'base64');
return signedEntry;
}
catch (e) {
throw errors_1.FAHError.failedToSignAuthEntryError(e);
}
}
else {
this.connect();
throw errors_1.FAHError.freighterIsNotConnectedError();
}
});
}
/**
*
* @param {boolean} enforceConnection - If true, it will perform all necessary verification to connect to Freighter and trigger the connection. Defaults to false.
* @param {function(string):void} callback - The callback to be called with the public key if successful.
*
* @returns {Promise<boolean>}
*
* @description - Perform all necessary verification to connect to Freighter. If enforceConnection is true, it will trigger the connection and call the callback with the public key if successful.
*
*/
isFreighterConnected(enforceConnection, callback) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const isFreighterInstalled = yield this.isFreighterInstalled();
if (!isFreighterInstalled) {
return false;
}
const isApplicationAllowed = yield this.isApplicationAuthorized();
if (!isApplicationAllowed) {
if (enforceConnection) {
(0, freighter_api_1.setAllowed)().then(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (callback) {
yield this.loadPublicKey(callback);
}
}));
}
return false;
}
try {
yield this.isNetworkCorrect();
}
catch (error) {
return false;
}
return true;
});
}
/**
*
* @returns {Promise<boolean>}
* @description - Verify is Freighter extension is installed
*/
isFreighterInstalled() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const isFreighterConnected = yield (0, freighter_api_1.isConnected)();
return isFreighterConnected;
});
}
/**
*
* @returns {Promise<boolean>}
* @description - Verify if the application is authorized to connect to Freighter
*/
isApplicationAuthorized() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const isApplicationAllowed = yield (0, freighter_api_1.isAllowed)();
if (!isApplicationAllowed) {
return false;
}
return true;
});
}
/**
*
* @returns {Promise<boolean>}
* @description - Verify if the network selected on Freighter is the same as the network selected on this handler
*/
isNetworkCorrect() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const networkDetails = yield (0, freighter_api_1.getNetworkDetails)();
if (networkDetails.networkPassphrase !== this.networkConfig.networkPassphrase) {
throw errors_1.FAHError.connectedToWrongNetworkError(this.networkConfig.name);
}
return true;
});
}
}
exports.FreighterAccountHandlerClient = FreighterAccountHandlerClient;