UNPKG

@ethersphere/bee-js

Version:
39 lines 1.38 kB
import { Binary } from 'cafe-utility'; import { BatchId, PrivateKey } from "../utils/typed-bytes.js"; export class Stamper { constructor(signer, batchId, buckets, depth) { this.signer = new PrivateKey(signer); this.batchId = new 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 = Binary.uint16ToNumber(address, 'BE'); const height = this.buckets[bucket]; if (height >= this.maxSlot) { throw Error('Bucket is full'); } this.buckets[bucket]++; const index = Binary.concatBytes(Binary.numberToUint32(bucket, 'BE'), Binary.numberToUint32(height, 'BE')); const timestamp = Binary.numberToUint64(BigInt(Date.now()), 'BE'); const signature = this.signer.sign(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; } }