|
| 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 | +``` |
0 commit comments