|
| 1 | +--- |
| 2 | +sidebar_position: 5 |
| 3 | +sidebar_label: buildAndSignWithWallet |
| 4 | +--- |
| 5 | + |
| 6 | +# buildAndSignWithWallet |
| 7 | + |
| 8 | +`buildAndSignWithWallet` is an asynchronous function that builds a transaction, automatically adds UTxOs from the wallet, and then signs the transaction using the provided CIP-30 compatible wallet. |
| 9 | + |
| 10 | +## Function Signature |
| 11 | + |
| 12 | +```typescript |
| 13 | +async buildAndSignWithWallet( |
| 14 | + cip30OrProvider: Cip30 | Cip30Provider, |
| 15 | + buildRequest: Record<string, any>, |
| 16 | + autoAddCollateral = false, |
| 17 | + estimatedSpending?: number | bigint, |
| 18 | +): Promise<SignTxResult> |
| 19 | +``` |
| 20 | + |
| 21 | +## Parameters |
| 22 | + |
| 23 | +- `cip30OrProvider`: A CIP-30 compatible wallet instance or provider. |
| 24 | +- `buildRequest`: An object following Kuber's transaction builder JSON specification. For a comprehensive reference on transaction builder fields and their usage, please refer to the [KuberIDE TxBuilder Object Reference](https://kuberide.com/kuber/docs/tx-builder-reference). |
| 25 | +- `autoAddCollateral`: An optional `boolean` indicating whether to automatically add collateral from the provider. Defaults to `false`. |
| 26 | +- `estimatedSpending`: An optional `number` or `bigint` representing the estimated amount of Lovelace to be spent. |
| 27 | + |
| 28 | +## Returns |
| 29 | + |
| 30 | +A `Promise` that resolves to a `SignTxResult` object, containing the signed transaction. |
| 31 | + |
| 32 | +## Example |
| 33 | + |
| 34 | +```typescript |
| 35 | +import { KuberHydraApiProvider } from "kuber-client"; |
| 36 | +import { loadCrypto, Ed25519Key } from "libcardano"; |
| 37 | +import { ShelleyWallet, Cip30ShelleyWallet } from "libcardano-wallet"; |
| 38 | +import { readFileSync } from "fs"; |
| 39 | + |
| 40 | +async function main() { |
| 41 | + await loadCrypto(); |
| 42 | + |
| 43 | + const hydra = new KuberHydraApiProvider("http://localhost:8081"); // Replace with your Hydra API URL |
| 44 | + |
| 45 | + // Load test wallet signing key |
| 46 | + const testWalletSigningKey = await Ed25519Key.fromCardanoCliJson( |
| 47 | + JSON.parse(readFileSync(process.env.HOME + "/.cardano/preview/hydra-0/credentials/funds.sk", "utf-8")), |
| 48 | + ); |
| 49 | + |
| 50 | + // Setup libcardano crypto and Shelley wallet |
| 51 | + const shelleyWallet = new ShelleyWallet(testWalletSigningKey); |
| 52 | + const cip30Wallet = new Cip30ShelleyWallet(hydra, hydra, shelleyWallet, 0); |
| 53 | + const walletAddress = (await cip30Wallet.getChangeAddress()).toBech32(); |
| 54 | + |
| 55 | + const transaction = { |
| 56 | + outputs: [{ address: walletAddress, value: "1_000_000" }], // Sending 1 ADA |
| 57 | + changeAddress: walletAddress, |
| 58 | + }; |
| 59 | + |
| 60 | + try { |
| 61 | + console.log("Building and signing transaction with wallet..."); |
| 62 | + const signedTx = await hydra.buildAndSignWithWallet(cip30Wallet, transaction); |
| 63 | + console.log("Signed transaction:", signedTx); |
| 64 | + console.log("Signed CBOR Hex:", signedTx.updatedTxBytes.toString('hex')); |
| 65 | + } catch (error) { |
| 66 | + console.error("Error building and signing transaction with wallet:", error); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +main(); |
0 commit comments