@bsv/sdk 1.6.5 → 1.6.6
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 +78 -52
- 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 +78 -52
- 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 +10 -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 +93 -55
- package/src/storage/StorageDownloader.ts +10 -11
package/docs/kvstore.md
CHANGED
|
@@ -14,7 +14,8 @@ Allows setting, getting, and removing key-value pairs, with optional encryption.
|
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
16
|
export default class LocalKVStore {
|
|
17
|
-
|
|
17
|
+
acceptDelayedBroadcast: boolean = false;
|
|
18
|
+
constructor(wallet: WalletInterface = new WalletClient(), context = "kvstore default", encrypt = true, originator?: string, acceptDelayedBroadcast = false)
|
|
18
19
|
async get(key: string, defaultValue: string | undefined = undefined): Promise<string | undefined>
|
|
19
20
|
async set(key: string, value: string): Promise<OutpointString>
|
|
20
21
|
async remove(key: string): Promise<string[]>
|
|
@@ -28,7 +29,7 @@ See also: [OutpointString](./wallet.md#type-outpointstring), [WalletClient](./wa
|
|
|
28
29
|
Creates an instance of the localKVStore.
|
|
29
30
|
|
|
30
31
|
```ts
|
|
31
|
-
constructor(wallet: WalletInterface = new WalletClient(), context = "kvstore default", encrypt = true, originator?: string)
|
|
32
|
+
constructor(wallet: WalletInterface = new WalletClient(), context = "kvstore default", encrypt = true, originator?: string, acceptDelayedBroadcast = false)
|
|
32
33
|
```
|
|
33
34
|
See also: [WalletClient](./wallet.md#class-walletclient), [WalletInterface](./wallet.md#interface-walletinterface), [encrypt](./messages.md#variable-encrypt)
|
|
34
35
|
|
|
@@ -96,13 +97,15 @@ Argument Details
|
|
|
96
97
|
|
|
97
98
|
#### Method set
|
|
98
99
|
|
|
99
|
-
Sets or updates the value associated with a given key.
|
|
100
|
+
Sets or updates the value associated with a given key atomically.
|
|
100
101
|
If the key already exists (one or more outputs found), it spends the existing output(s)
|
|
101
102
|
and creates a new one with the updated value. If multiple outputs exist for the key,
|
|
102
103
|
they are collapsed into a single new output.
|
|
103
104
|
If the key does not exist, it creates a new output.
|
|
104
105
|
Handles encryption if enabled.
|
|
105
106
|
If signing the update/collapse transaction fails, it relinquishes the original outputs and starts over with a new chain.
|
|
107
|
+
Ensures atomicity by locking the key during the operation, preventing concurrent updates
|
|
108
|
+
to the same key from missing earlier changes.
|
|
106
109
|
|
|
107
110
|
```ts
|
|
108
111
|
async set(key: string, value: string): Promise<OutpointString>
|
package/docs/storage.md
CHANGED
|
@@ -120,8 +120,6 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
|
|
|
120
120
|
|
|
121
121
|
### Class: StorageDownloader
|
|
122
122
|
|
|
123
|
-
Locates HTTP URLs where content can be downloaded. It uses the passed or the default one.
|
|
124
|
-
|
|
125
123
|
```ts
|
|
126
124
|
export class StorageDownloader {
|
|
127
125
|
constructor(config?: DownloaderConfig)
|
|
@@ -132,6 +130,41 @@ export class StorageDownloader {
|
|
|
132
130
|
|
|
133
131
|
See also: [DownloadResult](./storage.md#interface-downloadresult), [DownloaderConfig](./storage.md#interface-downloaderconfig)
|
|
134
132
|
|
|
133
|
+
#### Method download
|
|
134
|
+
|
|
135
|
+
Downloads the content from the UHRP URL after validating the hash for integrity.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
public async download(uhrpUrl: string): Promise<DownloadResult>
|
|
139
|
+
```
|
|
140
|
+
See also: [DownloadResult](./storage.md#interface-downloadresult)
|
|
141
|
+
|
|
142
|
+
Returns
|
|
143
|
+
|
|
144
|
+
A promise that resolves to the downloaded content.
|
|
145
|
+
|
|
146
|
+
Argument Details
|
|
147
|
+
|
|
148
|
+
+ **uhrpUrl**
|
|
149
|
+
+ The UHRP URL to download.
|
|
150
|
+
|
|
151
|
+
#### Method resolve
|
|
152
|
+
|
|
153
|
+
Resolves the UHRP URL to a list of HTTP URLs where content can be downloaded.
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
public async resolve(uhrpUrl: string): Promise<string[]>
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Returns
|
|
160
|
+
|
|
161
|
+
A promise that resolves to an array of HTTP URLs.
|
|
162
|
+
|
|
163
|
+
Argument Details
|
|
164
|
+
|
|
165
|
+
+ **uhrpUrl**
|
|
166
|
+
+ The UHRP URL to resolve.
|
|
167
|
+
|
|
135
168
|
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
|
|
136
169
|
|
|
137
170
|
---
|
package/package.json
CHANGED
|
@@ -38,6 +38,14 @@ export default class LocalKVStore {
|
|
|
38
38
|
*/
|
|
39
39
|
private readonly originator?: string
|
|
40
40
|
|
|
41
|
+
acceptDelayedBroadcast: boolean = false
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A map to store locks for each key to ensure atomic updates.
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
47
|
+
private readonly keyLocks: Map<string, Promise<void>> = new Map()
|
|
48
|
+
|
|
41
49
|
/**
|
|
42
50
|
* Creates an instance of the localKVStore.
|
|
43
51
|
*
|
|
@@ -51,7 +59,8 @@ export default class LocalKVStore {
|
|
|
51
59
|
wallet: WalletInterface = new WalletClient(),
|
|
52
60
|
context = 'kvstore default',
|
|
53
61
|
encrypt = true,
|
|
54
|
-
originator?: string
|
|
62
|
+
originator?: string,
|
|
63
|
+
acceptDelayedBroadcast = false
|
|
55
64
|
) {
|
|
56
65
|
if (typeof context !== 'string' || context.length < 1) {
|
|
57
66
|
throw new Error('A context in which to operate is required.')
|
|
@@ -60,6 +69,7 @@ export default class LocalKVStore {
|
|
|
60
69
|
this.context = context
|
|
61
70
|
this.encrypt = encrypt
|
|
62
71
|
this.originator = originator
|
|
72
|
+
this.acceptDelayedBroadcast = acceptDelayedBroadcast
|
|
63
73
|
}
|
|
64
74
|
|
|
65
75
|
private getProtocol (key: string): { protocolID: WalletProtocol, keyID: string } {
|
|
@@ -160,78 +170,106 @@ export default class LocalKVStore {
|
|
|
160
170
|
}
|
|
161
171
|
|
|
162
172
|
/**
|
|
163
|
-
* Sets or updates the value associated with a given key.
|
|
173
|
+
* Sets or updates the value associated with a given key atomically.
|
|
164
174
|
* If the key already exists (one or more outputs found), it spends the existing output(s)
|
|
165
175
|
* and creates a new one with the updated value. If multiple outputs exist for the key,
|
|
166
176
|
* they are collapsed into a single new output.
|
|
167
177
|
* If the key does not exist, it creates a new output.
|
|
168
178
|
* Handles encryption if enabled.
|
|
169
179
|
* If signing the update/collapse transaction fails, it relinquishes the original outputs and starts over with a new chain.
|
|
180
|
+
* Ensures atomicity by locking the key during the operation, preventing concurrent updates
|
|
181
|
+
* to the same key from missing earlier changes.
|
|
170
182
|
*
|
|
171
183
|
* @param {string} key - The key to set or update.
|
|
172
184
|
* @param {string} value - The value to associate with the key.
|
|
173
185
|
* @returns {Promise<OutpointString>} A promise that resolves to the outpoint string (txid.vout) of the new or updated token output.
|
|
174
186
|
*/
|
|
175
187
|
async set (key: string, value: string): Promise<OutpointString> {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
return current.outpoint
|
|
181
|
-
}
|
|
182
|
-
const protocol = this.getProtocol(key)
|
|
183
|
-
let valueAsArray = Utils.toArray(value, 'utf8')
|
|
184
|
-
if (this.encrypt) {
|
|
185
|
-
const { ciphertext } = await this.wallet.encrypt({
|
|
186
|
-
...protocol,
|
|
187
|
-
plaintext: valueAsArray
|
|
188
|
-
})
|
|
189
|
-
valueAsArray = ciphertext
|
|
188
|
+
// Check if a lock exists for this key and wait for it to resolve
|
|
189
|
+
const existingLock = this.keyLocks.get(key)
|
|
190
|
+
if (existingLock != null) {
|
|
191
|
+
await existingLock
|
|
190
192
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const { outputs, BEEF: inputBEEF } = current.lor
|
|
199
|
-
let outpoint: OutpointString
|
|
193
|
+
|
|
194
|
+
let resolveNewLock: () => void = () => {}
|
|
195
|
+
const newLock = new Promise<void>((resolve) => {
|
|
196
|
+
resolveNewLock = resolve
|
|
197
|
+
})
|
|
198
|
+
this.keyLocks.set(key, newLock)
|
|
199
|
+
|
|
200
200
|
try {
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
inputs,
|
|
206
|
-
outputs: [{
|
|
207
|
-
basket: this.context,
|
|
208
|
-
tags: [key],
|
|
209
|
-
lockingScript: lockingScript.toHex(),
|
|
210
|
-
satoshis: 1,
|
|
211
|
-
outputDescription: 'Key-value token'
|
|
212
|
-
}],
|
|
213
|
-
options: {
|
|
214
|
-
acceptDelayedBroadcast: false,
|
|
215
|
-
randomizeOutputs: false
|
|
201
|
+
const current = await this.lookupValue(key, undefined, 10)
|
|
202
|
+
if (current.value === value) {
|
|
203
|
+
if (current.outpoint === undefined) {
|
|
204
|
+
throw new Error('outpoint must be valid when value is valid and unchanged')
|
|
216
205
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
throw new Error('Wallet did not return a signable transaction when expected.')
|
|
206
|
+
// Don't create a new transaction if the value doesn't need to change
|
|
207
|
+
return current.outpoint
|
|
220
208
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const {
|
|
226
|
-
|
|
227
|
-
|
|
209
|
+
|
|
210
|
+
const protocol = this.getProtocol(key)
|
|
211
|
+
let valueAsArray = Utils.toArray(value, 'utf8')
|
|
212
|
+
if (this.encrypt) {
|
|
213
|
+
const { ciphertext } = await this.wallet.encrypt({
|
|
214
|
+
...protocol,
|
|
215
|
+
plaintext: valueAsArray
|
|
228
216
|
})
|
|
229
|
-
|
|
217
|
+
valueAsArray = ciphertext
|
|
230
218
|
}
|
|
231
|
-
|
|
232
|
-
|
|
219
|
+
|
|
220
|
+
const pushdrop = new PushDrop(this.wallet, this.originator)
|
|
221
|
+
const lockingScript = await pushdrop.lock(
|
|
222
|
+
[valueAsArray],
|
|
223
|
+
protocol.protocolID,
|
|
224
|
+
protocol.keyID,
|
|
225
|
+
'self'
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
const { outputs, BEEF: inputBEEF } = current.lor
|
|
229
|
+
let outpoint: OutpointString
|
|
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
|
+
}
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
if (outputs.length > 0 && typeof signableTransaction !== 'object') {
|
|
250
|
+
throw new Error('Wallet did not return a signable transaction when expected.')
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (signableTransaction == null) {
|
|
254
|
+
outpoint = `${txid as string}.0`
|
|
255
|
+
} else {
|
|
256
|
+
const spends = await this.getSpends(key, outputs, pushdrop, signableTransaction.tx)
|
|
257
|
+
const { txid } = await this.wallet.signAction({
|
|
258
|
+
reference: signableTransaction.reference,
|
|
259
|
+
spends
|
|
260
|
+
})
|
|
261
|
+
outpoint = `${txid as string}.0`
|
|
262
|
+
}
|
|
263
|
+
} catch (_) {
|
|
264
|
+
throw new Error(`There are ${outputs.length} outputs with tag ${key} that cannot be unlocked.`)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return outpoint
|
|
268
|
+
} finally {
|
|
269
|
+
// Release the lock by resolving the promise and removing it from the map
|
|
270
|
+
this.keyLocks.delete(key)
|
|
271
|
+
resolveNewLock()
|
|
233
272
|
}
|
|
234
|
-
return outpoint
|
|
235
273
|
}
|
|
236
274
|
|
|
237
275
|
/**
|
|
@@ -257,7 +295,7 @@ export default class LocalKVStore {
|
|
|
257
295
|
inputBEEF,
|
|
258
296
|
inputs,
|
|
259
297
|
options: {
|
|
260
|
-
acceptDelayedBroadcast:
|
|
298
|
+
acceptDelayedBroadcast: this.acceptDelayedBroadcast
|
|
261
299
|
}
|
|
262
300
|
})
|
|
263
301
|
if (typeof signableTransaction !== 'object') {
|
|
@@ -13,17 +13,6 @@ export interface DownloadResult {
|
|
|
13
13
|
mimeType: string | null
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
/**
|
|
17
|
-
* Locates HTTP URLs where content can be downloaded. It uses the passed or the default one.
|
|
18
|
-
*
|
|
19
|
-
* @param {Object} obj All parameters are passed in an object.
|
|
20
|
-
* @param {String} obj.uhrpUrl The UHRP url to resolve.
|
|
21
|
-
* @param {string} obj.confederacyHost HTTPS URL for for the with default setting.
|
|
22
|
-
*
|
|
23
|
-
* @return {Array<String>} An array of HTTP URLs where content can be downloaded.
|
|
24
|
-
* @throws {Error} If UHRP url parameter invalid or is not an array
|
|
25
|
-
* or there is an error retrieving url(s) stored in the UHRP token.
|
|
26
|
-
*/
|
|
27
16
|
export class StorageDownloader {
|
|
28
17
|
private readonly networkPreset?: 'mainnet' | 'testnet' | 'local' = 'mainnet'
|
|
29
18
|
|
|
@@ -31,6 +20,11 @@ export class StorageDownloader {
|
|
|
31
20
|
this.networkPreset = config?.networkPreset
|
|
32
21
|
}
|
|
33
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Resolves the UHRP URL to a list of HTTP URLs where content can be downloaded.
|
|
25
|
+
* @param uhrpUrl The UHRP URL to resolve.
|
|
26
|
+
* @returns A promise that resolves to an array of HTTP URLs.
|
|
27
|
+
*/
|
|
34
28
|
public async resolve (uhrpUrl: string): Promise<string[]> {
|
|
35
29
|
// Use UHRP lookup service
|
|
36
30
|
const lookupResolver = new LookupResolver({ networkPreset: this.networkPreset })
|
|
@@ -54,6 +48,11 @@ export class StorageDownloader {
|
|
|
54
48
|
return decodedResults
|
|
55
49
|
}
|
|
56
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Downloads the content from the UHRP URL after validating the hash for integrity.
|
|
53
|
+
* @param uhrpUrl The UHRP URL to download.
|
|
54
|
+
* @returns A promise that resolves to the downloaded content.
|
|
55
|
+
*/
|
|
57
56
|
public async download (uhrpUrl: string): Promise<DownloadResult> {
|
|
58
57
|
if (!StorageUtils.isValidURL(uhrpUrl)) {
|
|
59
58
|
throw new Error('Invalid parameter UHRP url')
|