@lerna-labs/hydra-sdk 1.0.0-beta.18 → 1.0.0-beta.20
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,5 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from 'node:events';
|
|
2
|
-
import type { ClientInput, ConnectionState, HydraStatus } from './types.js';
|
|
2
|
+
import type { ClientInput, ConnectionState, HydraStatus, HydraWsMessage } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Thin WebSocket wrapper for the Hydra node.
|
|
5
5
|
*
|
|
@@ -16,6 +16,7 @@ export declare class HydraWebSocket extends EventEmitter {
|
|
|
16
16
|
private readonly url;
|
|
17
17
|
private _status;
|
|
18
18
|
private _connectionState;
|
|
19
|
+
private _lastGreetings;
|
|
19
20
|
constructor(wsUrl: string);
|
|
20
21
|
/** Current connection state. */
|
|
21
22
|
get connectionState(): ConnectionState;
|
|
@@ -36,6 +37,8 @@ export declare class HydraWebSocket extends EventEmitter {
|
|
|
36
37
|
getStatus(): HydraStatus;
|
|
37
38
|
/** Register a status-change listener. Returns current status. */
|
|
38
39
|
onStatusChange(callback: (status: HydraStatus) => void): HydraStatus;
|
|
40
|
+
/** The last Greetings message received, if any. */
|
|
41
|
+
get lastGreetings(): HydraWsMessage | null;
|
|
39
42
|
private handleMessage;
|
|
40
43
|
private updateStatus;
|
|
41
44
|
}
|
|
@@ -31,6 +31,7 @@ export class HydraWebSocket extends EventEmitter {
|
|
|
31
31
|
url;
|
|
32
32
|
_status = 'IDLE';
|
|
33
33
|
_connectionState = 'IDLE';
|
|
34
|
+
_lastGreetings = null;
|
|
34
35
|
constructor(wsUrl) {
|
|
35
36
|
super();
|
|
36
37
|
this.url = wsUrl;
|
|
@@ -80,7 +81,9 @@ export class HydraWebSocket extends EventEmitter {
|
|
|
80
81
|
async waitForGreetings(timeoutMs = 30_000) {
|
|
81
82
|
if (this._connectionState === 'CONNECTED')
|
|
82
83
|
return true;
|
|
83
|
-
|
|
84
|
+
// Register the listener BEFORE connecting so the Greetings message
|
|
85
|
+
// (which the Hydra node sends immediately after the socket opens)
|
|
86
|
+
// cannot arrive before we're listening for it.
|
|
84
87
|
return new Promise((resolve, reject) => {
|
|
85
88
|
const timer = setTimeout(() => {
|
|
86
89
|
this.removeListener('message', onMsg);
|
|
@@ -96,6 +99,11 @@ export class HydraWebSocket extends EventEmitter {
|
|
|
96
99
|
}
|
|
97
100
|
};
|
|
98
101
|
this.on('message', onMsg);
|
|
102
|
+
this.connect().catch((err) => {
|
|
103
|
+
clearTimeout(timer);
|
|
104
|
+
this.removeListener('message', onMsg);
|
|
105
|
+
reject(err);
|
|
106
|
+
});
|
|
99
107
|
});
|
|
100
108
|
}
|
|
101
109
|
/** Close the WebSocket. */
|
|
@@ -136,7 +144,14 @@ export class HydraWebSocket extends EventEmitter {
|
|
|
136
144
|
this.on('status', callback);
|
|
137
145
|
return this._status;
|
|
138
146
|
}
|
|
147
|
+
/** The last Greetings message received, if any. */
|
|
148
|
+
get lastGreetings() {
|
|
149
|
+
return this._lastGreetings;
|
|
150
|
+
}
|
|
139
151
|
handleMessage(msg) {
|
|
152
|
+
if (msg.tag === 'Greetings') {
|
|
153
|
+
this._lastGreetings = msg;
|
|
154
|
+
}
|
|
140
155
|
this.emit('message', msg);
|
|
141
156
|
// Update status from Greetings headStatus
|
|
142
157
|
if (msg.tag === 'Greetings' && 'headStatus' in msg) {
|
package/dist/hydra/utxo.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
1
|
import { requireEnv } from '../config.js';
|
|
3
2
|
/**
|
|
4
3
|
* Fetch the full UTxO set from the Hydra head snapshot.
|
|
@@ -11,8 +10,11 @@ export async function getUtxoSet() {
|
|
|
11
10
|
const baseUrl = requireEnv('HYDRA_API_URL');
|
|
12
11
|
const url = `${baseUrl}/snapshot/utxo`;
|
|
13
12
|
try {
|
|
14
|
-
const response = await
|
|
15
|
-
|
|
13
|
+
const response = await fetch(url);
|
|
14
|
+
if (!response.ok) {
|
|
15
|
+
throw new Error(`HTTP ${response.status} fetching ${url}`);
|
|
16
|
+
}
|
|
17
|
+
const data = await response.json();
|
|
16
18
|
const UtxoSet = [];
|
|
17
19
|
for (const [txKey, utxo] of Object.entries(data)) {
|
|
18
20
|
const [tx_hash, index_str] = txKey.split('#');
|
package/dist/wrangler.js
CHANGED
|
@@ -81,6 +81,12 @@ export class Wrangler {
|
|
|
81
81
|
this.connectWithRetry()
|
|
82
82
|
.then(() => {
|
|
83
83
|
this.ws.on('message', onMsg);
|
|
84
|
+
// Replay the Greetings message that was consumed during connection.
|
|
85
|
+
// The Hydra node only sends Greetings once, so the handler above
|
|
86
|
+
// would never see it without this replay.
|
|
87
|
+
if (this.ws.lastGreetings) {
|
|
88
|
+
onMsg(this.ws.lastGreetings);
|
|
89
|
+
}
|
|
84
90
|
})
|
|
85
91
|
.catch((err) => settle(reject, new Error(`Failed to connect: ${String(err)}`)));
|
|
86
92
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lerna-labs/hydra-sdk",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.20",
|
|
4
4
|
"description": "TypeScript SDK for managing Cardano Hydra Heads — lifecycle, UTxO queries, wallet management, transaction submission, and signature verification",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cardano",
|
|
@@ -43,7 +43,6 @@
|
|
|
43
43
|
"@emurgo/cardano-serialization-lib-nodejs": "^15.0.3",
|
|
44
44
|
"@meshsdk/core": "1.9.0-beta.99",
|
|
45
45
|
"@meshsdk/core-cst": "1.9.0-beta.99",
|
|
46
|
-
"axios": "^1.11.0",
|
|
47
46
|
"ws": "^8.18.3"
|
|
48
47
|
},
|
|
49
48
|
"devDependencies": {
|