@ethersphere/bee-js 3.3.0 → 3.3.2-pre.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 (91) 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 +8 -8
  4. package/dist/cjs/package.json +1 -0
  5. package/dist/cjs/types/debug.js +1 -0
  6. package/dist/cjs/types/ky-options.js +8 -0
  7. package/dist/cjs/types/ky-universal/common.js +8 -0
  8. package/dist/cjs/types/ky-universal/hooks.js +8 -0
  9. package/dist/cjs/types/ky-universal/retry.js +8 -0
  10. package/dist/cjs/utils/eth.js +8 -3
  11. package/dist/cjs/utils/hash.js +7 -2
  12. package/dist/cjs/utils/stream.js +14 -5
  13. package/dist/index.browser.min.js +3 -0
  14. package/dist/index.browser.min.js.LICENSE.txt +60 -0
  15. package/dist/index.browser.min.js.map +1 -0
  16. package/dist/mjs/bee-debug.js +608 -508
  17. package/dist/mjs/bee.js +935 -836
  18. package/dist/mjs/chunk/bmt.js +34 -24
  19. package/dist/mjs/chunk/cac.js +24 -22
  20. package/dist/mjs/chunk/serialize.js +9 -9
  21. package/dist/mjs/chunk/signer.js +92 -73
  22. package/dist/mjs/chunk/soc.js +78 -66
  23. package/dist/mjs/chunk/span.js +19 -16
  24. package/dist/mjs/feed/index.js +110 -101
  25. package/dist/mjs/feed/json.js +21 -17
  26. package/dist/mjs/feed/topic.js +18 -17
  27. package/dist/mjs/feed/type.js +5 -5
  28. package/dist/mjs/index.js +7 -7
  29. package/dist/mjs/modules/bytes.js +33 -30
  30. package/dist/mjs/modules/bzz.js +80 -77
  31. package/dist/mjs/modules/chunk.js +22 -20
  32. package/dist/mjs/modules/debug/balance.js +26 -22
  33. package/dist/mjs/modules/debug/chequebook.js +84 -65
  34. package/dist/mjs/modules/debug/chunk.js +15 -13
  35. package/dist/mjs/modules/debug/connectivity.js +34 -34
  36. package/dist/mjs/modules/debug/settlements.js +14 -12
  37. package/dist/mjs/modules/debug/stamps.js +52 -47
  38. package/dist/mjs/modules/debug/states.js +16 -14
  39. package/dist/mjs/modules/debug/status.js +58 -38
  40. package/dist/mjs/modules/debug/tag.js +8 -7
  41. package/dist/mjs/modules/debug/transactions.js +31 -25
  42. package/dist/mjs/modules/feed.js +39 -33
  43. package/dist/mjs/modules/pinning.js +34 -28
  44. package/dist/mjs/modules/pss.js +18 -14
  45. package/dist/mjs/modules/soc.js +18 -15
  46. package/dist/mjs/modules/status.js +6 -5
  47. package/dist/mjs/modules/stewardship.js +13 -12
  48. package/dist/mjs/modules/tag.js +38 -30
  49. package/dist/mjs/package.json +1 -0
  50. package/dist/mjs/types/debug.js +6 -4
  51. package/dist/mjs/types/index.js +6 -2
  52. package/dist/mjs/types/ky-options.js +7 -0
  53. package/dist/mjs/types/ky-universal/common.js +7 -0
  54. package/dist/mjs/types/ky-universal/hooks.js +7 -0
  55. package/dist/mjs/types/ky-universal/retry.js +7 -0
  56. package/dist/mjs/utils/bytes.js +32 -26
  57. package/dist/mjs/utils/collection.browser.js +4 -3
  58. package/dist/mjs/utils/collection.js +47 -35
  59. package/dist/mjs/utils/collection.node.js +53 -42
  60. package/dist/mjs/utils/data.browser.js +64 -48
  61. package/dist/mjs/utils/data.js +29 -25
  62. package/dist/mjs/utils/error.js +47 -39
  63. package/dist/mjs/utils/eth.js +119 -104
  64. package/dist/mjs/utils/expose.js +9 -9
  65. package/dist/mjs/utils/file.js +22 -19
  66. package/dist/mjs/utils/hash.js +10 -5
  67. package/dist/mjs/utils/headers.js +43 -39
  68. package/dist/mjs/utils/hex.js +78 -63
  69. package/dist/mjs/utils/http.js +119 -100
  70. package/dist/mjs/utils/merge.js +26 -20
  71. package/dist/mjs/utils/pss.js +8 -6
  72. package/dist/mjs/utils/stamps.js +7 -3
  73. package/dist/mjs/utils/stream.js +107 -82
  74. package/dist/mjs/utils/tar.js +17 -14
  75. package/dist/mjs/utils/type.js +249 -209
  76. package/dist/mjs/utils/uint64.js +16 -16
  77. package/dist/mjs/utils/url.js +32 -25
  78. package/dist/types/bee-debug.d.ts +3 -3
  79. package/dist/types/modules/debug/stamps.d.ts +3 -3
  80. package/dist/types/modules/debug/status.d.ts +3 -3
  81. package/dist/types/types/debug.d.ts +1 -0
  82. package/dist/types/types/index.d.ts +2 -2
  83. package/dist/types/types/ky-options.d.ts +221 -0
  84. package/dist/types/types/ky-universal/common.d.ts +13 -0
  85. package/dist/types/types/ky-universal/hooks.d.ts +92 -0
  86. package/dist/types/types/ky-universal/retry.d.ts +38 -0
  87. package/dist/types/utils/stamps.d.ts +2 -2
  88. package/dist/types/utils/stream.d.ts +11 -8
  89. package/package.json +22 -19
  90. package/dist/index.js +0 -28326
  91. package/dist/index.js.map +0 -1
