Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove multiformats dependency #960

Merged
merged 7 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Config } from '@jest/types'
import { BeeRequestOptions } from './src'
import { createPostageBatch } from './src/modules/debug/stamps'

import { DEFAULT_BATCH_AMOUNT } from './test/utils'
import { DEFAULT_BATCH_AMOUNT, DEFAULT_BATCH_DEPTH } from './test/utils'

export default async (): Promise<Config.InitialOptions> => {
try {
Expand Down Expand Up @@ -34,7 +34,7 @@ export default async (): Promise<Config.InitialOptions> => {

const stamps = await Promise.all(
stampsOrder.map(async order =>
createPostageBatch(order.requestOptions, DEFAULT_BATCH_AMOUNT, 20, {
createPostageBatch(order.requestOptions, DEFAULT_BATCH_AMOUNT, DEFAULT_BATCH_DEPTH, {
waitForUsable: true,
}),
),
Expand Down Expand Up @@ -83,6 +83,6 @@ export default async (): Promise<Config.InitialOptions> => {
testPathIgnorePatterns: ['/node_modules/'],

// Increase timeout since we have long running cryptographic functions
testTimeout: 8 * 60 * 1000,
testTimeout: 9 * 60 * 1000,
}
}
37 changes: 4 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@
"bee": "npx @fairdatasociety/[email protected] start --detach --fresh"
},
"dependencies": {
"@ethersphere/swarm-cid": "^0.1.0",
"axios": "^0.28.1",
"cafe-utility": "^23.7.0",
"cafe-utility": "^23.10.0",
"elliptic": "^6.5.4",
"isomorphic-ws": "^4.0.1",
"js-sha3": "^0.8.0",
Expand All @@ -79,7 +78,6 @@
"@babel/preset-typescript": "^7.18.6",
"@commitlint/cli": "^17.0.2",
"@commitlint/config-conventional": "^17.4.2",
"@fluffy-spoon/substitute": "^1.208.0",
"@jest/types": "^29.6.3",
"@naholyr/cross-env": "^1.0.0",
"@types/elliptic": "^6.4.14",
Expand Down
19 changes: 9 additions & 10 deletions src/bee.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ReferenceType } from '@ethersphere/swarm-cid'
import { Objects, System } from 'cafe-utility'
import { Readable } from 'stream'
import { makeSigner } from './chunk/signer'
Expand Down Expand Up @@ -435,10 +434,10 @@
fileName,
fileOptions,
),
ReferenceType.MANIFEST,
'manifest',
)
} else if (isReadable(data) && options?.tag && !options.size) {
// TODO: Needed until https://github.com/ethersphere/bee/issues/2317 is resolved

Check warning on line 440 in src/bee.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Unexpected 'todo' comment: 'TODO: Needed until...'
const result = await bzz.uploadFile(
this.getRequestOptionsForCall(requestOptions),
data,
Expand All @@ -448,11 +447,11 @@
)
await this.updateTag(options.tag, result.reference)

return addCidConversionFunction(result, ReferenceType.MANIFEST)
return addCidConversionFunction(result, 'manifest')
} else {
return addCidConversionFunction(
await bzz.uploadFile(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, name, options),
ReferenceType.MANIFEST,
'manifest',
)
}
}
Expand All @@ -475,7 +474,7 @@
options?: BeeRequestOptions,
): Promise<FileData<Data>> {
assertRequestOptions(options)
reference = makeReferenceOrEns(reference, ReferenceType.MANIFEST)
reference = makeReferenceOrEns(reference, 'manifest')

return bzz.downloadFile(this.getRequestOptionsForCall(options), reference, path)
}
Expand All @@ -498,7 +497,7 @@
options?: BeeRequestOptions,
): Promise<FileData<ReadableStream<Uint8Array>>> {
assertRequestOptions(options)
reference = makeReferenceOrEns(reference, ReferenceType.MANIFEST)
reference = makeReferenceOrEns(reference, 'manifest')

return bzz.downloadFileReadable(this.getRequestOptionsForCall(options), reference, path)
}
Expand Down Expand Up @@ -536,7 +535,7 @@

return addCidConversionFunction(
await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options),
ReferenceType.MANIFEST,
'manifest',
)
}

