stellar-plus
Version:
beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain
110 lines (109 loc) • 4.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultAccountHandlerClient = void 0;
const tslib_1 = require("tslib");
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const errors_1 = require("../../../../stellar-plus/account/account-handler/default/errors");
const base_1 = require("../../../../stellar-plus/account/base");
/**
* @class DefaultAccountHandlerClient
* @extends AccountBase
* @implements DefaultAccountHandler
* @description - The default account handler is used for handling and creating new accounts by directly manipulating the secret key. Avoid using this handler in production environments.
*/
class DefaultAccountHandlerClient extends base_1.AccountBase {
/**
*
* @args payload - The payload for the account handler. Additional parameters may be provided to enable different helpers.
* @param {string=} payload.secretKey The secret key of the account. If not provided, a new random account will be created.
* @param {NetworkConfig} payload.networkConfig The network to use.
* @description - The default account handler is used for handling and creating new accounts by directly manipulating the secret key.
*/
constructor(payload) {
const secretKey = payload === null || payload === void 0 ? void 0 : payload.secretKey;
try {
const keypair = secretKey ? stellar_sdk_1.Keypair.fromSecret(secretKey) : stellar_sdk_1.Keypair.random();
const publicKey = keypair.publicKey();
super(Object.assign(Object.assign({}, payload), { publicKey }));
this.secretKey = keypair.secret();
}
catch (e) {
throw errors_1.DAHError.failedToLoadSecretKeyError(e);
}
}
/**
*
* @returns {string} The public key of the account.
*/
getPublicKey() {
try {
return stellar_sdk_1.Keypair.fromSecret(this.secretKey).publicKey();
}
catch (e) {
throw errors_1.DAHError.failedToLoadSecretKeyError(e);
}
}
/**
* @description - Returns the secret key of the account.
*
* @returns {string} The secret key of the account.
*/
getSecretKey() {
return this.secretKey;
}
/**
*
* @param {Transaction} tx - The transaction to sign.
*
* @description - Signs the given transaction with the account's secret key.
*
* @returns {TransactionXdr} The signed transaction in xdr format.
*/
sign(tx) {
try {
const keypair = stellar_sdk_1.Keypair.fromSecret(this.secretKey);
tx.sign(keypair);
return tx.toXDR();
}
catch (e) {
throw errors_1.DAHError.failedToSignTransactionError(e);
}
}
/**
*
* @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.
*
* @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* () {
try {
const keypair = stellar_sdk_1.Keypair.fromSecret(this.secretKey);
const signedEntry = yield (0, stellar_sdk_1.authorizeEntry)(entry, keypair, validUntilLedgerSeq, networkPassphrase); // Passphrase is necessary! Cannot be removed!
return signedEntry;
}
catch (e) {
throw errors_1.DAHError.failedToSignAuthorizationEntryError(e, entry.toXDR('base64'), validUntilLedgerSeq, networkPassphrase);
}
});
}
/**
*
* @param {Buffer} data - The data to sign.
* @returns {Buffer} The signature of the data.
*/
signData(data) {
try {
const keypair = stellar_sdk_1.Keypair.fromSecret(this.secretKey);
return keypair.sign(data);
}
catch (e) {
throw errors_1.DAHError.failedToSignDataError(e);
}
}
}
exports.DefaultAccountHandlerClient = DefaultAccountHandlerClient;