@@ -1,6 +1,10 @@
1
- import { keccak256 } from 'js-sha3';
2
- import { BeeArgumentError } from '../utils/error';
3
- import { keccak256Hash } from '../utils/hash';
1
+ // For ESM compatibility
2
+ import pkg from 'js-sha3';
3
+ const {
4
+ keccak256
5
+ } = pkg;
6
+ import { BeeArgumentError } from "../utils/error.js";
7
+ import { keccak256Hash } from "../utils/hash.js";
4
8
  const MAX_CHUNK_PAYLOAD_SIZE = 4096;
5
9
  const SEGMENT_SIZE = 32;
6
10
  const SEGMENT_PAIR_SIZE = 2 * SEGMENT_SIZE;
@@ -19,28 +23,34 @@ const HASH_SIZE = 32;
19
23
  *
20
24
  * @returns the keccak256 hash in a byte array
21
25
  */
26
+
22
27
  export function bmtHash(chunkContent) {
23
- const span = chunkContent.slice(0, 8);
24
- const payload = chunkContent.slice(8);
25
- const rootHash = bmtRootHash(payload);
26
- const chunkHashInput = new Uint8Array([...span, ...rootHash]);
27
- const chunkHash = keccak256Hash(chunkHashInput);
28
- return chunkHash;
28
+ const span = chunkContent.slice(0, 8);
29
+ const payload = chunkContent.slice(8);
30
+ const rootHash = bmtRootHash(payload);
31
+ const chunkHashInput = new Uint8Array([...span, ...rootHash]);
32
+ const chunkHash = keccak256Hash(chunkHashInput);
33
+ return chunkHash;
29
34
  }
