@ethersphere/bee-js 1.2.1 → 3.0.0

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 (181) hide show
  1. package/CHANGELOG.md +135 -2
  2. package/README.md +3 -1
  3. package/dist/index.browser.min.js +1 -1
  4. package/dist/index.browser.min.js.LICENSE.txt +51 -0
  5. package/dist/index.browser.min.js.map +1 -1
  6. package/dist/index.min.js +1 -1
  7. package/dist/index.min.js.LICENSE.txt +42 -0
  8. package/dist/index.min.js.map +1 -1
  9. package/dist/src/bee-debug.d.ts +75 -38
  10. package/dist/src/bee.d.ts +88 -82
  11. package/dist/src/chunk/soc.d.ts +6 -6
  12. package/dist/src/feed/index.d.ts +7 -7
  13. package/dist/src/feed/json.d.ts +2 -2
  14. package/dist/src/modules/bytes.d.ts +8 -11
  15. package/dist/src/modules/bzz.d.ts +9 -14
  16. package/dist/src/modules/chunk.d.ts +7 -11
  17. package/dist/src/modules/debug/balance.d.ts +9 -9
  18. package/dist/src/modules/debug/chequebook.d.ts +17 -18
  19. package/dist/src/modules/debug/chunk.d.ts +5 -5
  20. package/dist/src/modules/debug/connectivity.d.ts +7 -7
  21. package/dist/src/modules/debug/settlements.d.ts +5 -5
  22. package/dist/src/modules/debug/stamps.d.ts +7 -5
  23. package/dist/src/modules/debug/states.d.ts +5 -5
  24. package/dist/src/modules/debug/status.d.ts +5 -4
  25. package/dist/src/modules/debug/tag.d.ts +3 -3
  26. package/dist/src/modules/debug/transactions.d.ts +9 -9
  27. package/dist/src/modules/feed.d.ts +5 -5
  28. package/dist/src/modules/pinning.d.ts +9 -9
  29. package/dist/src/modules/pss.d.ts +4 -3
  30. package/dist/src/modules/soc.d.ts +3 -3
  31. package/dist/src/modules/status.d.ts +3 -2
  32. package/dist/src/modules/stewardship.d.ts +4 -4
  33. package/dist/src/modules/tag.d.ts +6 -6
  34. package/dist/src/types/debug.d.ts +4 -2
  35. package/dist/src/types/index.d.ts +71 -19
  36. package/dist/src/utils/bytes.d.ts +1 -1
  37. package/dist/src/utils/data.browser.d.ts +3 -1
  38. package/dist/src/utils/data.d.ts +7 -4
  39. package/dist/src/utils/expose.d.ts +6 -9
  40. package/dist/src/utils/headers.d.ts +3 -3
  41. package/dist/src/utils/http.d.ts +25 -0
  42. package/dist/src/utils/merge.d.ts +8 -0
  43. package/dist/src/utils/pss.d.ts +9 -0
  44. package/dist/src/utils/stream.d.ts +37 -0
  45. package/dist/src/utils/type.d.ts +19 -9
  46. package/package.json +15 -8
  47. package/.babelrc.js +0 -33
  48. package/.depcheckrc.json +0 -27
  49. package/.editorconfig +0 -6
  50. package/.eslintignore +0 -1
  51. package/.eslintrc.js +0 -108
  52. package/.gitattributes +0 -1
  53. package/.github/config.yaml +0 -23
  54. package/.github/workflows/check.yaml +0 -57
  55. package/.github/workflows/publish_npmjs.yaml +0 -20
  56. package/.github/workflows/release_github.yaml +0 -20
  57. package/.github/workflows/tests.yaml +0 -128
  58. package/.huskyrc +0 -5
  59. package/.prettierrc +0 -13
  60. package/.vscode/launch.json +0 -49
  61. package/.vscode/settings.json +0 -39
  62. package/CODEOWNERS +0 -1
  63. package/CODE_OF_CONDUCT.md +0 -76
  64. package/commitlint.config.js +0 -3
  65. package/dist/src/modules/stamps.d.ts +0 -4
  66. package/dist/src/utils/hooks.d.ts +0 -31
  67. package/dist/src/utils/safe-axios.d.ts +0 -8
  68. package/jest-puppeteer.config.js +0 -5
  69. package/jest.config.ts +0 -170
  70. package/src/bee-debug.ts +0 -440
  71. package/src/bee.ts +0 -941
  72. package/src/chunk/bmt.ts +0 -55
  73. package/src/chunk/cac.ts +0 -79
  74. package/src/chunk/serialize.ts +0 -16
  75. package/src/chunk/signer.ts +0 -141
  76. package/src/chunk/soc.ts +0 -190
  77. package/src/chunk/span.ts +0 -33
  78. package/src/feed/index.ts +0 -221
  79. package/src/feed/json.ts +0 -32
  80. package/src/feed/topic.ts +0 -23
  81. package/src/feed/type.ts +0 -12
  82. package/src/index.ts +0 -24
  83. package/src/modules/bytes.ts +0 -75
  84. package/src/modules/bzz.ts +0 -184
  85. package/src/modules/chunk.ts +0 -75
  86. package/src/modules/debug/balance.ts +0 -63
  87. package/src/modules/debug/chequebook.ts +0 -174
  88. package/src/modules/debug/chunk.ts +0 -39
  89. package/src/modules/debug/connectivity.ts +0 -61
  90. package/src/modules/debug/settlements.ts +0 -33
  91. package/src/modules/debug/stamps.ts +0 -69
  92. package/src/modules/debug/states.ts +0 -35
  93. package/src/modules/debug/status.ts +0 -34
  94. package/src/modules/debug/tag.ts +0 -19
  95. package/src/modules/debug/transactions.ts +0 -79
  96. package/src/modules/feed.ts +0 -92
  97. package/src/modules/pinning.ts +0 -74
  98. package/src/modules/pss.ts +0 -48
  99. package/src/modules/soc.ts +0 -41
  100. package/src/modules/stamps.ts +0 -59
  101. package/src/modules/status.ts +0 -13
  102. package/src/modules/stewardship.ts +0 -21
  103. package/src/modules/tag.ts +0 -83
  104. package/src/types/axios/index.d.ts +0 -18
  105. package/src/types/debug.ts +0 -204
  106. package/src/types/index.ts +0 -497
  107. package/src/types/tar-js.d.ts +0 -1
  108. package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
  109. package/src/utils/bytes.ts +0 -144
  110. package/src/utils/collection.ts +0 -151
  111. package/src/utils/data.browser.ts +0 -24
  112. package/src/utils/data.ts +0 -35
  113. package/src/utils/error.ts +0 -23
  114. package/src/utils/eth.ts +0 -229
  115. package/src/utils/expose.ts +0 -10
  116. package/src/utils/file.ts +0 -40
  117. package/src/utils/hash.ts +0 -16
  118. package/src/utils/headers.ts +0 -63
  119. package/src/utils/hex.ts +0 -186
  120. package/src/utils/hooks.ts +0 -89
  121. package/src/utils/safe-axios.ts +0 -98
  122. package/src/utils/tar.ts +0 -30
  123. package/src/utils/type.ts +0 -302
  124. package/src/utils/uint64.ts +0 -31
  125. package/src/utils/url.ts +0 -52
  126. package/test/bee.sh +0 -187
  127. package/test/data/1.txt +0 -1
  128. package/test/data/2.txt +0 -0
  129. package/test/data/empty +0 -0
  130. package/test/data/sub/3.txt +0 -1
  131. package/test/data/sub//303/251 +0 -0
  132. package/test/data/sub//360/237/230/216 +0 -0
  133. package/test/integration/bee-class.browser.spec.ts +0 -215
  134. package/test/integration/bee-class.spec.ts +0 -595
  135. package/test/integration/bee-debug-class.spec.ts +0 -85
  136. package/test/integration/chunk/bmt.spec.ts +0 -33
  137. package/test/integration/chunk/cac.spec.ts +0 -27
  138. package/test/integration/chunk/soc.spec.ts +0 -38
  139. package/test/integration/feed/index.spec.ts +0 -84
  140. package/test/integration/modules/bytes.spec.ts +0 -23
  141. package/test/integration/modules/bzz.spec.ts +0 -270
  142. package/test/integration/modules/chunk.spec.ts +0 -29
  143. package/test/integration/modules/debug/balance.spec.ts +0 -70
  144. package/test/integration/modules/debug/chequebook.spec.ts +0 -64
  145. package/test/integration/modules/debug/connectivity.spec.ts +0 -69
  146. package/test/integration/modules/debug/settlements.spec.ts +0 -34
  147. package/test/integration/modules/debug/states.spec.ts +0 -34
  148. package/test/integration/modules/debug/status.spec.ts +0 -65
  149. package/test/integration/modules/debug/tag.spec.ts +0 -27
  150. package/test/integration/modules/feed.spec.ts +0 -61
  151. package/test/integration/modules/pinning.spec.ts +0 -155
  152. package/test/integration/modules/pss.spec.ts +0 -92
  153. package/test/integration/modules/status.spec.ts +0 -10
  154. package/test/integration/modules/stewardship.spec.ts +0 -32
  155. package/test/integration/modules/tag.spec.ts +0 -51
  156. package/test/test-type-sequencer.js +0 -36
  157. package/test/testpage/testpage.html +0 -20
  158. package/test/unit/assertions.ts +0 -376
  159. package/test/unit/bee-class.spec.ts +0 -685
  160. package/test/unit/bee-debug-class.spec.ts +0 -365
  161. package/test/unit/chunk/bmt.spec.ts +0 -16
  162. package/test/unit/chunk/cac.spec.ts +0 -29
  163. package/test/unit/chunk/serialize.spec.ts +0 -24
  164. package/test/unit/chunk/signer.spec.ts +0 -140
  165. package/test/unit/chunk/soc.spec.ts +0 -25
  166. package/test/unit/chunk/span.spec.ts +0 -38
  167. package/test/unit/feed/json.spec.ts +0 -73
  168. package/test/unit/nock.ts +0 -134
  169. package/test/unit/utils/bytes.spec.ts +0 -32
  170. package/test/unit/utils/collection.spec.ts +0 -22
  171. package/test/unit/utils/eth.spec.ts +0 -224
  172. package/test/unit/utils/hex.spec.ts +0 -115
  173. package/test/unit/utils/hooks.spec.ts +0 -179
  174. package/test/unit/utils/safe-axios.spec.ts +0 -20
  175. package/test/unit/utils/type.spec.ts +0 -32
  176. package/test/unit/utils/uint64.spec.ts +0 -102
  177. package/test/utils.ts +0 -292
  178. package/tsconfig.json +0 -18
  179. package/tsconfig.test.json +0 -11
  180. package/typedoc.json +0 -7
  181. package/webpack.config.ts +0 -141
