@ledgerhq/coin-xrp 7.5.1 → 7.6.0-nightly.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/lib/api/index.d.ts.map +1 -1
  3. package/lib/api/index.integ.test.js +8 -0
  4. package/lib/api/index.integ.test.js.map +1 -1
  5. package/lib/api/index.js +5 -0
  6. package/lib/api/index.js.map +1 -1
  7. package/lib/api/index.test.js +10 -2
  8. package/lib/api/index.test.js.map +1 -1
  9. package/lib/logic/combine.d.ts.map +1 -1
  10. package/lib/logic/combine.js +16 -4
  11. package/lib/logic/combine.js.map +1 -1
  12. package/lib/logic/craftRawTransaction.d.ts +3 -0
  13. package/lib/logic/craftRawTransaction.d.ts.map +1 -0
  14. package/lib/logic/craftRawTransaction.js +70 -0
  15. package/lib/logic/craftRawTransaction.js.map +1 -0
  16. package/lib/logic/craftRawTransaction.test.d.ts +2 -0
  17. package/lib/logic/craftRawTransaction.test.d.ts.map +1 -0
  18. package/lib/logic/craftRawTransaction.test.js +304 -0
  19. package/lib/logic/craftRawTransaction.test.js.map +1 -0
  20. package/lib/logic/index.d.ts +1 -0
  21. package/lib/logic/index.d.ts.map +1 -1
  22. package/lib/logic/index.js +3 -1
  23. package/lib/logic/index.js.map +1 -1
  24. package/lib/logic/utils.d.ts +6 -0
  25. package/lib/logic/utils.d.ts.map +1 -1
  26. package/lib/logic/utils.js +18 -0
  27. package/lib/logic/utils.js.map +1 -1
  28. package/lib/logic/utils.test.js +62 -0
  29. package/lib/logic/utils.test.js.map +1 -1
  30. package/lib/logic/validateIntent.test.js +8 -0
  31. package/lib/logic/validateIntent.test.js.map +1 -1
  32. package/lib/types/model.d.ts +7 -0
  33. package/lib/types/model.d.ts.map +1 -1
  34. package/lib-es/api/index.d.ts.map +1 -1
  35. package/lib-es/api/index.integ.test.js +8 -0
  36. package/lib-es/api/index.integ.test.js.map +1 -1
  37. package/lib-es/api/index.js +6 -1
  38. package/lib-es/api/index.js.map +1 -1
  39. package/lib-es/api/index.test.js +10 -2
  40. package/lib-es/api/index.test.js.map +1 -1
  41. package/lib-es/logic/combine.d.ts.map +1 -1
  42. package/lib-es/logic/combine.js +16 -4
  43. package/lib-es/logic/combine.js.map +1 -1
  44. package/lib-es/logic/craftRawTransaction.d.ts +3 -0
  45. package/lib-es/logic/craftRawTransaction.d.ts.map +1 -0
  46. package/lib-es/logic/craftRawTransaction.js +67 -0
  47. package/lib-es/logic/craftRawTransaction.js.map +1 -0
  48. package/lib-es/logic/craftRawTransaction.test.d.ts +2 -0
  49. package/lib-es/logic/craftRawTransaction.test.d.ts.map +1 -0
  50. package/lib-es/logic/craftRawTransaction.test.js +302 -0
  51. package/lib-es/logic/craftRawTransaction.test.js.map +1 -0
  52. package/lib-es/logic/index.d.ts +1 -0
  53. package/lib-es/logic/index.d.ts.map +1 -1
  54. package/lib-es/logic/index.js +1 -0
  55. package/lib-es/logic/index.js.map +1 -1
  56. package/lib-es/logic/utils.d.ts +6 -0
  57. package/lib-es/logic/utils.d.ts.map +1 -1
  58. package/lib-es/logic/utils.js +18 -1
  59. package/lib-es/logic/utils.js.map +1 -1
  60. package/lib-es/logic/utils.test.js +63 -1
  61. package/lib-es/logic/utils.test.js.map +1 -1
  62. package/lib-es/logic/validateIntent.test.js +8 -0
  63. package/lib-es/logic/validateIntent.test.js.map +1 -1
  64. package/lib-es/types/model.d.ts +7 -0
  65. package/lib-es/types/model.d.ts.map +1 -1
  66. package/package.json +5 -5
  67. package/src/api/index.integ.test.ts +8 -0
  68. package/src/api/index.test.ts +21 -10
  69. package/src/api/index.ts +6 -0
  70. package/src/logic/combine.ts +26 -6
  71. package/src/logic/craftRawTransaction.test.ts +362 -0
  72. package/src/logic/craftRawTransaction.ts +91 -0
  73. package/src/logic/index.ts +1 -0
  74. package/src/logic/utils.test.ts +71 -1
  75. package/src/logic/utils.ts +20 -1
  76. package/src/logic/validateIntent.test.ts +8 -0
  77. package/src/types/model.ts +8 -0
