@ledgerhq/coin-canton 0.5.0-nightly.3 → 0.5.0-nightly.4

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 (104) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +12 -0
  3. package/lib/api/getBalance.integ.test.js +7 -3
  4. package/lib/api/getBalance.integ.test.js.map +1 -1
  5. package/lib/bridge/index.d.ts.map +1 -1
  6. package/lib/bridge/index.js +3 -0
  7. package/lib/bridge/index.js.map +1 -1
  8. package/lib/bridge/onboard.d.ts +4 -3
  9. package/lib/bridge/onboard.d.ts.map +1 -1
  10. package/lib/bridge/onboard.integ.test.js +2 -2
  11. package/lib/bridge/onboard.integ.test.js.map +1 -1
  12. package/lib/bridge/onboard.js +95 -35
  13. package/lib/bridge/onboard.js.map +1 -1
  14. package/lib/bridge/serialization.d.ts +4 -0
  15. package/lib/bridge/serialization.d.ts.map +1 -0
  16. package/lib/bridge/serialization.js +31 -0
  17. package/lib/bridge/serialization.js.map +1 -0
  18. package/lib/bridge/sync.d.ts +2 -1
  19. package/lib/bridge/sync.d.ts.map +1 -1
  20. package/lib/bridge/sync.integ.test.js +2 -2
  21. package/lib/bridge/sync.integ.test.js.map +1 -1
  22. package/lib/bridge/sync.js +20 -12
  23. package/lib/bridge/sync.js.map +1 -1
  24. package/lib/common-logic/transaction/estimateFees.d.ts.map +1 -1
  25. package/lib/common-logic/transaction/estimateFees.js +6 -1
  26. package/lib/common-logic/transaction/estimateFees.js.map +1 -1
  27. package/lib/config.d.ts +1 -0
  28. package/lib/config.d.ts.map +1 -1
  29. package/lib/config.js.map +1 -1
  30. package/lib/network/gateway.d.ts +5 -13
  31. package/lib/network/gateway.d.ts.map +1 -1
  32. package/lib/network/gateway.integ.test.js +2 -1
  33. package/lib/network/gateway.integ.test.js.map +1 -1
  34. package/lib/network/gateway.js +11 -4
  35. package/lib/network/gateway.js.map +1 -1
  36. package/lib/signer/getAddress.d.ts.map +1 -1
  37. package/lib/signer/getAddress.js +2 -2
  38. package/lib/signer/getAddress.js.map +1 -1
  39. package/lib/types/bridge.d.ts +13 -1
  40. package/lib/types/bridge.d.ts.map +1 -1
  41. package/lib/types/onboard.d.ts +2 -0
  42. package/lib/types/onboard.d.ts.map +1 -1
  43. package/lib/types/onboard.js.map +1 -1
  44. package/lib/types/signer.d.ts +2 -1
  45. package/lib/types/signer.d.ts.map +1 -1
  46. package/lib-es/api/getBalance.integ.test.js +7 -3
  47. package/lib-es/api/getBalance.integ.test.js.map +1 -1
  48. package/lib-es/bridge/index.d.ts.map +1 -1
  49. package/lib-es/bridge/index.js +3 -0
  50. package/lib-es/bridge/index.js.map +1 -1
  51. package/lib-es/bridge/onboard.d.ts +4 -3
  52. package/lib-es/bridge/onboard.d.ts.map +1 -1
  53. package/lib-es/bridge/onboard.integ.test.js +2 -2
  54. package/lib-es/bridge/onboard.integ.test.js.map +1 -1
  55. package/lib-es/bridge/onboard.js +92 -35
  56. package/lib-es/bridge/onboard.js.map +1 -1
  57. package/lib-es/bridge/serialization.d.ts +4 -0
  58. package/lib-es/bridge/serialization.d.ts.map +1 -0
  59. package/lib-es/bridge/serialization.js +27 -0
  60. package/lib-es/bridge/serialization.js.map +1 -0
  61. package/lib-es/bridge/sync.d.ts +2 -1
  62. package/lib-es/bridge/sync.d.ts.map +1 -1
  63. package/lib-es/bridge/sync.integ.test.js +2 -2
  64. package/lib-es/bridge/sync.integ.test.js.map +1 -1
  65. package/lib-es/bridge/sync.js +21 -13
  66. package/lib-es/bridge/sync.js.map +1 -1
  67. package/lib-es/common-logic/transaction/estimateFees.d.ts.map +1 -1
  68. package/lib-es/common-logic/transaction/estimateFees.js +3 -1
  69. package/lib-es/common-logic/transaction/estimateFees.js.map +1 -1
  70. package/lib-es/config.d.ts +1 -0
  71. package/lib-es/config.d.ts.map +1 -1
  72. package/lib-es/config.js.map +1 -1
  73. package/lib-es/network/gateway.d.ts +5 -13
  74. package/lib-es/network/gateway.d.ts.map +1 -1
  75. package/lib-es/network/gateway.integ.test.js +2 -1
  76. package/lib-es/network/gateway.integ.test.js.map +1 -1
  77. package/lib-es/network/gateway.js +11 -4
  78. package/lib-es/network/gateway.js.map +1 -1
  79. package/lib-es/signer/getAddress.d.ts.map +1 -1
  80. package/lib-es/signer/getAddress.js +2 -2
  81. package/lib-es/signer/getAddress.js.map +1 -1
  82. package/lib-es/types/bridge.d.ts +13 -1
  83. package/lib-es/types/bridge.d.ts.map +1 -1
  84. package/lib-es/types/onboard.d.ts +2 -0
  85. package/lib-es/types/onboard.d.ts.map +1 -1
  86. package/lib-es/types/onboard.js.map +1 -1
  87. package/lib-es/types/signer.d.ts +2 -1
  88. package/lib-es/types/signer.d.ts.map +1 -1
  89. package/package.json +3 -3
  90. package/src/api/getBalance.integ.test.ts +7 -3
  91. package/src/bridge/index.ts +3 -0
  92. package/src/bridge/onboard.integ.test.ts +2 -2
  93. package/src/bridge/onboard.ts +120 -47
  94. package/src/bridge/serialization.ts +36 -0
  95. package/src/bridge/sync.integ.test.ts +2 -2
  96. package/src/bridge/sync.ts +27 -19
  97. package/src/common-logic/transaction/estimateFees.ts +5 -1
  98. package/src/config.ts +1 -0
  99. package/src/network/gateway.integ.test.ts +2 -1
  100. package/src/network/gateway.ts +17 -23
  101. package/src/signer/getAddress.ts +6 -4
  102. package/src/types/bridge.ts +16 -0
  103. package/src/types/onboard.ts +3 -0
  104. package/src/types/signer.ts +2 -1
