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

Commit

Permalink
X-Address Encoding in Issued Currencies (#113)
Browse files Browse the repository at this point in the history
* feat(account): allow issued currency to use x-address
  • Loading branch information
natenichols authored Feb 4, 2021
1 parent ef39362 commit d70c28b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
16 changes: 15 additions & 1 deletion 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";
import { Buffer } from "buffer/";

Expand Down Expand Up @@ -45,6 +50,15 @@ class AccountID extends Hash160 {
* @returns an AccountID object
*/
static fromBase58(value: string): AccountID {
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

0 comments on commit d70c28b

Please sign in to comment.