@@ -0,0 +1,91 @@
1
+ import { CraftedTransaction } from "@ledgerhq/coin-framework/api/index";
2
+ import { decode, encode } from "ripple-binary-codec";
3
+ import { getLedgerIndex } from "../network";
4
+ import { estimateFees } from "./estimateFees";
5
+ import { JsonObject } from "ripple-binary-codec/dist/types/serialized-type";
6
+ import { SignerEntry } from "../types";
7
+ import { sortSignersByNumericAddress } from "./utils";
8
+
9
+ const LEDGER_OFFSET = 20;
10
+
11
+ function craftRawTransactionMultiSign(
12
+ xrplTransaction: JsonObject,
13
+ sender: string,
14
+ publicKey: string,
15
+ ): CraftedTransaction {
16
+ if (!xrplTransaction.Fee) {
17
+ throw new Error("Fee is required for multi sign transactions");
18
+ }
19
+ if (!xrplTransaction.Sequence) {
20
+ throw new Error("Sequence is required for multi sign transactions");
21
+ }
22
+
23
+ const signer: SignerEntry = {
24
+ Signer: {
25
+ Account: sender,
26
+ SigningPubKey: publicKey,
27
+ TxnSignature: "",
28
+ },
29
+ };
30
+
31
+ if (!xrplTransaction.Signers || !Array.isArray(xrplTransaction.Signers)) {
32
+ xrplTransaction.Signers = [signer];
33
+ } else {
34
+ xrplTransaction.Signers.push(signer);
35
+ // The Signers array must be sorted based on the numeric value of the signer addresses
36
+ // We could probably insert it at the right place directly but like this we make sure the rest is also sorted
37
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
38
+ xrplTransaction.Signers = sortSignersByNumericAddress(xrplTransaction.Signers as SignerEntry[]);
39
+ }
40
+
41
+ const serializedTransaction = encode(xrplTransaction);
42
+
43
+ return { transaction: serializedTransaction };
44
+ }
45
+
46
+ export async function craftRawTransaction(
47
+ transaction: string,
48
+ sender: string,
49
+ publicKey: string,
50
+ sequence: number,
51
+ ): Promise<CraftedTransaction> {
52
+ const xrplTransaction: JsonObject = decode(transaction);
53
+
54
+ // Multi sign transactions have an empty SigningPubKey
55
+ // We only check some of the fields and cannot autofill others
56
+ // The sender cannot be checked because the transaction can be signed by another account
57
+ // https://xrpl.org/docs/concepts/accounts/multi-signing#sending-multi-signed-transactions
58
+ // https://xrpl.org/docs/tutorials/how-tos/manage-account-settings/send-a-multi-signed-transaction
59
+ if (xrplTransaction.SigningPubKey === "") {
60
+ return craftRawTransactionMultiSign(xrplTransaction, sender, publicKey);
61
+ }
62
+
63
+ if (sender !== xrplTransaction.Account) {
64
+ throw new Error("Sender address does not match the transaction account");
65
+ }
66
+
67
+ if (!xrplTransaction.Fee) {
68
+ const { fees } = await estimateFees();
69
+ xrplTransaction.Fee = fees.toString();
70
+ }
71
+
72
+ // Enforce XRPL spec: If TicketSequence is provided, Sequence MUST be 0 regardless of any pre-set value.
73
+ // https://xrpl.org/docs/references/protocol/transactions/common-fields#transaction-common-fields
74
+ if (xrplTransaction.TicketSequence) {
75
+ xrplTransaction.Sequence = 0;
76
+ } else if (!xrplTransaction.Sequence) {
77
+ xrplTransaction.Sequence = sequence;
78
+ }
79
+
80
+ if (!xrplTransaction.LastLedgerSequence) {
81
+ xrplTransaction.LastLedgerSequence = (await getLedgerIndex()) + LEDGER_OFFSET;
82
+ }
83
+
84
+ if (!xrplTransaction.SigningPubKey) {
85
+ xrplTransaction.SigningPubKey = publicKey;
86
+ }
87
+
88
+ const serializedTransaction = encode(xrplTransaction);
89
+
90
+ return { transaction: serializedTransaction };
91
+ }
@@ -1,6 +1,7 @@
1
1
  export { broadcast } from "./broadcast";
2
2
  export { combine } from "./combine";
