@ledgerhq/coin-module-framework 3.6.0 → 4.0.1

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 (54) hide show
  1. package/.oxlintrc.json +13 -2
  2. package/CHANGELOG.md +18 -0
  3. package/jest.config.js +0 -1
  4. package/lib/api/index.d.ts +1 -0
  5. package/lib/api/index.d.ts.map +1 -1
  6. package/lib/api/index.js +1 -0
  7. package/lib/api/index.js.map +1 -1
  8. package/lib/api/notSupported.d.ts +9 -0
  9. package/lib/api/notSupported.d.ts.map +1 -0
  10. package/lib/api/notSupported.js +17 -0
  11. package/lib/api/notSupported.js.map +1 -0
  12. package/lib/api/types.d.ts +22 -8
  13. package/lib/api/types.d.ts.map +1 -1
  14. package/lib/currencies/localeUtility.d.ts.map +1 -1
  15. package/lib/currencies/localeUtility.js +2 -2
  16. package/lib/currencies/localeUtility.js.map +1 -1
  17. package/lib/test-utils/helpers.d.ts +21 -0
  18. package/lib/test-utils/helpers.d.ts.map +1 -0
  19. package/lib/test-utils/helpers.js +36 -0
  20. package/lib/test-utils/helpers.js.map +1 -0
  21. package/lib/utils.d.ts +2 -1
  22. package/lib/utils.d.ts.map +1 -1
  23. package/lib/utils.js.map +1 -1
  24. package/lib-es/api/index.d.ts +1 -0
  25. package/lib-es/api/index.d.ts.map +1 -1
  26. package/lib-es/api/index.js +1 -0
  27. package/lib-es/api/index.js.map +1 -1
  28. package/lib-es/api/notSupported.d.ts +9 -0
  29. package/lib-es/api/notSupported.d.ts.map +1 -0
  30. package/lib-es/api/notSupported.js +13 -0
  31. package/lib-es/api/notSupported.js.map +1 -0
  32. package/lib-es/api/types.d.ts +22 -8
  33. package/lib-es/api/types.d.ts.map +1 -1
  34. package/lib-es/currencies/localeUtility.d.ts.map +1 -1
  35. package/lib-es/currencies/localeUtility.js +2 -2
  36. package/lib-es/currencies/localeUtility.js.map +1 -1
  37. package/lib-es/test-utils/helpers.d.ts +21 -0
  38. package/lib-es/test-utils/helpers.d.ts.map +1 -0
  39. package/lib-es/test-utils/helpers.js +31 -0
  40. package/lib-es/test-utils/helpers.js.map +1 -0
  41. package/lib-es/utils.d.ts +2 -1
  42. package/lib-es/utils.d.ts.map +1 -1
  43. package/lib-es/utils.js.map +1 -1
  44. package/package.json +9 -1
  45. package/src/api/getBalance/rejectBalanceOptions.test.ts +3 -4
  46. package/src/api/index.ts +1 -0
  47. package/src/api/notSupported.ts +13 -0
  48. package/src/api/types.ts +25 -10
  49. package/src/currencies/formatCurrencyUnit.test.ts +4 -2
  50. package/src/currencies/localeUtility.ts +5 -3
  51. package/src/logic/craftTransactionData.test.ts +2 -2
  52. package/src/test-utils/helpers.test.ts +383 -0
  53. package/src/test-utils/helpers.ts +73 -0
  54. package/src/utils.ts +9 -4
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/test-utils/helpers.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,sCAAsC;AAuBtC,MAAM,UAAU,mCAAmC,CACjD,OAAwB;IAExB,MAAM,IAAI,GAAgE;QACxE,UAAU,EAAE,aAAa;QACzB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,gBAAgB;QAC1C,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,mBAAmB;QACnD,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;QAC5B,KAAK,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE;KAC3C,CAAA;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;AACzC,CAAC;AAeD,MAAM,UAAU,sCAAsC,CACpD,OAA2B;IAE3B,MAAM,IAAI,GAAmE;QAC3E,UAAU,EAAE,SAAS;QACrB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,gBAAgB;QAC1C,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,mBAAmB;QACnD,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;QAC5B,KAAK,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE;QAC1C,UAAU,EAAE,OAAO,CAAC,UAAU,IAAI,mBAAmB;KACtD,CAAA;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;AACzC,CAAC;AAED,MAAM,UAAU,uBAAuB,CACrC,OAA4D;IAE5D,MAAM,KAAK,GAAc,EAAE,GAAG,OAAO,EAAE,CAAA;IACvC,OAAO,KAAK,CAAA;AACd,CAAC"}
package/lib-es/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import BigNumber from 'bignumber.js';
2
2
  import { SendTransactionIntent, StakingTransactionIntent, TransactionIntent } from './api';
