@logsn/arweave
Version:
Arweave JS client library
131 lines (130 loc) • 5.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ar_1 = require("./ar");
const api_1 = require("./lib/api");
const node_driver_1 = require("./lib/crypto/webcrypto-driver");
const network_1 = require("./network");
const transactions_1 = require("./transactions");
const wallets_1 = require("./wallets");
const transaction_1 = require("./lib/transaction");
const ArweaveUtils = require("./lib/utils");
const silo_1 = require("./silo");
const chunks_1 = require("./chunks");
const blocks_1 = 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;