Skip to content

Commit

Permalink
feat: add envelope endpoint (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 authored Oct 24, 2024
1 parent 5434d66 commit 67ef011
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"dependencies": {
"@ethersphere/swarm-cid": "^0.1.0",
"axios": "^0.28.1",
"cafe-utility": "^21.3.1",
"cafe-utility": "^23.7.0",
"elliptic": "^6.5.4",
"isomorphic-ws": "^4.0.1",
"js-sha3": "^0.8.0",
Expand Down
11 changes: 11 additions & 0 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as states from './modules/debug/states'
import * as debugStatus from './modules/debug/status'
import * as debugTag from './modules/debug/tag'
import * as transactions from './modules/debug/transactions'
import { EnvelopeResponse, postEnvelope } from './modules/envelope'
import { createFeedManifest } from './modules/feed'
import * as grantee from './modules/grantee'
import * as pinning from './modules/pinning'
Expand Down Expand Up @@ -1225,6 +1226,16 @@ export class Bee {
}
}

async createEnvelope(
postageBatchId: BatchId,
reference: Reference,
options?: BeeRequestOptions,
): Promise<EnvelopeResponse> {
assertRequestOptions(options)

return postEnvelope(this.getRequestOptionsForCall(options), postageBatchId, reference)
}

/**
* Ping the Bee node to see if there is a live Bee node on the given URL.
*
Expand Down
34 changes: 34 additions & 0 deletions src/modules/envelope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Types } from 'cafe-utility'
import type { BatchId, BeeRequestOptions, Reference } from '../types'
import { http } from '../utils/http'

const ENVELOPE_ENDPOINT = 'envelope'

export interface EnvelopeResponse {
issuer: string
index: string
timestamp: string
signature: string
}

export async function postEnvelope(
requestOptions: BeeRequestOptions,
postageBatchId: BatchId,
reference: Reference,
): Promise<EnvelopeResponse> {
const response = await http<EnvelopeResponse>(requestOptions, {
method: 'post',
responseType: 'json',
url: `${ENVELOPE_ENDPOINT}/${reference}`,
headers: {
'swarm-postage-batch-id': postageBatchId,
},
})

return {
issuer: Types.asHexString(response.data.issuer, { name: 'issuer', byteLength: 20 }),
index: Types.asHexString(response.data.index, { name: 'index', byteLength: 8 }),
timestamp: Types.asHexString(response.data.timestamp, { name: 'timestamp', byteLength: 8 }),
signature: Types.asHexString(response.data.signature, { name: 'signature', byteLength: 65 }),
}
}
11 changes: 11 additions & 0 deletions test/integration/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,17 @@ describe('Bee class', () => {
})
})

describe('Envelope', () => {
it('should post envelope', async function () {
const uploadResult = await bee.uploadData(getPostageBatch(), 'Envelope')
const envelopeResult = await bee.createEnvelope(getPostageBatch(), uploadResult.reference)
expect(envelopeResult.index).toHaveLength(16)
expect(envelopeResult.issuer).toHaveLength(40)
expect(envelopeResult.signature).toHaveLength(130)
expect(envelopeResult.timestamp).toHaveLength(16)
})
})

describe('JsonFeed', () => {
it('should set JSON to feed', async function () {
const TOPIC = 'some=very%nice#topic'
Expand Down

0 comments on commit 67ef011

Please sign in to comment.