35
+
30
36
  function bmtRootHash(payload) {
31
- if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
32
- throw new BeeArgumentError('invalid data length', payload);
37
+ if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
38
+ throw new BeeArgumentError('invalid data length', payload);
39
+ } // create an input buffer padded with zeros
40
+
41
+
42
+ let input = new Uint8Array([...payload, ...new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE - payload.length)]);
43
+
44
+ while (input.length !== HASH_SIZE) {
45
+ const output = new Uint8Array(input.length / 2); // in each round we hash the segment pairs together
46
+
47
+ for (let offset = 0; offset < input.length; offset += SEGMENT_PAIR_SIZE) {
48
+ const hashNumbers = keccak256.array(input.slice(offset, offset + SEGMENT_PAIR_SIZE));
49
+ output.set(hashNumbers, offset / 2);
33
50
  }
34
- // create an input buffer padded with zeros
35
- let input = new Uint8Array([...payload, ...new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE - payload.length)]);
36
- while (input.length !== HASH_SIZE) {
37
- const output = new Uint8Array(input.length / 2);
38
- // in each round we hash the segment pairs together
39
- for (let offset = 0; offset < input.length; offset += SEGMENT_PAIR_SIZE) {
40
- const hashNumbers = keccak256.array(input.slice(offset, offset + SEGMENT_PAIR_SIZE));
41
- output.set(hashNumbers, offset / 2);
42
- }
43
- input = output;
44
- }
45
- return input;
46
- }
51
+
52
+ input = output;
53
+ }
54
+
55
+ return input;
56
+ }
@@ -1,8 +1,8 @@
1
- import { BeeError } from '../utils/error';
2
- import { bmtHash } from './bmt';
3
- import { bytesEqual, flexBytesAtOffset, assertFlexBytes } from '../utils/bytes';
4
- import { serializeBytes } from './serialize';
5
- import { makeSpan, SPAN_SIZE } from './span';
1
+ import { BeeError } from "../utils/error.js";
2
+ import { bmtHash } from "./bmt.js";
3
+ import { bytesEqual, flexBytesAtOffset, assertFlexBytes } from "../utils/bytes.js";
4
+ import { serializeBytes } from "./serialize.js";
5
+ import { makeSpan, SPAN_SIZE } from "./span.js";
6
6
  export const MIN_PAYLOAD_SIZE = 1;
7
7
  export const MAX_PAYLOAD_SIZE = 4096;
8
8
  const CAC_SPAN_OFFSET = 0;
@@ -12,16 +12,17 @@ const CAC_PAYLOAD_OFFSET = CAC_SPAN_OFFSET + SPAN_SIZE;
12
12
  *
13
13
  * @param payloadBytes the data to be stored in the chunk
14
14
  */
15
+
15
16
  export function makeContentAddressedChunk(payloadBytes) {
16
- const span = makeSpan(payloadBytes.length);
17
- assertFlexBytes(payloadBytes, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE);
18
- const data = serializeBytes(span, payloadBytes);
19
- return {
20
- data,
21
- span: () => span,
22
- payload: () => flexBytesAtOffset(data, CAC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE),
23
- address: () => bmtHash(data),
24
- };
17
+ const span = makeSpan(payloadBytes.length);
18
+ assertFlexBytes(payloadBytes, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE);
19
+ const data = serializeBytes(span, payloadBytes);
20
+ return {
21
+ data,
22
+ span: () => span,
23
+ payload: () => flexBytesAtOffset(data, CAC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE),
24
+ address: () => bmtHash(data)
25
+ };
25
26
  }
26
27
  /**
27
28
  * Type guard for valid content addressed chunk data
@@ -29,11 +30,11 @@ export function makeContentAddressedChunk(payloadBytes) {
29
30
  * @param data The chunk data
30
31
  * @param chunkAddress The address of the chunk
31
32
  */
33
+
32
34
  export function isValidChunkData(data, chunkAddress) {
33
- if (!(data instanceof Uint8Array))
34
- return false;
35
- const address = bmtHash(data);
36
- return bytesEqual(address, chunkAddress);
35
+ if (!(data instanceof Uint8Array)) return false;
36
+ const address = bmtHash(data);
37
+ return bytesEqual(address, chunkAddress);
37
38
  }
38
39
  /**
39
40
  * Asserts if data are representing given address of its chunk.
@@ -43,8 +44,9 @@ export function isValidChunkData(data, chunkAddress) {
43
44
  *
44
45
  * @returns a valid content addressed chunk or throws error
45
46
  */
47
+
46
48
  export function assertValidChunkData(data, chunkAddress) {
47
- if (!isValidChunkData(data, chunkAddress)) {
48
- throw new BeeError('Address of content address chunk does not match given data!');
49
- }
50
- }
49
+ if (!isValidChunkData(data, chunkAddress)) {
50
+ throw new BeeError('Address of content address chunk does not match given data!');
51
+ }
52
+ }
@@ -4,12 +4,12 @@
4
4
  * @param arrays Any number of byte array arguments
5
5
  */
