@bsv/sdk 1.4.0 → 1.4.2

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.
Files changed (80) hide show
  1. package/dist/cjs/mod.js +2 -0
  2. package/dist/cjs/mod.js.map +1 -1
  3. package/dist/cjs/package.json +1 -1
  4. package/dist/cjs/src/identity/IdentityClient.js +258 -0
  5. package/dist/cjs/src/identity/IdentityClient.js.map +1 -0
  6. package/dist/cjs/src/identity/index.js +19 -0
  7. package/dist/cjs/src/identity/index.js.map +1 -0
  8. package/dist/cjs/src/identity/types/index.js +30 -0
  9. package/dist/cjs/src/identity/types/index.js.map +1 -0
  10. package/dist/cjs/src/registry/RegistryClient.js +392 -0
  11. package/dist/cjs/src/registry/RegistryClient.js.map +1 -0
  12. package/dist/cjs/src/registry/index.js +19 -0
  13. package/dist/cjs/src/registry/index.js.map +1 -0
  14. package/dist/cjs/src/registry/types/index.js +3 -0
  15. package/dist/cjs/src/registry/types/index.js.map +1 -0
  16. package/dist/cjs/src/storage/StorageDownloader.js +82 -0
  17. package/dist/cjs/src/storage/StorageDownloader.js.map +1 -0
  18. package/dist/cjs/src/storage/__test/StorageDownloader.test.js +144 -0
  19. package/dist/cjs/src/storage/__test/StorageDownloader.test.js.map +1 -0
  20. package/dist/cjs/src/storage/index.js +3 -1
  21. package/dist/cjs/src/storage/index.js.map +1 -1
  22. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  23. package/dist/esm/mod.js +2 -0
  24. package/dist/esm/mod.js.map +1 -1
  25. package/dist/esm/src/identity/IdentityClient.js +255 -0
  26. package/dist/esm/src/identity/IdentityClient.js.map +1 -0
  27. package/dist/esm/src/identity/index.js +3 -0
  28. package/dist/esm/src/identity/index.js.map +1 -0
  29. package/dist/esm/src/identity/types/index.js +27 -0
  30. package/dist/esm/src/identity/types/index.js.map +1 -0
  31. package/dist/esm/src/registry/RegistryClient.js +388 -0
  32. package/dist/esm/src/registry/RegistryClient.js.map +1 -0
  33. package/dist/esm/src/registry/index.js +3 -0
  34. package/dist/esm/src/registry/index.js.map +1 -0
  35. package/dist/esm/src/registry/types/index.js +2 -0
  36. package/dist/esm/src/registry/types/index.js.map +1 -0
  37. package/dist/esm/src/storage/StorageDownloader.js +75 -0
  38. package/dist/esm/src/storage/StorageDownloader.js.map +1 -0
  39. package/dist/esm/src/storage/__test/StorageDownloader.test.js +139 -0
  40. package/dist/esm/src/storage/__test/StorageDownloader.test.js.map +1 -0
  41. package/dist/esm/src/storage/index.js +1 -0
  42. package/dist/esm/src/storage/index.js.map +1 -1
  43. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  44. package/dist/types/mod.d.ts +2 -0
  45. package/dist/types/mod.d.ts.map +1 -1
  46. package/dist/types/src/identity/IdentityClient.d.ts +50 -0
  47. package/dist/types/src/identity/IdentityClient.d.ts.map +1 -0
  48. package/dist/types/src/identity/index.d.ts +3 -0
  49. package/dist/types/src/identity/index.d.ts.map +1 -0
  50. package/dist/types/src/identity/types/index.d.ts +30 -0
  51. package/dist/types/src/identity/types/index.d.ts.map +1 -0
  52. package/dist/types/src/registry/RegistryClient.d.ts +94 -0
  53. package/dist/types/src/registry/RegistryClient.d.ts.map +1 -0
  54. package/dist/types/src/registry/index.d.ts +3 -0
  55. package/dist/types/src/registry/index.d.ts.map +1 -0
  56. package/dist/types/src/registry/types/index.d.ts +86 -0
  57. package/dist/types/src/registry/types/index.d.ts.map +1 -0
  58. package/dist/types/src/storage/StorageDownloader.d.ts +25 -0
  59. package/dist/types/src/storage/StorageDownloader.d.ts.map +1 -0
  60. package/dist/types/src/storage/__test/StorageDownloader.test.d.ts +2 -0
  61. package/dist/types/src/storage/__test/StorageDownloader.test.d.ts.map +1 -0
  62. package/dist/types/src/storage/index.d.ts +1 -0
  63. package/dist/types/src/storage/index.d.ts.map +1 -1
  64. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  65. package/dist/umd/bundle.js +1 -1
  66. package/docs/storage.md +51 -0
  67. package/mod.ts +3 -1
  68. package/package.json +2 -2
  69. package/src/identity/IdentityClient.ts +305 -0
  70. package/src/identity/README.md +93 -0
  71. package/src/identity/__tests/IdentityClient.test.ts +278 -0
  72. package/src/identity/index.ts +2 -0
  73. package/src/identity/types/index.ts +46 -0
  74. package/src/registry/RegistryClient.ts +493 -0
  75. package/src/registry/__tests/RegistryClient.test.ts +444 -0
  76. package/src/registry/index.ts +2 -0
  77. package/src/registry/types/index.ts +101 -0
  78. package/src/storage/StorageDownloader.ts +91 -0
  79. package/src/storage/__test/StorageDownloader.test.ts +170 -0
  80. package/src/storage/index.ts +3 -0
