-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsherlock.ts
More file actions
445 lines (403 loc) · 14.7 KB
/
sherlock.ts
File metadata and controls
445 lines (403 loc) · 14.7 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import * as ed from '@noble/ed25519'
import { sha512 } from '@noble/hashes/sha512'
import { z } from 'zod'
const API_URL = "https://api.sherlockdomains.com"
// Initialize ed25519 with SHA-512
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m))
export interface Contact {
first_name: string;
last_name: string;
email: string;
address: string;
city: string;
state: string;
postal_code: string;
country: string;
}
// Throws error with details if contact info is incomplete
export function requireCompleteContact(contact?: any): asserts contact is Contact {
if (!contact) {
throw { error: 'Contact information is required' }
}
const requiredFields = ['first_name', 'last_name', 'email', 'address', 'city', 'state', 'postal_code', 'country']
const missingFields = requiredFields.filter(field => !contact[field] || contact[field].trim() === '')
if (missingFields.length > 0) {
throw {
error: `Incomplete contact information: ${missingFields.join(', ')} ${missingFields.length === 1 ? 'is' : 'are'} required and cannot be empty`,
missingFields
}
}
}
export class Sherlock {
private accessToken?: string
private contact?: Contact
constructor(accessToken?: string) {
this.accessToken = accessToken
}
async handleResponse(response: Response) {
const data = await response.json()
// Check for HTTP error status
if (!response.ok) {
throw { status: response.status, ...data }
}
// Check for error field in the response data (even with 200 status)
if (data && data.error) {
throw { status: response.status, ...data }
}
return data
}
async me() {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/auth/me`, {
headers: { Authorization: `Bearer ${this.accessToken}` }
})
return this.handleResponse(r)
}
async claimAccount(email: string) {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/auth/email-link`, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ email })
})
return this.handleResponse(r)
}
async search(query: string) {
const params = new URLSearchParams({ query })
const r = await fetch(`${API_URL}/api/v0/domains/search?${params}`)
return this.handleResponse(r)
}
async domains() {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/domains/domains`, {
headers: { Authorization: `Bearer ${this.accessToken}` }
})
return this.handleResponse(r)
}
async updateNameservers(domainId: string, nameservers: string[]) {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/domains/${domainId}/nameservers`, {
method: 'PATCH',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ nameservers })
})
return this.handleResponse(r)
}
async dnsRecords(domainId: string) {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/domains/${domainId}/dns/records`, {
headers: { Authorization: `Bearer ${this.accessToken}` }
})
return this.handleResponse(r)
}
async createDns(domainId: string, {
type = "TXT",
name = "test",
value = "test-1",
ttl = 3600
}) {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/domains/${domainId}/dns/records`, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
records: [{type, name, value, ttl}]
})
})
return this.handleResponse(r)
}
async updateDns(domainId: string, recordId: string, {
type = "TXT",
name = "test-2",
value = "test-2",
ttl = 3600
}) {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/domains/${domainId}/dns/records`, {
method: 'PATCH',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
records: [{id: recordId, type, name, value, ttl}]
})
})
return this.handleResponse(r)
}
async deleteDns(domainId: string, recordId: string) {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/domains/${domainId}/dns/records/${recordId}`, {
method: 'DELETE',
headers: { Authorization: `Bearer ${this.accessToken}` }
})
return this.handleResponse(r)
}
async getContactInformation() {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/users/contact-information`, {
headers: { Authorization: `Bearer ${this.accessToken}` }
})
return this.handleResponse(r)
}
async setContactInformation(contact: Contact) {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/users/contact-information`, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(contact)
})
return this.handleResponse(r)
}
setContact(contact: Contact) {
this.contact = contact
}
async getPurchaseOffers(domain: string, searchId: string, contact?: Contact) {
if (!this.accessToken) throw 'Not authenticated'
const contactInfo = contact || this.contact
// Check contact info is complete
requireCompleteContact(contactInfo)
const r = await fetch(`${API_URL}/api/v0/domains/purchase`, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
domain,
contact_information: contactInfo,
search_id: searchId
})
})
return this.handleResponse(r)
}
async getX402PurchaseOffers(domain: string, searchId: string, contact?: Contact) {
if (!this.accessToken) throw 'Not authenticated'
const contactInfo = contact || this.contact
requireCompleteContact(contactInfo)
const r = await fetch(`${API_URL}/api/v0/domains/purchase-x402`, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
domain,
contact_information: contactInfo,
search_id: searchId
})
})
const data = await r.json()
return { status: 402, ...data }
}
async purchaseX402(domain: string, searchId: string, paymentSignature: string, contact?: Contact) {
if (!this.accessToken) throw 'Not authenticated'
const contactInfo = contact || this.contact
requireCompleteContact(contactInfo)
const r = await fetch(`${API_URL}/api/v0/domains/purchase-x402`, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json',
'PAYMENT-SIGNATURE': paymentSignature
},
body: JSON.stringify({
domain,
contact_information: contactInfo,
search_id: searchId
})
})
return this.handleResponse(r)
}
async purchaseDomain(searchId: string, domain: string, paymentMethod: string = 'credit_card') {
console.log('purchaseDomain', searchId, domain, paymentMethod)
if (!this.accessToken) throw 'Not authenticated'
const contact = await this.getContactInformation()
console.log('purchaseDomain', contact)
// This will throw with detailed message if contact info is incomplete
requireCompleteContact(contact)
const offers = await this.getPurchaseOffers(domain, searchId, contact)
console.log('purchaseDomain', offers)
// Since we've properly handled errors in getPurchaseOffers,
// we can safely access these properties
return await this.getPaymentDetails(
offers.payment_request_url,
offers.offers[0].id,
paymentMethod,
offers.payment_context_token
)
}
async getPaymentDetails(paymentRequestUrl: string, offerId: string, paymentMethod: string, paymentContextToken: string) {
const r = await fetch(paymentRequestUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
offer_id: offerId,
payment_method: paymentMethod,
payment_context_token: paymentContextToken
})
})
return this.handleResponse(r)
}
asTools() {
return {
me: {
description: 'Makes an authenticated request to verify the current authentication status and retrieve basic user details',
parameters: z.object({}),
execute: async () => await this.me()
},
setContactInformation: {
description: 'Set the contact information that will be used for domain purchases and ICANN registration',
parameters: z.object({
first_name: z.string().describe('First name'),
last_name: z.string().describe('Last name'),
email: z.string().describe('Email address'),
address: z.string().describe('Street address'),
city: z.string().describe('City'),
state: z.string().describe('Two-letter state code for US/Canada or province name'),
postal_code: z.string().describe('Postal code'),
country: z.string().describe('Two-letter country code')
}),
execute: async (contact: Contact) => await this.setContactInformation(contact)
},
getContactInformation: {
description: 'Get the contact information for the Sherlock user',
parameters: z.object({}),
execute: async () => await this.getContactInformation()
},
searchDomains: {
description: 'Search for domain names. Returns prices in USD cents.',
parameters: z.object({
query: z.string().describe('The domain name to search for')
}),
execute: async ({ query }: { query: string }) => await this.search(query)
},
purchaseDomain: {
description: 'Purchase a domain. This method won\'t charge your account, it will return the payment information needed to complete the purchase',
parameters: z.object({
searchId: z.string().describe('Search ID from a previous search request'),
domain: z.string().describe('Domain name to purchase'),
paymentMethod: z.string().default('credit_card').describe('Payment method to use {\'credit_card\', \'lightning\'}')
}),
execute: async ({ searchId, domain, paymentMethod = 'credit_card' }: {
searchId: string,
domain: string,
paymentMethod?: string
}) => await this.purchaseDomain(searchId, domain, paymentMethod)
},
listDomains: {
description: 'List domains owned by the authenticated user',
parameters: z.object({}),
execute: async () => await this.domains()
},
getDnsRecords: {
description: 'Get DNS records for a domain',
parameters: z.object({
domainId: z.string().describe('The domain ID')
}),
execute: async ({ domainId }: { domainId: string }) => await this.dnsRecords(domainId)
},
createDnsRecord: {
description: 'Create a new DNS record',
parameters: z.object({
domainId: z.string().describe('The domain ID'),
type: z.string().default('TXT').describe('Record type'),
name: z.string().default('test').describe('Record name'),
value: z.string().default('test-1').describe('Record value'),
ttl: z.number().default(3600).describe('Time to live')
}),
execute: async ({ domainId, ...params }: {
domainId: string,
type?: string,
name?: string,
value?: string,
ttl?: number
}) => await this.createDns(domainId, params)
},
updateDnsRecord: {
description: 'Update an existing DNS record',
parameters: z.object({
domainId: z.string().describe('The domain ID'),
recordId: z.string().describe('The record ID'),
type: z.string().default('TXT').describe('Record type'),
name: z.string().default('test-2').describe('Record name'),
value: z.string().default('test-2').describe('Record value'),
ttl: z.number().default(3600).describe('Time to live')
}),
execute: async ({ domainId, recordId, ...params }: {
domainId: string,
recordId: string,
type?: string,
name?: string,
value?: string,
ttl?: number
}) => await this.updateDns(domainId, recordId, params)
},
deleteDnsRecord: {
description: 'Delete a DNS record',
parameters: z.object({
domainId: z.string().describe('The domain ID'),
recordId: z.string().describe('The record ID')
}),
execute: async ({ domainId, recordId }: {
domainId: string,
recordId: string
}) => await this.deleteDns(domainId, recordId)
},
claimAccount: {
description: 'Claim an account by sending an email link for authentication',
parameters: z.object({
email: z.string()
}),
execute: async ({ email }: { email: string }) => await this.claimAccount(email)
},
updateNameservers: {
description: 'Update the nameservers for a domain',
parameters: z.object({
domainId: z.string(),
nameservers: z.array(z.string())
}),
execute: async ({ domainId, nameservers }: {
domainId: string,
nameservers: string[]
}) => await this.updateNameservers(domainId, nameservers)
},
getX402PurchaseOffers: {
description: 'Get X402 purchase offers for a domain. Returns payment requirements with a 402 status.',
parameters: z.object({
domain: z.string(),
searchId: z.string()
}),
execute: async ({ domain, searchId }: {
domain: string,
searchId: string
}) => await this.getX402PurchaseOffers(domain, searchId)
},
purchaseX402: {
description: 'Complete an X402 domain purchase with a payment signature',
parameters: z.object({
domain: z.string(),
searchId: z.string(),
paymentSignature: z.string()
}),
execute: async ({ domain, searchId, paymentSignature }: {
domain: string,
searchId: string,
paymentSignature: string
}) => await this.purchaseX402(domain, searchId, paymentSignature)
}
}
}
}