2025-06-22 00:21:09 +03:00

115 lines
4.5 KiB
JavaScript

import { e as error } from "./index.js";
import { g as get_store_value } from "./utils.js";
import { p as persisted } from "./index3.js";
import { TransactionBuilder } from "@stellar/stellar-sdk";
import { B as BROWSER } from "./false.js";
const browser = BROWSER;
function createWalletStore() {
const { subscribe, set } = persisted("bpa:walletStore", { keyId: "", publicKey: "" });
return {
subscribe,
/**
* Connects a user by their public key (wallet-based registration)
* @param {Object} opts Options object
* @param {string} opts.publicKey Public Stellar address
*/
connectWallet: async ({ publicKey }) => {
try {
set({
keyId: publicKey,
publicKey
});
} catch (err) {
console.error("Error connecting wallet", err);
throw error(400, { message: "Failed to connect wallet" });
}
},
/**
* Registers a user by storing their encrypted keypair in the browser's localStorage.
* @param {Object} opts Options object
* @param {string} opts.publicKey Public Stellar address which will be the user's public key throughout the application
* @param {string} opts.secretKey Secret key that corresponds to the user's public key
* @param {string} opts.pincode Pincode that will be used to encrypt this keypair
* @throws Will throw an error if there is a problem encrypting and/or storing the keypair
*/
register: async ({ publicKey, secretKey, pincode }) => {
{
throw error(400, { message: "Registration only available in browser" });
}
},
/**
* Compares a submitted pincode to make sure it is valid for the stored, encrypted keypair.
* @param {Object} opts Options object
* @param {string} opts.pincode Pincode being confirmed against existing stored wallet
* @param {string} [opts.firstPincode] On signup, the pincode that is being matched against
* @param {boolean} [opts.signup=false] Whether or not the confirmation is for the initial signup
* @throws Will throw an error if the signup pincodes don't match, or if the provided pincode doesn't decrypt the keypair.
*/
confirmPincode: async ({ pincode, firstPincode = "", signup = false }) => {
if (!signup) {
{
throw error(400, { message: "Pincode verification only available in browser" });
}
} else {
if (pincode !== firstPincode) {
throw error(400, { message: "pincode mismatch" });
}
}
},
/**
* Sign and return a Stellar transaction
* @param {Object} opts Options object
* @param {string} opts.transactionXDR A Stellar transaction in base64-encoded XDR format
* @param {string} opts.network Network passphrase for the network this transaction is intended for
* @param {string} opts.pincode Pincode to be used as the encryption password for the keypair
* @returns {Promise<Transaction>} A signed Stellar transaction ready to submit to the network
* @throws Will throw an error if there is a problem signing the transaction.
*/
sign: async ({ transactionXDR, network, pincode }) => {
try {
const { keyId, publicKey } = get_store_value(walletStore);
if (keyId === publicKey && browser) {
const {
StellarWalletsKit,
allowAllModules,
XBULL_ID
} = await import("@creit.tech/stellar-wallets-kit");
const kit = new StellarWalletsKit({
// @ts-ignore
network,
selectedWalletId: XBULL_ID,
modules: allowAllModules()
});
const { address } = await kit.getAddress();
const { signedTxXdr } = await kit.signTransaction(transactionXDR, {
address,
networkPassphrase: network
});
return signedTxXdr;
} else {
const keyManager = await setupKeyManager();
const signedTransaction = await keyManager.signTransaction({
// @ts-ignore
transaction: TransactionBuilder.fromXDR(transactionXDR, network),
id: keyId,
password: pincode
});
return signedTransaction;
}
} catch (err) {
console.error("Error signing transaction", err);
throw error(400, { message: err.toString() });
}
}
};
}
const walletStore = createWalletStore();
const setupKeyManager = async () => {
{
throw new Error("Key manager only available in browser");
}
};
export {
walletStore as w
};