6
6
  export function serializeBytes(...arrays) {
7
- const length = arrays.reduce((prev, curr) => prev + curr.length, 0);
8
- const buffer = new Uint8Array(length);
9
- let offset = 0;
10
- arrays.forEach(arr => {
11
- buffer.set(arr, offset);
12
- offset += arr.length;
13
- });
14
- return buffer;
15
- }
7
+ const length = arrays.reduce((prev, curr) => prev + curr.length, 0);
8
+ const buffer = new Uint8Array(length);
9
+ let offset = 0;
10
+ arrays.forEach(arr => {
11
+ buffer.set(arr, offset);
12
+ offset += arr.length;
13
+ });
14
+ return buffer;
15
+ }
@@ -1,15 +1,20 @@
1
- import { ec } from 'elliptic';
2
- import { BeeError } from '../utils/error';
3
- import { isBytes, assertBytes, wrapBytesWithHelpers } from '../utils/bytes';
4
- import { keccak256Hash } from '../utils/hash';
5
- import { hexToBytes, makeHexString } from '../utils/hex';
6
- import { SIGNATURE_BYTES_LENGTH, SIGNATURE_HEX_LENGTH } from '../types';
7
- import { isStrictlyObject } from '../utils/type';
1
+ // For ESM compatibility
2
+ import pkg from 'elliptic';
3
+ const {
4
+ ec
5
+ } = pkg;
6
+ import { BeeError } from "../utils/error.js";
7
+ import { isBytes, assertBytes, wrapBytesWithHelpers } from "../utils/bytes.js";
8
+ import { keccak256Hash } from "../utils/hash.js";
9
+ import { hexToBytes, makeHexString } from "../utils/hex.js";
10
+ import { SIGNATURE_BYTES_LENGTH, SIGNATURE_HEX_LENGTH } from "../types/index.js";
11
+ import { isStrictlyObject } from "../utils/type.js";
8
12
  const UNCOMPRESSED_RECOVERY_ID = 27;
13
+
9
14
  function hashWithEthereumPrefix(data) {
10
- const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`;
11
- const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix);
12
- return keccak256Hash(prefixBytes, data);
15
+ const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`;
16
+ const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix);
17
+ return keccak256Hash(prefixBytes, data);
13
18
  }
14
19
  /**
15
20
  * The default signer function that can be used for integrating with
@@ -18,24 +23,28 @@ function hashWithEthereumPrefix(data) {
18
23
  * @param data The data to be signed
19
24
  * @param privateKey The private key used for signing the data
20
25
  */
26
+
27
+
21
28
  export function defaultSign(data, privateKey) {
22
- const curve = new ec('secp256k1');
23
- const keyPair = curve.keyFromPrivate(privateKey);
24
- const hashedDigest = hashWithEthereumPrefix(data);
25
- const sigRaw = curve.sign(hashedDigest, keyPair, { canonical: true, pers: undefined });
26
- if (sigRaw.recoveryParam === null) {
27
- throw new BeeError('signDigest recovery param was null');
28
- }
29
- const signature = new Uint8Array([
30
- ...sigRaw.r.toArray('be', 32),
31
- ...sigRaw.s.toArray('be', 32),
32
- sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID,
33
- ]);
34
- return signature;
29
+ const curve = new ec('secp256k1');
30
+ const keyPair = curve.keyFromPrivate(privateKey);
31
+ const hashedDigest = hashWithEthereumPrefix(data);
32
+ const sigRaw = curve.sign(hashedDigest, keyPair, {
33
+ canonical: true,
34
+ pers: undefined
35
+ });
36
+
37
+ if (sigRaw.recoveryParam === null) {
38
+ throw new BeeError('signDigest recovery param was null');
39
+ }
40
+
41
+ const signature = new Uint8Array([...sigRaw.r.toArray('be', 32), ...sigRaw.s.toArray('be', 32), sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID]);
42
+ return signature;
35
43
  }
44
+
36
45
  function publicKeyToAddress(pubKey) {
37
- const pubBytes = pubKey.encode('array', false);
38
- return keccak256Hash(pubBytes.slice(1)).slice(12);
46
+ const pubBytes = pubKey.encode('array', false);
47
+ return keccak256Hash(pubBytes.slice(1)).slice(12);
39
48
  }
