Library for web and mobile applications that centralizes the logic of:
- Generation and parsing of QRs for Citizens and Merchants.
- Interaction with the ERC20 contract "izToken".
- Backend call to process tickets.
The library needs to initialize the environment variables once at the start of the application (before using any functionality).
initTokenizationLibEnvVars(newVars: Partial<{ BACK_END_URL: string; BLOCKCHAIN_RPC_URL: string; SMART_CONTRACT_ADDRESS: string }>): void
This method should be called only once at the beginning of the app (for example, in your App.tsx), and it’s required for the library to access the backend and blockchain. This function merges the provided variables with the internal configuration. You can call it multiple times, but it is recommended to do it only once at startup.
import { initTokenizationLibEnvVars } from "tokenization-library";
initTokenizationLibEnvVars({
BACK_END_URL: "https://api.example.com",
SMART_CONTRACT_ADDRESS: "0x123456789abcdef",
BLOCKCHAIN_RPC_URL: "https://rpc.sepolia.org",
});
Generation of strings to be used for QR generation
-
generateCitizenQR(did: string) > Generates a QR to identify a citizen.
-
generateMechantQR(merchantDID: string, citizenDID: string, CID: string) > Generates a QR for merchant payments. The CID will later be used by the citizen to retrieve the ticket breakdown.
-
parseMerchOrCitizenQR(qr: string) > Utility to parse a QR generated by generateMechantQR() or generateCitizenQR() and validate its structure. For citizens, it returns the DID contained in the QR, and for merchants, it returns the original parameters (merchantDID: string, citizenDID: string, CID: string)
Integrates ethers.js to interact with the pre-generated typechains of the izToken contract.
-
getTokenBalance(address: string) > Gets the user’s token balance in readable format.
-
getCitizenAidType(address: string) > Returns the benefit type associated with the user: 0 = NONE, 1 = STATIONERY, 2 = GROCERY.
-
getPartyPermission(address: string) > Returns the user’s current role: 0 = NONE, 1 = CITIZEN, 2 = MERCHANT.
-
getMerchantName(address: string) > Returns the registered merchant's name string
-
sendTokens(privateKey: string, toAddress: string, amount: number, eventData?: string) > Sends tokens to another address with optional data. Returns the transaction hash.
-
burnTokens(privateKey: string, amount: number, eventData?: string) > Burns tokens from the user’s balance. Returns the transaction hash.
Sends tickets in File | Blob | RNFileType format (see type definitions) to the backend API.
- processTicketImage(aid_id: AidCodeType, imageFile: TicketProcessingFileType, authorization: string): Sends a ticket image to the backend to process its products and calculate the economic aid.
-
aid_id (0 | 1 | 2 ) > Identifier for the type of aid (1 = STATIONERY, 2 = GROCERY).
-
imageFile (File | Blob | RNFileType) > Image in File, Blob or React Native format { uri, name, type }.
-
authorization (string) > String with credentials (Bearer or Basic Auth).
The response is an object with:
-
payment_amount > total amount.
-
aid_amount > subsidized amount.
-
aid_products > array of applicable products with name and price.
To build the library:
npm run build
To run tests:
npm run test