Expand Down Expand Up @@ -569,7 +568,7 @@
postageBatchId,
options,
),
ReferenceType.MANIFEST,
'manifest',
)
}

Expand Down Expand Up @@ -603,7 +602,7 @@

return addCidConversionFunction(
await bzz.uploadCollection(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options),
ReferenceType.MANIFEST,
'manifest',
)
}

Expand Down Expand Up @@ -827,7 +826,7 @@
await this.makeFeedReader(type, canonicalTopic, canonicalOwner).download()

return true
} catch (e: any) {

Check warning on line 829 in src/bee.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Unexpected any. Specify a different type
if (e?.status === 404 || e?.status === 500) {
return false
}
Expand Down Expand Up @@ -1049,7 +1048,7 @@
stamp,
)

return addCidConversionFunction({ reference }, ReferenceType.FEED)
return addCidConversionFunction({ reference }, 'feed')
}

/**
Expand Down Expand Up @@ -1931,7 +1930,7 @@
if (stamp.usable) {
return
}
} catch (error: any) {}

Check warning on line 1933 in src/bee.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Unexpected any. Specify a different type

Check warning on line 1933 in src/bee.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Empty block statement

await System.sleepMillis(TIME_STEP)
}
Expand Down
3 changes: 0 additions & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export interface UploadResultWithCid extends UploadResult {
* Function that converts the reference into Swarm CIDs
*
* @throws TypeError if the reference is encrypted reference (eq. 128 chars long) which is not supported in CID
* @see https://github.com/ethersphere/swarm-cid-js
*/
cid: () => string
}
Expand Down Expand Up @@ -449,8 +448,6 @@ export interface FeedManifestResult {

/**
* Function that converts the reference into Swarm Feed CID.
*
* @see https://github.com/ethersphere/swarm-cid-js
*/
cid: () => string
}
Expand Down
49 changes: 49 additions & 0 deletions src/utils/cid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Binary } from 'cafe-utility'
import { Reference } from '..'

export const SWARM_MANIFEST_CODEC = 0xfa
export const SWARM_FEED_CODEC = 0xfb

const CODEC_TABLE: Record<number, string | undefined> = {
[SWARM_MANIFEST_CODEC]: 'manifest',
[SWARM_FEED_CODEC]: 'feed',
}

export type DecodedCID = {
type: 'feed' | 'manifest'
reference: Reference
}

export function convertReferenceToCid(reference: Reference | string, type: 'feed' | 'manifest'): string {
if (reference.length !== 64) {
throw TypeError('Only 32-byte, non-encrypted references are supported')
}
const base32 = 'b'
const version = new Uint8Array([1])
const codec = new Uint8Array([type === 'feed' ? SWARM_FEED_CODEC : SWARM_MANIFEST_CODEC])
const unknown = new Uint8Array([1])
const sha256 = new Uint8Array([27])
const size = new Uint8Array([32])
const header = Binary.uint8ArrayToBase32(Binary.concatBytes(version, codec, unknown, sha256, size)).replace(
/\=+$/,
'',
)
const hash = Binary.uint8ArrayToBase32(Binary.hexToUint8Array(reference)).replace(/\=+$/, '')

return `${base32}${header}${hash}`.toLowerCase()
}

