SHA256
Добавить аватар профиля через Arweave и мастер загрузки
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { loadEncryptedUserSecrets } from './key-vault.js';
|
||||
import { loadEncryptedUserSecrets, updateEncryptedUserSecrets } from './key-vault.js';
|
||||
import { extractDeviceKey32FromStoredValue } from './device-key-utils.js';
|
||||
import { deriveArweaveWalletFromDeviceKey32 } from './sawd-v1.js';
|
||||
|
||||
@@ -72,7 +72,37 @@ async function loadArweaveLib() {
|
||||
return arweaveLibPromise;
|
||||
}
|
||||
|
||||
export async function getArweaveWalletFromStoredDeviceKey({ login, storagePwd }) {
|
||||
function pickCachedWallet(secrets) {
|
||||
const cached = secrets?.arweaveWallet;
|
||||
if (!cached || typeof cached !== 'object') return null;
|
||||
const derivation = String(cached.derivation || '').trim();
|
||||
const address = String(cached.address || '').trim();
|
||||
const owner = String(cached.owner || '').trim();
|
||||
const jwk = cached.jwk;
|
||||
if (derivation !== 'SAWD-v1' || !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,
|
||||
address,
|
||||
owner,
|
||||
jwk,
|
||||
};
|
||||
}
|
||||
|
||||
function safeStatus(onStatus, text) {
|
||||
if (typeof onStatus !== 'function') return;
|
||||
try {
|
||||
onStatus(String(text || ''));
|
||||
} catch {
|
||||
// ignore callback errors
|
||||
}
|
||||
}
|
||||
|
||||
export async function getArweaveWalletFromStoredDeviceKey({ login, storagePwd, onStatus } = {}) {
|
||||
const cleanLogin = String(login || '').trim();
|
||||
const cleanPwd = String(storagePwd || '').trim();
|
||||
if (!cleanLogin || !cleanPwd) {
|
||||
@@ -80,6 +110,14 @@ export async function getArweaveWalletFromStoredDeviceKey({ login, storagePwd })
|
||||
}
|
||||
|
||||
const secrets = await loadEncryptedUserSecrets(cleanLogin, cleanPwd);
|
||||
const cached = pickCachedWallet(secrets);
|
||||
if (cached) {
|
||||
safeStatus(onStatus, 'Arweave-кошелёк загружен.');
|
||||
return cached;
|
||||
}
|
||||
|
||||
safeStatus(onStatus, 'Сейчас мы впервые получаем Arweave-кошелёк из вашего device key. Это может занять немного времени.');
|
||||
|
||||
const storedDeviceKey = String(secrets?.deviceKey || '').trim();
|
||||
if (!storedDeviceKey) {
|
||||
throw new Error('На устройстве не найден device.key (wallet.key)');
|
||||
@@ -93,11 +131,24 @@ export async function getArweaveWalletFromStoredDeviceKey({ login, storagePwd })
|
||||
deviceKey32.fill(0);
|
||||
}
|
||||
|
||||
return {
|
||||
const cachedWallet = {
|
||||
derivation: wallet.derivation,
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user