3
3
  export { craftTransaction } from "./craftTransaction";
4
+ export { craftRawTransaction } from "./craftRawTransaction";
4
5
  export type { MemoInput } from "./craftTransaction";
5
6
  export { estimateFees } from "./estimateFees";
6
7
  export { getBalance } from "./getBalance";
@@ -1,7 +1,20 @@
1
- import { cachedRecipientIsNew } from "./utils";
1
+ import { cachedRecipientIsNew, sortSignersByNumericAddress } from "./utils";
2
+ import { SignerEntry } from "../types";
3
+
4
+ // We'll mock ripple-address-codec so that we control numeric ordering via decodeAccountID
5
+ // without re-implementing the util's logic inside the tests.
6
+ const decodeAccountIDMock = jest.fn((account: string) => {
7
+ // Provide deterministic 20-byte arrays whose last byte encodes the numeric ordering.
8
+ // (Big-endian comparison means earlier bytes are 0; ordering driven by final byte value.)
9
+ const bytes = new Uint8Array(20).fill(0);
10
+ const ordering: Record<string, number> = { alpha: 1, gamma: 2, beta: 3, epsilon: 4, delta: 5 };
11
+ if (ordering[account]) bytes[19] = ordering[account];
12
+ return bytes;
13
+ });
2
14
 
3
15
  jest.mock("ripple-address-codec", () => ({
4
16
  isValidClassicAddress: () => true,
17
+ decodeAccountID: (address: string) => decodeAccountIDMock(address),
5
18
  }));
6
19
  const mockGetAccountInfo = jest.fn();
7
20
  jest.mock("../network", () => ({
@@ -58,3 +71,60 @@ describe("cachedRecipientIsNew", () => {
58
71
  expect(mockGetAccountInfo).toHaveBeenCalledTimes(1);
59
72
  });
60
73
  });
74
+
75
+ // Helper to build a SignerEntry from an Account address
76
+ function signer(account: string): SignerEntry {
77
+ return {
78
+ Signer: {
79
+ Account: account,
80
+ SigningPubKey: "PUBKEY",
81
+ TxnSignature: "SIGNATURE",
82
+ },
83
+ };
84
+ }
85
+
86
+ // We intentionally use simple string identifiers (not real XRP addresses) since we fully mock decodeAccountID.
87
+ // Numeric order (smallest->largest) encoded in mock via last byte value: alpha(1), gamma(2), beta(3), epsilon(4), delta(5)
88
+ const orderedByNumeric = ["alpha", "gamma", "beta", "epsilon", "delta"];
89
+
90
+ describe("sortSignersByNumericAddress", () => {
91
+ it("returns an empty array when given an empty array", () => {
92
+ expect(sortSignersByNumericAddress([])).toEqual([]);
93
+ });
94
+
95
+ it("does not mutate the original array", () => {
96
+ const original: SignerEntry[] = [signer("beta"), signer("alpha"), signer("delta")];
97
+ const originalSnapshot = original.map(s => s.Signer.Account);
98
+ const result = sortSignersByNumericAddress(original);
99
+ expect(result).not.toBe(original);
100
+ expect(original.map(s => s.Signer.Account)).toEqual(originalSnapshot);
101
+ });
102
+
103
+ it("returns new array for single element", () => {
104
+ const single = [signer("alpha")];
105
+ const result = sortSignersByNumericAddress(single);
106
+ expect(result).toHaveLength(1);
107
+ expect(result[0].Signer.Account).toBe("alpha");
108
+ expect(result).not.toBe(single);
109
+ });
110
+
111
+ it("sorts by numeric account ID as provided by decodeAccountID mock", () => {
112
+ const shuffled = ["delta", "gamma", "alpha", "epsilon", "beta"]; // random order
113
+ const signers = shuffled.map(a => signer(a));
114
+ const result = sortSignersByNumericAddress(signers).map(s => s.Signer.Account);
115
+ expect(result).toEqual(orderedByNumeric);
116
+ // Ensure decodeAccountID was called for each element at least once
117
+ orderedByNumeric.forEach(addr => {
118
+ expect(decodeAccountIDMock).toHaveBeenCalledWith(addr);
119
+ });
120
+ });
121
+
122
+ it("ordering differs from naive lexicographic string sort", () => {
123
+ const signers = ["alpha", "beta", "delta", "epsilon", "gamma"].map(a => signer(a));
124
+ const numericSorted = sortSignersByNumericAddress(signers).map(s => s.Signer.Account);
125
+ const lexSorted = signers.map(s => s.Signer.Account).sort();
126
+ // Our predetermined numeric order should not equal simple lexical sort (which puts beta before gamma)
127
+ expect(numericSorted).toEqual(orderedByNumeric);
128
+ expect(numericSorted).not.toEqual(lexSorted);
129
+ });
130
+ });
@@ -1,6 +1,7 @@
1
1
  import BigNumber from "bignumber.js";
2
- import { isValidClassicAddress } from "ripple-address-codec";
2
+ import { decodeAccountID, isValidClassicAddress } from "ripple-address-codec";
3
3
  import { getAccountInfo } from "../network";
4
+ import { SignerEntry } from "../types";
4
5
 
5
6
  export const UINT32_MAX = new BigNumber(2).pow(32).minus(1);
6
7
 
@@ -54,3 +55,21 @@ export const cachedRecipientIsNew = async (recipient: string): Promise<boolean>
54
55
 
55
56
  return isNew;
56
57
  };
