mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-21 04:35:49 +00:00
fix: polyfill Buffer for browser compatibility (#112)
* Polyfill with "buffer" and "assert" for browser environments * Instead of relying on webpack to make this library browser compatible, we now use a buffer library that provides a polyfill when used in the browser
This commit is contained in:
@@ -9,6 +9,7 @@ import { sha512Half, transactionID } from "./hashes";
|
||||
import { FieldInstance } from "./enums";
|
||||
import { STObject } from "./types/st-object";
|
||||
import { JsonObject } from "./types/serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
import * as bigInt from "big-integer";
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as enums from "./definitions.json";
|
||||
import { SerializedType } from "../types/serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
const TYPE_WIDTH = 2;
|
||||
const LEDGER_ENTRY_WIDTH = 2;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Write a 32 bit integer to a Buffer
|
||||
*
|
||||
@@ -6,7 +8,7 @@
|
||||
*/
|
||||
function bytes(uint32: number): Buffer {
|
||||
const result = Buffer.alloc(4);
|
||||
result.writeUInt32BE(uint32);
|
||||
result.writeUInt32BE(uint32, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { HashPrefix } from "./hash-prefixes";
|
||||
import * as createHash from "create-hash";
|
||||
import { Hash256 } from "./types/hash-256";
|
||||
import { BytesList } from "./serdes/binary-serializer";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Class for hashing with SHA512
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { coreTypes } from "./types";
|
||||
import { Decimal } from "decimal.js";
|
||||
import * as bigInt from "big-integer";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* class for encoding and decoding quality
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as assert from "assert";
|
||||
import { Field, FieldInstance } from "../enums";
|
||||
import { SerializedType } from "../types/serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* BinaryParser is used to compute fields and values from a HexString
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as assert from "assert";
|
||||
import { FieldInstance } from "../enums";
|
||||
import { SerializedType } from "../types/serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Bytes list is a collection of buffer objects
|
||||
|
||||
@@ -4,6 +4,7 @@ import { HashPrefix } from "./hash-prefixes";
|
||||
import { Sha512Half } from "./hashes";
|
||||
import { Hash256 } from "./types/hash-256";
|
||||
import { BytesList } from "./serdes/binary-serializer";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Abstract class describing a SHAMapNode
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { decodeAccountID, encodeAccountID } from "ripple-address-codec";
|
||||
import { Hash160 } from "./hash-160";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
const HEX_REGEX = /^[A-F0-9]{40}$/;
|
||||
|
||||
@@ -44,7 +45,7 @@ class AccountID extends Hash160 {
|
||||
* @returns an AccountID object
|
||||
*/
|
||||
static fromBase58(value: string): AccountID {
|
||||
return new AccountID(decodeAccountID(value));
|
||||
return new AccountID(Buffer.from(decodeAccountID(value)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,7 +63,9 @@ class AccountID extends Hash160 {
|
||||
* @returns the base58 string defined by this.bytes
|
||||
*/
|
||||
toBase58(): string {
|
||||
return encodeAccountID(this.bytes);
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
return encodeAccountID(this.bytes as any);
|
||||
/* eslint-enable @typescript-eslint/no-explicit-any */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { AccountID } from "./account-id";
|
||||
import { Currency } from "./currency";
|
||||
import { JsonObject, SerializedType } from "./serialized-type";
|
||||
import * as bigInt from "big-integer";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Constants for validating amounts
|
||||
@@ -78,8 +79,8 @@ class Amount extends SerializedType {
|
||||
const number = bigInt(value);
|
||||
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)];
|
||||
intBuf[0].writeUInt32BE(Number(number.shiftRight(32)));
|
||||
intBuf[1].writeUInt32BE(Number(number.and(mask)));
|
||||
intBuf[0].writeUInt32BE(Number(number.shiftRight(32)), 0);
|
||||
intBuf[1].writeUInt32BE(Number(number.and(mask)), 0);
|
||||
|
||||
amount = Buffer.concat(intBuf);
|
||||
|
||||
@@ -102,8 +103,8 @@ class Amount extends SerializedType {
|
||||
|
||||
const num = bigInt(integerNumberString);
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)];
|
||||
intBuf[0].writeUInt32BE(Number(num.shiftRight(32)));
|
||||
intBuf[1].writeUInt32BE(Number(num.and(mask)));
|
||||
intBuf[0].writeUInt32BE(Number(num.shiftRight(32)), 0);
|
||||
intBuf[1].writeUInt32BE(Number(num.and(mask)), 0);
|
||||
|
||||
amount = Buffer.concat(intBuf);
|
||||
|
||||
@@ -151,8 +152,8 @@ class Amount extends SerializedType {
|
||||
const sign = isPositive ? "" : "-";
|
||||
bytes[0] &= 0x3f;
|
||||
|
||||
const msb = bigInt(bytes.slice(0, 4).readUInt32BE());
|
||||
const lsb = bigInt(bytes.slice(4).readUInt32BE());
|
||||
const msb = bigInt(bytes.slice(0, 4).readUInt32BE(0));
|
||||
const lsb = bigInt(bytes.slice(4).readUInt32BE(0));
|
||||
const num = msb.shiftLeft(32).or(lsb);
|
||||
|
||||
return `${sign}${num.toString()}`;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { SerializedType } from "./serialized-type";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Variable length encoded type
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { Hash160 } from "./hash-160";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
const ISO_REGEX = /^[A-Z0-9]{3}$/;
|
||||
const HEX_REGEX = /^[A-F0-9]{40}$/;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Hash } from "./hash";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Hash with a width of 128 bits
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Hash } from "./hash";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Hash with a width of 160 bits
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Hash } from "./hash";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Hash with a width of 256 bits
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Comparable } from "./serialized-type";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Base class defining how to encode and decode hashes
|
||||
@@ -47,8 +48,7 @@ class Hash extends Comparable {
|
||||
* @param other The Hash to compare this to
|
||||
*/
|
||||
compareTo(other: Hash): number {
|
||||
return Buffer.compare(
|
||||
this.bytes,
|
||||
return this.bytes.compare(
|
||||
(this.constructor as typeof Hash).from(other).bytes
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { AccountID } from "./account-id";
|
||||
import { Currency } from "./currency";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { SerializedType, JsonObject } from "./serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Constants for separating Paths in a PathSet
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { BytesList } from "../serdes/binary-serializer";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import * as bigInt from "big-integer";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
type JSON = string | number | boolean | null | undefined | JSON[] | JsonObject;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { SerializedType, JsonObject } from "./serialized-type";
|
||||
import { STObject } from "./st-object";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
const ARRAY_END_MARKER = Buffer.from([0xf1]);
|
||||
const ARRAY_END_MARKER_NAME = "ArrayEndMarker";
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "ripple-address-codec";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { BinarySerializer, BytesList } from "../serdes/binary-serializer";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
const OBJECT_END_MARKER_BYTE = Buffer.from([0xe1]);
|
||||
const OBJECT_END_MARKER = "ObjectEndMarker";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { UInt } from "./uint";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Derived UInt class for serializing/deserializing 16 bit UInt
|
||||
@@ -30,7 +31,7 @@ class UInt16 extends UInt {
|
||||
|
||||
if (typeof val === "number") {
|
||||
const buf = Buffer.alloc(UInt16.width);
|
||||
buf.writeUInt16BE(val);
|
||||
buf.writeUInt16BE(val, 0);
|
||||
return new UInt16(buf);
|
||||
}
|
||||
|
||||
@@ -43,7 +44,7 @@ class UInt16 extends UInt {
|
||||
* @returns the number represented by this.bytes
|
||||
*/
|
||||
valueOf(): number {
|
||||
return this.bytes.readUInt16BE();
|
||||
return this.bytes.readUInt16BE(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { UInt } from "./uint";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Derived UInt class for serializing/deserializing 32 bit UInt
|
||||
@@ -32,12 +33,12 @@ class UInt32 extends UInt {
|
||||
|
||||
if (typeof val === "string") {
|
||||
const num = Number.parseInt(val);
|
||||
buf.writeUInt32BE(num);
|
||||
buf.writeUInt32BE(num, 0);
|
||||
return new UInt32(buf);
|
||||
}
|
||||
|
||||
if (typeof val === "number") {
|
||||
buf.writeUInt32BE(val);
|
||||
buf.writeUInt32BE(val, 0);
|
||||
return new UInt32(buf);
|
||||
}
|
||||
|
||||
@@ -50,7 +51,7 @@ class UInt32 extends UInt {
|
||||
* @returns the number represented by this.bytes
|
||||
*/
|
||||
valueOf(): number {
|
||||
return this.bytes.readUInt32BE();
|
||||
return this.bytes.readUInt32BE(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { UInt } from "./uint";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import * as bigInt from "big-integer";
|
||||
import { isInstance } from "big-integer";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
const HEX_REGEX = /^[A-F0-9]{16}$/;
|
||||
const mask = bigInt(0x00000000ffffffff);
|
||||
@@ -46,8 +47,8 @@ class UInt64 extends UInt {
|
||||
const number = bigInt(val);
|
||||
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)];
|
||||
intBuf[0].writeUInt32BE(Number(number.shiftRight(32)));
|
||||
intBuf[1].writeUInt32BE(Number(number.and(mask)));
|
||||
intBuf[0].writeUInt32BE(Number(number.shiftRight(32)), 0);
|
||||
intBuf[1].writeUInt32BE(Number(number.and(mask)), 0);
|
||||
|
||||
return new UInt64(Buffer.concat(intBuf));
|
||||
}
|
||||
@@ -62,8 +63,8 @@ class UInt64 extends UInt {
|
||||
|
||||
if (isInstance(val)) {
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)];
|
||||
intBuf[0].writeUInt32BE(Number(val.shiftRight(bigInt(32))));
|
||||
intBuf[1].writeUInt32BE(Number(val.and(mask)));
|
||||
intBuf[0].writeUInt32BE(Number(val.shiftRight(bigInt(32))), 0);
|
||||
intBuf[1].writeUInt32BE(Number(val.and(mask)), 0);
|
||||
|
||||
return new UInt64(Buffer.concat(intBuf));
|
||||
}
|
||||
@@ -86,8 +87,8 @@ class UInt64 extends UInt {
|
||||
* @returns the number represented buy this.bytes
|
||||
*/
|
||||
valueOf(): bigInt.BigInteger {
|
||||
const msb = bigInt(this.bytes.slice(0, 4).readUInt32BE());
|
||||
const lsb = bigInt(this.bytes.slice(4).readUInt32BE());
|
||||
const msb = bigInt(this.bytes.slice(0, 4).readUInt32BE(0));
|
||||
const lsb = bigInt(this.bytes.slice(4).readUInt32BE(0));
|
||||
return msb.shiftLeft(bigInt(32)).or(lsb);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { UInt } from "./uint";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Derived UInt class for serializing/deserializing 8 bit UInt
|
||||
@@ -28,7 +29,7 @@ class UInt8 extends UInt {
|
||||
|
||||
if (typeof val === "number") {
|
||||
const buf = Buffer.alloc(UInt8.width);
|
||||
buf.writeUInt8(val);
|
||||
buf.writeUInt8(val, 0);
|
||||
return new UInt8(buf);
|
||||
}
|
||||
|
||||
@@ -41,7 +42,7 @@ class UInt8 extends UInt {
|
||||
* @returns the number represented by this.bytes
|
||||
*/
|
||||
valueOf(): number {
|
||||
return this.bytes.readUInt8();
|
||||
return this.bytes.readUInt8(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as bigInt from "big-integer";
|
||||
import { Comparable } from "./serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* Compare numbers and bigInts n1 and n2
|
||||
|
||||
@@ -2,6 +2,7 @@ import { SerializedType } from "./serialized-type";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Hash256 } from "./hash-256";
|
||||
import { BytesList } from "../serdes/binary-serializer";
|
||||
import { Buffer } from "buffer/";
|
||||
|
||||
/**
|
||||
* TypeGuard for Array<string>
|
||||
|
||||
Reference in New Issue
Block a user