@biglup/cometa 1.0.7 → 1.0.111

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,30 +18,296 @@
18
18
 
19
19
  <hr>
20
20
 
21
- Cometa.js is a lightweight, high-performance JavaScript library binding for the [libcardano-c](https://github.com/Biglup/cardano-c) Library, designed to simplify blockchain development on Cardano.
21
+ - [Official Website](https://cometa.dev/)
22
+ - [Installation](#installation)
23
+ - [Documentation](https://cometajs.readthedocs.io/en/latest/?badge=latest)
24
+
25
+ <hr>
26
+
27
+ Cometa.js is a lightweight, high-performance JavaScript library binding for the [libcardano-c](https://github.com/Biglup/cardano-c) library, designed to simplify blockchain development on Cardano.
28
+
29
+ Cometa.js packages [libcardano-c](https://github.com/Biglup/cardano-c), compiling, compressing and inlining it into a single JavaScript module, it works consistently in Node.js and browser environments. The result is an extremely compact, single-file distribution of about **~350kb**.
22
30
 
23
- It embeds the WASM module directly into a single JavaScript file, ensuring a minimal footprint, fast performance, and effortless setup without requiring special server configurations.
31
+ Furthermore, this package comes with three module variants: CommonJS, ESM, and a browser-ready IIFE build for direct CDN use. On top of this, the library provides a fully documented high-level, developer-friendly API with TypeScript support.
24
32
 
25
- Built on the foundation of [libcardano-c](https://github.com/Biglup/cardano-c), Cometa.js inherits its commercial-grade quality, rigorous testing, and adherence to strict standards. It is ideal for developing reliable and secure solutions for mission-critical blockchain applications.
33
+ This package works in all JavaScript environments and with all modern bundlers and ecosystems, including Rollup, Webpack, and Node.js projects.
34
+
35
+ Example:
36
+
37
+ ```typescript
38
+ const builder = await wallet.createTransactionBuilder();
39
+
40
+ const unsignedTx = await builder
41
+ .sendLovelace({ address: 'addr....', amount: 12000000n })
42
+ .expiresIn(3600) // in seconds
43
+ .build();
44
+ ```
26
45
  <hr>
27
46
 
28
- - [Official Website](https://cometa.dev/)
29
- - [Installation](#installation)
30
- - [Documentation](https://cometajs.readthedocs.io/en/latest/?badge=latest)
47
+ ## **Conway Era Support**
48
+
49
+ Cometa.js supports all features up to the Conway era, which is the current era of the Cardano blockchain. Conway era
50
+ brought to Cardano decentralized governance. You can see some of the governance related examples in the [examples](examples/) directory:
51
+
52
+ - [Register as DRep (PubKey)](examples/src/drep-pubkey-example.ts)
53
+ - [Register as DRep (Script)](examples/src/drep-script-example.ts)
54
+ - [Submit governance action proposal (Withdrawing from treasury)](examples/src/propose-treasury-withdrawal-example.ts)
55
+ - [Vote for proposal (PubKey DRep)](examples/src/vote-for-proposal-drep-pubkey-example.ts)
56
+ - [Vote for proposal (Script DRep)](examples/src/vote-for-proposal-drep-script-example.ts)
57
+
58
+ These are some of the examples illustrated in the [examples](examples/) directory. However, you should
59
+ be able to build any valid transaction for the current era. See the [Documentation](https://cometajs.readthedocs.io/) for more information.
31
60
 
32
61
  <hr>
33
62
 
34
- ### **Installation**
63
+ ## **Installation**
35
64
 
36
- To install Cometa.js on your project, use the following command:
65
+ ### Node.js or Bundlers (npm/yarn)
66
+ You can add Cometa.js to your project using either npm or Yarn:
37
67
 
38
68
  ```bash
69
+ # Using npm
70
+ npm install @biglup/cometa
71
+
72
+ # Using Yarn
39
73
  yarn add @biglup/cometa
40
74
  ```
41
75
 
76
+ Once installed, you can import it into your application:
77
+
78
+ **ESM (import)**
79
+
80
+ ```javascript
81
+ import * as Cometa from '@biglup/cometa';
82
+
83
+ await Cometa.ready();
84
+ const version = Cometa.getLibCardanoCVersion();
85
+ ```
86
+ **CommonJS (require)**
87
+
88
+ ```javascript
89
+ const Cometa = require('@biglup/cometa');
90
+
91
+ Cometa.ready().then(() => {
92
+ const version = Cometa.getLibCardanoCVersion();
93
+ });
94
+ ```
95
+
96
+ ### Browser (via CDN \<script\> Tag)
97
+
98
+ For use directly in an HTML file without a build step, you can include the library from a CDN like UNPKG. This is ideal for simple tests, online playgrounds, or quick integrations.
99
+
100
+ ```html
101
+ <script src="https://unpkg.com/@biglup/cometa@1.1.102/dist/iife/index.global.js"></script>
102
+ ```
103
+
104
+ The library will then be available on the global Cometa object.
105
+
106
+ ```html
107
+ <script>
108
+ // The Cometa object is now available on the window
109
+ Cometa.ready().then(() => {
110
+ console.log('Cometa is ready!');
111
+ const version = Cometa.getLibCardanoCVersion();
112
+ console.log('Library version:', version);
113
+ });
114
+ </script>
115
+ ```
116
+
117
+ > [!IMPORTANT]
118
+ > Is recommended that you pin the version of the library you are using in your HTML file. For example, use `@1.1.901` instead of `@latest`.
119
+
120
+ <hr>
121
+
122
+ ## **Getting Started**
123
+
124
+ The primary component for creating transactions is the [TransactionBuilder](https://cometajs.readthedocs.io/en/latest/classes/TransactionBuilder.html). It provides a fluent (chainable) API that simplifies the complex process of assembling inputs, outputs, and calculating fees.
125
+
126
+ Before using any part of the library, you must initialize its underlying WebAssembly module. This is done by calling and awaiting [Cometa.ready()](https://cometajs.readthedocs.io/en/latest/functions/ready.html).
127
+
128
+ ```javascript
129
+ import * as Cometa from '@biglup/cometa';
130
+
131
+ await Cometa.ready();
132
+ ```
133
+
134
+ First, establish a connection to the Cardano network using a Provider:
135
+
136
+ ```typescript
137
+ const provider = new Cometa.BlockfrostProvider({
138
+ network: Cometa.NetworkMagic.Preprod,
139
+ projectId: 'YOUR_BLOCKFROST_PROJECT_ID'
140
+ });
141
+ ```
142
+
143
+ > [!TIP]
144
+ >You can create your own providers by implementing the [Cometa.Provider interface](https://cometajs.readthedocs.io/en/latest/interfaces/Provider.html).
145
+
146
+ Cometa comes with two types of wallets depending on your environment.
147
+
148
+ **Programmatic Wallet (for Node.js)**
149
+
150
+ This method creates a software wallet from a mnemonic phrase and is ideal for scripting or backend applications.
151
+
152
+ ```typescript
153
+ const wallet = await Cometa.SingleAddressWallet.createFromMnemonics({
154
+ mnemonics: ['zoo', 'zoo', 'zoo' ...],
155
+ getPassword,
156
+ provider,
157
+ credentialsConfig: {
158
+ account: 0,
159
+ paymentIndex: 0,
160
+ stakingIndex: 0 // Optional, if not provided the wallet will generate an enterprise address
161
+ },
162
+ });
163
+ ```
164
+ **Connect to a Browser Wallet (for dApps)**
165
+
166
+ This method connects to a user's existing browser extension wallet (like Lace, Eternl, etc.) using the [CIP-30](https://cips.cardano.org/cip/CIP-0030) API. You can also request specific CIP extensions.
167
+
168
+ ```typescript
169
+ // In a browser environment:
170
+ const WALLET_NAME = 'lace';
171
+
172
+ // Request access, enabling the CIP-95 (governance) extension
173
+ const cip30Api = await window.cardano[WALLET_NAME].enable({
174
+ extensions: [{ cip: 95 }]
175
+ });
176
+
177
+ // Create a Cometa wallet instance from the CIP-30 API
178
+ const wallet = new Cometa.BrowserExtensionWallet(cip30Api, provider);
179
+ ```
180
+
181
+ The easiest way to start building a transaction is to use the [wallet.createTransactionBuilder()](https://cometajs.readthedocs.io/en/latest/interfaces/Wallet.html#createtransactionbuilder) helper. This automatically configures the builder with your wallet's current UTxOs, sets up collateral (following [CIP-0040](https://cips.cardano.org/cip/CIP-0040)), a change address, and the correct network parameters.
182
+
183
+ ```typescript
184
+ const builder = await wallet.createTransactionBuilder();
185
+ ```
186
+
187
+ Chain methods on the builder to define the transaction's contents. The final [.build()](https://cometajs.readthedocs.io/en/latest/classes/TransactionBuilder.html#build) call performs coin selection, calculates the fee, and assembles the unsigned transaction.
188
+
189
+ ```typescript
190
+ const unsignedTx = await builder
191
+ .sendLovelace({ address: 'addr_test1...', amount: 2000000n }) // Send 2 ADA
192
+ .expiresIn(7200) // Set TTL for 2 hours
193
+ .build();
194
+ ```
195
+
196
+ The unsigned transaction must be signed by the wallet's private keys.
197
+
198
+ ```typescript
199
+ const witnessSet = await wallet.signTransaction(unsignedTx, false);
200
+ ```
201
+
202
+ Cometa comes with an utility function that correctly applies the VKEY witness set to the transaction without altering the original transaction CBOR:
203
+
204
+ ```typescript
205
+ const signedTx = Cometa.applyVkeyWitnessSet(unsignedTx, witnessSet);
206
+ ```
207
+
208
+ Finally, submit the signed transaction to the blockchain and wait for confirmation.
209
+
210
+ ```typescript
211
+ const txId = await wallet.submitTransaction(signedTx);
212
+ console.log(`Transaction submitted successfully! TxID: ${txId}`);
213
+
214
+ const confirmed = await provider.confirmTransaction(txId, 120000 /* in ms */);
215
+ if (confirmed) {
216
+ console.log('Transaction confirmed!');
217
+ }
218
+ ```
219
+
220
+ You can see the full capabilities of the transaction builder here: [TransactionBuilder API](https://cometajs.readthedocs.io/en/latest/classes/TransactionBuilder.html). You can also refer to the library general documentation at [Cometa.js Documentation](https://cometajs.readthedocs.io/en/latest/), and make sure you check out the examples in the [examples](examples/) directory for practical use cases.
42
221
  <hr>
43
222
 
44
- ### **Building and Testing**
223
+ ## **Extending the Transaction Builder**
224
+
225
+ WebAssembly is fundamentally synchronous, while many JavaScript operations, like fetching data in a custom provider or evaluator, are asynchronous (async/await).
226
+
227
+ Cometa.js bridges this gap using Asyncify. This allows the synchronous Wasm code to "pause," hand control back to the JavaScript event loop to wait for your Promise to resolve, and then "resume" the Wasm execution exactly where it left off with the result.
228
+
229
+ This means you can simply write your select and evaluate methods with standard async/await syntax, and Cometa.js will handle the complex underlying state management for you.
230
+
231
+ The [TransactionBuilder API](https://cometajs.readthedocs.io/en/latest/classes/TransactionBuilder.html) allows you to override its core logic for coin selection and transaction evaluation. If these custom implementations are not provided, the builder uses the following defaults:
232
+
233
+ - Coin Selection: A "Largest First" strategy.
234
+ - Transaction Evaluation: A remote service via the configured Provider (e.g., Blockfrost)
235
+
236
+ ### Implementing a Custom CoinSelector
237
+
238
+ The coin selector is responsible for choosing which UTxOs to spend to cover the value required by the transaction's outputs. You can provide your own strategy by implementing the CoinSelector interface.
239
+
240
+ ```typescript
241
+ /**
242
+ * Defines the contract for a coin selection strategy.
243
+ */
244
+ export interface CoinSelector {
245
+ /**
246
+ * Gets the human-readable name of the coin selection strategy.
247
+ * @returns {string} The name of the selector.
248
+ */
249
+ getName(): string;
250
+
251
+ /**
252
+ * Performs the coin selection algorithm.
253
+ *
254
+ * @param {CoinSelectorParams} params - The input parameters for the selection.
255
+ * @returns {Promise<CoinSelectorResult>} A promise that resolves to the result of the selection.
256
+ */
257
+ select(params: CoinSelectorParams): Promise<CoinSelectorResult>;
258
+ }
259
+ ```
260
+
261
+ Once you have your custom implementation, attach it to the builder using the [setCoinSelector](https://cometajs.readthedocs.io/en/latest/classes/TransactionBuilder.html#setcoinselector) method
262
+
263
+ ```typescript
264
+ const myCustomSelector = new MyCoinSelector();
265
+ const builder = await wallet.createTransactionBuilder();
266
+
267
+ builder.setCoinSelector(myCustomSelector);
268
+ ```
269
+
270
+ ### Implementing a Custom TxEvaluator
271
+
272
+ The transaction evaluator is responsible for calculating the execution units (ExUnits) for any Plutus scripts in a transaction. By default, this is done by a remote service. For local evaluation or to use a different service, you can provide a custom implementation of the TxEvaluator interface.
273
+
274
+ ```typescript
275
+ /**
276
+ * Interface for transaction evaluation strategies. This will compute the required
277
+ * execution units for each redeemer in a transaction.
278
+ */
279
+ export interface TxEvaluator {
280
+ /**
281
+ * Gets the human-readable name of the transaction evaluator.
282
+ * @returns {string} The name of the evaluator.
283
+ */
284
+ getName(): string;
285
+
286
+ /**
287
+ * Runs transaction evaluation to obtain script execution units.
288
+ *
289
+ * @param tx - The transaction payload to evaluate (hex-encoded CBOR).
290
+ * @param additionalUtxos - Optional extra UTxOs for the evaluator to consider.
291
+ * @returns A promise that resolves to a list of redeemers with their calculated costs.
292
+ */
293
+ evaluate(tx: string, additionalUtxos?: UTxO[]): Promise<Redeemer[]>;
294
+ }
295
+ ```
296
+
297
+ Attach your custom evaluator to the builder using the setTxEvaluator method.
298
+
299
+ ```typescript
300
+ const myCustomEvaluator = new MyLocalTxEvaluator();
301
+ const builder = await wallet.createTransactionBuilder();
302
+
303
+ builder.setTxEvaluator(myCustomEvaluator);
304
+ ```
305
+
306
+ <hr>
307
+
308
+ ## **Building and Testing**
309
+
310
+ While the underlying [libcardano-c](https://github.com/Biglup/cardano-c) library has its own comprehensive test suite, Cometa.js maintains a separate, dedicated suite of tests. These binding-level tests verify the correctness of the JavaScript-to-Wasm interface, check for potential memory leaks, and ensure the high-level API functions as expected.
45
311
 
46
312
  To build and run the tests, use the following commands:
47
313
 
@@ -53,6 +319,6 @@ yarn test
53
319
 
54
320
  <hr>
55
321
 
56
- ### **License**
322
+ ## **License**
57
323
 
58
324
  Cometa.js is licensed under the Apache 2.0 License. See the [LICENSE](LICENSE) file for more information.