58
+
59
+ /**
60
+ * Sorts the Signers array by the numeric value of each Signer.Account address.
61
+ * The numeric value is the big-endian integer of the 20-byte account ID.
62
+ */
63
+ export function sortSignersByNumericAddress(signers: SignerEntry[]): SignerEntry[] {
64
+ return signers.slice().sort((a, b) => {
65
+ const aBytes = decodeAccountID(a.Signer.Account);
66
+ const bBytes = decodeAccountID(b.Signer.Account);
67
+
68
+ const aInt = BigInt("0x" + Buffer.from(aBytes).toString("hex"));
69
+ const bInt = BigInt("0x" + Buffer.from(bBytes).toString("hex"));
70
+
71
+ if (aInt > bInt) return 1;
72
+ if (aInt < bInt) return -1;
73
+ return 0;
74
+ });
75
+ }
@@ -50,6 +50,7 @@ describe("validateIntent", () => {
50
50
  const result = await validateIntent(
51
51
  // account as any,
52
52
  {
53
+ intentType: "transaction",
53
54
  sender: SENDER,
54
55
  amount: 20_000_000n,
55
56
  recipient: RECIPIENT,
@@ -84,6 +85,7 @@ describe("validateIntent", () => {
84
85
  const result = await validateIntent(
85
86
  // account as any,
86
87
  {
88
+ intentType: "transaction",
87
89
  sender: SENDER,
88
90
  amount: 1_000_000n,
89
91
  recipient: RECIPIENT,
@@ -117,6 +119,7 @@ describe("validateIntent", () => {
117
119
  const result = await validateIntent(
118
120
  // account as any,
119
121
  {
122
+ intentType: "transaction",
120
123
  sender: SENDER,
121
124
  amount: 10_000_000n,
122
125
  recipient: RECIPIENT,
@@ -146,6 +149,7 @@ describe("validateIntent", () => {
146
149
  const result = await validateIntent(
147
150
  // account as any,
148
151
  {
152
+ intentType: "transaction",
149
153
  sender: SENDER,
150
154
  amount: 10_000_000n,
151
155
  recipient: SENDER,
@@ -176,6 +180,7 @@ describe("validateIntent", () => {
176
180
  const result = await validateIntent(
177
181
  // account as any,
178
182
  {
183
+ intentType: "transaction",
179
184
  sender: SENDER,
180
185
  amount: 5_000_000n,
181
186
  recipient: RECIPIENT_NEW,
@@ -206,6 +211,7 @@ describe("validateIntent", () => {
206
211
  const result = await validateIntent(
207
212
  // account as any,
208
213
  {
214
+ intentType: "transaction",
209
215
  sender: SENDER,
210
216
  amount: 0n,
211
217
  recipient: RECIPIENT,
@@ -236,6 +242,7 @@ describe("validateIntent", () => {
236
242
  const result = await validateIntent(
237
243
  // account as any,
238
244
  {
245
+ intentType: "transaction",
239
246
  sender: SENDER,
240
247
  asset: { unit: { code: "XRP", magnitude: 6 } },
241
248
  amount: 1_000_000n,
@@ -266,6 +273,7 @@ describe("validateIntent", () => {
266
273
  const result = await validateIntent(
267
274
  // account as any,
268
275
  {
276
+ intentType: "transaction",
269
277
  sender: SENDER,
270
278
  asset: { unit: { code: "XRP", magnitude: 6 } },
271
279
  amount: 1_000_000n,
@@ -31,3 +31,11 @@ export type ListOperationsOptions = {
31
31
  // filters:
32
32
  minHeight?: number;
33
33
  };
34
+
35
+ export type SignerEntry = {
36
+ Signer: {
37
+ Account: string;
38
+ SigningPubKey: string;
39
+ TxnSignature: string;
40
+ };
41
+ };