UNPKG

lotus-sdk

Version:

Central repository for several classes of tools for integrating with, and building for, the Lotusia ecosystem

62 lines (61 loc) 1.46 kB
import { BufferWriter } from './bufferwriter.js'; import { BufferReader } from './bufferreader.js'; import { BN } from '../crypto/bn.js'; export class Varint { buf = Buffer.alloc(0); constructor(buf) { if (Buffer.isBuffer(buf)) { this.buf = buf; } else if (typeof buf === 'number') { const num = buf; this.fromNumber(num); } else if (buf instanceof BN) { const bn = buf; this.fromBN(bn); } else if (buf) { const obj = buf; this.set(obj); } } set(obj) { this.buf = obj.buf || this.buf; return this; } fromString(str) { this.set({ buf: Buffer.from(str, 'hex'), }); return this; } toString() { return this.buf.toString('hex'); } fromBuffer(buf) { this.buf = buf; return this; } fromBufferReader(br) { this.buf = br.readVarintBuf(); return this; } fromBN(bn) { this.buf = BufferWriter.varintBufBN(bn); return this; } fromNumber(num) { this.buf = new BufferWriter().writeVarintNum(num).concat(); return this; } toBuffer() { return this.buf; } toBN() { return new BufferReader(this.buf).readVarintBN(); } toNumber() { return new BufferReader(this.buf).readVarintNum(); } }