Skip to content

Commit 6680559

Browse files
all tests
1 parent a877ba8 commit 6680559

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

packages/ripple-binary-codec/src/types/issue.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Issue extends SerializedType {
5050
*
5151
* @param value An Amount, object representing an IOU, or a string
5252
* representing an integer amount
53-
* @returns An Amount object
53+
* @returns An Issue object
5454
*/
5555
static from<T extends Issue | IssueObject>(value: T): Issue {
5656
if (value instanceof Issue) {
@@ -87,9 +87,16 @@ class Issue extends SerializedType {
8787
* Read an amount from a BinaryParser
8888
*
8989
* @param parser BinaryParser to read the Amount from
90-
* @returns An Amount object
90+
* @param hint The number of bytes to consume from the parser.
91+
* For an MPT amount, pass 24 (the fixed length for Hash192).
92+
*
93+
* @returns An Issue object
9194
*/
92-
static fromParser(parser: BinaryParser): Issue {
95+
static fromParser(parser: BinaryParser, hint?: number): Issue {
96+
if (hint === Hash192.width) {
97+
const mptBytes = parser.read(Hash192.width)
98+
return new Issue(mptBytes)
99+
}
93100
const currency = parser.read(20)
94101
if (new Currency(currency).toJSON() === 'XRP') {
95102
return new Issue(currency)
@@ -105,7 +112,7 @@ class Issue extends SerializedType {
105112
*/
106113
toJSON(): IssueObject {
107114
// If the buffer is exactly 24 bytes, treat it as an MPT amount.
108-
if (this.toBytes.length === Hash192.width) {
115+
if (this.toBytes().length === Hash192.width) {
109116
return {
110117
mpt_issuance_id: this.toHex().toUpperCase(),
111118
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { BinaryParser } from '../src/binary'
2+
import { Issue } from '../src/types/issue'
3+
4+
describe('Issue type conversion functions', () => {
5+
it(`test from value xrp`, () => {
6+
const xrpJson = { currency: 'XRP' }
7+
const xrpIssue = Issue.from(xrpJson)
8+
expect(xrpIssue.toJSON()).toMatchObject(xrpJson)
9+
})
10+
11+
it(`test from value issued currency`, () => {
12+
const iouJson = {
13+
currency: 'USD',
14+
issuer: 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn',
15+
}
16+
const iouIssue = Issue.from(iouJson)
17+
expect(iouIssue.toJSON()).toMatchObject(iouJson)
18+
})
19+
20+
it(`test from value nonstandard currency`, () => {
21+
const iouJson = {
22+
currency: '0123456789ABCDEF0123456789ABCDEF01234567',
23+
issuer: 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn',
24+
}
25+
const iouIssue = Issue.from(iouJson)
26+
expect(iouIssue.toJSON()).toMatchObject(iouJson)
27+
})
28+
29+
it(`test from value mpt`, () => {
30+
const mptJson = {
31+
// value: '100', // MPT amounts must be an integer string (no decimal point)
32+
mpt_issuance_id: 'BAADF00DBAADF00DBAADF00DBAADF00DBAADF00DBAADF00D',
33+
}
34+
const mptIssue = Issue.from(mptJson)
35+
expect(mptIssue.toJSON()).toMatchObject(mptJson)
36+
})
37+
38+
it(`test from parser xrp`, () => {
39+
const xrpJson = { currency: 'XRP' }
40+
const xrpIssue = Issue.from(xrpJson)
41+
const parser = new BinaryParser(xrpIssue.toHex())
42+
const parserIssue = Issue.fromParser(parser)
43+
expect(parserIssue.toJSON()).toMatchObject(xrpJson)
44+
})
45+
46+
it(`test from parser issued currency`, () => {
47+
const iouJson = {
48+
currency: 'EUR',
49+
issuer: 'rLUEXYuLiQptky37CqLcm9USQpPiz5rkpD',
50+
}
51+
const iouIssue = Issue.from(iouJson)
52+
const parser = new BinaryParser(iouIssue.toHex())
53+
const parserIssue = Issue.fromParser(parser)
54+
expect(parserIssue.toJSON()).toMatchObject(iouJson)
55+
})
56+
57+
it(`test from parser nonstandard currency`, () => {
58+
const iouJson = {
59+
currency: '0123456789ABCDEF0123456789ABCDEF01234567',
60+
issuer: 'rLUEXYuLiQptky37CqLcm9USQpPiz5rkpD',
61+
}
62+
const iouIssue = Issue.from(iouJson)
63+
const parser = new BinaryParser(iouIssue.toHex())
64+
const parserIssue = Issue.fromParser(parser)
65+
expect(parserIssue.toJSON()).toMatchObject(iouJson)
66+
})
67+
68+
it(`test from parser mpt`, () => {
69+
const mptJson = {
70+
// value: '100', // MPT amounts must be an integer string (no decimal point)
71+
mpt_issuance_id: 'BAADF00DBAADF00DBAADF00DBAADF00DBAADF00DBAADF00D',
72+
}
73+
const mptIssue = Issue.from(mptJson)
74+
const parser = new BinaryParser(mptIssue.toHex())
75+
const parserIssue = Issue.fromParser(parser, 24)
76+
expect(parserIssue.toJSON()).toMatchObject(mptJson)
77+
})
78+
79+
it(`throws with invalid input`, () => {
80+
const invalidJson = { random: 123 }
81+
expect(() => {
82+
// @ts-expect-error -- need to test error message
83+
Issue.from(invalidJson)
84+
}).toThrow(new Error('Invalid type to construct an Amount'))
85+
})
86+
})

0 commit comments

Comments
 (0)