export function convertCidToReference(cid: string): DecodedCID {
const bytes = Binary.base32ToUint8Array(cid.toLowerCase())
const codec = bytes[2]

if (!CODEC_TABLE[codec]) {
throw new Error('Unknown codec')
}
const reference = bytes.slice(-32)

return {
type: CODEC_TABLE[codec] as 'feed' | 'manifest',
reference: Binary.uint8ArrayToHex(reference) as Reference,
}
}
12 changes: 6 additions & 6 deletions src/utils/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { decodeCid, encodeReference, ReferenceType } from '@ethersphere/swarm-cid'
import { Readable } from 'stream'
import {
Address,
Expand Down Expand Up @@ -28,6 +27,7 @@ import {
TransactionOptions,
UploadOptions,
} from '../types'
import { convertCidToReference, convertReferenceToCid } from './cid'
import { BeeArgumentError, BeeError } from './error'
import { isFile } from './file'
import { assertHexString, assertPrefixedHexString, isHexString } from './hex'
Expand Down Expand Up @@ -158,13 +158,13 @@ export function assertReferenceOrEns(value: unknown): asserts value is Reference
* @param value
* @param expectedCidType
*/
export function makeReferenceOrEns(value: unknown, expectedCidType: ReferenceType): ReferenceOrEns {
export function makeReferenceOrEns(value: unknown, expectedCidType: 'feed' | 'manifest'): ReferenceOrEns {
if (typeof value !== 'string') {
throw new TypeError('ReferenceCidOrEns has to be a string!')
}

try {
const result = decodeCid(value)
const result = convertCidToReference(value)

if (result.type !== expectedCidType) {
throw new BeeError(
Expand All @@ -185,16 +185,16 @@ export function makeReferenceOrEns(value: unknown, expectedCidType: ReferenceTyp
/**
* Function that adds getter which converts the reference into CID base32 encoded string.
* @param result
* @param cidType Type as described in the @ethersphere/swarm-cids-js -> ReferenceType
* @param cidType feed or manifest
*/
export function addCidConversionFunction<T extends { reference: string }>(
result: T,
cidType: ReferenceType,
cidType: 'feed' | 'manifest',
): T & { cid: () => string } {
return {
...result,
cid() {
return encodeReference(result.reference, cidType).toString()
return convertReferenceToCid(result.reference, cidType)
},
}
}
Expand Down
27 changes: 9 additions & 18 deletions test/unit/feed/json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Arg, Substitute } from '@fluffy-spoon/substitute'
import { AnyJson, Bee, FeedWriter, Reference } from '../../../src'
import { AnyJson, Reference } from '../../../src'
import { getJsonData, setJsonData } from '../../../src/feed/json'
import { FetchFeedUpdateResponse } from '../../../src/modules/feed'
import { wrapBytesWithHelpers } from '../../../src/utils/bytes'
Expand All @@ -19,27 +18,19 @@ describe('JsonFeed', () => {

function testSet(data: unknown, expectedBytes: Uint8Array): void {
it(`should set feed for data: ${data}`, async () => {
const bee = Substitute.for<Bee>()
bee.uploadData(Arg.all()).resolves({ reference: DATA_REFERENCE, tagUid: 0, historyAddress: '00'.repeat(32) })

const writer = Substitute.for<FeedWriter>()
writer.upload(Arg.all()).resolves({ reference: FEED_REFERENCE_HASH, historyAddress: '00'.repeat(32) })
const bee = {
uploadData: () => ({ reference: DATA_REFERENCE, tagUid: 0, historyAddress: '00'.repeat(32) }),
} as any
const writer = { upload: () => ({ reference: FEED_REFERENCE_HASH, historyAddress: '00'.repeat(32) }) } as any

expect((await setJsonData(bee, writer, testAddress, data as AnyJson)).reference).toBe(FEED_REFERENCE_HASH)
bee.received(1).uploadData(testAddress, expectedBytes)
writer.received(1).upload(testAddress, DATA_REFERENCE, { pin: undefined })
})

it(`should get feed for data: ${data}`, async () => {
const bee = Substitute.for<Bee>()
bee.downloadData(Arg.all()).resolves(wrapBytesWithHelpers(expectedBytes))

const writer = Substitute.for<FeedWriter>()
writer.download().resolves(FEED_REFERENCE)
const bee = { downloadData: () => wrapBytesWithHelpers(expectedBytes) } as any
const writer = { download: () => FEED_REFERENCE } as any

expect(await getJsonData(bee, writer)).toStrictEqual(data)
bee.received(1).downloadData(FEED_REFERENCE_HASH)
writer.received(1).download()
})
}

Expand All @@ -60,8 +51,8 @@ describe('JsonFeed', () => {
)

it(`should fail for non-serializable data`, async () => {
const bee = Substitute.for<Bee>()
const writer = Substitute.for<FeedWriter>()
const bee = {} as any
const writer = {} as any
await expect(setJsonData(bee, writer, testAddress, BigInt(123) as unknown as AnyJson)).rejects.toThrow(TypeError)

const circularReference: CircularReference = { otherData: 123 }
Expand Down
Loading
Loading