@ethersphere/bee-js 1.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/CHANGELOG.md +135 -2
  2. package/README.md +3 -1
  3. package/dist/index.browser.min.js +1 -1
  4. package/dist/index.browser.min.js.LICENSE.txt +51 -0
  5. package/dist/index.browser.min.js.map +1 -1
  6. package/dist/index.min.js +1 -1
  7. package/dist/index.min.js.LICENSE.txt +42 -0
  8. package/dist/index.min.js.map +1 -1
  9. package/dist/src/bee-debug.d.ts +75 -38
  10. package/dist/src/bee.d.ts +88 -82
  11. package/dist/src/chunk/soc.d.ts +6 -6
  12. package/dist/src/feed/index.d.ts +7 -7
  13. package/dist/src/feed/json.d.ts +2 -2
  14. package/dist/src/modules/bytes.d.ts +8 -11
  15. package/dist/src/modules/bzz.d.ts +9 -14
  16. package/dist/src/modules/chunk.d.ts +7 -11
  17. package/dist/src/modules/debug/balance.d.ts +9 -9
  18. package/dist/src/modules/debug/chequebook.d.ts +17 -18
  19. package/dist/src/modules/debug/chunk.d.ts +5 -5
  20. package/dist/src/modules/debug/connectivity.d.ts +7 -7
  21. package/dist/src/modules/debug/settlements.d.ts +5 -5
  22. package/dist/src/modules/debug/stamps.d.ts +7 -5
  23. package/dist/src/modules/debug/states.d.ts +5 -5
  24. package/dist/src/modules/debug/status.d.ts +5 -4
  25. package/dist/src/modules/debug/tag.d.ts +3 -3
  26. package/dist/src/modules/debug/transactions.d.ts +9 -9
  27. package/dist/src/modules/feed.d.ts +5 -5
  28. package/dist/src/modules/pinning.d.ts +9 -9
  29. package/dist/src/modules/pss.d.ts +4 -3
  30. package/dist/src/modules/soc.d.ts +3 -3
  31. package/dist/src/modules/status.d.ts +3 -2
  32. package/dist/src/modules/stewardship.d.ts +4 -4
  33. package/dist/src/modules/tag.d.ts +6 -6
  34. package/dist/src/types/debug.d.ts +4 -2
  35. package/dist/src/types/index.d.ts +71 -19
  36. package/dist/src/utils/bytes.d.ts +1 -1
  37. package/dist/src/utils/data.browser.d.ts +3 -1
  38. package/dist/src/utils/data.d.ts +7 -4
  39. package/dist/src/utils/expose.d.ts +6 -9
  40. package/dist/src/utils/headers.d.ts +3 -3
  41. package/dist/src/utils/http.d.ts +25 -0
  42. package/dist/src/utils/merge.d.ts +8 -0
  43. package/dist/src/utils/pss.d.ts +9 -0
  44. package/dist/src/utils/stream.d.ts +37 -0
  45. package/dist/src/utils/type.d.ts +19 -9
  46. package/package.json +15 -8
  47. package/.babelrc.js +0 -33
  48. package/.depcheckrc.json +0 -27
  49. package/.editorconfig +0 -6
  50. package/.eslintignore +0 -1
  51. package/.eslintrc.js +0 -108
  52. package/.gitattributes +0 -1
  53. package/.github/config.yaml +0 -23
  54. package/.github/workflows/check.yaml +0 -57
  55. package/.github/workflows/publish_npmjs.yaml +0 -20
  56. package/.github/workflows/release_github.yaml +0 -20
  57. package/.github/workflows/tests.yaml +0 -128
  58. package/.huskyrc +0 -5
  59. package/.prettierrc +0 -13
  60. package/.vscode/launch.json +0 -49
  61. package/.vscode/settings.json +0 -39
  62. package/CODEOWNERS +0 -1
  63. package/CODE_OF_CONDUCT.md +0 -76
  64. package/commitlint.config.js +0 -3
  65. package/dist/src/modules/stamps.d.ts +0 -4
  66. package/dist/src/utils/hooks.d.ts +0 -31
  67. package/dist/src/utils/safe-axios.d.ts +0 -8
  68. package/jest-puppeteer.config.js +0 -5
  69. package/jest.config.ts +0 -170
  70. package/src/bee-debug.ts +0 -440
  71. package/src/bee.ts +0 -941
  72. package/src/chunk/bmt.ts +0 -55
  73. package/src/chunk/cac.ts +0 -79
  74. package/src/chunk/serialize.ts +0 -16
  75. package/src/chunk/signer.ts +0 -141
  76. package/src/chunk/soc.ts +0 -190
  77. package/src/chunk/span.ts +0 -33
  78. package/src/feed/index.ts +0 -221
  79. package/src/feed/json.ts +0 -32
  80. package/src/feed/topic.ts +0 -23
  81. package/src/feed/type.ts +0 -12
  82. package/src/index.ts +0 -24
  83. package/src/modules/bytes.ts +0 -75
  84. package/src/modules/bzz.ts +0 -184
  85. package/src/modules/chunk.ts +0 -75
  86. package/src/modules/debug/balance.ts +0 -63
  87. package/src/modules/debug/chequebook.ts +0 -174
  88. package/src/modules/debug/chunk.ts +0 -39
  89. package/src/modules/debug/connectivity.ts +0 -61
  90. package/src/modules/debug/settlements.ts +0 -33
  91. package/src/modules/debug/stamps.ts +0 -69
  92. package/src/modules/debug/states.ts +0 -35
  93. package/src/modules/debug/status.ts +0 -34
  94. package/src/modules/debug/tag.ts +0 -19
  95. package/src/modules/debug/transactions.ts +0 -79
  96. package/src/modules/feed.ts +0 -92
  97. package/src/modules/pinning.ts +0 -74
  98. package/src/modules/pss.ts +0 -48
  99. package/src/modules/soc.ts +0 -41
  100. package/src/modules/stamps.ts +0 -59
  101. package/src/modules/status.ts +0 -13
  102. package/src/modules/stewardship.ts +0 -21
  103. package/src/modules/tag.ts +0 -83
  104. package/src/types/axios/index.d.ts +0 -18
  105. package/src/types/debug.ts +0 -204
  106. package/src/types/index.ts +0 -497
  107. package/src/types/tar-js.d.ts +0 -1
  108. package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
  109. package/src/utils/bytes.ts +0 -144
  110. package/src/utils/collection.ts +0 -151
  111. package/src/utils/data.browser.ts +0 -24
  112. package/src/utils/data.ts +0 -35
  113. package/src/utils/error.ts +0 -23
  114. package/src/utils/eth.ts +0 -229
  115. package/src/utils/expose.ts +0 -10
  116. package/src/utils/file.ts +0 -40
  117. package/src/utils/hash.ts +0 -16
  118. package/src/utils/headers.ts +0 -63
  119. package/src/utils/hex.ts +0 -186
  120. package/src/utils/hooks.ts +0 -89
  121. package/src/utils/safe-axios.ts +0 -98
  122. package/src/utils/tar.ts +0 -30
  123. package/src/utils/type.ts +0 -302
  124. package/src/utils/uint64.ts +0 -31
  125. package/src/utils/url.ts +0 -52
  126. package/test/bee.sh +0 -187
  127. package/test/data/1.txt +0 -1
  128. package/test/data/2.txt +0 -0
  129. package/test/data/empty +0 -0
  130. package/test/data/sub/3.txt +0 -1
  131. package/test/data/sub//303/251 +0 -0
  132. package/test/data/sub//360/237/230/216 +0 -0
  133. package/test/integration/bee-class.browser.spec.ts +0 -215
  134. package/test/integration/bee-class.spec.ts +0 -595
  135. package/test/integration/bee-debug-class.spec.ts +0 -85
  136. package/test/integration/chunk/bmt.spec.ts +0 -33
  137. package/test/integration/chunk/cac.spec.ts +0 -27
  138. package/test/integration/chunk/soc.spec.ts +0 -38
  139. package/test/integration/feed/index.spec.ts +0 -84
  140. package/test/integration/modules/bytes.spec.ts +0 -23
  141. package/test/integration/modules/bzz.spec.ts +0 -270
  142. package/test/integration/modules/chunk.spec.ts +0 -29
  143. package/test/integration/modules/debug/balance.spec.ts +0 -70
  144. package/test/integration/modules/debug/chequebook.spec.ts +0 -64
  145. package/test/integration/modules/debug/connectivity.spec.ts +0 -69
  146. package/test/integration/modules/debug/settlements.spec.ts +0 -34
  147. package/test/integration/modules/debug/states.spec.ts +0 -34
  148. package/test/integration/modules/debug/status.spec.ts +0 -65
  149. package/test/integration/modules/debug/tag.spec.ts +0 -27
  150. package/test/integration/modules/feed.spec.ts +0 -61
  151. package/test/integration/modules/pinning.spec.ts +0 -155
  152. package/test/integration/modules/pss.spec.ts +0 -92
  153. package/test/integration/modules/status.spec.ts +0 -10
  154. package/test/integration/modules/stewardship.spec.ts +0 -32
  155. package/test/integration/modules/tag.spec.ts +0 -51
  156. package/test/test-type-sequencer.js +0 -36
  157. package/test/testpage/testpage.html +0 -20
  158. package/test/unit/assertions.ts +0 -376
  159. package/test/unit/bee-class.spec.ts +0 -685
  160. package/test/unit/bee-debug-class.spec.ts +0 -365
  161. package/test/unit/chunk/bmt.spec.ts +0 -16
  162. package/test/unit/chunk/cac.spec.ts +0 -29
  163. package/test/unit/chunk/serialize.spec.ts +0 -24
  164. package/test/unit/chunk/signer.spec.ts +0 -140
  165. package/test/unit/chunk/soc.spec.ts +0 -25
  166. package/test/unit/chunk/span.spec.ts +0 -38
  167. package/test/unit/feed/json.spec.ts +0 -73
  168. package/test/unit/nock.ts +0 -134
  169. package/test/unit/utils/bytes.spec.ts +0 -32
  170. package/test/unit/utils/collection.spec.ts +0 -22
  171. package/test/unit/utils/eth.spec.ts +0 -224
  172. package/test/unit/utils/hex.spec.ts +0 -115
  173. package/test/unit/utils/hooks.spec.ts +0 -179
  174. package/test/unit/utils/safe-axios.spec.ts +0 -20
  175. package/test/unit/utils/type.spec.ts +0 -32
  176. package/test/unit/utils/uint64.spec.ts +0 -102
  177. package/test/utils.ts +0 -292
  178. package/tsconfig.json +0 -18
  179. package/tsconfig.test.json +0 -11
  180. package/typedoc.json +0 -7
  181. package/webpack.config.ts +0 -141
