@ethersphere/bee-js 2.1.0 → 3.2.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 (160) hide show
  1. package/README.md +3 -3
  2. package/dist/index.browser.min.js +1 -1
  3. package/dist/index.browser.min.js.map +1 -1
  4. package/dist/index.min.js +1 -1
  5. package/dist/index.min.js.map +1 -1
  6. package/dist/src/bee-debug.d.ts +5 -1
  7. package/dist/src/bee.d.ts +23 -46
  8. package/dist/src/modules/chunk.d.ts +2 -10
  9. package/dist/src/modules/debug/status.d.ts +10 -4
  10. package/dist/src/types/debug.d.ts +19 -0
  11. package/dist/src/types/index.d.ts +31 -2
  12. package/dist/src/utils/bytes.d.ts +1 -1
  13. package/dist/src/utils/error.d.ts +21 -2
  14. package/dist/src/utils/expose.d.ts +1 -0
  15. package/dist/src/utils/http.d.ts +7 -9
  16. package/dist/src/utils/stamps.d.ts +12 -0
  17. package/dist/src/utils/type.d.ts +6 -0
  18. package/package.json +28 -27
  19. package/.babelrc.js +0 -33
  20. package/.depcheckrc.json +0 -27
  21. package/.editorconfig +0 -6
  22. package/.eslintignore +0 -1
  23. package/.eslintrc.js +0 -109
  24. package/.gitattributes +0 -1
  25. package/.github/config.yaml +0 -23
  26. package/.github/workflows/check.yaml +0 -57
  27. package/.github/workflows/publish_npmjs.yaml +0 -20
  28. package/.github/workflows/release_github.yaml +0 -20
  29. package/.github/workflows/tests.yaml +0 -128
  30. package/.huskyrc +0 -5
  31. package/.prettierrc +0 -13
  32. package/.vscode/launch.json +0 -49
  33. package/.vscode/settings.json +0 -39
  34. package/CHANGELOG.md +0 -520
  35. package/CODEOWNERS +0 -1
  36. package/CODE_OF_CONDUCT.md +0 -76
  37. package/commitlint.config.js +0 -3
  38. package/dist/src/modules/stamps.d.ts +0 -4
  39. package/jest-puppeteer.config.js +0 -5
  40. package/jest.config.ts +0 -85
  41. package/src/bee-debug.ts +0 -601
  42. package/src/bee.ts +0 -1087
  43. package/src/chunk/bmt.ts +0 -55
  44. package/src/chunk/cac.ts +0 -79
  45. package/src/chunk/serialize.ts +0 -16
  46. package/src/chunk/signer.ts +0 -141
  47. package/src/chunk/soc.ts +0 -190
  48. package/src/chunk/span.ts +0 -33
  49. package/src/feed/index.ts +0 -217
  50. package/src/feed/json.ts +0 -33
  51. package/src/feed/topic.ts +0 -23
  52. package/src/feed/type.ts +0 -12
  53. package/src/index.ts +0 -24
  54. package/src/modules/bytes.ts +0 -70
  55. package/src/modules/bzz.ts +0 -179
  56. package/src/modules/chunk.ts +0 -69
  57. package/src/modules/debug/balance.ts +0 -63
  58. package/src/modules/debug/chequebook.ts +0 -175
  59. package/src/modules/debug/chunk.ts +0 -39
  60. package/src/modules/debug/connectivity.ts +0 -61
  61. package/src/modules/debug/settlements.ts +0 -33
  62. package/src/modules/debug/stamps.ts +0 -96
  63. package/src/modules/debug/states.ts +0 -35
  64. package/src/modules/debug/status.ts +0 -34
  65. package/src/modules/debug/tag.ts +0 -19
  66. package/src/modules/debug/transactions.ts +0 -79
  67. package/src/modules/feed.ts +0 -105
  68. package/src/modules/pinning.ts +0 -74
  69. package/src/modules/pss.ts +0 -49
  70. package/src/modules/soc.ts +0 -40
  71. package/src/modules/stamps.ts +0 -59
  72. package/src/modules/status.ts +0 -13
  73. package/src/modules/stewardship.ts +0 -32
  74. package/src/modules/tag.ts +0 -83
  75. package/src/types/debug.ts +0 -204
  76. package/src/types/index.ts +0 -561
  77. package/src/types/ky-universal/common.d.ts +0 -17
  78. package/src/types/ky-universal/hooks.d.ts +0 -103
  79. package/src/types/ky-universal/index.d.ts +0 -125
  80. package/src/types/ky-universal/options.d.ts +0 -235
  81. package/src/types/ky-universal/response.d.ts +0 -38
  82. package/src/types/ky-universal/retry.d.ts +0 -39
  83. package/src/types/tar-js.d.ts +0 -1
  84. package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
  85. package/src/utils/bytes.ts +0 -144
  86. package/src/utils/collection.ts +0 -151
  87. package/src/utils/data.browser.ts +0 -68
  88. package/src/utils/data.ts +0 -48
  89. package/src/utils/error.ts +0 -23
  90. package/src/utils/eth.ts +0 -229
  91. package/src/utils/expose.ts +0 -48
  92. package/src/utils/file.ts +0 -40
  93. package/src/utils/hash.ts +0 -16
  94. package/src/utils/headers.ts +0 -63
  95. package/src/utils/hex.ts +0 -186
  96. package/src/utils/http.ts +0 -141
  97. package/src/utils/merge.ts +0 -34
  98. package/src/utils/pss.ts +0 -16
  99. package/src/utils/stream.ts +0 -146
  100. package/src/utils/tar.ts +0 -30
  101. package/src/utils/type.ts +0 -394
  102. package/src/utils/uint64.ts +0 -31
  103. package/src/utils/url.ts +0 -52
  104. package/test/bee.sh +0 -187
  105. package/test/data/1.txt +0 -1
  106. package/test/data/2.txt +0 -0
  107. package/test/data/empty +0 -0
  108. package/test/data/sub/3.txt +0 -1
  109. package/test/data/sub//303/251 +0 -0
  110. package/test/data/sub//360/237/230/216 +0 -0
  111. package/test/integration/bee-class.browser.spec.ts +0 -272
  112. package/test/integration/bee-class.spec.ts +0 -648
  113. package/test/integration/bee-debug-class.spec.ts +0 -114
  114. package/test/integration/chunk/bmt.spec.ts +0 -33
  115. package/test/integration/chunk/cac.spec.ts +0 -27
  116. package/test/integration/chunk/soc.spec.ts +0 -38
  117. package/test/integration/feed/index.spec.ts +0 -84
  118. package/test/integration/modules/bytes.spec.ts +0 -23
  119. package/test/integration/modules/bzz.spec.ts +0 -265
  120. package/test/integration/modules/chunk.spec.ts +0 -29
  121. package/test/integration/modules/debug/balance.spec.ts +0 -70
  122. package/test/integration/modules/debug/chequebook.spec.ts +0 -64
  123. package/test/integration/modules/debug/connectivity.spec.ts +0 -69
  124. package/test/integration/modules/debug/settlements.spec.ts +0 -34
  125. package/test/integration/modules/debug/states.spec.ts +0 -34
  126. package/test/integration/modules/debug/status.spec.ts +0 -65
  127. package/test/integration/modules/debug/tag.spec.ts +0 -27
  128. package/test/integration/modules/debug/transactions.spec.ts +0 -10
  129. package/test/integration/modules/feed.spec.ts +0 -61
  130. package/test/integration/modules/pinning.spec.ts +0 -155
  131. package/test/integration/modules/pss.spec.ts +0 -105
  132. package/test/integration/modules/status.spec.ts +0 -10
  133. package/test/integration/modules/stewardship.spec.ts +0 -32
  134. package/test/integration/modules/tag.spec.ts +0 -51
  135. package/test/test-type-sequencer.js +0 -36
  136. package/test/testpage/testpage.html +0 -20
  137. package/test/tests-setup.ts +0 -94
  138. package/test/unit/assertions.ts +0 -460
  139. package/test/unit/bee-class.spec.ts +0 -976
  140. package/test/unit/bee-debug-class.spec.ts +0 -435
  141. package/test/unit/chunk/bmt.spec.ts +0 -16
  142. package/test/unit/chunk/cac.spec.ts +0 -29
  143. package/test/unit/chunk/serialize.spec.ts +0 -24
  144. package/test/unit/chunk/signer.spec.ts +0 -140
  145. package/test/unit/chunk/soc.spec.ts +0 -25
  146. package/test/unit/chunk/span.spec.ts +0 -38
  147. package/test/unit/feed/json.spec.ts +0 -73
  148. package/test/unit/nock.ts +0 -151
  149. package/test/unit/utils/bytes.spec.ts +0 -32
  150. package/test/unit/utils/collection.spec.ts +0 -22
  151. package/test/unit/utils/eth.spec.ts +0 -224
  152. package/test/unit/utils/hex.spec.ts +0 -115
  153. package/test/unit/utils/stream.spec.ts +0 -30
  154. package/test/unit/utils/type.spec.ts +0 -32
  155. package/test/unit/utils/uint64.spec.ts +0 -102
  156. package/test/utils.ts +0 -426
  157. package/tsconfig.json +0 -18
  158. package/tsconfig.test.json +0 -11
  159. package/typedoc.json +0 -7
  160. package/webpack.config.ts +0 -141
