mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-25 14:45:48 +00:00
X-Address Encoding in Issued Currencies (#113)
* feat(account): allow issued currency to use x-address
This commit is contained in:
@@ -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/";
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
@@ -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", () => {
|
||||
@@ -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 () {
|
||||
|
||||
Reference in New Issue
Block a user