@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.
Files changed (85) hide show
  1. package/dist/cjs/chunk/bmt.js +7 -2
  2. package/dist/cjs/chunk/signer.js +9 -4
  3. package/dist/cjs/modules/debug/status.js +5 -5
  4. package/dist/cjs/package.json +1 -0
  5. package/dist/cjs/types/ky-options.js +8 -0
  6. package/dist/cjs/types/ky-universal/common.js +8 -0
  7. package/dist/cjs/types/ky-universal/hooks.js +8 -0
  8. package/dist/cjs/types/ky-universal/retry.js +8 -0
  9. package/dist/cjs/utils/eth.js +8 -3
  10. package/dist/cjs/utils/hash.js +7 -2
  11. package/dist/cjs/utils/stream.js +14 -5
  12. package/dist/index.browser.min.js +3 -0
  13. package/dist/index.browser.min.js.LICENSE.txt +52 -0
  14. package/dist/index.browser.min.js.map +1 -0
  15. package/dist/mjs/bee-debug.js +608 -508
  16. package/dist/mjs/bee.js +935 -836
  17. package/dist/mjs/chunk/bmt.js +34 -24
  18. package/dist/mjs/chunk/cac.js +24 -22
  19. package/dist/mjs/chunk/serialize.js +9 -9
  20. package/dist/mjs/chunk/signer.js +92 -73
  21. package/dist/mjs/chunk/soc.js +78 -66
  22. package/dist/mjs/chunk/span.js +19 -16
  23. package/dist/mjs/feed/index.js +110 -101
  24. package/dist/mjs/feed/json.js +21 -17
  25. package/dist/mjs/feed/topic.js +18 -17
  26. package/dist/mjs/feed/type.js +5 -5
  27. package/dist/mjs/index.js +7 -7
  28. package/dist/mjs/modules/bytes.js +33 -30
  29. package/dist/mjs/modules/bzz.js +80 -77
  30. package/dist/mjs/modules/chunk.js +22 -20
  31. package/dist/mjs/modules/debug/balance.js +26 -22
  32. package/dist/mjs/modules/debug/chequebook.js +84 -65
  33. package/dist/mjs/modules/debug/chunk.js +15 -13
  34. package/dist/mjs/modules/debug/connectivity.js +34 -34
  35. package/dist/mjs/modules/debug/settlements.js +14 -12
  36. package/dist/mjs/modules/debug/stamps.js +52 -47
  37. package/dist/mjs/modules/debug/states.js +16 -14
  38. package/dist/mjs/modules/debug/status.js +55 -35
  39. package/dist/mjs/modules/debug/tag.js +8 -7
  40. package/dist/mjs/modules/debug/transactions.js +31 -25
  41. package/dist/mjs/modules/feed.js +39 -33
  42. package/dist/mjs/modules/pinning.js +34 -28
  43. package/dist/mjs/modules/pss.js +18 -14
  44. package/dist/mjs/modules/soc.js +18 -15
  45. package/dist/mjs/modules/status.js +6 -5
  46. package/dist/mjs/modules/stewardship.js +13 -12
  47. package/dist/mjs/modules/tag.js +38 -30
  48. package/dist/mjs/package.json +1 -0
  49. package/dist/mjs/types/debug.js +5 -4
  50. package/dist/mjs/types/index.js +6 -2
  51. package/dist/mjs/types/ky-options.js +7 -0
  52. package/dist/mjs/types/ky-universal/common.js +7 -0
  53. package/dist/mjs/types/ky-universal/hooks.js +7 -0
  54. package/dist/mjs/types/ky-universal/retry.js +7 -0
  55. package/dist/mjs/utils/bytes.js +32 -26
  56. package/dist/mjs/utils/collection.browser.js +4 -3
  57. package/dist/mjs/utils/collection.js +47 -35
  58. package/dist/mjs/utils/collection.node.js +53 -42
  59. package/dist/mjs/utils/data.browser.js +64 -48
  60. package/dist/mjs/utils/data.js +29 -25
  61. package/dist/mjs/utils/error.js +47 -39
  62. package/dist/mjs/utils/eth.js +119 -104
  63. package/dist/mjs/utils/expose.js +9 -9
  64. package/dist/mjs/utils/file.js +22 -19
  65. package/dist/mjs/utils/hash.js +10 -5
  66. package/dist/mjs/utils/headers.js +43 -39
  67. package/dist/mjs/utils/hex.js +78 -63
  68. package/dist/mjs/utils/http.js +119 -100
  69. package/dist/mjs/utils/merge.js +26 -20
  70. package/dist/mjs/utils/pss.js +8 -6
  71. package/dist/mjs/utils/stamps.js +7 -3
  72. package/dist/mjs/utils/stream.js +107 -82
  73. package/dist/mjs/utils/tar.js +17 -14
  74. package/dist/mjs/utils/type.js +249 -209
  75. package/dist/mjs/utils/uint64.js +16 -16
  76. package/dist/mjs/utils/url.js +32 -25
  77. package/dist/types/types/index.d.ts +1 -1
  78. package/dist/types/types/ky-options.d.ts +221 -0
  79. package/dist/types/types/ky-universal/common.d.ts +13 -0
  80. package/dist/types/types/ky-universal/hooks.d.ts +92 -0
  81. package/dist/types/types/ky-universal/retry.d.ts +38 -0
  82. package/dist/types/utils/stream.d.ts +11 -8
  83. package/package.json +9 -6
  84. package/dist/index.js +0 -28326
  85. package/dist/index.js.map +0 -1