3
- export declare function fromBigNumberToBigInt<T>(bigNumber: BigNumber | undefined, defaultValue?: T): bigint | T;
3
+ export declare function fromBigNumberToBigInt(bigNumber: BigNumber | undefined): bigint | undefined;
4
+ export declare function fromBigNumberToBigInt(bigNumber: BigNumber | undefined, defaultValue: bigint): bigint;
4
5
  export declare function isSendTransactionIntent(tx: TransactionIntent): tx is SendTransactionIntent;
5
6
  export declare function isStakingTransactionIntent(tx: TransactionIntent): tx is StakingTransactionIntent;
6
7
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,OAAO,SAAS,MAAM,cAAc,CAAA;AACpC,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAA;AAE1F,wBAAgB,qBAAqB,CAAC,CAAC,EACrC,SAAS,EAAE,SAAS,GAAG,SAAS,EAChC,YAAY,CAAC,EAAE,CAAC,GACf,MAAM,GAAG,CAAC,CAKZ;AAED,wBAAgB,uBAAuB,CAAC,EAAE,EAAE,iBAAiB,GAAG,EAAE,IAAI,qBAAqB,CAE1F;AAED,wBAAgB,0BAA0B,CAAC,EAAE,EAAE,iBAAiB,GAAG,EAAE,IAAI,wBAAwB,CAEhG"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,OAAO,SAAS,MAAM,cAAc,CAAA;AACpC,OAAO,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAA;AAE1F,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAAA;AAC3F,wBAAgB,qBAAqB,CACnC,SAAS,EAAE,SAAS,GAAG,SAAS,EAChC,YAAY,EAAE,MAAM,GACnB,MAAM,CAAA;AAWT,wBAAgB,uBAAuB,CAAC,EAAE,EAAE,iBAAiB,GAAG,EAAE,IAAI,qBAAqB,CAE1F;AAED,wBAAgB,0BAA0B,CAAC,EAAE,EAAE,iBAAiB,GAAG,EAAE,IAAI,wBAAwB,CAEhG"}
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,sCAAsC;AAEtC,OAAO,SAAS,MAAM,cAAc,CAAA;AAGpC,MAAM,UAAU,qBAAqB,CACnC,SAAgC,EAChC,YAAgB;IAEhB,IAAI,SAAS,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE,CAAC;QACrC,OAAO,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,CAAA;IACpC,CAAC;IACD,OAAO,YAAiB,CAAA;AAC1B,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,EAAqB;IAC3D,OAAO,EAAE,CAAC,UAAU,KAAK,aAAa,CAAA;AACxC,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,EAAqB;IAC9D,OAAO,EAAE,CAAC,UAAU,KAAK,SAAS,CAAA;AACpC,CAAC"}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,sCAAsC;AAEtC,OAAO,SAAS,MAAM,cAAc,CAAA;AAQpC,MAAM,UAAU,qBAAqB,CACnC,SAAgC,EAChC,YAAqB;IAErB,IAAI,SAAS,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE,CAAC;QACrC,OAAO,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,CAAA;IACpC,CAAC;IACD,OAAO,YAAY,CAAA;AACrB,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,EAAqB;IAC3D,OAAO,EAAE,CAAC,UAAU,KAAK,aAAa,CAAA;AACxC,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,EAAqB;IAC9D,OAAO,EAAE,CAAC,UAAU,KAAK,SAAS,CAAA;AACpC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ledgerhq/coin-module-framework",
3
- "version": "3.6.0",
3
+ "version": "4.0.1",
4
4
  "description": "Ledger framework for Coin integration",
