@upcoming/bee-js 0.12.0 → 0.14.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.
package/dist/mjs/bee.js CHANGED
@@ -1176,6 +1176,15 @@ export class Bee {
1176
1176
  const amount = getAmountForDuration(duration, chainState.currentPrice);
1177
1177
  return this.topUpBatch(batch.batchID, amount, options);
1178
1178
  }
1179
+ async getExtensionCost(postageBatchId, gigabytes, duration, options) {
1180
+ const batch = await this.getPostageBatch(postageBatchId, options);
1181
+ const chainState = await this.getChainState(options);
1182
+ const amount = getAmountForDuration(duration, chainState.currentPrice);
1183
+ const depth = getDepthForSize(gigabytes);
1184
+ const currentValue = getStampCost(batch.depth, batch.amount);
1185
+ const newValue = getStampCost(depth, amount);
1186
+ return newValue.minus(currentValue);
1187
+ }
1179
1188
  async getSizeExtensionCost(postageBatchId, gigabytes, options) {
1180
1189
  const batch = await this.getPostageBatch(postageBatchId, options);
1181
1190
  const depth = getDepthForSize(gigabytes);
@@ -25,4 +25,17 @@ export function makeContentAddressedChunk(payloadBytes) {
25
25
  payload: Bytes.fromSlice(data, Span.LENGTH),
26
26
  address: calculateChunkAddress(data)
27
27
  };
28
+ }
29
+ export function asContentAddressedChunk(chunkBytes) {
30
+ if (chunkBytes.length < MIN_PAYLOAD_SIZE + Span.LENGTH || chunkBytes.length > MAX_PAYLOAD_SIZE + Span.LENGTH) {
31
+ throw new RangeError(`chunk size ${chunkBytes.length} exceeds limits [${MIN_PAYLOAD_SIZE + Span.LENGTH}, ${Span.LENGTH}]`);
32
+ }
33
+ const span = Span.fromSlice(chunkBytes, 0);
34
+ const data = Binary.concatBytes(span.toUint8Array(), chunkBytes.slice(Span.LENGTH));
35
+ return {
36
+ data,
37
+ span,
38
+ payload: Bytes.fromSlice(data, Span.LENGTH),
39
+ address: calculateChunkAddress(data)
40
+ };
28
41
  }
@@ -5,7 +5,7 @@ import { Bytes } from "../utils/bytes.js";
5
5
  import { BeeError } from "../utils/error.js";
6
6
  import { EthAddress, Identifier, PrivateKey, Reference, Signature, Span } from "../utils/typed-bytes.js";
7
7
  import { calculateChunkAddress } from "./bmt.js";
8
- import { makeContentAddressedChunk } from "./cac.js";
8
+ import { asContentAddressedChunk, makeContentAddressedChunk } from "./cac.js";
9
9
  const SOC_SIGNATURE_OFFSET = Identifier.LENGTH;
10
10
  const SOC_SPAN_OFFSET = SOC_SIGNATURE_OFFSET + Signature.LENGTH;
11
11
  const SOC_PAYLOAD_OFFSET = SOC_SPAN_OFFSET + Span.LENGTH;
@@ -107,6 +107,12 @@ export async function uploadSingleOwnerChunkData(requestOptions, signer, stamp,
107
107
  const soc = makeSingleOwnerChunk(cac, identifier, signer);
108
108
  return uploadSingleOwnerChunk(requestOptions, soc, stamp, options);
109
109
  }
