|
| 1 | + |
| 2 | +# Building Transactions in Hydra |
| 3 | + |
| 4 | +**1. Create a Sample Transaction JSON** |
| 5 | + |
| 6 | +```ts |
| 7 | +const createSampleOutputTx = ( |
| 8 | + selectionAddress: string, |
| 9 | + outputAddress: string |
| 10 | +) => ({ |
| 11 | + selections: [selectionAddress], |
| 12 | + outputs: [ |
| 13 | + { |
| 14 | + address: outputAddress, |
| 15 | + value: 2_000_000, |
| 16 | + datum: { constructor: 1, fields: [] }, |
| 17 | + }, |
| 18 | + { |
| 19 | + address: outputAddress, |
| 20 | + value: 2_000_000, |
| 21 | + datum: { constructor: 2, fields: [] }, |
| 22 | + }, |
| 23 | + ], |
| 24 | +}); |
| 25 | +``` |
| 26 | + |
| 27 | +**2. Generate Address from Key** |
| 28 | + |
| 29 | +```ts |
| 30 | +import { setup } from "libcardano/lib/cardano/crypto"; |
| 31 | +import { Ed25519Key } from "libcardano/cardano/primitives/keys"; |
| 32 | +import { ShelleyWallet } from "libcardano/cardano/primitives/address"; |
| 33 | + |
| 34 | +await setup(); //this is necessary to run Ed25519Key funcitons |
| 35 | + |
| 36 | +const testWalletSigningKey = await Ed25519Key.fromCardanoCliFile( |
| 37 | + path.join("src", "example.sk") |
| 38 | +); |
| 39 | +const testWalletAddress = new ShelleyWallet(testWalletSigningKey).addressBech32( |
| 40 | + 0 |
| 41 | +); // 0 = testnet |
| 42 | +``` |
| 43 | + |
| 44 | +**3. Create a Hydra Wallet** |
| 45 | + |
| 46 | +```ts |
| 47 | +async function createHydraWallet( |
| 48 | + service: KuberHydraService, |
| 49 | + ed25519Key: Ed25519Key, |
| 50 | + network: 0 | 1 |
| 51 | +): Promise<HydraWallet> { |
| 52 | + try { |
| 53 | + const shelleyWallet = new ShelleyWallet(ed25519Key); |
| 54 | + const hydraWallet = new HydraWallet(service, shelleyWallet, network); |
| 55 | + return hydraWallet; |
| 56 | + } catch (error: any) { |
| 57 | + return respondWithError(error); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const myWallet = await createHydraWallet(hydraService, testWalletSigningKey, 0); |
| 62 | +``` |
| 63 | + |
| 64 | +> The `HydraWallet` supports CIP-30 functions such as `getUTxO`, `signData`, `signTx`, `submitTx`, etc. |
| 65 | +
|
| 66 | +**4. Build the Transaction** |
| 67 | + |
| 68 | +```ts |
| 69 | +// For the sake of simplicity, we can fund our own address since the protocol fees are `0`. |
| 70 | +const txBody = createSampleOutputTx(testWalletAddress, testWalletAddress); |
| 71 | +const buildTxResponse = await hydraService.buildTx(txBody, false); // false = don't submit as we have not signed the transaction yet |
| 72 | +``` |
| 73 | + |
| 74 | +The response from this function will provide the transaction details in the following format: |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "cborHex": "84a400d90102818258204051dd270c1a51da8645b6c91d23053273547f1f853929fbec5519527e18266d0d0183a300581d60182aeee511419facd4bf4eab7538187288a55a633f579be0cf36897b011a001e8480028201d81843d87b80a300581d60182aeee511419facd4bf4eab7538187288a55a633f579be0cf36897b011a001e8480028201d81843d87b8082581d60182aeee511419facd4bf4eab7538187288a55a633f579be0cf36897b1a00f4240002000ed9010281581c182aeee511419facd4bf4eab7538187288a55a633f579be0cf36897ba0f5f6", |
| 79 | + "description": "Ledger Cddl Format", |
| 80 | + "hash": "ff6de11af9d0998a85c3eb5333f4afd173a904164630fc0717be8ee819900d4f", |
| 81 | + "type": "Unwitnessed Tx ConwayEra" |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +**5. Sign and Submit** |
| 86 | + |
| 87 | +```ts |
| 88 | +const txCborHex = buildTxResponse.cborHex; |
| 89 | +const signature = await myWallet.signTx(txCborHex); |
| 90 | +``` |
| 91 | + |
| 92 | +The wallet's signTx function returns a witness set in cbor hex format. To add this witness set to the transaction, we need to merge the witnesses. |
| 93 | + |
| 94 | +```ts |
| 95 | +const signedTxHex = txWithMergedSignature(txCborHex, signature); |
| 96 | +``` |
| 97 | + |
| 98 | +Now, we have the signed transaction that we can submit to the hydra head. |
| 99 | + |
| 100 | +```ts |
| 101 | +const submissionResult = await myWallet.submitTx(signedTxHex); |
| 102 | +console.log("Tx Submitted: ", submissionResult); |
| 103 | +``` |
| 104 | + |
| 105 | +The response from the wallet is the signed transction cbor hex upon successful submission. |
0 commit comments