@ethersphere/bee-js 3.3.0 → 3.3.1
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/chunk/bmt.js +7 -2
- package/dist/cjs/chunk/signer.js +9 -4
- package/dist/cjs/modules/debug/status.js +5 -5
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/ky-options.js +8 -0
- package/dist/cjs/types/ky-universal/common.js +8 -0
- package/dist/cjs/types/ky-universal/hooks.js +8 -0
- package/dist/cjs/types/ky-universal/retry.js +8 -0
- package/dist/cjs/utils/eth.js +8 -3
- package/dist/cjs/utils/hash.js +7 -2
- package/dist/cjs/utils/stream.js +14 -5
- package/dist/index.browser.min.js +3 -0
- package/dist/index.browser.min.js.LICENSE.txt +52 -0
- package/dist/index.browser.min.js.map +1 -0
- package/dist/mjs/bee-debug.js +608 -508
- package/dist/mjs/bee.js +935 -836
- package/dist/mjs/chunk/bmt.js +34 -24
- package/dist/mjs/chunk/cac.js +24 -22
- package/dist/mjs/chunk/serialize.js +9 -9
- package/dist/mjs/chunk/signer.js +92 -73
- package/dist/mjs/chunk/soc.js +78 -66
- package/dist/mjs/chunk/span.js +19 -16
- package/dist/mjs/feed/index.js +110 -101
- package/dist/mjs/feed/json.js +21 -17
- package/dist/mjs/feed/topic.js +18 -17
- package/dist/mjs/feed/type.js +5 -5
- package/dist/mjs/index.js +7 -7
- package/dist/mjs/modules/bytes.js +33 -30
- package/dist/mjs/modules/bzz.js +80 -77
- package/dist/mjs/modules/chunk.js +22 -20
- package/dist/mjs/modules/debug/balance.js +26 -22
- package/dist/mjs/modules/debug/chequebook.js +84 -65
- package/dist/mjs/modules/debug/chunk.js +15 -13
- package/dist/mjs/modules/debug/connectivity.js +34 -34
- package/dist/mjs/modules/debug/settlements.js +14 -12
- package/dist/mjs/modules/debug/stamps.js +52 -47
- package/dist/mjs/modules/debug/states.js +16 -14
- package/dist/mjs/modules/debug/status.js +55 -35
- package/dist/mjs/modules/debug/tag.js +8 -7
- package/dist/mjs/modules/debug/transactions.js +31 -25
- package/dist/mjs/modules/feed.js +39 -33
- package/dist/mjs/modules/pinning.js +34 -28
- package/dist/mjs/modules/pss.js +18 -14
- package/dist/mjs/modules/soc.js +18 -15
- package/dist/mjs/modules/status.js +6 -5
- package/dist/mjs/modules/stewardship.js +13 -12
- package/dist/mjs/modules/tag.js +38 -30
- package/dist/mjs/package.json +1 -0
- package/dist/mjs/types/debug.js +5 -4
- package/dist/mjs/types/index.js +6 -2
- package/dist/mjs/types/ky-options.js +7 -0
- package/dist/mjs/types/ky-universal/common.js +7 -0
- package/dist/mjs/types/ky-universal/hooks.js +7 -0
- package/dist/mjs/types/ky-universal/retry.js +7 -0
- package/dist/mjs/utils/bytes.js +32 -26
- package/dist/mjs/utils/collection.browser.js +4 -3
- package/dist/mjs/utils/collection.js +47 -35
- package/dist/mjs/utils/collection.node.js +53 -42
- package/dist/mjs/utils/data.browser.js +64 -48
- package/dist/mjs/utils/data.js +29 -25
- package/dist/mjs/utils/error.js +47 -39
- package/dist/mjs/utils/eth.js +119 -104
- package/dist/mjs/utils/expose.js +9 -9
- package/dist/mjs/utils/file.js +22 -19
- package/dist/mjs/utils/hash.js +10 -5
- package/dist/mjs/utils/headers.js +43 -39
- package/dist/mjs/utils/hex.js +78 -63
- package/dist/mjs/utils/http.js +119 -100
- package/dist/mjs/utils/merge.js +26 -20
- package/dist/mjs/utils/pss.js +8 -6
- package/dist/mjs/utils/stamps.js +7 -3
- package/dist/mjs/utils/stream.js +107 -82
- package/dist/mjs/utils/tar.js +17 -14
- package/dist/mjs/utils/type.js +249 -209
- package/dist/mjs/utils/uint64.js +16 -16
- package/dist/mjs/utils/url.js +32 -25
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/types/ky-options.d.ts +221 -0
- package/dist/types/types/ky-universal/common.d.ts +13 -0
- package/dist/types/types/ky-universal/hooks.d.ts +92 -0
- package/dist/types/types/ky-universal/retry.d.ts +38 -0
- package/dist/types/utils/stream.d.ts +11 -8
- package/package.json +9 -6
- package/dist/index.js +0 -28326
- package/dist/index.js.map +0 -1
|
@@ -1,57 +1,73 @@
|
|
|
1
|
-
import { isNodeReadable, isReadableStream } from
|
|
1
|
+
import { isNodeReadable, isReadableStream } from "./stream.js";
|
|
2
2
|
import Blob from 'cross-blob';
|
|
3
3
|
/**
|
|
4
4
|
* Validates input and converts to Uint8Array
|
|
5
5
|
*
|
|
6
6
|
* @param data any string, ArrayBuffer or Uint8Array
|
|
7
7
|
*/
|
|
8
|
+
|
|
8
9
|
export async function prepareData(data) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
10
|
+
if (typeof data === 'string') return new Blob([data], {
|
|
11
|
+
type: 'text/plain'
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
|
|
15
|
+
return new Blob([data], {
|
|
16
|
+
type: 'application/octet-stream'
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (data instanceof Blob) {
|
|
21
|
+
return data;
|
|
22
|
+
} // Currently it is not possible to stream requests from browsers
|
|
23
|
+
// there are already first experiments on this field (Chromium)
|
|
24
|
+
// but till it is fully implemented across browsers-land we have to
|
|
25
|
+
// buffer the data before sending the requests.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if (isNodeReadable(data)) {
|
|
29
|
+
return new Promise(resolve => {
|
|
30
|
+
const buffers = [];
|
|
31
|
+
data.on('data', d => {
|
|
32
|
+
buffers.push(d);
|
|
33
|
+
});
|
|
34
|
+
data.on('end', () => {
|
|
35
|
+
resolve(new Blob(buffers, {
|
|
36
|
+
type: 'application/octet-stream'
|
|
37
|
+
}));
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (isReadableStream(data)) {
|
|
43
|
+
return new Promise(async resolve => {
|
|
44
|
+
const reader = data.getReader();
|
|
45
|
+
const buffers = [];
|
|
46
|
+
let done, value;
|
|
47
|
+
|
|
48
|
+
do {
|
|
49
|
+
;
|
|
50
|
+
({
|
|
51
|
+
done,
|
|
52
|
+
value
|
|
53
|
+
} = await reader.read());
|
|
54
|
+
|
|
55
|
+
if (!done) {
|
|
56
|
+
buffers.push(value);
|
|
57
|
+
}
|
|
58
|
+
} while (!done);
|
|
59
|
+
|
|
60
|
+
resolve(new Blob(buffers, {
|
|
61
|
+
type: 'application/octet-stream'
|
|
62
|
+
}));
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
throw new TypeError('unknown data type');
|
|
48
67
|
}
|
|
49
68
|
export async function prepareWebsocketData(data) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return new Uint8Array(await new Response(data).arrayBuffer());
|
|
56
|
-
throw new TypeError('unknown websocket data type');
|
|
57
|
-
}
|
|
69
|
+
if (typeof data === 'string') return new TextEncoder().encode(data);
|
|
70
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data);
|
|
71
|
+
if (data instanceof Blob) return new Uint8Array(await new Response(data).arrayBuffer());
|
|
72
|
+
throw new TypeError('unknown websocket data type');
|
|
73
|
+
}
|
package/dist/mjs/utils/data.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Blob from 'cross-blob';
|
|
2
|
-
import { isNodeReadable, isReadableStream, readableWebToNode } from
|
|
2
|
+
import { isNodeReadable, isReadableStream, readableWebToNode } from "./stream.js";
|
|
3
3
|
/**
|
|
4
4
|
* Prepare data for valid input for node-fetch.
|
|
5
5
|
*
|
|
@@ -8,32 +8,36 @@ import { isNodeReadable, isReadableStream, readableWebToNode } from './stream';
|
|
|
8
8
|
*
|
|
9
9
|
* @param data any string, ArrayBuffer, Uint8Array or Readable
|
|
10
10
|
*/
|
|
11
|
+
|
|
11
12
|
export async function prepareData(data) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
if (typeof data === 'string') return new Blob([data], {
|
|
14
|
+
type: 'text/plain'
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
|
|
18
|
+
return new Blob([data], {
|
|
19
|
+
type: 'application/octet-stream'
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (data instanceof Blob || isNodeReadable(data)) return data;
|
|
24
|
+
|
|
25
|
+
if (isReadableStream(data)) {
|
|
26
|
+
return readableWebToNode(data);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
throw new TypeError('unknown data type');
|
|
23
30
|
}
|
|
31
|
+
|
|
24
32
|
function isBufferArray(buffer) {
|
|
25
|
-
|
|
33
|
+
return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer);
|
|
26
34
|
}
|
|
35
|
+
|
|
27
36
|
export async function prepareWebsocketData(data) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return new Uint8Array(await new Response(data).arrayBuffer());
|
|
36
|
-
if (isBufferArray(data))
|
|
37
|
-
return new Uint8Array(Buffer.concat(data));
|
|
38
|
-
throw new TypeError('unknown websocket data type');
|
|
39
|
-
}
|
|
37
|
+
if (typeof data === 'string') return new TextEncoder().encode(data);
|
|
38
|
+
if (data instanceof Buffer) return new Uint8Array(data);
|
|
39
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data);
|
|
40
|
+
if (data instanceof Blob) return new Uint8Array(await new Response(data).arrayBuffer());
|
|
41
|
+
if (isBufferArray(data)) return new Uint8Array(Buffer.concat(data));
|
|
42
|
+
throw new TypeError('unknown websocket data type');
|
|
43
|
+
}
|
package/dist/mjs/utils/error.js
CHANGED
|
@@ -1,48 +1,56 @@
|
|
|
1
1
|
export class BeeError extends Error {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
}
|
|
5
|
+
|
|
5
6
|
}
|
|
6
7
|
export class BeeArgumentError extends BeeError {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
value;
|
|
9
|
+
|
|
10
|
+
constructor(message, value) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.value = value;
|
|
13
|
+
}
|
|
14
|
+
|
|
12
15
|
}
|
|
13
16
|
export class BeeRequestError extends BeeError {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
requestOptions;
|
|
18
|
+
/**
|
|
19
|
+
* @param message
|
|
20
|
+
* @param requestOptions KyOptions that were used to assemble the request. THIS MIGHT NOT BE COMPLETE! If custom Ky instance was used that has set defaults then these defaults are not visible in this object!
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
constructor(message, requestOptions) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.requestOptions = requestOptions;
|
|
26
|
+
}
|
|
27
|
+
|
|
23
28
|
}
|
|
24
29
|
export class BeeResponseError extends BeeError {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
30
|
+
status;
|
|
31
|
+
response;
|
|
32
|
+
responseBody;
|
|
33
|
+
requestOptions;
|
|
34
|
+
/**
|
|
35
|
+
* @param status HTTP status code number
|
|
36
|
+
* @param response Response returned from the server
|
|
37
|
+
* @param responseBody Response body as string which is returned from response.text() call
|
|
38
|
+
* @param requestOptions KyOptions that were used to assemble the request. THIS MIGHT NOT BE COMPLETE! If custom Ky instance was used that has set defaults then these defaults are not visible in this object!
|
|
39
|
+
* @param message
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
constructor(status, response, responseBody, requestOptions, message) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.status = status;
|
|
45
|
+
this.response = response;
|
|
46
|
+
this.responseBody = responseBody;
|
|
47
|
+
this.requestOptions = requestOptions;
|
|
48
|
+
}
|
|
49
|
+
|
|
43
50
|
}
|
|
44
51
|
export class BeeNotAJsonError extends BeeError {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
constructor() {
|
|
53
|
+
super(`Received response is not valid JSON.`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|
package/dist/mjs/utils/eth.js
CHANGED
|
@@ -1,67 +1,76 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
// For ESM compatibility
|
|
2
|
+
import pkg from 'js-sha3';
|
|
3
|
+
const {
|
|
4
|
+
keccak256,
|
|
5
|
+
sha3_256
|
|
6
|
+
} = pkg;
|
|
7
|
+
import { hexToBytes, intToHex, makeHexString, assertHexString } from "./hex.js";
|
|
8
|
+
import { assertBytes } from "./bytes.js";
|
|
4
9
|
const ETH_ADDR_BYTES_LENGTH = 20;
|
|
5
10
|
const ETH_ADDR_HEX_LENGTH = 40;
|
|
6
11
|
export function makeEthAddress(address) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
if (typeof address === 'string') {
|
|
13
|
+
const hexAddr = makeHexString(address, ETH_ADDR_HEX_LENGTH);
|
|
14
|
+
const ownerBytes = hexToBytes(hexAddr);
|
|
15
|
+
assertBytes(ownerBytes, ETH_ADDR_BYTES_LENGTH);
|
|
16
|
+
return ownerBytes;
|
|
17
|
+
} else if (address instanceof Uint8Array) {
|
|
18
|
+
assertBytes(address, ETH_ADDR_BYTES_LENGTH);
|
|
19
|
+
return address;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
throw new TypeError('Invalid EthAddress');
|
|
18
23
|
}
|
|
19
24
|
export function makeHexEthAddress(address) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
e.message = `Invalid HexEthAddress: ${e.message}`;
|
|
26
|
-
}
|
|
27
|
-
throw e;
|
|
25
|
+
try {
|
|
26
|
+
return makeHexString(address, ETH_ADDR_HEX_LENGTH);
|
|
27
|
+
} catch (e) {
|
|
28
|
+
if (e instanceof TypeError) {
|
|
29
|
+
e.message = `Invalid HexEthAddress: ${e.message}`;
|
|
28
30
|
}
|
|
31
|
+
|
|
32
|
+
throw e;
|
|
33
|
+
}
|
|
29
34
|
}
|
|
30
35
|
/**
|
|
31
36
|
* Check if this is all caps or small caps eth address (=address without checksum)
|
|
32
37
|
*
|
|
33
38
|
* @param address Ethereum address as hex string
|
|
34
39
|
*/
|
|
40
|
+
|
|
35
41
|
function isEthAddrCaseIns(address) {
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
// Check it's string, all small caps or all all caps hex and 40 chars long without the `0x` prefix
|
|
43
|
+
return typeof address === 'string' && (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address));
|
|
38
44
|
}
|
|
39
45
|
/**
|
|
40
46
|
* Check if this is checksummed ethereum address
|
|
41
47
|
*
|
|
42
48
|
* @param address Ethereum address as hex string
|
|
43
49
|
*/
|
|
50
|
+
|
|
51
|
+
|
|
44
52
|
function isValidChecksummedEthAddress(address) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
return true;
|
|
53
|
+
try {
|
|
54
|
+
// Check for valid case insensitive hex type string, 40 chars
|
|
55
|
+
const addr = makeHexString(address, ETH_ADDR_HEX_LENGTH); // Check the checksum
|
|
56
|
+
|
|
57
|
+
const addressHash = keccak256(addr.toLowerCase());
|
|
58
|
+
|
|
59
|
+
for (let i = 0; i < 40; i += 1) {
|
|
60
|
+
// the nth letter should be uppercase if the nth digit of casemap is 1
|
|
61
|
+
if (parseInt(addressHash[i], 16) > 7 && addr[i].toUpperCase() !== addr[i] || parseInt(addressHash[i], 16) <= 7 && addr[i].toLowerCase() !== addr[i]) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
58
64
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
|
|
66
|
+
return true;
|
|
67
|
+
} catch (e) {
|
|
68
|
+
if (e instanceof TypeError) {
|
|
69
|
+
return false;
|
|
64
70
|
}
|
|
71
|
+
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
65
74
|
}
|
|
66
75
|
/**
|
|
67
76
|
* Check if is valid ethereum address
|
|
@@ -73,8 +82,10 @@ function isValidChecksummedEthAddress(address) {
|
|
|
73
82
|
*
|
|
74
83
|
* @return True if is valid eth address
|
|
75
84
|
*/
|
|
85
|
+
|
|
86
|
+
|
|
76
87
|
export function isHexEthAddress(address) {
|
|
77
|
-
|
|
88
|
+
return isEthAddrCaseIns(address) || isValidChecksummedEthAddress(address);
|
|
78
89
|
}
|
|
79
90
|
/**
|
|
80
91
|
* Convert big-endian hex or number to little-endian.
|
|
@@ -85,26 +96,21 @@ export function isHexEthAddress(address) {
|
|
|
85
96
|
*
|
|
86
97
|
* @return little-endian encoded hexstring
|
|
87
98
|
*/
|
|
99
|
+
|
|
88
100
|
export function toLittleEndian(bigEndian, pad = 2) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
hexRep = hexRep.padStart(hexRep.length + 1, '0');
|
|
103
|
-
// Match all two pairs in the hexstring, reverse the pairs and join it again
|
|
104
|
-
const littleEndian = hexRep.match(/../g)?.reverse().join('');
|
|
105
|
-
if (littleEndian)
|
|
106
|
-
return littleEndian;
|
|
107
|
-
throw new Error('failed to convert');
|
|
101
|
+
if (!(Number.isInteger(pad) && pad >= 2 && pad % 2 === 0)) {
|
|
102
|
+
throw new TypeError('minimal padding for conversion needs to be positive even integer');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let hexRep;
|
|
106
|
+
if (typeof bigEndian === 'string') hexRep = makeHexString(bigEndian);else if (typeof bigEndian === 'number') hexRep = intToHex(bigEndian);else throw new TypeError('incorrect input type');
|
|
107
|
+
hexRep = hexRep.padStart(pad, '0'); // Extend to an even length hexstring
|
|
108
|
+
|
|
109
|
+
if (hexRep.length % 2 !== 0) hexRep = hexRep.padStart(hexRep.length + 1, '0'); // Match all two pairs in the hexstring, reverse the pairs and join it again
|
|
110
|
+
|
|
111
|
+
const littleEndian = hexRep.match(/../g)?.reverse().join('');
|
|
112
|
+
if (littleEndian) return littleEndian;
|
|
113
|
+
throw new Error('failed to convert');
|
|
108
114
|
}
|
|
109
115
|
/**
|
|
110
116
|
* Convert little-endian hex or number to big-endian
|
|
@@ -115,18 +121,20 @@ export function toLittleEndian(bigEndian, pad = 2) {
|
|
|
115
121
|
*
|
|
116
122
|
* @return big-endian encoded hexstring
|
|
117
123
|
*/
|
|
124
|
+
|
|
118
125
|
export function fromLittleEndian(littleEndian, pad = 2) {
|
|
119
|
-
|
|
120
|
-
|
|
126
|
+
// It's a reversible function
|
|
127
|
+
return toLittleEndian(littleEndian, pad);
|
|
121
128
|
}
|
|
129
|
+
|
|
122
130
|
function assertEthAddress(ethAddress) {
|
|
123
|
-
|
|
124
|
-
throw new TypeError('invalid ETH address');
|
|
131
|
+
if (!isHexEthAddress(ethAddress)) throw new TypeError('invalid ETH address');
|
|
125
132
|
}
|
|
133
|
+
|
|
126
134
|
function assertSwarmNetworkId(networkId) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
135
|
+
if (Number.isInteger(networkId && networkId > 0 && networkId < Number.MAX_SAFE_INTEGER)) {
|
|
136
|
+
throw new TypeError('swarm network id must be positive integer');
|
|
137
|
+
}
|
|
130
138
|
}
|
|
131
139
|
/**
|
|
132
140
|
* Get swarm overlay address from public ethereum address and swarm network id
|
|
@@ -136,13 +144,15 @@ function assertSwarmNetworkId(networkId) {
|
|
|
136
144
|
*
|
|
137
145
|
* @return Swarm overlay address
|
|
138
146
|
*/
|
|
147
|
+
|
|
148
|
+
|
|
139
149
|
export function ethToSwarmAddress(ethAddress, networkId = 1) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
150
|
+
assertEthAddress(ethAddress);
|
|
151
|
+
assertSwarmNetworkId(networkId);
|
|
152
|
+
const hex = `${makeHexString(ethAddress)}${toLittleEndian(networkId, 16)}`;
|
|
153
|
+
assertHexString(hex);
|
|
154
|
+
const overlayAddress = sha3_256(hexToBytes(hex));
|
|
155
|
+
return overlayAddress;
|
|
146
156
|
}
|
|
147
157
|
/**
|
|
148
158
|
* Function that takes Ethereum EIP-1193 compatible provider and create an Signer instance that
|
|
@@ -151,34 +161,39 @@ export function ethToSwarmAddress(ethAddress, networkId = 1) {
|
|
|
151
161
|
* @param provider Injected web3 provider like window.ethereum or other compatible with EIP-1193
|
|
152
162
|
* @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.
|
|
153
163
|
*/
|
|
164
|
+
|
|
154
165
|
export async function makeEthereumWalletSigner(provider, ethAddress) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
166
|
+
let executorFnc;
|
|
167
|
+
|
|
168
|
+
if (typeof provider !== 'object' || provider === null) {
|
|
169
|
+
throw new TypeError('We need JsonRPC provider object!');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (provider.request) {
|
|
173
|
+
executorFnc = provider.request;
|
|
174
|
+
} else if (provider.sendAsync) {
|
|
175
|
+
executorFnc = provider.sendAsync;
|
|
176
|
+
} else {
|
|
177
|
+
throw new Error('Incompatible interface of given provider!');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (!ethAddress) {
|
|
181
|
+
ethAddress = (await executorFnc({
|
|
182
|
+
method: 'eth_requestAccounts'
|
|
183
|
+
}))[0];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const bytesEthAddress = makeEthAddress(ethAddress);
|
|
187
|
+
const hexEthAddress = makeHexEthAddress(ethAddress);
|
|
188
|
+
return {
|
|
189
|
+
address: bytesEthAddress,
|
|
190
|
+
sign: async data => {
|
|
191
|
+
const result = await executorFnc({
|
|
192
|
+
jsonrpc: '2.0',
|
|
193
|
+
method: 'personal_sign',
|
|
194
|
+
params: ['0x' + data.hex(), '0x' + hexEthAddress]
|
|
195
|
+
});
|
|
196
|
+
return result;
|
|
158
197
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
else if (provider.sendAsync) {
|
|
163
|
-
executorFnc = provider.sendAsync;
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
throw new Error('Incompatible interface of given provider!');
|
|
167
|
-
}
|
|
168
|
-
if (!ethAddress) {
|
|
169
|
-
ethAddress = (await executorFnc({ method: 'eth_requestAccounts' }))[0];
|
|
170
|
-
}
|
|
171
|
-
const bytesEthAddress = makeEthAddress(ethAddress);
|
|
172
|
-
const hexEthAddress = makeHexEthAddress(ethAddress);
|
|
173
|
-
return {
|
|
174
|
-
address: bytesEthAddress,
|
|
175
|
-
sign: async (data) => {
|
|
176
|
-
const result = await executorFnc({
|
|
177
|
-
jsonrpc: '2.0',
|
|
178
|
-
method: 'personal_sign',
|
|
179
|
-
params: ['0x' + data.hex(), '0x' + hexEthAddress],
|
|
180
|
-
});
|
|
181
|
-
return result;
|
|
182
|
-
},
|
|
183
|
-
};
|
|
184
|
-
}
|
|
198
|
+
};
|
|
199
|
+
}
|
package/dist/mjs/utils/expose.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { getCollectionSize } from
|
|
2
|
-
export { getFolderSize } from
|
|
3
|
-
export { isBytes, assertBytes, isFlexBytes, assertFlexBytes, bytesAtOffset, flexBytesAtOffset, bytesEqual
|
|
4
|
-
export { makeHexString, hexToBytes, bytesToHex, intToHex, isHexString, assertHexString, assertPrefixedHexString
|
|
5
|
-
export { makeEthAddress, makeHexEthAddress, isHexEthAddress, ethToSwarmAddress, toLittleEndian, fromLittleEndian, makeEthereumWalletSigner
|
|
6
|
-
export { readableWebToNode, readableNodeToWeb, isReadableStream, isNodeReadable, normalizeToReadableStream, isReadable
|
|
7
|
-
export { keccak256Hash } from
|
|
8
|
-
export { makeMaxTarget } from
|
|
9
|
-
export { getStampUsage } from
|
|
1
|
+
export { getCollectionSize } from "./collection.js";
|
|
2
|
+
export { getFolderSize } from "./collection.node.js";
|
|
3
|
+
export { isBytes, assertBytes, isFlexBytes, assertFlexBytes, bytesAtOffset, flexBytesAtOffset, bytesEqual } from "./bytes.js";
|
|
4
|
+
export { makeHexString, hexToBytes, bytesToHex, intToHex, isHexString, assertHexString, assertPrefixedHexString } from "./hex.js";
|
|
5
|
+
export { makeEthAddress, makeHexEthAddress, isHexEthAddress, ethToSwarmAddress, toLittleEndian, fromLittleEndian, makeEthereumWalletSigner } from "./eth.js";
|
|
6
|
+
export { readableWebToNode, readableNodeToWeb, isReadableStream, isNodeReadable, normalizeToReadableStream, isReadable } from "./stream.js";
|
|
7
|
+
export { keccak256Hash } from "./hash.js";
|
|
8
|
+
export { makeMaxTarget } from "./pss.js";
|
|
9
|
+
export { getStampUsage } from "./stamps.js";
|
package/dist/mjs/utils/file.js
CHANGED
|
@@ -4,15 +4,14 @@
|
|
|
4
4
|
* https://developer.mozilla.org/en-US/docs/Web/API/File
|
|
5
5
|
*/
|
|
6
6
|
export function isFile(file) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
(typeof f.stream === 'function' || typeof f.arrayBuffer === 'function'));
|
|
7
|
+
// browser
|
|
8
|
+
if (typeof File === 'function') {
|
|
9
|
+
return file instanceof File;
|
|
10
|
+
} // node.js
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const f = file;
|
|
14
|
+
return typeof f === 'object' && typeof f.name === 'string' && (typeof f.stream === 'function' || typeof f.arrayBuffer === 'function');
|
|
16
15
|
}
|
|
17
16
|
/**
|
|
18
17
|
* Compatibility helper for browsers where the `arrayBuffer function is
|
|
@@ -20,14 +19,18 @@ export function isFile(file) {
|
|
|
20
19
|
*
|
|
21
20
|
* @param file A File object
|
|
22
21
|
*/
|
|
22
|
+
|
|
23
23
|
export async function fileArrayBuffer(file) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
if (file.arrayBuffer) {
|
|
25
|
+
return file.arrayBuffer();
|
|
26
|
+
} // workaround for Safari where arrayBuffer is not supported on Files
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
return new Promise(resolve => {
|
|
30
|
+
const fr = new FileReader();
|
|
31
|
+
|
|
32
|
+
fr.onload = () => resolve(fr.result);
|
|
33
|
+
|
|
34
|
+
fr.readAsArrayBuffer(file);
|
|
35
|
+
});
|
|
36
|
+
}
|