UNPKG

@logsn/arweave

Version:
68 lines (67 loc) 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ArweaveUtils = require("./lib/utils"); require("arconnect"); class Wallets { api; crypto; constructor(api, crypto) { this.api = api; this.crypto = crypto; } /** * Get the wallet balance for the given address. * * @param {string} address - The arweave address to get the balance for. * * @returns {Promise<string>} - Promise which resolves with a winston string balance. */ getBalance(address) { return this.api.get(`wallet/${address}/balance`).then((response) => { return response.data; }); } /** * Get the last transaction ID for the given wallet address. * * @param {string} address - The arweave address to get the transaction for. * * @returns {Promise<string>} - Promise which resolves with a transaction ID. */ getLastTransactionID(address) { return this.api.get(`wallet/${address}/last_tx`).then((response) => { return response.data; }); } generate() { return this.crypto.generateJWK(); } async jwkToAddress(jwk) { if (!jwk || jwk === "use_wallet") { return this.getAddress(); } else { return this.getAddress(jwk); } } async getAddress(jwk) { if (!jwk || jwk === "use_wallet") { try { // @ts-ignore await arweaveWallet.connect(["ACCESS_ADDRESS"]); } catch { // Permission is already granted } // @ts-ignore return arweaveWallet.getActiveAddress(); } else { return this.ownerToAddress(jwk.n); } } async ownerToAddress(owner) { return ArweaveUtils.bufferTob64Url(await this.crypto.hash(ArweaveUtils.b64UrlToBuffer(owner))); } } exports.default = Wallets;