gn-provider 1.1.3 → 1.1.5
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.
- package/dist/gn-provider.d.ts +2 -1
- package/dist/gn-provider.js +42 -10
- package/package.json +1 -1
package/dist/gn-provider.d.ts
CHANGED
|
@@ -10,8 +10,8 @@ export declare class GNProvider extends Provider {
|
|
|
10
10
|
private _network;
|
|
11
11
|
private _isConnected;
|
|
12
12
|
private _apiKey;
|
|
13
|
+
private apiPrefix;
|
|
13
14
|
private _getHeaders;
|
|
14
|
-
get apiPrefix(): string;
|
|
15
15
|
connect: () => Promise<this>;
|
|
16
16
|
getFeePerKb: () => Promise<number>;
|
|
17
17
|
constructor(network: scryptlib.bsv.Networks.Network, apiKey?: string);
|
|
@@ -20,6 +20,7 @@ export declare class GNProvider extends Provider {
|
|
|
20
20
|
getNetwork: () => scryptlib.bsv.Networks.Network;
|
|
21
21
|
protected _ready: () => Promise<void>;
|
|
22
22
|
sendRawTransaction: (rawTxHex: string) => Promise<TxHash>;
|
|
23
|
+
sendTransaction: (signedTx: scryptlib.bsv.Transaction) => Promise<string>;
|
|
23
24
|
listUnspent: (address: AddressOption, options?: UtxoQueryOptions) => Promise<UTXO[]>;
|
|
24
25
|
getBalance: (address: AddressOption) => Promise<{
|
|
25
26
|
confirmed: number;
|
package/dist/gn-provider.js
CHANGED
|
@@ -57,21 +57,25 @@ var ProviderEvent;
|
|
|
57
57
|
ProviderEvent["NetworkChange"] = "networkChange";
|
|
58
58
|
})(ProviderEvent || (ProviderEvent = {}));
|
|
59
59
|
class GNProvider extends abstract_provider_1.Provider {
|
|
60
|
-
get apiPrefix() {
|
|
61
|
-
const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
|
|
62
|
-
return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
|
|
63
|
-
}
|
|
64
60
|
constructor(network, apiKey = '') {
|
|
65
61
|
super();
|
|
66
62
|
this._isConnected = false;
|
|
67
63
|
this._getHeaders = () => {
|
|
68
64
|
return Object.assign({ 'Content-Type': 'application/json' }, (this._apiKey ? { 'woc-api-key': this._apiKey } : {}));
|
|
69
65
|
};
|
|
66
|
+
/*get apiPrefix(): string {
|
|
67
|
+
const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
|
|
68
|
+
return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
|
|
69
|
+
}*/
|
|
70
|
+
/*apiPrefix = (): string => {
|
|
71
|
+
const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
|
|
72
|
+
return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
|
|
73
|
+
}*/
|
|
70
74
|
this.connect = () => __awaiter(this, void 0, void 0, function* () {
|
|
71
75
|
var _a;
|
|
72
76
|
try {
|
|
73
77
|
const headers = this._getHeaders();
|
|
74
|
-
const res = yield superagent.get(`${this.apiPrefix}/woc`)
|
|
78
|
+
const res = yield superagent.get(`${this.apiPrefix()}/woc`) //`${this.apiPrefix}/woc`)
|
|
75
79
|
.timeout(3000)
|
|
76
80
|
.set(headers);
|
|
77
81
|
if (res.ok && res.text === "Whats On Chain") {
|
|
@@ -94,7 +98,7 @@ class GNProvider extends abstract_provider_1.Provider {
|
|
|
94
98
|
try {
|
|
95
99
|
const now = Math.floor(Date.now() / 1000);
|
|
96
100
|
const from = now - 1800; // 30 minutos atrás
|
|
97
|
-
const res = yield superagent.get(`${this.apiPrefix}/miner/fees?from=${from}&to=${now}`)
|
|
101
|
+
const res = yield superagent.get(`${this.apiPrefix()}/miner/fees?from=${from}&to=${now}`)
|
|
98
102
|
.set(headers);
|
|
99
103
|
if (res.body && Array.isArray(res.body) && res.body.length > 0) {
|
|
100
104
|
const totalFeeRate = res.body.reduce((sum, minerData) => {
|
|
@@ -159,7 +163,7 @@ class GNProvider extends abstract_provider_1.Provider {
|
|
|
159
163
|
const size = Math.max(1, rawTxHex.length / 2 / 1024); // Tamaño en KB
|
|
160
164
|
const timeout = Math.max(10000, 1000 * size); // Timeout dinámico
|
|
161
165
|
try {
|
|
162
|
-
const res = yield superagent.post(`${this.apiPrefix}/tx/raw`)
|
|
166
|
+
const res = yield superagent.post(`${this.apiPrefix()}/tx/raw`)
|
|
163
167
|
.timeout({
|
|
164
168
|
response: timeout,
|
|
165
169
|
deadline: 60000
|
|
@@ -178,11 +182,35 @@ class GNProvider extends abstract_provider_1.Provider {
|
|
|
178
182
|
throw new Error(`GNProvider ERROR: ${error.message}`);
|
|
179
183
|
}
|
|
180
184
|
});
|
|
185
|
+
// Implementación correcta de sendTransaction
|
|
186
|
+
this.sendTransaction = (signedTx) => __awaiter(this, void 0, void 0, function* () {
|
|
187
|
+
try {
|
|
188
|
+
const txHex = signedTx.serialize({ disableIsFullySigned: true });
|
|
189
|
+
return yield this.sendRawTransaction(txHex);
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
// Manejo seguro del error unknown
|
|
193
|
+
let errorMessage = "Unknown error occurred";
|
|
194
|
+
if (error instanceof Error) {
|
|
195
|
+
errorMessage = error.message;
|
|
196
|
+
}
|
|
197
|
+
else if (typeof error === "string") {
|
|
198
|
+
errorMessage = error;
|
|
199
|
+
}
|
|
200
|
+
else if (error && typeof error === "object" && "message" in error) {
|
|
201
|
+
errorMessage = String(error.message);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
errorMessage = JSON.stringify(error);
|
|
205
|
+
}
|
|
206
|
+
throw new Error(`GNProvider ERROR: failed to send transaction: ${errorMessage}`);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
181
209
|
//async listUnspent(address: AddressOption, options?: UtxoQueryOptions): Promise<UTXO[]> {
|
|
182
210
|
this.listUnspent = (address, options) => __awaiter(this, void 0, void 0, function* () {
|
|
183
211
|
yield this._ready();
|
|
184
212
|
const headers = this._getHeaders();
|
|
185
|
-
const res = yield superagent.get(`${this.apiPrefix}/address/${address}/unspent`)
|
|
213
|
+
const res = yield superagent.get(`${this.apiPrefix()}/address/${address}/unspent`)
|
|
186
214
|
.set(headers);
|
|
187
215
|
const utxos = res.body.map((item) => ({
|
|
188
216
|
txId: item.tx_hash,
|
|
@@ -196,7 +224,7 @@ class GNProvider extends abstract_provider_1.Provider {
|
|
|
196
224
|
this.getBalance = (address) => __awaiter(this, void 0, void 0, function* () {
|
|
197
225
|
try {
|
|
198
226
|
const headers = this._getHeaders();
|
|
199
|
-
const res = yield superagent.get(`${this.apiPrefix}/address/${address}/balance`)
|
|
227
|
+
const res = yield superagent.get(`${this.apiPrefix()}/address/${address}/balance`)
|
|
200
228
|
.set(headers);
|
|
201
229
|
return {
|
|
202
230
|
confirmed: res.body.confirmed || 0,
|
|
@@ -217,7 +245,7 @@ class GNProvider extends abstract_provider_1.Provider {
|
|
|
217
245
|
yield this._ready();
|
|
218
246
|
const headers = this._getHeaders();
|
|
219
247
|
try {
|
|
220
|
-
const res = yield superagent.get(`${this.apiPrefix}/tx/${txHash}/hex`)
|
|
248
|
+
const res = yield superagent.get(`${this.apiPrefix()}/tx/${txHash}/hex`)
|
|
221
249
|
.set(headers);
|
|
222
250
|
if (res.ok) {
|
|
223
251
|
return new scryptlib.bsv.Transaction(res.text);
|
|
@@ -232,6 +260,10 @@ class GNProvider extends abstract_provider_1.Provider {
|
|
|
232
260
|
this._network = network;
|
|
233
261
|
this._apiKey = apiKey;
|
|
234
262
|
this._isConnected = false;
|
|
263
|
+
this.apiPrefix = () => {
|
|
264
|
+
const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
|
|
265
|
+
return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
|
|
266
|
+
};
|
|
235
267
|
// Configura EventEmitter
|
|
236
268
|
Object.setPrototypeOf(this, events_1.EventEmitter.prototype);
|
|
237
269
|
// Conexión directa
|