UNPKG

lotus-sdk

Version:

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

190 lines (189 loc) 4.89 kB
import { BufferUtil } from '../util/buffer.js'; import { BN } from '../crypto/bn.js'; export class BufferWriter { bufs = []; bufLen = 0; constructor(obj) { this.bufLen = 0; if (obj) { this.set(obj); } else { this.bufs = []; } } static create(obj) { return new BufferWriter(obj); } set(obj) { this.bufs = obj.bufs || this.bufs || []; this.bufLen = this.bufs.reduce((prev, buf) => prev + buf.length, 0); return this; } toBuffer() { return this.concat(); } concat() { return Buffer.concat(this.bufs, this.bufLen); } write(buf) { if (!BufferUtil.isBuffer(buf)) { throw new Error('Expected Buffer'); } this.bufs.push(buf); this.bufLen += buf.length; return this; } writeReverse(buf) { if (!BufferUtil.isBuffer(buf)) { throw new Error('Expected Buffer'); } this.bufs.push(BufferUtil.reverse(buf)); this.bufLen += buf.length; return this; } writeUInt8(n) { const buf = Buffer.alloc(1); buf.writeUInt8(n, 0); this.write(buf); return this; } writeUInt16BE(n) { const buf = Buffer.alloc(2); buf.writeUInt16BE(n, 0); this.write(buf); return this; } writeUInt16LE(n) { const buf = Buffer.alloc(2); buf.writeUInt16LE(n, 0); this.write(buf); return this; } writeUInt32BE(n) { const buf = Buffer.alloc(4); buf.writeUInt32BE(n, 0); this.write(buf); return this; } writeInt32LE(n) { const buf = Buffer.alloc(4); buf.writeInt32LE(n, 0); this.write(buf); return this; } writeUInt32LE(n) { const buf = Buffer.alloc(4); buf.writeUInt32LE(n, 0); this.write(buf); return this; } writeUInt48LE(n) { const buf = Buffer.alloc(6); buf.writeUIntLE(n, 0, 6); this.write(buf); return this; } writeUInt56LE(n) { const buf = Buffer.alloc(7); buf.writeUIntLE(n, 0, 7); this.write(buf); return this; } writeUInt64BEBN(bn) { const buf = bn.toBuffer({ size: 8 }); this.write(buf); return this; } writeUInt56LEBN(bn) { const buf = bn.toBuffer({ size: 7 }); this.writeReverse(buf); return this; } writeUInt64LEBN(bn) { const buf = bn.toBuffer({ size: 8 }); this.writeReverse(buf); return this; } writeUInt64LEBigInt(value) { const bn = new BN(value.toString()); return this.writeUInt64LEBN(bn); } writeUInt32LEBN(bn) { const value = bn.toNumber(); this.writeUInt32LE(value); return this; } writeUInt32LENumber(value) { this.writeUInt32LE(value); return this; } writeVarintNum(n) { const buf = BufferWriter.varintBufNum(n); this.write(buf); return this; } writeVarintBN(bn) { const buf = BufferWriter.varintBufBN(bn); this.write(buf); return this; } writeVarLengthBuffer(buf) { this.writeVarintNum(buf.length); this.write(buf); return this; } static varintBufNum(n) { let buf; if (n < 253) { buf = Buffer.alloc(1); buf.writeUInt8(n, 0); } else if (n < 0x10000) { buf = Buffer.alloc(1 + 2); buf.writeUInt8(253, 0); buf.writeUInt16LE(n, 1); } else if (n < 0x100000000) { buf = Buffer.alloc(1 + 4); buf.writeUInt8(254, 0); buf.writeUInt32LE(n, 1); } else { buf = Buffer.alloc(1 + 8); buf.writeUInt8(255, 0); buf.writeInt32LE(n & -1, 1); buf.writeUInt32LE(Math.floor(n / 0x100000000), 5); } return buf; } static varintBufBN(bn) { const n = bn.toNumber(); if (n < 253) { const buf = Buffer.alloc(1); buf.writeUInt8(n, 0); return buf; } else if (n < 0x10000) { const buf = Buffer.alloc(1 + 2); buf.writeUInt8(253, 0); buf.writeUInt16LE(n, 1); return buf; } else if (n < 0x100000000) { const buf = Buffer.alloc(1 + 4); buf.writeUInt8(254, 0); buf.writeUInt32LE(n, 1); return buf; } else { const bw = new BufferWriter(); bw.writeUInt8(255); bw.writeUInt64LEBN(bn); return bw.concat(); } } } export function BufferWriterFactory(obj) { return new BufferWriter(obj); }