### Install Dependencies, Compile TS, and Run Sample Exchange Script Source: https://github.com/qubic/ts-library/blob/main/README.md Commands to prepare and execute a sample exchange application for the Qubic Network. It includes installing dependencies, compiling TypeScript, and running the exchange simulation. ```bash yarn install tsc node test/sample-exchange.js ``` -------------------------------- ### Install Qubic TS Library with Yarn Source: https://github.com/qubic/ts-library/blob/main/README.md Installs the Qubic TS Library using Yarn. This is the first step to integrating the library into your project. ```bash yarn add @qubic-lib/qubic-ts-library ``` -------------------------------- ### Install Dependencies, Compile TS, and Run Request Balance Script Source: https://github.com/qubic/ts-library/blob/main/README.md Steps to set up and run a Node.js sample application that requests account balances from the Qubic Network. This involves installing dependencies, compiling TypeScript, and executing the script. ```bash yarn install tsc node test/requestBalance.js ``` -------------------------------- ### Send Many (Batch Transfers) using Qubic TS Library and QUTIL Source: https://context7.com/qubic/ts-library/llms.txt This example demonstrates how to send QUBIC to multiple recipients in a single transaction using the QUTIL smart contract via the Qubic TS Library. It defines recipients and amounts, creates a SendMany payload, and constructs a QubicTransaction. The output includes transaction ID, recipient details, and fee information. ```javascript const { QubicTransaction } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/QubicTransaction'); const { PublicKey } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/PublicKey'); const { QubicTransferSendManyPayload } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/transacion-payloads/QubicTransferSendManyPayload'); const { QubicDefinitions } = require('@qubic-lib/qubic-ts-library/dist/QubicDefinitions'); async function sendManyTransfers() { const sourceSeed = "your55characterlowercaselatinseedhereabcdefghijklmnop"; const sourceKey = new PublicKey("CSOXIPNXRTKTCCOEQYNGUOGPOOBCUXZJNOULAFMYBBEUHCHLUZFJZLVEOPGM"); const currentTick = 15000000; // Define recipients and amounts const transfers = [ { address: "SUZFFQSCVPHYYBDCQODEMFAOKRJDDDIRJFFIWFLRDDJQRPKMJNOCSSKHXHGK", amount: 1000000 }, { address: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFXIB", amount: 500000 }, { address: "BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARMID", amount: 2000000 } ]; try { // Create send many payload const sendManyPayload = new QubicTransferSendManyPayload(); transfers.forEach(transfer => { sendManyPayload.addTransfer( new PublicKey(transfer.address), transfer.amount ); }); // Calculate total amount const totalAmount = transfers.reduce((sum, t) => sum + t.amount, 0); // Create transaction targeting QUTIL smart contract const tx = new QubicTransaction() .setSourcePublicKey(sourceKey) .setDestinationPublicKey(QubicDefinitions.QUTIL_ADDRESS) .setAmount(totalAmount + QubicDefinitions.QUTIL_SENDMANY_FEE) .setTick(currentTick + 10) .setInputType(QubicDefinitions.QUTIL_SENDMANY_INPUT_TYPE) .setPayload(sendManyPayload); // Build and sign await tx.build(sourceSeed); console.log("Send Many Transaction ID:", tx.getId()); console.log("Number of recipients:", transfers.length); console.log("Total amount:", totalAmount, "QUBIC"); console.log("Service fee:", QubicDefinitions.QUTIL_SENDMANY_FEE, "QUBIC"); console.log("Total cost:", totalAmount + QubicDefinitions.QUTIL_SENDMANY_FEE, "QUBIC"); transfers.forEach((t, i) => { console.log(`Recipient ${i + 1}: ${t.address} - ${t.amount} QUBIC`); }); } catch (error) { console.error("Send many transaction failed:", error); } } sendManyTransfers(); ``` -------------------------------- ### Build and Publish Qubic TS Library Source: https://github.com/qubic/ts-library/blob/main/README.md Commands to build the Qubic Typescript Library for distribution and publish it to a package registry (e.g., NPM) with public access. ```bash yarn build yarn publish --access public ``` -------------------------------- ### Run Qubic TS Library Tests Source: https://github.com/qubic/ts-library/blob/main/README.md Executes the test suite for the Qubic Typescript Library using the configured test runner. ```bash yarn run test ``` -------------------------------- ### Handle Network Packages with QubicConnector (JavaScript) Source: https://context7.com/qubic/ts-library/llms.txt Demonstrates how to set up a QubicConnector to receive and process different types of network packages. It includes handling responses for tick information, entity data, and transaction acknowledgments. Requires '@qubic-lib/qubic-ts-library'. ```javascript const { QubicPackageType } = require('@qubic-lib/qubic-ts-library/dist/qubic-communication/QubicPackageType'); const { RequestResponseHeader } = require('@qubic-lib/qubic-ts-library/dist/qubic-communication/RequestResponseHeader'); const { QubicConnector } = require('@qubic-lib/qubic-ts-library/dist/QubicConnectorNode'); const connector = new QubicConnector(); connector.onPackageReceived = (receivedPackage) => { const packageType = receivedPackage.header.getType(); const payload = receivedPackage.payLoad; const ipAddress = receivedPackage.ipAddress; console.log("Received package from:", ipAddress); console.log("Package size:", receivedPackage.header.getSize(), "bytes"); // Handle different package types switch(packageType) { case QubicPackageType.RESPOND_CURRENT_TICK_INFO: console.log("Received tick info response"); // Parse tick info from payload break; case QubicPackageType.RESPOND_ENTITY: console.log("Received entity (balance) response"); // Parse entity response from payload break; case QubicPackageType.BROADCAST_TRANSACTION: console.log("Transaction broadcast acknowledgment"); break; default: console.log("Received package type:", packageType); console.log("Payload length:", payload.length); } }; // Create custom request header const customHeader = new RequestResponseHeader( QubicPackageType.REQUEST_CURRENT_TICK_INFO ); customHeader.randomizeDejaVu(); // Add random dejavu for request tracking console.log("Request header size:", customHeader.getSize()); console.log("Request package data:", customHeader.getPackageData()); ``` -------------------------------- ### Import and Use QubicHelper in TypeScript Source: https://github.com/qubic/ts-library/blob/main/README.md Demonstrates how to import the QubicHelper class from the 'qubic-ts-library' and use its 'createIdPackage' method to generate an ID package with keys and a human-readable address. ```typescript // import helper import { QubicHelper } from 'qubic-ts-library/dist/qubicHelper' // create an id Package with private/public key and human readable address const id = await helper.createIdPackage("alsdjflasjfdlasdjflkasdjflasdjlkdjsf"); ``` -------------------------------- ### Create IPO Participation Transaction using Qubic TS Library Source: https://context7.com/qubic/ts-library/llms.txt This snippet shows how to create a transaction to participate in an Initial Public Offering (IPO) on the Qubic network. It utilizes the QubicHelper class and requires a source seed, contract index, price per share, quantity, and the current tick. The output includes transaction details and byte length. ```javascript const { QubicHelper } = require('@qubic-lib/qubic-ts-library/dist/qubicHelper'); async function participateInIPO() { const helper = new QubicHelper(); const sourceSeed = "your55characterlowercaselatinseedhereabcdefghijklmnop"; const contractIndex = 1; // Smart contract index const pricePerShare = 1000000; // Price in QUBIC units const quantity = 50; // Number of shares to purchase const currentTick = 15000000; try { // Create IPO transaction const ipoTx = await helper.createIpo( sourceSeed, contractIndex, pricePerShare, quantity, currentTick + 10 ); console.log("IPO Transaction created"); console.log("Contract index:", contractIndex); console.log("Price per share:", pricePerShare); console.log("Quantity:", quantity); console.log("Total cost:", pricePerShare * quantity, "QUBIC"); console.log("Transaction bytes:", ipoTx.length); // Use connector to broadcast: connector.sendPackage(ipoTx); } catch (error) { console.error("IPO transaction creation failed:", error); } } participateInIPO(); ``` -------------------------------- ### Build Qubic TS Library with Yarn and Webpack Source: https://github.com/qubic/ts-library/blob/main/README.md Commands to build the Qubic Typescript Library into a single JavaScript file using Yarn for dependency management and Webpack for bundling. ```bash yarn install yarn webpack ``` -------------------------------- ### Convert Public Keys and Identities (JavaScript) Source: https://context7.com/qubic/ts-library/llms.txt Demonstrates converting human-readable public key identities to byte arrays and back using QubicHelper. It also shows how to create a PublicKey object and extract its data. Requires '@qubic-lib/qubic-ts-library'. ```javascript const { QubicHelper } = require('@qubic-lib/qubic-ts-library/dist/qubicHelper'); const { PublicKey } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/PublicKey'); async function convertIdentities() { const helper = new QubicHelper(); const humanReadableId = "SUZFFQSCVPHYYBDCQODEMFAOKRJDDDIRJFFIWFLRDDJQRPKMJNOCSSKHXHGK"; try { // Convert human-readable ID to bytes const publicKeyBytes = helper.getIdentityBytes(humanReadableId); console.log("Public key bytes:", publicKeyBytes); console.log("Bytes length:", publicKeyBytes.length); // 32 bytes // Convert bytes back to human-readable ID const recoveredId = await helper.getIdentity(publicKeyBytes); console.log("Recovered ID:", recoveredId); console.log("Match:", recoveredId === humanReadableId); // true // Create PublicKey object const pubKey = new PublicKey(humanReadableId); console.log("PublicKey object created"); console.log("Package size:", pubKey.getPackageSize()); // 32 bytes console.log("Package data:", pubKey.getPackageData()); // Get lowercase version const lowercaseId = await helper.getHumanReadableBytes(publicKeyBytes); console.log("Lowercase ID:", lowercaseId); } catch (error) { console.error("Conversion error:", error); } } convertIdentities(); ``` -------------------------------- ### Connect to Qubic Network and Query Balance (Node.js) Source: https://context7.com/qubic/ts-library/llms.txt Establishes a connection to a Qubic node using QubicConnector and retrieves balance information for specified public IDs. Handles connection events and balance responses. Requires '@qubic-lib/qubic-ts-library'. ```javascript const { QubicConnector } = require('@qubic-lib/qubic-ts-library/dist/QubicConnectorNode'); const { PublicKey } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/PublicKey'); const { QubicEntityResponse } = require('@qubic-lib/qubic-ts-library/dist/qubic-communication/QubicEntityResponse'); // Node addresses to query const ids = [ "SUZFFQSCVPHYYBDCQODEMFAOKRJDDDIRJFFIWFLRDDJQRPKMJNOCSSKHXHGK", "CSOXIPNXRTKTCCOEQYNGUOGPOOBCUXZJNOULAFMYBBEUHCHLUZFJZLVEOPGM" ]; // Qubic node peer address const peerAddress = "65.109.66.49"; let receivedBalances = 0; let totalValue = 0; const connector = new QubicConnector(); // Setup event handlers connector.onReady = () => { console.log("Connector ready"); }; connector.onPeerConnected = () => { console.log("Connected to peer:", peerAddress); // Request balance for each ID ids.forEach(id => { connector.requestBalance(new PublicKey(id)); }); }; connector.onBalance = (balanceResponse) => { if (balanceResponse && balanceResponse.entity) { const balance = balanceResponse.entity.getBalance(); const publicId = balanceResponse.entity.getPublicKey(); console.log("Balance for", publicId, ":", balance, "QUBIC"); totalValue += balance; } receivedBalances++; if (receivedBalances >= ids.length) { console.log("Total value:", totalValue, "QUBIC"); connector.destroy(); } }; connector.onTick = (tick) => { console.log("Current network tick:", tick); }; connector.onPeerDisconnected = () => { console.log("Disconnected from peer"); }; // Start the connector and connect to peer connector.start(); connector.connect(peerAddress); ``` -------------------------------- ### Create Qubic Identity Package from Seed (TypeScript) Source: https://context7.com/qubic/ts-library/llms.txt Generates a complete identity package, including private key, public key, and public ID, from a given seed phrase. Requires the '@qubic-lib/qubic-ts-library' package. ```typescript import { QubicHelper } from '@qubic-lib/qubic-ts-library'; async function createIdentity() { const helper = new QubicHelper(); // Create an identity from a 55-character lowercase seed const seed = "wqbdupxgcaimwdsnchitjmsplzclkqokhadgehdxqogeeiovzvadstt"; try { const idPackage = await helper.createIdPackage(seed); console.log("Public ID:", idPackage.publicId); console.log("Public Key (bytes):", idPackage.publicKey); console.log("Private Key (bytes):", idPackage.privateKey); // Expected output: // Public ID: SUZFFQSCVPHYYBDCQODEMFAOKRJDDDIRJFFIWFLRDDJQRPKMJNOCSSKHXHGK } catch (error) { console.error("Failed to create identity:", error); } } createIdentity(); ``` -------------------------------- ### Transfer Asset via QX Smart Contract (JavaScript) Source: https://context7.com/qubic/ts-library/llms.txt Creates a transaction to transfer assets using the Qubic Exchange (QX) smart contract. It requires a seed, source and new owner public keys, asset name, number of units, and current tick. The output includes the transaction ID, details of the transfer, and a Base64 encoded transaction for API submission. ```javascript const { QubicTransaction } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/QubicTransaction'); const { PublicKey } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/PublicKey'); const { QubicTransferAssetPayload } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/transacion-payloads/QubicTransferAssetPayload'); const { QubicDefinitions } = require('@qubic-lib/qubic-ts-library/dist/QubicDefinitions'); async function transferAsset() { const seed = "wqbdupxgcaimwdsnchitjmsplzclkqokhadgehdxqogeeiovzvadstt"; const sourceKey = new PublicKey("SUZFFQSCVPHYYBDCQODEMFAOKRJDDDIRJFFIWFLRDDJQRPKMJNOCSSKHXHGK"); const newOwnerKey = new PublicKey("CSOXIPNXRTKTCCOEQYNGUOGPOOBCUXZJNOULAFMYBBEUHCHLUZFJZLVEOPGM"); const assetName = "MYTOKEN"; const numberOfUnits = 100; const currentTick = 15000000; try { // Create asset transfer payload const assetTransfer = new QubicTransferAssetPayload() .setIssuer(sourceKey) .setNewOwnerAndPossessor(newOwnerKey) .setAssetName(assetName) .setNumberOfUnits(numberOfUnits); // Create transaction targeting QX smart contract const tx = new QubicTransaction() .setSourcePublicKey(sourceKey) .setDestinationPublicKey(QubicDefinitions.QX_ADDRESS) .setAmount(QubicDefinitions.QX_TRANSFER_ASSET_FEE) // 100 QUBIC fee .setTick(currentTick + 10) .setInputType(QubicDefinitions.QX_TRANSFER_ASSET_INPUT_TYPE) .setPayload(assetTransfer); // Build and sign await tx.build(seed); console.log("Asset transfer transaction ID:", tx.getId()); console.log("Transferring", numberOfUnits, "units of", assetName); console.log("From issuer:", sourceKey); console.log("To owner:", newOwnerKey); console.log("Fee:", QubicDefinitions.QX_TRANSFER_ASSET_FEE, "QUBIC"); // Convert to base64 for API submission const base64Tx = tx.encodeTransactionToBase64(tx.getPackageData()); console.log("Base64 encoded transaction:", base64Tx); } catch (error) { console.error("Asset transfer failed:", error); } } transferAsset(); ``` -------------------------------- ### Create Computor Proposal (JavaScript) Source: https://context7.com/qubic/ts-library/llms.txt Generates a proposal transaction package for computor operators. This function requires the operator's seed phrase, protocol version, computor index, and the proposal URL. It uses QubicHelper to construct the proposal. Dependencies: '@qubic-lib/qubic-ts-library'. ```javascript const { QubicHelper } = require('@qubic-lib/qubic-ts-library/dist/qubicHelper'); async function createComputorProposal() { const helper = new QubicHelper(); const operatorSeed = "operatorseedphrase55characterslowercaselatincharacters"; const protocol = 0; // Protocol version const computorIndex = 123; // Computor index const proposalUrl = "https://example.com/proposal"; try { const proposalPackage = await helper.createProposal( protocol, computorIndex, operatorSeed, proposalUrl ); console.log("Computor proposal created"); console.log("Computor index:", computorIndex); console.log("Proposal URL:", proposalUrl); console.log("Package size:", proposalPackage.length, "bytes"); // Use connector to send: connector.sendPackage(proposalPackage); } catch (error) { console.error("Proposal creation failed:", error); } } createComputorProposal(); ``` -------------------------------- ### Create and Build Simple Transfer Transaction (JavaScript) Source: https://context7.com/qubic/ts-library/llms.txt Creates a signed transaction to transfer QUBIC tokens between two addresses. It requires the source seed, source public key, destination public key, amount, and the current network tick. The output includes the transaction ID, digest, and broadcast data. ```javascript const { QubicTransaction } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/QubicTransaction'); const { PublicKey } = require('@qubic-lib/qubic-ts-library/dist/qubic-types/PublicKey'); const { QubicPackageBuilder } = require('@qubic-lib/qubic-ts-library/dist/QubicPackageBuilder'); const { RequestResponseHeader } = require('@qubic-lib/qubic-ts-library/dist/qubic-communication/RequestResponseHeader'); const { QubicPackageType } = require('@qubic-lib/qubic-ts-library/dist/qubic-communication/QubicPackageType'); async function createTransfer() { const sourceSeed = "your55characterlowercaselatinseedhereabcdefghijklmnop"; const sourceKey = new PublicKey("CSOXIPNXRTKTCCOEQYNGUOGPOOBCUXZJNOULAFMYBBEUHCHLUZFJZLVEOPGM"); const destKey = new PublicKey("SUZFFQSCVPHYYBDCQODEMFAOKRJDDDIRJFFIWFLRDDJQRPKMJNOCSSKHXHGK"); const amount = 1000000; // 1 million QUBIC units const currentTick = 15000000; // Get from network via connector.onTick const tickAddition = 10; // Offset from current tick try { // Create transaction const tx = new QubicTransaction() .setSourcePublicKey(sourceKey) .setDestinationPublicKey(destKey) .setAmount(amount) .setTick(currentTick + tickAddition); // Build and sign the transaction await tx.build(sourceSeed); console.log("Transaction ID:", tx.getId()); console.log("Transaction digest:", tx.digest); // Prepare for broadcasting const header = new RequestResponseHeader( QubicPackageType.BROADCAST_TRANSACTION, tx.getPackageSize() ); const builder = new QubicPackageBuilder(header.getSize()); builder.add(header); builder.add(tx); const broadcastData = builder.getData(); console.log("Transaction ready to broadcast"); console.log("Package size:", broadcastData.length, "bytes"); // To broadcast: connector.sendPackage(broadcastData); } catch (error) { console.error("Transaction creation failed:", error); } } createTransfer(); ``` -------------------------------- ### Verify Qubic Identity Checksum (TypeScript) Source: https://context7.com/qubic/ts-library/llms.txt Validates the checksum of a 60-character Qubic identity string. Returns true if the checksum is correct, false otherwise. Requires the '@qubic-lib/qubic-ts-library' package. ```typescript import { QubicHelper } from '@qubic-lib/qubic-ts-library'; async function verifyIdentity() { const helper = new QubicHelper(); const validId = "SUZFFQSCVPHYYBDCQODEMFAOKRJDDDIRJFFIWFLRDDJQRPKMJNOCSSKHXHGK"; const invalidId = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1234"; try { const isValid = await helper.verifyIdentity(validId); const isInvalid = await helper.verifyIdentity(invalidId); console.log("Valid ID check:", isValid); // true console.log("Invalid ID check:", isInvalid); // false } catch (error) { console.error("Verification error:", error); } } verifyIdentity(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.