@@ -1,151 +0,0 @@
1
- import fs from 'fs'
2
- import path from 'path'
3
- import { Collection } from '../types'
4
- import { BeeArgumentError } from './error'
5
- import { fileArrayBuffer } from './file'
6
- import { isUint8Array } from './type'
7
-
8
- export function isCollection(data: unknown): data is Collection<Uint8Array> {
9
- if (!Array.isArray(data)) {
10
- return false
11
- }
12
-
13
- return data.every(entry => typeof entry === 'object' && entry.data && entry.path && isUint8Array(entry.data))
14
- }
15
-
16
- export function assertCollection(data: unknown): asserts data is Collection<Uint8Array> {
17
- if (!isCollection(data)) {
18
- throw new BeeArgumentError('invalid collection', data)
19
- }
20
- }
21
-
22
- /**
23
- * Creates array in the format of Collection with data loaded from directory on filesystem.
24
- * The function loads all the data into memory!
25
- *
26
- * @param dir path to the directory
27
- */
28
- export async function makeCollectionFromFS(dir: string): Promise<Collection<Uint8Array>> {
29
- if (typeof dir !== 'string') {
30
- throw new TypeError('dir has to be string!')
31
- }
32
-
33
- if (dir === '') {
34
- throw new TypeError('dir must not be empty string!')
35
- }
36
-
37
- return buildCollectionRelative(dir, '')
38
- }
39
-
40
- async function buildCollectionRelative(dir: string, relativePath: string): Promise<Collection<Uint8Array>> {
41
- // Handles case when the dir is not existing or it is a file ==> throws an error
42
- const dirname = path.join(dir, relativePath)
43
- const entries = await fs.promises.opendir(dirname)
44
- let collection: Collection<Uint8Array> = []
45
-
46
- for await (const entry of entries) {
47
- const fullPath = path.join(dir, relativePath, entry.name)
48
- const entryPath = path.join(relativePath, entry.name)
49
-
50
- if (entry.isFile()) {
51
- collection.push({
52
- path: entryPath,
53
- data: new Uint8Array(await fs.promises.readFile(fullPath)),
54
- })
55
- } else if (entry.isDirectory()) {
56
- collection = [...(await buildCollectionRelative(dir, entryPath)), ...collection]
57
- }
58
- }
59
-
60
- return collection
61
- }
62
-
63
- /*
64
- * This is a workaround for fixing the type definitions
65
- * regarding the missing `webkitRelativePath` property which is
66
- * provided on files if you specify the `webkitdirectory`
67
- * property on the HTML input element. This is a non-standard
68
- * functionality supported in all major browsers.
69
- */
70
- interface WebkitFile extends File {
71
- readonly webkitRelativePath?: string
72
- }
73
-
74
- function makeFilePath(file: WebkitFile) {
75
- if (file.webkitRelativePath && file.webkitRelativePath !== '') {
76
- return file.webkitRelativePath.replace(/.*?\//i, '')
77
- }
78
-
79
- if (file.name) {
80
- return file.name
81
- }
82
-
83
- throw new TypeError('file is not valid File object')
84
- }
85
-
86
- export async function makeCollectionFromFileList(fileList: FileList | File[]): Promise<Collection<Uint8Array>> {
87
- const collection: Collection<Uint8Array> = []
88
-
89
- for (let i = 0; i < fileList.length; i++) {
90
- const file = fileList[i] as WebkitFile
91
-
92
- if (file) {
93
- collection.push({
94
- path: makeFilePath(file),
95
- data: new Uint8Array(await fileArrayBuffer(file)),
96
- })
97
- }
98
- }
99
-
100
- return collection
101
- }
102
-
103
- /**
104
- * Calculate cumulative size of files
105
- *
106
- * @param fileList list of files to check
107
- * @returns size in bytes
108
- */
109
- export function getCollectionSize(fileList: FileList | File[]): number {
110
- let sum = 0
111
-
112
- for (let i = 0; i < fileList.length; i++) {
113
- const file = fileList[i] as WebkitFile
114
-
115
- if (file) {
116
- sum += file.size
117
- }
118
- }
119
-
120
- return sum
121
- }
122
-
123
- /**
124
- * Calculate folder size recursively
125
- *
126
- * @param dir the path to the folder to check
127
- * @returns size in bytes
128
- */
129
- export async function getFolderSize(dir: string): Promise<number> {
130
- if (typeof dir !== 'string') {
131
- throw new TypeError('dir has to be string!')
132
- }
133
-
134
- if (dir === '') {
135
- throw new TypeError('dir must not be empty string!')
136
- }
137
-
138
- const entries = await fs.promises.opendir(dir)
139
- let size = 0
140
-
141
- for await (const entry of entries) {
142
- if (entry.isFile()) {
143
- const stats = await fs.promises.stat(path.join(dir, entry.name))
144
- size += stats.size
145
- } else if (entry.isDirectory()) {
146
- size += await getFolderSize(path.join(dir, entry.name))
147
- }
148
- }
149
-
150
- return size
151
- }
@@ -1,24 +0,0 @@
1
- /**
2
- * Validates input and converts to Uint8Array
3
- *
4
- * @param data any string, ArrayBuffer or Uint8Array
5
- */
6
- export function prepareData(data: string | ArrayBuffer | Uint8Array): Uint8Array | never {
7
- if (typeof data === 'string') return new TextEncoder().encode(data)
8
-
9
- if (data instanceof ArrayBuffer) return new Uint8Array(data)
10
-
11
- if (data instanceof Uint8Array) return data
12
-
13
- throw new TypeError('unknown data type')
14
- }
15
-
16
- export async function prepareWebsocketData(data: string | ArrayBuffer | Blob): Promise<Uint8Array> | never {
17
- if (typeof data === 'string') return new TextEncoder().encode(data)
18
-
19
- if (data instanceof ArrayBuffer) return new Uint8Array(data)
20
-
21
- if (data instanceof Blob) return new Uint8Array(await new Response(data as Blob).arrayBuffer())
22
-
23
- throw new TypeError('unknown websocket data type')
24
- }
package/src/utils/data.ts DELETED
@@ -1,35 +0,0 @@
1
- import { Readable } from 'stream'
2
- import type { Data } from 'ws'
3
-
4
- /**
5
- * Validates input and converts to Uint8Array or Readable
6
- *
7
- * @param data any string, ArrayBuffer, Uint8Array or Readable
8
- */
9
- export function prepareData(data: string | ArrayBuffer | Uint8Array | Readable): Uint8Array | Readable | never {
10
- if (typeof data === 'string') return new TextEncoder().encode(data)
11
-
12
- if (data instanceof ArrayBuffer) return new Uint8Array(data)
13
-
14
- if (data instanceof Uint8Array || data instanceof Readable) return data
15
-
16
- throw new TypeError('unknown data type')
17
- }
18
-
19
- function isBufferArray(buffer: unknown): buffer is Buffer[] {
20
- return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer)
21
- }
22
-
23
- export async function prepareWebsocketData(data: Data | Blob): Promise<Uint8Array> | never {
24
- if (typeof data === 'string') return new TextEncoder().encode(data)
25
-
26
- if (data instanceof Buffer) return new Uint8Array(data)
27
-
28
- if (data instanceof ArrayBuffer) return new Uint8Array(data)
29
-
30
- if (data instanceof Blob) return new Uint8Array(await new Response(data as Blob).arrayBuffer())
31
-
32
- if (isBufferArray(data)) return new Uint8Array(Buffer.concat(data))
33
-
34
- throw new TypeError('unknown websocket data type')
35
- }
@@ -1,23 +0,0 @@
1
- export class BeeError extends Error {
2
- public constructor(message: string) {
3
- super(message)
4
- }
5
- }
6
-
7
- export class BeeArgumentError extends BeeError {
8
- public constructor(message: string, readonly value: unknown) {
9
- super(message)
10
- }
11
- }
12
-
13
- export class BeeRequestError extends BeeError {
14
- public constructor(message: string) {
15
- super(message)
16
- }
17
- }
18
-
19
- export class BeeResponseError extends BeeError {
20
- public constructor(readonly status: number, message: string) {
21
- super(message)
22
- }
23
- }
package/src/utils/eth.ts DELETED
@@ -1,229 +0,0 @@
1
- import { keccak256, sha3_256 } from 'js-sha3'
2
- import { BrandedString, Data, Signer } from '../types'
3
- import { HexString, hexToBytes, intToHex, makeHexString, assertHexString } from './hex'
4
- import { Bytes, assertBytes } from './bytes'
5
-
6
- export type OverlayAddress = BrandedString<'OverlayAddress'>
7
- export type EthAddress = Bytes<20>
8
- export type HexEthAddress = HexString<40>
9
- const ETH_ADDR_BYTES_LENGTH = 20
10
- const ETH_ADDR_HEX_LENGTH = 40
11
-
12
- export function makeEthAddress(address: EthAddress | Uint8Array | string | unknown): EthAddress {
13
- if (typeof address === 'string') {
14
- const hexAddr = makeHexString(address, ETH_ADDR_HEX_LENGTH)
15
- const ownerBytes = hexToBytes<typeof ETH_ADDR_BYTES_LENGTH>(hexAddr)
16
- assertBytes(ownerBytes, ETH_ADDR_BYTES_LENGTH)
17
-
18
- return ownerBytes
19
- } else if (address instanceof Uint8Array) {
20
- assertBytes(address, ETH_ADDR_BYTES_LENGTH)
21
-
22
- return address
23
- }
24
- throw new TypeError('Invalid EthAddress')
25
- }
26
-
27
- export function makeHexEthAddress(address: EthAddress | Uint8Array | string | unknown): HexEthAddress {
28
- try {
29
- return makeHexString(address, ETH_ADDR_HEX_LENGTH)
30
- } catch (e) {
31
- if (e instanceof TypeError) {
32
- e.message = `Invalid HexEthAddress: ${e.message}`
33
- }
34
-
35
- throw e
36
- }
37
- }
38
-
39
- /**
40
- * Check if this is all caps or small caps eth address (=address without checksum)
41
- *
42
- * @param address Ethereum address as hex string
43
- */
44
- function isEthAddrCaseIns(address: string | HexString | HexEthAddress): address is HexEthAddress {
45
- // Check it's string, all small caps or all all caps hex and 40 chars long without the `0x` prefix
46
- return (
47
- typeof address === 'string' && (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address))
48
- )
49
- }
50
-
51
- /**
52
- * Check if this is checksummed ethereum address
53
- *
54
- * @param address Ethereum address as hex string
55
- */
56
- function isValidChecksummedEthAddress(address: string | HexString | HexEthAddress): address is HexEthAddress {
57
- try {
58
- // Check for valid case insensitive hex type string, 40 chars
59
- const addr = makeHexString(address, ETH_ADDR_HEX_LENGTH)
60
-
61
- // Check the checksum
62
- const addressHash = keccak256(addr.toLowerCase())
63
- for (let i = 0; i < 40; i += 1) {
64
- // the nth letter should be uppercase if the nth digit of casemap is 1
65
- if (
66
- (parseInt(addressHash[i], 16) > 7 && addr[i].toUpperCase() !== addr[i]) ||
67
- (parseInt(addressHash[i], 16) <= 7 && addr[i].toLowerCase() !== addr[i])
68
- ) {
69
- return false
70
- }
71
- }
72
-
73
- return true
74
- } catch (e) {
75
- if (e instanceof TypeError) {
76
- return false
77
- }
78
-
79
- throw e
80
- }
81
- }
82
-
83
- /**
84
- * Check if is valid ethereum address
85
- *
86
- * Pretty much typed version from web3js
87
- * https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3-utils/src/utils.js
88
- *
89
- * @param address Ethereum address as hex string
90
- *
91
- * @return True if is valid eth address
92
- */
93
- export function isHexEthAddress(address: string | HexString | HexEthAddress): address is HexEthAddress {
94
- return isEthAddrCaseIns(address) || isValidChecksummedEthAddress(address)
95
- }
96
-
97
- /**
98
- * Convert big-endian hex or number to little-endian.
99
- * Note: Before conversion it is automatically padded to even length hexstring
100
- *
101
- * @param bigEndian Big-endian hex string or number to convert
102
- * @param pad Length to which the string should be padded before conversion (defaul: 2)
103
- *
104
- * @return little-endian encoded hexstring
105
- */
106
- export function toLittleEndian(bigEndian: number | string | HexString, pad = 2): HexString | never {
107
- if (!(Number.isInteger(pad) && pad >= 2 && pad % 2 === 0)) {
108
- throw new TypeError('minimal padding for conversion needs to be positive even integer')
109
- }
110
-
111
- let hexRep
112
-
113
- if (typeof bigEndian === 'string') hexRep = makeHexString(bigEndian as HexString)
114
- else if (typeof bigEndian === 'number') hexRep = intToHex(bigEndian)
115
- else throw new TypeError('incorrect input type')
116
-
117
- hexRep = hexRep.padStart(pad, '0')
118
-
119
- // Extend to an even length hexstring
120
- if (hexRep.length % 2 !== 0) hexRep = hexRep.padStart(hexRep.length + 1, '0')
121
-
122
- // Match all two pairs in the hexstring, reverse the pairs and join it again
123
- const littleEndian = hexRep.match(/../g)?.reverse().join('')
124
-
125
- if (littleEndian) return littleEndian as HexString
126
-
127
- throw new Error('failed to convert')
128
- }
129
-
130
- /**
131
- * Convert little-endian hex or number to big-endian
132
- * Note: Before conversion it is automatically padded to even length hexstring
133
- *
134
- * @param littleEndian Little-endian hex string or number to convert
135
- * @param pad Length to which the string should be padded before conversion (defaul: 2)
136
- *
137
- * @return big-endian encoded hexstring
138
- */
139
- export function fromLittleEndian(littleEndian: number | string | HexString, pad = 2): HexString | never {
140
- // It's a reversible function
141
- return toLittleEndian(littleEndian, pad)
142
- }
143
-
144
- function assertEthAddress(ethAddress: string | HexString | HexEthAddress): asserts ethAddress is HexEthAddress {
145
- if (!isHexEthAddress(ethAddress)) throw new TypeError('invalid ETH address')
146
- }
147
-
148
- function assertSwarmNetworkId(networkId: number): asserts networkId is number {
149
- if (Number.isInteger(networkId && networkId > 0 && networkId < Number.MAX_SAFE_INTEGER)) {
150
- throw new TypeError('swarm network id must be positive integer')
151
- }
152
- }
153
-
154
- /**
155
- * Get swarm overlay address from public ethereum address and swarm network id
156
- *
157
- * @param ethAddress Public ethereum address
158
- * @param networkId Swarm network id
159
- *
160
- * @return Swarm overlay address
161
- */
162
- export function ethToSwarmAddress(ethAddress: string | HexString | HexEthAddress, networkId = 1): OverlayAddress {
163
- assertEthAddress(ethAddress)
164
- assertSwarmNetworkId(networkId)
165
-
166
- const hex = `${makeHexString(ethAddress)}${toLittleEndian(networkId, 16)}`
167
- assertHexString(hex)
168
-
169
- const overlayAddress = sha3_256(hexToBytes(hex))
170
-
171
- return overlayAddress as OverlayAddress
172
- }
173
-
174
- interface RequestArguments {
175
- method: string
176
- jsonrpc?: string
177
- params?: unknown[] | Record<string, unknown>
178
- }
179
-
180
- export interface JsonRPC {
181
- request?(args: RequestArguments): Promise<unknown>
182
- sendAsync?(args: RequestArguments): Promise<unknown>
183
- }
184
-
185
- /**
186
- * Function that takes Ethereum EIP-1193 compatible provider and create an Signer instance that
187
- * uses `personal_sign` method to sign requested data.
188
- *
189
- * @param provider Injected web3 provider like window.ethereum or other compatible with EIP-1193
190
- * @param ethAddress Optional address of the account which the data should be signed with. If not specified `eth_requestAccounts` request is used to get the account address.
191
- */
192
- export async function makeEthereumWalletSigner(
193
- provider: JsonRPC,
194
- ethAddress?: string | HexString | HexEthAddress,
195
- ): Promise<Signer> {
196
- let executorFnc: (args: RequestArguments) => Promise<unknown>
197
-
198
- if (typeof provider !== 'object' || provider === null) {
199
- throw new TypeError('We need JsonRPC provider object!')
200
- }
201
-
202
- if (provider.request) {
203
- executorFnc = provider.request
204
- } else if (provider.sendAsync) {
205
- executorFnc = provider.sendAsync
206
- } else {
207
- throw new Error('Incompatible interface of given provider!')
208
- }
209
-
210
- if (!ethAddress) {
211
- ethAddress = ((await executorFnc({ method: 'eth_requestAccounts' })) as string[])[0]
212
- }
213
-
214
- const bytesEthAddress = makeEthAddress(ethAddress)
215
- const hexEthAddress = makeHexEthAddress(ethAddress)
216
-
217
- return {
218
- address: bytesEthAddress,
219
- sign: async (data: Data): Promise<string> => {
220
- const result = await executorFnc({
221
- jsonrpc: '2.0',
222
- method: 'personal_sign',
223
- params: ['0x' + data.hex(), '0x' + hexEthAddress],
224
- })
225
-
226
- return result as string
227
- },
228
- } as Signer
229
- }
@@ -1,10 +0,0 @@
1
- export * as Collections from './collection'
2
- export * as Bytes from './bytes'
3
- export * as Data from './data'
4
- export * as File from './file'
5
- export * as Hex from './hex'
6
- export * as Tar from './tar'
7
- export * as Eth from './eth'
8
- export * as Hooks from './hooks'
9
- export { setDefaultHeaders } from './safe-axios'
10
- export { keccak256Hash } from './hash'
package/src/utils/file.ts DELETED
@@ -1,40 +0,0 @@
1
- /**
2
- * Compatibility functions for working with File API objects
3
- *
4
- * https://developer.mozilla.org/en-US/docs/Web/API/File
5
- */
6
-
7
- export function isFile(file: unknown): file is File {
8
- // browser
9
- if (typeof File === 'function') {
10
- return file instanceof File
11
- }
12
-
13
- // node.js
14
- const f = file as File
15
-
16
- return (
17
- typeof f === 'object' &&
18
- typeof f.name === 'string' &&
19
- (typeof f.stream === 'function' || typeof f.arrayBuffer === 'function')
20
- )
21
- }
22
-
23
- /**
24
- * Compatibility helper for browsers where the `arrayBuffer function is
25
- * missing from `File` objects.
26
- *
27
- * @param file A File object
28
- */
29
- export async function fileArrayBuffer(file: File): Promise<ArrayBuffer> {
30
- if (file.arrayBuffer) {
31
- return file.arrayBuffer()
32
- }
33
-
34
- // workaround for Safari where arrayBuffer is not supported on Files
35
- return new Promise(resolve => {
36
- const fr = new FileReader()
37
- fr.onload = () => resolve(fr.result as ArrayBuffer)
38
- fr.readAsArrayBuffer(file)
39
- })
40
- }
package/src/utils/hash.ts DELETED
@@ -1,16 +0,0 @@
1
- import { keccak256, Message } from 'js-sha3'
2
- import { Bytes } from './bytes'
3
-
4
- /**
5
- * Helper function for calculating the keccak256 hash with
6
- * correct types.
7
- *
8
- * @param messages Any number of messages (strings, byte arrays etc.)
9
- */
10
- export function keccak256Hash(...messages: Message[]): Bytes<32> {
11
- const hasher = keccak256.create()
12
-
13
- messages.forEach(bytes => hasher.update(bytes))
14
-
15
- return Uint8Array.from(hasher.digest()) as Bytes<32>
16
- }
@@ -1,63 +0,0 @@
1
- import { BatchId, Dictionary, FileHeaders, UploadHeaders, UploadOptions } from '../types'
2
- import { BeeError } from './error'
3
-
4
- /**
5
- * Read the filename from the content-disposition header
6
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
7
- *
8
- * @param header the content-disposition header value
9
- *
10
- * @returns the filename
11
- */
12
- function readContentDispositionFilename(header?: string): string {
13
- if (!header) {
14
- throw new BeeError('missing content-disposition header')
15
- }
16
-
17
- // Regex was found here
18
- // https://stackoverflow.com/questions/23054475/javascript-regex-for-extracting-filename-from-content-disposition-header
19
- const dispositionMatch = header.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i)
20
-
21
- if (dispositionMatch && dispositionMatch.length > 0) {
22
- return dispositionMatch[1]
23
- }
24
- throw new BeeError('invalid content-disposition header')
25
- }
26
-
27
- function readTagUid(header?: string): number | undefined {
28
- if (!header) {
29
- return undefined
30
- }
31
-
32
- return parseInt(header, 10)
33
- }
34
-
35
- export function readFileHeaders(headers: Dictionary<string>): FileHeaders {
36
- const name = readContentDispositionFilename(headers['content-disposition'])
37
- const tagUid = readTagUid(headers['swarm-tag-uid'])
38
- const contentType = headers['content-type']
39
-
40
- return {
41
- name,
42
- tagUid,
43
- contentType,
44
- }
45
- }
46
-
47
- export function extractUploadHeaders(postageBatchId: BatchId, options?: UploadOptions): UploadHeaders {
48
- if (!postageBatchId) {
49
- throw new BeeError('Postage BatchID has to be specified!')
50
- }
51
-
52
- const headers: UploadHeaders = {
53
- 'swarm-postage-batch-id': postageBatchId,
54
- }
55
-
56
- if (options?.pin) headers['swarm-pin'] = String(options.pin)
57
-
58
- if (options?.encrypt) headers['swarm-encrypt'] = String(options.encrypt)
59
-
60
- if (options?.tag) headers['swarm-tag'] = String(options.tag)
61
-
62
- return headers
63
- }