-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.ts
151 lines (120 loc) · 4.19 KB
/
client.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { Principal } from "@dfinity/principal";
// import { QuotaType } from "./canisters/registrar/registrar.did";
import { IcNamingClientBase, IcNamingClientInitOptions } from "./internal/base";
import { throwable } from "./internal/utils";
export class IcNamingClient extends IcNamingClientBase {
constructor(options: IcNamingClientInitOptions) {
super(options);
}
/* Registrar */
async isAvailableNaming(naming: string) {
return await throwable(() => this.registrar.available(naming));
}
async getExpiredTimeOfName(name: string) {
return await throwable(() => this.registrar.get_name_expires(name));
}
async getRegistrarPrices() {
const { items } = await throwable(() => this.registrar.get_price_table());
return items;
}
// async submitRegisterOrder(name: string, years: number) {
// return await throwable(() => this.registrar.submit_order({ name, years }));
// }
// async refundRegisterOrder() {
// return await throwable(() => this.registrar.refund_order());
// }
// async cancelRegistrarOrder() {
// return await throwable(() => this.registrar.cancel_order());
// }
// async confirmRegistrarOrder(blockHeight: bigint) {
// return await throwable(() => this.registrar.confirm_pay_order(blockHeight));
// }
// async getPendingOrder() {
// return await throwable(() => this.registrar.get_pending_order());
// }
// async registerNameByQuota(
// name: string,
// quota: Extract<QuotaType, { LenGte: unknown }>["LenGte"]
// ) {
// const wrappedQuota: QuotaType = { LenGte: quota };
// return await throwable(() =>
// this.registrar.register_with_quota(name, wrappedQuota)
// );
// }
async getRegistrationOfName(name: string) {
return await throwable(() => this.registrar.get_details(name));
}
async getRegistrantOfName(name: string) {
return await throwable(() => this.registrar.get_owner(name));
}
async getNamesOfRegistrant(
address: Principal,
params: { offset: bigint; limit: bigint }
) {
const { items } = await throwable(() =>
this.registrar.get_names(address, params)
);
return items;
}
// --- Registry ---
async getNamesOfController(
address: Principal,
params: { offset: bigint; limit: bigint }
) {
const { items } = await throwable(() =>
this.registry.get_controlled_names(address, params)
);
return items;
}
async getControllerOfName(name: string) {
return await throwable(() => this.registry.get_owner(name));
}
async getResolverOfName(name: string) {
return await throwable(() => this.registry.get_resolver(name));
}
async getRegistryOfName(name: string) {
return await throwable(() => this.registry.get_details(name));
}
// --- Resolver ---
async getRecordsOfName(name: string): Promise<Array<[string, string]>> {
let cachedRecords: Array<[string, string]> | undefined;
await this.dispatchNameRecordsCache(async (store) => {
const target = await store.getRecordsByName(name);
if (target) {
if (Date.now() >= target.expired_at) {
const { ttl } = await this.getRegistryOfName(name);
await store.setRecordsByName(name, {
name,
expired_at: new Date(Date.now() + Number(ttl) * 1000).getTime(),
});
} else {
if (target.records) {
cachedRecords = target.records.map((i) => [i.key, i.value]);
}
}
} else {
const { ttl } = await this.getRegistryOfName(name);
await store.setRecordsByName(name, {
name,
expired_at: new Date(Date.now() + Number(ttl) * 1000).getTime(),
});
}
});
if (cachedRecords) return cachedRecords;
const result = await throwable(() => this.resolver.get_record_value(name));
await this.dispatchNameRecordsCache(async (store) => {
const target = await store.getRecordsByName(name);
if (target) {
await store.setRecordsByName(name, {
...target,
records: result.map(([key, value]) => ({ key, value })),
});
}
});
return result;
}
// --- Favorites ---
async getFavoriteNames() {
return await throwable(() => this.favorites.get_favorites());
}
}