@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
|
@@ -33,6 +33,12 @@ export default class LocalKVStore {
|
|
|
33
33
|
* @readonly
|
|
34
34
|
*/
|
|
35
35
|
originator;
|
|
36
|
+
acceptDelayedBroadcast = false;
|
|
37
|
+
/**
|
|
38
|
+
* A map to store locks for each key to ensure atomic updates.
|
|
39
|
+
* @private
|
|
40
|
+
*/
|
|
41
|
+
keyLocks = new Map();
|
|
36
42
|
/**
|
|
37
43
|
* Creates an instance of the localKVStore.
|
|
38
44
|
*
|
|
@@ -42,7 +48,7 @@ export default class LocalKVStore {
|
|
|
42
48
|
* @param {string} [originator] — An originator to use with PushDrop and the wallet, if provided.
|
|
43
49
|
* @throws {Error} If the context is missing or empty.
|
|
44
50
|
*/
|
|
45
|
-
constructor(wallet = new WalletClient(), context = 'kvstore default', encrypt = true, originator) {
|
|
51
|
+
constructor(wallet = new WalletClient(), context = 'kvstore default', encrypt = true, originator, acceptDelayedBroadcast = false) {
|
|
46
52
|
if (typeof context !== 'string' || context.length < 1) {
|
|
47
53
|
throw new Error('A context in which to operate is required.');
|
|
48
54
|
}
|
|
@@ -50,6 +56,35 @@ export default class LocalKVStore {
|
|
|
50
56
|
this.context = context;
|
|
51
57
|
this.encrypt = encrypt;
|
|
52
58
|
this.originator = originator;
|
|
59
|
+
this.acceptDelayedBroadcast = acceptDelayedBroadcast;
|
|
60
|
+
}
|
|
61
|
+
async queueOperationOnKey(key) {
|
|
62
|
+
// Check if a lock exists for this key and wait for it to resolve
|
|
63
|
+
let lockQueue = this.keyLocks.get(key);
|
|
64
|
+
if (lockQueue == null) {
|
|
65
|
+
lockQueue = [];
|
|
66
|
+
this.keyLocks.set(key, lockQueue);
|
|
67
|
+
}
|
|
68
|
+
let resolveNewLock = () => { };
|
|
69
|
+
const newLock = new Promise((resolve) => {
|
|
70
|
+
resolveNewLock = resolve;
|
|
71
|
+
if (lockQueue != null) {
|
|
72
|
+
lockQueue.push(resolve);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
// If we are the only request, resolve the lock immediately, queue remains at 1 item until request ends.
|
|
76
|
+
if (lockQueue.length === 1) {
|
|
77
|
+
resolveNewLock();
|
|
78
|
+
}
|
|
79
|
+
await newLock;
|
|
80
|
+
return lockQueue;
|
|
81
|
+
}
|
|
82
|
+
finishOperationOnKey(key, lockQueue) {
|
|
83
|
+
lockQueue.shift(); // Remove the current lock from the queue
|
|
84
|
+
if (lockQueue.length > 0) {
|
|
85
|
+
// If there are more locks waiting, resolve the next one
|
|
86
|
+
lockQueue[0]();
|
|
87
|
+
}
|
|
53
88
|
}
|
|
54
89
|
getProtocol(key) {
|
|
55
90
|
return { protocolID: [2, this.context], keyID: key };
|
|
@@ -75,8 +110,14 @@ export default class LocalKVStore {
|
|
|
75
110
|
* @throws {Error} If the found output's locking script cannot be decoded or represents an invalid token format.
|
|
76
111
|
*/
|
|
77
112
|
async get(key, defaultValue = undefined) {
|
|
78
|
-
const
|
|
79
|
-
|
|
113
|
+
const lockQueue = await this.queueOperationOnKey(key);
|
|
114
|
+
try {
|
|
115
|
+
const r = await this.lookupValue(key, defaultValue, 5);
|
|
116
|
+
return r.value;
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
this.finishOperationOnKey(key, lockQueue);
|
|
120
|
+
}
|
|
80
121
|
}
|
|
81
122
|
getLockingScript(output, beef) {
|
|
82
123
|
const [txid, vout] = output.outpoint.split('.');
|
|
@@ -148,77 +189,85 @@ export default class LocalKVStore {
|
|
|
148
189
|
return spends;
|
|
149
190
|
}
|
|
150
191
|
/**
|
|
151
|
-
* Sets or updates the value associated with a given key.
|
|
192
|
+
* Sets or updates the value associated with a given key atomically.
|
|
152
193
|
* If the key already exists (one or more outputs found), it spends the existing output(s)
|
|
153
194
|
* and creates a new one with the updated value. If multiple outputs exist for the key,
|
|
154
195
|
* they are collapsed into a single new output.
|
|
155
196
|
* If the key does not exist, it creates a new output.
|
|
156
197
|
* Handles encryption if enabled.
|
|
157
198
|
* If signing the update/collapse transaction fails, it relinquishes the original outputs and starts over with a new chain.
|
|
199
|
+
* Ensures atomicity by locking the key during the operation, preventing concurrent updates
|
|
200
|
+
* to the same key from missing earlier changes.
|
|
158
201
|
*
|
|
159
202
|
* @param {string} key - The key to set or update.
|
|
160
203
|
* @param {string} value - The value to associate with the key.
|
|
161
204
|
* @returns {Promise<OutpointString>} A promise that resolves to the outpoint string (txid.vout) of the new or updated token output.
|
|
162
205
|
*/
|
|
163
206
|
async set(key, value) {
|
|
164
|
-
const
|
|
165
|
-
if (current.value === value) {
|
|
166
|
-
if (current.outpoint === undefined) {
|
|
167
|
-
throw new Error('outpoint must be valid when value is valid and unchanged');
|
|
168
|
-
}
|
|
169
|
-
// Don't create a new transaction if the value doesn't need to change...
|
|
170
|
-
return current.outpoint;
|
|
171
|
-
}
|
|
172
|
-
const protocol = this.getProtocol(key);
|
|
173
|
-
let valueAsArray = Utils.toArray(value, 'utf8');
|
|
174
|
-
if (this.encrypt) {
|
|
175
|
-
const { ciphertext } = await this.wallet.encrypt({
|
|
176
|
-
...protocol,
|
|
177
|
-
plaintext: valueAsArray
|
|
178
|
-
});
|
|
179
|
-
valueAsArray = ciphertext;
|
|
180
|
-
}
|
|
181
|
-
const pushdrop = new PushDrop(this.wallet, this.originator);
|
|
182
|
-
const lockingScript = await pushdrop.lock([valueAsArray], protocol.protocolID, protocol.keyID, 'self');
|
|
183
|
-
const { outputs, BEEF: inputBEEF } = current.lor;
|
|
184
|
-
let outpoint;
|
|
207
|
+
const lockQueue = await this.queueOperationOnKey(key);
|
|
185
208
|
try {
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
inputs,
|
|
191
|
-
outputs: [{
|
|
192
|
-
basket: this.context,
|
|
193
|
-
tags: [key],
|
|
194
|
-
lockingScript: lockingScript.toHex(),
|
|
195
|
-
satoshis: 1,
|
|
196
|
-
outputDescription: 'Key-value token'
|
|
197
|
-
}],
|
|
198
|
-
options: {
|
|
199
|
-
acceptDelayedBroadcast: false,
|
|
200
|
-
randomizeOutputs: false
|
|
209
|
+
const current = await this.lookupValue(key, undefined, 10);
|
|
210
|
+
if (current.value === value) {
|
|
211
|
+
if (current.outpoint === undefined) {
|
|
212
|
+
throw new Error('outpoint must be valid when value is valid and unchanged');
|
|
201
213
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
throw new Error('Wallet did not return a signable transaction when expected.');
|
|
214
|
+
// Don't create a new transaction if the value doesn't need to change
|
|
215
|
+
return current.outpoint;
|
|
205
216
|
}
|
|
206
|
-
|
|
207
|
-
|
|
217
|
+
const protocol = this.getProtocol(key);
|
|
218
|
+
let valueAsArray = Utils.toArray(value, 'utf8');
|
|
219
|
+
if (this.encrypt) {
|
|
220
|
+
const { ciphertext } = await this.wallet.encrypt({
|
|
221
|
+
...protocol,
|
|
222
|
+
plaintext: valueAsArray
|
|
223
|
+
});
|
|
224
|
+
valueAsArray = ciphertext;
|
|
208
225
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
226
|
+
const pushdrop = new PushDrop(this.wallet, this.originator);
|
|
227
|
+
const lockingScript = await pushdrop.lock([valueAsArray], protocol.protocolID, protocol.keyID, 'self');
|
|
228
|
+
const { outputs, BEEF: inputBEEF } = current.lor;
|
|
229
|
+
let outpoint;
|
|
230
|
+
try {
|
|
231
|
+
const inputs = this.getInputs(outputs);
|
|
232
|
+
const { txid, signableTransaction } = await this.wallet.createAction({
|
|
233
|
+
description: `Update ${key} in ${this.context}`,
|
|
234
|
+
inputBEEF,
|
|
235
|
+
inputs,
|
|
236
|
+
outputs: [{
|
|
237
|
+
basket: this.context,
|
|
238
|
+
tags: [key],
|
|
239
|
+
lockingScript: lockingScript.toHex(),
|
|
240
|
+
satoshis: 1,
|
|
241
|
+
outputDescription: 'Key-value token'
|
|
242
|
+
}],
|
|
243
|
+
options: {
|
|
244
|
+
acceptDelayedBroadcast: this.acceptDelayedBroadcast,
|
|
245
|
+
randomizeOutputs: false
|
|
246
|
+
}
|
|
214
247
|
});
|
|
215
|
-
|
|
248
|
+
if (outputs.length > 0 && typeof signableTransaction !== 'object') {
|
|
249
|
+
throw new Error('Wallet did not return a signable transaction when expected.');
|
|
250
|
+
}
|
|
251
|
+
if (signableTransaction == null) {
|
|
252
|
+
outpoint = `${txid}.0`;
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
const spends = await this.getSpends(key, outputs, pushdrop, signableTransaction.tx);
|
|
256
|
+
const { txid } = await this.wallet.signAction({
|
|
257
|
+
reference: signableTransaction.reference,
|
|
258
|
+
spends
|
|
259
|
+
});
|
|
260
|
+
outpoint = `${txid}.0`;
|
|
261
|
+
}
|
|
216
262
|
}
|
|
263
|
+
catch (_) {
|
|
264
|
+
throw new Error(`There are ${outputs.length} outputs with tag ${key} that cannot be unlocked.`);
|
|
265
|
+
}
|
|
266
|
+
return outpoint;
|
|
217
267
|
}
|
|
218
|
-
|
|
219
|
-
|
|
268
|
+
finally {
|
|
269
|
+
this.finishOperationOnKey(key, lockQueue);
|
|
220
270
|
}
|
|
221
|
-
return outpoint;
|
|
222
271
|
}
|
|
223
272
|
/**
|
|
224
273
|
* Removes the key-value pair associated with the given key.
|
|
@@ -231,43 +280,49 @@ export default class LocalKVStore {
|
|
|
231
280
|
* @returns {Promise<string[]>} A promise that resolves to the txids of the removal transactions if successful.
|
|
232
281
|
*/
|
|
233
282
|
async remove(key) {
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
283
|
+
const lockQueue = await this.queueOperationOnKey(key);
|
|
284
|
+
try {
|
|
285
|
+
const txids = [];
|
|
286
|
+
for (;;) {
|
|
287
|
+
const { outputs, BEEF: inputBEEF, totalOutputs } = await this.getOutputs(key);
|
|
288
|
+
if (outputs.length > 0) {
|
|
289
|
+
const pushdrop = new PushDrop(this.wallet, this.originator);
|
|
290
|
+
try {
|
|
291
|
+
const inputs = this.getInputs(outputs);
|
|
292
|
+
const { signableTransaction } = await this.wallet.createAction({
|
|
293
|
+
description: `Remove ${key} in ${this.context}`,
|
|
294
|
+
inputBEEF,
|
|
295
|
+
inputs,
|
|
296
|
+
options: {
|
|
297
|
+
acceptDelayedBroadcast: this.acceptDelayedBroadcast
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
if (typeof signableTransaction !== 'object') {
|
|
301
|
+
throw new Error('Wallet did not return a signable transaction when expected.');
|
|
247
302
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
303
|
+
const spends = await this.getSpends(key, outputs, pushdrop, signableTransaction.tx);
|
|
304
|
+
const { txid } = await this.wallet.signAction({
|
|
305
|
+
reference: signableTransaction.reference,
|
|
306
|
+
spends
|
|
307
|
+
});
|
|
308
|
+
if (txid === undefined) {
|
|
309
|
+
throw new Error('signAction must return a valid txid');
|
|
310
|
+
}
|
|
311
|
+
txids.push(txid);
|
|
251
312
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
reference: signableTransaction.reference,
|
|
255
|
-
spends
|
|
256
|
-
});
|
|
257
|
-
if (txid === undefined) {
|
|
258
|
-
throw new Error('signAction must return a valid txid');
|
|
313
|
+
catch (_) {
|
|
314
|
+
throw new Error(`There are ${totalOutputs} outputs with tag ${key} that cannot be unlocked.`);
|
|
259
315
|
}
|
|
260
|
-
txids.push(txid);
|
|
261
316
|
}
|
|
262
|
-
|
|
263
|
-
|
|
317
|
+
if (outputs.length === totalOutputs) {
|
|
318
|
+
break;
|
|
264
319
|
}
|
|
265
320
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
321
|
+
return txids;
|
|
322
|
+
}
|
|
323
|
+
finally {
|
|
324
|
+
this.finishOperationOnKey(key, lockQueue);
|
|
269
325
|
}
|
|
270
|
-
return txids;
|
|
271
326
|
}
|
|
272
327
|
}
|
|
273
328
|
//# sourceMappingURL=LocalKVStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LocalKVStore.js","sourceRoot":"","sources":["../../../../src/kvstore/LocalKVStore.ts"],"names":[],"mappings":"AACA,OAAO,QAAQ,MAAM,iCAAiC,CAAA;AACtD,OAAO,KAAK,KAAK,MAAM,wBAAwB,CAAA;AAE/C,OAAO,YAAY,MAAM,2BAA2B,CAAA;AACpD,OAAO,WAAW,MAAM,+BAA+B,CAAA;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAA;AAE7C;;;;GA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B,CAAC,EAAE,CAAC,CAAA;wBACnF,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC;4BAC5C,SAAS,EAAE,mBAAmB,CAAC,SAAS;4BACxC,MAAM;yBACP,CAAC,CAAA;wBACF,IAAI,IAAI,KAAK,SAAS,EAAE;4BAAE,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;yBAAE;wBAClF,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;qBACjB;oBAAC,OAAO,CAAC,EAAE;wBACV,MAAM,IAAI,KAAK,CAAC,aAAa,YAAY,qBAAqB,GAAG,2BAA2B,CAAC,CAAA;qBAC9F;iBACF;gBACD,IAAI,OAAO,CAAC,MAAM,KAAK,YAAY,EAAE;oBAAE,MAAK;iBAAE;aAC/C;YACD,OAAO,KAAK,CAAA;SACb;gBAAS;YACR,IAAI,CAAC,oBAAoB,CAAC,GAAG,EAAE,SAAS,CAAC,CAAA;SAC1C;IACH,CAAC;CACF"}
|
|
@@ -3,22 +3,16 @@ import { StorageUtils } from './index.js';
|
|
|
3
3
|
import PushDrop from '../script/templates/PushDrop.js';
|
|
4
4
|
import Transaction from '../transaction/Transaction.js';
|
|
5
5
|
import { Hash, Utils } from '../primitives/index.js';
|
|
6
|
-
/**
|
|
7
|
-
* Locates HTTP URLs where content can be downloaded. It uses the passed or the default one.
|
|
8
|
-
*
|
|
9
|
-
* @param {Object} obj All parameters are passed in an object.
|
|
10
|
-
* @param {String} obj.uhrpUrl The UHRP url to resolve.
|
|
11
|
-
* @param {string} obj.confederacyHost HTTPS URL for for the with default setting.
|
|
12
|
-
*
|
|
13
|
-
* @return {Array<String>} An array of HTTP URLs where content can be downloaded.
|
|
14
|
-
* @throws {Error} If UHRP url parameter invalid or is not an array
|
|
15
|
-
* or there is an error retrieving url(s) stored in the UHRP token.
|
|
16
|
-
*/
|
|
17
6
|
export class StorageDownloader {
|
|
18
7
|
networkPreset = 'mainnet';
|
|
19
8
|
constructor(config) {
|
|
20
9
|
this.networkPreset = config?.networkPreset;
|
|
21
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Resolves the UHRP URL to a list of HTTP URLs where content can be downloaded.
|
|
13
|
+
* @param uhrpUrl The UHRP URL to resolve.
|
|
14
|
+
* @returns A promise that resolves to an array of HTTP URLs.
|
|
15
|
+
*/
|
|
22
16
|
async resolve(uhrpUrl) {
|
|
23
17
|
// Use UHRP lookup service
|
|
24
18
|
const lookupResolver = new LookupResolver({ networkPreset: this.networkPreset });
|
|
@@ -39,6 +33,11 @@ export class StorageDownloader {
|
|
|
39
33
|
}
|
|
40
34
|
return decodedResults;
|
|
41
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Downloads the content from the UHRP URL after validating the hash for integrity.
|
|
38
|
+
* @param uhrpUrl The UHRP URL to download.
|
|
39
|
+
* @returns A promise that resolves to the downloaded content.
|
|
40
|
+
*/
|
|
42
41
|
async download(uhrpUrl) {
|
|
43
42
|
if (!StorageUtils.isValidURL(uhrpUrl)) {
|
|
44
43
|
throw new Error('Invalid parameter UHRP url');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StorageDownloader.js","sourceRoot":"","sources":["../../../../src/storage/StorageDownloader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AACzC,OAAO,QAAQ,MAAM,iCAAiC,CAAA;AACtD,OAAO,WAAW,MAAM,+BAA+B,CAAA;AACvD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AAWpD
|
|
1
|
+
{"version":3,"file":"StorageDownloader.js","sourceRoot":"","sources":["../../../../src/storage/StorageDownloader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,YAA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