Socket
Socket
Sign inDemoInstall

soprox-abi

Package Overview
Dependencies
52
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.15 to 0.0.16

.prettierrc.json

9

dist/types/isize.js

@@ -29,2 +29,3 @@ "use strict";

var ZERO = new BN(0);
var ONE = new BN(1);

@@ -43,4 +44,4 @@ var NEONE = new BN(-1);

if (bigInt.gt(FMAX) || bigInt.lt(FMIN)) throw new Error('BigInt is too big');
var reverseBigInt = F.add(bigInt).add(ONE);
var arrayBuf = reverseBigInt.toArray('le', this.length);
var transformedBigInt = bigInt.gte(ZERO) ? bigInt : F.add(bigInt).add(ONE);
var arrayBuf = transformedBigInt.toArray('le', this.length);

@@ -67,4 +68,4 @@ for (var i = 0; i < this.length; i++) {

if (bigInt.gt(FFMAX) || bigInt.lt(FFMIN)) throw new Error('BigInt is too big');
var reverseBigInt = FF.add(bigInt).add(ONE);
var arrayBuf = reverseBigInt.toArray('le', this.length);
var transformedBigInt = bigInt.gte(ZERO) ? bigInt : FF.add(bigInt).add(ONE);
var arrayBuf = transformedBigInt.toArray('le', this.length);

@@ -71,0 +72,0 @@ for (var i = 0; i < this.length; i++) {

{
"name": "soprox-abi",
"version": "0.0.15",
"version": "0.0.16",
"description": "A convention that convert Rust types to NodeJS types",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -1,19 +0,19 @@

const types = require('./types');
const util = require('./util');
const types = require('./types')
const util = require('./util')
const create = (type, value = null) => {
if (types[type]) return new types[type](value);
const temp = type.split('');
const boundary = [temp[0], temp[temp.length - 1]].join('');
if (boundary === '[]') return new types.array(type, value);
if (boundary === '()') return new types.tuple(type, value);
throw new Error('Invalid type');
if (types[type]) return new types[type](value)
const temp = type.split('')
const boundary = [temp[0], temp[temp.length - 1]].join('')
if (boundary === '[]') return new types.array(type, value)
if (boundary === '()') return new types.tuple(type, value)
throw new Error('Invalid type')
}
const span = (register) => {
const { type, schema } = register;
if (type) return create(type).space;
const { type, schema } = register
if (type) return create(type).space
return schema.reduce((total, nestedRegister) => {
return span(nestedRegister) + total;
}, 0);
return span(nestedRegister) + total
}, 0)
}

@@ -23,33 +23,33 @@

constructor(schema, value = {}) {
this.schema = schema;
this.value = value;
this.space = span({ schema: this.schema });
this.schema = schema
this.value = value
this.space = span({ schema: this.schema })
}
toBuffer = () => {
const eleBufs = this.schema.map(each => {
const { key, type, schema } = each;
return type ?
create(type, this.value[key]) :
new struct(schema, this.value[key]);
});
return util.pack(...eleBufs);
const eleBufs = this.schema.map((each) => {
const { key, type, schema } = each
return type
? create(type, this.value[key])
: new struct(schema, this.value[key])
})
return util.pack(...eleBufs)
}
fromBuffer = (buf) => {
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer');
this.value = {};
let offset = 0;
this.schema.forEach(each => {
const { key, type, schema } = each;
const item = type ? create(type) : new struct(schema);
const subBuf = buf.slice(offset, offset + item.space);
item.fromBuffer(subBuf);
this.value[key] = item.value;
offset += item.space;
});
return this.value;
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer')
this.value = {}
let offset = 0
this.schema.forEach((each) => {
const { key, type, schema } = each
const item = type ? create(type) : new struct(schema)
const subBuf = buf.slice(offset, offset + item.space)
item.fromBuffer(subBuf)
this.value[key] = item.value
offset += item.space
})
return this.value
}
}
module.exports = { struct, ...types, ...util, create, span };
module.exports = { struct, ...types, ...util, create, span }

