UNPKG

@logsn/arweave

Version:
102 lines 3.89 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SiloResource = void 0; const ArweaveUtils = __importStar(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; //# sourceMappingURL=silo.js.map