Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: run binary-codec tests in the browser #2566

Merged
merged 9 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions packages/ripple-binary-codec/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
* Remove `assert` dependency. If you were catching `AssertionError` you need to change to `Error`.
* Remove `create-hash` in favor of `@noble/hashes`

### Changes
* Update type definitions which causing errors in tests that the code already supported
* `makeParser` to accept a `Buffer` in addition to `string`
* `SerializedType` constructor allows not passing in a byte array
* `Comparable` is now a generic type so that it allows `compareTo` methods to take more that the type itself.

## 1.10.0 (2023-09-27)
### Added
- Support for the XChainBridge amendment.
Expand Down
15 changes: 15 additions & 0 deletions packages/ripple-binary-codec/karma.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const baseKarmaConfig = require('../../karma.config')
const webpackConfig = require('./test/webpack.config')
delete webpackConfig.entry

module.exports = function (config) {
baseKarmaConfig(config)

config.set({
base: '',
webpack: webpackConfig,

// list of files / patterns to load in the browser
files: ['test/**/*.test.ts'],
})
}
7 changes: 4 additions & 3 deletions packages/ripple-binary-codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
"ripple-address-codec": "^5.0.0-beta.0"
},
"scripts": {
"build": "tsc -b && copyfiles ./src/enums/definitions.json ./dist/enums/",
"clean": "rm -rf ./dist ./coverage tsconfig.tsbuildinfo",
"build": "tsc --build tsconfig.build.json && copyfiles ./src/enums/definitions.json ./dist/enums/",
"clean": "rm -rf ./dist ./coverage ./test/testCompiledForWeb tsconfig.build.tsbuildinfo",
"prepublishOnly": "npm test",
"test": "npm run build && jest --verbose false --silent=false ./test/*.test.js",
"test": "npm run build && jest --verbose false --silent=false ./test/*.test.ts",
"test:browser": "npm run build && karma start ./karma.config.js",
"lint": "eslint . --ext .ts --ext .test.js"
},
"keywords": [
Expand Down
11 changes: 7 additions & 4 deletions packages/ripple-binary-codec/src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ import {
} from './enums'
import { STObject } from './types/st-object'
import { JsonObject } from './types/serialized-type'
import { Buffer } from 'buffer/'

/**
* Construct a BinaryParser
*
* @param bytes hex-string to construct BinaryParser from
ckniffen marked this conversation as resolved.
Show resolved Hide resolved
* @param definitions rippled definitions used to parse the values of transaction types and such.
* Can be customized for sidechains and amendments.
* @returns A BinaryParser
* @returns BinaryParser
*/
const makeParser = (
bytes: string,
bytes: string | Buffer,
definitions?: XrplDefinitionsBase,
): BinaryParser => new BinaryParser(bytes, definitions)
): BinaryParser =>
new BinaryParser(
bytes instanceof Buffer ? bytes.toString('hex') : bytes,
definitions,
)