@@ -1,14 +1,14 @@
1
1
  import BigNumber from "bignumber.js";
2
2
  import { Operation } from "@ledgerhq/types-live";
3
- import { encodeAccountId } from "@ledgerhq/coin-framework/account/index";
3
+ import { decodeAccountId, encodeAccountId } from "@ledgerhq/coin-framework/account/index";
4
4
  import { GetAccountShape, mergeOps } from "@ledgerhq/coin-framework/bridge/jsHelpers";
5
- import { getBalance, getLedgerEnd, getOperations, type OperationInfo } from "../network/gateway";
6
-
7
5
  import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
6
+ import { getBalance, getLedgerEnd, getOperations, type OperationInfo } from "../network/gateway";
7
+ import { CantonAccount } from "../types";
8
8
  import coinConfig from "../config";
9
9
 
10
10
  const txInfoToOperationAdapter =
11
- (accountId: string, address: string) =>
11
+ (accountId: string, partyId: string) =>
12
12
  (txInfo: OperationInfo): Operation => {
13
13
  const {
14
14
  transaction_hash,
@@ -20,7 +20,7 @@ const txInfoToOperationAdapter =
20
20
  fee: { value: fee },
21
21
  transfers: [{ value: transferValue }],
22
22
  } = txInfo;
23
- const type = senders.includes(address) ? "OUT" : "IN";
23
+ const type = senders.includes(partyId) ? "OUT" : "IN";
24
24
  const value = new BigNumber(transferValue);
25
25
  const feeValue = new BigNumber(fee);
26
26
 
@@ -48,24 +48,31 @@ const txInfoToOperationAdapter =
48
48
  const filterOperations = (
49
49
  transactions: OperationInfo[],
50
50
  accountId: string,
51
- address: string,
51
+ partyId: string,
52
52
  ): Operation[] => {
53
53
  return transactions
54
54
  .filter(tx => tx.type === "Receive" || tx.type === "Send")
55
- .map(txInfoToOperationAdapter(accountId, address));
55
+ .map(txInfoToOperationAdapter(accountId, partyId));
56
56
  };
57
57
 
58
- export const getAccountShape: GetAccountShape = async info => {
59
- const { address, initialAccount, currency, derivationMode } = info;
60
- // TODO: we need better solution ?
61
- const xpubOrAddress = address?.replace(/:/g, "_") || "";
58
+ export const getAccountShape: GetAccountShape<CantonAccount> = async info => {
59
+ const { address, initialAccount, currency, derivationMode, derivationPath, rest } = info;
60
+
61
+ const xpubOrAddress = (
62
+ (initialAccount && initialAccount.id && decodeAccountId(initialAccount.id).xpubOrAddress) ||
63
+ ""
64
+ ).replace(/:/g, "_");
65
+ const partyId =
66
+ rest?.cantonResources?.partyId ||
67
+ initialAccount?.cantonResources?.partyId ||
68
+ xpubOrAddress.replace(/_/g, ":");
62
69
 
63
70
  const accountId = encodeAccountId({
64
71
  type: "js",
65
72
  version: "2",
66
73
  currencyId: currency.id,
67
74
  xpubOrAddress,
68
- derivationMode: "",
75
+ derivationMode,
69
76
  });
70
77
 
71
78
  // blockheight retrieval
@@ -73,7 +80,7 @@ export const getAccountShape: GetAccountShape = async info => {
73
80
 
74
81
  // Account info retrieval + spendable balance calculation
75
82
  // const accountInfo = await getAccountInfo(address);
76
- const balances = await getBalance(address);
83
+ const balances = await getBalance(partyId);
77
84
 
78
85
  // TODO change to balance.instrument_id === "Amulet" after update on backend
79
86
  const balanceData = balances.find(balance => balance.instrument_id.includes("Amulet")) || {
@@ -93,16 +100,12 @@ export const getAccountShape: GetAccountShape = async info => {
93
100
  // Tx history fetching
94
101
  const oldOperations = initialAccount?.operations || [];
95
102
  const startAt = oldOperations.length ? (oldOperations[0].blockHeight || 0) + 1 : 0;
96
- const transactionData = await getOperations(address, {
103
+ const transactionData = await getOperations(partyId, {
97
104
  cursor: startAt,
98
105
  limit: 100,
99
106
  });
100
107
 
101
- const newOperations = filterOperations(
102
- transactionData.operations,
103
- accountId,
104
- address.replace(/_/g, ":"),
105
- );
108
+ const newOperations = filterOperations(transactionData.operations, accountId, partyId);
106
109
  const operations = mergeOps(oldOperations, newOperations);
107
110
 
108
111
  // We return the new account shape
@@ -114,6 +117,11 @@ export const getAccountShape: GetAccountShape = async info => {
114
117
  spendableBalance,
115
118
  operations,
116
119
  operationsCount: operations.length,
120
+ freshAddress: address,
121
+ freshAddressPath: derivationPath,
122
+ cantonResources: {
123
+ partyId,
124
+ },
117
125
  };
118
126
 
119
127
  return shape;
@@ -1,4 +1,8 @@
1
+ import coinConfig from "../../config";
2
+
3
+ const feeValue = () => coinConfig.getCoinConfig().fee ?? 10_000;
4
+
1
5
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
6
  export async function estimateFees(serializedTransaction: string): Promise<bigint> {
3
- return Promise.resolve(BigInt(10_000)); // TODO replace with real implementation
7
+ return Promise.resolve(BigInt(feeValue())); // TODO replace with real implementation
4
8
  }
package/src/config.ts CHANGED
@@ -10,6 +10,7 @@ export type CantonConfig = {
10
10
  networkType: "mainnet" | "devnet" | "localnet";
11
11
  useGateway?: boolean;
12
12
  nativeInstrumentId?: string;
13
+ fee?: number;
13
14
  };
14
15
 
15
16
  export type CantonCoinConfig = CurrencyConfig & CantonConfig;
@@ -58,7 +58,8 @@ describe("gateway (devnet)", () => {
58
58
  expect(response).toHaveProperty("public_key_fingerprint");
59
59
  expect(response).toHaveProperty("transactions");
60
60
  expect(response.transactions).toHaveProperty("combined_hash");
61
- expect(response.party_name).toBe(keyPair.publicKeyHex);
61
+ expect(response.party_name).toBeDefined();
62
+ expect(typeof response.party_name).toBe("string");
62
63
 
63
64
  expect(response.public_key_fingerprint).toBe(keyPair.fingerprint);
64
65
  }, 30000);
@@ -54,16 +54,6 @@ export type PrepareTransferRequest = {
54
54
  reason?: string;
55
55
  };
56
56
 
57
- export type PrepareTapRequest = {
58
- type: "tap-request";
59
- amount: number;
60
- };
61
-
62
- export type PreparePreapprovalRequest = {
63
- type: "transfer-pre-approval-proposal";
64
- receiver: string;
65
- };
66
-
67
57
  type OnboardingSubmitRequest = {
68
58
  prepare_request: OnboardingPrepareRequest;
69
59
  prepare_response: OnboardingPrepareResponse;
@@ -274,6 +264,7 @@ export type OperationInfo =
274
264
 
275
265
  const getGatewayUrl = () => coinConfig.getCoinConfig().gatewayUrl;
276
266
  const getNodeId = () => coinConfig.getCoinConfig().nodeId || "ledger-devnet-stg";
267
+ const getNetworkType = () => coinConfig.getCoinConfig().networkType;
277
268
 
278
269
  const gatewayNetwork = <T, U = unknown>(req: LiveNetworkRequest<U>) => {
279
270
  const API_KEY = getEnv("CANTON_API_KEY");
@@ -338,8 +329,7 @@ export async function submit(partyId: string, serialized: string, signature: str
338
329
  export async function getBalance(partyId: string): Promise<InstrumentBalance[]> {
339
330
  const { data } = await gatewayNetwork<InstrumentBalance[]>({
340
331
  method: "GET",
341
- // TODO: we need better solution ?
342
- url: `${getGatewayUrl()}/v1/node/${getNodeId()}/party/${partyId.replace(/_/g, ":")}/balance`,
332
+ url: `${getGatewayUrl()}/v1/node/${getNodeId()}/party/${partyId}/balance`,
343
333
  });
344
334
  return data;
345
335
  }
@@ -377,21 +367,21 @@ export async function getOperations(
377
367
  operations: OperationInfo[];
378
368
  }>({
379
369
  method: "GET",
380
- url: `${getGatewayUrl()}/v1/node/${getNodeId()}/party/${partyId.replace(/_/g, ":")}/operations`,
370
+ url: `${getGatewayUrl()}/v1/node/${getNodeId()}/party/${partyId}/operations`,
381
371
  params: options,
382
372
  });
383
373
  return data;
384
374
  }
385
375
 
386
- type PrepareTapRequestRequest = {
376
+ type PrepareTapRequest = {
387
377
  partyId: string;
388
378
  amount?: number;
389
379
  };
390
380
 
391
- type PrepareTapRequestResponse = {
392
- serialized: "string";
381
+ type PrepareTapResponse = {
382
+ serialized: string;
393
383
  json: null;
394
- hash: "string";
384
+ hash: string;
395
385
  };
396
386
 
397
387
  enum TransactionType {
@@ -402,11 +392,15 @@ enum TransactionType {
402
392
  export async function prepareTapRequest({
403
393
  partyId,
404
394
  amount = 1000000,
405
- }: PrepareTapRequestRequest): Promise<PrepareTapRequestResponse> {
406
- const { data } = await gatewayNetwork<
407
- PrepareTapRequestResponse,
408
- { amount: number; type: string }
409
- >({
395
+ }: PrepareTapRequest): Promise<PrepareTapResponse> {
396
+ if (getNetworkType() === "mainnet") {
397
+ return {
398
+ serialized: "",
399
+ json: null,
400
+ hash: "",
401
+ };
402
+ }
403
+ const { data } = await gatewayNetwork<PrepareTapResponse, { amount: number; type: string }>({
410
404
  method: "POST",
411
405
  url: `${getGatewayUrl()}/v1/node/${getNodeId()}/party/${partyId}/transaction/prepare`,
412
406
  data: {
@@ -451,7 +445,7 @@ export async function prepareTransferRequest(
451
445
  partyId: string,
452
446
  params: PrepareTransferRequest,
453
447
  ): Promise<PrepareTransferResponse> {
454
- const { data } = await network<PrepareTransferResponse>({
448
+ const { data } = await gatewayNetwork<PrepareTransferResponse, PrepareTransferRequest>({
455
449
  method: "POST",
456
450
  url: `${getGatewayUrl()}/v1/node/${getNodeId()}/party/${partyId}/transaction/prepare`,
457
451
  data: params,
@@ -1,17 +1,19 @@
1
1
  import { GetAddressOptions } from "@ledgerhq/coin-framework/derivation";
2
2
  import { GetAddressFn } from "@ledgerhq/coin-framework/bridge/getAddressWrapper";
3
3
  import { SignerContext } from "@ledgerhq/coin-framework/signer";
4
- import { CantonSigner } from "../types";
4
+ import { CantonSigner, CantonAddress } from "../types";
5
5
 
6
6
  const getAddress = (signerContext: SignerContext<CantonSigner>): GetAddressFn => {
7
- return async (deviceId: string, { path }: GetAddressOptions) => {
8
- const { address, publicKey } = await signerContext(deviceId, signer => signer.getAddress(path));
7
+ return async (deviceId: string, { path, verify }: GetAddressOptions) => {
8
+ const { address, publicKey } = await signerContext(deviceId, signer =>
9
+ signer.getAddress(path, verify),
10
+ );
9
11
 
10
12
  return {
11
13
  path,
12
14
  address,
13
15
  publicKey,
14
- };
16
+ } satisfies CantonAddress;
15
17
  };
16
18
  };
17
19
 
@@ -1,6 +1,8 @@
1
1
  import type { BigNumber } from "bignumber.js";
2
2
  import type { Observable } from "rxjs";
3
3
  import type {
4
+ Account,
5
+ AccountRaw,
4
6
  CurrencyBridge,
5
7
  TransactionCommon,
6
8
  TransactionCommonRaw,
@@ -50,3 +52,17 @@ export type TransactionRaw = TransactionCommonRaw & {
50
52
 
51
53
  export type TransactionStatus = TransactionStatusCommon;
52
54
  export type TransactionStatusRaw = TransactionStatusCommonRaw;
55
+
56
+ export type CantonResources = {
57
+ partyId: string;
58
+ };
59
+ export type CantonResourcesRaw = {
60
+ partyId: string;
61
+ };
62
+
63
+ export type CantonAccount = Account & {
64
+ cantonResources?: CantonResources;
65
+ };
66
+ export type CantonAccountRaw = AccountRaw & {
67
+ cantonResources: CantonResourcesRaw;
68
+ };
@@ -1,3 +1,5 @@
1
+ import { Account } from "@ledgerhq/types-live";
2
+
1
3
  export enum OnboardStatus {
2
4
  INIT,
3
5
  PREPARE,
@@ -22,6 +24,7 @@ export type CantonOnboardProgress = {
22
24
 
23
25
  export type CantonOnboardResult = {
24
26
  partyId: string;
27
+ account: Partial<Account>;
25
28
  };
26
29
 
27
30
  export type CantonPreApprovalProgress = {
@@ -1,11 +1,12 @@
1
1
  export type CantonAddress = {
2
2
  publicKey: string;
3
3
  address: string;
4
+ path: string; // TODO: check if necessary
4
5
  };
5
6
 
6
7
  export type CantonSignature = string; // `0x${string}`
7
8
 
8
9
  export interface CantonSigner {
9
- getAddress(path: string): Promise<CantonAddress>;
10
+ getAddress(path: string, display?: boolean): Promise<CantonAddress>;
10
11
  signTransaction(path: string, rawTx: string): Promise<CantonSignature>;
11
12
  }