Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ripple-binary-codec/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
* Fix STNumber serialization logic to work with large mantissa scale [10^18, 10^19-1].
* Error if a decimal is passed into a `UInt`-typed field.

## 2.6.0 (2025-12-16)

Expand Down
4 changes: 2 additions & 2 deletions packages/ripple-binary-codec/src/types/uint-16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class UInt16 extends UInt {
return val
}

if (typeof val === 'number') {
if (typeof val === 'number' && Number.isInteger(val)) {
UInt16.checkUintRange(val, 0, 0xffff)

const buf = new Uint8Array(UInt16.width)
writeUInt16BE(buf, val, 0)
return new UInt16(buf)
}

throw new Error('Can not construct UInt16 with given value')
throw new Error('Cannot construct UInt16 from given value')
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/types/uint-32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class UInt32 extends UInt {
return new UInt32(buf)
}

if (typeof val === 'number') {
if (typeof val === 'number' && Number.isInteger(val)) {
UInt32.checkUintRange(val, 0, 0xffffffff)
writeUInt32BE(buf, val, 0)
return new UInt32(buf)
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/types/uint-64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class UInt64 extends UInt {

let buf = new Uint8Array(UInt64.width)

if (typeof val === 'number') {
if (typeof val === 'number' && Number.isInteger(val)) {
if (val < 0) {
throw new Error('value must be an unsigned integer')
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/types/uint-8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UInt8 extends UInt {
return val
}

if (typeof val === 'number') {
if (typeof val === 'number' && Number.isInteger(val)) {
UInt8.checkUintRange(val, 0, 0xff)

const buf = new Uint8Array(UInt8.width)
Expand Down
108 changes: 107 additions & 1 deletion packages/ripple-binary-codec/test/uint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UInt8, UInt64 } from '../src/types'
import { UInt8, UInt16, UInt32, UInt64 } from '../src/types'
import { encode, decode } from '../src'

const binary =
Expand Down Expand Up @@ -195,3 +195,109 @@ it('UInt64 is parsed as base 10 for MPT amounts', () => {
expect(typeof decodedToken.MPTAmount).toBe('string')
expect(decodedToken.MPTAmount).toBe('100')
})

describe('UInt decimal validation', () => {
describe('UInt8', () => {
it('should throw error when passed a decimal number', () => {
expect(() => UInt8.from(1.5)).toThrow(
new Error('Cannot construct UInt8 from given value'),
)
})

it('should throw error when passed a negative decimal', () => {
expect(() => UInt8.from(-1.5)).toThrow(
new Error('Cannot construct UInt8 from given value'),
)
})

it('should throw error when passed a small decimal', () => {
expect(() => UInt8.from(0.1)).toThrow(
new Error('Cannot construct UInt8 from given value'),
)
})

it('should accept valid integer values', () => {
expect(() => UInt8.from(0)).not.toThrow()
expect(() => UInt8.from(1)).not.toThrow()
expect(() => UInt8.from(255)).not.toThrow()
})
})

describe('UInt16', () => {
it('should throw error when passed a decimal number', () => {
expect(() => UInt16.from(100.5)).toThrow(
new Error('Cannot construct UInt16 from given value'),
)
})

it('should throw error when passed a negative decimal', () => {
expect(() => UInt16.from(-100.5)).toThrow(
new Error('Cannot construct UInt16 from given value'),
)
})

it('should throw error when passed a small decimal', () => {
expect(() => UInt16.from(0.001)).toThrow(
new Error('Cannot construct UInt16 from given value'),
)
})

it('should accept valid integer values', () => {
expect(() => UInt16.from(0)).not.toThrow()
expect(() => UInt16.from(1000)).not.toThrow()
expect(() => UInt16.from(65535)).not.toThrow()
})
})

describe('UInt32', () => {
it('should throw error when passed a decimal number', () => {
expect(() => UInt32.from(1000.5)).toThrow(
new Error('Cannot construct UInt32 from given value'),
)
})

it('should throw error when passed a negative decimal', () => {
expect(() => UInt32.from(-1000.5)).toThrow(
new Error('Cannot construct UInt32 from given value'),
)
})

it('should throw error when passed a small decimal', () => {
expect(() => UInt32.from(0.0001)).toThrow(
new Error('Cannot construct UInt32 from given value'),
)
})

it('should accept valid integer values', () => {
expect(() => UInt32.from(0)).not.toThrow()
expect(() => UInt32.from(100000)).not.toThrow()
expect(() => UInt32.from(4294967295)).not.toThrow()
})
})

describe('UInt64', () => {
it('should throw error when passed a decimal number', () => {
expect(() => UInt64.from(10000.5)).toThrow(
new Error('Cannot construct UInt64 from given value'),
)
})

it('should throw error when passed a negative decimal', () => {
expect(() => UInt64.from(-10000.5)).toThrow(
new Error('Cannot construct UInt64 from given value'),
)
})

it('should throw error when passed a small decimal', () => {
expect(() => UInt64.from(0.00001)).toThrow(
new Error('Cannot construct UInt64 from given value'),
)
})

it('should accept valid integer values', () => {
expect(() => UInt64.from(0)).not.toThrow()
expect(() => UInt64.from(1000000)).not.toThrow()
expect(() => UInt64.from(BigInt('9223372036854775807'))).not.toThrow()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ describe('Lending Protocol IT', () => {
Account: loanBrokerWallet.address,
LoanBrokerID: loanBrokerObjectId,
PrincipalRequested: '100000',
InterestRate: 0.1,
InterestRate: 0,
Counterparty: borrowerWallet.address,
PaymentTotal: 1,
}
Expand Down
Loading