-
Notifications
You must be signed in to change notification settings - Fork 512
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
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
37117d9
remove old design stuff
mvadari 158cff2
first pass
mvadari f717990
get testcase passing
mvadari 1b82ee1
add XChainSeqNumCreate, XChainTransfer test cases
mvadari fad3949
add support for XChainClaim
mvadari b38b9ff
edit changelog
mvadari 8a0615d
first draft of new transaction types
mvadari ac5956e
remove old tests
mvadari 40827ab
fix comment
mvadari 6488c41
add transactions to validate list
mvadari d1f560a
match rippled implementation
mvadari a88af97
fix tests
mvadari 15b2522
improve eslint error
mvadari b305689
more matching to rippled
mvadari d6df030
clean up
mvadari f07d47c
fix types
mvadari d440917
back out change
mvadari 8b4026d
fix installs
mvadari b7a35a4
fix ci install again
mvadari 49a6e1d
stop running tests on prepare
mvadari dc6e164
remove federator_info
mvadari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/ripple-binary-codec/src/types/issued-currency.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 modifythis.bytes