@cosmjs/math
Advanced tools
Comparing version 0.23.0-alpha.0 to 0.23.0-alpha.1
@@ -23,3 +23,13 @@ "use strict"; | ||
} | ||
/** @deprecated use Uint32.fromBytes */ | ||
static fromBigEndianBytes(bytes) { | ||
return Uint32.fromBytes(bytes); | ||
} | ||
/** | ||
* Creates a Uint32 from a fixed length byte array. | ||
* | ||
* @param bytes a list of exactly 4 bytes | ||
* @param endianess defaults to big endian | ||
*/ | ||
static fromBytes(bytes, endianess = "be") { | ||
if (bytes.length !== 4) { | ||
@@ -33,6 +43,13 @@ throw new Error("Invalid input length. Expected 4 bytes."); | ||
} | ||
const beBytes = endianess === "be" ? bytes : Array.from(bytes).reverse(); | ||
// Use mulitiplication instead of shifting since bitwise operators are defined | ||
// on SIGNED int32 in JavaScript and we don't want to risk surprises | ||
return new Uint32(bytes[0] * 2 ** 24 + bytes[1] * 2 ** 16 + bytes[2] * 2 ** 8 + bytes[3]); | ||
return new Uint32(beBytes[0] * 2 ** 24 + beBytes[1] * 2 ** 16 + beBytes[2] * 2 ** 8 + beBytes[3]); | ||
} | ||
static fromString(str) { | ||
if (!str.match(/^[0-9]+$/)) { | ||
throw new Error("Invalid string format"); | ||
} | ||
return new Uint32(Number.parseInt(str, 10)); | ||
} | ||
toBytesBigEndian() { | ||
@@ -123,3 +140,13 @@ // Use division instead of shifting since bitwise operators are defined | ||
} | ||
/** @deprecated use Uint64.fromBytes */ | ||
static fromBytesBigEndian(bytes) { | ||
return Uint64.fromBytes(bytes); | ||
} | ||
/** | ||
* Creates a Uint64 from a fixed length byte array. | ||
* | ||
* @param bytes a list of exactly 8 bytes | ||
* @param endianess defaults to big endian | ||
*/ | ||
static fromBytes(bytes, endianess = "be") { | ||
if (bytes.length !== 8) { | ||
@@ -133,7 +160,4 @@ throw new Error("Invalid input length. Expected 8 bytes."); | ||
} | ||
const asArray = []; | ||
for (let i = 0; i < bytes.length; ++i) { | ||
asArray.push(bytes[i]); | ||
} | ||
return new Uint64(new bn_js_1.default([...asArray])); | ||
const beBytes = endianess === "be" ? Array.from(bytes) : Array.from(bytes).reverse(); | ||
return new Uint64(new bn_js_1.default(beBytes)); | ||
} | ||
@@ -176,2 +200,8 @@ static fromString(str) { | ||
exports.Uint64 = Uint64; | ||
// Assign classes to unused variables in order to verify static interface conformance at compile time. | ||
// Workaround for https://github.com/microsoft/TypeScript/issues/33892 | ||
const _int53Class = Int53; | ||
const _uint53Class = Uint53; | ||
const _uint32Class = Uint32; | ||
const _uint64Class = Uint64; | ||
//# sourceMappingURL=integers.js.map |
{ | ||
"name": "@cosmjs/math", | ||
"version": "0.23.0-alpha.0", | ||
"version": "0.23.0-alpha.1", | ||
"description": "Math helpers for blockchain projects", | ||
@@ -52,3 +52,3 @@ "contributors": [ | ||
}, | ||
"gitHead": "5e79100fe341f7f745d28d8d4616912e2122e1ed" | ||
"gitHead": "864f3d237271b6c93b8bf1913165277ce5890575" | ||
} |
@@ -11,3 +11,12 @@ /** Internal interface to ensure all integer types can be used equally */ | ||
export declare class Uint32 implements Integer, WithByteConverters { | ||
/** @deprecated use Uint32.fromBytes */ | ||
static fromBigEndianBytes(bytes: ArrayLike<number>): Uint32; | ||
/** | ||
* Creates a Uint32 from a fixed length byte array. | ||
* | ||
* @param bytes a list of exactly 4 bytes | ||
* @param endianess defaults to big endian | ||
*/ | ||
static fromBytes(bytes: ArrayLike<number>, endianess?: "be" | "le"): Uint32; | ||
static fromString(str: string): Uint32; | ||
protected readonly data: number; | ||
@@ -35,3 +44,11 @@ constructor(input: number); | ||
export declare class Uint64 implements Integer, WithByteConverters { | ||
/** @deprecated use Uint64.fromBytes */ | ||
static fromBytesBigEndian(bytes: ArrayLike<number>): Uint64; | ||
/** | ||
* Creates a Uint64 from a fixed length byte array. | ||
* | ||
* @param bytes a list of exactly 8 bytes | ||
* @param endianess defaults to big endian | ||
*/ | ||
static fromBytes(bytes: ArrayLike<number>, endianess?: "be" | "le"): Uint64; | ||
static fromString(str: string): Uint64; | ||
@@ -38,0 +55,0 @@ static fromNumber(input: number): Uint64; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41633
475