@@ -1,57 +1,73 @@
1
- import { isNodeReadable, isReadableStream } from './stream';
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
- if (typeof data === 'string')
10
- return new Blob([data], { type: 'text/plain' });
11
- if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
12
- return new Blob([data], { type: 'application/octet-stream' });
13
- }
14
- if (data instanceof Blob) {
15
- return data;
16
- }
17
- // Currently it is not possible to stream requests from browsers
18
- // there are already first experiments on this field (Chromium)
19
- // but till it is fully implemented across browsers-land we have to
20
- // buffer the data before sending the requests.
21
- if (isNodeReadable(data)) {
22
- return new Promise(resolve => {
23
- const buffers = [];
24
- data.on('data', d => {
25
- buffers.push(d);
26
- });
27
- data.on('end', () => {
28
- resolve(new Blob(buffers, { type: 'application/octet-stream' }));
29
- });
30
- });
31
- }
32
- if (isReadableStream(data)) {
33
- return new Promise(async (resolve) => {
34
- const reader = data.getReader();
35
- const buffers = [];
36
- let done, value;
37
- do {
38
- ;
39
- ({ done, value } = await reader.read());
40
- if (!done) {
41
- buffers.push(value);
42
- }
43
- } while (!done);
44
- resolve(new Blob(buffers, { type: 'application/octet-stream' }));
45
- });
46
- }
47
- throw new TypeError('unknown data type');
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
- if (typeof data === 'string')
51
- return new TextEncoder().encode(data);
52
- if (data instanceof ArrayBuffer)
53
- return new Uint8Array(data);
54
- if (data instanceof Blob)
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
+ }
@@ -1,5 +1,5 @@
1
1
  import Blob from 'cross-blob';
2
- import { isNodeReadable, isReadableStream, readableWebToNode } from './stream';
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
- if (typeof data === 'string')
13
- return new Blob([data], { type: 'text/plain' });
14
- if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
15
- return new Blob([data], { type: 'application/octet-stream' });
16
- }
17
- if (data instanceof Blob || isNodeReadable(data))
18
- return data;
19
- if (isReadableStream(data)) {
20
- return readableWebToNode(data);
21
- }
22
- throw new TypeError('unknown data type');
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
- return Array.isArray(buffer) && buffer.length > 0 && buffer.every(data => data instanceof Buffer);
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
- if (typeof data === 'string')
29
- return new TextEncoder().encode(data);
30
- if (data instanceof Buffer)
31
- return new Uint8Array(data);
32
- if (data instanceof ArrayBuffer)
33
- return new Uint8Array(data);
34
- if (data instanceof Blob)
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
+ }
@@ -1,48 +1,56 @@
1
1
  export class BeeError extends Error {
2
- constructor(message) {
3
- super(message);
4
- }
2
+ constructor(message) {
3
+ super(message);
4
+ }
5
+
5
6
  }
