From f47d1d1dbf607cd430a87f2464429a191efa5f4a Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Thu, 2 Jan 2025 15:55:13 +0000 Subject: [PATCH 01/10] chore: save work --- .../aptos/{ => bridge}/bridge.integration.test.ts | 8 ++++---- .../src/families/aptos/synchronisation.test.ts | 0 2 files changed, 4 insertions(+), 4 deletions(-) rename libs/ledger-live-common/src/families/aptos/{ => bridge}/bridge.integration.test.ts (94%) create mode 100644 libs/ledger-live-common/src/families/aptos/synchronisation.test.ts diff --git a/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts b/libs/ledger-live-common/src/families/aptos/bridge/bridge.integration.test.ts similarity index 94% rename from libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts rename to libs/ledger-live-common/src/families/aptos/bridge/bridge.integration.test.ts index 3eb9370529cd..810b24bdf917 100644 --- a/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts +++ b/libs/ledger-live-common/src/families/aptos/bridge/bridge.integration.test.ts @@ -1,9 +1,9 @@ import { CurrenciesData, DatasetTest } from "@ledgerhq/types-live"; import BigNumber from "bignumber.js"; -import { testBridge } from "../../__tests__/test-helpers/bridge"; -import "../../__tests__/test-helpers/setup"; -import { fromTransactionRaw } from "./transaction"; -import { Transaction } from "./types"; +import { testBridge } from "../../../__tests__/test-helpers/bridge"; +import "../../../__tests__/test-helpers/setup"; +import { fromTransactionRaw } from "../transaction"; +import { Transaction } from "../types"; const aptos: CurrenciesData = { scanAccounts: [ diff --git a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts new file mode 100644 index 000000000000..e69de29bb2d1 From 66d626e30c8043a6352932eabdf805e6a3931eb4 Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Thu, 2 Jan 2025 20:29:02 +0000 Subject: [PATCH 02/10] chore: save work --- libs/ledger-live-common/jest.config.ts | 10 +- .../bridge.integration.test.ts.snap | 193 ++++++++++++++++++ .../families/aptos/synchronisation.test.ts | 144 +++++++++++++ .../src/families/aptos/synchronisation.ts | 12 +- 4 files changed, 348 insertions(+), 11 deletions(-) create mode 100644 libs/ledger-live-common/src/families/aptos/bridge/__snapshots__/bridge.integration.test.ts.snap diff --git a/libs/ledger-live-common/jest.config.ts b/libs/ledger-live-common/jest.config.ts index 38b398820b24..dc028f61fbc3 100644 --- a/libs/ledger-live-common/jest.config.ts +++ b/libs/ledger-live-common/jest.config.ts @@ -69,15 +69,15 @@ export default { globalSetup: process.env.UPDATE_BACKEND_MOCKS ? "/src/__tests__/test-helpers/bridgeSetupUpdateMocks.ts" : process.env.USE_BACKEND_MOCKS - ? "/src/__tests__/test-helpers/bridgeSetupUseMocks.ts" - : undefined, + ? "/src/__tests__/test-helpers/bridgeSetupUseMocks.ts" + : undefined, globalTeardown: process.env.UPDATE_BACKEND_MOCKS ? "/src/__tests__/test-helpers/bridgeTeardownUpdateMocks.ts" : process.env.USE_BACKEND_MOCKS - ? "/src/__tests__/test-helpers/bridgeTeardownUseMocks.ts" - : undefined, + ? "/src/__tests__/test-helpers/bridgeTeardownUseMocks.ts" + : undefined, collectCoverage: true, collectCoverageFrom: ["src/**/*.{ts,tsx}"], - coverageReporters: ["json", "lcov", "clover", "json-summary"], + coverageReporters: ["json", "lcov", "clover", "json-summary", "html"], projects: [defaultConfig], }; diff --git a/libs/ledger-live-common/src/families/aptos/bridge/__snapshots__/bridge.integration.test.ts.snap b/libs/ledger-live-common/src/families/aptos/bridge/__snapshots__/bridge.integration.test.ts.snap new file mode 100644 index 000000000000..d7e264eba4a1 --- /dev/null +++ b/libs/ledger-live-common/src/families/aptos/bridge/__snapshots__/bridge.integration.test.ts.snap @@ -0,0 +1,193 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`aptos currency bridge scanAccounts aptos seed 1 1`] = ` +[ + { + "balance": "30100", + "currencyId": "aptos", + "derivationMode": "", + "freshAddress": "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", + "freshAddressPath": "44'/637'/0'/0/0", + "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", + "index": 0, + "operationsCount": 5, + "pendingOperations": [], + "seedIdentifier": "d6816f4f22f867b56cf9304b776f452a16d107835d73ee8a33c4ced210300583", + "spendableBalance": "30100", + "swapHistory": [], + "syncHash": undefined, + "used": true, + "xpub": "d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956", + }, + { + "balance": "20000", + "currencyId": "aptos", + "derivationMode": "", + "freshAddress": "0xd20fa44192f94ba086ab16bfdf57e43ff118ada69b4c66fa9b9a9223cbc068c1", + "freshAddressPath": "44'/637'/1'/0/0", + "id": "js:2:aptos:6a7712fdac0cb4ed27076c707e7798be52cf6c93a2d43d5cf9b874d0a45a111e:", + "index": 1, + "operationsCount": 1, + "pendingOperations": [], + "seedIdentifier": "d6816f4f22f867b56cf9304b776f452a16d107835d73ee8a33c4ced210300583", + "spendableBalance": "20000", + "swapHistory": [], + "syncHash": undefined, + "used": true, + "xpub": "6a7712fdac0cb4ed27076c707e7798be52cf6c93a2d43d5cf9b874d0a45a111e", + }, + { + "balance": "0", + "currencyId": "aptos", + "derivationMode": "", + "freshAddress": "0xf4bf78be42e07959793c98c7e8345bb948bf10b8e6baac5e368eab66d09a9671", + "freshAddressPath": "44'/637'/2'/0/0", + "id": "js:2:aptos:8ffc0c2e141ead220f05b30fa01ce9a3783c5a157219f922b02ec194308b1b45:", + "index": 2, + "operationsCount": 0, + "pendingOperations": [], + "seedIdentifier": "d6816f4f22f867b56cf9304b776f452a16d107835d73ee8a33c4ced210300583", + "spendableBalance": "0", + "swapHistory": [], + "syncHash": undefined, + "used": false, + "xpub": "8ffc0c2e141ead220f05b30fa01ce9a3783c5a157219f922b02ec194308b1b45", + }, +] +`; + +exports[`aptos currency bridge scanAccounts aptos seed 1 2`] = ` +[ + [ + { + "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", + "blockHash": "0x4c000b55c435dc0d1040d26a7ee782fb7e151748d41ea9126ebe6bbbf2e95111", + "blockHeight": 266342014, + "extra": { + "version": "2066042051", + }, + "fee": "900", + "hasFailed": false, + "hash": "0x1e85342da3a81f9c3a30c585677d3e50101d5731dcaf31cc157a3c694e6aece8", + "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0x1e85342da3a81f9c3a30c585677d3e50101d5731dcaf31cc157a3c694e6aece8-IN", + "recipients": [ + "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", + ], + "senders": [ + "0xa0d8abc262e3321f87d745bd5d687e8f3fb14c87d48f840b6b56867df0026ec8", + ], + "transactionSequenceNumber": 10, + "type": "IN", + "value": "100000", + }, + { + "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", + "blockHash": "0x46de657932759f16ffcda6ef6ace41996f263287e538d3e0f5cbf85994695247", + "blockHeight": 266316843, + "extra": { + "version": "2065700142", + }, + "fee": "900", + "hasFailed": false, + "hash": "0x3c586d8c15c76848fbd3c84ddfbf11a1a07c92734da4dd8d530c9f23a0e7744c", + "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0x3c586d8c15c76848fbd3c84ddfbf11a1a07c92734da4dd8d530c9f23a0e7744c-IN", + "recipients": [ + "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", + ], + "senders": [ + "0xa0d8abc262e3321f87d745bd5d687e8f3fb14c87d48f840b6b56867df0026ec8", + ], + "transactionSequenceNumber": 9, + "type": "IN", + "value": "20000", + }, + { + "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", + "blockHash": "0xc546b75fd87fb75a2f328bebffade4ccf3345844eec4d2b6cf82e042fe9a7661", + "blockHeight": 266313878, + "extra": { + "version": "2065659252", + }, + "fee": "99900", + "hasFailed": false, + "hash": "0x5f2c2f597ab912dff9fe413b503036edbcc5488c03a4606f11eef868ed68258e", + "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0x5f2c2f597ab912dff9fe413b503036edbcc5488c03a4606f11eef868ed68258e-IN", + "recipients": [ + "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", + ], + "senders": [ + "0xa0d8abc262e3321f87d745bd5d687e8f3fb14c87d48f840b6b56867df0026ec8", + ], + "transactionSequenceNumber": 7, + "type": "IN", + "value": "20000", + }, + { + "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", + "blockHash": "0x1bbe17059abba7dc500aca2eb174c217e71f3dcd7486cd4b27dc6136cce8a60a", + "blockHeight": 266315762, + "extra": { + "version": "2065684418", + }, + "fee": "900", + "hasFailed": false, + "hash": "0xb74e3ab13f7a00faeb51c0e251602f53d387a64ac9fea7c76a8be3d0bf6f7a19", + "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0xb74e3ab13f7a00faeb51c0e251602f53d387a64ac9fea7c76a8be3d0bf6f7a19-IN", + "recipients": [ + "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", + ], + "senders": [ + "0xa0d8abc262e3321f87d745bd5d687e8f3fb14c87d48f840b6b56867df0026ec8", + ], + "transactionSequenceNumber": 8, + "type": "IN", + "value": "10000", + }, + { + "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", + "blockHash": "0xf37ce698cf2e6d9e4a0048cd6d09fb3f19f417ab8251a3cc1f19b8d0e503538f", + "blockHeight": 266342506, + "extra": { + "version": "2066048548", + }, + "fee": "99900", + "hasFailed": false, + "hash": "0xf980601fe40ad1dab0cc68fe08d2bc95c73e2a21c6d257475e0879394638058e", + "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0xf980601fe40ad1dab0cc68fe08d2bc95c73e2a21c6d257475e0879394638058e-OUT", + "recipients": [ + "0xd20fa44192f94ba086ab16bfdf57e43ff118ada69b4c66fa9b9a9223cbc068c1", + ], + "senders": [ + "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", + ], + "transactionSequenceNumber": 0, + "type": "OUT", + "value": "119900", + }, + ], + [ + { + "accountId": "js:2:aptos:6a7712fdac0cb4ed27076c707e7798be52cf6c93a2d43d5cf9b874d0a45a111e:", + "blockHash": "0xf37ce698cf2e6d9e4a0048cd6d09fb3f19f417ab8251a3cc1f19b8d0e503538f", + "blockHeight": 266342506, + "extra": { + "version": "2066048548", + }, + "fee": "99900", + "hasFailed": false, + "hash": "0xf980601fe40ad1dab0cc68fe08d2bc95c73e2a21c6d257475e0879394638058e", + "id": "js:2:aptos:6a7712fdac0cb4ed27076c707e7798be52cf6c93a2d43d5cf9b874d0a45a111e:-0xf980601fe40ad1dab0cc68fe08d2bc95c73e2a21c6d257475e0879394638058e-IN", + "recipients": [ + "0xd20fa44192f94ba086ab16bfdf57e43ff118ada69b4c66fa9b9a9223cbc068c1", + ], + "senders": [ + "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", + ], + "transactionSequenceNumber": 0, + "type": "IN", + "value": "20000", + }, + ], + [], +] +`; diff --git a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts index e69de29bb2d1..e6f03d999c4b 100644 --- a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts +++ b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts @@ -0,0 +1,144 @@ +import { AccountShapeInfo } from "@ledgerhq/coin-framework/bridge/jsHelpers"; +import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets/index"; +import { Account, SyncConfig } from "@ledgerhq/types-live"; +import { firstValueFrom } from "rxjs"; +import { getAccountShape } from "./synchronisation"; + +jest.mock("rxjs"); +const mockedFistValueFrom = jest.mocked(firstValueFrom); + +// const mockGetServerInfos = jest.fn().mockResolvedValue({ +// info: { +// complete_ledgers: "1-2", +// validated_ledger: { +// reserve_base_xrp: "0", +// reserve_inc_xrp: "0", +// }, +// }, +// }); +// const mockGetAccountInfo = jest.fn().mockResolvedValue({ +// isNewAccount: true, +// balance: "0", +// ownerCount: 0, +// sequence: 0, +// }); +// const mockGetTransactions = jest.fn(); +// jest.mock("../network", () => ({ +// getServerInfos: () => mockGetServerInfos(), +// getAccountInfo: () => mockGetAccountInfo(), +// getTransactions: () => mockGetTransactions(), +// })); + +describe("getAccountShape", () => { + // beforeEach(() => { + // mockGetServerInfos.mockClear(); + // mockGetAccountInfo.mockClear(); + // mockGetTransactions.mockClear(); + // }); + + it.only("returns early when account is new", async () => { + // // Given + // mockGetAccountInfo.mockResolvedValue({ + // isNewAccount: true, + // balance: "0", + // ownerCount: 0, + // sequence: 0, + // }); + + const account = await getAccountShape( + { + address: "address", + currency: getCryptoCurrencyById("aptos"), + derivationMode: "", + index: 0, + xpub: "address", + derivationPath: "", + } as AccountShapeInfo, + {} as SyncConfig, + ); + + expect(account.xpub).toEqual("address"); + expect(mockedFistValueFrom).toHaveBeenCalledTimes(1); + + // Then + // expect(mockGetAccountInfo).toHaveBeenCalledTimes(1); + // expect(mockGetServerInfos).toHaveBeenCalledTimes(0); + // expect(mockGetTransactions).toHaveBeenCalledTimes(0); + // expect(account).toEqual({ + // id: "js:2:ripple:address:", + // xpub: "address", + // blockHeight: 0, + // balance: new BigNumber(0), + // spendableBalance: new BigNumber(0), + // operations: [], + // operationsCount: 0, + // }); + }); + + // it("convert correctly operations", async () => { + // // Given + // mockGetAccountInfo.mockResolvedValue({ + // isNewAccount: false, + // balance: "0", + // ownerCount: 0, + // sequence: 0, + // }); + // mockGetTransactions.mockResolvedValue([ + // { + // ledger_hash: "HASH_VALUE_BLOCK", + // hash: "HASH_VALUE", + // meta: { delivered_amount: "100" }, + // tx_json: { + // TransactionType: "Payment", + // Fee: "10", + // ledger_index: 1, + // date: 1000, + // Account: "account_addr", + // Destination: "destination_addr", + // Sequence: 1, + // }, + // }, + // ]); + + // // When + // const account = await getAccountShape( + // { + // address: "address", + // currency: getCryptoCurrencyById("ripple"), + // derivationMode: "", + // index: 0, + // } as AccountShapeInfo, + // {} as SyncConfig, + // ); + + // // Then + // expect(mockGetAccountInfo).toHaveBeenCalledTimes(1); + // expect(mockGetServerInfos).toHaveBeenCalled(); + // expect(mockGetTransactions).toHaveBeenCalledTimes(1); + // expect(account).toEqual({ + // id: "js:2:ripple:address:", + // xpub: "address", + // blockHeight: 2, + // operationsCount: 1, + // balance: new BigNumber(0), + // spendableBalance: new BigNumber(0), + // operations: [ + // { + // accountId: "js:2:ripple:address:", + // blockHash: "HASH_VALUE_BLOCK", + // blockHeight: 1, + // date: new Date("2000-01-01T00:16:40.000Z"), + // hash: "HASH_VALUE", + // id: "js:2:ripple:address:-HASH_VALUE-IN", + // recipients: ["destination_addr"], + // senders: ["account_addr"], + // transactionSequenceNumber: 1, + // type: "IN", + // value: new BigNumber("100"), + // fee: new BigNumber("10"), + // extra: {}, + // }, + // ], + // }); + // }); +}); diff --git a/libs/ledger-live-common/src/families/aptos/synchronisation.ts b/libs/ledger-live-common/src/families/aptos/synchronisation.ts index 083963d35833..ef98c9fffe65 100644 --- a/libs/ledger-live-common/src/families/aptos/synchronisation.ts +++ b/libs/ledger-live-common/src/families/aptos/synchronisation.ts @@ -1,16 +1,16 @@ -import { from, firstValueFrom } from "rxjs"; -import { withDevice } from "../../hw/deviceAccess"; +import { firstValueFrom, from } from "rxjs"; import type { GetAccountShape } from "../../bridge/jsHelpers"; +import { makeScanAccounts, makeSync, mergeOps } from "../../bridge/jsHelpers"; +import { withDevice } from "../../hw/deviceAccess"; import type { AptosAccount } from "./types"; -import { makeSync, makeScanAccounts, mergeOps } from "../../bridge/jsHelpers"; -import { encodeAccountId, decodeAccountId } from "../../account"; +import { decodeAccountId, encodeAccountId } from "../../account"; +import Aptos from "@ledgerhq/hw-app-aptos"; import { AptosAPI } from "./api"; import { txsToOps } from "./logic"; -import Aptos from "@ledgerhq/hw-app-aptos"; -const getAccountShape: GetAccountShape = async info => { +export const getAccountShape: GetAccountShape = async info => { const { address, initialAccount, derivationMode, currency, deviceId, derivationPath } = info; // "xpub" field is used to store publicKey to simulate transaction during sending tokens. From 419c7b563efe59dd2f01019b54bc396c0af3699c Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Thu, 2 Jan 2025 20:53:21 +0000 Subject: [PATCH 03/10] chore: undo bridge integration test move --- .../{bridge => }/bridge.integration.test.ts | 0 .../bridge.integration.test.ts.snap | 193 ------------------ 2 files changed, 193 deletions(-) rename libs/ledger-live-common/src/families/aptos/{bridge => }/bridge.integration.test.ts (100%) delete mode 100644 libs/ledger-live-common/src/families/aptos/bridge/__snapshots__/bridge.integration.test.ts.snap diff --git a/libs/ledger-live-common/src/families/aptos/bridge/bridge.integration.test.ts b/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts similarity index 100% rename from libs/ledger-live-common/src/families/aptos/bridge/bridge.integration.test.ts rename to libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts diff --git a/libs/ledger-live-common/src/families/aptos/bridge/__snapshots__/bridge.integration.test.ts.snap b/libs/ledger-live-common/src/families/aptos/bridge/__snapshots__/bridge.integration.test.ts.snap deleted file mode 100644 index d7e264eba4a1..000000000000 --- a/libs/ledger-live-common/src/families/aptos/bridge/__snapshots__/bridge.integration.test.ts.snap +++ /dev/null @@ -1,193 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`aptos currency bridge scanAccounts aptos seed 1 1`] = ` -[ - { - "balance": "30100", - "currencyId": "aptos", - "derivationMode": "", - "freshAddress": "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", - "freshAddressPath": "44'/637'/0'/0/0", - "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", - "index": 0, - "operationsCount": 5, - "pendingOperations": [], - "seedIdentifier": "d6816f4f22f867b56cf9304b776f452a16d107835d73ee8a33c4ced210300583", - "spendableBalance": "30100", - "swapHistory": [], - "syncHash": undefined, - "used": true, - "xpub": "d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956", - }, - { - "balance": "20000", - "currencyId": "aptos", - "derivationMode": "", - "freshAddress": "0xd20fa44192f94ba086ab16bfdf57e43ff118ada69b4c66fa9b9a9223cbc068c1", - "freshAddressPath": "44'/637'/1'/0/0", - "id": "js:2:aptos:6a7712fdac0cb4ed27076c707e7798be52cf6c93a2d43d5cf9b874d0a45a111e:", - "index": 1, - "operationsCount": 1, - "pendingOperations": [], - "seedIdentifier": "d6816f4f22f867b56cf9304b776f452a16d107835d73ee8a33c4ced210300583", - "spendableBalance": "20000", - "swapHistory": [], - "syncHash": undefined, - "used": true, - "xpub": "6a7712fdac0cb4ed27076c707e7798be52cf6c93a2d43d5cf9b874d0a45a111e", - }, - { - "balance": "0", - "currencyId": "aptos", - "derivationMode": "", - "freshAddress": "0xf4bf78be42e07959793c98c7e8345bb948bf10b8e6baac5e368eab66d09a9671", - "freshAddressPath": "44'/637'/2'/0/0", - "id": "js:2:aptos:8ffc0c2e141ead220f05b30fa01ce9a3783c5a157219f922b02ec194308b1b45:", - "index": 2, - "operationsCount": 0, - "pendingOperations": [], - "seedIdentifier": "d6816f4f22f867b56cf9304b776f452a16d107835d73ee8a33c4ced210300583", - "spendableBalance": "0", - "swapHistory": [], - "syncHash": undefined, - "used": false, - "xpub": "8ffc0c2e141ead220f05b30fa01ce9a3783c5a157219f922b02ec194308b1b45", - }, -] -`; - -exports[`aptos currency bridge scanAccounts aptos seed 1 2`] = ` -[ - [ - { - "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", - "blockHash": "0x4c000b55c435dc0d1040d26a7ee782fb7e151748d41ea9126ebe6bbbf2e95111", - "blockHeight": 266342014, - "extra": { - "version": "2066042051", - }, - "fee": "900", - "hasFailed": false, - "hash": "0x1e85342da3a81f9c3a30c585677d3e50101d5731dcaf31cc157a3c694e6aece8", - "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0x1e85342da3a81f9c3a30c585677d3e50101d5731dcaf31cc157a3c694e6aece8-IN", - "recipients": [ - "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", - ], - "senders": [ - "0xa0d8abc262e3321f87d745bd5d687e8f3fb14c87d48f840b6b56867df0026ec8", - ], - "transactionSequenceNumber": 10, - "type": "IN", - "value": "100000", - }, - { - "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", - "blockHash": "0x46de657932759f16ffcda6ef6ace41996f263287e538d3e0f5cbf85994695247", - "blockHeight": 266316843, - "extra": { - "version": "2065700142", - }, - "fee": "900", - "hasFailed": false, - "hash": "0x3c586d8c15c76848fbd3c84ddfbf11a1a07c92734da4dd8d530c9f23a0e7744c", - "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0x3c586d8c15c76848fbd3c84ddfbf11a1a07c92734da4dd8d530c9f23a0e7744c-IN", - "recipients": [ - "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", - ], - "senders": [ - "0xa0d8abc262e3321f87d745bd5d687e8f3fb14c87d48f840b6b56867df0026ec8", - ], - "transactionSequenceNumber": 9, - "type": "IN", - "value": "20000", - }, - { - "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", - "blockHash": "0xc546b75fd87fb75a2f328bebffade4ccf3345844eec4d2b6cf82e042fe9a7661", - "blockHeight": 266313878, - "extra": { - "version": "2065659252", - }, - "fee": "99900", - "hasFailed": false, - "hash": "0x5f2c2f597ab912dff9fe413b503036edbcc5488c03a4606f11eef868ed68258e", - "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0x5f2c2f597ab912dff9fe413b503036edbcc5488c03a4606f11eef868ed68258e-IN", - "recipients": [ - "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", - ], - "senders": [ - "0xa0d8abc262e3321f87d745bd5d687e8f3fb14c87d48f840b6b56867df0026ec8", - ], - "transactionSequenceNumber": 7, - "type": "IN", - "value": "20000", - }, - { - "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", - "blockHash": "0x1bbe17059abba7dc500aca2eb174c217e71f3dcd7486cd4b27dc6136cce8a60a", - "blockHeight": 266315762, - "extra": { - "version": "2065684418", - }, - "fee": "900", - "hasFailed": false, - "hash": "0xb74e3ab13f7a00faeb51c0e251602f53d387a64ac9fea7c76a8be3d0bf6f7a19", - "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0xb74e3ab13f7a00faeb51c0e251602f53d387a64ac9fea7c76a8be3d0bf6f7a19-IN", - "recipients": [ - "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", - ], - "senders": [ - "0xa0d8abc262e3321f87d745bd5d687e8f3fb14c87d48f840b6b56867df0026ec8", - ], - "transactionSequenceNumber": 8, - "type": "IN", - "value": "10000", - }, - { - "accountId": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:", - "blockHash": "0xf37ce698cf2e6d9e4a0048cd6d09fb3f19f417ab8251a3cc1f19b8d0e503538f", - "blockHeight": 266342506, - "extra": { - "version": "2066048548", - }, - "fee": "99900", - "hasFailed": false, - "hash": "0xf980601fe40ad1dab0cc68fe08d2bc95c73e2a21c6d257475e0879394638058e", - "id": "js:2:aptos:d1a8c6a1cdd52dd40c7ea61ee4571fb51fcae440a594c1eca18636928f1d3956:-0xf980601fe40ad1dab0cc68fe08d2bc95c73e2a21c6d257475e0879394638058e-OUT", - "recipients": [ - "0xd20fa44192f94ba086ab16bfdf57e43ff118ada69b4c66fa9b9a9223cbc068c1", - ], - "senders": [ - "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", - ], - "transactionSequenceNumber": 0, - "type": "OUT", - "value": "119900", - }, - ], - [ - { - "accountId": "js:2:aptos:6a7712fdac0cb4ed27076c707e7798be52cf6c93a2d43d5cf9b874d0a45a111e:", - "blockHash": "0xf37ce698cf2e6d9e4a0048cd6d09fb3f19f417ab8251a3cc1f19b8d0e503538f", - "blockHeight": 266342506, - "extra": { - "version": "2066048548", - }, - "fee": "99900", - "hasFailed": false, - "hash": "0xf980601fe40ad1dab0cc68fe08d2bc95c73e2a21c6d257475e0879394638058e", - "id": "js:2:aptos:6a7712fdac0cb4ed27076c707e7798be52cf6c93a2d43d5cf9b874d0a45a111e:-0xf980601fe40ad1dab0cc68fe08d2bc95c73e2a21c6d257475e0879394638058e-IN", - "recipients": [ - "0xd20fa44192f94ba086ab16bfdf57e43ff118ada69b4c66fa9b9a9223cbc068c1", - ], - "senders": [ - "0x445fa0013887abd1a0c14acdec6e48090e0ad3fed3e08202aac15ca14f3be26b", - ], - "transactionSequenceNumber": 0, - "type": "IN", - "value": "20000", - }, - ], - [], -] -`; From 9f237ef9e543a78f953f4f6cec3d23ff1d4dd9db Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Thu, 2 Jan 2025 20:54:15 +0000 Subject: [PATCH 04/10] fix: import paths --- .../src/families/aptos/bridge.integration.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts b/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts index b7943a4ed119..8ff913a4403e 100644 --- a/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts +++ b/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts @@ -1,9 +1,9 @@ import { CurrenciesData, DatasetTest } from "@ledgerhq/types-live"; import BigNumber from "bignumber.js"; -import { testBridge } from "../../../__tests__/test-helpers/bridge"; -import "../../../__tests__/test-helpers/setup"; -import { fromTransactionRaw } from "../transaction"; -import { Transaction } from "../types"; +import { testBridge } from "../../__tests__/test-helpers/bridge"; +import "../../__tests__/test-helpers/setup"; +import { fromTransactionRaw } from "./transaction"; +import { Transaction } from "./types"; const aptos: CurrenciesData = { scanAccounts: [ From 26539f15fdee0fbccedd7ddedcce80c25d0c06ff Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Fri, 3 Jan 2025 17:58:27 +0000 Subject: [PATCH 05/10] test: synchronisation tests --- .../src/families/aptos/api/index.test.ts | 4 +- .../families/aptos/synchronisation.test.ts | 293 +++++++++++------- .../src/families/aptos/synchronisation.ts | 8 +- 3 files changed, 180 insertions(+), 125 deletions(-) diff --git a/libs/ledger-live-common/src/families/aptos/api/index.test.ts b/libs/ledger-live-common/src/families/aptos/api/index.test.ts index ca140f293687..eff5afc25973 100644 --- a/libs/ledger-live-common/src/families/aptos/api/index.test.ts +++ b/libs/ledger-live-common/src/families/aptos/api/index.test.ts @@ -75,12 +75,12 @@ describe("Aptos API", () => { }; }); - const mockSimpleSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount"); + const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount"); const api = new AptosAPI("aptos"); await api.getAccount(Account.APTOS_1.address); - expect(mockSimpleSpy).toHaveBeenCalledWith({ + expect(mockGetAccountSpy).toHaveBeenCalledWith({ accountAddress: Account.APTOS_1.address, }); }); diff --git a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts index e6f03d999c4b..afc1355b1ec1 100644 --- a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts +++ b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts @@ -2,143 +2,200 @@ import { AccountShapeInfo } from "@ledgerhq/coin-framework/bridge/jsHelpers"; import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets/index"; import { Account, SyncConfig } from "@ledgerhq/types-live"; import { firstValueFrom } from "rxjs"; +import { decodeAccountId } from "../../account"; +import { makeScanAccounts, makeSync, mergeOps } from "../../bridge/jsHelpers"; +import { AptosAPI } from "./api"; +import { txsToOps } from "./logic"; import { getAccountShape } from "./synchronisation"; jest.mock("rxjs"); -const mockedFistValueFrom = jest.mocked(firstValueFrom); - -// const mockGetServerInfos = jest.fn().mockResolvedValue({ -// info: { -// complete_ledgers: "1-2", -// validated_ledger: { -// reserve_base_xrp: "0", -// reserve_inc_xrp: "0", -// }, -// }, -// }); -// const mockGetAccountInfo = jest.fn().mockResolvedValue({ -// isNewAccount: true, -// balance: "0", -// ownerCount: 0, -// sequence: 0, -// }); -// const mockGetTransactions = jest.fn(); -// jest.mock("../network", () => ({ -// getServerInfos: () => mockGetServerInfos(), -// getAccountInfo: () => mockGetAccountInfo(), -// getTransactions: () => mockGetTransactions(), -// })); +let mockedFistValueFrom; + +jest.mock("../../account"); +let mockedDecodeAccountId; + +jest.mock("./api"); +let mockedAptosAPI; + +jest.mock("./logic"); +jest.mocked(txsToOps); + +jest.mock("../../bridge/jsHelpers"); +jest.mocked(makeScanAccounts); +jest.mocked(makeSync); describe("getAccountShape", () => { - // beforeEach(() => { - // mockGetServerInfos.mockClear(); - // mockGetAccountInfo.mockClear(); - // mockGetTransactions.mockClear(); - // }); - - it.only("returns early when account is new", async () => { - // // Given - // mockGetAccountInfo.mockResolvedValue({ - // isNewAccount: true, - // balance: "0", - // ownerCount: 0, - // sequence: 0, - // }); + beforeEach(() => { + mockedAptosAPI = jest.mocked(AptosAPI); + + mockedDecodeAccountId = jest.mocked(decodeAccountId).mockReturnValue({ + currencyId: "aptos", + derivationMode: "", + type: "js", + version: "1", + xpubOrAddress: "address", + }); + + mockedFistValueFrom = jest + .mocked(firstValueFrom) + .mockImplementation(async () => ({ publicKey: "publicKey" })); + + jest.mocked(mergeOps).mockReturnValue([]); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it("get xpub from device id", async () => { + const mockGetAccountInfo = jest.fn().mockImplementation(async () => ({ + balance: BigInt(0), + transactions: [], + blockHeight: 0, + })); + mockedAptosAPI.mockImplementation(() => ({ + getAccountInfo: mockGetAccountInfo, + })); + const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount"); const account = await getAccountShape( { + id: "1", address: "address", currency: getCryptoCurrencyById("aptos"), derivationMode: "", index: 0, xpub: "address", derivationPath: "", - } as AccountShapeInfo, + deviceId: "1", + initialAccount: { + id: "1:1:1:1:1", + // xpub: "address", + seedIdentifier: "1", + derivationMode: "", + index: 0, + freshAddress: "address", + freshAddressPath: "", + used: true, + balance: BigInt(10), + spendableBalance: BigInt(10), + creationDate: new Date(), + blockHeight: 0, + currency: getCryptoCurrencyById("aptos"), + operationsCount: 0, + operations: [], + pendingOperations: [], + lastSyncDate: new Date(), + balanceHistoryCache: {}, + swapHistory: [], + }, + } as unknown as AccountShapeInfo, {} as SyncConfig, ); - expect(account.xpub).toEqual("address"); + expect(account.xpub).toEqual("7075626c69634b6579"); expect(mockedFistValueFrom).toHaveBeenCalledTimes(1); + expect(mockedDecodeAccountId).toHaveBeenCalledTimes(0); + expect(mockedAptosAPI).toHaveBeenCalledTimes(1); + expect(mockGetAccountSpy).toHaveBeenCalledWith("address", undefined); + }); + + it("get xpub from initial account id", async () => { + const mockGetAccountInfo = jest.fn().mockImplementation(async () => ({ + balance: BigInt(0), + transactions: [], + blockHeight: 0, + })); + mockedAptosAPI.mockImplementation(() => ({ + getAccountInfo: mockGetAccountInfo, + })); + const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount"); - // Then - // expect(mockGetAccountInfo).toHaveBeenCalledTimes(1); - // expect(mockGetServerInfos).toHaveBeenCalledTimes(0); - // expect(mockGetTransactions).toHaveBeenCalledTimes(0); - // expect(account).toEqual({ - // id: "js:2:ripple:address:", - // xpub: "address", - // blockHeight: 0, - // balance: new BigNumber(0), - // spendableBalance: new BigNumber(0), - // operations: [], - // operationsCount: 0, - // }); + const account = await getAccountShape( + { + id: "1", + address: "address", + currency: getCryptoCurrencyById("aptos"), + derivationMode: "", + index: 0, + xpub: "address", + derivationPath: "", + // deviceId: "1", + initialAccount: { + id: "1:1:1:1:1", + // xpub: "address", + seedIdentifier: "1", + derivationMode: "", + index: 0, + freshAddress: "address", + freshAddressPath: "", + used: true, + balance: BigInt(10), + spendableBalance: BigInt(10), + creationDate: new Date(), + blockHeight: 0, + currency: getCryptoCurrencyById("aptos"), + operationsCount: 0, + operations: [], + pendingOperations: [], + lastSyncDate: new Date(), + balanceHistoryCache: {}, + swapHistory: [], + }, + } as unknown as AccountShapeInfo, + {} as SyncConfig, + ); + + expect(account.xpub).toEqual("address"); + expect(mockedFistValueFrom).toHaveBeenCalledTimes(0); + expect(mockedDecodeAccountId).toHaveBeenCalledTimes(1); + expect(mockedAptosAPI).toHaveBeenCalledTimes(1); + expect(mockGetAccountSpy).toHaveBeenCalledWith("address", undefined); }); - // it("convert correctly operations", async () => { - // // Given - // mockGetAccountInfo.mockResolvedValue({ - // isNewAccount: false, - // balance: "0", - // ownerCount: 0, - // sequence: 0, - // }); - // mockGetTransactions.mockResolvedValue([ - // { - // ledger_hash: "HASH_VALUE_BLOCK", - // hash: "HASH_VALUE", - // meta: { delivered_amount: "100" }, - // tx_json: { - // TransactionType: "Payment", - // Fee: "10", - // ledger_index: 1, - // date: 1000, - // Account: "account_addr", - // Destination: "destination_addr", - // Sequence: 1, - // }, - // }, - // ]); - - // // When - // const account = await getAccountShape( - // { - // address: "address", - // currency: getCryptoCurrencyById("ripple"), - // derivationMode: "", - // index: 0, - // } as AccountShapeInfo, - // {} as SyncConfig, - // ); - - // // Then - // expect(mockGetAccountInfo).toHaveBeenCalledTimes(1); - // expect(mockGetServerInfos).toHaveBeenCalled(); - // expect(mockGetTransactions).toHaveBeenCalledTimes(1); - // expect(account).toEqual({ - // id: "js:2:ripple:address:", - // xpub: "address", - // blockHeight: 2, - // operationsCount: 1, - // balance: new BigNumber(0), - // spendableBalance: new BigNumber(0), - // operations: [ - // { - // accountId: "js:2:ripple:address:", - // blockHash: "HASH_VALUE_BLOCK", - // blockHeight: 1, - // date: new Date("2000-01-01T00:16:40.000Z"), - // hash: "HASH_VALUE", - // id: "js:2:ripple:address:-HASH_VALUE-IN", - // recipients: ["destination_addr"], - // senders: ["account_addr"], - // transactionSequenceNumber: 1, - // type: "IN", - // value: new BigNumber("100"), - // fee: new BigNumber("10"), - // extra: {}, - // }, - // ], - // }); - // }); + it("unable to get xpub", async () => { + mockedDecodeAccountId = jest.mocked(decodeAccountId).mockImplementation(() => { + throw new Error("Unable to retrieve public key"); + }); + + expect( + async () => + await getAccountShape( + { + id: "1", + address: "address", + currency: getCryptoCurrencyById("aptos"), + derivationMode: "", + index: 0, + xpub: "address", + derivationPath: "", + // deviceId: "1", + initialAccount: { + id: "1:1:1:1:1", + // xpub: "address", + seedIdentifier: "1", + derivationMode: "", + index: 0, + freshAddress: "address", + freshAddressPath: "", + used: true, + balance: BigInt(10), + spendableBalance: BigInt(10), + creationDate: new Date(), + blockHeight: 0, + currency: getCryptoCurrencyById("aptos"), + operationsCount: 0, + operations: [], + pendingOperations: [], + lastSyncDate: new Date(), + balanceHistoryCache: {}, + swapHistory: [], + }, + } as unknown as AccountShapeInfo, + {} as SyncConfig, + ), + ).rejects.toThrow(); + + expect; + }); }); diff --git a/libs/ledger-live-common/src/families/aptos/synchronisation.ts b/libs/ledger-live-common/src/families/aptos/synchronisation.ts index ef98c9fffe65..2e692a3dce1a 100644 --- a/libs/ledger-live-common/src/families/aptos/synchronisation.ts +++ b/libs/ledger-live-common/src/families/aptos/synchronisation.ts @@ -1,14 +1,12 @@ +import Aptos from "@ledgerhq/hw-app-aptos"; import { firstValueFrom, from } from "rxjs"; +import { decodeAccountId, encodeAccountId } from "../../account"; import type { GetAccountShape } from "../../bridge/jsHelpers"; import { makeScanAccounts, makeSync, mergeOps } from "../../bridge/jsHelpers"; import { withDevice } from "../../hw/deviceAccess"; -import type { AptosAccount } from "./types"; - -import { decodeAccountId, encodeAccountId } from "../../account"; - -import Aptos from "@ledgerhq/hw-app-aptos"; import { AptosAPI } from "./api"; import { txsToOps } from "./logic"; +import type { AptosAccount } from "./types"; export const getAccountShape: GetAccountShape = async info => { const { address, initialAccount, derivationMode, currency, deviceId, derivationPath } = info; From 50b50b06851621a0eab93591cb2048d03587f857 Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Fri, 3 Jan 2025 18:01:52 +0000 Subject: [PATCH 06/10] test: synchronisation tests --- .../src/families/aptos/synchronisation.test.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts index afc1355b1ec1..5f0f4bec20e9 100644 --- a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts +++ b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts @@ -154,8 +154,12 @@ describe("getAccountShape", () => { }); it("unable to get xpub", async () => { - mockedDecodeAccountId = jest.mocked(decodeAccountId).mockImplementation(() => { - throw new Error("Unable to retrieve public key"); + mockedDecodeAccountId = jest.mocked(decodeAccountId).mockReturnValue({ + currencyId: "aptos", + derivationMode: "", + type: "js", + version: "1", + xpubOrAddress: "", }); expect( @@ -194,8 +198,6 @@ describe("getAccountShape", () => { } as unknown as AccountShapeInfo, {} as SyncConfig, ), - ).rejects.toThrow(); - - expect; + ).rejects.toThrow("Unable to retrieve public key"); }); }); From e6c0106818e64c7962c05a8d2ea50e3573ce6cb2 Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Fri, 3 Jan 2025 18:05:58 +0000 Subject: [PATCH 07/10] test: synchronisation tests --- .../families/aptos/synchronisation.test.ts | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts index 5f0f4bec20e9..2d76416fd2f8 100644 --- a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts +++ b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts @@ -100,6 +100,38 @@ describe("getAccountShape", () => { expect(mockGetAccountSpy).toHaveBeenCalledWith("address", undefined); }); + it("get xpub from device id when there is no initial account", async () => { + const mockGetAccountInfo = jest.fn().mockImplementation(async () => ({ + balance: BigInt(0), + transactions: [], + blockHeight: 0, + })); + mockedAptosAPI.mockImplementation(() => ({ + getAccountInfo: mockGetAccountInfo, + })); + const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount"); + + const account = await getAccountShape( + { + id: "1", + address: "address", + currency: getCryptoCurrencyById("aptos"), + derivationMode: "", + index: 0, + xpub: "address", + derivationPath: "", + deviceId: "1", + } as unknown as AccountShapeInfo, + {} as SyncConfig, + ); + + expect(account.xpub).toEqual("7075626c69634b6579"); + expect(mockedFistValueFrom).toHaveBeenCalledTimes(1); + expect(mockedDecodeAccountId).toHaveBeenCalledTimes(0); + expect(mockedAptosAPI).toHaveBeenCalledTimes(1); + expect(mockGetAccountSpy).toHaveBeenCalledWith("address", undefined); + }); + it("get xpub from initial account id", async () => { const mockGetAccountInfo = jest.fn().mockImplementation(async () => ({ balance: BigInt(0), @@ -153,7 +185,7 @@ describe("getAccountShape", () => { expect(mockGetAccountSpy).toHaveBeenCalledWith("address", undefined); }); - it("unable to get xpub", async () => { + it("unable to get xpub error is thrown", async () => { mockedDecodeAccountId = jest.mocked(decodeAccountId).mockReturnValue({ currencyId: "aptos", derivationMode: "", @@ -200,4 +232,30 @@ describe("getAccountShape", () => { ), ).rejects.toThrow("Unable to retrieve public key"); }); + + it("unable to get xpub error is thrown when there is no initial account", async () => { + mockedDecodeAccountId = jest.mocked(decodeAccountId).mockReturnValue({ + currencyId: "aptos", + derivationMode: "", + type: "js", + version: "1", + xpubOrAddress: "", + }); + + expect( + async () => + await getAccountShape( + { + id: "1", + address: "address", + currency: getCryptoCurrencyById("aptos"), + derivationMode: "", + index: 0, + xpub: "address", + derivationPath: "", + } as unknown as AccountShapeInfo, + {} as SyncConfig, + ), + ).rejects.toThrow("Unable to retrieve public key"); + }); }); From c95bebfc2b7f5e271f15719e16042eb997d77505 Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Fri, 3 Jan 2025 18:13:35 +0000 Subject: [PATCH 08/10] test: synchronisation tests --- .../families/aptos/synchronisation.test.ts | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts index 2d76416fd2f8..84e58dd2a99d 100644 --- a/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts +++ b/libs/ledger-live-common/src/families/aptos/synchronisation.test.ts @@ -258,4 +258,140 @@ describe("getAccountShape", () => { ), ).rejects.toThrow("Unable to retrieve public key"); }); + + it("get xpub from device id and account has operations history", async () => { + const mockGetAccountInfo = jest.fn().mockImplementation(async () => ({ + balance: BigInt(0), + transactions: [], + blockHeight: 0, + })); + mockedAptosAPI.mockImplementation(() => ({ + getAccountInfo: mockGetAccountInfo, + })); + const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount"); + + const account = await getAccountShape( + { + id: "1", + address: "address", + currency: getCryptoCurrencyById("aptos"), + derivationMode: "", + index: 0, + xpub: "address", + derivationPath: "", + deviceId: "1", + initialAccount: { + id: "1:1:1:1:1", + // xpub: "address", + seedIdentifier: "1", + derivationMode: "", + index: 0, + freshAddress: "address", + freshAddressPath: "", + used: true, + balance: BigInt(10), + spendableBalance: BigInt(10), + creationDate: new Date(), + blockHeight: 0, + currency: getCryptoCurrencyById("aptos"), + operationsCount: 1, + operations: [ + { + id: "1", + hash: "hash", + type: "OUT", + value: BigInt(10), + fee: BigInt(0), + blockHeight: 0, + blockHash: "blockHash", + accountId: "1", + senders: ["sender"], + recipients: ["recipient"], + date: new Date(), + // extra: {}, + }, + ], + pendingOperations: [], + lastSyncDate: new Date(), + balanceHistoryCache: {}, + swapHistory: [], + }, + } as unknown as AccountShapeInfo, + {} as SyncConfig, + ); + + expect(account.xpub).toEqual("7075626c69634b6579"); + expect(mockedFistValueFrom).toHaveBeenCalledTimes(1); + expect(mockedDecodeAccountId).toHaveBeenCalledTimes(0); + expect(mockedAptosAPI).toHaveBeenCalledTimes(1); + expect(mockGetAccountSpy).toHaveBeenCalledWith("address", undefined); + }); + + it("get xpub from device id and account has operations history with extra", async () => { + const mockGetAccountInfo = jest.fn().mockImplementation(async () => ({ + balance: BigInt(0), + transactions: [], + blockHeight: 0, + })); + mockedAptosAPI.mockImplementation(() => ({ + getAccountInfo: mockGetAccountInfo, + })); + const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount"); + + const account = await getAccountShape( + { + id: "1", + address: "address", + currency: getCryptoCurrencyById("aptos"), + derivationMode: "", + index: 0, + xpub: "address", + derivationPath: "", + deviceId: "1", + initialAccount: { + id: "1:1:1:1:1", + // xpub: "address", + seedIdentifier: "1", + derivationMode: "", + index: 0, + freshAddress: "address", + freshAddressPath: "", + used: true, + balance: BigInt(10), + spendableBalance: BigInt(10), + creationDate: new Date(), + blockHeight: 0, + currency: getCryptoCurrencyById("aptos"), + operationsCount: 1, + operations: [ + { + id: "1", + hash: "hash", + type: "OUT", + value: BigInt(10), + fee: BigInt(0), + blockHeight: 0, + blockHash: "blockHash", + accountId: "1", + senders: ["sender"], + recipients: ["recipient"], + date: new Date(), + extra: { version: 1 }, + }, + ], + pendingOperations: [], + lastSyncDate: new Date(), + balanceHistoryCache: {}, + swapHistory: [], + }, + } as unknown as AccountShapeInfo, + {} as SyncConfig, + ); + + expect(account.xpub).toEqual("7075626c69634b6579"); + expect(mockedFistValueFrom).toHaveBeenCalledTimes(1); + expect(mockedDecodeAccountId).toHaveBeenCalledTimes(0); + expect(mockedAptosAPI).toHaveBeenCalledTimes(1); + expect(mockGetAccountSpy).toHaveBeenCalledWith("address", 1); + }); }); From 6ab6d11284dc942a76e33ccca86e09969edefa0d Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Fri, 3 Jan 2025 18:14:54 +0000 Subject: [PATCH 09/10] fix: remove jest config --- libs/ledger-live-common/jest.config.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/ledger-live-common/jest.config.ts b/libs/ledger-live-common/jest.config.ts index dc028f61fbc3..ff58b5cc236e 100644 --- a/libs/ledger-live-common/jest.config.ts +++ b/libs/ledger-live-common/jest.config.ts @@ -69,13 +69,13 @@ export default { globalSetup: process.env.UPDATE_BACKEND_MOCKS ? "/src/__tests__/test-helpers/bridgeSetupUpdateMocks.ts" : process.env.USE_BACKEND_MOCKS - ? "/src/__tests__/test-helpers/bridgeSetupUseMocks.ts" - : undefined, + ? "/src/__tests__/test-helpers/bridgeSetupUseMocks.ts" + : undefined, globalTeardown: process.env.UPDATE_BACKEND_MOCKS ? "/src/__tests__/test-helpers/bridgeTeardownUpdateMocks.ts" : process.env.USE_BACKEND_MOCKS - ? "/src/__tests__/test-helpers/bridgeTeardownUseMocks.ts" - : undefined, + ? "/src/__tests__/test-helpers/bridgeTeardownUseMocks.ts" + : undefined, collectCoverage: true, collectCoverageFrom: ["src/**/*.{ts,tsx}"], coverageReporters: ["json", "lcov", "clover", "json-summary", "html"], From 003b3875b7ab2347b4a95cfac7efc46bed65d00c Mon Sep 17 00:00:00 2001 From: Pedro Semeano Date: Fri, 3 Jan 2025 18:15:24 +0000 Subject: [PATCH 10/10] fix: remove jest config --- libs/ledger-live-common/jest.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/ledger-live-common/jest.config.ts b/libs/ledger-live-common/jest.config.ts index ff58b5cc236e..38b398820b24 100644 --- a/libs/ledger-live-common/jest.config.ts +++ b/libs/ledger-live-common/jest.config.ts @@ -78,6 +78,6 @@ export default { : undefined, collectCoverage: true, collectCoverageFrom: ["src/**/*.{ts,tsx}"], - coverageReporters: ["json", "lcov", "clover", "json-summary", "html"], + coverageReporters: ["json", "lcov", "clover", "json-summary"], projects: [defaultConfig], };