UNPKG

@logsn/arweave

Version:
79 lines (78 loc) 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SiloResource = void 0; const ArweaveUtils = require("./lib/utils"); class Silo { api; crypto; transactions; constructor(api, crypto, transactions) { this.api = api; this.crypto = crypto; this.transactions = transactions; } async get(siloURI) { if (!siloURI) { throw new Error(`No Silo URI specified`); } const resource = await this.parseUri(siloURI); const ids = await this.transactions.search("Silo-Name", resource.getAccessKey()); if (ids.length == 0) { throw new Error(`No data could be found for the Silo URI: ${siloURI}`); } const transaction = await this.transactions.get(ids[0]); if (!transaction) { throw new Error(`No data could be found for the Silo URI: ${siloURI}`); } const encrypted = transaction.get("data", { decode: true, string: false }); return this.crypto.decrypt(encrypted, resource.getEncryptionKey()); } async readTransactionData(transaction, siloURI) { if (!siloURI) { throw new Error(`No Silo URI specified`); } const resource = await this.parseUri(siloURI); const encrypted = transaction.get("data", { decode: true, string: false }); return this.crypto.decrypt(encrypted, resource.getEncryptionKey()); } async parseUri(siloURI) { const parsed = siloURI.match(/^([a-z0-9-_]+)\.([0-9]+)/i); if (!parsed) { throw new Error(`Invalid Silo name, must be a name in the format of [a-z0-9]+.[0-9]+, e.g. 'bubble.7'`); } const siloName = parsed[1]; const hashIterations = Math.pow(2, parseInt(parsed[2])); const digest = await this.hash(ArweaveUtils.stringToBuffer(siloName), hashIterations); const accessKey = ArweaveUtils.bufferTob64(digest.slice(0, 15)); const encryptionkey = await this.hash(digest.slice(16, 31), 1); return new SiloResource(siloURI, accessKey, encryptionkey); } async hash(input, iterations) { let digest = await this.crypto.hash(input); for (let count = 0; count < iterations - 1; count++) { digest = await this.crypto.hash(digest); } return digest; } } exports.default = Silo; class SiloResource { uri; accessKey; encryptionKey; constructor(uri, accessKey, encryptionKey) { this.uri = uri; this.accessKey = accessKey; this.encryptionKey = encryptionKey; } getUri() { return this.uri; } getAccessKey() { return this.accessKey; } getEncryptionKey() { return this.encryptionKey; } } exports.SiloResource = SiloResource;