/**
* Parse BinaryParser into JSON
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/enums/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BytesList, BinaryParser } from '../binary'
import { Buffer } from 'buffer/'

/*
* @brief: Bytes, name, and ordinal representing one type, ledger_type, transaction type, or result
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/enums/field.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Bytes } from './bytes'
import { SerializedType } from '../types/serialized-type'
import { TYPE_WIDTH } from './constants'
import { Buffer } from 'buffer/'

/**
* Encoding information for a rippled field, often used in transactions.
Expand Down
2 changes: 0 additions & 2 deletions packages/ripple-binary-codec/src/hash-prefixes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Buffer } from 'buffer/'

/**
* Write a 32 bit integer to a Buffer
*
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/hashes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HashPrefix } from './hash-prefixes'
import { Hash256 } from './types'
import { BytesList } from './serdes/binary-serializer'
import { Buffer } from 'buffer/'

import { sha512 } from '@xrplf/isomorphic/sha512'

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/quality.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { coreTypes } from './types'
import { Buffer } from 'buffer/'

import BigNumber from 'bignumber.js'

/**
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/serdes/binary-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
FieldInstance,
} from '../enums'
import { type SerializedType } from '../types/serialized-type'
import { Buffer } from 'buffer/'

/**
* BinaryParser is used to compute fields and values from a HexString
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FieldInstance } from '../enums'
import { type SerializedType } from '../types/serialized-type'
import { Buffer } from 'buffer/'

/**
* Bytes list is a collection of buffer objects
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/shamap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { HashPrefix } from './hash-prefixes'
import { Sha512Half } from './hashes'
import { Hash256 } from './types/hash-256'
import { BytesList } from './serdes/binary-serializer'
import { Buffer } from 'buffer/'

/**
* Abstract class describing a SHAMapNode
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/account-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
xAddressToClassicAddress,
} from 'ripple-address-codec'
import { Hash160 } from './hash-160'
import { Buffer } from 'buffer/'

const HEX_REGEX = /^[A-F0-9]{40}$/

Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/types/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BinaryParser } from '../serdes/binary-parser'
import { AccountID } from './account-id'
import { Currency } from './currency'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'

import BigNumber from 'bignumber.js'

/**
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/blob.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SerializedType } from './serialized-type'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Variable length encoded type
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/currency.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hash160 } from './hash-160'
import { Buffer } from 'buffer/'

const XRP_HEX_REGEX = /^0{40}$/
const ISO_REGEX = /^[A-Z0-9a-z?!@#$%^&*(){}[\]|]{3}$/
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/hash-128.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hash } from './hash'
import { Buffer } from 'buffer/'

/**
* Hash with a width of 128 bits
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/hash-160.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hash } from './hash'
import { Buffer } from 'buffer/'

/**
* Hash with a width of 160 bits
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/hash-256.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hash } from './hash'
import { Buffer } from 'buffer/'

/**
* Hash with a width of 256 bits
Expand Down
3 changes: 1 addition & 2 deletions packages/ripple-binary-codec/src/types/hash.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Comparable } from './serialized-type'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Base class defining how to encode and decode hashes
*/
class Hash extends Comparable {
class Hash extends Comparable<Hash | string> {
static readonly width: number

constructor(bytes: Buffer) {
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BinaryParser } from '../serdes/binary-parser'
import { AccountID } from './account-id'
import { Currency } from './currency'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'

/**
* Interface for JSON objects that represent amounts
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/path-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AccountID } from './account-id'
import { Currency } from './currency'
import { BinaryParser } from '../serdes/binary-parser'
import { SerializedType, JsonObject } from './serialized-type'
import { Buffer } from 'buffer/'

/**
* Constants for separating Paths in a PathSet
Expand Down
24 changes: 14 additions & 10 deletions packages/ripple-binary-codec/src/types/serialized-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BytesList } from '../serdes/binary-serializer'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

import { XrplDefinitionsBase } from '../enums'

type JSON = string | number | boolean | null | undefined | JSON[] | JsonObject
Expand All @@ -13,7 +13,7 @@ type JsonObject = { [key: string]: JSON }
class SerializedType {
protected readonly bytes: Buffer = Buffer.alloc(0)

constructor(bytes: Buffer) {
constructor(bytes?: Buffer) {
this.bytes = bytes ?? Buffer.alloc(0)
}

Expand Down Expand Up @@ -80,26 +80,30 @@ class SerializedType {
}

/**
* Base class for SerializedTypes that are comparable
* Base class for SerializedTypes that are comparable.
*
* @template T - What types you want to allow comparisons between. You must specify all types.
ckniffen marked this conversation as resolved.
Show resolved Hide resolved
*
* Ex. `class Hash extends Comparable<Hash | string>`
*/
class Comparable extends SerializedType {
lt(other: Comparable): boolean {
class Comparable<T extends Object> extends SerializedType {
lt(other: T): boolean {
return this.compareTo(other) < 0
}

eq(other: Comparable): boolean {
eq(other: T): boolean {
return this.compareTo(other) === 0
}

gt(other: Comparable): boolean {
gt(other: T): boolean {
return this.compareTo(other) > 0
}

gte(other: Comparable): boolean {
gte(other: T): boolean {
return this.compareTo(other) > -1
}

lte(other: Comparable): boolean {
lte(other: T): boolean {
return this.compareTo(other) < 1
}

Expand All @@ -109,7 +113,7 @@ class Comparable extends SerializedType {
* @param other The comparable object to compare this to
* @returns A number denoting the relationship of this and other
*/
compareTo(other: Comparable): number {
compareTo(other: T): number {
throw new Error(`cannot compare ${this.toString()} and ${other.toString()}`)
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/st-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DEFAULT_DEFINITIONS, XrplDefinitionsBase } from '../enums'
import { SerializedType, JsonObject } from './serialized-type'
import { STObject } from './st-object'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

const ARRAY_END_MARKER = Buffer.from([0xf1])
const ARRAY_END_MARKER_NAME = 'ArrayEndMarker'
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/types/st-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SerializedType, JsonObject } from './serialized-type'
import { xAddressToClassicAddress, isValidXAddress } from 'ripple-address-codec'
import { BinaryParser } from '../serdes/binary-parser'
import { BinarySerializer, BytesList } from '../serdes/binary-serializer'
import { Buffer } from 'buffer/'

import { STArray } from './st-array'

const OBJECT_END_MARKER_BYTE = Buffer.from([0xe1])
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/uint-16.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UInt } from './uint'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Derived UInt class for serializing/deserializing 16 bit UInt
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/uint-32.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UInt } from './uint'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Derived UInt class for serializing/deserializing 32 bit UInt
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/uint-64.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UInt } from './uint'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

const HEX_REGEX = /^[a-fA-F0-9]{1,16}$/
const mask = BigInt(0x00000000ffffffff)
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/uint-8.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UInt } from './uint'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Derived UInt class for serializing/deserializing 8 bit UInt
Expand Down
5 changes: 2 additions & 3 deletions packages/ripple-binary-codec/src/types/uint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Comparable } from './serialized-type'
import { Buffer } from 'buffer/'

/**
* Compare numbers and bigInts n1 and n2
Expand All @@ -15,7 +14,7 @@ function compare(n1: number | bigint, n2: number | bigint): number {
/**
* Base class for serializing and deserializing unsigned integers.
*/
abstract class UInt extends Comparable {
abstract class UInt extends Comparable<UInt | number> {
protected static width: number

constructor(bytes: Buffer) {
Expand All @@ -28,7 +27,7 @@ abstract class UInt extends Comparable {
* @param other other UInt to compare this to
* @returns -1, 0, or 1 depending on how the objects relate to each other
*/
compareTo(other: UInt): number {
compareTo(other: UInt | number): number {
return compare(this.valueOf(), other.valueOf())
}

Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/vector-256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { SerializedType } from './serialized-type'
import { BinaryParser } from '../serdes/binary-parser'
import { Hash256 } from './hash-256'
import { BytesList } from '../serdes/binary-serializer'
import { Buffer } from 'buffer/'

/**
* TypeGuard for Array<string>
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/types/xchain-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BinaryParser } from '../serdes/binary-parser'

import { AccountID } from './account-id'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'

import { Issue, IssueObject } from './issue'

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { loadFixture } = require('./utils')
const { coreTypes } = require('../src/types')
import { coreTypes } from '../src/types'
import fixtures from './fixtures/data-driven-tests.json'
const { Amount } = coreTypes
const fixtures = loadFixture('data-driven-tests.json')

function amountErrorTests() {
fixtures.values_tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fixtures = require('./fixtures/codec-fixtures.json')
const { decode, encode, decodeLedgerData } = require('../src')
import fixtures from './fixtures/codec-fixtures.json'
import { decode, encode, decodeLedgerData } from '../src'

function json(object) {
return JSON.stringify(object)
Expand All @@ -13,12 +13,12 @@ describe('ripple-binary-codec', function () {
function makeSuite(name, entries) {
describe(name, function () {
entries.forEach((t, testN) => {
test(`${name}[${testN}] can encode ${truncateForDisplay(
it(`${name}[${testN}] can encode ${truncateForDisplay(
json(t.json),
)} to ${truncateForDisplay(t.binary)}`, () => {
expect(encode(t.json)).toEqual(t.binary)
})
test(`${name}[${testN}] can decode ${truncateForDisplay(
it(`${name}[${testN}] can decode ${truncateForDisplay(
t.binary,
)} to ${truncateForDisplay(json(t.json))}`, () => {
const decoded = decode(t.binary)
Expand All @@ -33,7 +33,7 @@ describe('ripple-binary-codec', function () {
describe('ledgerData', function () {
if (fixtures.ledgerData) {
fixtures.ledgerData.forEach((t, testN) => {
test(`ledgerData[${testN}] can decode ${t.binary} to ${json(
it(`ledgerData[${testN}] can decode ${t.binary} to ${json(
t.json,
)}`, () => {
const decoded = decodeLedgerData(t.binary)
Expand Down
Loading