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

feat(act)!: add ACT to bee-js #942

Merged
merged 11 commits into from
Sep 13, 2024
69 changes: 67 additions & 2 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { DEFAULT_FEED_TYPE, FeedType, assertFeedType } from './feed/type'
import * as bytes from './modules/bytes'
import * as bzz from './modules/bzz'
import * as grantee from './modules/grantee'
import * as chunk from './modules/chunk'
import * as balance from './modules/debug/balance'
import * as chequebook from './modules/debug/chequebook'
Expand Down Expand Up @@ -81,6 +82,8 @@
UploadOptions,
UploadRedundancyOptions,
UploadResultWithCid,
GranteesResult,
GetGranteesResult,
WalletBalance,
} from './types'
import {
Expand Down Expand Up @@ -260,7 +263,7 @@
data: Uint8Array,
options?: UploadOptions,
requestOptions?: BeeRequestOptions,
): Promise<Reference> {
): Promise<UploadResult> {
assertBatchId(postageBatchId)

if (!(data instanceof Uint8Array)) {
Expand Down Expand Up @@ -297,6 +300,68 @@
return chunk.download(this.getRequestOptionsForCall(options), reference)
}

/**
* Create a grantees list from the given array of public keys.
*
* The grantees list can be obtained with the `getGrantees` method.
*
* @param postageBatchId - The ID of the postage batch.
* @param grantees - An array of public keys representing the grantees.
* @param requestOptions - Optional request options.
* @returns A promise that resolves to a `GranteesResult` object.
*/
async createGrantees(
postageBatchId: string | BatchId,
grantees: string[],
requestOptions?: BeeRequestOptions,
): Promise<GranteesResult> {
assertBatchId(postageBatchId)

return grantee.createGrantees(this.getRequestOptionsForCall(requestOptions), postageBatchId, grantees)
}

/**
* Retrieves the grantees for a given reference.
*
* @param reference - The reference.
* @param requestOptions - Optional request options.
* @returns A promise that resolves to a `GetGranteesResult object.
*/
async getGrantees(
reference: ReferenceOrEns | string,
requestOptions?: BeeRequestOptions,
): Promise<GetGranteesResult> {
return grantee.getGrantees(reference, this.getRequestOptionsForCall(requestOptions))
}

/**
* Updates the grantees of a specific reference and history.
*
* @param reference - The reference.
* @param history - The history.
* @param postageBatchId - The ID of the postage batch.
* @param grantees - The grantees.
* @param requestOptions - Optional request options.
* @returns A Promise that resolves to to a `GranteesResult` object.
*/
async patchGrantees(
reference: Reference | string,
histrory: Reference | string,
postageBatchId: string | BatchId,
grantees: string,
requestOptions?: BeeRequestOptions,
): Promise<GranteesResult> {
assertBatchId(postageBatchId)

return grantee.patchGrantees(
reference,
histrory,
postageBatchId,
grantees,
this.getRequestOptionsForCall(requestOptions),
)
}

/**
* Upload single file to a Bee node.
*
Expand Down Expand Up @@ -346,7 +411,7 @@
ReferenceType.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 414 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 Down Expand Up @@ -724,7 +789,7 @@
await this.makeFeedReader(type, canonicalTopic, canonicalOwner).download()

return true
} catch (e: any) {

Check warning on line 792 in src/bee.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Unexpected any. Specify a different type
if (e?.response?.status === 404) {
return false
}
Expand Down Expand Up @@ -1024,7 +1089,7 @@
data: T,
options?: JsonFeedOptions,
requestOptions?: BeeRequestOptions,
): Promise<Reference> {
): Promise<UploadResult> {
assertRequestOptions(options, 'JsonFeedOptions')
assertBatchId(postageBatchId)

Expand Down Expand Up @@ -1818,7 +1883,7 @@
if (stamp.usable) {
return
}
} catch (error: any) {}

Check warning on line 1886 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 1886 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
14 changes: 11 additions & 3 deletions src/chunk/soc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Binary } from 'cafe-utility'
import * as chunkAPI from '../modules/chunk'
import * as socAPI from '../modules/soc'
import { BatchId, BeeRequestOptions, PlainBytesReference, Reference, Signature, Signer, UploadOptions } from '../types'
import {
BatchId,
BeeRequestOptions,
PlainBytesReference,
Signature,
Signer,
UploadOptions,
UploadResult,
} from '../types'
import { Bytes, bytesAtOffset, bytesEqual, flexBytesAtOffset } from '../utils/bytes'
import { BeeError } from '../utils/error'
import { EthAddress } from '../utils/eth'
Expand Down Expand Up @@ -130,7 +138,7 @@ export async function uploadSingleOwnerChunk(
chunk: SingleOwnerChunk,
postageBatchId: BatchId,
options?: UploadOptions,
): Promise<Reference> {
): Promise<UploadResult> {
const owner = bytesToHex(chunk.owner())
const identifier = bytesToHex(chunk.identifier())
const signature = bytesToHex(chunk.signature())
Expand All @@ -156,7 +164,7 @@ export async function uploadSingleOwnerChunkData(
identifier: Identifier,
data: Uint8Array,
options?: UploadOptions,
): Promise<Reference> {
): Promise<UploadResult> {
assertAddress(postageBatchId)
const cac = makeContentAddressedChunk(data)
const soc = await makeSingleOwnerChunk(cac, identifier, signer)
Expand Down
3 changes: 2 additions & 1 deletion src/feed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Signer,
Topic,
UploadOptions,
UploadResult,
} from '../types'
import { Bytes, bytesAtOffset, makeBytes } from '../utils/bytes'
import { EthAddress, HexEthAddress, makeHexEthAddress } from '../utils/eth'
Expand Down Expand Up @@ -58,7 +59,7 @@
const feedUpdate = await fetchLatestFeedUpdate(requestOptions, owner, topic, options)

return makeHexString(feedUpdate.feedIndexNext, FEED_INDEX_HEX_LENGTH)
} catch (e: any) {

Check warning on line 62 in src/feed/index.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Unexpected any. Specify a different type
if (e?.response?.status === 404) {
return bytesToHex(makeBytes(8))
}
Expand All @@ -73,7 +74,7 @@
reference: BytesReference,
postageBatchId: BatchId,
options?: FeedUploadOptions,
): Promise<Reference> {
): Promise<UploadResult> {
const ownerHex = makeHexEthAddress(signer.address)
const nextIndex = options?.index ?? (await findNextIndex(requestOptions, ownerHex, topic, options))

Expand Down
4 changes: 2 additions & 2 deletions src/feed/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
FeedReader,
FeedWriter,
JsonFeedOptions,
Reference,
UploadOptions,
UploadResult,
} from '../types'
import { isError } from '../utils/type'

Expand Down Expand Up @@ -38,7 +38,7 @@ export async function setJsonData(
data: AnyJson,
options?: JsonFeedOptions & UploadOptions,
requestOptions?: BeeRequestOptions,
): Promise<Reference> {
): Promise<UploadResult> {
const serializedData = serializeJson(data)
const { reference } = await bee.uploadData(postageBatchId, serializedData, options, requestOptions)

Expand Down
1 change: 1 addition & 0 deletions src/modules/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export async function upload(
return {
reference: response.data.reference,
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
history_address: response.headers['swarm-act-history-address'] || '',
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/modules/bzz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export async function uploadFile(
return {
reference: response.data.reference,
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
history_address: response.headers['swarm-act-history-address'] || '',
}
}

Expand Down Expand Up @@ -180,5 +181,6 @@ export async function uploadCollection(
return {
reference: response.data.reference,
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
history_address: response.headers['swarm-act-history-address'] || '',
}
}
11 changes: 8 additions & 3 deletions src/modules/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import type {
BatchId,
BeeRequestOptions,
Data,
Reference,
ReferenceOrEns,
ReferenceResponse,
UploadOptions,
UploadResult,
} from '../types'
import { wrapBytesWithHelpers } from '../utils/bytes'
import { extractUploadHeaders } from '../utils/headers'
import { http } from '../utils/http'
import { makeTagUid } from '../utils/type'

const endpoint = 'chunks'

Expand All @@ -30,7 +31,7 @@ export async function upload(
data: Uint8Array,
postageBatchId: BatchId,
options?: UploadOptions,
): Promise<Reference> {
): Promise<UploadResult> {
const response = await http<ReferenceResponse>(requestOptions, {
method: 'post',
url: `${endpoint}`,
Expand All @@ -42,7 +43,11 @@ export async function upload(
responseType: 'json',
})

return response.data.reference
return {
reference: response.data.reference,
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
history_address: response.headers['swarm-act-history-address'] || '',
}
}

/**
Expand Down
68 changes: 68 additions & 0 deletions src/modules/grantee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { BatchId, BeeRequestOptions, GetGranteesResult, GranteesResult } from '../types'
import { extractRedundantUploadHeaders } from '../utils/headers'
import { http } from '../utils/http'

const granteeEndpoint = 'grantee'

export async function getGrantees(reference: string, requestOptions: BeeRequestOptions): Promise<GetGranteesResult> {
const response = await http<GetGranteesResult>(requestOptions, {
method: 'get',
url: `${granteeEndpoint}/${reference}`,
responseType: 'json',
})

return {
status: response.status,
statusText: response.statusText,
data: response.data.data,
}
}

export async function createGrantees(
requestOptions: BeeRequestOptions,
postageBatchId: BatchId,
grantees: string[],
): Promise<GranteesResult> {
const response = await http<GranteesResult>(requestOptions, {
method: 'post',
url: granteeEndpoint,
data: { grantees: grantees },
headers: {
...extractRedundantUploadHeaders(postageBatchId),
},
responseType: 'json',
})

return {
status: response.status,
statusText: response.statusText,
ref: response.data.ref,
historyref: response.data.historyref,
}
}

export async function patchGrantees(
reference: string,
historyRef: string,
postageBatchId: BatchId,
grantees: string,
requestOptions: BeeRequestOptions,
): Promise<GranteesResult> {
const response = await http<GranteesResult>(requestOptions, {
method: 'patch',
url: `${granteeEndpoint}/${reference}`,
data: grantees,
headers: {
...extractRedundantUploadHeaders(postageBatchId),
'swarm-act-history-address': historyRef,
},
responseType: 'json',
})

return {
status: response.status,
statusText: response.statusText,
ref: response.data.ref,
historyref: response.data.historyref,
}
}
11 changes: 8 additions & 3 deletions src/modules/soc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BatchId, BeeRequestOptions, Reference, ReferenceResponse, UploadOptions } from '../types'
import { BatchId, BeeRequestOptions, ReferenceResponse, UploadOptions, UploadResult } from '../types'
import { extractUploadHeaders } from '../utils/headers'
import { http } from '../utils/http'
import { makeTagUid } from '../utils/type'

const socEndpoint = 'soc'

Expand All @@ -23,7 +24,7 @@ export async function upload(
data: Uint8Array,
postageBatchId: BatchId,
options?: UploadOptions,
): Promise<Reference> {
): Promise<UploadResult> {
const response = await http<ReferenceResponse>(requestOptions, {
method: 'post',
url: `${socEndpoint}/${owner}/${identifier}`,
Expand All @@ -36,5 +37,9 @@ export async function upload(
params: { sig: signature },
})

return response.data.reference
return {
reference: response.data.reference,
tagUid: response.headers['swarm-tag'] ? makeTagUid(response.headers['swarm-tag']) : undefined,
history_address: response.headers['swarm-act-history-address'] || '',
}
}
Loading
Loading