@@ -0,0 +1,170 @@
1
+ import { StorageDownloader } from '../StorageDownloader.js'
2
+ import { StorageUtils } from '../index.js'
3
+ import { LookupResolver } from '../../overlay-tools/index.js'
4
+ import Transaction from '../../transaction/Transaction.js'
5
+ import PushDrop from '../../script/templates/PushDrop.js'
6
+ import { Hash, PublicKey } from '../../primitives/index.js'
7
+ import { Utils } from '../../primitives/index.js'
8
+
9
+ beforeEach(() => {
10
+ jest.restoreAllMocks()
11
+ })
12
+
13
+ describe('StorageDownloader', () => {
14
+ let downloader: StorageDownloader
15
+
16
+ beforeEach(() => {
17
+ // Create a fresh instance
18
+ downloader = new StorageDownloader()
19
+ })
20
+
21
+ describe('resolve()', () => {
22
+ it('throws if the lookup response is not "output-list"', async () => {
23
+ // Mock the LookupResolver to return something invalid
24
+ jest.spyOn(LookupResolver.prototype, 'query').mockResolvedValue({
25
+ type: 'something-else',
26
+ outputs: []
27
+ } as any)
28
+
29
+ await expect(downloader.resolve('fakeUhrpUrl'))
30
+ .rejects
31
+ .toThrow('Lookup answer must be an output list')
32
+ })
33
+
34
+ it('decodes each output with Transaction.fromBEEF and PushDrop.decode', async () => {
35
+ // 1) Mock lookup response
36
+ jest.spyOn(LookupResolver.prototype, 'query').mockResolvedValue({
37
+ type: 'output-list',
38
+ outputs: [
39
+ { beef: 'fake-beef-a', outputIndex: 0 },
40
+ { beef: 'fake-beef-b', outputIndex: 1 }
41
+ ]
42
+ } as any)
43
+
44
+ // 2) Mock Transaction.fromBEEF -> returns a dummy transaction
45
+ jest.spyOn(Transaction, 'fromBEEF').mockImplementation(() => {
46
+ // Each transaction might have multiple outputs; we only care about `outputIndex`
47
+ return {
48
+ outputs: [
49
+ { lockingScript: {} }, // index 0
50
+ { lockingScript: {} } // index 1
51
+ ]
52
+ } as any
53
+ })
54
+
55
+ // 3) Mock PushDrop.decode -> returns { fields: number[][] }
56
+ jest.spyOn(PushDrop, 'decode').mockImplementation(() => {
57
+ // The decode function returns an object with `fields`,
58
+ return {
59
+ lockingPublicKey: {} as PublicKey,
60
+ fields: [
61
+ [11],
62
+ [22],
63
+ [104, 116, 116, 112, 58, 47, 47, 97, 46, 99, 111, 109]
64
+ ]
65
+ }
66
+ })
67
+
68
+ // 4) Mock Utils.toUTF8 to convert that number[] to a string
69
+ jest.spyOn(Utils, 'toUTF8').mockReturnValue('http://a.com')
70
+
71
+ const resolved = await downloader.resolve('fakeUhrpUrl')
72
+ expect(resolved).toEqual(['http://a.com', 'http://a.com'])
73
+ })
74
+ })
75
+
76
+ describe('download()', () => {
77
+ it('throws if UHRP URL is invalid', async () => {
78
+ jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(false)
79
+
80
+ await expect(downloader.download('invalidUrl'))
81
+ .rejects
82
+ .toThrow('Invalid parameter UHRP url')
83
+ })
84
+
85
+ it('throws if no hosts are found', async () => {
86
+ // Valid UHRP URL
87
+ jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(true)
88
+ // Return some random 32-byte hash so we can pass the check
89
+ jest.spyOn(StorageUtils, 'getHashFromURL').mockReturnValue(new Array(32).fill(0))
90
+
91
+ // Force resolve() to return an empty array
92
+ jest.spyOn(downloader, 'resolve').mockResolvedValue([])
93
+
94
+ await expect(downloader.download('validButUnhostedUrl'))
95
+ .rejects
96
+ .toThrow('No one currently hosts this file!')
97
+ })
98
+
99
+ it('downloads successfully from the first working host', async () => {
100
+ jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(true)
101
+ const knownHash = [
102
+ 102, 104, 122, 173, 248, 98, 189, 119, 108, 143,
103
+ 193, 139, 142, 159, 142, 32, 8, 151, 20, 133,
104
+ 110, 226, 51, 179, 144, 42, 89, 29, 13, 95,
105
+ 41, 37
106
+ ]
107
+ jest.spyOn(StorageUtils, 'getHashFromURL').mockReturnValue(knownHash)
108
+
109
+ // Suppose two possible download URLs
110
+ jest.spyOn(downloader, 'resolve').mockResolvedValue([
111
+ 'http://host1/404',
112
+ 'http://host2/ok'
113
+ ])
114
+
115
+ // The first fetch -> 404, second fetch -> success
116
+ const fetchSpy = jest.spyOn(global, 'fetch')
117
+ .mockResolvedValueOnce(new Response(null, { status: 404 }))
118
+ .mockResolvedValueOnce(new Response(new Uint8Array(32).fill(0), {
119
+ status: 200,
120
+ headers: { 'Content-Type': 'application/test' }
121
+ }))
122
+
123
+ const result = await downloader.download('validUrl')
124
+ expect(fetchSpy).toHaveBeenCalledTimes(2)
125
+ expect(result).toEqual({
126
+ data: new Array(32).fill(0),
127
+ mimeType: 'application/test'
128
+ })
129
+ })
130
+
131
+ it('throws if content hash mismatches the UHRP hash', async () => {
132
+ jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(true)
133
+ // The expected hash is all zeros
134
+ jest.spyOn(StorageUtils, 'getHashFromURL').mockReturnValue(new Array(32).fill(0))
135
+
136
+ // One potential host
137
+ jest.spyOn(downloader, 'resolve').mockResolvedValue([
138
+ 'http://bad-content.test'
139
+ ])
140
+
141
+ // The fetch returns 32 bytes of all 1's => hash mismatch
142
+ jest.spyOn(global, 'fetch').mockResolvedValue(
143
+ new Response(new Uint8Array(32).fill(1), { status: 200 })
144
+ )
145
+
146
+ await expect(downloader.download('validButBadHashUrl'))
147
+ .rejects
148
+ .toThrow()
149
+ })
150
+
151
+ it('throws if all hosts fail or mismatch', async () => {
152
+ jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(true)
153
+ jest.spyOn(StorageUtils, 'getHashFromURL').mockReturnValue(new Array(32).fill(0))
154
+
155
+ jest.spyOn(downloader, 'resolve').mockResolvedValue([
156
+ 'http://host1.test',
157
+ 'http://host2.test'
158
+ ])
159
+
160
+ // Both fetches fail with 500 or something >=400
161
+ jest.spyOn(global, 'fetch').mockResolvedValue(
162
+ new Response(null, { status: 500 })
163
+ )
164
+
165
+ await expect(downloader.download('validButNoGoodHostUrl'))
166
+ .rejects
167
+ .toThrow('Unable to download content from validButNoGoodHostUrl')
168
+ })
169
+ })
170
+ })
@@ -1,2 +1,5 @@
1
1
  export * as StorageUtils from './StorageUtils.js'
2
+
2
3
  export { StorageUploader } from './StorageUploader.js'
4
+
5
+ export { StorageDownloader } from './StorageDownloader.js'