Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.

Commit a3e67a7

Browse files
authored
fix: Encode hex strings length <16 to match rippled (#133)
1 parent d67ab9d commit a3e67a7

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

src/types/uint-64.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as bigInt from "big-integer";
44
import { isInstance } from "big-integer";
55
import { Buffer } from "buffer/";
66

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

1010
/**
@@ -54,14 +54,12 @@ class UInt64 extends UInt {
5454
}
5555

5656
if (typeof val === "string") {
57-
if (val === "0") {
58-
return UInt64.from(0);
59-
}
60-
6157
if (!HEX_REGEX.test(val)) {
6258
throw new Error(`${val} is not a valid hex-string`);
6359
}
64-
buf = Buffer.from(val, "hex");
60+
61+
const strBuf = val.padStart(16, "0");
62+
buf = Buffer.from(strBuf, "hex");
6563
return new UInt64(buf);
6664
}
6765

test/uint.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,49 @@ const json = {
2727
LowNode: "0",
2828
};
2929

30+
const binaryEntry1 =
31+
"1100642200000000320000000000000002580CB3C1AD2C371136AEA434246D971C5FCCD32CBF520667E131AB7B10D706E7528214BA53D10260FFCC968ACD16BA30F7CEABAD6E5D92011340A3454ACED87177146EABD5E4A256021D836D1E3617618B1EB362D10B0D1BAC6AE1ED9E8D280BBE0B6656748FD647231851C6C650794D5E6852DFA1E35E68630F";
32+
const jsonEntry1 = {
33+
Flags: 0,
34+
IndexPrevious: "2",
35+
Indexes: [
36+
"A3454ACED87177146EABD5E4A256021D836D1E3617618B1EB362D10B0D1BAC6A",
37+
"E1ED9E8D280BBE0B6656748FD647231851C6C650794D5E6852DFA1E35E68630F",
38+
],
39+
LedgerEntryType: "DirectoryNode",
40+
Owner: "rHzDaMNybxQppiE3uWyt2N265KvAKdiRdP",
41+
RootIndex: "0CB3C1AD2C371136AEA434246D971C5FCCD32CBF520667E131AB7B10D706E752",
42+
index: "0B4A2E68C111F7E42FAEEE405F7344560C8240840B151D9D04131EB79D080167",
43+
};
44+
45+
const binaryEntry2 =
46+
"1100722200210000250178D1CA37000000000000000038000000000000028355C0C37CE200B509E0A529880634F7841A9EF4CB65F03C12E6004CFAD9718D66946280000000000000000000000000000000000000004743420000000000000000000000000000000000000000000000000166D6071AFD498D000000000000000000000000000047434200000000002599D1D255BCA61189CA64C84528F2FCBE4BFC3867800000000000000000000000000000000000000047434200000000006EEBB1D1852CE667876A0B3630861FB6C6AB358E";
47+
const jsonEntry2 = {
48+
Balance: {
49+
currency: "GCB",
50+
issuer: "rrrrrrrrrrrrrrrrrrrrBZbvji",
51+
value: "0",
52+
},
53+
Flags: 2162688,
54+
HighLimit: {
55+
currency: "GCB",
56+
issuer: "rBfVgTnsdh8ckC19RM8aVGNuMZnpwrMP6n",
57+
value: "0",
58+
},
59+
HighNode: "283",
60+
LedgerEntryType: "RippleState",
61+
LowLimit: {
62+
currency: "GCB",
63+
issuer: "rhRFGCy2RJTA8oxkjjtYTvofPVGqcgvXWj",
64+
value: "2000000",
65+
},
66+
LowNode: "0",
67+
PreviousTxnID:
68+
"C0C37CE200B509E0A529880634F7841A9EF4CB65F03C12E6004CFAD9718D6694",
69+
PreviousTxnLgrSeq: 24695242,
70+
index: "0000041EFD027808D3F78C8352F97E324CB816318E00B977C74ECDDC7CD975B2",
71+
};
72+
3073
test("compareToTests[0]", () => {
3174
expect(UInt8.from(124).compareTo(UInt64.from(124))).toBe(0);
3275
});
@@ -64,6 +107,11 @@ test("UInt64 from string zero", () => {
64107
expect(encode(json)).toEqual(binary);
65108
});
66109

110+
test("UInt64 from non 16 length hex", () => {
111+
expect(encode(jsonEntry1)).toEqual(binaryEntry1);
112+
expect(encode(jsonEntry2)).toEqual(binaryEntry2);
113+
});
114+
67115
test("valueOfTests", () => {
68116
let val = UInt8.from(1);
69117
val |= 0x2;

0 commit comments

Comments
 (0)