@@ -1,38 +0,0 @@
1
- import { makeSpan } from '../../../src/chunk/span'
2
- import { BeeArgumentError } from '../../../src/utils/error'
3
-
4
- describe('span', () => {
5
- it('should construct correct span', () => {
6
- const expected: [number, Uint8Array][] = [
7
- [2 ** 0, new Uint8Array([0x01, 0, 0, 0, 0, 0, 0, 0])],
8
- [2 ** 4, new Uint8Array([0x10, 0, 0, 0, 0, 0, 0, 0])],
9
- [2 ** 8, new Uint8Array([0, 0x01, 0, 0, 0, 0, 0, 0])],
10
- [2 ** 16, new Uint8Array([0, 0, 0x01, 0, 0, 0, 0, 0])],
11
- [2 ** 24, new Uint8Array([0, 0, 0, 0x01, 0, 0, 0, 0])],
12
- [2 ** 32 - 1, new Uint8Array([0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0])],
13
- ]
14
-
15
- expected.forEach(elem => {
16
- const result = makeSpan(elem[0])
17
- expect(result).toEqual(elem[1])
18
- })
19
- })
20
-
21
- it('should throw error with negative length', () => {
22
- const length = -1
23
- const t = () => makeSpan(length)
24
- expect(t).toThrow(BeeArgumentError)
25
- })
26
-
27
- it('should throw error with zero length', () => {
28
- const length = 0
29
- const t = () => makeSpan(length)
30
- expect(t).toThrow(BeeArgumentError)
31
- })
32
-
33
- it('should throw error when it is too big', () => {
34
- const length = 2 ** 32
35
- const t = () => makeSpan(length)
36
- expect(t).toThrow(BeeArgumentError)
37
- })
38
- })
@@ -1,73 +0,0 @@
1
- import { Arg, Substitute } from '@fluffy-spoon/substitute'
2
- import { AnyJson, Bee, FeedWriter, Reference } from '../../../src'
3
- import { testAddress, testBatchId, testChunkHash } from '../../utils'
4
- import { getJsonData, setJsonData } from '../../../src/feed/json'
5
- import { FetchFeedUpdateResponse } from '../../../src/modules/feed'
6
- import { wrapBytesWithHelpers } from '../../../src/utils/bytes'
7
-
8
- interface CircularReference {
9
- otherData: 123
10
- myself?: CircularReference
11
- }
12
-
13
- describe('JsonFeed', () => {
14
- const DATA_REFERENCE = testChunkHash as Reference
15
- const FEED_REFERENCE_HASH = 'ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a1111' as Reference
16
- const FEED_REFERENCE = {
17
- reference: FEED_REFERENCE_HASH,
18
- } as FetchFeedUpdateResponse
19
-
20
- function testSet(data: unknown, expectedBytes: Uint8Array): void {
21
- it(`should set feed for data: ${data}`, async () => {
22
- const bee = Substitute.for<Bee>()
23
- bee.uploadData(Arg.all()).resolves({ reference: DATA_REFERENCE, tagUid: 0 })
24
-
25
- const writer = Substitute.for<FeedWriter>()
26
- writer.upload(Arg.all()).resolves(FEED_REFERENCE_HASH)
27
-
28
- await expect(setJsonData(bee, writer, testAddress, data as AnyJson)).resolves.toEqual(FEED_REFERENCE_HASH)
29
- bee.received(1).uploadData(testAddress, expectedBytes)
30
- writer.received(1).upload(testAddress, DATA_REFERENCE)
31
- })
32
-
33
- it(`should get feed for data: ${data}`, async () => {
34
- const bee = Substitute.for<Bee>()
35
- bee.downloadData(Arg.all()).resolves(wrapBytesWithHelpers(expectedBytes))
36
-
37
- const writer = Substitute.for<FeedWriter>()
38
- writer.download().resolves(FEED_REFERENCE)
39
-
40
- await expect(getJsonData(bee, writer)).resolves.toEqual(data)
41
- bee.received(1).downloadData(FEED_REFERENCE_HASH)
42
- writer.received(1).download()
43
- })
44
- }
45
-
46
- testSet('', Uint8Array.from([34, 34]))
47
- testSet('hello world', Uint8Array.from([34, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 34]))
48
- testSet(null, Uint8Array.from([110, 117, 108, 108]))
49
- testSet(true, Uint8Array.from([116, 114, 117, 101]))
50
- testSet(10, Uint8Array.from([49, 48]))
51
- testSet([], Uint8Array.from([91, 93]))
52
- testSet(
53
- [1, 'hello', null],
54
- Uint8Array.from([91, 49, 44, 34, 104, 101, 108, 108, 111, 34, 44, 110, 117, 108, 108, 93]),
55
- )
56
- // prettier-ignore
57
- testSet(
58
- { hello: 'world', from: null },
59
- Uint8Array.from([123, 34, 104, 101, 108, 108, 111, 34, 58, 34, 119, 111, 114, 108, 100, 34, 44, 34, 102, 114, 111, 109, 34, 58, 110, 117, 108, 108, 125]),
60
- )
61
-
62
- it(`should fail for non-serializable data`, async () => {
63
- const bee = Substitute.for<Bee>()
64
- const writer = Substitute.for<FeedWriter>()
65
- await expect(setJsonData(bee, writer, testAddress, BigInt(123) as unknown as AnyJson)).rejects.toThrow(TypeError)
66
-
67
- const circularReference: CircularReference = { otherData: 123 }
68
- circularReference.myself = circularReference
69
-
70
- // @ts-ignore: Circular references are detected with TS, so we have to ts-ignore to test it.
71
- await expect(setJsonData(bee, writer, testBatchId, circularReference)).rejects.toThrow(TypeError)
72
- })
73
- })
package/test/unit/nock.ts DELETED
@@ -1,151 +0,0 @@
1
- import nock, { RequestHeaderMatcher } from 'nock'
2
- import { DEFAULT_FEED_TYPE, FeedType } from '../../src/feed/type'
3
- import { Reference } from '../../src/types'
4
- import { HexEthAddress } from '../../src/utils/eth'
5
-
6
- export const MOCK_SERVER_URL = 'http://localhost:12345/'
7
-
8
- // Endpoints
9
- const FEED_ENDPOINT = '/feeds'
10
- const BZZ_ENDPOINT = '/bzz'
11
- const BYTES_ENDPOINT = '/bytes'
12
- const POSTAGE_ENDPOINT = '/stamps'
13
- const CHEQUEBOOK_ENDPOINT = '/chequebook'
14
-
15
- export function assertAllIsDone(): void {
16
- if (!nock.isDone()) {
17
- throw new Error('Some expected request was not performed!')
18
- }
19
- }
20
-
21
- export function fetchFeedUpdateMock(
22
- address: HexEthAddress | string,
23
- hashedTopic: string,
24
- type: FeedType = DEFAULT_FEED_TYPE,
25
- ): nock.Interceptor {
26
- return nock(MOCK_SERVER_URL)
27
- .defaultReplyHeaders({
28
- 'swarm-feed-index': '1',
29
- 'swarm-feed-index-next': '2',
30
- })
31
- .get(`${FEED_ENDPOINT}/${address}/${hashedTopic}?type=${type}`)
32
- }
33
-
34
- export function downloadDataMock(reference: Reference | string): nock.Interceptor {
35
- return nock(MOCK_SERVER_URL).get(`${BYTES_ENDPOINT}/${reference}`)
36
- }
37
-
38
- interface UploadOptions {
39
- name?: string
40
- tag?: number
41
- pin?: boolean
42
- encrypt?: boolean
43
- collection?: boolean
44
- indexDocument?: string
45
- errorDocument?: string
46
- }
47
-
48
- function camelCaseToDashCase(str: string) {
49
- return str.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`)
50
- }
51
-
52
- export function uploadFileMock(
53
- batchId: string,
54
- name?: string,
55
- options?: UploadOptions,
56
- extraHeaders?: Record<string, string>,
57
- ): nock.Interceptor {
58
- // Prefixes the options with `swarm-` so the object can be used for required headers
59
- const headers = Object.entries(options || {}).reduce<Record<string, string>>((prev, curr) => {
60
- prev[`swarm-${camelCaseToDashCase(curr[0])}`] = curr[1]
61
-
62
- return prev
63
- }, {})
64
-
65
- return nock(MOCK_SERVER_URL, { reqheaders: { 'swarm-postage-batch-id': batchId, ...headers, ...extraHeaders } })
66
- .defaultReplyHeaders({
67
- 'swarm-tag': '123',
68
- })
69
- .post(`${BZZ_ENDPOINT}`)
70
- .query({ name })
71
- }
72
-
73
- export function createPostageBatchMock(
74
- amount: string,
75
- depth: string,
76
- gasPrice?: string,
77
- label?: string,
78
- immutableFlag?: string,
79
- ): nock.Interceptor {
80
- let nockScope: nock.Scope
81
-
82
- const reqheaders: Record<string, RequestHeaderMatcher> = {}
83
-
84
- if (immutableFlag) {
85
- reqheaders.immutable = immutableFlag
86
- }
87
-
88
- if (gasPrice) {
89
- reqheaders['gas-price'] = gasPrice
90
- }
91
-
92
- if (immutableFlag || gasPrice) {
93
- nockScope = nock(MOCK_SERVER_URL, {
94
- reqheaders,
95
- })
96
- } else {
97
- nockScope = nock(MOCK_SERVER_URL)
98
- }
99
-
100
- const nockMock = nockScope.post(`${POSTAGE_ENDPOINT}/${amount}/${depth}`)
101
-
102
- if (label) {
103
- return nockMock.query({ label })
104
- } else {
105
- return nockMock
106
- }
107
- }
108
-
109
- export function cashoutLastChequeMock(peer: string, gasPrice?: string, gasLimit?: string): nock.Interceptor {
110
- const headers: Record<string, string> = {}
111
-
112
- if (gasPrice) {
113
- headers['gas-price'] = gasPrice
114
- }
115
-
116
- if (gasLimit) {
117
- headers['gas-limit'] = gasLimit
118
- }
119
-
120
- return nock(MOCK_SERVER_URL, {
121
- reqheaders: headers,
122
- }).post(`${CHEQUEBOOK_ENDPOINT}/cashout/${peer}`)
123
- }
124
-
125
- export function depositTokensMock(
126
- amount: string,
127
- gasPrice?: string,
128
- extraHeaders?: Record<string, string>,
129
- ): nock.Interceptor {
130
- const headers: Record<string, string> = {}
131
-
132
- if (gasPrice) {
133
- headers['gas-price'] = gasPrice
134
- }
135
-
136
- return nock(MOCK_SERVER_URL, {
137
- reqheaders: { ...headers, ...extraHeaders },
138
- }).post(`${CHEQUEBOOK_ENDPOINT}/deposit?amount=${amount}`)
139
- }
140
-
141
- export function withdrawTokensMock(amount: string, gasPrice?: string): nock.Interceptor {
142
- const headers: Record<string, string> = {}
143
-
144
- if (gasPrice) {
145
- headers['gas-price'] = gasPrice
146
- }
147
-
148
- return nock(MOCK_SERVER_URL, {
149
- reqheaders: headers,
150
- }).post(`${CHEQUEBOOK_ENDPOINT}/withdraw?amount=${amount}`)
151
- }
@@ -1,32 +0,0 @@
1
- import { wrapBytesWithHelpers } from '../../../src/utils/bytes'
2
-
3
- describe('bytes', () => {
4
- describe('wrapBytesWithHelpers', () => {
5
- it('should still be Uint8Array', () => {
6
- const dataA = wrapBytesWithHelpers(new Uint8Array([104, 101, 108, 108, 111, 32]))
7
- const dataB = wrapBytesWithHelpers(new Uint8Array([119, 111, 114, 108, 100]))
8
-
9
- expect(dataA.text()).toEqual('hello ')
10
- expect(dataB.text()).toEqual('world')
11
- expect(new TextDecoder().decode(new Uint8Array([...dataA, ...dataB]))).toEqual('hello world')
12
- })
13
-
14
- it('should convert to string', () => {
15
- const data = new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])
16
-
17
- expect(wrapBytesWithHelpers(data).text()).toEqual('hello world')
18
- expect(wrapBytesWithHelpers(new Uint8Array([])).text()).toEqual('')
19
- expect(() => wrapBytesWithHelpers(null as unknown as Uint8Array).text()).toThrowError()
20
- })
21
-
22
- it('should convert to json', () => {
23
- const data = new Uint8Array([123, 34, 104, 101, 108, 108, 111, 34, 58, 34, 119, 111, 114, 108, 100, 34, 125])
24
- expect(wrapBytesWithHelpers(data).json()).toEqual({ hello: 'world' })
25
- })
26
-
27
- it('should convert to hex', () => {
28
- const data = new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])
29
- expect(wrapBytesWithHelpers(data).hex()).toEqual('68656c6c6f20776f726c64')
30
- })
31
- })
32
- })
@@ -1,22 +0,0 @@
1
- import { Utils } from '../../../src'
2
-
3
- function createFakeFile(): Partial<File> {
4
- return {
5
- size: 32,
6
- }
7
- }
8
-
9
- describe('collection', () => {
10
- test('should calculate folder size', async () => {
11
- const size = await Utils.getFolderSize('./test/data')
12
-
13
- expect(size).toBeGreaterThan(1)
14
- })
15
-
16
- test('should calculate collection size', async () => {
17
- const files: File[] = [createFakeFile() as File]
18
- const size = Utils.getCollectionSize(files)
19
-
20
- expect(size).toBe(32)
21
- })
22
- })
@@ -1,224 +0,0 @@
1
- /* eslint @typescript-eslint/no-empty-function: 0 */
2
- import {
3
- makeEthereumWalletSigner,
4
- ethToSwarmAddress,
5
- fromLittleEndian,
6
- isHexEthAddress,
7
- JsonRPC,
8
- toLittleEndian,
9
- } from '../../../src/utils/eth'
10
- import { HexString, hexToBytes } from '../../../src/utils/hex'
11
- import { wrapBytesWithHelpers } from '../../../src/utils/bytes'
12
-
13
- describe('eth', () => {
14
- describe('isEthAddress', () => {
15
- const testValues = [
16
- { value: () => {}, result: false },
17
- { value: new Function(), result: false },
18
- { value: 'function', result: false },
19
- { value: {}, result: false },
20
- { value: '0xc6d9d2cd449a754c494264e1809c50e34d64562b', result: true },
21
- { value: '0xc6d9d2cd449a754c494264e1809c50e34d64562b'.toUpperCase(), result: true },
22
- { value: 'c6d9d2cd449a754c494264e1809c50e34d64562b', result: true },
23
- { value: 'c6d9d2cd449a754c494264e1809c50e34d64562b'.toUpperCase(), result: true },
24
- { value: 'E247A45c287191d435A8a5D72A7C8dc030451E9F', result: true },
25
- { value: '0xE247A45c287191d435A8a5D72A7C8dc030451E9F', result: true },
26
- { value: '0xE247a45c287191d435A8a5D72A7C8dc030451E9F', result: false },
27
- { value: '0xe247a45c287191d435a8a5d72a7c8dc030451e9f', result: true },
28
- { value: '0xE247A45C287191D435A8A5D72A7C8DC030451E9F', result: true },
29
- { value: '0XE247A45C287191D435A8A5D72A7C8DC030451E9F', result: true },
30
- { value: '1234567890123456789012345678901234567890', result: true },
31
- { value: 1234567890123456789012345678901234567890, result: false },
32
- ]
33
-
34
- testValues.forEach(({ value, result }) => {
35
- test(`should test if value ${value} is address: ${result}`, () => {
36
- expect(isHexEthAddress(value as unknown as string)).toBe(result)
37
- })
38
- })
39
- })
40
-
41
- describe('toLittleEndian', () => {
42
- const testValues = [
43
- { value: '1', result: '01' },
44
- { value: 1, result: '01' },
45
- { value: '123', result: '2301' },
46
- { value: '0123', result: '2301' },
47
- { value: '0123', result: '230100', pad: 6 },
48
- { value: '0x0123', result: '230100', pad: 6 },
49
- { value: 124, result: '7c' },
50
- { value: 28721856816, result: '30c1f4af06' },
51
- ]
52
-
53
- testValues.forEach(({ value, result, pad }) => {
54
- test(`should conver value ${value}${pad ? ` with pad ${pad}` : ''} to ${result}`, () => {
55
- expect(toLittleEndian(value, pad)).toBe(result)
56
- })
57
- })
58
-
59
- const wrongTestValues = [124.1, -1, () => {}, new Function(), Number.MAX_SAFE_INTEGER + 1]
60
-
61
- wrongTestValues.forEach(value =>
62
- test(`should throw for non string or positive int values: ${value}`, () => {
63
- expect(() => toLittleEndian(value as unknown as string)).toThrow()
64
- }),
65
- )
66
- })
67
-
68
- describe('fromLittleEndian', () => {
69
- const testValues = ['0123', '0x0123', 124, 28721856816]
70
-
71
- testValues.forEach(value => {
72
- test(`should conver value ${value} back and forth`, () => {
73
- // We are converting first time to make for the comparison value to be padded and of the same type
74
- const littleEndian1 = toLittleEndian(value)
75
- const bigEndian = fromLittleEndian(littleEndian1)
76
- const littleEndian2 = fromLittleEndian(bigEndian)
77
-
78
- expect(littleEndian1).toBe(littleEndian2)
79
- })
80
- })
81
-
82
- testValues.forEach(value => {
83
- test(`should conver value ${value} back and forth with padding`, () => {
84
- // We are converting first time to make for the comparison value to be padded and of the same type
85
- const littleEndian1 = toLittleEndian(value, 10)
86
- const bigEndian = fromLittleEndian(littleEndian1, 10)
87
- const littleEndian2 = fromLittleEndian(bigEndian, 10)
88
-
89
- expect(littleEndian1).toBe(littleEndian2)
90
- })
91
- })
92
- })
93
-
94
- describe('ethToSwarmAddress', () => {
95
- const testValues = [
96
- {
97
- value: '1815cac638d1525b47f848daf02b7953e4edd15c',
98
- result: 'b003840cac8f71dc3e6025dbccae613fd107dcb2fb187808b54cab92cfdd8299',
99
- },
100
- {
101
- value: '0x1815cac638d1525b47f848daf02b7953e4edd15c',
102
- result: 'b003840cac8f71dc3e6025dbccae613fd107dcb2fb187808b54cab92cfdd8299',
103
- },
104
- {
105
- value: '0737e7c2e82fac12ca9e2bae01bea910593300e6',
106
- result: 'ba898f4dd93c5b29dc2d9daff3ef3d183fa6b5bfac9c23d975c0ee1fb06fcad9',
107
- },
108
- {
109
- value: 'd26bc1715e933bd5f8fad16310042f13abc16159',
110
- result: '8e186467e0ed20b73667b5353210c0e650401cde0461c25c6b3e4a1f636b8cb8',
111
- },
112
- ]
113
-
114
- testValues.forEach(({ value, result }) => {
115
- test(`should create from ${value} to ${result}`, () => {
116
- expect(ethToSwarmAddress(value)).toBe(result)
117
- })
118
- })
119
-
120
- const wrongTestValues = [
121
- {
122
- address: '1815cac638d1525b47f848daf02b7953e4edd15cf',
123
- netId: 1,
124
- },
125
- {
126
- address: '1815cac638d1525b47f848daf02b7953e4edd15c',
127
- netId: 0,
128
- },
129
- {
130
- address: '1815cac638d1525b47f848daf02b7953e4edd15c',
131
- netId: Number.MAX_SAFE_INTEGER + 1,
132
- },
133
- {
134
- address: '1815cac638d1525b47f848daf02b7953e4edd15c',
135
- netId: () => {},
136
- },
137
- {
138
- address: () => {},
139
- netId: 1,
140
- },
141
- ]
142
-
143
- wrongTestValues.forEach((address, netId) =>
144
- test(`should throw for incorrect values address ${address} netId ${netId}`, () => {
145
- expect(() => ethToSwarmAddress(address as unknown as string, netId as unknown as number)).toThrow()
146
- }),
147
- )
148
- })
149
-
150
- describe('makeEthereumWalletSigner', () => {
151
- const dataToSignBytes = hexToBytes('2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' as HexString)
152
- const dataToSignWithHelpers = wrapBytesWithHelpers(dataToSignBytes)
153
- const expectedSignatureHex =
154
- '0x336d24afef78c5883b96ad9a62552a8db3d236105cb059ddd04dc49680869dc16234f6852c277087f025d4114c4fac6b40295ecffd1194a84cdb91bd571769491b' as HexString
155
-
156
- it('should detect valid interface', async () => {
157
- await expect(makeEthereumWalletSigner({})).rejects.toThrow()
158
- await expect(makeEthereumWalletSigner('' as unknown as JsonRPC)).rejects.toThrow(TypeError)
159
- await expect(makeEthereumWalletSigner(1 as unknown as JsonRPC)).rejects.toThrow(TypeError)
160
- await expect(makeEthereumWalletSigner(null as unknown as JsonRPC)).rejects.toThrow(TypeError)
161
- await expect(makeEthereumWalletSigner(undefined as unknown as JsonRPC)).rejects.toThrow(TypeError)
162
- })
163
-
164
- it('should request address if not specified', async () => {
165
- const providerMock = jest.fn()
166
- providerMock.mockReturnValue(['0xf1B07aC6E91A423d9c3c834cc9d938E89E19334a'])
167
-
168
- const signer = await makeEthereumWalletSigner({ request: providerMock } as JsonRPC)
169
-
170
- expect(signer.address).toEqual(hexToBytes('f1B07aC6E91A423d9c3c834cc9d938E89E19334a'))
171
- expect(providerMock.mock.calls.length).toEqual(1)
172
- expect(providerMock.mock.calls[0][0]).toEqual({ method: 'eth_requestAccounts' })
173
- })
174
-
175
- it('should request signature when sign() is called', async () => {
176
- const providerMock = jest.fn()
177
- providerMock.mockReturnValue(expectedSignatureHex)
178
-
179
- const signer = await makeEthereumWalletSigner(
180
- { request: providerMock } as JsonRPC,
181
- '0xf1B07aC6E91A423d9c3c834cc9d938E89E19334a',
182
- )
183
- await expect(signer.sign(dataToSignWithHelpers)).resolves.toEqual(expectedSignatureHex)
184
- expect(providerMock.mock.calls.length).toEqual(1)
185
- expect(providerMock.mock.calls[0][0]).toEqual({
186
- jsonrpc: '2.0',
187
- method: 'personal_sign',
188
- params: [
189
- '0x2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae',
190
- '0xf1B07aC6E91A423d9c3c834cc9d938E89E19334a',
191
- ],
192
- })
193
- })
194
-
195
- it('should normalize hex prefix for address', async () => {
196
- const providerMock = jest.fn()
197
- providerMock.mockReturnValue(expectedSignatureHex)
198
-
199
- const signer = await makeEthereumWalletSigner(
200
- { request: providerMock } as JsonRPC,
201
- 'f1B07aC6E91A423d9c3c834cc9d938E89E19334a',
202
- )
203
- await expect(signer.sign(dataToSignWithHelpers)).resolves.toEqual(expectedSignatureHex)
204
- expect(providerMock.mock.calls.length).toEqual(1)
205
- expect(providerMock.mock.calls[0][0]).toEqual({
206
- jsonrpc: '2.0',
207
- method: 'personal_sign',
208
- params: [
209
- '0x2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae',
210
- '0xf1B07aC6E91A423d9c3c834cc9d938E89E19334a',
211
- ],
212
- })
213
- })
214
-
215
- it('should validate eth address', async () => {
216
- const providerMock = jest.fn()
217
- providerMock.mockReturnValue(expectedSignatureHex)
218
-
219
- await expect(
220
- makeEthereumWalletSigner({ request: providerMock } as JsonRPC, '0x307aC6E91A423d9c3c834cc9d938E89E19334a'),
221
- ).rejects.toThrow(TypeError)
222
- })
223
- })
224
- })
@@ -1,115 +0,0 @@
1
- import { bytesToHex, HexString, hexToBytes, intToHex, isHexString, makeHexString } from '../../../src/utils/hex'
2
-
3
- describe('hex', () => {
4
- // prettier-ignore
5
- const testBytes = new Uint8Array([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])
6
- const testHex = '00112233445566778899aabbccddeeff' as HexString
7
-
8
- describe('makeHexString', () => {
9
- describe('strings', () => {
10
- it('should strip prefix from valid prefixed string', () => {
11
- const input = '0xC0fFEE'
12
- const result = makeHexString(input)
13
-
14
- expect(result).toBe('C0fFEE')
15
- })
16
-
17
- it('should return valid non prefixed string', () => {
18
- const input = 'C0FFEE'
19
- const result = makeHexString(input)
20
-
21
- expect(result).toBe('C0FFEE')
22
- })
23
-
24
- it('should throw for other non valid strings', () => {
25
- expect(() => makeHexString('')).toThrowError(TypeError)
26
- expect(() => makeHexString('COFFEE')).toThrowError(TypeError)
27
- })
28
-
29
- it('should validate length if specified', () => {
30
- expect(makeHexString('C0fFEE', 6)).toBe('C0fFEE')
31
- expect(makeHexString('0xC0fFEE', 6)).toBe('C0fFEE')
32
- expect(() => makeHexString('C0fFEE', 5)).toThrowError(TypeError)
33
- expect(() => makeHexString('0xC0fFEE', 7)).toThrowError(TypeError)
34
- })
35
- })
36
- })
37
-
38
- describe('isHexString', () => {
39
- function testCase(input: unknown, result: boolean): void {
40
- it(`should ${result ? 'accept' : 'reject'} input: ${input}`, () => {
41
- expect(isHexString(input)).toEqual(result)
42
- })
43
- }
44
-
45
- testCase('C0FFEE', true)
46
- testCase('123C0FFEE', true)
47
- testCase('ZACOFFEE', false)
48
- testCase('', false)
49
- testCase(undefined, false)
50
- testCase(null, false)
51
- testCase(1, false)
52
- testCase({}, false)
53
- testCase([], false)
54
-
55
- it('should validate length if specified', () => {
56
- expect(isHexString('C0FFEE', 6)).toEqual(true)
57
- expect(isHexString('C0FFEE', 7)).toEqual(false)
58
- })
59
-
60
- it('chequebookaddress', () => {
61
- const input = '20d7855b548C71b69dA434D46187C336BDcef00F'
62
- const result = isHexString(input)
63
-
64
- expect(result).toBeTruthy()
65
- })
66
- })
67
-
68
- describe('hexToBytes', () => {
69
- it('converts hex to bytes', () => {
70
- const input = testHex
71
- const result = hexToBytes(input)
72
-
73
- expect(result).toEqual(testBytes)
74
- })
75
- })
76
-
77
- describe('bytesToHex', () => {
78
- it('converts bytes to hex', () => {
79
- const input = testBytes
80
- const result = bytesToHex(input)
81
-
82
- expect(result).toEqual(testHex)
83
- })
84
- })
85
-
86
- describe('intToHex', () => {
87
- const testValues = [
88
- { value: 1, result: '1' },
89
- { value: 1, result: '1', length: 1 },
90
- { value: 15, length: 2, throws: TypeError },
91
- { value: 16, result: '10', length: 2 },
92
- { value: 16, result: '10' },
93
- { value: 124, result: '7c' },
94
- { value: 28721856816, result: '6aff4c130' },
95
- { value: Number.MAX_SAFE_INTEGER, result: '1fffffffffffff' },
96
- { value: Number.MAX_SAFE_INTEGER + 1, throws: TypeError },
97
- { value: 124.1, throws: TypeError },
98
- { value: 'a', throws: TypeError },
99
- { value: '0', throws: TypeError },
100
- { value: -1, throws: TypeError },
101
- ]
102
-
103
- testValues.forEach(({ value, result, length, throws }) => {
104
- if (throws) {
105
- it(`should throw error for value ${value}`, () => {
106
- expect(() => intToHex(value as number, length)).toThrowError(throws)
107
- })
108
- } else {
109
- it(`should convert value ${value} to ${result}`, () => {
110
- expect(intToHex(value as number, length)).toBe(result)
111
- })
112
- }
113
- })
114
- })
115
- })
@@ -1,30 +0,0 @@
1
- import { Readable } from 'stream'
2
- import { readableNodeToWeb, readableWebToNode } from '../../../src/utils/stream'
3
- import { bytesEqual } from '../../../src/utils/bytes'
4
-
5
- async function getAllReadable(read: Readable): Promise<any[]> {
6
- const arr = []
7
-
8
- for await (const readElement of read) {
9
- arr.push(readElement)
10
- }
11
-
12
- return arr
13
- }
14
-
15
- describe('stream', () => {
16
- it('should convert from nodejs readable to whatwg readablestream and back', async () => {
17
- const input = [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6]), new Uint8Array([7, 8, 9])]
18
- const nodeJsReadable = Readable.from(input)
19
-
20
- const readableStream = readableNodeToWeb(nodeJsReadable)
21
- const nodeJsRevertedReadable = readableWebToNode(readableStream, { highWaterMark: 1 })
22
-
23
- const result = await getAllReadable(nodeJsRevertedReadable)
24
- expect(result.length).toEqual(input.length)
25
-
26
- for (let i = 0; i < input.length; i++) {
27
- expect(bytesEqual(result[i], input[i])).toBeTruthy()
28
- }
29
- })
30
- })