mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-04 13:05:49 +00:00
[ripple-binary-codec] fix ISO when parsing currency code (#1921)
This commit is contained in:
@@ -2,10 +2,13 @@
|
||||
|
||||
## Unreleased
|
||||
### Added
|
||||
- Exported `TRANSACTION_TYPES` value
|
||||
- Exported `TRANSACTION_TYPES` value
|
||||
### Fixed
|
||||
- Adds missing fields from XLS-20 NFT implementation
|
||||
|
||||
## 1.2.3 (2022-2-2)
|
||||
- Fix issue where ISO is invalid when parsing currency code
|
||||
|
||||
## 1.2.2 (2021-12-2)
|
||||
- Fix issue where unsupported currency codes weren't being correctly processed
|
||||
- Added a workaround for rippled UNLModify encoding bug (#1830)
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { Hash160 } from './hash-160'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
const XRP_HEX_REGEX = /^0{40}$/
|
||||
const ISO_REGEX = /^[A-Z0-9]{3}$/
|
||||
const HEX_REGEX = /^[A-F0-9]{40}$/
|
||||
// eslint-disable-next-line no-control-regex
|
||||
const STANDARD_FORMAT_HEX_REGEX = /^0{24}[\x00-\x7F]{6}0{10}$/
|
||||
|
||||
/**
|
||||
* Convert an ISO code to a currency bytes representation
|
||||
@@ -85,14 +88,14 @@ class Currency extends Hash160 {
|
||||
|
||||
constructor(byteBuf: Buffer) {
|
||||
super(byteBuf ?? Currency.XRP.bytes)
|
||||
const code = this.bytes.slice(12, 15)
|
||||
const hex = this.bytes.toString('hex')
|
||||
|
||||
if (this.bytes[0] !== 0) {
|
||||
this._iso = null
|
||||
} else if (/^0*$/.test(this.bytes.toString('hex'))) {
|
||||
if (XRP_HEX_REGEX.test(hex)) {
|
||||
this._iso = 'XRP'
|
||||
} else if (STANDARD_FORMAT_HEX_REGEX.test(hex)) {
|
||||
this._iso = isoCodeFromHex(this.bytes.slice(12, 15))
|
||||
} else {
|
||||
this._iso = isoCodeFromHex(code)
|
||||
this._iso = null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ describe('Hash256', function () {
|
||||
})
|
||||
|
||||
describe('Currency', function () {
|
||||
test('Will throw an error for dodgy XRP ', function () {
|
||||
test('Will throw an error for dodgy XRP', function () {
|
||||
expect(() =>
|
||||
Currency.from('0000000000000000000000005852500000000000'),
|
||||
).toThrow()
|
||||
@@ -70,6 +70,18 @@ describe('Currency', function () {
|
||||
expect(Currency.from('USD').toJSON()).toBe('USD')
|
||||
})
|
||||
|
||||
test('Currency codes with no contiguous zeroes in first 96 type code & reserved bits', function () {
|
||||
expect(
|
||||
Currency.from('0000000023410000000000005852520000000000').iso(),
|
||||
).toBe(null)
|
||||
})
|
||||
|
||||
test('Currency codes with no contiguous zeroes in last 40 reserved bits', function () {
|
||||
expect(
|
||||
Currency.from('0000000000000000000000005852527570656500').iso(),
|
||||
).toBe(null)
|
||||
})
|
||||
|
||||
test('can be constructed from a Buffer', function () {
|
||||
const xrp = new Currency(Buffer.alloc(20))
|
||||
expect(xrp.iso()).toBe('XRP')
|
||||
|
||||
Reference in New Issue
Block a user