From f25aecd85339fb208c8c5e9dbb06b1690e8cdb37 Mon Sep 17 00:00:00 2001 From: Mayukha Vadari Date: Thu, 24 Jun 2021 17:35:49 -0400 Subject: [PATCH] fix encoding problems --- .../ripple-binary-codec/src/types/currency.ts | 63 ++++++++++++------- .../test/binary-parser.test.js | 4 +- .../ripple-binary-codec/test/hash.test.js | 8 +-- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/packages/ripple-binary-codec/src/types/currency.ts b/packages/ripple-binary-codec/src/types/currency.ts index 0c92afe5..ee39ccc1 100644 --- a/packages/ripple-binary-codec/src/types/currency.ts +++ b/packages/ripple-binary-codec/src/types/currency.ts @@ -20,7 +20,19 @@ function isoToBytes(iso: string): Buffer { * Tests if ISO is a valid iso code */ function isIsoCode(iso: string): boolean { - return iso.length === 3; + return ISO_REGEX.test(iso); +} + +function isoCodeFromHex(code: Buffer): string | undefined { + const iso = code.toString(); + console.log(iso); + if (iso === "XRP") { + throw new Error("Disallowed currency code: to indicate the currency XRP you must use 20 bytes of 0s"); + } + if (isIsoCode(iso)) { + return iso; + } + return undefined; } /** @@ -69,37 +81,46 @@ function bytesFromRepresentation(input: string): Buffer { class Currency extends Hash160 { static readonly XRP = new Currency(Buffer.alloc(20)); private readonly _iso?: string; - private readonly _isNative: boolean; + // private readonly _isNative: boolean; constructor(byteBuf: Buffer) { super(byteBuf ?? Currency.XRP.bytes); - let onlyISO = true; + // let onlyISO = true; - const bytes = this.bytes; + // const bytes = this.bytes; const code = this.bytes.slice(12, 15); - const iso = code.toString(); + // const iso = code.toString(); - for (let i = bytes.length - 1; i >= 0; i--) { - if (bytes[i] !== 0 && !(i === 12 || i === 13 || i === 14)) { - onlyISO = false; - break; - } + + // for (let i = bytes.length - 1; i >= 0; i--) { + // if (bytes[i] !== 0 && !(i === 12 || i === 13 || i === 14)) { + // onlyISO = false; + // break; + // } + // } + + if (this.bytes[0] !== 0 && this.bytes[0] !== 0) { + this._iso = undefined; + } else if (code.toString("hex") === "000000") { + this._iso = "XRP"; + } else { + this._iso = isoCodeFromHex(code); } - const lossLessISO = onlyISO && iso !== "XRP" && ISO_REGEX.test(iso); - this._isNative = onlyISO && code.toString("hex") === "000000"; - this._iso = this._isNative ? "XRP" : lossLessISO ? iso : undefined; + // const lossLessISO = onlyISO && iso !== "XRP" && ISO_REGEX.test(iso); + // this._isNative = onlyISO && code.toString("hex") === "000000"; + // this._iso = this._isNative ? "XRP" : lossLessISO ? iso : undefined; } - /** - * Tells if this currency is native - * - * @returns true if native, false if not - */ - isNative(): boolean { - return this._isNative; - } + // /** + // * Tells if this currency is native + // * + // * @returns true if native, false if not + // */ + // isNative(): boolean { + // return this._isNative; + // } /** * Return the ISO code of this currency diff --git a/packages/ripple-binary-codec/test/binary-parser.test.js b/packages/ripple-binary-codec/test/binary-parser.test.js index 7e733cf8..4ac07360 100644 --- a/packages/ripple-binary-codec/test/binary-parser.test.js +++ b/packages/ripple-binary-codec/test/binary-parser.test.js @@ -313,7 +313,7 @@ function pathSetBinaryTests() { 0000585E1F3BD02A15D6185F8BB9B57CC60DEDDB37C13157180C769B66D942EE 69E6DCC940CA48D82337AD000000000000000000000000425443000000000057 180C769B66D942EE69E6DCC940CA48D82337AD10000000000000000000000000 - 58525000000000003000000000000000000000000055534400000000000A20B3 + 00000000000000003000000000000000000000000055534400000000000A20B3 C85F482532A9578DBB3950B85CA06594D100` ); @@ -371,7 +371,7 @@ function pathSetBinaryTests() { currency: "BTC", issuer: "r3AWbdp2jQLXLywJypdoNwVSvr81xs3uhn", }, - { currency: "0000000000000000000000005852500000000000" }, + { currency: "XRP" }, { currency: "USD", issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B", diff --git a/packages/ripple-binary-codec/test/hash.test.js b/packages/ripple-binary-codec/test/hash.test.js index 70952a46..f22f0e3b 100644 --- a/packages/ripple-binary-codec/test/hash.test.js +++ b/packages/ripple-binary-codec/test/hash.test.js @@ -50,10 +50,10 @@ describe("Hash256", function () { }); describe("Currency", function () { - test("Will have a null iso() for dodgy XRP ", function () { - const bad = Currency.from("0000000000000000000000005852500000000000"); - expect(bad.iso()).toBeUndefined(); - expect(bad.isNative()).toBe(false); + test("Will throw an error for dodgy XRP ", function () { + expect(() => + Currency.from("0000000000000000000000005852500000000000") + ).toThrow(); }); test("Currency with lowercase letters decode to hex", () => { expect(Currency.from("xRp").toJSON()).toBe(