110
+ export async function uploadSingleOwnerChunkWithWrappedChunk(requestOptions, signer, stamp, identifier, rootChunk, options) {
111
+ signer = new PrivateKey(signer);
112
+ identifier = new Identifier(identifier);
113
+ const soc = makeSingleOwnerChunk(asContentAddressedChunk(rootChunk), identifier, signer);
114
+ return uploadSingleOwnerChunk(requestOptions, soc, stamp, options);
115
+ }
110
116
  /**
111
117
  * Helper function to download SOC.
112
118
  *
@@ -1,10 +1,13 @@
1
1
  import { Binary, Optional, Types } from 'cafe-utility';
2
- import { makeSingleOwnerChunkFromData, uploadSingleOwnerChunkData } from "../chunk/soc.js";
2
+ import { asContentAddressedChunk } from "../chunk/cac.js";
3
+ import { makeSingleOwnerChunkFromData, uploadSingleOwnerChunkData, uploadSingleOwnerChunkWithWrappedChunk } from "../chunk/soc.js";
4
+ import * as bytes from "../modules/bytes.js";
3
5
  import * as chunkAPI from "../modules/chunk.js";
4
6
  import { fetchLatestFeedUpdate, probeFeed } from "../modules/feed.js";
5
7
  import { Bytes } from "../utils/bytes.js";
6
8
  import { BeeResponseError } from "../utils/error.js";
7
- import { FeedIndex, Reference } from "../utils/typed-bytes.js";
9
+ import { ResourceLocator } from "../utils/resource-locator.js";
10
+ import { FeedIndex, Identifier, Reference, Signature } from "../utils/typed-bytes.js";
8
11
  import { makeFeedIdentifier } from "./identifier.js";
9
12
  const TIMESTAMP_PAYLOAD_OFFSET = 0;
10
13
  const TIMESTAMP_PAYLOAD_SIZE = 8;
@@ -35,6 +38,11 @@ export async function updateFeedWithReference(requestOptions, signer, topic, ref
35
38
  export async function updateFeedWithPayload(requestOptions, signer, topic, data, postageBatchId, options) {
36
39
  const nextIndex = options?.index ?? (await findNextIndex(requestOptions, signer.publicKey().address(), topic));
37
40
  const identifier = makeFeedIdentifier(topic, nextIndex);
41
+ if (data.length > 4096) {
42
+ const uploadResult = await bytes.upload(requestOptions, data, postageBatchId, options);
43
+ const rootChunk = await chunkAPI.download(requestOptions, uploadResult.reference);
44
+ return uploadSingleOwnerChunkWithWrappedChunk(requestOptions, signer, postageBatchId, identifier, rootChunk, options);
45
+ }
38
46
  return uploadSingleOwnerChunkData(requestOptions, signer, postageBatchId, identifier, Types.isString(data) ? Bytes.fromUtf8(data).toUint8Array() : data, options);
39
47
  }
40
48
  export function getFeedUpdateChunkReference(owner, topic, index) {
@@ -56,6 +64,12 @@ export async function downloadFeedUpdate(requestOptions, owner, topic, index, ha
56
64
  payload: new Bytes(soc.payload.offset(hasTimestamp ? REFERENCE_PAYLOAD_OFFSET : 0))
57
65
  };
58
66
  }
67
+ export async function downloadFeedUpdateAsCAC(requestOptions, owner, topic, index) {
68
+ index = typeof index === 'number' ? FeedIndex.fromBigInt(BigInt(index)) : index;
69
+ const address = getFeedUpdateChunkReference(owner, topic, index);
70
+ const data = await chunkAPI.download(requestOptions, address);
71
+ return asContentAddressedChunk(data.slice(Identifier.LENGTH + Signature.LENGTH));
72
+ }
59
73
  export function makeFeedReader(requestOptions, topic, owner) {
60
74
  // TODO: remove after enough time has passed in deprecated version
61
75
  const download = async options => {
@@ -73,10 +87,11 @@ export function makeFeedReader(requestOptions, topic, owner) {
73
87
  if (options?.index === undefined) {
74
88
  return fetchLatestFeedUpdate(requestOptions, owner, topic);
75
89
  }
76
- const update = await downloadFeedUpdate(requestOptions, owner, topic, options.index, options.hasTimestamp);
90
+ const cac = await downloadFeedUpdateAsCAC(requestOptions, owner, topic, options.index);
91
+ const payload = cac.span.toBigInt() <= 4096n ? cac.payload : await bytes.download(requestOptions, new ResourceLocator(cac.address));
77
92
  const feedIndex = typeof options.index === 'number' ? FeedIndex.fromBigInt(BigInt(options.index)) : options.index;
78
93
  return {
79
- payload: update.payload,
94
+ payload,
80
95
  feedIndex
81
96
  };
82
97
  };
@@ -653,6 +653,7 @@ export declare class Bee {
653
653
  getStorageCost(gigabytes: number, duration: Duration, options?: BeeRequestOptions): Promise<BZZ>;
654
654
  extendStorageSize(postageBatchId: BatchId | Uint8Array | string, gigabytes: number, options?: BeeRequestOptions): Promise<BatchId>;
655
655
  extendStorageDuration(postageBatchId: BatchId | Uint8Array | string, duration: Duration, options?: BeeRequestOptions): Promise<BatchId>;
656
+ getExtensionCost(postageBatchId: BatchId | Uint8Array | string, gigabytes: number, duration: Duration, options?: BeeRequestOptions): Promise<BZZ>;
656
657
  getSizeExtensionCost(postageBatchId: BatchId | Uint8Array | string, gigabytes: number, options?: BeeRequestOptions): Promise<BZZ>;
657
658
  getDurationExtensionCost(postageBatchId: BatchId | Uint8Array | string, duration: Duration, options?: BeeRequestOptions): Promise<BZZ>;
658
659
  /**
@@ -23,3 +23,4 @@ export interface Chunk {
23
23
  * @param payloadBytes the data to be stored in the chunk
24
24
  */
