Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/types/uint-64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as bigInt from "big-integer";
import { isInstance } from "big-integer";
import { Buffer } from "buffer/";

const HEX_REGEX = /^[A-F0-9]{16}$/;
const HEX_REGEX = /^[A-F0-9]{1,16}$/;
const mask = bigInt(0x00000000ffffffff);

/**
Expand Down Expand Up @@ -54,14 +54,12 @@ class UInt64 extends UInt {
}

if (typeof val === "string") {
if (val === "0") {
return UInt64.from(0);
}

if (!HEX_REGEX.test(val)) {
throw new Error(`${val} is not a valid hex-string`);
}
buf = Buffer.from(val, "hex");

const strBuf = val.padStart(16, "0");
buf = Buffer.from(strBuf, "hex");
return new UInt64(buf);
}

Expand Down
48 changes: 48 additions & 0 deletions test/uint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ const json = {
LowNode: "0",
};

const binaryEntry1 =
"1100642200000000320000000000000002580CB3C1AD2C371136AEA434246D971C5FCCD32CBF520667E131AB7B10D706E7528214BA53D10260FFCC968ACD16BA30F7CEABAD6E5D92011340A3454ACED87177146EABD5E4A256021D836D1E3617618B1EB362D10B0D1BAC6AE1ED9E8D280BBE0B6656748FD647231851C6C650794D5E6852DFA1E35E68630F";
const jsonEntry1 = {
Flags: 0,
IndexPrevious: "2",
Indexes: [
"A3454ACED87177146EABD5E4A256021D836D1E3617618B1EB362D10B0D1BAC6A",
"E1ED9E8D280BBE0B6656748FD647231851C6C650794D5E6852DFA1E35E68630F",
],
LedgerEntryType: "DirectoryNode",
Owner: "rHzDaMNybxQppiE3uWyt2N265KvAKdiRdP",
RootIndex: "0CB3C1AD2C371136AEA434246D971C5FCCD32CBF520667E131AB7B10D706E752",
index: "0B4A2E68C111F7E42FAEEE405F7344560C8240840B151D9D04131EB79D080167",
};

const binaryEntry2 =
"1100722200210000250178D1CA37000000000000000038000000000000028355C0C37CE200B509E0A529880634F7841A9EF4CB65F03C12E6004CFAD9718D66946280000000000000000000000000000000000000004743420000000000000000000000000000000000000000000000000166D6071AFD498D000000000000000000000000000047434200000000002599D1D255BCA61189CA64C84528F2FCBE4BFC3867800000000000000000000000000000000000000047434200000000006EEBB1D1852CE667876A0B3630861FB6C6AB358E";
const jsonEntry2 = {
Balance: {
currency: "GCB",
issuer: "rrrrrrrrrrrrrrrrrrrrBZbvji",
value: "0",
},
Flags: 2162688,
HighLimit: {
currency: "GCB",
issuer: "rBfVgTnsdh8ckC19RM8aVGNuMZnpwrMP6n",
value: "0",
},
HighNode: "283",
LedgerEntryType: "RippleState",
LowLimit: {
currency: "GCB",
issuer: "rhRFGCy2RJTA8oxkjjtYTvofPVGqcgvXWj",
value: "2000000",
},
LowNode: "0",
PreviousTxnID:
"C0C37CE200B509E0A529880634F7841A9EF4CB65F03C12E6004CFAD9718D6694",
PreviousTxnLgrSeq: 24695242,
index: "0000041EFD027808D3F78C8352F97E324CB816318E00B977C74ECDDC7CD975B2",
};

test("compareToTests[0]", () => {
expect(UInt8.from(124).compareTo(UInt64.from(124))).toBe(0);
});
Expand Down Expand Up @@ -64,6 +107,11 @@ test("UInt64 from string zero", () => {
expect(encode(json)).toEqual(binary);
});

test("UInt64 from non 16 length hex", () => {
expect(encode(jsonEntry1)).toEqual(binaryEntry1);
expect(encode(jsonEntry2)).toEqual(binaryEntry2);
});

test("valueOfTests", () => {
let val = UInt8.from(1);
val |= 0x2;
Expand Down