package/src/chunk/bmt.ts DELETED
@@ -1,55 +0,0 @@
1
- import { keccak256 } from 'js-sha3'
2
- import { BeeArgumentError } from '../utils/error'
3
- import { Bytes } from '../utils/bytes'
4
- import { keccak256Hash } from '../utils/hash'
5
-
6
- const MAX_CHUNK_PAYLOAD_SIZE = 4096
7
- const SEGMENT_SIZE = 32
8
- const SEGMENT_PAIR_SIZE = 2 * SEGMENT_SIZE
9
- const HASH_SIZE = 32
10
-
11
- /**
12
- * Calculate a Binary Merkle Tree hash for a chunk
13
- *
14
- * The BMT chunk address is the hash of the 8 byte span and the root
15
- * hash of a binary Merkle tree (BMT) built on the 32-byte segments
16
- * of the underlying data.
17
- *
18
- * If the chunk content is less than 4k, the hash is calculated as
19
- * if the chunk was padded with all zeros up to 4096 bytes.
20
- *
21
- * @param chunkContent Chunk data including span and payload as well
22
- *
23
- * @returns the keccak256 hash in a byte array
24
- */
25
- export function bmtHash(chunkContent: Uint8Array): Bytes<32> {
26
- const span = chunkContent.slice(0, 8)
27
- const payload = chunkContent.slice(8)
28
- const rootHash = bmtRootHash(payload)
29
- const chunkHashInput = new Uint8Array([...span, ...rootHash])
30
- const chunkHash = keccak256Hash(chunkHashInput)
31
-
32
- return chunkHash
33
- }
34
-
35
- function bmtRootHash(payload: Uint8Array): Uint8Array {
36
- if (payload.length > MAX_CHUNK_PAYLOAD_SIZE) {
37
- throw new BeeArgumentError('invalid data length', payload)
38
- }
39
-
40
- // create an input buffer padded with zeros
41
- let input = new Uint8Array([...payload, ...new Uint8Array(MAX_CHUNK_PAYLOAD_SIZE - payload.length)])
42
- while (input.length !== HASH_SIZE) {
43
- const output = new Uint8Array(input.length / 2)
44
-
45
- // in each round we hash the segment pairs together
46
- for (let offset = 0; offset < input.length; offset += SEGMENT_PAIR_SIZE) {
47
- const hashNumbers = keccak256.array(input.slice(offset, offset + SEGMENT_PAIR_SIZE))
48
- output.set(hashNumbers, offset / 2)
49
- }
50
-
51
- input = output
52
- }
53
-
54
- return input
55
- }
package/src/chunk/cac.ts DELETED
@@ -1,79 +0,0 @@
1
- import { BrandedType } from '../types'
2
- import { BeeError } from '../utils/error'
3
- import { bmtHash } from './bmt'
4
- import { Bytes, bytesEqual, FlexBytes, flexBytesAtOffset, assertFlexBytes } from '../utils/bytes'
5
- import { serializeBytes } from './serialize'
6
- import { makeSpan, SPAN_SIZE } from './span'
7
-
8
- export const MIN_PAYLOAD_SIZE = 1
9
- export const MAX_PAYLOAD_SIZE = 4096
10
-
11
- const CAC_SPAN_OFFSET = 0
12
- const CAC_PAYLOAD_OFFSET = CAC_SPAN_OFFSET + SPAN_SIZE
13
-
14
- export type ChunkAddress = Bytes<32>
15
-
16
- /**
17
- * General chunk interface for Swarm
18
- *
19
- * It stores the serialized data and provides functions to access
20
- * the fields of a chunk.
21
- *
22
- * It also provides an address function to calculate the address of
23
- * the chunk that is required for the Chunk API.
24
- */
25
- export interface Chunk {
26
- readonly data: Uint8Array
27
- span(): Bytes<8>
28
- payload(): FlexBytes<1, 4096>
29
-
30
- address(): ChunkAddress
31
- }
32
-
33
- type ValidChunkData = BrandedType<Uint8Array, 'ValidChunkData'>
34
-
35
- /**
36
- * Creates a content addressed chunk and verifies the payload size.
37
- *
38
- * @param payloadBytes the data to be stored in the chunk
39
- */
40
- export function makeContentAddressedChunk(payloadBytes: Uint8Array): Chunk {
41
- const span = makeSpan(payloadBytes.length)
42
- assertFlexBytes(payloadBytes, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE)
43
- const data = serializeBytes(span, payloadBytes) as ValidChunkData
44
-
45
- return {
46
- data,
47
- span: () => span,
48
- payload: () => flexBytesAtOffset(data, CAC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE),
49
- address: () => bmtHash(data),
50
- }
51
- }
52
-
53
- /**
54
- * Type guard for valid content addressed chunk data
55
- *
56
- * @param data The chunk data
57
- * @param chunkAddress The address of the chunk
58
- */
59
- export function isValidChunkData(data: unknown, chunkAddress: ChunkAddress): data is ValidChunkData {
60
- if (!(data instanceof Uint8Array)) return false
61
-
62
- const address = bmtHash(data)
63
-
64
- return bytesEqual(address, chunkAddress)
65
- }
66
-
67
- /**
68
- * Asserts if data are representing given address of its chunk.
69
- *
70
- * @param data The chunk data
71
- * @param chunkAddress The address of the chunk
72
- *
73
- * @returns a valid content addressed chunk or throws error
74
- */
75
- export function assertValidChunkData(data: unknown, chunkAddress: ChunkAddress): asserts data is ValidChunkData {
76
- if (!isValidChunkData(data, chunkAddress)) {
77
- throw new BeeError('Address of content address chunk does not match given data!')
78
- }
79
- }
@@ -1,16 +0,0 @@
1
- /**
2
- * Helper function for serialize byte arrays
3
- *
4
- * @param arrays Any number of byte array arguments
5
- */
6
- export function serializeBytes(...arrays: Uint8Array[]): Uint8Array {
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
-
15
- return buffer
16
- }
@@ -1,141 +0,0 @@
1
- import { ec, curve } from 'elliptic'
2
- import { BeeError } from '../utils/error'
3
- import { Bytes, isBytes, assertBytes, wrapBytesWithHelpers } from '../utils/bytes'
4
- import { keccak256Hash } from '../utils/hash'
5
- import { hexToBytes, makeHexString } from '../utils/hex'
6
- import { EthAddress } from '../utils/eth'
7
- import { Data, PrivateKeyBytes, Signature, SIGNATURE_BYTES_LENGTH, SIGNATURE_HEX_LENGTH, Signer } from '../types'
8
- import { isStrictlyObject } from '../utils/type'
9
-
10
- type EllipticPublicKey = curve.base.BasePoint
11
- const UNCOMPRESSED_RECOVERY_ID = 27
12
-
13
- function hashWithEthereumPrefix(data: Uint8Array): Bytes<32> {
14
- const ethereumSignedMessagePrefix = `\x19Ethereum Signed Message:\n${data.length}`
15
- const prefixBytes = new TextEncoder().encode(ethereumSignedMessagePrefix)
16
-
17
- return keccak256Hash(prefixBytes, data)
18
- }
19
-
20
- /**
21
- * The default signer function that can be used for integrating with
22
- * other applications (e.g. wallets).
23
- *
24
- * @param data The data to be signed
25
- * @param privateKey The private key used for signing the data
26
- */
27
- export function defaultSign(data: Uint8Array, privateKey: PrivateKeyBytes): Signature {
28
- const curve = new ec('secp256k1')
29
- const keyPair = curve.keyFromPrivate(privateKey)
30
-
31
- const hashedDigest = hashWithEthereumPrefix(data)
32
- const sigRaw = curve.sign(hashedDigest, keyPair, { canonical: true, pers: undefined })
33
-
34
- if (sigRaw.recoveryParam === null) {
35
- throw new BeeError('signDigest recovery param was null')
36
- }
37
- const signature = new Uint8Array([
38
- ...sigRaw.r.toArray('be', 32),
39
- ...sigRaw.s.toArray('be', 32),
40
- sigRaw.recoveryParam + UNCOMPRESSED_RECOVERY_ID,
41
- ])
42
-
43
- return signature as Signature
44
- }
45
-
46
- function publicKeyToAddress(pubKey: EllipticPublicKey): EthAddress {
47
- const pubBytes = pubKey.encode('array', false)
48
-
49
- return keccak256Hash(pubBytes.slice(1)).slice(12) as EthAddress
50
- }
51
-
52
- /**
53
- * Recovers the ethereum address from a given signature.
54
- *
55
- * Can be used for verifying a piece of data when the public key is
56
- * known.
57
- *
58
- * @param signature The signature
59
- * @param digest The digest of the data
60
- *
61
- * @returns the recovered address
62
- */
63
- export function recoverAddress(signature: Signature, digest: Uint8Array): EthAddress {
64
- const curve = new ec('secp256k1')
65
- const sig = {
66
- r: signature.slice(0, 32),
67
- s: signature.slice(32, 64),
68
- }
69
- const recoveryParam = signature[64] - UNCOMPRESSED_RECOVERY_ID
70
- const hash = hashWithEthereumPrefix(digest)
71
- const recPubKey = curve.recoverPubKey(hash, sig, recoveryParam)
72
-
73
- return publicKeyToAddress(recPubKey)
74
- }
75
-
76
- /**
77
- * Creates a singer object that can be used when the private key is known.
78
- *
79
- * @param privateKey The private key
80
- */
81
- export function makePrivateKeySigner(privateKey: PrivateKeyBytes): Signer {
82
- const curve = new ec('secp256k1')
83
- const keyPair = curve.keyFromPrivate(privateKey)
84
- const address = publicKeyToAddress(keyPair.getPublic())
85
-
86
- return {
87
- sign: (digest: Data) => defaultSign(digest, privateKey),
88
- address,
89
- }
90
- }
91
-
92
- export function assertSigner(signer: unknown): asserts signer is Signer {
93
- if (!isStrictlyObject(signer)) {
94
- throw new TypeError('Signer must be an object!')
95
- }
96
-
97
- const typedSigner = signer as Signer
98
-
99
- if (!isBytes(typedSigner.address, 20)) {
100
- throw new TypeError("Signer's address must be Uint8Array with 20 bytes!")
101
- }
102
-
103
- if (typeof typedSigner.sign !== 'function') {
104
- throw new TypeError('Signer sign property needs to be function!')
105
- }
106
- }
107
-
108
- export function makeSigner(signer: Signer | Uint8Array | string | unknown): Signer {
109
- if (typeof signer === 'string') {
110
- const hexKey = makeHexString(signer, 64)
111
- const keyBytes = hexToBytes<32>(hexKey) // HexString is verified for 64 length => 32 is guaranteed
112
-
113
- return makePrivateKeySigner(keyBytes)
114
- } else if (signer instanceof Uint8Array) {
115
- assertBytes(signer, 32)
116
-
117
- return makePrivateKeySigner(signer)
118
- }
119
-
120
- assertSigner(signer)
121
-
122
- return signer
123
- }
124
-
125
- export async function sign(signer: Signer, data: Uint8Array): Promise<Signature> {
126
- const result = await signer.sign(wrapBytesWithHelpers(data))
127
-
128
- if (typeof result === 'string') {
129
- const hexString = makeHexString(result, SIGNATURE_HEX_LENGTH)
130
-
131
- return hexToBytes<65>(hexString)
132
- }
133
-
134
- if (result instanceof Uint8Array) {
135
- assertBytes(result, SIGNATURE_BYTES_LENGTH)
136
-
137
- return result
138
- }
139
-
140
- throw new TypeError('Invalid output of sign function!')
141
- }
package/src/chunk/soc.ts DELETED
@@ -1,190 +0,0 @@
1
- import { Bytes, 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 {
9
- Chunk,
10
- ChunkAddress,
11
- makeContentAddressedChunk,
12
- MAX_PAYLOAD_SIZE,
13
- MIN_PAYLOAD_SIZE,
14
- assertValidChunkData,
15
- } from './cac'
16
- import { UploadOptions, Signature, Signer, BatchId, Reference } from '../types'
17
- import { bytesToHex } from '../utils/hex'
18
- import * as socAPI from '../modules/soc'
19
- import * as chunkAPI from '../modules/chunk'
20
- import { EthAddress } from '../utils/eth'
21
- import { assertAddress } from '../utils/type'
22
-
23
- const IDENTIFIER_SIZE = 32
24
- const SIGNATURE_SIZE = 65
25
-
26
- const SOC_IDENTIFIER_OFFSET = 0
27
- const SOC_SIGNATURE_OFFSET = SOC_IDENTIFIER_OFFSET + IDENTIFIER_SIZE
28
- const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + SIGNATURE_SIZE
29
- const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + SPAN_SIZE
30
-
31
- export type Identifier = Bytes<32>
32
-
33
- /**
34
- * With single owner chunks, a user can assign arbitrary data to an
35
- * address and attest chunk integrity with their digital signature.
36
- *
37
- * This interface extends the Chunk interface so it has the same
38
- * properties, but the address calculation is based on the identifier
39
- * and the owner of the chunk.
40
- */
41
- export interface SingleOwnerChunk extends Chunk {
42
- identifier: () => Identifier
43
- signature: () => Signature
44
- owner: () => EthAddress
45
- }
46
-
47
- function recoverChunkOwner(data: Uint8Array): EthAddress {
48
- const cacData = data.slice(SOC_SPAN_OFFSET)
49
- const chunkAddress = bmtHash(cacData)
50
- const signature = bytesAtOffset(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE)
51
- const identifier = bytesAtOffset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE)
52
- const digest = keccak256Hash(identifier, chunkAddress)
53
- const ownerAddress = recoverAddress(signature, digest)
54
-
55
- return ownerAddress
56
- }
57
-
58
- /**
59
- * Verifies if the data is a valid single owner chunk
60
- *
61
- * @param data The chunk data
62
- * @param address The address of the single owner chunk
63
- *
64
- * @returns a single owner chunk or throws error
65
- */
66
- export function makeSingleOwnerChunkFromData(data: Uint8Array, address: ChunkAddress): SingleOwnerChunk {
67
- const ownerAddress = recoverChunkOwner(data)
68
- const identifier = bytesAtOffset(data, SOC_IDENTIFIER_OFFSET, IDENTIFIER_SIZE)
69
- const socAddress = keccak256Hash(identifier, ownerAddress)
70
-
71
- if (!bytesEqual(address, socAddress)) {
72
- throw new BeeError('SOC Data does not match given address!')
73
- }
74
-
75
- const signature = () => bytesAtOffset(data, SOC_SIGNATURE_OFFSET, SIGNATURE_SIZE)
76
- const span = () => bytesAtOffset(data, SOC_SPAN_OFFSET, SPAN_SIZE)
77
- const payload = () => flexBytesAtOffset(data, SOC_PAYLOAD_OFFSET, MIN_PAYLOAD_SIZE, MAX_PAYLOAD_SIZE)
78
-
79
- return {
80
- data,
81
- identifier: () => identifier,
82
- signature,
83
- span,
84
- payload,
85
- address: () => socAddress,
86
- owner: () => ownerAddress,
87
- }
88
- }
89
-
90
- export function makeSOCAddress(identifier: Identifier, address: EthAddress): ChunkAddress {
91
- return keccak256Hash(identifier, address)
92
- }
93
-
94
- /**
95
- * Creates a single owner chunk object
96
- *
97
- * @param chunk A chunk object used for the span and payload
98
- * @param identifier The identifier of the chunk
99
- * @param signer The singer interface for signing the chunk
100
- */
101
- export async function makeSingleOwnerChunk(
102
- chunk: Chunk,
103
- identifier: Identifier,
104
- signer: Signer,
105
- ): Promise<SingleOwnerChunk> {
106
- const chunkAddress = chunk.address()
107
- assertValidChunkData(chunk.data, chunkAddress)
108
-
109
- const digest = keccak256Hash(identifier, chunkAddress)
110
- const signature = await sign(signer, digest)
111
- const data = serializeBytes(identifier, signature, chunk.span(), chunk.payload())
112
- const address = makeSOCAddress(identifier, signer.address)
113
-
114
- return {
115
- data,
116
- identifier: () => identifier,
117
- signature: () => signature,
118
- span: () => chunk.span(),
119
- payload: () => chunk.payload(),
120
- address: () => address,
121
- owner: () => signer.address,
122
- }
123
- }
124
-
125
- /**
126
- * Helper function to upload a chunk.
127
- *
128
- * It uses the Chunk API and calculates the address before uploading.
129
- *
130
- * @param url The url of the Bee service
131
- * @param chunk A chunk object
132
- * @param postageBatchId Postage BatchId that will be assigned to uploaded data
133
- * @param options Upload options
134
- */
135
- export async function uploadSingleOwnerChunk(
136
- url: string,
137
- chunk: SingleOwnerChunk,
138
- postageBatchId: BatchId,
139
- options?: UploadOptions,
140
- ): Promise<Reference> {
141
- const owner = bytesToHex(chunk.owner())
142
- const identifier = bytesToHex(chunk.identifier())
143
- const signature = bytesToHex(chunk.signature())
144
- const data = serializeBytes(chunk.span(), chunk.payload())
145
-
146
- return socAPI.upload(url, owner, identifier, signature, data, postageBatchId, options)
147
- }
148
-
149
- /**
150
- * Helper function to create and upload SOC.
151
- *
152
- * @param url The url of the Bee service
153
- * @param signer The singer interface for signing the chunk
154
- * @param postageBatchId
155
- * @param identifier The identifier of the chunk
156
- * @param data The chunk data
157
- * @param options
158
- */
159
- export async function uploadSingleOwnerChunkData(
160
- url: string,
161
- signer: Signer,
162
- postageBatchId: BatchId | string,
163
- identifier: Identifier,
164
- data: Uint8Array,
165
- options?: UploadOptions,
166
- ): Promise<Reference> {
167
- assertAddress(postageBatchId)
168
- const cac = makeContentAddressedChunk(data)
169
- const soc = await makeSingleOwnerChunk(cac, identifier, signer)
170
-
171
- return uploadSingleOwnerChunk(url, soc, postageBatchId, options)
172
- }
173
-
174
- /**
175
- * Helper function to download SOC.
176
- *
177
- * @param url The url of the Bee service
178
- * @param ownerAddress The singer interface for signing the chunk
179
- * @param identifier The identifier of the chunk
180
- */
181
- export async function downloadSingleOwnerChunk(
182
- url: string,
183
- ownerAddress: EthAddress,
184
- identifier: Identifier,
185
- ): Promise<SingleOwnerChunk> {
186
- const address = makeSOCAddress(identifier, ownerAddress)
187
- const data = await chunkAPI.download(url, bytesToHex(address))
188
-
189
- return makeSingleOwnerChunkFromData(data, address)
190
- }
package/src/chunk/span.ts DELETED
@@ -1,33 +0,0 @@
1
- import { BeeArgumentError } from '../../src/utils/error'
2
- import { Bytes } from '../utils/bytes'
3
-
4
- export const SPAN_SIZE = 8
5
-
6
- // we limit the maximum span size in 32 bits to avoid BigInt compatibility issues
7
- const MAX_SPAN_LENGTH = 2 ** 32 - 1
8
-
9
- /**
10
- * Create a span for storing the length of the chunk
11
- *
12
- * The length is encoded in 64-bit little endian.
13
- *
14
- * @param length The length of the span
15
- */
16
- export function makeSpan(length: number): Bytes<8> {
17
- if (length <= 0) {
18
- throw new BeeArgumentError('invalid length for span', length)
19
- }
20
-
21
- if (length > MAX_SPAN_LENGTH) {
22
- throw new BeeArgumentError('invalid length (> MAX_SPAN_LENGTH)', length)
23
- }
24
-
25
- const span = new Uint8Array(SPAN_SIZE)
26
- const dataView = new DataView(span.buffer)
27
- const littleEndian = true
28
- const lengthLower32 = length & 0xffffffff
29
-
30
- dataView.setUint32(0, lengthLower32, littleEndian)
31
-
32
- return span as Bytes<8>
33
- }