UNPKG

@kyve/core-beta

Version:

🚀 The base KYVE node implementation.

50 lines (49 loc) • 1.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Arweave = void 0; const arweave_1 = __importDefault(require("arweave")); const axios_1 = __importDefault(require("axios")); class Arweave { constructor() { this.name = "Arweave"; this.decimals = 12; this.client = new arweave_1.default({ host: "arweave.net", protocol: "https", }); } async init(storagePriv) { this.jwk = JSON.parse(storagePriv); return this; } async getBalance() { const account = await this.client.wallets.getAddress(this.jwk); return await this.client.wallets.getBalance(account); } async saveBundle(bundle, tags) { const transaction = await this.client.createTransaction({ data: bundle, }); for (const tag of tags) { transaction.addTag(tag.name, tag.value); } await this.client.transactions.sign(transaction, this.jwk); const balance = await this.getBalance(); if (parseInt(transaction.reward) > parseInt(balance)) { throw Error(`Not enough funds in Arweave wallet. Found = ${balance} required = ${transaction.reward}`); } await this.client.transactions.post(transaction); return { storageId: transaction.id, storageData: Buffer.from(transaction.data), }; } async retrieveBundle(storageId, timeout) { const { data: storageData } = await axios_1.default.get(`https://arweave.net/${storageId}`, { responseType: "arraybuffer", timeout }); return { storageId, storageData }; } } exports.Arweave = Arweave;