@ethersphere/bee-js
Version:
Javascript client for Bee
44 lines (43 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Stamper = void 0;
const cafe_utility_1 = require("cafe-utility");
const typed_bytes_1 = require("../utils/typed-bytes");
class Stamper {
constructor(signer, batchId, buckets, depth) {
this.signer = new typed_bytes_1.PrivateKey(signer);
this.batchId = new typed_bytes_1.BatchId(batchId);
this.buckets = buckets;
this.depth = depth;
this.maxSlot = 2 ** (this.depth - 16);
}
static fromBlank(signer, batchId, depth) {
return new Stamper(signer, batchId, new Uint32Array(65536), depth);
}
static fromState(signer, batchId, buckets, depth) {
return new Stamper(signer, batchId, buckets, depth);
}
stamp(chunk) {
const address = chunk.hash();
const bucket = cafe_utility_1.Binary.uint16ToNumber(address, 'BE');
const height = this.buckets[bucket];
if (height >= this.maxSlot) {
throw Error('Bucket is full');
}
this.buckets[bucket]++;
const index = cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint32(bucket, 'BE'), cafe_utility_1.Binary.numberToUint32(height, 'BE'));
const timestamp = cafe_utility_1.Binary.numberToUint64(BigInt(Date.now()), 'BE');
const signature = this.signer.sign(cafe_utility_1.Binary.concatBytes(address, this.batchId.toUint8Array(), index, timestamp));
return {
batchId: this.batchId,
index,
issuer: this.signer.publicKey().address().toUint8Array(),
signature: signature.toUint8Array(),
timestamp,
};
}
getState() {
return this.buckets;
}
}
exports.Stamper = Stamper;