40
49
  /**
41
50
  * Recovers the ethereum address from a given signature.
@@ -48,65 +57,75 @@ function publicKeyToAddress(pubKey) {
48
57
  *
49
58
  * @returns the recovered address
50
59
  */
60
+
61
+
51
62
  export function recoverAddress(signature, digest) {
52
- const curve = new ec('secp256k1');
53
- const sig = {
54
- r: signature.slice(0, 32),
55
- s: signature.slice(32, 64),
56
- };
57
- const recoveryParam = signature[64] - UNCOMPRESSED_RECOVERY_ID;
58
- const hash = hashWithEthereumPrefix(digest);
59
- const recPubKey = curve.recoverPubKey(hash, sig, recoveryParam);
60
- return publicKeyToAddress(recPubKey);
63
+ const curve = new ec('secp256k1');
64
+ const sig = {
65
+ r: signature.slice(0, 32),
66
+ s: signature.slice(32, 64)
67
+ };
68
+ const recoveryParam = signature[64] - UNCOMPRESSED_RECOVERY_ID;
69
+ const hash = hashWithEthereumPrefix(digest);
70
+ const recPubKey = curve.recoverPubKey(hash, sig, recoveryParam);
71
+ return publicKeyToAddress(recPubKey);
61
72
  }
62
73
  /**
63
74
  * Creates a singer object that can be used when the private key is known.
64
75
  *
65
76
  * @param privateKey The private key
66
77
  */
78
+
67
79
  export function makePrivateKeySigner(privateKey) {
68
- const curve = new ec('secp256k1');
69
- const keyPair = curve.keyFromPrivate(privateKey);
70
- const address = publicKeyToAddress(keyPair.getPublic());
71
- return {
72
- sign: (digest) => defaultSign(digest, privateKey),
73
- address,
74
- };
80
+ const curve = new ec('secp256k1');
81
+ const keyPair = curve.keyFromPrivate(privateKey);
82
+ const address = publicKeyToAddress(keyPair.getPublic());
83
+ return {
84
+ sign: digest => defaultSign(digest, privateKey),
85
+ address
86
+ };
75
87
  }
76
88
  export function assertSigner(signer) {
77
- if (!isStrictlyObject(signer)) {
78
- throw new TypeError('Signer must be an object!');
79
- }
80
- const typedSigner = signer;
81
- if (!isBytes(typedSigner.address, 20)) {
82
- throw new TypeError("Signer's address must be Uint8Array with 20 bytes!");
83
- }
84
- if (typeof typedSigner.sign !== 'function') {
85
- throw new TypeError('Signer sign property needs to be function!');
86
- }
89
+ if (!isStrictlyObject(signer)) {
90
+ throw new TypeError('Signer must be an object!');
91
+ }
92
+
93
+ const typedSigner = signer;
94
+
95
+ if (!isBytes(typedSigner.address, 20)) {
96
+ throw new TypeError("Signer's address must be Uint8Array with 20 bytes!");
97
+ }
98
+
99
+ if (typeof typedSigner.sign !== 'function') {
100
+ throw new TypeError('Signer sign property needs to be function!');
101
+ }
87
102
  }
88
103
  export function makeSigner(signer) {
89
- if (typeof signer === 'string') {
90
- const hexKey = makeHexString(signer, 64);
91
- const keyBytes = hexToBytes(hexKey); // HexString is verified for 64 length => 32 is guaranteed
92
- return makePrivateKeySigner(keyBytes);
93
- }
94
- else if (signer instanceof Uint8Array) {
95
- assertBytes(signer, 32);
96
- return makePrivateKeySigner(signer);
97
- }
98
- assertSigner(signer);
99
- return signer;
104
+ if (typeof signer === 'string') {
105
+ const hexKey = makeHexString(signer, 64);
106
+ const keyBytes = hexToBytes(hexKey); // HexString is verified for 64 length => 32 is guaranteed
107
+
108
+ return makePrivateKeySigner(keyBytes);
109
+ } else if (signer instanceof Uint8Array) {
110
+ assertBytes(signer, 32);
111
+ return makePrivateKeySigner(signer);
112
+ }
113
+
114
+ assertSigner(signer);
115
+ return signer;
100
116
  }
