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

feat: add support for current sidechain design #2012

Closed
wants to merge 21 commits into from
Closed
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
5 changes: 4 additions & 1 deletion packages/ripple-binary-codec/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# ripple-binary-codec Release History

## Unreleased
### Added
- Support for sidechain transactions

## 1.4.1 (2022-06-02)
### Fixed
- Added a clearer error message for trying to encode an invalid transaction. (Ex. With an incorrect TransactionType)


## 1.4.0 (2022-04-18)
- Updated NFT definitions to match 1.9.0's breaking naming changes

Expand Down
5 changes: 3 additions & 2 deletions packages/ripple-binary-codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
"build:default": "tsc -b && copy .\\src\\enums\\definitions.json .\\dist\\enums",
"build:nix": "tsc -b && cp ./src/enums/definitions.json ./dist/enums",
"clean": "rm -rf ./dist && rm -rf tsconfig.tsbuildinfo",
"prepare": "npm run build && npm test",
"prepare": "npm run build",
"test": "jest",
"lint": "eslint . --ext .ts --ext .test.js"
"lint": "eslint . --ext .ts --ext .test.js",
"prepublishOnly": "npm run build && npm run test"
},
"repository": {
"type": "git",
Expand Down
106 changes: 103 additions & 3 deletions packages/ripple-binary-codec/src/enums/definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"NotPresent": 0,
"UInt64": 3,
"UInt32": 2,
"STArray": 15
"STArray": 15,
"Sidechain": 24,
"XChainClaimProof": 25
},
"LEDGER_ENTRY_TYPES": {
"Any": -3,
Expand All @@ -31,8 +33,10 @@
"Ticket": 84,
"SignerList": 83,
"Offer": 111,
"Sidechain": 105,
"LedgerHashes": 104,
"Amendments": 102,
"CrosschainSequenceNum": 113,
"FeeSettings": 115,
"Escrow": 117,
"PayChannel": 120,
Expand Down Expand Up @@ -825,6 +829,16 @@
"type": "Amount"
}
],
[
"XChainFee",
{
"nth": 11,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "Amount"
}
],
[
"MinimumOffer",
{
Expand Down Expand Up @@ -1175,6 +1189,26 @@
"type": "AccountID"
}
],
[
"ThisChainAccount",
{
"nth": 17,
"isVLEncoded": true,
"isSerialized": true,
"isSigningField": true,
"type": "AccountID"
}
],
[
"OtherChainAccount",
{
"nth": 18,
"isVLEncoded": true,
"isSerialized": true,
"isSigningField": true,
"type": "AccountID"
}
],
[
"ObjectEndMarker",
{
Expand Down Expand Up @@ -1325,6 +1359,16 @@
"type": "STObject"
}
],
[
"XChainProofSig",
{
"nth": 25,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "STObject"
}
],
[
"ArrayEndMarker",
{
Expand Down Expand Up @@ -1435,6 +1479,16 @@
"type": "STArray"
}
],
[
"XChainProofSigs",
{
"nth": 21,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "STArray"
}
],
[
"CloseResolution",
{
Expand Down Expand Up @@ -1515,6 +1569,26 @@
"type": "PathSet"
}
],
[
"Sidechain",
{
"nth": 1,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "Sidechain"
}
],
[
"XChainClaimProof",
{
"nth": 1,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "XChainClaimProof"
}
],
[
"Indexes",
{
Expand Down Expand Up @@ -1655,6 +1729,16 @@
"type": "UInt32"
}
],
[
"XChainSequence",
{
"nth": 47,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "UInt32"
}
],
[
"Channel",
{
Expand Down Expand Up @@ -1848,6 +1932,9 @@
"temUNKNOWN": -265,
"temSEQ_AND_TICKET": -264,
"temBAD_NFTOKEN_TRANSFER_FEE": -263,
"temEQUAL_DOOR_ACCOUNTS": -262,
"temBAD_XChain_Proof": -261,
"temSIDECHAIN_BAD_ISSUES": -260,

"tefFAILURE": -199,
"tefALREADY": -198,
Expand Down Expand Up @@ -1933,8 +2020,14 @@
"tecINSUFFICIENT_FUNDS": 159,
"tecOBJECT_NOT_FOUND": 160,
"tecINSUFFICIENT_PAYMENT": 161,
"tecINCORRECT_ASSET": 162,
"tecTOO_MANY": 163

"tecBAD_XCHAIN_TRANSFER_ISSUE": 162,
"tecBAD_XCHAIN_TRANSFER_SEQ_NUM": 163,
"tecXCHAIN_PROOF_NO_QUORUM": 164,
"tecXCHAIN_PROOF_UNKNOWN_KEY": 165,
"tecXCHAIN_CREATE_ACCOUNT_NONXRP_ISSUE": 166,
"tecXCHAIN_CLAIM_ACCOUNT_DST_EXISTS": 167,
"tecXCHAIN_CLAIM_WRONG_CHAIN": 168
},
"TRANSACTION_TYPES": {
"Invalid": -1,
Expand Down Expand Up @@ -1966,6 +2059,13 @@
"NFTokenCreateOffer": 27,
"NFTokenCancelOffer": 28,
"NFTokenAcceptOffer": 29,
"XChainDoorCreate": 30,
"XChainSeqNumCreate": 31,
"XChainTransfer": 32,
"XChainClaim": 33,
"XChainAccountCreate": 34,
"XChainAccountClaim": 35,

"EnableAmendment": 100,
"SetFee": 101,
"UNLModify": 102
Expand Down
3 changes: 2 additions & 1 deletion packages/ripple-binary-codec/src/types/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class Amount extends SerializedType {
*/
toJSON(): AmountObject | string {
if (this.isNative()) {
const bytes = this.bytes
const bytes = Buffer.alloc(this.bytes.length)
this.bytes.copy(bytes)
Comment on lines -150 to +151
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes a minor issue I ran into, where I wasn't expecting toJSON to modify this.bytes

const isPositive = bytes[0] & 0x40
const sign = isPositive ? '' : '-'
bytes[0] &= 0x3f
Expand Down
6 changes: 6 additions & 0 deletions packages/ripple-binary-codec/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import { Currency } from './currency'
import { Hash128 } from './hash-128'
import { Hash160 } from './hash-160'
import { Hash256 } from './hash-256'
import { IssuedCurrency } from './issued-currency'
import { PathSet } from './path-set'
import { Sidechain } from './sidechain'
import { STArray } from './st-array'
import { STObject } from './st-object'
import { UInt16 } from './uint-16'
import { UInt32 } from './uint-32'
import { UInt64 } from './uint-64'
import { UInt8 } from './uint-8'
import { Vector256 } from './vector-256'
import { XChainClaimProof } from './xchain-claim-proof'

const coreTypes = {
AccountID,
Expand All @@ -28,14 +31,17 @@ const coreTypes = {
Hash128,
Hash160,
Hash256,
IssuedCurrency,
PathSet,
Sidechain,
STArray,
STObject,
UInt8,
UInt16,
UInt32,
UInt64,
Vector256,
XChainClaimProof,
}

Object.values(Field).forEach((field) => {
Expand Down
114 changes: 114 additions & 0 deletions packages/ripple-binary-codec/src/types/issued-currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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
*/
interface IssuedCurrencyObject extends JsonObject {
currency: string
issuer: string
}

/**
* Type guard for AmountObject
*/
function isIssuedCurrencyObject(arg): arg is IssuedCurrencyObject {
const keys = Object.keys(arg).sort()
return keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'
}

/**
* Class for serializing/Deserializing Amounts
*/
class IssuedCurrency extends SerializedType {
static readonly ZERO_ISSUED_CURRENCY: IssuedCurrency = new IssuedCurrency(
Buffer.alloc(20),
)

constructor(bytes: Buffer) {
super(bytes ?? IssuedCurrency.ZERO_ISSUED_CURRENCY.bytes)
}

/**
* Construct an amount from an IOU or string amount
*
* @param value An Amount, object representing an IOU, or a string
* representing an integer amount
* @returns An Amount object
*/
static from<T extends IssuedCurrency | IssuedCurrencyObject | string>(
value: T,
): IssuedCurrency {
if (value instanceof IssuedCurrency) {
return value
}

if (typeof value === 'string') {
IssuedCurrency.assertXrpIsValid(value)

const currency = Currency.from(value).toBytes()

return new IssuedCurrency(currency)
}

if (isIssuedCurrencyObject(value)) {
const currency = Currency.from(value.currency).toBytes()
const issuer = AccountID.from(value.issuer).toBytes()
return new IssuedCurrency(Buffer.concat([currency, issuer]))
}

throw new Error('Invalid type to construct an Amount')
}

/**
* Read an amount from a BinaryParser
*
* @param parser BinaryParser to read the Amount from
* @returns An Amount object
*/
static fromParser(parser: BinaryParser): IssuedCurrency {
const currency = parser.read(20)
if (new Currency(currency).toJSON() === 'XRP') {
return new IssuedCurrency(currency)
}
const currencyAndIssuer = [currency, parser.read(20)]
return new IssuedCurrency(Buffer.concat(currencyAndIssuer))
}

/**
* Get the JSON representation of this Amount
*
* @returns the JSON interpretation of this.bytes
*/
toJSON(): IssuedCurrencyObject | string {
const parser = new BinaryParser(this.toString())
const currency = Currency.fromParser(parser) as Currency
if (currency.toJSON() === 'XRP') {
return currency.toJSON()
}
const issuer = AccountID.fromParser(parser) as AccountID

return {
currency: currency.toJSON(),
issuer: issuer.toJSON(),
}
}

/**
* Validate XRP amount
*
* @param value String representing XRP amount
* @returns void, but will throw if invalid amount
*/
private static assertXrpIsValid(value: string): void {
if (value !== 'XRP') {
throw new Error(`${value} is an illegal amount`)
}
}
}

export { IssuedCurrency, IssuedCurrencyObject }
Loading