fix: encoding issue with encoding non-traditional currency codes (#1857)

* add test

* fix decoding issue
This commit is contained in:
Mayukha Vadari
2021-12-02 14:43:26 -05:00
committed by GitHub
parent 8461ef6804
commit fe845de884
2 changed files with 8 additions and 1 deletions

View File

@@ -89,7 +89,7 @@ class Currency extends Hash160 {
if (this.bytes[0] !== 0) {
this._iso = null
} else if (code.toString('hex') === '000000') {
} else if (/^0*$/.test(this.bytes.toString('hex'))) {
this._iso = 'XRP'
} else {
this._iso = isoCodeFromHex(code)

View File

@@ -69,6 +69,7 @@ describe('Currency', function () {
expect(Currency.from('X8P').toJSON()).toBe('X8P')
expect(Currency.from('USD').toJSON()).toBe('USD')
})
test('can be constructed from a Buffer', function () {
const xrp = new Currency(Buffer.alloc(20))
expect(xrp.iso()).toBe('XRP')
@@ -77,6 +78,12 @@ describe('Currency', function () {
const currency = '015841551A748AD2C1F76FF6ECB0CCCD00000000'
expect(Currency.from(currency).toJSON()).toBe(currency)
})
test('Can handle other non-standard currency codes', () => {
const currency = '0000000000414C6F676F30330000000000000000'
expect(Currency.from(currency).toJSON()).toBe(currency)
})
test('throws on invalid reprs', function () {
expect(() => Currency.from(Buffer.alloc(19))).toThrow()
expect(() => Currency.from(1)).toThrow()