@@ -1,8 +0,8 @@

const bool = require('./bool');
const char = require('./char');
const usize = require('./usize');
const isize = require('./isize');
const pub = require('./pub');
const types = { ...bool, ...char, ...usize, ...isize, ...pub };
const util = require('../util');
const bool = require('./bool')
const char = require('./char')
const usize = require('./usize')
const isize = require('./isize')
const pub = require('./pub')
const types = { ...bool, ...char, ...usize, ...isize, ...pub }
const util = require('../util')

@@ -15,23 +15,27 @@ /**

constructor(type, value = []) {
this.value = value;
this.type = type;
const { primaryType, space } = this._parseType(this.type);
this._primaryType = primaryType;
this.space = space;
this.value = value
this.type = type
const { primaryType, space } = this._parseType(this.type)
this._primaryType = primaryType
this.space = space
}
_parseType = (type) => {
if (!type) throw new Error('Invalid type array');
if (typeof type !== 'string') throw new Error('Invalid type array');
type = type.trim();
type = type.split('');
if (type.shift() !== '[') throw new Error('Invalid type array');
if (type.pop() !== ']') throw new Error('Invalid type array');
type = type.join('');
let [primaryType, len] = type.split(';');
if (!primaryType || !len) throw new Error('Invalid type array');
if (!types[primaryType]) throw new Error('Invalid type array');
try { len = parseInt(len) } catch (er) { len = 0 }
if (len <= 0) throw new Error('Invalid type array');
const space = (new types[primaryType]).space * len;
if (!type) throw new Error('Invalid type array')
if (typeof type !== 'string') throw new Error('Invalid type array')
type = type.trim()
type = type.split('')
if (type.shift() !== '[') throw new Error('Invalid type array')
if (type.pop() !== ']') throw new Error('Invalid type array')
type = type.join('')
let [primaryType, len] = type.split(';')
if (!primaryType || !len) throw new Error('Invalid type array')
if (!types[primaryType]) throw new Error('Invalid type array')
try {
len = parseInt(len)
} catch (er) {
len = 0
}
if (len <= 0) throw new Error('Invalid type array')
const space = new types[primaryType]().space * len
return { primaryType, space }

@@ -41,15 +45,15 @@ }

toBuffer = () => {
const eleBufs = this.value.map(item => new types[this._primaryType](item));
return util.pack(...eleBufs);
const eleBufs = this.value.map((item) => new types[this._primaryType](item))
return util.pack(...eleBufs)
}
fromBuffer = (buf) => {
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer');
this.value = [];
let offset = 0;
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer')
this.value = []
let offset = 0
while (offset < this.space) {
const type = new types[this._primaryType]();
type.fromBuffer(buf.slice(offset, offset + type.space));
this.value.push(type.value);
offset += type.space;
const type = new types[this._primaryType]()
type.fromBuffer(buf.slice(offset, offset + type.space))
this.value.push(type.value)
offset += type.space
}

@@ -59,2 +63,2 @@ }

module.exports = { array }
module.exports = { array }

@@ -6,20 +6,20 @@ /**

constructor(value = false) {
this.value = value;
this.type = 'bool';
this.space = 1;
this.value = value
this.type = 'bool'
this.space = 1
}
toBuffer = () => {
const buf = Buffer.allocUnsafe(this.space);
buf.writeUIntLE(this.value ? 1 : 0, 0, this.space);
return buf;
const buf = Buffer.allocUnsafe(this.space)
buf.writeUIntLE(this.value ? 1 : 0, 0, this.space)
return buf
}
fromBuffer = (buf) => {
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer');
this.value = Boolean(buf.readUIntLE(0, this.space));
return this.value;
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer')
this.value = Boolean(buf.readUIntLE(0, this.space))
return this.value
}
}
module.exports = { bool }
module.exports = { bool }

@@ -6,21 +6,22 @@ /**

constructor(value) {
this.value = value;
this.type = 'char';
this.space = 4;
this.value = value
this.type = 'char'
this.space = 4
}
toBuffer = () => {
const buf = Buffer.from(this.value, 'utf8');
if (buf.length > this.space) throw new Error('Invalid char');
if (buf.length < this.space) return Buffer.concat([buf, Buffer.alloc(this.space - buf.length)]);
return buf;
const buf = Buffer.from(this.value, 'utf8')
if (buf.length > this.space) throw new Error('Invalid char')
if (buf.length < this.space)
return Buffer.concat([buf, Buffer.alloc(this.space - buf.length)])
return buf
}
fromBuffer = (buf) => {
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer');
this.value = buf.toString('utf8');
return this.value;
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer')
this.value = buf.toString('utf8')
return this.value
}
}
module.exports = { char }
module.exports = { char }

@@ -1,8 +0,8 @@

const { bool } = require('./bool');
const { char } = require('./char');
const { u8, u16, u32, u64, u128 } = require('./usize');
const { i8, i16, i32, i64, i128 } = require('./isize');
const { array } = require('./array');
const { tuple } = require('./tuple');
const { pub } = require('./pub');
const { bool } = require('./bool')
const { char } = require('./char')
const { u8, u16, u32, u64, u128 } = require('./usize')
const { i8, i16, i32, i64, i128 } = require('./isize')
const { array } = require('./array')
const { tuple } = require('./tuple')
const { pub } = require('./pub')

@@ -12,7 +12,15 @@ module.exports = {

char,
u8, u16, u32, u64, u128,
i8, i16, i32, i64, i128,
u8,
u16,
u32,
u64,
u128,
i8,
i16,
i32,
i64,
i128,
array,
tuple,
pub,
};
}

@@ -1,2 +0,2 @@

const BN = require('bn.js');
const BN = require('bn.js')

@@ -6,42 +6,60 @@ /**

*/
const ONE = new BN(1);
const NEONE = new BN(-1);
const F = new BN(Buffer.from('ffffffffffffffff', 'hex'), 16, 'le');
const FMAX = new BN(Buffer.from('ffffffffffffff7f', 'hex'), 16, 'le');
const FMIN = FMAX.mul(NEONE).add(NEONE);
const FF = new BN(Buffer.from('ffffffffffffffffffffffffffffffff', 'hex'), 16, 'le');
const FFMAX = new BN(Buffer.from('ffffffffffffffffffffffffffffff7f', 'hex'), 16, 'le');
const FFMIN = FFMAX.mul(NEONE).add(NEONE);
Buffer.prototype.writeBigInt64LE = Buffer.prototype.writeBigInt64LE || function (jsBigInt, offset = 0) {
const bigInt = new BN(jsBigInt.toString());
if (bigInt.gt(FMAX) || bigInt.lt(FMIN)) throw new Error('BigInt is too big');
const reverseBigInt = F.add(bigInt).add(ONE);
const arrayBuf = reverseBigInt.toArray('le', this.length);
for (let i = 0; i < this.length; i++)
this[i] = arrayBuf[i];
return offset;
}
Buffer.prototype.readBigInt64LE = Buffer.prototype.readBigInt64LE || function (offset = 0) {
let bigInt = new BN(this, 16, 'le');
if (bigInt.gt(F)) throw new Error('BigInt is too big');
if (bigInt.gt(FMAX)) bigInt = F.sub(bigInt).add(ONE);
// Using global.BigInt instead of BigInt due to browser understanding
return global.BigInt(bigInt.toString());
}
Buffer.prototype.writeBigInt128LE = Buffer.prototype.writeBigInt128LE || function (jsBigInt, offset = 0) {
const bigInt = new BN(jsBigInt.toString());
if (bigInt.gt(FFMAX) || bigInt.lt(FFMIN)) throw new Error('BigInt is too big');
const reverseBigInt = FF.add(bigInt).add(ONE);
const arrayBuf = reverseBigInt.toArray('le', this.length);
for (let i = 0; i < this.length; i++)
this[i] = arrayBuf[i];
return offset;
}
Buffer.prototype.readBigInt128LE = Buffer.prototype.readBigInt128LE || function (offset = 0) {
let bigInt = new BN(this, 16, 'le');
if (bigInt.gt(FF)) throw new Error('BigInt is too big');
if (bigInt.gt(FFMAX)) bigInt = FF.sub(bigInt).add(ONE);
// Using global.BigInt instead of BigInt due to browser understanding
return global.BigInt(bigInt.toString());
}
const ZERO = new BN(0)
const ONE = new BN(1)
const NEONE = new BN(-1)
const F = new BN(Buffer.from('ffffffffffffffff', 'hex'), 16, 'le')
const FMAX = new BN(Buffer.from('ffffffffffffff7f', 'hex'), 16, 'le')
const FMIN = FMAX.mul(NEONE).add(NEONE)
const FF = new BN(
Buffer.from('ffffffffffffffffffffffffffffffff', 'hex'),
16,
'le',
)
const FFMAX = new BN(
Buffer.from('ffffffffffffffffffffffffffffff7f', 'hex'),
16,
'le',
)
const FFMIN = FFMAX.mul(NEONE).add(NEONE)
Buffer.prototype.writeBigInt64LE =
Buffer.prototype.writeBigInt64LE ||
function (jsBigInt, offset = 0) {
const bigInt = new BN(jsBigInt.toString())
if (bigInt.gt(FMAX) || bigInt.lt(FMIN)) throw new Error('BigInt is too big')
const transformedBigInt = bigInt.gte(ZERO) ? bigInt : F.add(bigInt).add(ONE)
const arrayBuf = transformedBigInt.toArray('le', this.length)
for (let i = 0; i < this.length; i++) this[i] = arrayBuf[i]
return offset
}
Buffer.prototype.readBigInt64LE =
Buffer.prototype.readBigInt64LE ||
function (offset = 0) {
let bigInt = new BN(this, 16, 'le')
if (bigInt.gt(F)) throw new Error('BigInt is too big')
if (bigInt.gt(FMAX)) bigInt = F.sub(bigInt).add(ONE)
// Using global.BigInt instead of BigInt due to browser understanding
return global.BigInt(bigInt.toString())
}
Buffer.prototype.writeBigInt128LE =
Buffer.prototype.writeBigInt128LE ||
function (jsBigInt, offset = 0) {
const bigInt = new BN(jsBigInt.toString())
if (bigInt.gt(FFMAX) || bigInt.lt(FFMIN))
throw new Error('BigInt is too big')
const transformedBigInt = bigInt.gte(ZERO)
? bigInt
: FF.add(bigInt).add(ONE)
const arrayBuf = transformedBigInt.toArray('le', this.length)
for (let i = 0; i < this.length; i++) this[i] = arrayBuf[i]
return offset
}
Buffer.prototype.readBigInt128LE =
Buffer.prototype.readBigInt128LE ||
function (offset = 0) {
let bigInt = new BN(this, 16, 'le')
if (bigInt.gt(FF)) throw new Error('BigInt is too big')
if (bigInt.gt(FFMAX)) bigInt = FF.sub(bigInt).add(ONE)
// Using global.BigInt instead of BigInt due to browser understanding
return global.BigInt(bigInt.toString())
}
/**

@@ -53,13 +71,13 @@ * Supportive functions

case 'i8':
return 'writeInt8';
return 'writeInt8'
case 'i16':
return 'writeInt16LE';
return 'writeInt16LE'
case 'i32':
return 'writeInt32LE';
return 'writeInt32LE'
case 'i64':
return 'writeBigInt64LE';
return 'writeBigInt64LE'
case 'i128':
return 'writeBigInt128LE';
return 'writeBigInt128LE'
default:
throw new Error('Invalid type');
throw new Error('Invalid type')
}

@@ -71,13 +89,13 @@ }

case 'i8':
return 'readInt8';
return 'readInt8'
case 'i16':
return 'readInt16LE';
return 'readInt16LE'
case 'i32':
return 'readInt32LE';
return 'readInt32LE'
case 'i64':
return 'readBigInt64LE';
return 'readBigInt64LE'
case 'i128':
return 'readBigInt128LE';
return 'readBigInt128LE'
default:
throw new Error('Invalid type');
throw new Error('Invalid type')
}

@@ -91,17 +109,17 @@ }

constructor(value, type, byteLength) {
this.value = value;
this.type = type;
this.space = byteLength;
this.value = value
this.type = type
this.space = byteLength
}
toBuffer = () => {
const buf = Buffer.allocUnsafe(this.space);
buf[type2Write(this.type)](this.value, 0);
return buf;
const buf = Buffer.allocUnsafe(this.space)
buf[type2Write(this.type)](this.value, 0)
return buf
}
fromBuffer = (buf) => {
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer');
this.value = buf[type2Read(this.type)](0);
return this.value;
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer')
this.value = buf[type2Read(this.type)](0)
return this.value
}

@@ -112,3 +130,3 @@ }

constructor(value = 0) {
super(value, 'i8', 1);
super(value, 'i8', 1)
}

@@ -119,3 +137,3 @@ }

constructor(value = 0) {
super(value, 'i16', 2);
super(value, 'i16', 2)
}

@@ -126,3 +144,3 @@ }

constructor(value = 0) {
super(value, 'i32', 4);
super(value, 'i32', 4)
}

@@ -133,3 +151,3 @@ }

constructor(value = 0) {
super(value, 'i64', 8);
super(value, 'i64', 8)
}

@@ -140,6 +158,6 @@ }

constructor(value = 0) {
super(value, 'i128', 16);
super(value, 'i128', 16)
}
}
module.exports = { i8, i16, i32, i64, i128 }
module.exports = { i8, i16, i32, i64, i128 }

@@ -1,2 +0,2 @@

const { PublicKey } = require('@solana/web3.js');
const { PublicKey } = require('@solana/web3.js')

@@ -9,19 +9,19 @@ /**

constructor(value = '11111111111111111111111111111111') {
this.value = value;
this.type = 'pub';
this.space = 32;
this.value = value
this.type = 'pub'
this.space = 32
}
toBuffer = () => {
const buf = (new PublicKey(this.value)).toBuffer();
return buf;
const buf = new PublicKey(this.value).toBuffer()
return buf
}
fromBuffer = (buf) => {
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer');
this.value = (new PublicKey(buf)).toBase58();
return this.value;
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer')
this.value = new PublicKey(buf).toBase58()
return this.value
}
}
module.exports = { pub }
module.exports = { pub }

@@ -1,7 +0,7 @@

const bool = require('./bool');
const char = require('./char');
const usize = require('./usize');
const isize = require('./isize');
const types = { ...bool, ...char, ...usize, ...isize };
const util = require('../util');
const bool = require('./bool')
const char = require('./char')
const usize = require('./usize')
const isize = require('./isize')
const types = { ...bool, ...char, ...usize, ...isize }
const util = require('../util')

@@ -13,45 +13,47 @@ /**

constructor(type, value = []) {
this.type = type;
this.value = value;
const { primaryTypes, space } = this._parseType(this.type);
this._primaryTypes = primaryTypes;
this.space = space;
this.type = type
this.value = value
const { primaryTypes, space } = this._parseType(this.type)
this._primaryTypes = primaryTypes
this.space = space
}
_parseType = (type) => {
if (!type) throw new Error('Invalid type tuple');
if (typeof type !== 'string') throw new Error('Invalid type tuple');
type = type.trim();
type = type.split('');
if (type.shift() !== '(') throw new Error('Invalid type tuple');
if (type.pop() !== ')') throw new Error('Invalid type tuple');
type = type.join('');
let primaryTypes = type.split(';');
if (primaryTypes.length <= 0) throw new Error('Invalid type tuple');
let space = 0;
primaryTypes.forEach(primaryType => {
if (!types[primaryType]) throw new Error('Invalid type array');
space += (new types[primaryType]()).space;
});
return { primaryTypes, space };
if (!type) throw new Error('Invalid type tuple')
if (typeof type !== 'string') throw new Error('Invalid type tuple')
type = type.trim()
type = type.split('')
if (type.shift() !== '(') throw new Error('Invalid type tuple')
if (type.pop() !== ')') throw new Error('Invalid type tuple')
type = type.join('')
let primaryTypes = type.split(';')
if (primaryTypes.length <= 0) throw new Error('Invalid type tuple')
let space = 0
primaryTypes.forEach((primaryType) => {
if (!types[primaryType]) throw new Error('Invalid type array')
space += new types[primaryType]().space
})
return { primaryTypes, space }
}
toBuffer = () => {
const eleBufs = this._primaryTypes.map((type, i) => new types[type](this.value[i]));
return util.pack(...eleBufs);
const eleBufs = this._primaryTypes.map(
(type, i) => new types[type](this.value[i]),
)
return util.pack(...eleBufs)
}
fromBuffer = (buf) => {
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer');
this.value = [];
let offset = 0;
this._primaryTypes.forEach(primaryType => {
const type = new types[primaryType]();
type.fromBuffer(buf.slice(offset, offset + type.space));
this.value.push(type.value);
offset += type.space;
});
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer')
this.value = []
let offset = 0
this._primaryTypes.forEach((primaryType) => {
const type = new types[primaryType]()
type.fromBuffer(buf.slice(offset, offset + type.space))
this.value.push(type.value)
offset += type.space
})
}
}
module.exports = { tuple }
module.exports = { tuple }

@@ -1,2 +0,2 @@

const BN = require('bn.js');
const BN = require('bn.js')

@@ -6,32 +6,42 @@ /**

*/
const F = new BN(Buffer.from('ffffffffffffffff', 'hex'), 16, 'le');
const FF = new BN(Buffer.from('ffffffffffffffffffffffffffffffff', 'hex'), 16, 'le');
Buffer.prototype.writeBigUInt64LE = Buffer.prototype.writeBigUInt64LE || function (jsBigInt, offset = 0) {
const bigInt = new BN(jsBigInt.toString());
if (bigInt.gt(F)) throw new Error('BigInt is too big');
const arrayBuf = bigInt.toArray('le', this.length);
for (let i = 0; i < this.length; i++)
this[i] = arrayBuf[i];
return offset;
}
Buffer.prototype.readBigUInt64LE = Buffer.prototype.readBigUInt64LE || function (offset = 0) {
const bigInt = new BN(this, 16, 'le');
if (bigInt.gt(F)) throw new Error('BigInt is too big');
// Using global.BigInt instead of BigInt due to browser understanding
return global.BigInt(bigInt.toString());
}
Buffer.prototype.writeBigUInt128LE = Buffer.prototype.writeBigUInt128LE || function (jsBigInt, offset = 0) {
const bigInt = new BN(jsBigInt.toString());
if (bigInt.gt(FF)) throw new Error('BigInt is too big');
const arrayBuf = bigInt.toArray('le', this.length);
for (let i = 0; i < this.length; i++)
this[i] = arrayBuf[i];
return offset;
}
Buffer.prototype.readBigUInt128LE = Buffer.prototype.readBigUInt128LE || function (offset = 0) {
const bigInt = new BN(this, 16, 'le');
if (bigInt.gt(FF)) throw new Error('BigInt is too big');
// Using global.BigInt instead of BigInt due to browser understanding
return global.BigInt(bigInt.toString());
}
const F = new BN(Buffer.from('ffffffffffffffff', 'hex'), 16, 'le')
const FF = new BN(
Buffer.from('ffffffffffffffffffffffffffffffff', 'hex'),
16,
'le',
)
Buffer.prototype.writeBigUInt64LE =
Buffer.prototype.writeBigUInt64LE ||
function (jsBigInt, offset = 0) {
const bigInt = new BN(jsBigInt.toString())
if (bigInt.gt(F)) throw new Error('BigInt is too big')
const arrayBuf = bigInt.toArray('le', this.length)
for (let i = 0; i < this.length; i++) this[i] = arrayBuf[i]
return offset
}
Buffer.prototype.readBigUInt64LE =
Buffer.prototype.readBigUInt64LE ||
function (offset = 0) {
const bigInt = new BN(this, 16, 'le')
if (bigInt.gt(F)) throw new Error('BigInt is too big')
// Using global.BigInt instead of BigInt due to browser understanding
return global.BigInt(bigInt.toString())
}
Buffer.prototype.writeBigUInt128LE =
Buffer.prototype.writeBigUInt128LE ||
function (jsBigInt, offset = 0) {
const bigInt = new BN(jsBigInt.toString())
if (bigInt.gt(FF)) throw new Error('BigInt is too big')
const arrayBuf = bigInt.toArray('le', this.length)
for (let i = 0; i < this.length; i++) this[i] = arrayBuf[i]
return offset
}
Buffer.prototype.readBigUInt128LE =
Buffer.prototype.readBigUInt128LE ||
function (offset = 0) {
const bigInt = new BN(this, 16, 'le')
if (bigInt.gt(FF)) throw new Error('BigInt is too big')
// Using global.BigInt instead of BigInt due to browser understanding
return global.BigInt(bigInt.toString())
}

