Skip to content

Commit

Permalink
update Issue form
Browse files Browse the repository at this point in the history
  • Loading branch information
mvadari committed Nov 16, 2022
1 parent 29517f1 commit 90b6026
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
32 changes: 9 additions & 23 deletions packages/ripple-binary-codec/src/types/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import { Buffer } from 'buffer/'
*/
interface IssueObject extends JsonObject {
currency: string
issuer: string
issuer?: string
}

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

Expand All @@ -43,16 +46,11 @@ class Issue extends SerializedType {
return value
}

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

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

return new Issue(currency)
}

if (isIssueObject(value)) {
const currency = Currency.from(value.currency).toBytes()
if (value.issuer == null) {
return new Issue(currency)
}
const issuer = AccountID.from(value.issuer).toBytes()
return new Issue(Buffer.concat([currency, issuer]))
}
Expand Down Expand Up @@ -80,11 +78,11 @@ class Issue extends SerializedType {
*
* @returns the JSON interpretation of this.bytes
*/
toJSON(): IssueObject | string {
toJSON(): IssueObject {
const parser = new BinaryParser(this.toString())
const currency = Currency.fromParser(parser) as Currency
if (currency.toJSON() === 'XRP') {
return currency.toJSON()
return { currency: currency.toJSON() }
}
const issuer = AccountID.fromParser(parser) as AccountID

Expand All @@ -93,18 +91,6 @@ class Issue extends SerializedType {
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 { Issue, IssueObject }
32 changes: 16 additions & 16 deletions packages/ripple-binary-codec/test/fixtures/codec-fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -4456,9 +4456,9 @@
"Account": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"XChainBridge": {
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
"LockingChainIssue": "XRP",
"LockingChainIssue": {"currency": "XRP"},
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"IssuingChainIssue": "XRP"
"IssuingChainIssue": {"currency": "XRP"}
},
"Fee": "10",
"Flags": 0,
Expand All @@ -4476,9 +4476,9 @@
"Account": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"XChainBridge": {
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
"LockingChainIssue": "XRP",
"LockingChainIssue": {"currency": "XRP"},
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"IssuingChainIssue": "XRP"
"IssuingChainIssue": {"currency": "XRP"}
},
"Fee": "10",
"Flags": 0,
Expand All @@ -4496,9 +4496,9 @@
"Account": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"XChainBridge": {
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
"LockingChainIssue": "XRP",
"LockingChainIssue": {"currency": "XRP"},
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"IssuingChainIssue": "XRP"
"IssuingChainIssue": {"currency": "XRP"}
},
"Fee": "10",
"Flags": 2147483648,
Expand All @@ -4517,9 +4517,9 @@
"Amount": "10000",
"XChainBridge": {
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
"LockingChainIssue": "XRP",
"LockingChainIssue": {"currency": "XRP"},
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"IssuingChainIssue": "XRP"
"IssuingChainIssue": {"currency": "XRP"}
},
"Fee": "10",
"Flags": 2147483648,
Expand All @@ -4537,9 +4537,9 @@
"Amount": "10000",
"XChainBridge": {
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
"LockingChainIssue": "XRP",
"LockingChainIssue": {"currency": "XRP"},
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"IssuingChainIssue": "XRP"
"IssuingChainIssue": {"currency": "XRP"}
},
"Destination": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"Fee": "10",
Expand All @@ -4564,9 +4564,9 @@
"XChainAttestationBatch" : {
"XChainBridge" : {
"IssuingChainDoor" : "rKeSSvHvaMZJp9ykaxutVwkhZgWuWMLnQt",
"IssuingChainIssue" : "XRP",
"IssuingChainIssue" : {"currency": "XRP"},
"LockingChainDoor" : "rJvExveLEL4jNDEeLKCVdxaSCN9cEBnEQC",
"LockingChainIssue" : "XRP"
"LockingChainIssue" : {"currency": "XRP"}
},
"XChainClaimAttestationBatch" : [
{
Expand All @@ -4592,9 +4592,9 @@
"Account": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"XChainBridge": {
"LockingChainDoor": "rGzx83BVoqTYbGn7tiVAnFw7cbxjin13jL",
"LockingChainIssue": "XRP",
"LockingChainIssue": {"currency": "XRP"},
"IssuingChainDoor": "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"IssuingChainIssue": "XRP"
"IssuingChainIssue": {"currency": "XRP"}
},
"Amount": "1000000",
"Fee": "10",
Expand All @@ -4620,9 +4620,9 @@
"XChainAttestationBatch": {
"XChainBridge": {
"IssuingChainDoor": "rKeSSvHvaMZJp9ykaxutVwkhZgWuWMLnQt",
"IssuingChainIssue": "XRP",
"IssuingChainIssue": {"currency": "XRP"},
"LockingChainDoor": "rJvExveLEL4jNDEeLKCVdxaSCN9cEBnEQC",
"LockingChainIssue": "XRP"
"LockingChainIssue": {"currency": "XRP"}
},
"XChainClaimAttestationBatch": [],
"XChainCreateAccountAttestationBatch": [
Expand Down
4 changes: 2 additions & 2 deletions packages/xrpl/src/models/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export interface NFTOffer {

export interface XChainBridge {
LockingChainDoor: string
LockingChainIssue: 'XRP' | IssuedCurrency
LockingChainIssue: Currency
IssuingChainDoor: string
IssuingChainIssue: 'XRP' | IssuedCurrency
IssuingChainIssue: Currency
}

0 comments on commit 90b6026

Please sign in to comment.