@lerna-labs/hydra-sdk 1.0.0-beta.13 → 1.0.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,12 @@
1
- import { serializeNativeScript, resolveScriptHash, deserializeAddress, } from '@meshsdk/core';
1
+ import { deserializeAddress, resolveScriptHash, serializeNativeScript } from '@meshsdk/core';
2
2
  /**
3
- * Create a multisig address using 'any' policy (AND logic)
4
- * @param address1 The first address to include in the script (staking or payment)
5
- * @param address2 The second address to include in the script (staking or payment)
6
- * @param networkId 0 = testnet, 1 = mainnet
7
- * @param scriptType
3
+ * Create a multisig script address from two Cardano addresses.
4
+ *
5
+ * @param address1 - First address (bech32) to include in the native script.
6
+ * @param address2 - Second address (bech32) to include in the native script.
7
+ * @param networkId - `0` for testnet, `1` for mainnet.
8
+ * @param scriptType - `"any"` requires one signature, `"all"` requires both.
9
+ * @returns The script address, serialized CBOR, and script hash.
8
10
  */
9
11
  export function createMultisigAddress(address1, address2, networkId = 0, scriptType = 'any') {
10
12
  const keyHash1 = deserializeAddress(address1).pubKeyHash;
@@ -23,14 +25,18 @@ export function createMultisigAddress(address1, address2, networkId = 0, scriptT
23
25
  ],
24
26
  };
25
27
  const { address, scriptCbor } = serializeNativeScript(script, undefined, networkId);
26
- let scriptHash;
27
- if (scriptCbor != null) {
28
- scriptHash = resolveScriptHash(scriptCbor);
29
- }
28
+ const scriptHash = scriptCbor != null ? resolveScriptHash(scriptCbor) : undefined;
30
29
  return { address, scriptCbor, scriptHash };
31
30
  }
32
31
  /**
33
- * Create a Native Script policy for minting tokens
32
+ * Create a native script policy for minting tokens.
33
+ *
34
+ * @param address1 - Address (bech32) whose key hash is the required signer.
35
+ * @param networkId - `0` for testnet, `1` for mainnet.
36
+ * @param scriptType - `"any"` or `"all"` when combined with time-lock scripts.
37
+ * @param invalidBefore - Optional slot number before which the script is invalid.
38
+ * @param invalidHereafter - Optional slot number after which the script is invalid.
39
+ * @returns The script address, serialized CBOR, and script hash.
34
40
  */
35
41
  export function createNativeScript(address1, networkId = 0, scriptType = 'all', invalidBefore = null, invalidHereafter = null) {
36
42
  const keyHash = deserializeAddress(address1).pubKeyHash;
@@ -56,9 +62,6 @@ export function createNativeScript(address1, networkId = 0, scriptType = 'all',
56
62
  });
57
63
  }
58
64
  const { address, scriptCbor } = serializeNativeScript(script, undefined, networkId);
59
- let scriptHash;
60
- if (scriptCbor != null) {
61
- scriptHash = resolveScriptHash(scriptCbor);
62
- }
65
+ const scriptHash = scriptCbor != null ? resolveScriptHash(scriptCbor) : undefined;
63
66
  return { address, scriptCbor, scriptHash };
64
67
  }
