@upcoming/bee-js 0.10.1 → 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.
- package/dist/cjs/feed/index.js +59 -19
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/modules/feed.js +14 -3
- package/dist/cjs/stamper/stamper.js +43 -0
- package/dist/cjs/utils/bytes.js +3 -0
- package/dist/cjs/utils/typed-bytes.js +9 -0
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/feed/index.js +63 -21
- package/dist/mjs/index.js +2 -1
- package/dist/mjs/modules/feed.js +12 -2
- package/dist/mjs/stamper/stamper.js +39 -0
- package/dist/mjs/utils/bytes.js +3 -0
- package/dist/mjs/utils/typed-bytes.js +10 -1
- package/dist/types/bee.d.ts +2 -2
- package/dist/types/feed/index.d.ts +5 -3
- package/dist/types/index.d.ts +3 -1
- package/dist/types/manifest/manifest.d.ts +2 -2
- package/dist/types/modules/feed.d.ts +12 -4
- package/dist/types/stamper/stamper.d.ts +15 -0
- package/dist/types/types/index.d.ts +15 -3
- package/dist/types/utils/bytes.d.ts +1 -0
- package/dist/types/utils/typed-bytes.d.ts +3 -0
- package/package.json +1 -1
package/dist/cjs/feed/index.js
CHANGED
|
@@ -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.
|
|
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
|
|
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.
|
|
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
|
-
|
|
75
|
-
|
|
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
|
|
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;
|
package/dist/cjs/index.js
CHANGED
|
@@ -26,11 +26,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
26
26
|
return result;
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.BeeDev = exports.Bee = exports.Utils = exports.Duration = exports.Bytes = exports.SUPPORTED_BEE_VERSION_EXACT = exports.SUPPORTED_BEE_VERSION = exports.MantarayNode = exports.MerkleTree = void 0;
|
|
29
|
+
exports.Stamper = exports.BeeDev = exports.Bee = exports.Utils = exports.Duration = exports.Bytes = exports.SUPPORTED_BEE_VERSION_EXACT = exports.SUPPORTED_BEE_VERSION = exports.MantarayNode = exports.MerkleTree = void 0;
|
|
30
30
|
const bee_1 = require("./bee");
|
|
31
31
|
Object.defineProperty(exports, "Bee", { enumerable: true, get: function () { return bee_1.Bee; } });
|
|
32
32
|
const bee_dev_1 = require("./bee-dev");
|
|
33
33
|
Object.defineProperty(exports, "BeeDev", { enumerable: true, get: function () { return bee_dev_1.BeeDev; } });
|
|
34
|
+
const stamper_1 = require("./stamper/stamper");
|
|
35
|
+
Object.defineProperty(exports, "Stamper", { enumerable: true, get: function () { return stamper_1.Stamper; } });
|
|
34
36
|
var cafe_utility_1 = require("cafe-utility");
|
|
35
37
|
Object.defineProperty(exports, "MerkleTree", { enumerable: true, get: function () { return cafe_utility_1.MerkleTree; } });
|
|
36
38
|
var manifest_1 = require("./manifest/manifest");
|
package/dist/cjs/modules/feed.js
CHANGED
|
@@ -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
|
|
55
|
-
* @param topic Topic
|
|
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;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Stamper = void 0;
|
|
4
|
+
const cafe_utility_1 = require("cafe-utility");
|
|
5
|
+
const typed_bytes_1 = require("../utils/typed-bytes");
|
|
6
|
+
class Stamper {
|
|
7
|
+
constructor(signer, batchId, buckets, depth) {
|
|
8
|
+
this.signer = new typed_bytes_1.PrivateKey(signer);
|
|
9
|
+
this.batchId = new typed_bytes_1.BatchId(batchId);
|
|
10
|
+
this.buckets = buckets;
|
|
11
|
+
this.depth = depth;
|
|
12
|
+
this.maxSlot = 2 ** (this.depth - 16);
|
|
13
|
+
}
|
|
14
|
+
static fromBlank(signer, batchId, depth) {
|
|
15
|
+
return new Stamper(signer, batchId, new Uint32Array(65536), depth);
|
|
16
|
+
}
|
|
17
|
+
static fromState(signer, batchId, buckets, depth) {
|
|
18
|
+
return new Stamper(signer, batchId, buckets, depth);
|
|
19
|
+
}
|
|
20
|
+
stamp(chunk) {
|
|
21
|
+
const address = chunk.hash();
|
|
22
|
+
const bucket = cafe_utility_1.Binary.uint16ToNumber(address, 'BE');
|
|
23
|
+
const height = this.buckets[bucket];
|
|
24
|
+
if (height >= this.maxSlot) {
|
|
25
|
+
throw Error('Bucket is full');
|
|
26
|
+
}
|
|
27
|
+
this.buckets[bucket]++;
|
|
28
|
+
const index = cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint32(bucket, 'BE'), cafe_utility_1.Binary.numberToUint32(height, 'BE'));
|
|
29
|
+
const timestamp = cafe_utility_1.Binary.numberToUint64(BigInt(Date.now()), 'BE');
|
|
30
|
+
const signature = this.signer.sign(cafe_utility_1.Binary.concatBytes(address, this.batchId.toUint8Array(), index, timestamp));
|
|
31
|
+
return {
|
|
32
|
+
batchId: this.batchId,
|
|
33
|
+
index,
|
|
34
|
+
issuer: this.signer.publicKey().address().toUint8Array(),
|
|
35
|
+
signature: signature.toUint8Array(),
|
|
36
|
+
timestamp,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
getState() {
|
|
40
|
+
return this.buckets;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.Stamper = Stamper;
|
package/dist/cjs/utils/bytes.js
CHANGED
|
@@ -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));
|