-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsherlock.ts
More file actions
263 lines (245 loc) · 8.18 KB
/
sherlock.ts
File metadata and controls
263 lines (245 loc) · 8.18 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
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;
}
export class Sherlock {
private accessToken: string
private contact?: Contact
constructor(accessToken: string) {
this.accessToken = accessToken
}
async me() {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/auth/me`, {
headers: { Authorization: `Bearer ${this.accessToken}` }
})
return await r.json()
}
async search(query: string) {
const params = new URLSearchParams({ query })
console.log('Search params:', params)
const r = await fetch(`${API_URL}/api/v0/domains/search?${params}`)
console.log('Search response:', r)
return await r.json()
}
async domains() {
if (!this.accessToken) throw 'Not authenticated'
const r = await fetch(`${API_URL}/api/v0/domains/domains`, {
headers: { Authorization: `Bearer ${this.accessToken}` }
})
return await r.json()
}
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 await r.json()
}
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 await r.json()
}
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 await r.json()
}
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 await r.json()
}
async requestPurchase(domain: string, searchId: string, contact?: Contact) {
if (!this.accessToken) throw 'Not authenticated'
const contactInfo = contact || this.contact
if (!contactInfo) throw 'Contact information is required'
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 await r.json()
}
async processPayment(paymentRequestUrl: string, {
offerId,
paymentMethod,
paymentContextToken
}: {
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 await r.json()
}
setContact(contact: Contact) {
this.contact = contact
}
asTools() {
return {
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)
},
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)
},
requestDomainPurchase: {
description: 'Request a purchase of a domain',
parameters: z.object({
domain: z.string().describe('The domain name'),
searchId: z.string().describe('The search ID'),
contact: z.object({
first_name: z.string(),
last_name: z.string(),
email: z.string(),
address: z.string(),
city: z.string(),
state: z.string(),
postal_code: z.string(),
country: z.string()
}).optional().describe('Contact information')
}),
execute: async ({ domain, searchId, contact }: {
domain: string,
searchId: string,
contact?: Contact
}) => await this.requestPurchase(domain, searchId, contact)
},
processPayment: {
description: 'Process a payment for an offer',
parameters: z.object({
paymentRequestUrl: z.string().describe('Payment request URL'),
offerId: z.string().describe('Offer ID'),
paymentMethod: z.string().describe('Payment method'),
paymentContextToken: z.string().describe('Payment context token')
}),
execute: async ({ paymentRequestUrl, ...params }: {
paymentRequestUrl: string,
offerId: string,
paymentMethod: string,
paymentContextToken: string
}) => await this.processPayment(paymentRequestUrl, 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)
}
}
}
}