From 417978e1315cc792cd4b64339d393da1ba9c6e9f Mon Sep 17 00:00:00 2001 From: Jackson Mills Date: Thu, 21 Apr 2022 14:36:10 -0700 Subject: [PATCH] Fix deriveXAddress null behavior (#1964) * Fix deriveXAddress null behavior * Also check for undefined * Simplify theTag * Add test case for undefined --- packages/ripple-address-codec/HISTORY.md | 3 +++ packages/ripple-address-codec/src/index.ts | 5 +++-- packages/xrpl/test/utils/deriveXAddress.ts | 26 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/ripple-address-codec/HISTORY.md b/packages/ripple-address-codec/HISTORY.md index 80e59f29..13e5af0f 100644 --- a/packages/ripple-address-codec/HISTORY.md +++ b/packages/ripple-address-codec/HISTORY.md @@ -1,5 +1,8 @@ # ripple-address-codec +## Unreleased +- Fixed `encodeXAddress` to handle `null` equivalently to `false`. + ## 4.2.1 (2021-12-1) - Fix issue where npm < 7 could not install the library - Initial pass at linting this codebase with new rules diff --git a/packages/ripple-address-codec/src/index.ts b/packages/ripple-address-codec/src/index.ts index f94ea796..f380dce2 100644 --- a/packages/ripple-address-codec/src/index.ts +++ b/packages/ripple-address-codec/src/index.ts @@ -43,8 +43,9 @@ function encodeXAddress( if (tag > MAX_32_BIT_UNSIGNED_INT) { throw new Error('Invalid tag') } - const theTag = tag === false ? 0 : tag - const flag = tag === false ? 0 : 1 + const theTag = tag || 0 + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- Passing null is a common js mistake + const flag = tag === false || tag == null ? 0 : 1 /* eslint-disable no-bitwise --- * need to use bitwise operations here */ const bytes = Buffer.concat([ diff --git a/packages/xrpl/test/utils/deriveXAddress.ts b/packages/xrpl/test/utils/deriveXAddress.ts index ebe38af8..fa19a8a9 100644 --- a/packages/xrpl/test/utils/deriveXAddress.ts +++ b/packages/xrpl/test/utils/deriveXAddress.ts @@ -22,4 +22,30 @@ describe('deriveXAddress', function () { 'TVVrSWtmQQssgVcmoMBcFQZKKf56QscyWLKnUyiuZW8ALU4', ) }) + + it('does not include tag when null', function () { + assert.equal( + deriveXAddress({ + publicKey: + 'ED02C98225BD1C79E9A4F95C6978026D300AFB7CA2A34358920BCFBCEBE6AFCD6A', + // @ts-expect-error -- Assessing null behavior (Common js mistake) + tag: null, + test: false, + }), + 'X7FbrqVEqdTNoX5qq94rTdarGjeVYmkxi8A1TKAJUnyLL9g', + ) + }) + + it('does not include tag when undefined', function () { + assert.equal( + deriveXAddress({ + publicKey: + 'ED02C98225BD1C79E9A4F95C6978026D300AFB7CA2A34358920BCFBCEBE6AFCD6A', + // @ts-expect-error -- Assessing undefined behavior + tag: undefined, + test: false, + }), + 'X7FbrqVEqdTNoX5qq94rTdarGjeVYmkxi8A1TKAJUnyLL9g', + ) + }) })