-
Notifications
You must be signed in to change notification settings - Fork 142
Various fixes, part 5 #963
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
Changes from all commits
00b4cfc
552f6eb
ad22db8
06d451d
6904d28
eb6ce90
4df96f1
0d3750e
be74640
b9744b3
d2ec4ba
c8d6495
008caed
3250551
e4ed91e
b2fca1d
268a91d
b1a78bf
0eab134
f7fb823
d32b8df
c6e8f0e
9d92c67
c70ca5c
dd57f2f
0e5828a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,10 +20,14 @@ export function manageData( | |
| throw new Error("name must be a string, up to 64 characters"); | ||
| } | ||
|
|
||
| // undefined is accepted (treated as null/delete) for internal round-trip | ||
| // compatibility: fromXDRObject returns undefined for absent optional fields. | ||
| // The public API contract is null for delete-entry. | ||
| if ( | ||
| typeof opts.value !== "string" && | ||
| !Buffer.isBuffer(opts.value) && | ||
| opts.value !== null | ||
| opts.value !== null && | ||
| opts.value !== undefined | ||
| ) { | ||
| throw new Error("value must be a string, Buffer or null"); | ||
| } | ||
|
|
@@ -32,7 +36,7 @@ export function manageData( | |
| if (typeof opts.value === "string") { | ||
| dataValue = Buffer.from(opts.value); | ||
| } else { | ||
| dataValue = opts.value; | ||
| dataValue = opts.value ?? null; | ||
| } | ||
|
Comment on lines
26
to
40
|
||
|
|
||
| if (dataValue !== null && dataValue.length > 64) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,6 +103,34 @@ describe("Operation.changeTrust()", () => { | |
| ).toThrow(TypeError); | ||
| }); | ||
|
|
||
| it("round-trips an Asset changeTrust through fromXDRObject and back", () => { | ||
| const op = Operation.changeTrust({ asset: usd, limit: "50.0000000" }); | ||
| const xdrHex = op.toXDR("hex"); | ||
| const parsed = expectOperationType( | ||
| Operation.fromXDRObject(xdr.Operation.fromXDR(xdrHex, "hex")), | ||
| "changeTrust", | ||
| ); | ||
|
|
||
| // parsed has `line` (not `asset`); changeTrust accepts both | ||
| const rebuilt = Operation.changeTrust(parsed); | ||
| expect(rebuilt).toBeInstanceOf(xdr.Operation); | ||
| expect(rebuilt.toXDR("hex")).toBe(xdrHex); | ||
| }); | ||
|
|
||
| it("round-trips a LiquidityPoolAsset changeTrust through fromXDRObject and back", () => { | ||
| const op = Operation.changeTrust({ asset: lpAsset }); | ||
| const xdrHex = op.toXDR("hex"); | ||
| const parsed = expectOperationType( | ||
| Operation.fromXDRObject(xdr.Operation.fromXDR(xdrHex, "hex")), | ||
| "changeTrust", | ||
| ); | ||
|
|
||
| // parsed has `line` (not `asset`); changeTrust accepts both | ||
| const rebuilt = Operation.changeTrust(parsed); | ||
| expect(rebuilt).toBeInstanceOf(xdr.Operation); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should check that the rebuilt and the original op are the same. An easy way to do this would be string comparing the xdr strings of the two |
||
| expect(rebuilt.toXDR("hex")).toBe(xdrHex); | ||
| }); | ||
|
|
||
| it("preserves an optional source account", () => { | ||
| const source = "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"; | ||
| const op = Operation.changeTrust({ asset: usd, source }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,24 @@ describe("Operation.manageData()", () => { | |
| expect(obj.value).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("round-trips a null-value (delete) manageData through fromXDRObject and back", () => { | ||
| const op = Operation.manageData({ name: "test", value: null }); | ||
| const xdrHex = op.toXDR("hex"); | ||
| const operation = xdr.Operation.fromXDR(Buffer.from(xdrHex, "hex")); | ||
| const parsed = expectOperationType( | ||
| Operation.fromXDRObject(operation), | ||
| "manageData", | ||
| ); | ||
|
|
||
| // Rebuilding from the parsed result must not throw | ||
| const rebuilt = Operation.manageData({ | ||
| name: parsed.name, | ||
| value: parsed.value, | ||
| }); | ||
| expect(rebuilt).toBeInstanceOf(xdr.Operation); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto here we should check that they are the same operation |
||
| expect(rebuilt.toXDR("hex")).toBe(xdrHex); | ||
| }); | ||
|
|
||
| it("creates a manageData operation with source account", () => { | ||
| const source = "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"; | ||
| const opts = { name: "test", value: "data", source }; | ||
|
|
||
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.
changeTrustnow acceptslineas an alias forassetto support round-trippingfromXDRObjectresults, but the publicChangeTrustOptstype still requiresassetand does not includeline. Consider updating the exported typings (and optionally docs) to reflect that callers may passline(or change the record shape) so the round-trip pattern is supported without casts.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.
Same approach as the
manageDatacase —lineis only produced internally byfromXDRObject. The public API contract remainsasset. ChangingChangeTrustOptsto exposelinewould either makeassetoptional (breaking) or require a union type that adds complexity for an internal concern. The cast is intentional and the comment explains why.