-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
379 lines (327 loc) · 10 KB
/
index.js
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
const ArweaveLib = require('arweave')
const { LRUMap } = require('lru_map')
const { backOff } = require('exponential-backoff')
const fetch = require('cross-fetch')
const DEFAULT_ARWEAVE_OPTIONS = {
host: 'arweave.net',
port: 443,
protocol: 'https',
timeout: 20000,
logging: false,
}
const DEFAULT_FETCH_OPTIONS = {
maxRetries: 10,
verifiedOnly: true,
maxResults: 25,
compatabilityMode: false,
skipHydration: false,
}
class UnhydratedDocument {
txID
client
name
version
tags
constructor(parentClient, data) {
this.client = parentClient
this.txID = data.txID
this.name = data.name
this.version = data.version
this.tags = data.tags
}
async getHydratedDocument() {
return this.client.getDocumentByTxId(this.txId)
}
}
class Document {
txID
client
posted
timestamp
name
version
tags
constructor(parentClient, name, content, tags, version = 0) {
this.client = parentClient
this.txID = undefined
this.posted = false
this.timestamp = undefined
this.name = name
this.content = content
this.version = version
this.tags = tags
}
data() {
return {
name: this.name,
content: this.content,
version: this.version,
tags: this.tags,
}
}
async update(content) {
this.content = content
this.version += 1
this.posted = false
return await this.client.updateDocument(this)
}
bumpTimestamp(dateMs) {
const time = new Date(dateMs * 1000)
this.timestamp = time.toString()
}
}
// Constants
const VERSION = "DOC_VERSION"
const NAME = "DOC_NAME"
const META = "DOC_META"
class ArweaveClient {
// Key object for associated admin wallet
#key
adminAddr
client
cache
constructor(adminAddress, keyFile, cacheSize = 500, options = DEFAULT_ARWEAVE_OPTIONS) {
if (keyFile === undefined) {
console.log("WARN: keyFile is undefined. Client is now in READ-ONLY mode. If this isn't intentional, make sure you are passing in a key")
} else {
this.#key = JSON.parse(keyFile)
}
this.adminAddr = adminAddress
this.client = ArweaveLib.init(options)
this.cache = new LRUMap(cacheSize)
}
// Internal function for adding single document to permaweb
async #insert(doc) {
if (!this.#key) {
throw "Can't call .insert() in READ-ONLY mode!"
}
const tx = await this.client.createTransaction({
data: JSON.stringify(doc.data())
}, this.#key)
// tag with metadata (actual meta is stringified in body)
tx.addTag(VERSION, doc.version)
tx.addTag(NAME, doc.name)
// add user defined metadata
Object.entries(doc.tags).forEach(([tag, content]) => {
tx.addTag(`${META}_${tag}`, content)
})
// sign + send tx
await this.client.transactions.sign(tx, this.#key)
const txResult = await this.client.transactions.post(tx)
// check if something went wrong
if (txResult.status !== 200) {
return Promise.reject(txResult)
}
// success, update doc data, add to cache
doc.txID = tx.id
doc.posted = true
this.cache.set(doc.txID, doc)
return doc
}
isCached(txId, desiredVersion) {
const inCache = this.cache.has(txId)
if (!inCache) {
return false
}
const cached = this.cache.get(txId)
const versionMatch = desiredVersion !== undefined ? cached.version === desiredVersion : true
return cached.posted && versionMatch
}
async addDocument(name, content, tags) {
// create document + transaction
const doc = new Document(this, name, content, tags)
return this.#insert(doc)
}
async updateDocument(document) {
// check if cache has latest version of document
if (this.isCached(document.txID, document.version)) {
return document
}
// otherwise, update latest
return await this.#insert(document)
}
async pollForConfirmation(txId, maxRetries = 10) {
if (!txId) {
return Promise.reject("Document has not been posted! Use .update() first")
}
if (this.cache.has(txId)) {
return true
}
return await backOff(async () => {
const txStatus = await this.client.transactions.getStatus(txId)
if (txStatus.status === 200) {
return txStatus
} else {
return Promise.reject(txStatus.status)
}
}, {
numOfAttempts: maxRetries
})
}
// Internal fn for building GraphQL queries for fetching data.
// Both names and versions are arrays. Use `verifiedOnly = false` to include
// all submitted TXs (including ones from non-admin wallet accounts)
#queryBuilder(names, versions, userTags, verifiedOnly = true, cursor = undefined, compat = false) {
// parse use defined tags
const tags = Object.entries(userTags).map(([k, v]) => `{
name: "${compat ? k : `${META}_${k}`}",
values: ["${v}"]
}`)
// add name tag
if (names.length > 0) {
tags.push(`{
name: "${NAME}",
values: ${JSON.stringify(names)},
}`)
}
// versions is an optional field
if (versions.length > 0) {
tags.push(`{
name: "${VERSION}",
values: ${JSON.stringify(versions.map(n => n.toString()))},
}`)
}
return {
query: `
query {
transactions(
tags: [${tags.join(",")}],
${verifiedOnly ? `owners: ["${this.adminAddr}"],` : ""}
${cursor ? `after: "${cursor}",` : ""}
) {
edges {
cursor
node {
id
owner {
address
}
tags {
name
value
}
}
}
}
}
`
}
}
async executeQuery(names, versions, userTags, userOptions = DEFAULT_FETCH_OPTIONS) {
const options = {
...DEFAULT_FETCH_OPTIONS,
...userOptions,
}
const fetchQuery = async (cursor) => {
// fetch latest to cache
// build query to lookup by name (and optionally version) and send request to arweave graphql server
const query = this.#queryBuilder(names, versions, userTags, options.verifiedOnly, cursor, options.compatabilityMode)
const req = await fetch('https://arweave.net/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(query),
})
const json = await req.json()
return json.data.transactions
}
const resultEdges = []
let nResults = 1
let cursor = undefined
while (nResults > 0 && resultEdges.length < options.maxResults) {
const newEdges = await fetchQuery(cursor)
nResults = newEdges.edges.length
resultEdges.push(...newEdges.edges)
cursor = newEdges.cursor
}
// safe to get first item as we specify specific tags in the query building stage
const getVersion = (edge) => edge.node.tags.find(tag => tag.name === VERSION).value || 0
const getName = (edge) => edge.node.tags.find(tag => tag.name === NAME).value || "Untitled Document"
const txs = resultEdges.sort((a, b) => getVersion(b) - getVersion(a))
if (options.skipHydration) {
return txs.map(e => new UnhydratedDocument(this.client, {
txID: e.id,
name: getName(e),
version: getVersion(e),
tags: e.node.tags.reduce((accum, tag) => {
accum[tag.name] = tag.value
return accum
}, {})
}))
} else {
// hydrate document, update cache
const promises = txs
.map(e => e.node.id)
.map(txId => this.getDocumentByTxId(txId, options))
const docs = (await Promise.allSettled(promises))
.filter(p => p.status === "fulfilled")
.map(p => p.value)
.slice(0, userOptions.maxResults)
docs.forEach(doc => this.cache.set(doc.name, doc))
return docs
}
}
async getDocumentsByTags(tags, options = DEFAULT_FETCH_OPTIONS) {
return this.executeQuery([], [], tags, options)
}
async getDocumentsByName(name, version, tags = [], options = DEFAULT_FETCH_OPTIONS) {
return this.executeQuery([name], version === undefined ? [] : [version], tags, options)
}
async getDocumentByTxId(txId, userOptions = DEFAULT_FETCH_OPTIONS) {
const options = {
...DEFAULT_FETCH_OPTIONS,
...userOptions
}
if (this.cache.has(txId)) {
return this.cache.get(txId)
}
// ensure block with tx is confirmed (do not assume it is in cache)
const txStatus = await this.pollForConfirmation(txId, options.maxRetries)
// fetch tx metadata
const transactionMetadata = await this.client.transactions.get(txId)
const readOnlyMode = !this.#key
if ((options.verifiedOnly && !readOnlyMode) && transactionMetadata.owner !== this.#key.n) {
return Promise.reject(`Document is not verified. Owner address mismatched! Got: ${transactionMetadata.owner}`)
}
// tag parsing
const metaTags = transactionMetadata.get('tags').reduce((accum, tag) => {
let key = tag.get('name', { decode: true, string: true })
accum[key] = tag.get('value', { decode: true, string: true })
return accum
}, {})
// assert that these are actually documents
if (!(metaTags.hasOwnProperty(NAME) && metaTags.hasOwnProperty(VERSION))) {
return Promise.reject(`Transaction ${txId} is not a document. Make sure your transaction ID is correct`)
}
// concurrently fetch associated block + block metadata + data
const blockId = txStatus.confirmed.block_indep_hash
const [blockMeta, dataString] = await Promise.all([
this.client.blocks.get(blockId),
this.client.transactions.getData(txId, {
decode: true,
string: true,
}),
])
const {
name,
content,
version,
tags
} = JSON.parse(dataString)
// transform into document and return
const doc = new Document(this, name, content, tags, version)
doc.posted = true
doc.txID = txId
doc.bumpTimestamp(blockMeta.timestamp)
this.cache.set(doc.name, doc)
return doc
}
}
module.exports = {
ArweaveClient,
Document,
DEFAULT_ARWEAVE_OPTIONS,
DEFAULT_FETCH_OPTIONS,
}