gn-provider 1.1.7 → 1.1.9

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.
@@ -5,6 +5,9 @@ declare enum ProviderEvent {
5
5
  Connected = "connected",
6
6
  NetworkChange = "networkChange"
7
7
  }
8
+ export type UTXOWithHeight = UTXO & {
9
+ height: number;
10
+ };
8
11
  export declare class GNProvider extends Provider {
9
12
  emit: (event: ProviderEvent, ...args: any[]) => boolean;
10
13
  private _network;
@@ -21,7 +24,7 @@ export declare class GNProvider extends Provider {
21
24
  protected _ready: () => Promise<void>;
22
25
  sendRawTransaction: (rawTxHex: string) => Promise<TxHash>;
23
26
  sendTransaction: (signedTx: scryptlib.bsv.Transaction) => Promise<string>;
24
- listUnspent: (address: AddressOption, options?: UtxoQueryOptions) => Promise<UTXO[]>;
27
+ listUnspent: (address: AddressOption, options?: UtxoQueryOptions) => Promise<UTXOWithHeight[]>;
25
28
  getBalance: (address: AddressOption) => Promise<{
26
29
  confirmed: number;
27
30
  unconfirmed: number;
@@ -43,14 +43,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
43
43
  };
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
45
  exports.GNProvider = void 0;
46
- // Agrega esto al inicio del archivo
47
- // @ts-ignore
48
46
  const events_1 = require("events");
49
47
  const scryptlib = __importStar(require("scryptlib"));
50
48
  const abstract_provider_1 = require("scrypt-ts/dist/bsv/abstract-provider");
51
49
  const superagent = __importStar(require("superagent"));
52
- const utils_1 = require("scrypt-ts/dist/bsv/utils");
53
- //import { EventEmitter } from 'events';
54
50
  var ProviderEvent;
55
51
  (function (ProviderEvent) {
56
52
  ProviderEvent["Connected"] = "connected";
@@ -63,19 +59,11 @@ class GNProvider extends abstract_provider_1.Provider {
63
59
  this._getHeaders = () => {
64
60
  return Object.assign({ 'Content-Type': 'application/json' }, (this._apiKey ? { 'woc-api-key': this._apiKey } : {}));
65
61
  };
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
- }*/
74
62
  this.connect = () => __awaiter(this, void 0, void 0, function* () {
75
63
  var _a;
76
64
  try {
77
65
  const headers = this._getHeaders();
78
- const res = yield superagent.get(`${this.apiPrefix()}/woc`) //`${this.apiPrefix}/woc`)
66
+ const res = yield superagent.get(`${this.apiPrefix()}/woc`)
79
67
  .timeout(3000)
80
68
  .set(headers);
81
69
  if (res.ok && res.text === "Whats On Chain") {
@@ -91,7 +79,6 @@ class GNProvider extends abstract_provider_1.Provider {
91
79
  throw new Error(`connect failed: ${error.message || "unknown error"}`);
92
80
  }
93
81
  });
94
- //async getFeePerKb(): Promise<number> {
95
82
  this.getFeePerKb = () => __awaiter(this, void 0, void 0, function* () {
96
83
  yield this._ready();
97
84
  const headers = this._getHeaders();
@@ -111,40 +98,15 @@ class GNProvider extends abstract_provider_1.Provider {
111
98
  throw new Error("No fee data available");
112
99
  }
113
100
  catch (error) {
114
- return 1.05; // Valor de fallback
101
+ return 1.05;
115
102
  }
116
103
  });
117
- /*isConnected(): boolean {
118
- return this._isConnected;
119
- }*/
120
104
  this.isConnected = () => this._isConnected;
121
- /*private _getHeaders() {
122
- return {
123
- 'Content-Type': 'application/json',
124
- ...(this._apiKey ? { 'woc-api-key': this._apiKey } : {})
125
- };
126
- }*/
127
- /*updateNetwork(network: scryptlib.bsv.Networks.Network): void {
128
- this._network = network;
129
- this.emit(ProviderEvent.NetworkChange, network);
130
- }*/
131
105
  this.updateNetwork = (network) => {
132
106
  this._network = network;
133
107
  this.emit(ProviderEvent.NetworkChange, network);
134
108
  };
135
- /*getNetwork(): scryptlib.bsv.Networks.Network {
136
- return this._network;
137
- }*/
138
109
  this.getNetwork = () => this._network;
139
- /*protected async _ready(): Promise<void> {
140
- if (!this.isConnected()) {
141
- try {
142
- await this.connect();
143
- } catch (error) {
144
- throw error;
145
- }
146
- }
147
- }*/
148
110
  this._ready = () => __awaiter(this, void 0, void 0, function* () {
149
111
  if (!this.isConnected()) {
150
112
  try {
@@ -155,13 +117,12 @@ class GNProvider extends abstract_provider_1.Provider {
155
117
  }
156
118
  }
157
119
  });
158
- //async sendRawTransaction(rawTxHex: string): Promise<TxHash> {
159
120
  this.sendRawTransaction = (rawTxHex) => __awaiter(this, void 0, void 0, function* () {
160
121
  var _a;
161
122
  yield this._ready();
162
123
  const headers = this._getHeaders();
163
- const size = Math.max(1, rawTxHex.length / 2 / 1024); // Tamaño en KB
164
- const timeout = Math.max(10000, 1000 * size); // Timeout dinámico
124
+ const size = Math.max(1, rawTxHex.length / 2 / 1024);
125
+ const timeout = Math.max(10000, 1000 * size);
165
126
  try {
166
127
  const res = yield superagent.post(`${this.apiPrefix()}/tx/raw`)
167
128
  .timeout({
@@ -182,14 +143,12 @@ class GNProvider extends abstract_provider_1.Provider {
182
143
  throw new Error(`GNProvider ERROR: ${error.message}`);
183
144
  }
184
145
  });
185
- // Implementación correcta de sendTransaction
186
146
  this.sendTransaction = (signedTx) => __awaiter(this, void 0, void 0, function* () {
187
147
  try {
188
148
  const txHex = signedTx.serialize({ disableIsFullySigned: true });
189
149
  return yield this.sendRawTransaction(txHex);
190
150
  }
191
151
  catch (error) {
192
- // Manejo seguro del error unknown
193
152
  let errorMessage = "Unknown error occurred";
194
153
  if (error instanceof Error) {
195
154
  errorMessage = error.message;
@@ -206,21 +165,20 @@ class GNProvider extends abstract_provider_1.Provider {
206
165
  throw new Error(`GNProvider ERROR: failed to send transaction: ${errorMessage}`);
207
166
  }
208
167
  });
209
- //async listUnspent(address: AddressOption, options?: UtxoQueryOptions): Promise<UTXO[]> {
210
168
  this.listUnspent = (address, options) => __awaiter(this, void 0, void 0, function* () {
211
169
  yield this._ready();
212
170
  const headers = this._getHeaders();
213
171
  const res = yield superagent.get(`${this.apiPrefix()}/address/${address}/unspent`)
214
172
  .set(headers);
215
- const utxos = res.body.map((item) => ({
173
+ return res.body.map((item) => ({
216
174
  txId: item.tx_hash,
217
175
  outputIndex: item.tx_pos,
218
176
  satoshis: item.value,
219
177
  script: scryptlib.bsv.Script.buildPublicKeyHashOut(address).toHex(),
178
+ height: item.height
220
179
  }));
221
- return options ? (0, utils_1.filterUTXO)(utxos, options) : utxos;
180
+ //return options ? filterUTXO(utxos, options) : utxos;
222
181
  });
223
- //async getBalance(address: AddressOption): Promise<{ confirmed: number; unconfirmed: number }> {
224
182
  this.getBalance = (address) => __awaiter(this, void 0, void 0, function* () {
225
183
  try {
226
184
  const headers = this._getHeaders();
@@ -240,7 +198,6 @@ class GNProvider extends abstract_provider_1.Provider {
240
198
  };
241
199
  }
242
200
  });
243
- //async getTransaction(txHash: string): Promise<TransactionResponse> {
244
201
  this.getTransaction = (txHash) => __awaiter(this, void 0, void 0, function* () {
245
202
  yield this._ready();
246
203
  const headers = this._getHeaders();
@@ -256,7 +213,34 @@ class GNProvider extends abstract_provider_1.Provider {
256
213
  throw new Error(`Error fetching transaction: ${error.message}`);
257
214
  }
258
215
  });
259
- // Inicializa propiedades primero
216
+ this.needIgnoreError = (inMsg) => {
217
+ if (inMsg.includes('Transaction already in the mempool'))
218
+ return true;
219
+ if (inMsg.includes('txn-already-known'))
220
+ return true;
221
+ return false;
222
+ };
223
+ this.friendlyBIP22RejectionMsg = (inMsg) => {
224
+ const messages = {
225
+ 'bad-txns-vin-empty': 'Transaction is missing inputs.',
226
+ 'bad-txns-vout-empty': 'Transaction is missing outputs.',
227
+ 'bad-txns-oversize': 'Transaction is too large.',
228
+ 'bad-txns-vout-negative': 'Transaction output value is negative.',
229
+ 'bad-txns-vout-toolarge': 'Transaction output value is too large.',
230
+ 'bad-txns-txouttotal-toolarge': 'Transaction total output value is too large.',
231
+ 'bad-txns-prevout-null': 'Transaction inputs previous TX reference is null.',
232
+ 'bad-txns-inputs-duplicate': 'Transaction contains duplicate inputs.',
233
+ 'bad-txns-inputs-too-large': 'Transaction inputs too large.',
234
+ 'bad-txns-fee-negative': 'Transaction network fee is negative.',
235
+ 'bad-txns-fee-outofrange': 'Transaction network fee is out of range.',
236
+ 'mandatory-script-verify-flag-failed': 'Script evaluation failed.'
237
+ };
238
+ for (const [key, msg] of Object.entries(messages)) {
239
+ if (inMsg.includes(key))
240
+ return msg;
241
+ }
242
+ return inMsg;
243
+ };
260
244
  this._network = network;
261
245
  this._apiKey = apiKey;
262
246
  this._isConnected = false;
@@ -264,38 +248,8 @@ class GNProvider extends abstract_provider_1.Provider {
264
248
  const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
265
249
  return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
266
250
  };
267
- // Configura EventEmitter
268
251
  Object.setPrototypeOf(this, events_1.EventEmitter.prototype);
269
- // Conexión directa
270
252
  this.connect().catch(console.error);
271
253
  }
272
- needIgnoreError(inMsg) {
273
- if (inMsg.includes('Transaction already in the mempool'))
274
- return true;
275
- if (inMsg.includes('txn-already-known'))
276
- return true;
277
- return false;
278
- }
279
- friendlyBIP22RejectionMsg(inMsg) {
280
- const messages = {
281
- 'bad-txns-vin-empty': 'Transaction is missing inputs.',
282
- 'bad-txns-vout-empty': 'Transaction is missing outputs.',
283
- 'bad-txns-oversize': 'Transaction is too large.',
284
- 'bad-txns-vout-negative': 'Transaction output value is negative.',
285
- 'bad-txns-vout-toolarge': 'Transaction output value is too large.',
286
- 'bad-txns-txouttotal-toolarge': 'Transaction total output value is too large.',
287
- 'bad-txns-prevout-null': 'Transaction inputs previous TX reference is null.',
288
- 'bad-txns-inputs-duplicate': 'Transaction contains duplicate inputs.',
289
- 'bad-txns-inputs-too-large': 'Transaction inputs too large.',
290
- 'bad-txns-fee-negative': 'Transaction network fee is negative.',
291
- 'bad-txns-fee-outofrange': 'Transaction network fee is out of range.',
292
- 'mandatory-script-verify-flag-failed': 'Script evaluation failed.'
293
- };
294
- for (const [key, msg] of Object.entries(messages)) {
295
- if (inMsg.includes(key))
296
- return msg;
297
- }
298
- return inMsg;
299
- }
300
254
  }
301
255
  exports.GNProvider = GNProvider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gn-provider",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "files": [
5
5
  "dist",
6
6
  "scripts",