Files
SHiNE-server/shine-UI/js/services/arweave-wallet-service.js
T

334 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { t } from '../i18n/index.js';
import { formatNumber } from '../i18n/format.js';
import { loadEncryptedUserSecrets, updateEncryptedUserSecrets } from './key-vault.js';
import { extractEd25519Seed32FromStoredValue } from './client-key-utils.js';
import { deriveArweaveWalletFromBlockchainKey32 } from './sawd-v1.js';
const DEFAULT_ARWEAVE_GATEWAY = 'https://arweave.net';
const AR_TOPUP_URL = 'https://changenow.io/exchange?from=usd&to=ar&amount=10&fiatMode=true';
const WINSTON_PER_AR = 1_000_000_000_000n;
let arweaveLibPromise = null;
function normalizeGateway(rawGateway) {
const raw = String(rawGateway || '').trim();
if (!raw) return DEFAULT_ARWEAVE_GATEWAY;
let parsed;
try {
parsed = new URL(raw);
} catch {
throw new Error(t('remaining.arweave_wallet_errors.invalid_arweave_gateway_url'));
}
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
throw new Error(t('remaining.arweave_wallet_errors.arweave_gateway_must_use_http_or_https'));
}
const normalized = `${parsed.protocol}//${parsed.host}${parsed.pathname}`.replace(/\/+$/g, '');
return normalized || DEFAULT_ARWEAVE_GATEWAY;
}
function parseGatewayForArweaveInit(gateway) {
let parsed;
try {
parsed = new URL(gateway);
} catch {
throw new Error(t('remaining.arweave_wallet_errors.invalid_arweave_gateway_url'));
}
const protocol = parsed.protocol.replace(':', '');
if (protocol !== 'http' && protocol !== 'https') {
throw new Error(t('remaining.arweave_wallet_errors.arweave_gateway_must_use_http_or_https'));
}
const port = parsed.port
? Number(parsed.port)
: (protocol === 'https' ? 443 : 80);
return {
protocol,
host: parsed.hostname,
port,
};
}
function parseArToWinston(amountAr) {
const raw = String(amountAr ?? '').trim().replace(',', '.');
const match = raw.match(/^(\d+)(?:\.(\d+))?$/);
if (!match) {
throw new Error(t('remaining.arweave_wallet.the_transfer_amount_must_be_a_number'));
}
const intPart = BigInt(match[1] || '0');
const frac = String(match[2] || '');
if (frac.length > 12) {
throw new Error(t('remaining.arweave_wallet.too_many_decimal_places_maximum_12'));
}
const fracPadded = `${frac}${'0'.repeat(12 - frac.length)}`;
const winston = (intPart * WINSTON_PER_AR) + BigInt(fracPadded || '0');
if (winston <= 0n) {
throw new Error(t('remaining.arweave_wallet.the_transfer_amount_must_be_greater_than_zero'));
}
return winston.toString();
}
async function loadArweaveLib() {
if (!arweaveLibPromise) {
arweaveLibPromise = import('https://esm.sh/arweave@1.15.7?bundle');
}
return arweaveLibPromise;
}
function pickCachedWallet(secrets) {
const cached = secrets?.arweaveWallet;
if (!cached || typeof cached !== 'object') return null;
const derivation = String(cached.derivation || '').trim();
const sourceKey = String(cached.sourceKey || '').trim();
const address = String(cached.address || '').trim();
const owner = String(cached.owner || '').trim();
const jwk = cached.jwk;
if (derivation !== 'SAWD-v1' || sourceKey !== 'blockchain' || !address || !owner || !jwk || typeof jwk !== 'object') {
return null;
}
if (!String(jwk.kty || '').trim() || !String(jwk.e || '').trim() || !String(jwk.n || '').trim() || !String(jwk.d || '').trim()) {
return null;
}
return {
derivation,
sourceKey,
address,
owner,
jwk,
};
}
function normalizeExtraWallets(secrets) {
const rows = Array.isArray(secrets?.arweaveWallets) ? secrets.arweaveWallets : [];
return rows
.filter((item) => item && typeof item === 'object')
.map((item) => ({
id: String(item.id || item.address || '').trim(),
label: String(item.label || '').trim(),
address: String(item.address || '').trim(),
owner: String(item.owner || '').trim(),
jwk: item.jwk,
createdAtMs: Number(item.createdAtMs || 0),
}))
.filter((item) => item.id && item.address && item.jwk && typeof item.jwk === 'object');
}
function parseJwkSecretString(secret) {
const raw = String(secret || '').trim();
if (!raw) throw new Error(t('remaining.arweave_wallet.enter_your_arweave_wallet_secret'));
const tryParse = (value) => {
const parsed = JSON.parse(value);
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new Error(t('remaining.arweave_wallet.invalid_arweave_jwk'));
}
return parsed;
};
if (raw.startsWith('{')) return tryParse(raw);
const normalized = raw
.replace(/-/g, '+')
.replace(/_/g, '/')
.padEnd(Math.ceil(raw.length / 4) * 4, '=');
try {
return tryParse(atob(normalized));
} catch {
throw new Error(t('remaining.arweave_wallet.the_secret_must_be_json_jwk_or_base64url_json_jwk'));
}
}
function validateArweaveJwk(jwk) {
if (!jwk || typeof jwk !== 'object') throw new Error(t('remaining.arweave_wallet.invalid_arweave_jwk'));
['kty', 'e', 'n', 'd', 'p', 'q', 'dp', 'dq', 'qi'].forEach((key) => {
if (!String(jwk[key] || '').trim()) throw new Error(`В JWK Arweave нет поля ${key}.`);
});
}
function safeStatus(onStatus, text) {
if (typeof onStatus !== 'function') return;
try {
onStatus(String(text || ''));
} catch {
// ignore callback errors
}
}
export async function getArweaveWalletChoices({ login, storagePwd, onStatus } = {}) {
const derived = await getArweaveWalletFromStoredBlockchainKey({ login, storagePwd, onStatus });
const secrets = await loadEncryptedUserSecrets(String(login || '').trim(), String(storagePwd || '').trim());
const extras = normalizeExtraWallets(secrets);
return [
{
id: 'derived-blockchain-key',
label: t('remaining.arweave_wallet.derived_from_blockchain_key'),
address: derived.address,
owner: derived.owner,
jwk: derived.jwk,
isDefault: true,
},
...extras.map((item) => ({
...item,
label: item.label || `Arweave ${item.address.slice(0, 6)}...${item.address.slice(-6)}`,
isDefault: false,
})),
];
}
export async function addArweaveWalletSecret({ login, storagePwd, secret, label = '' } = {}) {
const cleanLogin = String(login || '').trim();
const cleanPwd = String(storagePwd || '').trim();
if (!cleanLogin || !cleanPwd) throw new Error(t('remaining.arweave_wallet.no_active_session'));
const jwk = parseJwkSecretString(secret);
validateArweaveJwk(jwk);
const moduleRef = await loadArweaveLib();
const Arweave = moduleRef?.default || moduleRef;
const arweave = Arweave.init({ host: 'arweave.net', port: 443, protocol: 'https' });
const address = await arweave.wallets.jwkToAddress(jwk);
const owner = String(jwk.n || '').trim();
const id = `manual-${address}`;
const createdAtMs = Date.now();
const wallet = {
id,
label: String(label || '').trim() || `Arweave ${address.slice(0, 6)}...${address.slice(-6)}`,
address,
owner,
jwk,
createdAtMs,
};
await updateEncryptedUserSecrets(cleanLogin, cleanPwd, (current) => {
const wallets = normalizeExtraWallets(current).filter((item) => item.address !== address);
wallets.push(wallet);
return {
...current,
arweaveWallets: wallets,
};
});
return wallet;
}
export async function getArweaveWalletFromStoredBlockchainKey({ login, storagePwd, onStatus } = {}) {
const cleanLogin = String(login || '').trim();
const cleanPwd = String(storagePwd || '').trim();
if (!cleanLogin || !cleanPwd) {
throw new Error(t('remaining.arweave_wallet_errors.no_active_session_to_access_blockchain_key'));
}
const secrets = await loadEncryptedUserSecrets(cleanLogin, cleanPwd);
const cached = pickCachedWallet(secrets);
if (cached) {
safeStatus(onStatus, t('remaining.arweave_wallet.arweave_wallet_loaded'));
return cached;
}
safeStatus(onStatus, t('remaining.arweave_wallet.your_arweave_wallet_is_being_derived_from_the_blockchain_k'));
const storedBlockchainKey = String(secrets?.blockchainKey || '').trim();
if (!storedBlockchainKey) {
throw new Error(t('remaining.arweave_wallet_errors.blockchain_key_not_found_on_this_device'));
}
const blockchainKey32 = extractEd25519Seed32FromStoredValue(storedBlockchainKey);
let wallet;
try {
wallet = await deriveArweaveWalletFromBlockchainKey32(blockchainKey32);
} finally {
blockchainKey32.fill(0);
}
const cachedWallet = {
derivation: wallet.derivation,
sourceKey: 'blockchain',
address: wallet.address,
owner: wallet.owner,
jwk: wallet.jwk,
createdAtMs: Date.now(),
};
await updateEncryptedUserSecrets(cleanLogin, cleanPwd, (nextSecrets) => ({
...nextSecrets,
arweaveWallet: cachedWallet,
}));
return {
derivation: cachedWallet.derivation,
address: cachedWallet.address,
owner: cachedWallet.owner,
jwk: cachedWallet.jwk,
};
}
export async function getArweaveBalance({ gateway, address }) {
const normalizedGateway = normalizeGateway(gateway);
const cleanAddress = String(address || '').trim();
if (!cleanAddress) {
throw new Error(t('remaining.arweave_wallet.arweave_address_missing'));
}
const url = `${normalizedGateway}/wallet/${encodeURIComponent(cleanAddress)}/balance`;
const response = await fetch(url, { method: 'GET' });
const rawText = (await response.text()).trim();
if (!response.ok) {
throw new Error(`Не удалось получить баланс Arweave (${response.status} ${response.statusText})`);
}
if (!/^\d+$/.test(rawText)) {
throw new Error(t('remaining.arweave_wallet.arweave_gateway_returned_an_invalid_balance'));
}
const winstonBig = BigInt(rawText);
const ar = Number(winstonBig) / Number(WINSTON_PER_AR);
return {
gateway: normalizedGateway,
winston: rawText,
ar,
};
}
export function formatAr(value, digits = 6) {
const n = Number(value);
if (!Number.isFinite(n)) return '0';
return formatNumber(n, {
minimumFractionDigits: 0,
maximumFractionDigits: digits,
});
}
export function getArweaveTopupSiteUrl() {
return AR_TOPUP_URL;
}
export async function transferAr({ gateway, jwk, toAddress, amountAr }) {
const cleanTo = String(toAddress || '').trim();
if (!cleanTo) throw new Error(t('remaining.arweave_wallet.arweave_recipient_address_missing'));
if (!jwk || typeof jwk !== 'object') throw new Error(t('remaining.arweave_wallet.arweave_wallet_is_not_initialized'));
const normalizedGateway = normalizeGateway(gateway);
const { host, port, protocol } = parseGatewayForArweaveInit(normalizedGateway);
const winston = parseArToWinston(amountAr);
const moduleRef = await loadArweaveLib();
const Arweave = moduleRef?.default || moduleRef;
const arweave = Arweave.init({ host, port, protocol });
const tx = await arweave.createTransaction(
{
target: cleanTo,
quantity: winston,
},
jwk,
);
await arweave.transactions.sign(tx, jwk);
const postResult = await arweave.transactions.post(tx);
return {
gateway: normalizedGateway,
id: tx.id,
status: Number(postResult?.status || 0),
statusText: String(postResult?.statusText || ''),
};
}