@stoar/cli
Version:
CLI tool for STOAR - decentralized file storage on Arweave
62 lines • 2.3 kB
JavaScript
import { readFileSync } from 'fs';
import { StoarClient } from '@stoar/sdk';
import { getArweave } from './arweave.js';
export async function getWalletConfig(options) {
let wallet;
// Check for wallet in options first, then environment variable
const walletPath = options.wallet || process.env.STOAR_WALLET_PATH;
const walletBase64 = process.env.STOAR_WALLET;
if (walletPath) {
try {
const walletData = readFileSync(walletPath, 'utf-8');
wallet = JSON.parse(walletData);
}
catch (error) {
throw new Error(`Failed to read wallet file: ${error.message}`);
}
}
else if (walletBase64) {
try {
const walletData = Buffer.from(walletBase64, 'base64').toString('utf-8');
wallet = JSON.parse(walletData);
}
catch (error) {
throw new Error(`Failed to decode wallet from STOAR_WALLET env var: ${error.message}`);
}
}
else {
throw new Error('No wallet configured. Use --wallet flag or set STOAR_WALLET/STOAR_WALLET_PATH environment variable');
}
// Create Arweave instance
const Arweave = await getArweave();
const arweave = Arweave.init({
host: new URL(options.gateway || 'https://arweave.net').hostname,
port: 443,
protocol: 'https'
});
// Create client with arweave instance
const client = new StoarClient({
arweave,
gateway: options.gateway || 'https://arweave.net'
});
// Initialize client with wallet
await client.init(wallet);
return { wallet, arweave, client };
}
export function formatArweaveAmount(winston) {
// The SDK already returns the value in AR, not winston
const ar = parseFloat(winston);
return `${ar.toFixed(6)} AR`;
}
export async function checkBalance(client, requiredAmount) {
const balance = await client.getBalance();
if (requiredAmount) {
const balanceNum = parseFloat(balance);
const requiredNum = parseFloat(requiredAmount);
if (balanceNum < requiredNum) {
throw new Error(`Insufficient balance. Current: ${formatArweaveAmount(balance)}, Required: ${formatArweaveAmount(requiredAmount)}`);
}
}
return balance;
}
//# sourceMappingURL=wallet.js.map