@@ -1,10 +1,36 @@
1
- import { HydraProvider } from "@meshsdk/hydra";
2
- export interface CommitArgs {
1
+ import type { hydraStatus, hydraTransaction } from '@meshsdk/hydra';
2
+ import { HydraProvider } from '@meshsdk/hydra';
3
+ import type { HeadStatus } from '../hydra/messages.js';
4
+ /** UTxO reference for committing funds into a Hydra head. */
5
+ export interface UTxORef {
6
+ /** Transaction hash containing the UTxO. */
3
7
  txHash: string;
4
- txIndex: number;
8
+ /** Output index of the UTxO within the transaction. */
9
+ outputIndex: number;
10
+ }
11
+ /** Arguments for committing UTxOs into a Hydra head. */
12
+ export interface CommitArgs {
13
+ /** One or more UTxO references to commit. */
14
+ utxos: UTxORef[];
15
+ /** Blueprint transaction that spends the committed UTxOs (CBOR-encoded, unsigned). Optional — omit for simple ADA-only commits. */
16
+ blueprintTx?: hydraTransaction;
5
17
  }
18
+ /**
19
+ * High-level controller for Hydra head lifecycle operations.
20
+ *
21
+ * Wraps `HydraProvider` and `HydraInstance` to provide a simplified API
22
+ * for initializing, opening, and closing a Hydra head.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const wrangler = new Wrangler("http://localhost:4001");
27
+ * await wrangler.waitForHeadOpen({
28
+ * utxos: [{ txHash: "abc...", outputIndex: 0 }],
29
+ * blueprintTx: { type: "Tx ConwayEra", cborHex: "...", description: "" },
30
+ * });
31
+ * ```
32
+ */
6
33
  export declare class Wrangler {
7
- private readonly BLOCKFROST_KEY;
8
34
  private mode;
9
35
  readonly provider: HydraProvider;
10
36
  private instance;
@@ -14,16 +40,78 @@ export declare class Wrangler {
14
40
  constructor(url?: string, wsUrl?: string);
15
41
  private createHydraProvider;
16
42
  private createHydraInstance;
43
+ /**
44
+ * Connect to the Hydra node with exponential-backoff retry.
45
+ *
46
+ * Uses `HydraProvider.isConnected()` which establishes the WebSocket
47
+ * **and** waits for the Hydra `Greetings` handshake, unlike the raw
48
+ * `connect()` which only opens the socket.
49
+ *
50
+ * @param maxAttempts - Maximum number of connection attempts (default 5).
51
+ * @param baseDelayMs - Initial retry delay in milliseconds (default 1000). Doubles each attempt, capped at 30 s.
52
+ */
53
+ private connectWithRetry;
54
+ /**
55
+ * Shared helper for promise-based methods that wait for a specific
56
+ * Hydra message. Handles connection, timeout, and settlement in one place.
57
+ */
58
+ private awaitMessage;
59
+ /** Connect the underlying HydraProvider WebSocket with retry logic. */
17
60
  connect(): Promise<void>;
18
- startHead(txHash: string, txIndex: number): Promise<void>;
61
+ /** Disconnect the underlying HydraProvider WebSocket. */
62
+ disconnect(timeout?: number): Promise<void>;
63
+ /** Return the current HydraProvider connection/head status. */
64
+ getStatus(): hydraStatus;
65
+ /** Register a callback for HydraProvider status changes. */
66
+ onStatusChange(callback: (status: hydraStatus) => void): hydraStatus;
67
+ /** Begin the head-opening sequence: init, commit, and listen for state changes. */
68
+ startHead(commitArgs: CommitArgs): Promise<void>;
69
+ /** Begin the head-closing sequence: close, fanout, and finalize. */
19
70
  shutdownHead(): Promise<void>;
20
- waitForHeadClose(timeoutMs: 180000): Promise<void>;
21
- waitForHeadOpen(commitArgs: {
22
- txHash: string;
23
- txIndex: number;
24
- }, timeoutMs?: number): Promise<void>;
25
- getHeadStatus(timeoutMs?: number): Promise<string>;
71
+ /**
72
+ * Wait for the Hydra head to fully close and finalize.
73
+ * @param timeoutMs - Maximum time to wait in milliseconds.
74
+ */
75
+ waitForHeadClose(timeoutMs?: number): Promise<void>;
76
+ /**
77
+ * Wait for the Hydra head to reach the `Open` state.
78
+ * @param commitArgs - UTxO to commit into the head during initialization.
79
+ * @param timeoutMs - Maximum time to wait in milliseconds.
80
+ */
81
+ waitForHeadOpen(commitArgs: CommitArgs, timeoutMs?: number): Promise<void>;
82
+ /**
83
+ * Query the current Hydra head status via a `Greetings` message.
84
+ * @param timeoutMs - Maximum time to wait for the status response.
85
+ * @returns The head status string (e.g. `"Idle"`, `"Open"`, `"Closed"`).
86
+ */
87
+ getHeadStatus(timeoutMs?: number): Promise<HeadStatus>;
26
88
  private doCommit;
89
+ /**
90
+ * Decommit funds from an open Hydra head back to L1.
91
+ *
92
+ * Posts the decommit transaction via `provider.publishDecommit()` (HTTP POST)
93
+ * instead of `provider.decommit()` to avoid overwriting the Wrangler's
94
+ * `onMessage` handler (single-callback replacement pattern).
95
+ *
96
+ * Resolves on `DecommitApproved` — L1 settlement happens asynchronously.
97
+ *
98
+ * @param transaction - The decommit transaction (CBOR-encoded).
99
+ * @param timeoutMs - Maximum time to wait for approval (default 60s).
100
+ */
101
+ decommit(transaction: hydraTransaction, timeoutMs?: number): Promise<void>;
102
+ /**
103
+ * Incrementally commit funds into an already-open Hydra head.
104
+ *
105
+ * Only single-UTxO commits are supported (MeshJS limitation).
106
+ * The raw L1 transaction is submitted to Blockfrost automatically.
107
+ *
108
+ * Resolves on `CommitFinalized`.
109
+ *
110
+ * @param commitArgs - Single UTxO (with optional blueprint) to commit.
111
+ * @param timeoutMs - Maximum time to wait for finalization (default 120s).
112
+ */
113
+ incrementalCommit(commitArgs: CommitArgs, timeoutMs?: number): Promise<void>;
114
+ private doIncrementalCommit;
27
115
  private handleIncoming;
28
116
  private onGreetings;
29
117
  }