Skip to content

Commit b4372f5

Browse files
committed
Revamp docs section
1 parent 898d49a commit b4372f5

File tree

12 files changed

+907
-94
lines changed

12 files changed

+907
-94
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
sidebar_position: 2
3+
sidebar_label: Commiting - Using cip-30
4+
---
5+
6+
# Using CIP-30 Interface
7+
8+
The Kuber Hydra Client can be integrated with a CIP-30 compatible wallet to sign transactions. This guide demonstrates how to set up and use a `Cip30ShelleyWallet` with the `KuberHydraApiProvider`.
9+
10+
## Prerequisites
11+
12+
- Node.js environment
13+
- `libcardano` and `libcardano-wallet` installed.
14+
- Access to a running Hydra node and its credentials (e.g., `node.addr`, `funds.sk`).
15+
16+
This example demonstrates how to set up a `Cip30ShelleyWallet` and use it to sign and submit a transaction to a Hydra Head.
17+
18+
```typescript
19+
import { readFileSync } from "fs";
20+
import { loadCrypto, Ed25519Key, Value } from "libcardano";
21+
import { ShelleyWallet, Cip30ShelleyWallet } from "libcardano-wallet";
22+
import { KuberHydraApiProvider } from "../../../src/service/KuberHydraApiProvider"; // Adjust path as needed
23+
import { UTxO } from "libcardano/cardano/serialization";
24+
25+
async function runCip30CommitExample() {
26+
// Initialize Hydra API Provider
27+
const hydra = new KuberHydraApiProvider("http://172.31.6.1:8082"); // Replace with your Hydra node URL
28+
29+
// Load node address and test wallet signing key
30+
const node_addr_path = process.env.HOME + "/.cardano/preview/hydra-0/credentials/node.addr";
31+
const nodeAddr = readFileSync(node_addr_path).toString("utf-8").trim();
32+
const testWalletSigningKey = await Ed25519Key.fromCardanoCliJson(
33+
JSON.parse(readFileSync(process.env.HOME + "/.cardano/preview/hydra-0/credentials/funds.sk", "utf-8")),
34+
);
35+
36+
// Setup libcardano crypto and Shelley wallet
37+
await loadCrypto();
38+
const shelleyWallet = new ShelleyWallet(testWalletSigningKey);
39+
console.log("Wallet", shelleyWallet.toJSON());
40+
41+
// Create Cip30ShelleyWallet instance
42+
// The first two arguments are for the L1 API provider and the Hydra API provider, respectively.
43+
// In this case, KuberHydraApiProvider implements both interfaces.
44+
const cip30Wallet = new Cip30ShelleyWallet(hydra, hydra, shelleyWallet, 0);
45+
const walletAddress = (await cip30Wallet.getChangeAddress()).toBech32();
46+
47+
console.log("Wallet Address:", walletAddress);
48+
49+
// Ensure head is in 'Initial' state before committing
50+
const headState = await hydra.queryHeadState();
51+
if (headState.state !== "Initial") {
52+
console.log("Head is not in 'Initial' state. Skipping commit example.");
53+
return;
54+
}
55+
56+
// Query UTxOs from the L1 chain using the wallet address
57+
const l1Utxos = await hydra.l1Api.queryUTxOByAddress(walletAddress);
58+
if (l1Utxos.length === 0) {
59+
throw new Error(`No balance on ${walletAddress} in L1 chain`);
60+
}
61+
62+
// Select UTxOs to commit (e.g., the first one with a value greater than 4 ADA)
63+
const selectedUtxos = l1Utxos.filter((x: UTxO) => x.txOut.value.greaterThan(Value.fromString("4A")));
64+
if (selectedUtxos.length === 0) {
65+
throw new Error(`Not enough balance on ${walletAddress} in L1 chain for commit example`);
66+
}
67+
68+
const txIn = selectedUtxos[0].txIn;
69+
const utxoToCommit = [`${txIn.txHash.toString("hex")}#${txIn.index}`];
70+
71+
// Build the commit transaction using Hydra API
72+
const commitResult = await hydra.commit({ utxos: utxoToCommit });
73+
console.log("Transaction to be signed:", commitResult.hash);
74+
75+
// Sign the transaction using the CIP-30 wallet
76+
const signResult = await cip30Wallet.signTx(commitResult.cborHex);
77+
78+
// Submit the signed transaction to the L1 chain
79+
await hydra.l1Api.submitTx(signResult.updatedTxBytes.toString("hex"));
80+
console.log("Submitted Commit transaction hash:", commitResult.hash);
81+
82+
// Wait for the transaction to be confirmed and head state to change
83+
await hydra.l1Api.waitForUtxoConsumption(selectedUtxos[0].txIn, 280000);
84+
console.log("Commit transaction confirmed.");
85+
86+
}
87+
88+
runCip30CommitExample();
89+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
sidebar_position: 3
3+
sidebar_label: Building and Submitting TXs
4+
---
5+
6+
# Building and Submitting TXs
7+
8+
This guide demonstrates how to build and submit transactions within an open Hydra Head using the cip30 interface and `KuberHydraApiProvider`.
9+
10+
## Prerequisites
11+
12+
- Node.js environment
13+
- `libcardano` and `libcardano-wallet` installed.
14+
- An active Hydra Head (in "Open" state).
15+
- A configured `KuberHydraApiProvider` instance.
16+
17+
This example demonstrates how to build and submit a simple transaction (sending Lovelace to an address) within an open Hydra Head using the `KuberHydraApiProvider` and a CIP-30 compatible wallet.
18+
19+
```typescript
20+
import { KuberHydraApiProvider } from "kuber-client"; // Adjust path as needed
21+
import { Value,Ed25519Key ,loadCrypto } from "libcardano";
22+
import { ShelleyWallet, Cip30ShelleyWallet } from "libcardano-wallet";
23+
import { readFileSync } from "fs";
24+
25+
async function runBuildAndSubmitTransactionExample() {
26+
// Initialize Hydra API Provider (replace with your Hydra node URL)
27+
const hydra = new KuberHydraApiProvider("http://172.31.6.1:8082");
28+
29+
// Load test wallet signing key
30+
const testWalletSigningKey = await Ed25519Key.fromCardanoCliJson(
31+
JSON.parse(readFileSync(process.env.HOME + "/.cardano/preview/hydra-0/credentials/funds.sk", "utf-8")),
32+
);
33+
34+
// Setup libcardano crypto and Shelley wallet
35+
await loadCrypto();
36+
const shelleyWallet = new ShelleyWallet(testWalletSigningKey);
37+
const cip30Wallet = new Cip30ShelleyWallet(hydra, hydra, shelleyWallet, 0);
38+
const walletAddress = (await cip30Wallet.getChangeAddress()).toBech32();
39+
40+
console.log("Wallet Address:", walletAddress);
41+
42+
// Ensure head is in 'Open' state
43+
const headState = await hydra.queryHeadState();
44+
if (headState.state !== "Open") {
45+
console.log("Head is not in 'Open' state. Please ensure it's in 'Open' state before running this example.");
46+
return;
47+
}
48+
console.log("Hydra Head is Open. Proceeding with transaction.");
49+
50+
// Define the transaction outputs
51+
const transaction = {
52+
outputs: [{ address: walletAddress, value: { lovelace: "3000000" } }], // Sending 3 ADA
53+
changeAddress: walletAddress,
54+
};
55+
56+
try {
57+
// Use the buildAndSubmitWithWallet function from KuberProvider
58+
const submitResult = await hydra.buildAndSubmitWithWallet(cip30Wallet, transaction);
59+
console.log("Transaction submitted to Hydra Head. Result:", submitResult);
60+
61+
} catch (error: unknown) {
62+
if (error instanceof Error) {
63+
console.error("Error building or submitting transaction:", error.message);
64+
} else {
65+
console.error("An unknown error occurred:", error);
66+
}
67+
}
68+
}
69+
70+
runBuildAndSubmitTransactionExample();
71+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
sidebar_position: 1
3+
sidebar_label: Working with Wallets
4+
---
5+
6+
# Working with Wallets
7+
8+
This guide demonstrates how to interact with wallets to query UTxOs from both the Layer 1 (L1) Cardano chain and the Hydra Head using the `KuberHydraApiProvider`. We will use same cip30 interface for htis example
9+
10+
## Prerequisites
11+
12+
- Node.js environment
13+
- `libcardano` and `libcardano-wallet` installed.
14+
- Access to a running Hydra node and its credentials (e.g., `node.addr`, `funds.sk`).
15+
- An active Hydra Head (can be in any state, but "Open" is ideal for demonstrating Hydra UTxOs).
16+
17+
## Example: Querying UTxOs from L1 and Hydra
18+
19+
This example sets up a wallet and then queries its UTxOs on both the L1 chain and within the Hydra Head.
20+
21+
```typescript
22+
import { readFileSync } from "fs";
23+
import { loadCrypto, Ed25519Key } from "libcardano";
24+
import { ShelleyWallet, Cip30ShelleyWallet } from "libcardano-wallet";
25+
import { KuberHydraApiProvider } from "kuber-client"; // Adjust path as needed
26+
27+
async function runWalletQueryExample() {
28+
// Initialize Hydra API Provider
29+
const hydra = new KuberHydraApiProvider("http://172.31.6.1:8082"); // Replace with your Hydra node URL
30+
31+
// Load test wallet signing key
32+
const testWalletSigningKey = await Ed25519Key.fromCardanoCliJson(
33+
JSON.parse(readFileSync(process.env.HOME + "/.cardano/preview/hydra-0/credentials/funds.sk", "utf-8")),
34+
);
35+
36+
// Setup libcardano crypto and Shelley wallet
37+
await loadCrypto();
38+
const shelleyWallet = new ShelleyWallet(testWalletSigningKey);
39+
console.log("Base Shelley Wallet:", shelleyWallet.toJSON());
40+
41+
// Create a wallet instance for Hydra operations
42+
const hydraWallet = new Cip30ShelleyWallet(hydra, hydra, shelleyWallet, 0);
43+
const hydraWalletAddress = (await hydraWallet.getChangeAddress()).toBech32();
44+
console.log("Hydra Wallet Address:", hydraWalletAddress);
45+
46+
// Create a wallet instance for Layer 1 (L1) operations
47+
// Pass hydra.l1Api as both L1 and Hydra API provider for this wallet,
48+
// as it only interacts with L1.
49+
const layer1Wallet = new Cip30ShelleyWallet(hydra.l1Api, hydra.l1Api, shelleyWallet, 0);
50+
const layer1WalletAddress = (await layer1Wallet.getChangeAddress()).toBech32();
51+
console.log("Layer 1 Wallet Address:", layer1WalletAddress);
52+
53+
54+
55+
console.log("\n--- Querying Balance from Layer 1 (L1) Chain using layer1Wallet ---");
56+
// Use the layer1Wallet's getBalance() method (CIP-30)
57+
const l1Balance = await layer1Wallet.getBalance();
58+
console.log(`L1 Balance for address ${layer1WalletAddress}: ${l1Balance.lovelace} lovelace`);
59+
60+
console.log("\n--- Querying Balance from Hydra Head using hydraWallet ---");
61+
// Use the hydraWallet's getBalance() method (CIP-30)
62+
const hydraBalance = await hydraWallet.getBalance();
63+
console.log(`Hydra Head Balance for address ${hydraWalletAddress}: ${hydraBalance.lovelace} lovelace`);
64+
65+
}
66+
67+
runWalletQueryExample();
68+
```
69+
70+
This example demonstrates how to use the `KuberHydraApiProvider` to query the balance of wallets from both the underlying L1 chain (via `hydra.l1Api`) and directly from the Hydra Head. This is crucial for understanding the state of funds available to your wallet in both environments.

0 commit comments

Comments
 (0)