mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-21 20:55:48 +00:00
fix encoding problems
This commit is contained in:
@@ -20,7 +20,19 @@ function isoToBytes(iso: string): Buffer {
|
|||||||
* Tests if ISO is a valid iso code
|
* Tests if ISO is a valid iso code
|
||||||
*/
|
*/
|
||||||
function isIsoCode(iso: string): boolean {
|
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 {
|
class Currency extends Hash160 {
|
||||||
static readonly XRP = new Currency(Buffer.alloc(20));
|
static readonly XRP = new Currency(Buffer.alloc(20));
|
||||||
private readonly _iso?: string;
|
private readonly _iso?: string;
|
||||||
private readonly _isNative: boolean;
|
// private readonly _isNative: boolean;
|
||||||
|
|
||||||
constructor(byteBuf: Buffer) {
|
constructor(byteBuf: Buffer) {
|
||||||
super(byteBuf ?? Currency.XRP.bytes);
|
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 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)) {
|
// for (let i = bytes.length - 1; i >= 0; i--) {
|
||||||
onlyISO = false;
|
// if (bytes[i] !== 0 && !(i === 12 || i === 13 || i === 14)) {
|
||||||
break;
|
// 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);
|
// const lossLessISO = onlyISO && iso !== "XRP" && ISO_REGEX.test(iso);
|
||||||
this._isNative = onlyISO && code.toString("hex") === "000000";
|
// this._isNative = onlyISO && code.toString("hex") === "000000";
|
||||||
this._iso = this._isNative ? "XRP" : lossLessISO ? iso : undefined;
|
// this._iso = this._isNative ? "XRP" : lossLessISO ? iso : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* Tells if this currency is native
|
// * Tells if this currency is native
|
||||||
*
|
// *
|
||||||
* @returns true if native, false if not
|
// * @returns true if native, false if not
|
||||||
*/
|
// */
|
||||||
isNative(): boolean {
|
// isNative(): boolean {
|
||||||
return this._isNative;
|
// return this._isNative;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the ISO code of this currency
|
* Return the ISO code of this currency
|
||||||
|
|||||||
@@ -313,7 +313,7 @@ function pathSetBinaryTests() {
|
|||||||
0000585E1F3BD02A15D6185F8BB9B57CC60DEDDB37C13157180C769B66D942EE
|
0000585E1F3BD02A15D6185F8BB9B57CC60DEDDB37C13157180C769B66D942EE
|
||||||
69E6DCC940CA48D82337AD000000000000000000000000425443000000000057
|
69E6DCC940CA48D82337AD000000000000000000000000425443000000000057
|
||||||
180C769B66D942EE69E6DCC940CA48D82337AD10000000000000000000000000
|
180C769B66D942EE69E6DCC940CA48D82337AD10000000000000000000000000
|
||||||
58525000000000003000000000000000000000000055534400000000000A20B3
|
00000000000000003000000000000000000000000055534400000000000A20B3
|
||||||
C85F482532A9578DBB3950B85CA06594D100`
|
C85F482532A9578DBB3950B85CA06594D100`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -371,7 +371,7 @@ function pathSetBinaryTests() {
|
|||||||
currency: "BTC",
|
currency: "BTC",
|
||||||
issuer: "r3AWbdp2jQLXLywJypdoNwVSvr81xs3uhn",
|
issuer: "r3AWbdp2jQLXLywJypdoNwVSvr81xs3uhn",
|
||||||
},
|
},
|
||||||
{ currency: "0000000000000000000000005852500000000000" },
|
{ currency: "XRP" },
|
||||||
{
|
{
|
||||||
currency: "USD",
|
currency: "USD",
|
||||||
issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
|
issuer: "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
|
||||||
|
|||||||
@@ -50,10 +50,10 @@ describe("Hash256", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("Currency", function () {
|
describe("Currency", function () {
|
||||||
test("Will have a null iso() for dodgy XRP ", function () {
|
test("Will throw an error for dodgy XRP ", function () {
|
||||||
const bad = Currency.from("0000000000000000000000005852500000000000");
|
expect(() =>
|
||||||
expect(bad.iso()).toBeUndefined();
|
Currency.from("0000000000000000000000005852500000000000")
|
||||||
expect(bad.isNative()).toBe(false);
|
).toThrow();
|
||||||
});
|
});
|
||||||
test("Currency with lowercase letters decode to hex", () => {
|
test("Currency with lowercase letters decode to hex", () => {
|
||||||
expect(Currency.from("xRp").toJSON()).toBe(
|
expect(Currency.from("xRp").toJSON()).toBe(
|
||||||
|
|||||||
Reference in New Issue
Block a user