UNPKG

@ethersphere/swarm-cli

Version:
122 lines (121 loc) 5.66 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FeedCommand = void 0; const bee_js_1 = require("@ethersphere/bee-js"); const furious_commander_1 = require("furious-commander"); const process_1 = require("process"); const identity_1 = require("../../service/identity"); const stamp_1 = require("../../service/stamp"); const option_1 = require("../../utils/option"); const spinner_1 = require("../../utils/spinner"); const text_1 = require("../../utils/text"); const root_command_1 = require("../root-command"); const command_log_1 = require("../root-command/command-log"); class FeedCommand extends root_command_1.RootCommand { constructor() { super(...arguments); Object.defineProperty(this, "identity", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "topic", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "topicString", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "password", { enumerable: true, configurable: true, writable: true, value: void 0 }); } async updateFeedAndPrint(stamp, chunkReference) { const wallet = await this.getWallet(); const topic = this.topic ? new bee_js_1.Topic(this.topic) : bee_js_1.Topic.fromString(this.topicString); const { reference, manifest } = await this.writeFeed(stamp, wallet, topic, chunkReference); this.console.verbose((0, text_1.createKeyValue)('Chunk Reference', chunkReference.toHex())); this.console.verbose((0, text_1.createKeyValue)('Chunk Reference URL', `${this.bee.url}/bzz/${chunkReference}/`)); this.console.verbose((0, text_1.createKeyValue)('Feed Reference', reference.toHex())); this.console.verbose((0, text_1.createKeyValue)('Feed Manifest', manifest.toHex())); this.console.log((0, text_1.createKeyValue)('Feed Manifest URL', `${this.bee.url}/bzz/${manifest}/`)); this.console.quiet(manifest.toHex()); if (!this.quiet) { (0, stamp_1.printStamp)(await this.bee.getPostageBatch(stamp), this.console, { shortenBatchId: true }); } return manifest; } async getWallet() { const identity = await this.getIdentity(); const wallet = await (0, identity_1.getWalletFromIdentity)(this.console, this.quiet, identity, this.password); return wallet; } async getIdentity() { const { identities } = this.commandConfig.config; if (this.identity && !identities[this.identity]) { if (this.quiet) { this.console.error('The provided identity does not exist.'); (0, process_1.exit)(1); } this.console.error('The provided identity does not exist. Please select one that exists.'); } return identities[this.identity] || identities[await (0, identity_1.pickIdentity)(this.commandConfig, this.console)]; } async writeFeed(stamp, wallet, topic, chunkReference) { const spinner = (0, spinner_1.createSpinner)('Writing feed...'); if (this.verbosity !== command_log_1.VerbosityLevel.Quiet && !this.curl) { spinner.start(); } try { const writer = this.bee.makeFeedWriter(topic, wallet.getPrivateKey()); const feedManifestResult = await this.bee.createFeedManifest(stamp, topic, wallet.getAddressString()); const data = await this.bee.downloadData(chunkReference); const { reference } = await writer.uploadPayload(stamp, data.toUint8Array()); return { reference, manifest: feedManifestResult }; } finally { spinner.stop(); } } } __decorate([ (0, furious_commander_1.Option)({ key: 'identity', alias: 'i', description: 'Name of the identity', required: { when: 'quiet' }, conflicts: 'address', }), __metadata("design:type", String) ], FeedCommand.prototype, "identity", void 0); __decorate([ (0, furious_commander_1.Option)(option_1.topicProperties), __metadata("design:type", String) ], FeedCommand.prototype, "topic", void 0); __decorate([ (0, furious_commander_1.Option)(option_1.topicStringProperties), __metadata("design:type", String) ], FeedCommand.prototype, "topicString", void 0); __decorate([ (0, furious_commander_1.Option)({ key: 'password', alias: 'P', description: 'Password for the wallet' }), __metadata("design:type", String) ], FeedCommand.prototype, "password", void 0); exports.FeedCommand = FeedCommand;