25
25
  export declare function makeContentAddressedChunk(payloadBytes: Uint8Array | string): Chunk;
26
+ export declare function asContentAddressedChunk(chunkBytes: Uint8Array): Chunk;
@@ -55,6 +55,7 @@ export declare function uploadSingleOwnerChunk(requestOptions: BeeRequestOptions
55
55
  * @param options
56
56
  */
57
57
  export declare function uploadSingleOwnerChunkData(requestOptions: BeeRequestOptions, signer: PrivateKey | Uint8Array | string, stamp: BatchId | Uint8Array | string, identifier: Identifier | Uint8Array | string, data: Uint8Array, options?: UploadOptions): Promise<UploadResult>;
58
+ export declare function uploadSingleOwnerChunkWithWrappedChunk(requestOptions: BeeRequestOptions, signer: PrivateKey | Uint8Array | string, stamp: BatchId | Uint8Array | string, identifier: Identifier | Uint8Array | string, rootChunk: Uint8Array, options?: UploadOptions): Promise<UploadResult>;
58
59
  /**
59
60
  * Helper function to download SOC.
60
61
  *
@@ -1,4 +1,5 @@
1
1
  import { Optional } from 'cafe-utility';
2
+ import { Chunk } from '../chunk/cac';
2
3
  import { FeedUpdateOptions } from '../modules/feed';
3
4
  import { BeeRequestOptions, FeedReader, FeedWriter, UploadOptions, UploadResult } from '../types';
4
5
  import { Bytes } from '../utils/bytes';
@@ -18,5 +19,6 @@ export declare function updateFeedWithReference(requestOptions: BeeRequestOption
18
19
  export declare function updateFeedWithPayload(requestOptions: BeeRequestOptions, signer: PrivateKey, topic: Topic, data: Uint8Array | string, postageBatchId: BatchId, options?: FeedUploadOptions): Promise<UploadResult>;
19
20
  export declare function getFeedUpdateChunkReference(owner: EthAddress, topic: Topic, index: FeedIndex): Reference;
20
21
  export declare function downloadFeedUpdate(requestOptions: BeeRequestOptions, owner: EthAddress, topic: Topic, index: FeedIndex | number, hasTimestamp?: boolean): Promise<FeedUpdate>;
22
+ export declare function downloadFeedUpdateAsCAC(requestOptions: BeeRequestOptions, owner: EthAddress, topic: Topic, index: FeedIndex | number): Promise<Chunk>;
21
23
  export declare function makeFeedReader(requestOptions: BeeRequestOptions, topic: Topic, owner: EthAddress): FeedReader;
22
24
  export declare function makeFeedWriter(requestOptions: BeeRequestOptions, topic: Topic, signer: PrivateKey): FeedWriter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@upcoming/bee-js",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",