@logsn/arweave
Version:
Arweave JS client library
157 lines • 6.81 kB
JavaScript
;
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ar_1 = __importDefault(require("./ar"));
const api_1 = __importDefault(require("./lib/api"));
const node_driver_1 = __importDefault(require("./lib/crypto/node-driver"));
const network_1 = __importDefault(require("./network"));
const transactions_1 = __importDefault(require("./transactions"));
const wallets_1 = __importDefault(require("./wallets"));
const transaction_1 = __importDefault(require("./lib/transaction"));
const ArweaveUtils = __importStar(require("./lib/utils"));
const silo_1 = __importDefault(require("./silo"));
const chunks_1 = __importDefault(require("./chunks"));
const blocks_1 = __importDefault(require("./blocks"));
class Arweave {
api;
wallets;
transactions;
network;
blocks;
ar;
silo;
chunks;
static init;
static crypto = new node_driver_1.default();
static utils = ArweaveUtils;
constructor(apiConfig) {
this.api = new api_1.default(apiConfig);
this.wallets = new wallets_1.default(this.api, Arweave.crypto);
this.chunks = new chunks_1.default(this.api);
this.transactions = new transactions_1.default(this.api, Arweave.crypto, this.chunks);
this.silo = new silo_1.default(this.api, this.crypto, this.transactions);
this.network = new network_1.default(this.api);
this.blocks = new blocks_1.default(this.api, this.network);
this.ar = new ar_1.default();
}
/** @deprecated */
get crypto() {
return Arweave.crypto;
}
/** @deprecated */
get utils() {
return Arweave.utils;
}
getConfig() {
return {
api: this.api.getConfig(),
crypto: null,
};
}
async createTransaction(attributes, jwk) {
const transaction = {};
Object.assign(transaction, attributes);
if (!attributes.data && !(attributes.target && attributes.quantity)) {
throw new Error(`A new Arweave transaction must have a 'data' value, or 'target' and 'quantity' values.`);
}
if (attributes.owner == undefined) {
if (jwk && jwk !== "use_wallet") {
transaction.owner = jwk.n;
}
}
if (attributes.last_tx == undefined) {
transaction.last_tx = await this.transactions.getTransactionAnchor();
}
if (typeof attributes.data === "string") {
attributes.data = ArweaveUtils.stringToBuffer(attributes.data);
}
if (attributes.data instanceof ArrayBuffer) {
attributes.data = new Uint8Array(attributes.data);
}
if (attributes.data && !(attributes.data instanceof Uint8Array)) {
throw new Error("Expected data to be a string, Uint8Array or ArrayBuffer");
}
if (attributes.reward == undefined) {
const length = attributes.data ? attributes.data.byteLength : 0;
transaction.reward = await this.transactions.getPrice(length, transaction.target);
}
// here we should call prepare chunk
transaction.data_root = "";
transaction.data_size = attributes.data
? attributes.data.byteLength.toString()
: "0";
transaction.data = attributes.data || new Uint8Array(0);
const createdTransaction = new transaction_1.default(transaction);
await createdTransaction.getSignatureData();
return createdTransaction;
}
async createSiloTransaction(attributes, jwk, siloUri) {
const transaction = {};
Object.assign(transaction, attributes);
if (!attributes.data) {
throw new Error(`Silo transactions must have a 'data' value`);
}
if (!siloUri) {
throw new Error(`No Silo URI specified.`);
}
if (attributes.target || attributes.quantity) {
throw new Error(`Silo transactions can only be used for storing data, sending AR to other wallets isn't supported.`);
}
if (attributes.owner == undefined) {
if (!jwk || !jwk.n) {
throw new Error(`A new Arweave transaction must either have an 'owner' attribute, or you must provide the jwk parameter.`);
}
transaction.owner = jwk.n;
}
if (attributes.last_tx == undefined) {
transaction.last_tx = await this.transactions.getTransactionAnchor();
}
const siloResource = await this.silo.parseUri(siloUri);
if (typeof attributes.data == "string") {
const encrypted = await this.crypto.encrypt(ArweaveUtils.stringToBuffer(attributes.data), siloResource.getEncryptionKey());
transaction.reward = await this.transactions.getPrice(encrypted.byteLength);
transaction.data = ArweaveUtils.bufferTob64Url(encrypted);
}
if (attributes.data instanceof Uint8Array) {
const encrypted = await this.crypto.encrypt(attributes.data, siloResource.getEncryptionKey());
transaction.reward = await this.transactions.getPrice(encrypted.byteLength);
transaction.data = ArweaveUtils.bufferTob64Url(encrypted);
}
const siloTransaction = new transaction_1.default(transaction);
siloTransaction.addTag("Silo-Name", siloResource.getAccessKey());
siloTransaction.addTag("Silo-Version", `0.1.0`);
return siloTransaction;
}
arql(query) {
return this.api
.post("/arql", query)
.then((response) => response.data || []);
}
}
exports.default = Arweave;
//# sourceMappingURL=common.js.map