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

X-Address Encoding in Issued Currencies #113

Merged
merged 3 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 16 additions & 2 deletions src/types/account-id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { decodeAccountID, encodeAccountID } from "ripple-address-codec";
import {
decodeAccountID,
encodeAccountID,
isValidXAddress,
xAddressToClassicAddress,
} from "ripple-address-codec";
import { Hash160 } from "./hash-160";

const HEX_REGEX = /^[A-F0-9]{40}$/;
Expand Down Expand Up @@ -44,7 +49,16 @@ class AccountID extends Hash160 {
* @returns an AccountID object
*/
static fromBase58(value: string): AccountID {
return new AccountID(decodeAccountID(value));
if (isValidXAddress(value)) {
const classic = xAddressToClassicAddress(value);

if (classic.tag !== false)
throw new Error("Only allowed to have tag on Account or Destination");

value = classic.classicAddress;
}

return new AccountID(Buffer.from(decodeAccountID(value)));
}

/**
Expand Down
34 changes: 34 additions & 0 deletions test/x-address.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ let invalid_json_x_and_tagged = {
SourceTag: 12345,
};

let json_issued_x = {
TakerPays: {
currency: "USD",
issuer: "X7WZKEeNVS2p9Tire9DtNFkzWBZbFtJHWxDjN9fCrBGqVA4",
value: "7072.8",
},
};

let json_issued_r = {
TakerPays: {
currency: "USD",
issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
value: "7072.8",
},
};

let json_issued_with_tag = {
TakerPays: {
currency: "USD",
issuer: "X7WZKEeNVS2p9Tire9DtNFkzWBZbFtSiS2eDBib7svZXuc2",
value: "7072.8",
},
};

describe("X-Address Account is equivalent to a classic address w/ SourceTag", () => {
let encoded_x = encode(json_x1);
let encoded_r = encode(json_r1);
Expand All @@ -114,6 +138,10 @@ describe("X-Address Account is equivalent to a classic address w/ SourceTag", ()
test("Throws when X-Address is invalid", () => {
expect(() => encode(json_invalid_x)).toThrow("checksum_invalid");
});

test("Encodes issued currency w/ x-address", () => {
expect(encode(json_issued_x)).toEqual(encode(json_issued_r));
});
});

describe("Invalid X-Address behavior", () => {
Expand All @@ -128,6 +156,12 @@ describe("Invalid X-Address behavior", () => {
new Error("Cannot have Account X-Address and SourceTag")
);
});

test("Throws when issued currency has tag", () => {
expect(() => encode(json_issued_with_tag)).toThrow(
"Only allowed to have tag on Account or Destination"
);
});
});

describe("ripple-binary-codec x-address test", function () {
Expand Down