@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
@@ -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(DATA_REFERENCE)
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,134 +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).get(`${FEED_ENDPOINT}/${address}/${hashedTopic}?type=${type}`)
27
- }
28
-
29
- export function downloadDataMock(reference: Reference | string): nock.Interceptor {
30
- return nock(MOCK_SERVER_URL).get(`${BYTES_ENDPOINT}/${reference}`)
31
- }
32
-
33
- interface UploadOptions {
34
- name?: string
35
- tag?: number
36
- pin?: boolean
37
- encrypt?: boolean
38
- collection?: boolean
39
- indexDocument?: string
40
- errorDocument?: string
41
- }
42
-
43
- function camelCaseToDashCase(str: string) {
44
- return str.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`)
45
- }
46
-
47
- export function uploadFileMock(batchId: string, name?: string, options?: UploadOptions): nock.Interceptor {
48
- // Prefixes the options with `swarm-` so the object can be used for required headers
49
- const headers = Object.entries(options || {}).reduce<Record<string, string>>((prev, curr) => {
50
- prev[`swarm-${camelCaseToDashCase(curr[0])}`] = curr[1]
51
-
52
- return prev
53
- }, {})
54
-
55
- return nock(MOCK_SERVER_URL, { reqheaders: { 'swarm-postage-batch-id': batchId, ...headers } })
56
- .post(`${BZZ_ENDPOINT}`)
57
- .query({ name })
58
- }
59
-
60
- export function createPostageBatchMock(
61
- amount: string,
62
- depth: string,
63
- gasPrice?: string,
64
- label?: string,
65
- immutableFlag?: string,
66
- ): nock.Interceptor {
67
- let nockScope: nock.Scope
68
-
69
- const reqheaders: Record<string, RequestHeaderMatcher> = {}
70
-
71
- if (immutableFlag) {
72
- reqheaders.immutable = immutableFlag
73
- }
74
-
75
- if (gasPrice) {
76
- reqheaders['gas-price'] = gasPrice
77
- }
78
-
79
- if (immutableFlag || gasPrice) {
80
- nockScope = nock(MOCK_SERVER_URL, {
81
- reqheaders,
82
- })
83
- } else {
84
- nockScope = nock(MOCK_SERVER_URL)
85
- }
86
-
87
- const nockMock = nockScope.post(`${POSTAGE_ENDPOINT}/${amount}/${depth}`)
88
-
89
- if (label) {
90
- return nockMock.query({ label })
91
- } else {
92
- return nockMock
93
- }
94
- }
95
-
96
- export function cashoutLastChequeMock(peer: string, gasPrice?: string, gasLimit?: string): nock.Interceptor {
97
- const headers: Record<string, string> = {}
98
-
99
- if (gasPrice) {
100
- headers['gas-price'] = gasPrice
101
- }
102
-
103
- if (gasLimit) {
104
- headers['gas-limit'] = gasLimit
105
- }
106
-
107
- return nock(MOCK_SERVER_URL, {
108
- reqheaders: headers,
109
- }).post(`${CHEQUEBOOK_ENDPOINT}/cashout/${peer}`)
110
- }
111
-
112
- export function depositTokensMock(amount: string, gasPrice?: string): nock.Interceptor {
113
- const headers: Record<string, string> = {}
114
-
115
- if (gasPrice) {
116
- headers['gas-price'] = gasPrice
117
- }
118
-
119
- return nock(MOCK_SERVER_URL, {
120
- reqheaders: headers,
121
- }).post(`${CHEQUEBOOK_ENDPOINT}/deposit?amount=${amount}`)
122
- }
123
-
124
- export function withdrawTokensMock(amount: string, gasPrice?: string): nock.Interceptor {
125
- const headers: Record<string, string> = {}
126
-
127
- if (gasPrice) {
128
- headers['gas-price'] = gasPrice
129
- }
130
-
131
- return nock(MOCK_SERVER_URL, {
132
- reqheaders: headers,
133
- }).post(`${CHEQUEBOOK_ENDPOINT}/withdraw?amount=${amount}`)
134
- }
@@ -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.Collections.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.Collections.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
- })