@bsv/sdk 1.6.5 → 1.6.7
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/cjs/package.json +1 -1
- package/dist/cjs/src/kvstore/LocalKVStore.js +139 -84
- package/dist/cjs/src/kvstore/LocalKVStore.js.map +1 -1
- package/dist/cjs/src/storage/StorageDownloader.js +10 -11
- package/dist/cjs/src/storage/StorageDownloader.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/kvstore/LocalKVStore.js +139 -84
- package/dist/esm/src/kvstore/LocalKVStore.js.map +1 -1
- package/dist/esm/src/storage/StorageDownloader.js +10 -11
- package/dist/esm/src/storage/StorageDownloader.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/kvstore/LocalKVStore.d.ts +12 -2
- package/dist/types/src/kvstore/LocalKVStore.d.ts.map +1 -1
- package/dist/types/src/storage/StorageDownloader.d.ts +10 -11
- package/dist/types/src/storage/StorageDownloader.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +1 -1
- package/docs/kvstore.md +6 -3
- package/docs/storage.md +35 -2
- package/package.json +1 -1
- package/src/kvstore/LocalKVStore.ts +154 -84
- package/src/storage/StorageDownloader.ts +10 -11
package/dist/cjs/package.json
CHANGED
|
@@ -46,7 +46,13 @@ class LocalKVStore {
|
|
|
46
46
|
* @param {string} [originator] — An originator to use with PushDrop and the wallet, if provided.
|
|
47
47
|
* @throws {Error} If the context is missing or empty.
|
|
48
48
|
*/
|
|
49
|
-
constructor(wallet = new WalletClient_js_1.default(), context = 'kvstore default', encrypt = true, originator) {
|
|
49
|
+
constructor(wallet = new WalletClient_js_1.default(), context = 'kvstore default', encrypt = true, originator, acceptDelayedBroadcast = false) {
|
|
50
|
+
this.acceptDelayedBroadcast = false;
|
|
51
|
+
/**
|
|
52
|
+
* A map to store locks for each key to ensure atomic updates.
|
|
53
|
+
* @private
|
|
54
|
+
*/
|
|
55
|
+
this.keyLocks = new Map();
|
|
50
56
|
if (typeof context !== 'string' || context.length < 1) {
|
|
51
57
|
throw new Error('A context in which to operate is required.');
|
|
52
58
|
}
|
|
@@ -54,6 +60,35 @@ class LocalKVStore {
|
|
|
54
60
|
this.context = context;
|
|
55
61
|
this.encrypt = encrypt;
|
|
56
62
|
this.originator = originator;
|
|
63
|
+
this.acceptDelayedBroadcast = acceptDelayedBroadcast;
|
|
64
|
+
}
|
|
65
|
+
async queueOperationOnKey(key) {
|
|
66
|
+
// Check if a lock exists for this key and wait for it to resolve
|
|
67
|
+
let lockQueue = this.keyLocks.get(key);
|
|
68
|
+
if (lockQueue == null) {
|
|
69
|
+
lockQueue = [];
|
|
70
|
+
this.keyLocks.set(key, lockQueue);
|
|
71
|
+
}
|
|
72
|
+
let resolveNewLock = () => { };
|
|
73
|
+
const newLock = new Promise((resolve) => {
|
|
74
|
+
resolveNewLock = resolve;
|
|
75
|
+
if (lockQueue != null) {
|
|
76
|
+
lockQueue.push(resolve);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
// If we are the only request, resolve the lock immediately, queue remains at 1 item until request ends.
|
|
80
|
+
if (lockQueue.length === 1) {
|
|
81
|
+
resolveNewLock();
|
|
82
|
+
}
|
|
83
|
+
await newLock;
|
|
84
|
+
return lockQueue;
|
|
85
|
+
}
|
|
86
|
+
finishOperationOnKey(key, lockQueue) {
|
|
87
|
+
lockQueue.shift(); // Remove the current lock from the queue
|
|
88
|
+
if (lockQueue.length > 0) {
|
|
89
|
+
// If there are more locks waiting, resolve the next one
|
|
90
|
+
lockQueue[0]();
|
|
91
|
+
}
|
|
57
92
|
}
|
|
58
93
|
getProtocol(key) {
|
|
59
94
|
return { protocolID: [2, this.context], keyID: key };
|
|
@@ -79,8 +114,14 @@ class LocalKVStore {
|
|
|
79
114
|
* @throws {Error} If the found output's locking script cannot be decoded or represents an invalid token format.
|
|
80
115
|
*/
|
|
81
116
|
async get(key, defaultValue = undefined) {
|
|
82
|
-
const
|
|
83
|
-
|
|
117
|
+
const lockQueue = await this.queueOperationOnKey(key);
|
|
118
|
+
try {
|
|
119
|
+
const r = await this.lookupValue(key, defaultValue, 5);
|
|
120
|
+
return r.value;
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
this.finishOperationOnKey(key, lockQueue);
|
|
124
|
+
}
|
|
84
125
|
}
|
|
85
126
|
getLockingScript(output, beef) {
|
|
86
127
|
var _a;
|
|
@@ -153,77 +194,85 @@ class LocalKVStore {
|
|
|
153
194
|
return spends;
|
|
154
195
|
}
|
|
155
196
|
/**
|
|
156
|
-
* Sets or updates the value associated with a given key.
|
|
197
|
+
* Sets or updates the value associated with a given key atomically.
|
|
157
198
|
* If the key already exists (one or more outputs found), it spends the existing output(s)
|
|
158
199
|
* and creates a new one with the updated value. If multiple outputs exist for the key,
|
|
159
200
|
* they are collapsed into a single new output.
|
|
160
201
|
* If the key does not exist, it creates a new output.
|
|
161
202
|
* Handles encryption if enabled.
|
|
162
203
|
* If signing the update/collapse transaction fails, it relinquishes the original outputs and starts over with a new chain.
|
|
204
|
+
* Ensures atomicity by locking the key during the operation, preventing concurrent updates
|
|
205
|
+
* to the same key from missing earlier changes.
|
|
163
206
|
*
|
|
164
207
|
* @param {string} key - The key to set or update.
|
|
165
208
|
* @param {string} value - The value to associate with the key.
|
|
166
209
|
* @returns {Promise<OutpointString>} A promise that resolves to the outpoint string (txid.vout) of the new or updated token output.
|
|
167
210
|
*/
|
|
168
211
|
async set(key, value) {
|
|
169
|
-
const
|
|
170
|
-
if (current.value === value) {
|
|
171
|
-
if (current.outpoint === undefined) {
|
|
172
|
-
throw new Error('outpoint must be valid when value is valid and unchanged');
|
|
173
|
-
}
|
|
174
|
-
// Don't create a new transaction if the value doesn't need to change...
|
|
175
|
-
return current.outpoint;
|
|
176
|
-
}
|
|
177
|
-
const protocol = this.getProtocol(key);
|
|
178
|
-
let valueAsArray = Utils.toArray(value, 'utf8');
|
|
179
|
-
if (this.encrypt) {
|
|
180
|
-
const { ciphertext } = await this.wallet.encrypt({
|
|
181
|
-
...protocol,
|
|
182
|
-
plaintext: valueAsArray
|
|
183
|
-
});
|
|
184
|
-
valueAsArray = ciphertext;
|
|
185
|
-
}
|
|
186
|
-
const pushdrop = new PushDrop_js_1.default(this.wallet, this.originator);
|
|
187
|
-
const lockingScript = await pushdrop.lock([valueAsArray], protocol.protocolID, protocol.keyID, 'self');
|
|
188
|
-
const { outputs, BEEF: inputBEEF } = current.lor;
|
|
189
|
-
let outpoint;
|
|
212
|
+
const lockQueue = await this.queueOperationOnKey(key);
|
|
190
213
|
try {
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
inputs,
|
|
196
|
-
outputs: [{
|
|
197
|
-
basket: this.context,
|
|
198
|
-
tags: [key],
|
|
199
|
-
lockingScript: lockingScript.toHex(),
|
|
200
|
-
satoshis: 1,
|
|
201
|
-
outputDescription: 'Key-value token'
|
|
202
|
-
}],
|
|
203
|
-
options: {
|
|
204
|
-
acceptDelayedBroadcast: false,
|
|
205
|
-
randomizeOutputs: false
|
|
214
|
+
const current = await this.lookupValue(key, undefined, 10);
|
|
215
|
+
if (current.value === value) {
|
|
216
|
+
if (current.outpoint === undefined) {
|
|
217
|
+
throw new Error('outpoint must be valid when value is valid and unchanged');
|
|
206
218
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
throw new Error('Wallet did not return a signable transaction when expected.');
|
|
219
|
+
// Don't create a new transaction if the value doesn't need to change
|
|
220
|
+
return current.outpoint;
|
|
210
221
|
}
|
|
211
|
-
|
|
212
|
-
|
|
222
|
+
const protocol = this.getProtocol(key);
|
|
223
|
+
let valueAsArray = Utils.toArray(value, 'utf8');
|
|
224
|
+
if (this.encrypt) {
|
|
225
|
+
const { ciphertext } = await this.wallet.encrypt({
|
|
226
|
+
...protocol,
|
|
227
|
+
plaintext: valueAsArray
|
|
228
|
+
});
|
|
229
|
+
valueAsArray = ciphertext;
|
|
213
230
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
231
|
+
const pushdrop = new PushDrop_js_1.default(this.wallet, this.originator);
|
|
232
|
+
const lockingScript = await pushdrop.lock([valueAsArray], protocol.protocolID, protocol.keyID, 'self');
|
|
233
|
+
const { outputs, BEEF: inputBEEF } = current.lor;
|
|
234
|
+
let outpoint;
|
|
235
|
+
try {
|
|
236
|
+
const inputs = this.getInputs(outputs);
|
|
237
|
+
const { txid, signableTransaction } = await this.wallet.createAction({
|
|
238
|
+
description: `Update ${key} in ${this.context}`,
|
|
239
|
+
inputBEEF,
|
|
240
|
+
inputs,
|
|
241
|
+
outputs: [{
|
|
242
|
+
basket: this.context,
|
|
243
|
+
tags: [key],
|
|
244
|
+
lockingScript: lockingScript.toHex(),
|
|
245
|
+
satoshis: 1,
|
|
246
|
+
outputDescription: 'Key-value token'
|
|
247
|
+
}],
|
|
248
|
+
options: {
|
|
249
|
+
acceptDelayedBroadcast: this.acceptDelayedBroadcast,
|
|
250
|
+
randomizeOutputs: false
|
|
251
|
+
}
|
|
219
252
|
});
|
|
220
|
-
|
|
253
|
+
if (outputs.length > 0 && typeof signableTransaction !== 'object') {
|
|
254
|
+
throw new Error('Wallet did not return a signable transaction when expected.');
|
|
255
|
+
}
|
|
256
|
+
if (signableTransaction == null) {
|
|
257
|
+
outpoint = `${txid}.0`;
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
const spends = await this.getSpends(key, outputs, pushdrop, signableTransaction.tx);
|
|
261
|
+
const { txid } = await this.wallet.signAction({
|
|
262
|
+
reference: signableTransaction.reference,
|
|
263
|
+
spends
|
|
264
|
+
});
|
|
265
|
+
outpoint = `${txid}.0`;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
catch (_) {
|
|
269
|
+
throw new Error(`There are ${outputs.length} outputs with tag ${key} that cannot be unlocked.`);
|
|
221
270
|
}
|
|
271
|
+
return outpoint;
|
|
222
272
|
}
|
|
223
|
-
|
|
224
|
-
|
|
273
|
+
finally {
|
|
274
|
+
this.finishOperationOnKey(key, lockQueue);
|
|
225
275
|
}
|
|
226
|
-
return outpoint;
|
|
227
276
|
}
|
|
228
277
|
/**
|
|
229
278
|
* Removes the key-value pair associated with the given key.
|
|
@@ -236,43 +285,49 @@ class LocalKVStore {
|
|
|
236
285
|
* @returns {Promise<string[]>} A promise that resolves to the txids of the removal transactions if successful.
|
|
237
286
|
*/
|
|
238
287
|
async remove(key) {
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
288
|
+
const lockQueue = await this.queueOperationOnKey(key);
|
|
289
|
+
try {
|
|
290
|
+
const txids = [];
|
|
291
|
+
for (;;) {
|
|
292
|
+
const { outputs, BEEF: inputBEEF, totalOutputs } = await this.getOutputs(key);
|
|
293
|
+
if (outputs.length > 0) {
|
|
294
|
+
const pushdrop = new PushDrop_js_1.default(this.wallet, this.originator);
|
|
295
|
+
try {
|
|
296
|
+
const inputs = this.getInputs(outputs);
|
|
297
|
+
const { signableTransaction } = await this.wallet.createAction({
|
|
298
|
+
description: `Remove ${key} in ${this.context}`,
|
|
299
|
+
inputBEEF,
|
|
300
|
+
inputs,
|
|
301
|
+
options: {
|
|
302
|
+
acceptDelayedBroadcast: this.acceptDelayedBroadcast
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
if (typeof signableTransaction !== 'object') {
|
|
306
|
+
throw new Error('Wallet did not return a signable transaction when expected.');
|
|
252
307
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
308
|
+
const spends = await this.getSpends(key, outputs, pushdrop, signableTransaction.tx);
|
|
309
|
+
const { txid } = await this.wallet.signAction({
|
|
310
|
+
reference: signableTransaction.reference,
|
|
311
|
+
spends
|
|
312
|
+
});
|
|
313
|
+
if (txid === undefined) {
|
|
314
|
+
throw new Error('signAction must return a valid txid');
|
|
315
|
+
}
|
|
316
|
+
txids.push(txid);
|
|
256
317
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
reference: signableTransaction.reference,
|
|
260
|
-
spends
|
|
261
|
-
});
|
|
262
|
-
if (txid === undefined) {
|
|
263
|
-
throw new Error('signAction must return a valid txid');
|
|
318
|
+
catch (_) {
|
|
319
|
+
throw new Error(`There are ${totalOutputs} outputs with tag ${key} that cannot be unlocked.`);
|
|
264
320
|
}
|
|
265
|
-
txids.push(txid);
|
|
266
321
|
}
|
|
267
|
-
|
|
268
|
-
|
|
322
|
+
if (outputs.length === totalOutputs) {
|
|
323
|
+
break;
|
|
269
324
|
}
|
|
270
325
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
326
|
+
return txids;
|
|
327
|
+
}
|
|
328
|
+
finally {
|
|
329
|
+
this.finishOperationOnKey(key, lockQueue);
|
|
274
330
|
}
|
|
275
|
-
return txids;
|
|
276
331
|
}
|
|
277
332
|
}
|
|
278
333
|
exports.default = LocalKVStore;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocalKVStore.js","sourceRoot":"","sources":["../../../../src/kvstore/LocalKVStore.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,kFAAsD;AACtD,8DAA+C;AAE/C,gFAAoD;AACpD,mFAAuD;AACvD,oDAA6C;AAE7C;;;;GAIG;AACH,MAAqB,YAAY;
|
|
1
|
+
{"version":3,"file":"LocalKVStore.js","sourceRoot":"","sources":["../../../../src/kvstore/LocalKVStore.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,kFAAsD;AACtD,8DAA+C;AAE/C,gFAAoD;AACpD,mFAAuD;AACvD,oDAA6C;AAE7C;;;;GAIG;AAC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op.js"));
|
|
10
10
|
const Transaction_js_1 = __importDefault(require("../transaction/Transaction.js"));
|
|
11
11
|
const index_js_3 = require("../primitives/index.js");
|
|
12
|
-
/**
|
|
13
|
-
* Locates HTTP URLs where content can be downloaded. It uses the passed or the default one.
|
|
14
|
-
*
|
|
15
|
-
* @param {Object} obj All parameters are passed in an object.
|
|
16
|
-
* @param {String} obj.uhrpUrl The UHRP url to resolve.
|
|
17
|
-
* @param {string} obj.confederacyHost HTTPS URL for for the with default setting.
|
|
18
|
-
*
|
|
19
|
-
* @return {Array<String>} An array of HTTP URLs where content can be downloaded.
|
|
20
|
-
* @throws {Error} If UHRP url parameter invalid or is not an array
|
|
21
|
-
* or there is an error retrieving url(s) stored in the UHRP token.
|
|
22
|
-
*/
|
|
23
12
|
class StorageDownloader {
|
|
24
13
|
constructor(config) {
|
|
25
14
|
this.networkPreset = 'mainnet';
|
|
26
15
|
this.networkPreset = config === null || config === void 0 ? void 0 : config.networkPreset;
|
|
27
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Resolves the UHRP URL to a list of HTTP URLs where content can be downloaded.
|
|
19
|
+
* @param uhrpUrl The UHRP URL to resolve.
|
|
20
|
+
* @returns A promise that resolves to an array of HTTP URLs.
|
|
21
|
+
*/
|
|
28
22
|
async resolve(uhrpUrl) {
|
|
29
23
|
// Use UHRP lookup service
|
|
30
24
|
const lookupResolver = new index_js_1.LookupResolver({ networkPreset: this.networkPreset });
|
|
@@ -45,6 +39,11 @@ class StorageDownloader {
|
|
|
45
39
|
}
|
|
46
40
|
return decodedResults;
|
|
47
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Downloads the content from the UHRP URL after validating the hash for integrity.
|
|
44
|
+
* @param uhrpUrl The UHRP URL to download.
|
|
45
|
+
* @returns A promise that resolves to the downloaded content.
|
|
46
|
+
*/
|
|
48
47
|
async download(uhrpUrl) {
|
|
49
48
|
if (!index_js_2.StorageUtils.isValidURL(uhrpUrl)) {
|
|
50
49
|
throw new Error('Invalid parameter UHRP url');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StorageDownloader.js","sourceRoot":"","sources":["../../../../src/storage/StorageDownloader.ts"],"names":[],"mappings":";;;;;;AAAA,wDAA0D;AAC1D,yCAAyC;AACzC,kFAAsD;AACtD,mFAAuD;AACvD,qDAAoD;AAWpD
|
|
1
|
+
{"version":3,"file":"StorageDownloader.js","sourceRoot":"","sources":["../../../../src/storage/StorageDownloader.ts"],"names":[],"mappings":";;;;;;AAAA,wDAA0D;AAC1D,yCAAyC;AACzC,kFAAsD;AACtD,mFAAuD;AACvD,qDAAoD;AAWpD,MAAa,iBAAiB;IAG5B,YAAa,MAAyB;QAFrB,kBAAa,GAAqC,SAAS,CAAA;QAG1E,IAAI,CAAC,aAAa,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,aAAa,CAAA;IAC5C,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,OAAO,CAAE,OAAe;QACnC,0BAA0B;QAC1B,MAAM,cAAc,GAAG,IAAI,yBAAc,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAA;QAChF,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,CAAC,CAAA;QACvF,IAAI,QAAQ,CAAC,IAAI,KAAK,aAAa,EAAE;YACnC,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAA;SACxD;QACD,MAAM,cAAc,GAAa,EAAE,CAAA;QACnC,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAA;QACjD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YAChD,MAAM,EAAE,GAAG,wBAAW,CAAC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