> For the complete documentation index, see [llms.txt](https://docs.kasware.xyz/wallet/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kasware.xyz/wallet/developer-documentation/kaspa/kaspa-transaction.md).

# Transaction API

## sendKaspa

```javascript
kasware.sendKaspa(toAddress, sompi, options);
```

Send KAS (Kaspa) to a specified address. This method constructs, signs, and broadcasts a Kaspa transaction in a single call. It requires user approval through a transaction signing dialog.

### Parameters

* **toAddress** - `string`: The destination Kaspa address to receive the KAS. Must be a valid Kaspa address format (e.g., `kaspa:qz...` for mainnet or `kaspatest:qz...` for testnet).
* **sompi** - `number`: The amount to send in sompi. 1 KAS = 100,000,000 sompi. This is the smallest unit of Kaspa currency.
* **options** - `object` (optional):
  * **priorityFee** - `number`: The network priority fee in sompi. A higher priority fee can result in faster transaction confirmation. Default is 0.
  * **payload** - `string`: Optional data to attach to the transaction. The payload will be encoded as hex and included in the transaction. Note that payload data incurs additional network fees based on its length.

### Returns

* `Promise<string>`: Resolves to the serialized transaction JSON string. The transaction has already been broadcast to the Kaspa network. You can parse the JSON to extract the `id` field which contains the transaction ID (txid).

### Behavior

1. **Permission Required**: This method triggers an approval dialog showing the recipient address, amount, and any attached payload.
2. **UTXO Selection**: The wallet automatically selects available UTXOs from the current account to fund the transaction.
3. **Fee Calculation**: Network fees are automatically calculated based on transaction size and any payload data.
4. **Change Address**: Any remaining KAS after the transfer is sent back to the sender's address.
5. **Transaction Broadcast**: Once approved and signed, the transaction is immediately broadcast to the Kaspa network.

### Important Notes

* **Insufficient Balance**: Throws an error if the account doesn't have enough KAS to cover both the transfer amount and network fees.
* **Fee Exceeds Amount**: Throws an error if the calculated network fee is greater than or equal to the transfer amount.
* **No UTXOs**: Throws an error if no UTXOs are found for the sender's address.
* **Payload Fees**: Transactions with payload data incur additional network fees calculated as `payload_hex_length * 1 sompi`.

### Example

```javascript
// Basic KAS transfer
try {
  const txJson = await window.kasware.sendKaspa(
    "kaspa:qzhkxxaully72gk23lyn7z3d9tdzdpw48ujsavrwlulekyk7pkxzzxj26hcsq",
    100000000  // 1 KAS (in sompi)
  );
  const tx = JSON.parse(txJson);
  console.log("Transaction ID:", tx.id);
} catch (e) {
  console.error("Transfer failed:", e);
}

// Transfer with priority fee and payload
try {
  const txJson = await window.kasware.sendKaspa(
    "kaspa:qzhkxxaully72gk23lyn7z3d9tdzdpw48ujsavrwlulekyk7pkxzzxj26hcsq",
    100000000,  // 1 KAS (in sompi)
    {
      priorityFee: 1000,  // Priority fee for faster confirmation
      payload: "Hello Kaspa!"  // Optional message attached to transaction
    }
  );
  const tx = JSON.parse(txJson);
  console.log("Transaction ID:", tx.id);
} catch (e) {
  console.error("Transfer failed:", e);
}

// Convert KAS to sompi helper
const KAS_TO_SOMPI = 100000000;
const amountInKas = 0.5;
const amountInSompi = Math.floor(amountInKas * KAS_TO_SOMPI);

const txJson = await window.kasware.sendKaspa(
  "kaspa:qzhkxxaully72gk23lyn7z3d9tdzdpw48ujsavrwlulekyk7pkxzzxj26hcsq",
  amountInSompi
);
```

### Related APIs

* **`getBalance()`** - Check account balance before sending
* **`pushTx(txJsonString)`** - Broadcast a pre-signed transaction
* **`signPskt({ txJsonString, options })`** - Sign a transaction without broadcasting

***

## signPskt

```javascript
kasware.signPskt({ txJsonString, options });
```

Sign a Partially Signed Kaspa Transaction (PSKT). This method allows dApps to sign specific inputs of a transaction using the wallet's private key. PSKT is useful for multi-signature scenarios, coinjoin transactions, or any case where multiple parties need to sign the same transaction.

### Parameters

* **txJsonString** - `string`: A JSON string representing the PSKT transaction. The transaction must be in "Safe JSON" format that can be deserialized by kaspa-wasm's `Transaction.deserializeFromSafeJSON()` method.
* **options** - `object` (optional):
  * **signInputs** - `SignInput[]`: An array specifying which inputs to sign and their sighash types.
    * **index** - `number`: The index of the input in the transaction's inputs array to sign.
    * **sighashType** - `number`: The sighash type that determines which parts of the transaction are committed to the signature. Defaults to `SighashType.All` (1) if not specified.

### Sighash Types

Kaspa supports the following sighash types, which define what transaction data is covered by the signature:

| Type                 | Value | Binary       | Description                                                                       |
| -------------------- | ----- | ------------ | --------------------------------------------------------------------------------- |
| `All`                | 1     | `0b00000001` | Signs all inputs and outputs. Most common for standard transactions.              |
| `None`               | 2     | `0b00000010` | Signs all inputs but no outputs. Allows anyone to modify outputs.                 |
| `Single`             | 4     | `0b00000100` | Signs all inputs and only the output at the same index as the input being signed. |
| `AllAnyOneCanPay`    | 129   | `0b10000001` | Signs all outputs, but only the current input. Allows others to add inputs.       |
| `NoneAnyOneCanPay`   | 130   | `0b10000010` | Signs only the current input, no outputs. Maximum flexibility.                    |
| `SingleAnyOneCanPay` | 132   | `0b10000100` | Signs only the current input and its corresponding output.                        |

> **Reference**: [Kaspa Sighash Documentation](https://kaspa-mdbook.aspectron.com/transactions/sighashes.html)

### Returns

* `Promise<string>`: A JSON string of the signed transaction in "Safe JSON" format. The returned transaction contains signatures for the specified inputs and can be broadcast to the network using `pushTx()`.

### Behavior

1. **Permission Required**: This method requires user approval via the `SignPsbt` approval dialog.
2. **Default Signing**: If `options.signInputs` is not provided, the wallet will attempt to sign all inputs with `SighashType.All`.
3. **Partial Signing**: You can sign only specific inputs by providing explicit `signInputs`. This is useful for multi-signature or coinjoin transactions.
4. **Security**: The wallet only signs inputs that belong to addresses derived from the current account's public key.

### Example

```javascript
try {
  // Define sighash types for reference
  const SighashType = {
    All: 0b00000001, // 1
    None: 0b00000010, // 2
    Single: 0b00000100, // 4
    AllAnyOneCanPay: 0b10000001, // 129
    NoneAnyOneCanPay: 0b10000010, // 130
    SingleAnyOneCanPay: 0b10000100 // 132
  };

  // PSKT transaction JSON string (Safe JSON format from kaspa-wasm)
  const pskt = `{"id":"6664c0a75a041dd1f591c5ecc539875a99e5a49aaacb2e1ef04e354d0e69c271","version":0,"inputs":[{"transactionId":"006866a332181f4e3aab471af6435a645f5ca33697633fa20233af59ae4e8b2f","index":1,"sequence":"0","sigOpCount":1,"signatureScript":"","utxo":{"address":"kaspatest:qz9dvce5d92czd6t6msm5km3p5m9dyxh5av9xkzjl6pz8hhvc4q7wqg8njjyp","amount":"99986271","scriptPublicKey":"0000208ad66334695581374bd6e1ba5b710d365690d7a758535852fe8223deecc541e7ac","blockDaaScore":"87857448","isCoinbase":false}},{"transactionId":"c46a6fcd4746bbe60a68bf2efb543a65685f81b72440fcaa271cf86b25720d31","index":0,"sequence":"0","sigOpCount":1,"signatureScript":"","utxo":{"address":"kaspatest:qz9dvce5d92czd6t6msm5km3p5m9dyxh5av9xkzjl6pz8hhvc4q7wqg8njjyp","amount":"49100001963","scriptPublicKey":"0000208ad66334695581374bd6e1ba5b710d365690d7a758535852fe8223deecc541e7ac","blockDaaScore":"87513486","isCoinbase":false}}],"outputs":[{"value":"110000000","scriptPublicKey":"000020ab4b389073830cb400647fb8dc116e6b8a71c17e1c09ba7eaef2ddd78616f306ac"},{"value":"49089970826","scriptPublicKey":"0000208ad66334695581374bd6e1ba5b710d365690d7a758535852fe8223deecc541e7ac"}],"subnetworkId":"0000000000000000000000000000000000000000","lockTime":"0","gas":"0","mass":"3154","payload":""}`;

  // Sign specific inputs with All sighash type
  const signedTxJson = await window.kasware.signPskt({
    txJsonString: pskt,
    options: {
      signInputs: [
        { index: 0, sighashType: SighashType.All },
        { index: 1, sighashType: SighashType.All }
      ]
    }
  });

  console.log("Signed transaction:", signedTxJson);

  // Optionally broadcast the signed transaction
  // const txid = await window.kasware.pushTx(signedTxJson);
  // console.log("Transaction ID:", txid);

} catch (e) {
  console.error("Failed to sign PSKT:", e);
}
```

### Related APIs

* **`pushTx(txJsonString)`** - Broadcast a signed transaction to the Kaspa network.
* **`decodePskt(psktJsonString, options)`** - Decode and inspect a PSKT transaction without signing.

### Use Cases

1. **Multi-signature Transactions**: Collect signatures from multiple parties for a single transaction.
2. **Coinjoin/Mixing**: Participate in privacy-enhancing transaction protocols.
3. **Dex Swaps**: Atomic swap transactions where both parties need to sign.
4. **KRC-20 Token Trading**: Sign buy/sell orders on KRC-20 marketplaces.

***

## pushPskt

```javascript
kasware.pushPskt({ txJsonString, options });
```

Sign a Partially Signed Kaspa Transaction and submit it, in one approved step. This is `signPskt` plus a broadcast: the approval screen is the same, and the difference is only what comes back — a transaction ID instead of the signed bytes.

### Parameters

* **txJsonString** - `string`: A JSON string representing the PSKT transaction, in "Safe JSON" format.
* **options** - `object` (optional):
  * **signInputs** - `SignInput[]`: Which inputs to sign and their sighash types, the same shape as `signPskt`. Defaults to signing every input the wallet owns with `SighashType.All`.

### Returns

* `Promise<string>`: The transaction ID.

### Behavior

1. **Permission Required**: The same approval dialog as `signPskt`.
2. **Covenant-aware submission**: The signed transaction is submitted as the live object rather than through a re-parse, so a transaction carrying KIP-20 v1 covenant bindings submits with those bindings intact.

### Example

```javascript
try {
  const txid = await window.kasware.pushPskt({
    txJsonString: pskt,
    options: {
      signInputs: [{ index: 0, sighashType: 1 }]  // SighashType.All
    }
  });
  console.log("Transaction ID:", txid);
} catch (e) {
  console.error("Failed to sign and submit:", e);
}
```

### Related APIs

* **`signPskt({ txJsonString, options })`** - The same signing without the submission, when you want the signed bytes back.
* **`pushTx(txJsonString)`** - Broadcast a transaction that is already signed.
* **`signKcc20Deploy` / `signKcc20Mint` / `signKcc20Transfer`** - The KCC-20 equivalents. Prefer those for a KCC-20 covenant transaction: they verify the covenant states before signing, which `pushPskt` does not.

### When to use which

| You want to                            | Use                                                       |
| -------------------------------------- | --------------------------------------------------------- |
| Sign and broadcast in one call         | `pushPskt`                                                |
| Sign, then broadcast yourself          | `signPskt`, then `pushTx`                                 |
| Sign a KCC-20 deploy, mint or transfer | `signKcc20Deploy` / `signKcc20Mint` / `signKcc20Transfer` |
