-
Notifications
You must be signed in to change notification settings - Fork 21
/
starknet.ts
64 lines (47 loc) · 1.77 KB
/
starknet.ts
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fetch from 'node-fetch';
import { Provider, RpcProvider } from 'starknet';
import { getUrl, resize } from '../utils';
import { max } from '../constants.json';
import { fetchHttpImage } from './utils';
const DEFAULT_IMG_URL = 'https://starknet.id/api/identicons/0';
const provider = new Provider(
new RpcProvider({
nodeUrl: process.env.INFURA_API_KEY
? `https://starknet-mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`
: 'https://starknet-mainnet.public.blastapi.io'
})
);
function isStarknetDomain(domain: string): boolean {
return domain.endsWith('.stark');
}
function normalizeAddress(address: string): string {
if (!address.match(/^(0x)?[0-9a-fA-F]{64}$/)) throw new Error('Invalid starknet address');
return address;
}
async function getStarknetAddress(domain: string): Promise<string | null> {
const address = await provider.getAddressFromStarkName(domain);
return address === '0x0' ? null : address;
}
async function getImage(domainOrAddress: string): Promise<string | null> {
const address = isStarknetDomain(domainOrAddress)
? await getStarknetAddress(domainOrAddress)
: normalizeAddress(domainOrAddress);
if (!address) return null;
return (await provider.getStarkProfile(address))?.profilePicture ?? null;
}
export default async function resolve(domainOrAddress: string) {
try {
let img_url = await getImage(domainOrAddress);
if (img_url === DEFAULT_IMG_URL) return false;
if (img_url?.startsWith('https://api.starkurabu.com')) {
const response = await fetch(img_url);
const data = await response.json();
img_url = data.image;
}
if (!img_url) return false;
const input = await fetchHttpImage(getUrl(img_url));
return await resize(input, max, max);
} catch (e) {
return false;
}
}