@ethersphere/bee-js 2.0.0 → 3.1.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.
- package/CHANGELOG.md +91 -0
- package/README.md +2 -2
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/src/bee-debug.d.ts +38 -6
- package/dist/src/bee.d.ts +34 -46
- package/dist/src/modules/chunk.d.ts +2 -10
- package/dist/src/modules/debug/stamps.d.ts +2 -0
- package/dist/src/modules/debug/status.d.ts +10 -4
- package/dist/src/modules/stewardship.d.ts +1 -0
- package/dist/src/types/debug.d.ts +11 -0
- package/dist/src/types/index.d.ts +6 -7
- package/dist/src/utils/bytes.d.ts +1 -1
- package/dist/src/utils/expose.d.ts +1 -0
- package/dist/src/utils/stamps.d.ts +12 -0
- package/package.json +10 -8
- package/.babelrc.js +0 -33
- package/.depcheckrc.json +0 -27
- package/.editorconfig +0 -6
- package/.eslintignore +0 -1
- package/.eslintrc.js +0 -109
- package/.gitattributes +0 -1
- package/.github/config.yaml +0 -23
- package/.github/workflows/check.yaml +0 -57
- package/.github/workflows/publish_npmjs.yaml +0 -20
- package/.github/workflows/release_github.yaml +0 -20
- package/.github/workflows/tests.yaml +0 -128
- package/.huskyrc +0 -5
- package/.prettierrc +0 -13
- package/.vscode/launch.json +0 -49
- package/.vscode/settings.json +0 -39
- package/CODEOWNERS +0 -1
- package/CODE_OF_CONDUCT.md +0 -76
- package/commitlint.config.js +0 -3
- package/dist/src/modules/stamps.d.ts +0 -4
- package/jest-puppeteer.config.js +0 -5
- package/jest.config.ts +0 -85
- package/src/bee-debug.ts +0 -559
- package/src/bee.ts +0 -1070
- package/src/chunk/bmt.ts +0 -55
- package/src/chunk/cac.ts +0 -79
- package/src/chunk/serialize.ts +0 -16
- package/src/chunk/signer.ts +0 -141
- package/src/chunk/soc.ts +0 -190
- package/src/chunk/span.ts +0 -33
- package/src/feed/index.ts +0 -217
- package/src/feed/json.ts +0 -33
- package/src/feed/topic.ts +0 -23
- package/src/feed/type.ts +0 -12
- package/src/index.ts +0 -24
- package/src/modules/bytes.ts +0 -70
- package/src/modules/bzz.ts +0 -179
- package/src/modules/chunk.ts +0 -69
- package/src/modules/debug/balance.ts +0 -63
- package/src/modules/debug/chequebook.ts +0 -175
- package/src/modules/debug/chunk.ts +0 -39
- package/src/modules/debug/connectivity.ts +0 -61
- package/src/modules/debug/settlements.ts +0 -33
- package/src/modules/debug/stamps.ts +0 -76
- package/src/modules/debug/states.ts +0 -35
- package/src/modules/debug/status.ts +0 -35
- package/src/modules/debug/tag.ts +0 -19
- package/src/modules/debug/transactions.ts +0 -79
- package/src/modules/feed.ts +0 -105
- package/src/modules/pinning.ts +0 -74
- package/src/modules/pss.ts +0 -49
- package/src/modules/soc.ts +0 -40
- package/src/modules/stamps.ts +0 -59
- package/src/modules/status.ts +0 -13
- package/src/modules/stewardship.ts +0 -18
- package/src/modules/tag.ts +0 -83
- package/src/types/debug.ts +0 -204
- package/src/types/index.ts +0 -566
- package/src/types/ky-universal/common.d.ts +0 -17
- package/src/types/ky-universal/hooks.d.ts +0 -103
- package/src/types/ky-universal/index.d.ts +0 -125
- package/src/types/ky-universal/options.d.ts +0 -235
- package/src/types/ky-universal/response.d.ts +0 -38
- package/src/types/ky-universal/retry.d.ts +0 -39
- package/src/types/tar-js.d.ts +0 -1
- package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
- package/src/utils/bytes.ts +0 -144
- package/src/utils/collection.ts +0 -151
- package/src/utils/data.browser.ts +0 -68
- package/src/utils/data.ts +0 -48
- package/src/utils/error.ts +0 -23
- package/src/utils/eth.ts +0 -229
- package/src/utils/expose.ts +0 -48
- package/src/utils/file.ts +0 -40
- package/src/utils/hash.ts +0 -16
- package/src/utils/headers.ts +0 -63
- package/src/utils/hex.ts +0 -186
- package/src/utils/http.ts +0 -141
- package/src/utils/merge.ts +0 -34
- package/src/utils/pss.ts +0 -16
- package/src/utils/stream.ts +0 -146
- package/src/utils/tar.ts +0 -30
- package/src/utils/type.ts +0 -394
- package/src/utils/uint64.ts +0 -31
- package/src/utils/url.ts +0 -52
- package/test/bee.sh +0 -187
- package/test/data/1.txt +0 -1
- package/test/data/2.txt +0 -0
- package/test/data/empty +0 -0
- package/test/data/sub/3.txt +0 -1
- package/test/data/sub//303/251 +0 -0
- package/test/data/sub//360/237/230/216 +0 -0
- package/test/integration/bee-class.browser.spec.ts +0 -272
- package/test/integration/bee-class.spec.ts +0 -631
- package/test/integration/bee-debug-class.spec.ts +0 -85
- package/test/integration/chunk/bmt.spec.ts +0 -33
- package/test/integration/chunk/cac.spec.ts +0 -27
- package/test/integration/chunk/soc.spec.ts +0 -38
- package/test/integration/feed/index.spec.ts +0 -84
- package/test/integration/modules/bytes.spec.ts +0 -23
- package/test/integration/modules/bzz.spec.ts +0 -265
- package/test/integration/modules/chunk.spec.ts +0 -29
- package/test/integration/modules/debug/balance.spec.ts +0 -70
- package/test/integration/modules/debug/chequebook.spec.ts +0 -64
- package/test/integration/modules/debug/connectivity.spec.ts +0 -69
- package/test/integration/modules/debug/settlements.spec.ts +0 -34
- package/test/integration/modules/debug/states.spec.ts +0 -34
- package/test/integration/modules/debug/status.spec.ts +0 -65
- package/test/integration/modules/debug/tag.spec.ts +0 -27
- package/test/integration/modules/debug/transactions.spec.ts +0 -10
- package/test/integration/modules/feed.spec.ts +0 -61
- package/test/integration/modules/pinning.spec.ts +0 -155
- package/test/integration/modules/pss.spec.ts +0 -97
- package/test/integration/modules/status.spec.ts +0 -10
- package/test/integration/modules/stewardship.spec.ts +0 -32
- package/test/integration/modules/tag.spec.ts +0 -51
- package/test/test-type-sequencer.js +0 -36
- package/test/testpage/testpage.html +0 -20
- package/test/tests-setup.ts +0 -94
- package/test/unit/assertions.ts +0 -460
- package/test/unit/bee-class.spec.ts +0 -976
- package/test/unit/bee-debug-class.spec.ts +0 -435
- package/test/unit/chunk/bmt.spec.ts +0 -16
- package/test/unit/chunk/cac.spec.ts +0 -29
- package/test/unit/chunk/serialize.spec.ts +0 -24
- package/test/unit/chunk/signer.spec.ts +0 -140
- package/test/unit/chunk/soc.spec.ts +0 -25
- package/test/unit/chunk/span.spec.ts +0 -38
- package/test/unit/feed/json.spec.ts +0 -73
- package/test/unit/nock.ts +0 -151
- package/test/unit/utils/bytes.spec.ts +0 -32
- package/test/unit/utils/collection.spec.ts +0 -22
- package/test/unit/utils/eth.spec.ts +0 -224
- package/test/unit/utils/hex.spec.ts +0 -115
- package/test/unit/utils/stream.spec.ts +0 -30
- package/test/unit/utils/type.spec.ts +0 -32
- package/test/unit/utils/uint64.spec.ts +0 -102
- package/test/utils.ts +0 -328
- package/tsconfig.json +0 -18
- package/tsconfig.test.json +0 -11
- package/typedoc.json +0 -7
- package/webpack.config.ts +0 -141
package/src/utils/collection.ts
DELETED
|
@@ -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,68 +0,0 @@
|
|
|
1
|
-
import { isNodeReadable, isReadableStream } from './stream'
|
|
2
|
-
import { Readable } from '../types'
|
|
3
|
-
import Blob from 'cross-blob'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Validates input and converts to Uint8Array
|
|
7
|
-
*
|
|
8
|
-
* @param data any string, ArrayBuffer or Uint8Array
|
|
9
|
-
*/
|
|
10
|
-
export async function prepareData(
|
|
11
|
-
data: string | Blob | ArrayBuffer | Uint8Array | Readable,
|
|
12
|
-
): Promise<Blob | ReadableStream<Uint8Array> | never> {
|
|
13
|
-
if (typeof data === 'string') return new Blob([data], { type: 'text/plain' })
|
|
14
|
-
|
|
15
|
-
if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
|
|
16
|
-
return new Blob([data], { type: 'application/octet-stream' })
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (data instanceof Blob) {
|
|
20
|
-
return data
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Currently it is not possible to stream requests from browsers
|
|
24
|
-
// there are already first experiments on this field (Chromium)
|
|
25
|
-
// but till it is fully implemented across browsers-land we have to
|
|
26
|
-
// buffer the data before sending the requests.
|
|
27
|
-
if (isNodeReadable(data)) {
|
|
28
|
-
return new Promise(resolve => {
|
|
29
|
-
const buffers: Array<Uint8Array> = []
|
|
30
|
-
data.on('data', d => {
|
|
31
|
-
buffers.push(d)
|
|
32
|
-
})
|
|
33
|
-
data.on('end', () => {
|
|
34
|
-
resolve(new Blob(buffers, { type: 'application/octet-stream' }))
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (isReadableStream(data)) {
|
|
40
|
-
return new Promise(async resolve => {
|
|
41
|
-
const reader = data.getReader()
|
|
42
|
-
const buffers: Array<Uint8Array> = []
|
|
43
|
-
|
|
44
|
-
let done, value
|
|
45
|
-
do {
|
|
46
|
-
;({ done, value } = await reader.read())
|
|
47
|
-
|
|
48
|
-
if (!done) {
|
|
49
|
-
buffers.push(value)
|
|
50
|
-
}
|
|
51
|
-
} while (!done)
|
|
52
|
-
|
|
53
|
-
resolve(new Blob(buffers, { type: 'application/octet-stream' }))
|
|
54
|
-
})
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
throw new TypeError('unknown data type')
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export async function prepareWebsocketData(data: string | ArrayBuffer | Blob): Promise<Uint8Array> | never {
|
|
61
|
-
if (typeof data === 'string') return new TextEncoder().encode(data)
|
|
62
|
-
|
|
63
|
-
if (data instanceof ArrayBuffer) return new Uint8Array(data)
|
|
64
|
-
|
|
65
|
-
if (data instanceof Blob) return new Uint8Array(await new Response(data as Blob).arrayBuffer())
|
|
66
|
-
|
|
67
|
-
throw new TypeError('unknown websocket data type')
|
|
68
|
-
}
|
package/src/utils/data.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { Data } from 'ws'
|
|
2
|
-
import Blob from 'cross-blob'
|
|
3
|
-
import { isNodeReadable, isReadableStream, readableWebToNode } from './stream'
|
|
4
|
-
import { Readable } from '../types'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Prepare data for valid input for node-fetch.
|
|
8
|
-
*
|
|
9
|
-
* node-fetch is not using WHATWG ReadableStream but NodeJS Readable so we need to convert in case of ReadableStream,
|
|
10
|
-
* but the typings are set to use ReadableStream so hence why type conversion here.
|
|
11
|
-
*
|
|
12
|
-
* @param data any string, ArrayBuffer, Uint8Array or Readable
|
|
13
|
-
*/
|
|
14
|
-
export async function prepareData(
|
|
15
|
-
data: string | ArrayBuffer | Uint8Array | Readable,
|
|
16
|
-
): Promise<Blob | ReadableStream<Uint8Array> | never> {
|
|
17
|
-
if (typeof data === 'string') return new Blob([data], { type: 'text/plain' })
|
|
18
|
-
|
|
19
|
-
if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
|
|
20
|
-
return new Blob([data], { type: 'application/octet-stream' })
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (data instanceof Blob || isNodeReadable(data)) return data as ReadableStream<Uint8Array>
|
|
24
|
-
|
|
25
|
-
if (isReadableStream(data)) {
|
|
26
|
-
return readableWebToNode(data) as unknown as ReadableStream<Uint8Array>
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
throw new TypeError('unknown data type')
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function isBufferArray(buffer: unknown): buffer is Buffer[] {
|
|
33
|
-
return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export async function prepareWebsocketData(data: Data | Blob): Promise<Uint8Array> | never {
|
|
37
|
-
if (typeof data === 'string') return new TextEncoder().encode(data)
|
|
38
|
-
|
|
39
|
-
if (data instanceof Buffer) return new Uint8Array(data)
|
|
40
|
-
|
|
41
|
-
if (data instanceof ArrayBuffer) return new Uint8Array(data)
|
|
42
|
-
|
|
43
|
-
if (data instanceof Blob) return new Uint8Array(await new Response(data as Blob).arrayBuffer())
|
|
44
|
-
|
|
45
|
-
if (isBufferArray(data)) return new Uint8Array(Buffer.concat(data))
|
|
46
|
-
|
|
47
|
-
throw new TypeError('unknown websocket data type')
|
|
48
|
-
}
|
package/src/utils/error.ts
DELETED
|
@@ -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
|
-
}
|
package/src/utils/expose.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
export { getCollectionSize, getFolderSize } from './collection'
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
Bytes,
|
|
5
|
-
FlexBytes,
|
|
6
|
-
isBytes,
|
|
7
|
-
assertBytes,
|
|
8
|
-
isFlexBytes,
|
|
9
|
-
assertFlexBytes,
|
|
10
|
-
bytesAtOffset,
|
|
11
|
-
flexBytesAtOffset,
|
|
12
|
-
bytesEqual,
|
|
13
|
-
} from './bytes'
|
|
14
|
-
|
|
15
|
-
export {
|
|
16
|
-
HexString,
|
|
17
|
-
PrefixedHexString,
|
|
18
|
-
makeHexString,
|
|
19
|
-
hexToBytes,
|
|
20
|
-
bytesToHex,
|
|
21
|
-
intToHex,
|
|
22
|
-
isHexString,
|
|
23
|
-
assertHexString,
|
|
24
|
-
assertPrefixedHexString,
|
|
25
|
-
} from './hex'
|
|
26
|
-
|
|
27
|
-
export {
|
|
28
|
-
EthAddress,
|
|
29
|
-
makeEthAddress,
|
|
30
|
-
makeHexEthAddress,
|
|
31
|
-
isHexEthAddress,
|
|
32
|
-
ethToSwarmAddress,
|
|
33
|
-
toLittleEndian,
|
|
34
|
-
fromLittleEndian,
|
|
35
|
-
makeEthereumWalletSigner,
|
|
36
|
-
} from './eth'
|
|
37
|
-
|
|
38
|
-
export {
|
|
39
|
-
readableWebToNode,
|
|
40
|
-
readableNodeToWeb,
|
|
41
|
-
isReadableStream,
|
|
42
|
-
isNodeReadable,
|
|
43
|
-
normalizeToReadableStream,
|
|
44
|
-
isReadable,
|
|
45
|
-
} from './stream'
|
|
46
|
-
|
|
47
|
-
export { keccak256Hash } from './hash'
|
|
48
|
-
export { makeMaxTarget } from './pss'
|
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
|
-
}
|
package/src/utils/headers.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { BatchId, FileHeaders, 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 | null): 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 | null): number | undefined {
|
|
28
|
-
if (!header) {
|
|
29
|
-
return undefined
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return parseInt(header, 10)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function readFileHeaders(headers: Headers): FileHeaders {
|
|
36
|
-
const name = readContentDispositionFilename(headers.get('content-disposition'))
|
|
37
|
-
const tagUid = readTagUid(headers.get('swarm-tag-uid'))
|
|
38
|
-
const contentType = headers.get('content-type') || undefined
|
|
39
|
-
|
|
40
|
-
return {
|
|
41
|
-
name,
|
|
42
|
-
tagUid,
|
|
43
|
-
contentType,
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function extractUploadHeaders(postageBatchId: BatchId, options?: UploadOptions): Record<string, string> {
|
|
48
|
-
if (!postageBatchId) {
|
|
49
|
-
throw new BeeError('Postage BatchID has to be specified!')
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const headers: Record<string, string> = {
|
|
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
|
-
}
|