forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar-interact.ts
More file actions
27 lines (23 loc) · 991 Bytes
/
Copy pathstellar-interact.ts
File metadata and controls
27 lines (23 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import * as StellarSdk from '@stellar/stellar-sdk';
const HORIZON_URL = process.env.NEXT_PUBLIC_HORIZON_URL ?? 'https://horizon-testnet.stellar.org';
const server = new StellarSdk.Horizon.Server(HORIZON_URL);
function decodeBase64(value: string): string {
if (typeof atob === 'function') {
return atob(value);
}
return Buffer.from(value, 'base64').toString();
}
/** Fetch Guardian reputation score from contract data entries. */
export async function getReputation(publicKey: string): Promise<number> {
const account = await server.loadAccount(publicKey);
const entry = (account.data_attr as Record<string, string>)['vero_reputation'];
if (!entry) {
return 0;
}
const decodedReputation = decodeBase64(entry).trim();
const reputation = Number(decodedReputation);
if (!Number.isInteger(reputation) || reputation < 0) {
throw new Error('Stellar reputation data is invalid. Refresh the page or contact support if this continues.');
}
return reputation;
}