A upowjs JavaScript library for interacting with the blockchain, designed specifically for uPow blockchain networks. This library provides functionalities for wallet management, transaction processing, and more.
Install upowjs using npm:
npm install upowjsimport { upowjs } from "upowjs";
async function createWallet() {
try {
const walletInfo = await upowjs.uPowKeys();
console.log("Wallet created:", walletInfo);
} catch (error) {
console.error("Error during wallet creation:", error.message);
}
}
createWallet();import { upowjs } from "upowjs";
const KEY = "your_private_key_here";
const TO = "recipient_wallet_address";
const AMOUNT = "amount_to_send";
const ENDPOINT = "https://api.upow.ai/";
async function sendTransaction() {
const myWallet = new upowjs.Wallet(KEY, ENDPOINT);
try {
const transactionInfo = await upowjs.sendTransaction(myWallet, TO, AMOUNT);
console.log("Transaction successful:", transactionInfo);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
sendTransaction();import { upowjs } from "upowjs";
async function stakeTransactionPush() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const info = await upowjs.stakeTransaction(myWallet, AMOUNT);
console.log("Transaction successful:", info);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
stakeTransactionPush();import { upowjs } from "upowjs";
async function unstakeTransactionPush() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const info = await upowjs.unStakeTransaction(myWallet);
console.log("Transaction successful:", info);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
unstakeTransactionPush();import { upowjs } from "upowjs";
async function registerInodeTransactionPush() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const info = await upowjs.registerInodeTransaction(myWallet);
console.log("Transaction successful:", info);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
registerInodeTransactionPush();import { upowjs } from "upowjs";
async function deRegisterInodeTransactionPush() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const info = await upowjs.deRegisterInodeTransaction(myWallet);
console.log("Transaction successful:", info);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
deRegisterInodeTransactionPush();import { upowjs } from "upowjs";
async function registerValidatorTransactionPush() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const info = await upowjs.registerValidatorTransaction(myWallet);
console.log("Transaction successful:", info);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
registerValidatorTransactionPush();import { upowjs } from "upowjs";
async function voteTransactionPush() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const info = await upowjs.voteTransaction(myWallet, VOTING_RANGE, VOTE_TO);
console.log("Transaction successful:", info);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
voteTransactionPush();import { upowjs } from "upowjs";
async function revokeTransactionPush() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const info = await upowjs.revokeTransaction(myWallet, REVOKE_FROM);
console.log("Transaction successful:", info);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
revokeTransactionPush();import { upowjs } from "upowjs";
async function checkUserBalance() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const bal = await upowjs.uPowBalance(myWallet, WALLET);
console.log("Balance:", bal);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
checkUserBalance();import { upowjs } from "upowjs";
async function checkTx() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const txhash = await upowjs.uPowTxHash(myWallet, txhashval);
console.log(JSON.stringify(txhash, null, 2));
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
checkTx();import { upowjs } from "upowjs";
async function getAddress() {
const endpoint = ENDPOINT;
const myWallet = new upowjs.Wallet(KEY, endpoint);
try {
const address = await upowjs.uPowGetAdress(myWallet);
console.log(address);
} catch (error) {
console.error("Error during transaction:", error.message);
}
}
getAddress();import { upowjs } from "upowjs";
(async () => {
const endpoint = "https://api.upow.ai/";
const wallet = new upowjs.Wallet("<PRIVATE_KEY>", endpoint);
// Create and sign a transaction
const tx = await upowjs.generateTransactionHex(
wallet,
"<RECIPIENT_ADDRESS>",
1.23, // amount in UPOW
true, // include a message
"" // message text
);
console.log("Signed hex:", tx.signedTransaction);
})();import { upowjs } from "upowjs";
const decoded = upowjs.decodeTransactionHex("<RAW_TX_HEX>");
console.log(JSON.stringify(decoded, null, 2));Useful for testing or estimating fees/UTXO selection.
import { upowjs } from "upowjs";
(async () => {
const sim = await upowjs.generateTransactionHexWithoutPrivateKey(
"<SENDER_ADDRESS>",
"<RECIPIENT_ADDRESS>",
0.5, // amount in UPOW
"https://api.upow.ai/", // node endpoint
false // include message?
);
console.log(sim.transactionHex); // raw, unsigned hex
console.log(sim.canPushToBlockchain); // always false
})();Analyze the UTXO distribution in a wallet to understand fragmentation and optimize transaction efficiency.
import { upowjs } from "upowjs";
async function analyzeWalletUTXOs() {
const endpoint = "https://api.upow.ai/";
const myWallet = new upowjs.Wallet("<PRIVATE_KEY>", endpoint);
try {
const analysis = await upowjs.analyzeUTXOs(myWallet);
if (analysis.response.success) {
const { total, totalValue, ranges, distribution } = analysis.response.analysis;
console.log(`Total UTXOs: ${total}`);
console.log(`Total Value: ${totalValue}`);
// Check distribution by ranges (dust, small, medium, large, etc.)
console.log("UTXO Distribution:", ranges);
// Check most common UTXO amounts
console.log("Most common amounts:", Object.entries(distribution).slice(0, 5));
} else {
console.error(analysis.response.message);
}
} catch (error) {
console.error("Error analyzing UTXOs:", error.message);
}
}
analyzeWalletUTXOs();Consolidate many small UTXOs into a single larger one to improve transaction efficiency and reduce fees.
import { upowjs } from "upowjs";
async function consolidateSmallUTXOs() {
const endpoint = "https://api.upow.ai/";
const myWallet = new upowjs.Wallet("<PRIVATE_KEY>", endpoint);
try {
// Get your wallet address
const myAddress = await upowjs.uPowGetAdress(myWallet);
// Consolidate UTXOs less than or equal to 5 UPOW
// Parameters: wallet, recipient address, max UTXO amount to include, max inputs
const result = await upowjs.consolidateUTXOs(
myWallet,
myAddress.response, // Send back to same address
5, // Max UTXO amount to include
255 // Max inputs to include (default: 255)
);
if (result.response.success) {
console.log(`Successfully consolidated ${result.consolidatedUtxos} UTXOs`);
console.log(`Total amount: ${result.totalAmount}`);
console.log(`Transaction hash: ${result.response.txhash}`);
} else {
console.error(result.response.message);
}
} catch (error) {
console.error("Error consolidating UTXOs:", error.message);
}
}
consolidateSmallUTXOs();Contributions are welcome! Please feel free to submit a pull request or open an issue.
This project is licensed under the MIT License - see the LICENSE file for details.