@upcoming/bee-js 0.11.0 → 0.12.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.
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.makeFeedWriter = exports.makeFeedReader = exports.downloadFeedUpdate = exports.getFeedUpdateChunkReference = exports.updateFeed = exports.findNextIndex = void 0;
26
+ exports.makeFeedWriter = exports.makeFeedReader = exports.downloadFeedUpdate = exports.getFeedUpdateChunkReference = exports.updateFeedWithPayload = exports.updateFeedWithReference = exports.findNextIndex = void 0;
27
27
  const cafe_utility_1 = require("cafe-utility");
28
28
  const soc_1 = require("../chunk/soc");
29
29
  const chunkAPI = __importStar(require("../modules/chunk"));
@@ -51,7 +51,7 @@ async function findNextIndex(requestOptions, owner, topic) {
51
51
  }
52
52
  }
53
53
  exports.findNextIndex = findNextIndex;
54
- async function updateFeed(requestOptions, signer, topic, reference, postageBatchId, options) {
54
+ async function updateFeedWithReference(requestOptions, signer, topic, reference, postageBatchId, options) {
55
55
  reference = new typed_bytes_1.Reference(reference);
56
56
  const nextIndex = options?.index ?? (await findNextIndex(requestOptions, signer.publicKey().address(), topic));
57
57
  const identifier = (0, identifier_1.makeFeedIdentifier)(topic, nextIndex);
@@ -60,50 +60,90 @@ async function updateFeed(requestOptions, signer, topic, reference, postageBatch
60
60
  const payloadBytes = cafe_utility_1.Binary.concatBytes(timestamp, reference.toUint8Array());
61
61
  return (0, soc_1.uploadSingleOwnerChunkData)(requestOptions, signer, postageBatchId, identifier, payloadBytes, options);
62
62
  }
63
- exports.updateFeed = updateFeed;
63
+ exports.updateFeedWithReference = updateFeedWithReference;
64
+ async function updateFeedWithPayload(requestOptions, signer, topic, data, postageBatchId, options) {
65
+ const nextIndex = options?.index ?? (await findNextIndex(requestOptions, signer.publicKey().address(), topic));
66
+ const identifier = (0, identifier_1.makeFeedIdentifier)(topic, nextIndex);
67
+ return (0, soc_1.uploadSingleOwnerChunkData)(requestOptions, signer, postageBatchId, identifier, cafe_utility_1.Types.isString(data) ? bytes_1.Bytes.fromUtf8(data).toUint8Array() : data, options);
68
+ }
69
+ exports.updateFeedWithPayload = updateFeedWithPayload;
64
70
  function getFeedUpdateChunkReference(owner, topic, index) {
65
71
  const identifier = (0, identifier_1.makeFeedIdentifier)(topic, index);
66
72
  return new typed_bytes_1.Reference(cafe_utility_1.Binary.keccak256(cafe_utility_1.Binary.concatBytes(identifier.toUint8Array(), owner.toUint8Array())));
67
73
  }
68
74
  exports.getFeedUpdateChunkReference = getFeedUpdateChunkReference;
69
- async function downloadFeedUpdate(requestOptions, owner, topic, index) {
75
+ async function downloadFeedUpdate(requestOptions, owner, topic, index, hasTimestamp = false) {
70
76
  index = typeof index === 'number' ? typed_bytes_1.FeedIndex.fromBigInt(BigInt(index)) : index;
71
77
  const address = getFeedUpdateChunkReference(owner, topic, index);
72
78
  const data = await chunkAPI.download(requestOptions, address.toHex());
73
79
  const soc = (0, soc_1.makeSingleOwnerChunkFromData)(data, address);
74
- const timestampBytes = bytes_1.Bytes.fromSlice(soc.payload.toUint8Array(), TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE);
75
- const timestamp = Number(cafe_utility_1.Binary.uint64ToNumber(timestampBytes.toUint8Array(), 'BE'));
80
+ let timestamp = cafe_utility_1.Optional.empty();
81
+ if (hasTimestamp) {
82
+ const timestampBytes = bytes_1.Bytes.fromSlice(soc.payload.toUint8Array(), TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE);
83
+ timestamp = cafe_utility_1.Optional.of(Number(cafe_utility_1.Binary.uint64ToNumber(timestampBytes.toUint8Array(), 'BE')));
84
+ }
76
85
  return {
77
86
  timestamp,
78
- payload: new bytes_1.Bytes(soc.payload.offset(REFERENCE_PAYLOAD_OFFSET)),
87
+ payload: new bytes_1.Bytes(soc.payload.offset(hasTimestamp ? REFERENCE_PAYLOAD_OFFSET : 0)),
79
88
  };
80
89
  }
81
90
  exports.downloadFeedUpdate = downloadFeedUpdate;
82
91
  function makeFeedReader(requestOptions, topic, owner) {
92
+ // TODO: remove after enough time has passed in deprecated version
93
+ const download = async (options) => {
94
+ if (options?.index === undefined) {
95
+ return (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic);
96
+ }
97
+ const update = await downloadFeedUpdate(requestOptions, owner, topic, options.index, options.hasTimestamp ?? true);
98
+ const feedIndex = typeof options.index === 'number' ? typed_bytes_1.FeedIndex.fromBigInt(BigInt(options.index)) : options.index;
99
+ return {
100
+ payload: update.payload,
101
+ feedIndex,
102
+ };
103
+ };
104
+ const downloadPayload = async (options) => {
105
+ if (options?.index === undefined) {
106
+ return (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic);
107
+ }
108
+ const update = await downloadFeedUpdate(requestOptions, owner, topic, options.index, options.hasTimestamp);
109
+ const feedIndex = typeof options.index === 'number' ? typed_bytes_1.FeedIndex.fromBigInt(BigInt(options.index)) : options.index;
110
+ return {
111
+ payload: update.payload,
112
+ feedIndex,
113
+ };
114
+ };
115
+ const downloadReference = async (options) => {
116
+ let index = options?.index;
117
+ if (index === undefined) {
118
+ index = (await (0, feed_1.probeFeed)(requestOptions, owner, topic)).feedIndex;
119
+ }
120
+ const payload = await download({ ...options, index: index });
121
+ return {
122
+ reference: new typed_bytes_1.Reference(payload.payload.toUint8Array()),
123
+ feedIndex: payload.feedIndex,
124
+ };
125
+ };
83
126
  return {
127
+ download,
128
+ downloadPayload,
129
+ downloadReference,
84
130
  owner,
85
131
  topic,
86
- async download(options) {
87
- if (options?.index === undefined) {
88
- return (0, feed_1.fetchLatestFeedUpdate)(requestOptions, owner, topic);
89
- }
90
- const update = await downloadFeedUpdate(requestOptions, owner, topic, options.index);
91
- const feedIndex = typeof options.index === 'number' ? typed_bytes_1.FeedIndex.fromBigInt(BigInt(options.index)) : options.index;
92
- return {
93
- payload: update.payload,
94
- feedIndex,
95
- };
96
- },
97
132
  };
98
133
  }
99
134
  exports.makeFeedReader = makeFeedReader;
100
135
  function makeFeedWriter(requestOptions, topic, signer) {
101
136
  const upload = async (postageBatchId, reference, options) => {
102
- return updateFeed(requestOptions, signer, topic, reference, postageBatchId, options);
137
+ return updateFeedWithReference(requestOptions, signer, topic, reference, postageBatchId, options);
138
+ };
139
+ const uploadPayload = async (postageBatchId, data, options) => {
140
+ return updateFeedWithPayload(requestOptions, signer, topic, data, postageBatchId, options);
103
141
  };
104
142
  return {
105
143
  ...makeFeedReader(requestOptions, topic, signer.publicKey().address()),
106
144
  upload,
145
+ uploadReference: upload,
146
+ uploadPayload,
107
147
  };
108
148
  }
109
149
  exports.makeFeedWriter = makeFeedWriter;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetchLatestFeedUpdate = exports.createFeedManifest = void 0;
3
+ exports.probeFeed = exports.fetchLatestFeedUpdate = exports.createFeedManifest = void 0;
4
4
  const cafe_utility_1 = require("cafe-utility");
5
5
  const bytes_1 = require("../utils/bytes");
6
6
  const error_1 = require("../utils/error");
@@ -51,8 +51,8 @@ function readFeedUpdateHeaders(headers) {
51
51
  * index of the subsequent update.
52
52
  *
53
53
  * @param requestOptions Options for making requests
54
- * @param owner Owner's ethereum address in hex
55
- * @param topic Topic in hex
54
+ * @param owner Owner's ethereum address
55
+ * @param topic Topic
56
56
  * @param options Additional options, like index, at, type
57
57
  */
58
58
  async function fetchLatestFeedUpdate(requestOptions, owner, topic, options) {
@@ -67,3 +67,14 @@ async function fetchLatestFeedUpdate(requestOptions, owner, topic, options) {
67
67
  };
68
68
  }
69
69
  exports.fetchLatestFeedUpdate = fetchLatestFeedUpdate;
70
+ async function probeFeed(requestOptions, owner, topic) {
71
+ const response = await (0, http_1.http)(requestOptions, {
72
+ responseType: 'arraybuffer',
73
+ url: `${feedEndpoint}/${owner}/${topic}`,
74
+ params: {
75
+ 'Swarm-Only-Root-Chunk': true,
76
+ },
77
+ });
78
+ return readFeedUpdateHeaders(response.headers);
79
+ }
80
+ exports.probeFeed = probeFeed;
@@ -66,5 +66,8 @@ class Bytes {
66
66
  toJSON() {
67
67
  return JSON.parse(this.toUtf8());
68
68
  }
69
+ equals(other) {
70
+ return this.toHex() === new Bytes(other).toHex();
71
+ }
69
72
  }
70
73
  exports.Bytes = Bytes;
@@ -63,6 +63,9 @@ class Identifier extends bytes_1.Bytes {
63
63
  constructor(bytes) {
64
64
  super(bytes, 32);
65
65
  }
66
+ static fromString(value) {
67
+ return new Identifier(cafe_utility_1.Binary.keccak256(ENCODER.encode(value)));
68
+ }
66
69
  }
67
70
  exports.Identifier = Identifier;
68
71
  Identifier.LENGTH = 32;
@@ -142,6 +145,11 @@ class Signature extends bytes_1.Bytes {
142
145
  const [x, y] = cafe_utility_1.Elliptic.recoverPublicKey(cafe_utility_1.Binary.concatBytes(ENCODER.encode(`\x19Ethereum Signed Message:\n32`), cafe_utility_1.Binary.keccak256(digest instanceof Uint8Array ? digest : ENCODER.encode(digest))), r, s, v);
143
146
  return new PublicKey(cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint256(x, 'BE'), cafe_utility_1.Binary.numberToUint256(y, 'BE')));
144
147
  }
148
+ isValid(digest, expectedAddress) {
149
+ const publicKey = this.recoverPublicKey(digest);
150
+ const address = publicKey.address();
151
+ return address.equals(expectedAddress);
152
+ }
145
153
  }
146
154
  exports.Signature = Signature;
147
155
  Signature.LENGTH = 65;
@@ -168,3 +176,4 @@ class FeedIndex extends bytes_1.Bytes {
168
176
  }
169
177
  exports.FeedIndex = FeedIndex;
170
178
  FeedIndex.LENGTH = 8;
179
+ FeedIndex.MINUS_ONE = new FeedIndex(new Uint8Array(8).fill(0xff, 0, 8));