6
7
  export class BeeArgumentError extends BeeError {
7
- value;
8
- constructor(message, value) {
9
- super(message);
10
- this.value = value;
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
- requestOptions;
15
- /**
16
- * @param message
17
- * @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!
18
- */
19
- constructor(message, requestOptions) {
20
- super(message);
21
- this.requestOptions = requestOptions;
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
- status;
26
- response;
27
- responseBody;
28
- requestOptions;
29
- /**
30
- * @param status HTTP status code number
31
- * @param response Response returned from the server
32
- * @param responseBody Response body as string which is returned from response.text() call
33
- * @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!
34
- * @param message
35
- */
36
- constructor(status, response, responseBody, requestOptions, message) {
37
- super(message);
38
- this.status = status;
39
- this.response = response;
40
- this.responseBody = responseBody;
41
- this.requestOptions = requestOptions;
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
- constructor() {
46
- super(`Received response is not valid JSON.`);
47
- }
48
- }
52
+ constructor() {
53
+ super(`Received response is not valid JSON.`);
54
+ }
55
+
56
+ }
@@ -1,67 +1,76 @@
1
- import { keccak256, sha3_256 } from 'js-sha3';
2
- import { hexToBytes, intToHex, makeHexString, assertHexString } from './hex';
3
- import { assertBytes } from './bytes';
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
- if (typeof address === 'string') {
8
- const hexAddr = makeHexString(address, ETH_ADDR_HEX_LENGTH);
9
- const ownerBytes = hexToBytes(hexAddr);
10
- assertBytes(ownerBytes, ETH_ADDR_BYTES_LENGTH);
11
- return ownerBytes;
12
- }
13
- else if (address instanceof Uint8Array) {
14
- assertBytes(address, ETH_ADDR_BYTES_LENGTH);
15
- return address;
16
- }
17
- throw new TypeError('Invalid EthAddress');
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
- try {
21
- return makeHexString(address, ETH_ADDR_HEX_LENGTH);
22
- }
23
- catch (e) {
24
- if (e instanceof TypeError) {
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
- // Check it's string, all small caps or all all caps hex and 40 chars long without the `0x` prefix
37
- return (typeof address === 'string' && (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)));
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
- try {
46
- // Check for valid case insensitive hex type string, 40 chars
47
- const addr = makeHexString(address, ETH_ADDR_HEX_LENGTH);
48
- // Check the checksum
49
- const addressHash = keccak256(addr.toLowerCase());
50
- for (let i = 0; i < 40; i += 1) {
51
- // the nth letter should be uppercase if the nth digit of casemap is 1
52
- if ((parseInt(addressHash[i], 16) > 7 && addr[i].toUpperCase() !== addr[i]) ||
53
- (parseInt(addressHash[i], 16) <= 7 && addr[i].toLowerCase() !== addr[i])) {
54
- return false;
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
- catch (e) {
60
- if (e instanceof TypeError) {
61
- return false;
62
- }
63
- throw e;
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
- return isEthAddrCaseIns(address) || isValidChecksummedEthAddress(address);
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
- if (!(Number.isInteger(pad) && pad >= 2 && pad % 2 === 0)) {
90
- throw new TypeError('minimal padding for conversion needs to be positive even integer');
91
- }
92
- let hexRep;
93
- if (typeof bigEndian === 'string')
94
- hexRep = makeHexString(bigEndian);
95
- else if (typeof bigEndian === 'number')
96
- hexRep = intToHex(bigEndian);
97
- else
98
- throw new TypeError('incorrect input type');
99
- hexRep = hexRep.padStart(pad, '0');
100
- // Extend to an even length hexstring
101
- if (hexRep.length % 2 !== 0)
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
- // It's a reversible function
120
- return toLittleEndian(littleEndian, pad);
126
+ // It's a reversible function
127
+ return toLittleEndian(littleEndian, pad);
121
128
  }
129
+
122
130
  function assertEthAddress(ethAddress) {
123
- if (!isHexEthAddress(ethAddress))
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
- if (Number.isInteger(networkId && networkId > 0 && networkId < Number.MAX_SAFE_INTEGER)) {
128
- throw new TypeError('swarm network id must be positive integer');
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
- assertEthAddress(ethAddress);
141
- assertSwarmNetworkId(networkId);
142
- const hex = `${makeHexString(ethAddress)}${toLittleEndian(networkId, 16)}`;
143
- assertHexString(hex);
144
- const overlayAddress = sha3_256(hexToBytes(hex));
145
- return overlayAddress;
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
- let executorFnc;
156
- if (typeof provider !== 'object' || provider === null) {
157
- throw new TypeError('We need JsonRPC provider object!');
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
- if (provider.request) {
160
- executorFnc = provider.request;
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
+ }
@@ -1,9 +1,9 @@
1
- export { getCollectionSize } from './collection';
2
- export { getFolderSize } from './collection.node';
3
- export { isBytes, assertBytes, isFlexBytes, assertFlexBytes, bytesAtOffset, flexBytesAtOffset, bytesEqual, } from './bytes';
4
- export { makeHexString, hexToBytes, bytesToHex, intToHex, isHexString, assertHexString, assertPrefixedHexString, } from './hex';
5
- export { makeEthAddress, makeHexEthAddress, isHexEthAddress, ethToSwarmAddress, toLittleEndian, fromLittleEndian, makeEthereumWalletSigner, } from './eth';
6
- export { readableWebToNode, readableNodeToWeb, isReadableStream, isNodeReadable, normalizeToReadableStream, isReadable, } from './stream';
7
- export { keccak256Hash } from './hash';
8
- export { makeMaxTarget } from './pss';
9
- export { getStampUsage } from './stamps';
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";
@@ -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
- // browser
8
- if (typeof File === 'function') {
9
- return file instanceof File;
10
- }
11
- // node.js
12
- const f = file;
13
- return (typeof f === 'object' &&
14
- typeof f.name === 'string' &&
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
- if (file.arrayBuffer) {
25
- return file.arrayBuffer();
26
- }
27
- // workaround for Safari where arrayBuffer is not supported on Files
28
- return new Promise(resolve => {
29
- const fr = new FileReader();
30
- fr.onload = () => resolve(fr.result);
31
- fr.readAsArrayBuffer(file);
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
+ }