101
117
  export async function sign(signer, data) {
102
- const result = await signer.sign(wrapBytesWithHelpers(data));
103
- if (typeof result === 'string') {
104
- const hexString = makeHexString(result, SIGNATURE_HEX_LENGTH);
105
- return hexToBytes(hexString);
106
- }
107
- if (result instanceof Uint8Array) {
108
- assertBytes(result, SIGNATURE_BYTES_LENGTH);
109
- return result;
110
- }
111
- throw new TypeError('Invalid output of sign function!');
112
- }
118
+ const result = await signer.sign(wrapBytesWithHelpers(data));
119
+
120
+ if (typeof result === 'string') {
121
+ const hexString = makeHexString(result, SIGNATURE_HEX_LENGTH);
122
+ return hexToBytes(hexString);
123
+ }
124
+
125
+ if (result instanceof Uint8Array) {
126
+ assertBytes(result, SIGNATURE_BYTES_LENGTH);
127
+ return result;
128
+ }
129
+
130
+ throw new TypeError('Invalid output of sign function!');
131
+ }
@@ -1,29 +1,30 @@
1
- import { bytesAtOffset, bytesEqual, flexBytesAtOffset } from '../utils/bytes';
2
- import { bmtHash } from './bmt';
3
- import { recoverAddress, sign } from './signer';
4
- import { keccak256Hash } from '../utils/hash';
5
- import { SPAN_SIZE } from './span';
6
- import { serializeBytes } from './serialize';
7
- import { BeeError } from '../utils/error';
8
- import { makeContentAddressedChunk, MAX_PAYLOAD_SIZE, MIN_PAYLOAD_SIZE, assertValidChunkData, } from './cac';
9
- import { bytesToHex } from '../utils/hex';
10
- import * as socAPI from '../modules/soc';
11
- import * as chunkAPI from '../modules/chunk';
12
- import { assertAddress } from '../utils/type';
1
+ import { bytesAtOffset, bytesEqual, flexBytesAtOffset } from "../utils/bytes.js";
2
+ import { bmtHash } from "./bmt.js";
3
+ import { recoverAddress, sign } from "./signer.js";
4
+ import { keccak256Hash } from "../utils/hash.js";
5
+ import { SPAN_SIZE } from "./span.js";
6
+ import { serializeBytes } from "./serialize.js";
7
+ import { BeeError } from "../utils/error.js";
8
+ import { makeContentAddressedChunk, MAX_PAYLOAD_SIZE, MIN_PAYLOAD_SIZE, assertValidChunkData } from "./cac.js";
9
+ import { bytesToHex } from "../utils/hex.js";
10
+ import * as socAPI from "../modules/soc.js";
11
+ import * as chunkAPI from "../modules/chunk.js";
12
+ import { assertAddress } from "../utils/type.js";
13
13
  const IDENTIFIER_SIZE = 32;
14
14
  const SIGNATURE_SIZE = 65;
15
15
  const SOC_IDENTIFIER_OFFSET = 0;
16
16
  const SOC_SIGNATURE_OFFSET = SOC_IDENTIFIER_OFFSET + IDENTIFIER_SIZE;
17
17
  const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + SIGNATURE_SIZE;
18
18
  const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + SPAN_SIZE;
