@dignetwork/dig-sdk 0.0.1-alpha.84 → 0.0.1-alpha.86
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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Wallet.d.ts","sourceRoot":"","sources":["../../src/blockchain/Wallet.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EASL,IAAI,EACJ,IAAI,EAEJ,SAAS,EAEV,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"Wallet.d.ts","sourceRoot":"","sources":["../../src/blockchain/Wallet.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EASL,IAAI,EACJ,IAAI,EAEJ,SAAS,EAEV,MAAM,8BAA8B,CAAC;AAQtC,eAAO,MAAM,qBAAqB,WAAa,CAAC;AAOhD,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAuB;IACvC,OAAO,CAAC,UAAU,CAAS;IAE3B,OAAO;WAKa,IAAI,CACtB,UAAU,GAAE,MAAkB,EAC9B,iBAAiB,GAAE,OAAc,GAChC,OAAO,CAAC,MAAM,CAAC;IAsBX,WAAW,IAAI,MAAM;WAOR,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;WAMpD,YAAY,CAC9B,UAAU,EAAE,MAAM,EAClB,IAAI,CAAC,EAAE,MAAM,GACZ,OAAO,CAAC,MAAM,CAAC;WASE,oBAAoB,CACtC,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,CAAC;IA6BL,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAKrC,qBAAqB,IAAI,OAAO,CAAC,MAAM,CAAC;IAMxC,sBAAsB,IAAI,OAAO,CAAC,MAAM,CAAC;IAKzC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAMrC,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC;WAK7B,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;WASlD,WAAW,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;mBAU/B,oBAAoB;mBAcpB,mBAAmB;IAS3B,2BAA2B,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;WAUpD,2BAA2B,CAC7C,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,OAAO,CAAC;IASN,kBAAkB,CAC7B,IAAI,EAAE,IAAI,EACV,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,SAAS,GAAE,IAAI,EAAO,GACrB,OAAO,CAAC,IAAI,EAAE,CAAC;WA+EE,yBAAyB,CAC3C,IAAI,EAAE,IAAI,EACV,UAAU,EAAE,SAAS,EAAE,GAAG,IAAI,GAC7B,OAAO,CAAC,MAAM,CAAC;WAIE,eAAe,CACjC,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,OAAO,CAAC;CAWpB"}
|
|
@@ -41,6 +41,8 @@ const buffer_1 = require("buffer");
|
|
|
41
41
|
const datalayer_driver_1 = require("@dignetwork/datalayer-driver");
|
|
42
42
|
const config_1 = require("../utils/config");
|
|
43
43
|
const FileCache_1 = require("../utils/FileCache");
|
|
44
|
+
const config_2 = require("../utils/config");
|
|
45
|
+
const path_1 = __importDefault(require("path"));
|
|
44
46
|
const KEYRING_FILE = "keyring.json";
|
|
45
47
|
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
|
|
46
48
|
exports.DEFAULT_FEE_COIN_COST = 64000000;
|
|
@@ -179,26 +181,57 @@ class Wallet {
|
|
|
179
181
|
return (0, datalayer_driver_1.verifySignedMessage)(buffer_1.Buffer.from(signature, "hex"), buffer_1.Buffer.from(publicKey, "hex"), buffer_1.Buffer.from(message, "utf-8"));
|
|
180
182
|
}
|
|
181
183
|
async selectUnspentCoins(peer, coinAmount, feeBigInt, omitCoins = []) {
|
|
182
|
-
const cache = new FileCache_1.FileCache("reserved_coins");
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
184
|
+
const cache = new FileCache_1.FileCache(path_1.default.join(config_2.USER_DIR_PATH, "reserved_coins"));
|
|
185
|
+
const ownerPuzzleHash = await this.getOwnerPuzzleHash();
|
|
186
|
+
// Define a function to attempt selecting unspent coins
|
|
187
|
+
const trySelectCoins = async () => {
|
|
188
|
+
const now = Date.now();
|
|
189
|
+
const omitCoinIds = omitCoins.map((coin) => (0, datalayer_driver_1.getCoinId)(coin).toString("hex"));
|
|
190
|
+
// Update omitCoinIds with currently valid reserved coins
|
|
191
|
+
const cachedReservedCoins = cache.getCachedKeys();
|
|
192
|
+
cachedReservedCoins.forEach((coinId) => {
|
|
193
|
+
const reservation = cache.get(coinId);
|
|
194
|
+
if (reservation && reservation.expiry > now) {
|
|
195
|
+
if (!omitCoinIds.includes(coinId)) {
|
|
196
|
+
omitCoinIds.push(coinId);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
cache.delete(coinId);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
const coinsResp = await peer.getAllUnspentCoins(ownerPuzzleHash, config_1.MIN_HEIGHT, buffer_1.Buffer.from(config_1.MIN_HEIGHT_HEADER_HASH, "hex"));
|
|
204
|
+
const unspentCoins = coinsResp.coins.filter((coin) => !omitCoinIds.includes((0, datalayer_driver_1.getCoinId)(coin).toString("hex")));
|
|
205
|
+
const selectedCoins = (0, datalayer_driver_1.selectCoins)(unspentCoins, feeBigInt + coinAmount);
|
|
206
|
+
return selectedCoins;
|
|
207
|
+
};
|
|
208
|
+
let selectedCoins = [];
|
|
209
|
+
let retry = true;
|
|
210
|
+
while (retry) {
|
|
211
|
+
selectedCoins = await trySelectCoins();
|
|
212
|
+
if (selectedCoins.length > 0) {
|
|
213
|
+
// Coins have been successfully selected
|
|
214
|
+
retry = false;
|
|
190
215
|
}
|
|
191
216
|
else {
|
|
192
|
-
|
|
217
|
+
const now = Date.now();
|
|
218
|
+
// Check if there are any valid cached reserved coins left
|
|
219
|
+
const cachedReservedCoins = cache.getCachedKeys().filter((coinId) => {
|
|
220
|
+
const reservation = cache.get(coinId);
|
|
221
|
+
return reservation && reservation.expiry > now;
|
|
222
|
+
});
|
|
223
|
+
if (cachedReservedCoins.length > 0) {
|
|
224
|
+
// Wait 10 seconds and try again
|
|
225
|
+
console.log("No unspent coins available. Waiting 10 seconds...");
|
|
226
|
+
await new Promise((resolve) => setTimeout(resolve, 10000));
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
// No unspent coins and no reserved coins
|
|
230
|
+
throw new Error("No unspent coins available.");
|
|
231
|
+
}
|
|
193
232
|
}
|
|
194
|
-
});
|
|
195
|
-
const ownerPuzzleHash = await this.getOwnerPuzzleHash();
|
|
196
|
-
const coinsResp = await peer.getAllUnspentCoins(ownerPuzzleHash, config_1.MIN_HEIGHT, buffer_1.Buffer.from(config_1.MIN_HEIGHT_HEADER_HASH, "hex"));
|
|
197
|
-
const unspentCoins = coinsResp.coins.filter((coin) => !omitCoinIds.includes((0, datalayer_driver_1.getCoinId)(coin).toString("hex")));
|
|
198
|
-
const selectedCoins = (0, datalayer_driver_1.selectCoins)(unspentCoins, feeBigInt + coinAmount);
|
|
199
|
-
if (selectedCoins.length === 0) {
|
|
200
|
-
throw new Error("No unspent coins available.");
|
|
201
233
|
}
|
|
234
|
+
// Reserve the selected coins
|
|
202
235
|
selectedCoins.forEach((coin) => {
|
|
203
236
|
const coinId = (0, datalayer_driver_1.getCoinId)(coin).toString("hex");
|
|
204
237
|
cache.set(coinId, { coinId, expiry: Date.now() + CACHE_DURATION });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/utils/network.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/utils/network.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,kBAAkB,QAAa,OAAO,CAAC,MAAM,GAAG,SAAS,CA6CrE,CAAC"}
|
package/dist/utils/network.js
CHANGED
|
@@ -4,31 +4,47 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.getPublicIpAddress = void 0;
|
|
7
|
+
/*
|
|
8
|
+
* Stopgap until better solution for finding public IPS found
|
|
9
|
+
*/
|
|
7
10
|
const superagent_1 = __importDefault(require("superagent"));
|
|
8
11
|
const MAX_RETRIES = 5;
|
|
9
12
|
const RETRY_DELAY = 2000; // in milliseconds
|
|
13
|
+
// Regular expression for validating both IPv4 and IPv6 addresses
|
|
14
|
+
const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
15
|
+
const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,7}|:):(([0-9a-fA-F]{1,4}:){1,6}|:):([0-9a-fA-F]{1,4}|:):([0-9a-fA-F]{1,4}|:)|::)$/;
|
|
16
|
+
// Helper function to validate the IP address
|
|
17
|
+
const isValidIp = (ip) => {
|
|
18
|
+
return ipv4Regex.test(ip) || ipv6Regex.test(ip);
|
|
19
|
+
};
|
|
10
20
|
const getPublicIpAddress = async () => {
|
|
11
21
|
const publicIp = process.env.PUBLIC_IP;
|
|
12
22
|
if (publicIp) {
|
|
13
|
-
console.log(
|
|
14
|
-
|
|
23
|
+
console.log("Public IP address from env:", publicIp);
|
|
24
|
+
if (isValidIp(publicIp)) {
|
|
25
|
+
return publicIp;
|
|
26
|
+
}
|
|
27
|
+
console.error("Invalid public IP address in environment variable");
|
|
28
|
+
return undefined;
|
|
15
29
|
}
|
|
16
30
|
let attempt = 0;
|
|
17
31
|
while (attempt < MAX_RETRIES) {
|
|
18
32
|
try {
|
|
19
|
-
const response = await superagent_1.default.get(
|
|
33
|
+
const response = await superagent_1.default.get("https://api.datalayer.storage/user/v1/get_user_ip");
|
|
20
34
|
if (response.body && response.body.success) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
const ipAddress = response.body.ip_address;
|
|
36
|
+
if (isValidIp(ipAddress)) {
|
|
37
|
+
return ipAddress;
|
|
38
|
+
}
|
|
39
|
+
throw new Error("Invalid IP address format received");
|
|
25
40
|
}
|
|
41
|
+
throw new Error("Failed to retrieve public IP address");
|
|
26
42
|
}
|
|
27
43
|
catch (error) {
|
|
28
44
|
attempt++;
|
|
29
45
|
console.error(`Error fetching public IP address (Attempt ${attempt}):`, error.message);
|
|
30
46
|
if (attempt >= MAX_RETRIES) {
|
|
31
|
-
throw new Error(
|
|
47
|
+
throw new Error("Could not retrieve public IP address after several attempts");
|
|
32
48
|
}
|
|
33
49
|
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
|
|
34
50
|
}
|