Skip to content

Commit

Permalink
basic scaffolding, some impl of addDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Jan 22, 2022
0 parents commit f6181ea
Show file tree
Hide file tree
Showing 5 changed files with 1,195 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
node_modules
Empty file added index.d.ts
Empty file.
107 changes: 107 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const ArweaveLib = require('arweave')
const { LRUMap } = require('lru_map')

const DEFAULT_OPTIONS = {
host: 'arweave.net',
port: 443,
protocol: 'https',
timeout: 20000,
logging: false,
}

// A single managed document
class Document {
data
txID
client
synced

constructor(parentClient, content, version, name, meta) {
this.client = parentClient
this.data = {
content,
version,
name,
meta,
}
this.txID = undefined
this.synced = false
}

update(partialDocument) {
this.synced = false
this.data = {
...this.data,
version: this.data.version + 1,
...partialDocument,
}
}
}

// Constants
const VERSION = "DOC_VERSION"
const NAME = "DOC_NAME"

// Thin wrapper client around Arweave for versioned document/data management.
// Relies on an 'admin' wallet for fronting transaction + gas costs for users.
class ArweaveClient {
#key
adminAddr
client
cache

// Construct a new client given the address of the admin account,
// keys to the wallet, and a set of options for connecting to an Arweave network.
// Options are identical to the ones supported by the official arweave-js library
constructor(adminAddress, key, cache_size = 500, options = DEFAULT_OPTIONS) {
this.#key = key
this.adminAddr = adminAddress
this.client = ArweaveLib.init(options)
this.cache = new LRUMap(500)
}

// Function to add a new document
async addDocument(content, version, name, meta) {
// create document + transaction
const doc = new Document(this, content, version, name, meta)
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.data.version)
tx.addTag(NAME, doc.data.name)

// sign + send tx
await this.client.transactions.sign(tx, this.#key)
const txResult = await this.client.transactions.post(tx)

// success, update doc data, add to cache
doc.txID = tx.id
doc.synced = true
this.cache.set(doc.data.name, doc)

return {
...txResult,
id: tx.id,
}
}

async updateDocument(content, version, meta) {
}

async getDocumentByName(name) {
// check if doc is in cache and entry is up to date
if (this.cache.has(name) && this.cache.get(name).synced) {
return this.cache.get(name)
}

// otherwise, fetch latest to cache

// return
return this.cache.get(name)
}

// get document by specific version and hash

}
Loading

0 comments on commit f6181ea

Please sign in to comment.