@@ -44,13 +54,13 @@ /**

case 'u8':
return 'writeUInt8';
return 'writeUInt8'
case 'u16':
return 'writeUInt16LE';
return 'writeUInt16LE'
case 'u32':
return 'writeUInt32LE';
return 'writeUInt32LE'
case 'u64':
return 'writeBigUInt64LE';
return 'writeBigUInt64LE'
case 'u128':
return 'writeBigUInt128LE';
return 'writeBigUInt128LE'
default:
throw new Error('Invalid type');
throw new Error('Invalid type')
}

@@ -62,13 +72,13 @@ }

case 'u8':
return 'readUInt8';
return 'readUInt8'
case 'u16':
return 'readUInt16LE';
return 'readUInt16LE'
case 'u32':
return 'readUInt32LE';
return 'readUInt32LE'
case 'u64':
return 'readBigUInt64LE';
return 'readBigUInt64LE'
case 'u128':
return 'readBigUInt128LE';
return 'readBigUInt128LE'
default:
throw new Error('Invalid type');
throw new Error('Invalid type')
}

@@ -82,17 +92,17 @@ }

constructor(value, type, byteLength) {
this.value = value;
this.type = type;
this.space = byteLength;
this.value = value
this.type = type
this.space = byteLength
}
toBuffer = () => {
const buf = Buffer.allocUnsafe(this.space);
buf[type2Write(this.type)](this.value, 0);
return buf;
const buf = Buffer.allocUnsafe(this.space)
buf[type2Write(this.type)](this.value, 0)
return buf
}
fromBuffer = (buf) => {
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer');
this.value = buf[type2Read(this.type)](0);
return this.value;
if (!Buffer.isBuffer(buf)) throw new Error('Invalid buffer')
this.value = buf[type2Read(this.type)](0)
return this.value
}

@@ -103,3 +113,3 @@ }

constructor(value = 0) {
super(value, 'u8', 1);
super(value, 'u8', 1)
}

@@ -110,3 +120,3 @@ }

constructor(value = 0) {
super(value, 'u16', 2);
super(value, 'u16', 2)
}

@@ -117,3 +127,3 @@ }

constructor(value = 0) {
super(value, 'u32', 4);
super(value, 'u32', 4)
}

@@ -124,3 +134,3 @@ }

constructor(value = 0) {
super(value, 'u64', 8);
super(value, 'u64', 8)
}

@@ -131,6 +141,6 @@ }

constructor(value = 0) {
super(value, 'u128', 16);
super(value, 'u128', 16)
}
}
module.exports = { u8, u16, u32, u64, u128 }
module.exports = { u8, u16, u32, u64, u128 }
const pack = (...items) => {
return Buffer.concat(items.map(type => type.toBuffer()));
return Buffer.concat(items.map((type) => type.toBuffer()))
}
module.exports = { pack }
module.exports = { pack }
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc