UNPKG

@ethersphere/swarm-cli

Version:
58 lines (57 loc) 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BzzAddress = void 0; exports.makeBzzAddress = makeBzzAddress; const bee_js_1 = require("@ethersphere/bee-js"); const cafe_utility_1 = require("cafe-utility"); const error_1 = require("./error"); class BzzAddress { constructor(url) { Object.defineProperty(this, "hash", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "path", { enumerable: true, configurable: true, writable: true, value: void 0 }); if (url.startsWith('bzz://')) { url = url.slice(6); } if (url.includes('//')) { throw new error_1.CommandLineError('Invalid BZZ path: cannot contain multiple continuous slashes'); } const parts = url.split('/'); this.hash = parts[0].toLowerCase(); if (this.hash.startsWith('0x')) { this.hash = this.hash.slice(2); } if (!/[a-z0-9]{64,128}/.test(this.hash)) { throw new error_1.CommandLineError('Invalid BZZ hash: expected 64 or 128 long hexadecimal hash'); } const pathParts = parts.slice(1); this.path = pathParts.length ? pathParts.join('/') : null; } } exports.BzzAddress = BzzAddress; async function makeBzzAddress(bee, url) { const address = new BzzAddress(url); try { const manifest = await bee_js_1.MantarayNode.unmarshal(bee, address.hash); await manifest.loadRecursively(bee); const resolvedFeed = await manifest.resolveFeed(bee); await resolvedFeed.ifPresentAsync(async (feed) => { const merkleTree = await bee_js_1.MerkleTree.root(feed.payload.toUint8Array()); const cacAddress = cafe_utility_1.Binary.uint8ArrayToHex(merkleTree.hash()); address.hash = cacAddress; }); return address; } catch { return address; } }