5
5
  "keywords": [
6
6
  "Ledger",
@@ -29,6 +29,9 @@
29
29
  "currencies": [
30
30
  "lib/currencies/index"
31
31
  ],
32
+ "test-utils": [
33
+ "lib/test-utils/helpers"
34
+ ],
32
35
  "*": [
33
36
  "lib/*"
34
37
  ]
@@ -42,6 +45,11 @@
42
45
  "require": "./lib/currencies/index.js",
43
46
  "default": "./lib-es/currencies/index.js"
44
47
  },
48
+ "./test-utils": {
49
+ "@ledgerhq/source": "./src/test-utils/helpers.ts",
50
+ "require": "./lib/test-utils/helpers.js",
51
+ "default": "./lib-es/test-utils/helpers.js"
52
+ },
45
53
  "./*": {
46
54
  "@ledgerhq/source": "./src/*.ts",
47
55
  "require": "./lib/*.js",
@@ -2,15 +2,14 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { InvalidParameterError } from '@ledgerhq/errors'
5
- import { BalanceOptions } from '../types'
6
5
  import { rejectBalanceOptions } from './rejectBalanceOptions'
7
6
 
8
7
  describe('rejectBalanceOptions', () => {
9
8
  it('should throw an exception when options is provided', async () => {
10
9
  const getBalance = jest.fn().mockResolvedValueOnce([])
11
- await expect(rejectBalanceOptions(getBalance, {} as unknown as BalanceOptions)).rejects.toThrow(
12
- InvalidParameterError
13
- )
10
+ await expect(
11
+ rejectBalanceOptions(getBalance, { includeAssets: async () => true })
12
+ ).rejects.toThrow(InvalidParameterError)
14
13
  expect(getBalance).not.toHaveBeenCalled()
15
14
  })
16
15
 
package/src/api/index.ts CHANGED
@@ -7,4 +7,5 @@
7
7
  */
8
8
 
9
9
  export * from './errors'
10
+ export * from './notSupported'
10
11
  export * from './types'
@@ -0,0 +1,13 @@
1
+ // SPDX-FileCopyrightText: © 2026 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * Build a {@link CoinModuleApi} method stub that always throws a standardized
6
+ * "not supported" error. Use it for optional API methods a module does not implement.
7
+ *
8
+ * @param name the API method name, used in the error message
9
+ * @returns a function that throws `"<name> is not supported"` when invoked
10
+ */
11
+ export const notSupported = (name: string) => (): never => {
12
+ throw new Error(`${name} is not supported`)
13
+ }
package/src/api/types.ts CHANGED
@@ -178,15 +178,6 @@ export type OtherBlockOperation = {
178
178
  type: 'other'
179
179
  } & Record<string, unknown>
180
180
 
181
- // Other coins take different parameters What do we want to do ?
182
- export type Account = {
183
- currencyName: string
184
- address: string
185
- balance: bigint
186
- currencyUnit: Unit
187
- spendableBalance: bigint // NOTE: check if we can get rid of this one
188
- }
189
-
190
181
  /**
191
182
  * A component of an account/address balance, for a single asset.
192
183
  *
@@ -452,7 +443,7 @@ export type ListOperationsOptions = {
452
443
  * Implementation must guarantee the cursor is not volatile, i.e. it can be used long after the last request and still
453
444
  * provide consistent results - for instance, a date or transaction hash.
454
445
  */
455
- cursor?: Cursor
446
+ cursor?: Cursor | undefined
456
447
 
457
448
  /**
458
449
  * The maximum number of operations to fetch (note this is a soft limit, the implementation may return less or more
@@ -515,6 +506,12 @@ export type BalanceOptions = {
515
506
  includeAssets?: (asset: AssetInfo) => Promise<boolean>
516
507
  }
517
508
 
509
+ /** Parameters for {@link CoinModuleApi.call}. */
510
+ export type CallParams = Record<string, unknown> | unknown[]
511
+
512
+ /** Result of {@link CoinModuleApi.call}. */
513
+ export type CallResult = unknown
514
+
518
515
  export type CoinModuleApi<
519
516
  MemoType extends Memo = MemoNotSupported,
520
517
  TxDataType extends TxData = TxDataNotSupported,
@@ -562,6 +559,24 @@ export type CoinModuleApi<
562
559
  */
563
560
  getBlock: (height: number) => Promise<Block>
564
561
 
562
+ /**
563
+ * Perform a direct, read-only smart-contract call against the underlying node or indexer.
564
+ *
565
+ * This low-level escape hatch forwards opaque, network-defined parameters to the chain's single
566
+ * read-only contract-call primitive (for example, EVM `eth_call`). The module selects that
567
+ * primitive; callers cannot choose an RPC method. Callers are responsible for encoding parameters
568
+ * and decoding results. Parameters and results are transported as JSON, so callers must ensure
569
+ * they are JSON-serializable; the framework does not enforce or validate this.
570
+ *
571
+ * This API is optional and may not be supported on all networks: implementations should raise a
572
+ * "not supported" error in such cases.
573
+ *
574
+ * @param params opaque, network-defined parameters for the read-only call
575
+ * @returns the opaque, network-defined result
576
+ * @throws "not supported" if the network or implementation does not support contract calls
577
+ */
578
+ call: (params: CallParams) => Promise<CallResult>
579
+
565
580
  /**
566
581
  * Get the list of validators available on the network.
567
582
  * @param cursor a pagination cursor to resume listing (the implementation must guarantee the cursor is not volatile,
@@ -19,7 +19,8 @@ describe('formatCurrencyUnit', () => {
19
19
 
20
20
  test('handles non-BigNumber', () => {
21
21
  const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
22
- expect(formatCurrencyUnit(unit, null as unknown as BigNumber)).toBe('')
22
+ // @ts-expect-error intentionally passing null for negative-path test
23
+ expect(formatCurrencyUnit(unit, null)).toBe('')
23
24
  consoleSpy.mockRestore()
24
25
  })
25
26
 
@@ -173,7 +174,8 @@ describe('formatCurrencyUnit', () => {
173
174
 
174
175
  test('error handling', () => {
175
176
  const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
176
- const result = formatCurrencyUnitFragment(unit, null as unknown as BigNumber)
177
+ // @ts-expect-error intentionally passing null for negative-path test
178
+ const result = formatCurrencyUnitFragment(unit, null)
177
179
  expect(result.integerPart).toBe('')
178
180
  expect(result.decimalPart).toBe('')
179
181
  consoleSpy.mockRestore()
@@ -2,14 +2,16 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  // Forked from @ledgerhq/live-currency-format/src/localeUtility.ts
5
- const memoize = <T extends (arg: string) => ReturnType<T>>(fn: T): T => {
5
+ const memoize = <T extends (arg: string) => ReturnType<T>>(
6
+ fn: T
7
+ ): ((arg: string) => ReturnType<T>) => {
6
8
  const cache: Record<string, ReturnType<T>> = {}
7
- return ((arg: string) => {
9
+ return (arg: string) => {
8
10
  if (!(arg in cache)) {
9
11
  cache[arg] = fn(arg)
10
12
  }
11
13
  return cache[arg]
12
- }) as T
14
+ }
13
15
  }
14
16
 
15
17
  const options = {
@@ -1,11 +1,11 @@
1
1
  // SPDX-FileCopyrightText: © 2024 LEDGER SAS
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { TransactionIntent } from '../api'
5
4
  import { craftTransactionData } from './craftTransactionData'
6
5
 
7
6
  describe('craftTransactionData', () => {
8
7
  it('should return not supported tx data', () => {
9
- expect(craftTransactionData({} as unknown as TransactionIntent)).toEqual({ type: 'none' })
8
+ // @ts-expect-error intentionally passing empty object for negative-path test
9
+ expect(craftTransactionData({})).toEqual({ type: 'none' })
10
10
  })
11
11
  })
@@ -0,0 +1,383 @@
1
+ // SPDX-FileCopyrightText: © 2024 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { StakingOperation } from '../api/index'
5
+ import {
6
+ createAssetInfoMockData,
7
+ createSendTransactionIntentMockData,
8
+ createStakingTransactionIntentMockData,
9
+ } from './helpers'
10
+
11
+ describe('createSendTransactionIntentMockData', () => {
12
+ it.each([
13
+ [
14
+ 'minimal defaults',
15
+ { type: 'SendTransaction' },
16
+ {
17
+ intentType: 'transaction',
18
+ type: 'SendTransaction',
19
+ sender: 'sender-address',
20
+ recipient: 'recipient-address',
21
+ amount: 0n,
22
+ asset: { type: 'native' },
23
+ },
24
+ ],
25
+ [
26
+ 'sender override wins',
27
+ { type: 'SendTransaction', sender: 'alice' },
28
+ {
29
+ intentType: 'transaction',
30
+ type: 'SendTransaction',
31
+ sender: 'alice',
32
+ recipient: 'recipient-address',
33
+ amount: 0n,
34
+ asset: { type: 'native' },
35
+ },
36
+ ],
37
+ [
38
+ 'recipient override wins',
39
+ { type: 'SendTransaction', recipient: 'bob' },
40
+ {
41
+ intentType: 'transaction',
42
+ type: 'SendTransaction',
43
+ sender: 'sender-address',
44
+ recipient: 'bob',
45
+ amount: 0n,
46
+ asset: { type: 'native' },
47
+ },
48
+ ],
49
+ [
50
+ 'amount override wins',
51
+ { type: 'SendTransaction', amount: 1000000n },
52
+ {
53
+ intentType: 'transaction',
54
+ type: 'SendTransaction',
55
+ sender: 'sender-address',
56
+ recipient: 'recipient-address',
57
+ amount: 1000000n,
58
+ asset: { type: 'native' },
59
+ },
60
+ ],
61
+ [
62
+ 'amount 0n is preserved (nullish coalescing, not falsy)',
63
+ { type: 'SendTransaction', amount: 0n },
64
+ {
65
+ intentType: 'transaction',
66
+ type: 'SendTransaction',
67
+ sender: 'sender-address',
68
+ recipient: 'recipient-address',
69
+ amount: 0n,
70
+ asset: { type: 'native' },
71
+ },
72
+ ],
73
+ [
74
+ 'asset native with name override wins',
75
+ { type: 'SendTransaction', asset: { type: 'native', name: 'CoinA' } },
76
+ {
77
+ intentType: 'transaction',
78
+ type: 'SendTransaction',
79
+ sender: 'sender-address',
80
+ recipient: 'recipient-address',
81
+ amount: 0n,
82
+ asset: { type: 'native', name: 'CoinA' },
83
+ },
84
+ ],
85
+ [
86
+ 'asset token override wins',
87
+ { type: 'SendTransaction', asset: { type: 'token', assetReference: '0xcafe' } },
88
+ {
89
+ intentType: 'transaction',
90
+ type: 'SendTransaction',
91
+ sender: 'sender-address',
92
+ recipient: 'recipient-address',
93
+ amount: 0n,
94
+ asset: { type: 'token', assetReference: '0xcafe' },
95
+ },
96
+ ],
97
+ [
98
+ 'all fields overridden (no defaults used)',
99
+ {
100
+ type: 'SendToken',
101
+ sender: 'alice',
102
+ recipient: 'bob',
103
+ amount: 50000000n,
104
+ asset: { type: 'native' },
105
+ },
106
+ {
107
+ intentType: 'transaction',
108
+ type: 'SendToken',
109
+ sender: 'alice',
110
+ recipient: 'bob',
111
+ amount: 50000000n,
112
+ asset: { type: 'native' },
113
+ },
114
+ ],
115
+ ])('%s', (_label, partial, expected) => {
116
+ expect(createSendTransactionIntentMockData(partial)).toEqual(expected)
117
+ })
118
+
119
+ it('attaches memo when provided', () => {
120
+ expect(
121
+ createSendTransactionIntentMockData({
122
+ type: 'SendTransaction',
123
+ memo: { type: 'text', text: 'hello' },
124
+ })
125
+ ).toEqual({
126
+ intentType: 'transaction',
127
+ type: 'SendTransaction',
128
+ sender: 'sender-address',
129
+ recipient: 'recipient-address',
130
+ amount: 0n,
131
+ asset: { type: 'native' },
132
+ memo: { type: 'text', text: 'hello' },
133
+ })
134
+ })
135
+
136
+ it('attaches data when provided', () => {
137
+ expect(
138
+ createSendTransactionIntentMockData({
139
+ type: 'SendTransaction',
140
+ data: { type: 'calldata', hex: '0xcafe' },
141
+ })
142
+ ).toEqual({
143
+ intentType: 'transaction',
144
+ type: 'SendTransaction',
145
+ sender: 'sender-address',
146
+ recipient: 'recipient-address',
147
+ amount: 0n,
148
+ asset: { type: 'native' },
149
+ data: { type: 'calldata', hex: '0xcafe' },
150
+ })
151
+ })
152
+
153
+ it('attaches both memo and data when provided', () => {
154
+ expect(
155
+ createSendTransactionIntentMockData({
156
+ type: 'SendTransaction',
157
+ memo: { type: 'text', text: 'hello' },
158
+ data: { type: 'calldata', hex: '0xcafe' },
159
+ })
160
+ ).toEqual({
161
+ intentType: 'transaction',
162
+ type: 'SendTransaction',
163
+ sender: 'sender-address',
164
+ recipient: 'recipient-address',
165
+ amount: 0n,
166
+ asset: { type: 'native' },
167
+ memo: { type: 'text', text: 'hello' },
168
+ data: { type: 'calldata', hex: '0xcafe' },
169
+ })
170
+ })
171
+ })
172
+
173
+ describe('createStakingTransactionIntentMockData', () => {
174
+ it.each([
175
+ [
176
+ 'minimal defaults',
177
+ { type: 'StakeDelegate', mode: 'delegate' },
178
+ {
179
+ intentType: 'staking',
180
+ type: 'StakeDelegate',
181
+ mode: 'delegate',
182
+ sender: 'sender-address',
183
+ recipient: 'recipient-address',
184
+ amount: 0n,
185
+ asset: { type: 'native' },
186
+ valAddress: 'validator-address',
187
+ },
188
+ ],
189
+ [
190
+ 'sender override wins',
191
+ { type: 'StakeDelegate', mode: 'delegate', sender: 'alice' },
192
+ {
193
+ intentType: 'staking',
194
+ type: 'StakeDelegate',
195
+ mode: 'delegate',
196
+ sender: 'alice',
197
+ recipient: 'recipient-address',
198
+ amount: 0n,
199
+ asset: { type: 'native' },
200
+ valAddress: 'validator-address',
201
+ },
202
+ ],
203
+ [
204
+ 'recipient override wins',
205
+ { type: 'StakeDelegate', mode: 'delegate', recipient: 'bob' },
206
+ {
207
+ intentType: 'staking',
208
+ type: 'StakeDelegate',
209
+ mode: 'delegate',
210
+ sender: 'sender-address',
211
+ recipient: 'bob',
212
+ amount: 0n,
213
+ asset: { type: 'native' },
214
+ valAddress: 'validator-address',
215
+ },
216
+ ],
217
+ [
218
+ 'amount override wins',
219
+ { type: 'StakeDelegate', mode: 'delegate', amount: 5000000n },
220
+ {
221
+ intentType: 'staking',
222
+ type: 'StakeDelegate',
223
+ mode: 'delegate',
224
+ sender: 'sender-address',
225
+ recipient: 'recipient-address',
226
+ amount: 5000000n,
227
+ asset: { type: 'native' },
228
+ valAddress: 'validator-address',
229
+ },
230
+ ],
231
+ [
232
+ 'valAddress override wins',
233
+ { type: 'StakeDelegate', mode: 'delegate', valAddress: 'custom-validator' },
234
+ {
235
+ intentType: 'staking',
236
+ type: 'StakeDelegate',
237
+ mode: 'delegate',
238
+ sender: 'sender-address',
239
+ recipient: 'recipient-address',
240
+ amount: 0n,
241
+ asset: { type: 'native' },
242
+ valAddress: 'custom-validator',
243
+ },
244
+ ],
245
+ [
246
+ 'dstValAddress passes through',
247
+ { type: 'StakeRedelegate', mode: 'redelegate', dstValAddress: 'dst-validator' },
248
+ {
249
+ intentType: 'staking',
250
+ type: 'StakeRedelegate',
251
+ mode: 'redelegate',
252
+ sender: 'sender-address',
253
+ recipient: 'recipient-address',
254
+ amount: 0n,
255
+ asset: { type: 'native' },
256
+ valAddress: 'validator-address',
257
+ dstValAddress: 'dst-validator',
258
+ },
259
+ ],
260
+ [
261
+ 'asset override wins',
262
+ { type: 'StakeDelegate', mode: 'delegate', asset: { type: 'native', name: 'CoinA' } },
263
+ {
264
+ intentType: 'staking',
265
+ type: 'StakeDelegate',
266
+ mode: 'delegate',
267
+ sender: 'sender-address',
268
+ recipient: 'recipient-address',
269
+ amount: 0n,
270
+ asset: { type: 'native', name: 'CoinA' },
271
+ valAddress: 'validator-address',
272
+ },
273
+ ],
274
+ ] as const)('%s', (_label, partial, expected) => {
275
+ expect(createStakingTransactionIntentMockData(partial)).toEqual(expected)
276
+ })
277
+
278
+ it.each<StakingOperation>([
279
+ 'delegate',
280
+ 'undelegate',
281
+ 'redelegate',
282
+ 'claimReward',
283
+ 'compoundReward',
284
+ 'withdraw',
285
+ ])('mode %s is preserved', (mode) => {
286
+ expect(createStakingTransactionIntentMockData({ type: 'StakeOperation', mode })).toEqual({
287
+ intentType: 'staking',
288
+ type: 'StakeOperation',
289
+ mode,
290
+ sender: 'sender-address',
291
+ recipient: 'recipient-address',
292
+ amount: 0n,
293
+ asset: { type: 'native' },
294
+ valAddress: 'validator-address',
295
+ })
296
+ })
297
+
298
+ it('attaches memo when provided', () => {
299
+ expect(
300
+ createStakingTransactionIntentMockData({
301
+ type: 'StakeDelegate',
302
+ mode: 'delegate',
303
+ memo: { type: 'text', text: 'stake' },
304
+ })
305
+ ).toEqual({
306
+ intentType: 'staking',
307
+ type: 'StakeDelegate',
308
+ mode: 'delegate',
309
+ sender: 'sender-address',
310
+ recipient: 'recipient-address',
311
+ amount: 0n,
312
+ asset: { type: 'native' },
313
+ valAddress: 'validator-address',
314
+ memo: { type: 'text', text: 'stake' },
315
+ })
316
+ })
317
+
318
+ it('attaches data when provided', () => {
319
+ expect(
320
+ createStakingTransactionIntentMockData({
321
+ type: 'StakeDelegate',
322
+ mode: 'delegate',
323
+ data: { type: 'msg-data', encoded: '0xcafe' },
324
+ })
325
+ ).toEqual({
326
+ intentType: 'staking',
327
+ type: 'StakeDelegate',
328
+ mode: 'delegate',
329
+ sender: 'sender-address',
330
+ recipient: 'recipient-address',
331
+ amount: 0n,
332
+ asset: { type: 'native' },
333
+ valAddress: 'validator-address',
334
+ data: { type: 'msg-data', encoded: '0xcafe' },
335
+ })
336
+ })
337
+
338
+ it('attaches both memo and data when provided', () => {
339
+ expect(
340
+ createStakingTransactionIntentMockData({
341
+ type: 'StakeDelegate',
342
+ mode: 'delegate',
343
+ memo: { type: 'text', text: 'stake' },
344
+ data: { type: 'msg-data', encoded: '0xcafe' },
345
+ })
346
+ ).toEqual({
347
+ intentType: 'staking',
348
+ type: 'StakeDelegate',
349
+ mode: 'delegate',
350
+ sender: 'sender-address',
351
+ recipient: 'recipient-address',
352
+ amount: 0n,
353
+ asset: { type: 'native' },
354
+ valAddress: 'validator-address',
355
+ memo: { type: 'text', text: 'stake' },
356
+ data: { type: 'msg-data', encoded: '0xcafe' },
357
+ })
358
+ })
359
+ })
360
+
361
+ describe('createAssetInfoMockData', () => {
362
+ it.each([
363
+ ['native minimal', { type: 'native' }, { type: 'native' }],
364
+ ['native with name', { type: 'native', name: 'CoinA' }, { type: 'native', name: 'CoinA' }],
365
+ [
366
+ 'fungible token',
367
+ { type: 'token', assetReference: '0xcafe' },
368
+ { type: 'token', assetReference: '0xcafe' },
369
+ ],
370
+ [
371
+ 'nft with owner',
372
+ { type: 'nft', assetReference: '0xcafe', assetOwner: '0xcafe' },
373
+ { type: 'nft', assetReference: '0xcafe', assetOwner: '0xcafe' },
374
+ ],
375
+ [
376
+ 'token with name',
377
+ { type: 'fungible-token', name: 'TokenA' },
378
+ { type: 'fungible-token', name: 'TokenA' },
379
+ ],
380
+ ])('%s', (_label, partial, expected) => {
381
+ expect(createAssetInfoMockData(partial)).toEqual(expected)
382
+ })
383
+ })
@@ -0,0 +1,73 @@
1
+ // SPDX-FileCopyrightText: © 2024 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type {
5
+ AssetInfo,
6
+ Memo,
7
+ MemoNotSupported,
8
+ SendTransactionIntent,
9
+ StakingOperation,
10
+ StakingTransactionIntent,
11
+ TxData,
12
+ TxDataNotSupported,
13
+ } from '../api/index'
14
+
15
+ type SendPartialBase = { type: string } & Partial<
16
+ Omit<SendTransactionIntent<MemoNotSupported, TxDataNotSupported>, 'intentType' | 'type'>
17
+ >
18
+
19
+ export function createSendTransactionIntentMockData<
20
+ MemoType extends Memo = MemoNotSupported,
21
+ TxDataType extends TxData = TxDataNotSupported,
22
+ >(
23
+ partial: SendPartialBase & { memo?: MemoType; data?: TxDataType }
24
+ ): SendTransactionIntent<MemoType, TxDataType>
25
+ export function createSendTransactionIntentMockData(
26
+ partial: SendPartialBase
27
+ ): SendTransactionIntent<MemoNotSupported, TxDataNotSupported> {
28
+ const base: SendTransactionIntent<MemoNotSupported, TxDataNotSupported> = {
29
+ intentType: 'transaction',
30
+ type: partial.type,
31
+ sender: partial.sender ?? 'sender-address',
32
+ recipient: partial.recipient ?? 'recipient-address',
33
+ amount: partial.amount ?? 0n,
34
+ asset: partial.asset ?? { type: 'native' },
35
+ }
36
+ return Object.assign({}, base, partial)
37
+ }
38
+
39
+ type StakingPartialBase = { type: string; mode: StakingOperation } & Partial<
40
+ Omit<
41
+ StakingTransactionIntent<MemoNotSupported, TxDataNotSupported>,
42
+ 'intentType' | 'type' | 'mode'
43
+ >
44
+ >
45
+
46
+ export function createStakingTransactionIntentMockData<
47
+ MemoType extends Memo = MemoNotSupported,
48
+ TxDataType extends TxData = TxDataNotSupported,
49
+ >(
50
+ partial: StakingPartialBase & { memo?: MemoType; data?: TxDataType }
51
+ ): StakingTransactionIntent<MemoType, TxDataType>
52
+ export function createStakingTransactionIntentMockData(
53
+ partial: StakingPartialBase
54
+ ): StakingTransactionIntent<MemoNotSupported, TxDataNotSupported> {
55
+ const base: StakingTransactionIntent<MemoNotSupported, TxDataNotSupported> = {
56
+ intentType: 'staking',
57
+ type: partial.type,
58
+ mode: partial.mode,
59
+ sender: partial.sender ?? 'sender-address',
60
+ recipient: partial.recipient ?? 'recipient-address',
61
+ amount: partial.amount ?? 0n,
62
+ asset: partial.asset ?? { type: 'native' },
63
+ valAddress: partial.valAddress ?? 'validator-address',
64
+ }
65
+ return Object.assign({}, base, partial)
66
+ }
67
+
68
+ export function createAssetInfoMockData(
69
+ partial: { type: string } & Partial<Omit<AssetInfo, 'type'>>
70
+ ): AssetInfo {
71
+ const asset: AssetInfo = { ...partial }
72
+ return asset
73
+ }