19
+
19
20
  function recoverChunkOwner(data) {
20
- const cacData = data.slice(SOC_SPAN_OFFSET);
21
- const chunkAddress = bmtHash(cacData);
22
- const signature = bytesAtOffset(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
23
- const identifier = bytesAtOffset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
24
- const digest = keccak256Hash(identifier, chunkAddress);
25
- const ownerAddress = recoverAddress(signature, digest);
26
- return ownerAddress;
21
+ const cacData = data.slice(SOC_SPAN_OFFSET);
22
+ const chunkAddress = bmtHash(cacData);
23
+ const signature = bytesAtOffset(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
24
+ const identifier = bytesAtOffset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
25
+ const digest = keccak256Hash(identifier, chunkAddress);
26
+ const ownerAddress = recoverAddress(signature, digest);
27
+ return ownerAddress;
27
28
  }
28
29
  /**
29
30
  * Verifies if the data is a valid single owner chunk
@@ -33,28 +34,35 @@ function recoverChunkOwner(data) {
33
34
  *
34
35
  * @returns a single owner chunk or throws error
35
36
  */
37
+
38
+
36
39
  export function makeSingleOwnerChunkFromData(data, address) {
37
- const ownerAddress = recoverChunkOwner(data);
38
- const identifier = bytesAtOffset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
39
- const socAddress = keccak256Hash(identifier, ownerAddress);
40
- if (!bytesEqual(address, socAddress)) {
41
- throw new BeeError('SOC Data does not match given address!');
42
- }
43
- const signature = () => bytesAtOffset(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
44
- const span = () => bytesAtOffset(data, SOC_SPAN_OFFSET, SPAN_SIZE);
45
- const payload = () => flexBytesAtOffset(data, SOC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE);
46
- return {
47
- data,
48
- identifier: () => identifier,
49
- signature,
50
- span,
51
- payload,
52
- address: () => socAddress,
53
- owner: () => ownerAddress,
54
- };
40
+ const ownerAddress = recoverChunkOwner(data);
41
+ const identifier = bytesAtOffset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE);
42
+ const socAddress = keccak256Hash(identifier, ownerAddress);
43
+
44
+ if (!bytesEqual(address, socAddress)) {
45
+ throw new BeeError('SOC Data does not match given address!');
46
+ }
47
+
48
+ const signature = () => bytesAtOffset(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE);
49
+
50
+ const span = () => bytesAtOffset(data, SOC_SPAN_OFFSET, SPAN_SIZE);
51
+
52
+ const payload = () => flexBytesAtOffset(data, SOC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE);
53
+
54
+ return {
55
+ data,
56
+ identifier: () => identifier,
57
+ signature,
58
+ span,
59
+ payload,
60
+ address: () => socAddress,
61
+ owner: () => ownerAddress
62
+ };
55
63
  }
56
64
  export function makeSOCAddress(identifier, address) {
57
- return keccak256Hash(identifier, address);
65
+ return keccak256Hash(identifier, address);
58
66
  }
59
67
  /**
60
68
  * Creates a single owner chunk object
@@ -63,22 +71,23 @@ export function makeSOCAddress(identifier, address) {
63
71
  * @param identifier The identifier of the chunk
64
72
  * @param signer The singer interface for signing the chunk
65
73
  */
74
+
66
75
  export async function makeSingleOwnerChunk(chunk, identifier, signer) {
67
- const chunkAddress = chunk.address();
68
- assertValidChunkData(chunk.data, chunkAddress);
69
- const digest = keccak256Hash(identifier, chunkAddress);
70
- const signature = await sign(signer, digest);
71
- const data = serializeBytes(identifier, signature, chunk.span(), chunk.payload());
72
- const address = makeSOCAddress(identifier, signer.address);
73
- return {
74
- data,
75
- identifier: () => identifier,
76
- signature: () => signature,
77
- span: () => chunk.span(),
78
- payload: () => chunk.payload(),
79
- address: () => address,
80
- owner: () => signer.address,
81
- };
76
+ const chunkAddress = chunk.address();
77
+ assertValidChunkData(chunk.data, chunkAddress);
78
+ const digest = keccak256Hash(identifier, chunkAddress);
79
+ const signature = await sign(signer, digest);
80
+ const data = serializeBytes(identifier, signature, chunk.span(), chunk.payload());
81
+ const address = makeSOCAddress(identifier, signer.address);
82
+ return {
83
+ data,
84
+ identifier: () => identifier,
85
+ signature: () => signature,
86
+ span: () => chunk.span(),
87
+ payload: () => chunk.payload(),
88
+ address: () => address,
89
+ owner: () => signer.address
90
+ };
82
91
  }
83
92
  /**
84
93
  * Helper function to upload a chunk.
@@ -90,12 +99,13 @@ export async function makeSingleOwnerChunk(chunk, identifier, signer) {
90
99
  * @param postageBatchId Postage BatchId that will be assigned to uploaded data
91
100
  * @param options Upload options
92
101
  */
102
+
93
103
  export async function uploadSingleOwnerChunk(ky, chunk, postageBatchId, options) {
94
- const owner = bytesToHex(chunk.owner());
95
- const identifier = bytesToHex(chunk.identifier());
96
- const signature = bytesToHex(chunk.signature());
97
- const data = serializeBytes(chunk.span(), chunk.payload());
98
- return socAPI.upload(ky, owner, identifier, signature, data, postageBatchId, options);
104
+ const owner = bytesToHex(chunk.owner());
105
+ const identifier = bytesToHex(chunk.identifier());
106
+ const signature = bytesToHex(chunk.signature());
107
+ const data = serializeBytes(chunk.span(), chunk.payload());
108
+ return socAPI.upload(ky, owner, identifier, signature, data, postageBatchId, options);
99
109
  }
100
110
  /**
101
111
  * Helper function to create and upload SOC.
@@ -107,11 +117,12 @@ export async function uploadSingleOwnerChunk(ky, chunk, postageBatchId, options)
107
117
  * @param data The chunk data
108
118
  * @param options
109
119
  */
120
+
110
121
  export async function uploadSingleOwnerChunkData(ky, signer, postageBatchId, identifier, data, options) {
111
- assertAddress(postageBatchId);
112
- const cac = makeContentAddressedChunk(data);
113
- const soc = await makeSingleOwnerChunk(cac, identifier, signer);
114
- return uploadSingleOwnerChunk(ky, soc, postageBatchId, options);
122
+ assertAddress(postageBatchId);
123
+ const cac = makeContentAddressedChunk(data);
124
+ const soc = await makeSingleOwnerChunk(cac, identifier, signer);
125
+ return uploadSingleOwnerChunk(ky, soc, postageBatchId, options);
115
126
  }
116
127
  /**
117
128
  * Helper function to download SOC.
@@ -120,8 +131,9 @@ export async function uploadSingleOwnerChunkData(ky, signer, postageBatchId, ide
120
131
  * @param ownerAddress The singer interface for signing the chunk
121
132
  * @param identifier The identifier of the chunk
122
133
  */
134
+
123
135
  export async function downloadSingleOwnerChunk(ky, ownerAddress, identifier) {
124
- const address = makeSOCAddress(identifier, ownerAddress);
125
- const data = await chunkAPI.download(ky, bytesToHex(address));
126
- return makeSingleOwnerChunkFromData(data, address);
127
- }
136
+ const address = makeSOCAddress(identifier, ownerAddress);
137
+ const data = await chunkAPI.download(ky, bytesToHex(address));
138
+ return makeSingleOwnerChunkFromData(data, address);
139
+ }
@@ -1,6 +1,6 @@
1
- import { BeeArgumentError } from '../utils/error';
2
- export const SPAN_SIZE = 8;
3
- // we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
1
+ import { BeeArgumentError } from "../utils/error.js";
2
+ export const SPAN_SIZE = 8; // we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
3
+
4
4
  const MAX_SPAN_LENGTH = 2 ** 32 - 1;
5
5
  /**
6
6
  * Create a span for storing the length of the chunk
@@ -9,17 +9,20 @@ const MAX_SPAN_LENGTH = 2 ** 32 - 1;
9
9
  *
10
10
  * @param length The length of the span
11
11
  */
12
+
12
13
  export function makeSpan(length) {
13
- if (length <= 0) {
14
- throw new BeeArgumentError('invalid length for span', length);
15
- }
16
- if (length > MAX_SPAN_LENGTH) {
17
- throw new BeeArgumentError('invalid length (> MAX_SPAN_LENGTH)', length);
18
- }
19
- const span = new Uint8Array(SPAN_SIZE);
20
- const dataView = new DataView(span.buffer);
21
- const littleEndian = true;
22
- const lengthLower32 = length & 0xffffffff;
23
- dataView.setUint32(0, lengthLower32, littleEndian);
24
- return span;
25
- }
14
+ if (length <= 0) {
15
+ throw new BeeArgumentError('invalid length for span', length);
16
+ }
17
+
18
+ if (length > MAX_SPAN_LENGTH) {
19
+ throw new BeeArgumentError('invalid length (> MAX_SPAN_LENGTH)', length);
20
+ }
21
+
22
+ const span = new Uint8Array(SPAN_SIZE);
23
+ const dataView = new DataView(span.buffer);
24
+ const littleEndian = true;
25
+ const lengthLower32 = length & 0xffffffff;
26
+ dataView.setUint32(0, lengthLower32, littleEndian);
27
+ return span;
28
+ }