UNPKG

@ledgerhq/coin-aptos

Version:
299 lines 12.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const bignumber_js_1 = __importDefault(require("bignumber.js")); const rxjs_1 = require("rxjs"); const bridge_fixture_1 = require("../../bridge/bridge.fixture"); const signOperation_1 = __importStar(require("../../bridge/signOperation")); const network_1 = require("../../network"); const faker_1 = require("@faker-js/faker"); const index_1 = require("@ledgerhq/coin-framework/account/index"); const generateTransaction = jest.fn(() => "tx"); jest.mock("../../network/client", () => { return { AptosAPI: () => ({ generateTransaction, }), }; }); jest.mock("../../logic/buildTransaction", () => { return () => ({ sequence_number: "789", }); }); jest.mock("../../network"); let mockedSignTransaction; describe("getAddress", () => { it("should return address and derivationPath", () => { const account = (0, bridge_fixture_1.createFixtureAccount)(); expect((0, signOperation_1.getAddress)(account)).toEqual({ address: "address", derivationPath: "derivation_path" }); }); }); describe("buildSignOperation", () => { beforeEach(() => { mockedSignTransaction = jest.mocked(network_1.signTransaction); }); afterEach(() => jest.clearAllMocks()); it("should thrown an error", async () => { mockedSignTransaction.mockImplementation(() => { throw new Error("observable-catch-error"); }); const account = (0, bridge_fixture_1.createFixtureAccount)(); const transaction = (0, bridge_fixture_1.createFixtureTransaction)(); account.id = "js:2:aptos:0x000:"; transaction.mode = "send"; const observable = await (0, signOperation_1.default)({})({ account, deviceId: "1", transaction, }); observable.subscribe({ error: err => { expect(err.message).toBe("observable-catch-error"); }, }); }); it("should return 3 operations", async () => { mockedSignTransaction.mockReturnValue("signedTx"); const date = new Date("2020-01-01"); jest.useFakeTimers().setSystemTime(date); const account = (0, bridge_fixture_1.createFixtureAccount)(); const transaction = (0, bridge_fixture_1.createFixtureTransaction)(); account.id = "js:2:aptos:0x000:"; transaction.mode = "send"; const observable = await (0, signOperation_1.default)({})({ account, deviceId: "1", transaction, }); expect(observable).toBeInstanceOf(rxjs_1.Observable); const expectedValues = [ { type: "device-signature-requested" }, { type: "device-signature-granted" }, { type: "signed", signedOperation: { operation: { id: "js:2:aptos:0x000:--OUT", hash: "", type: "OUT", value: new bignumber_js_1.default(0), fee: new bignumber_js_1.default(0), extra: {}, blockHash: null, blockHeight: null, senders: [account.freshAddress], recipients: [transaction.recipient], accountId: "js:2:aptos:0x000:", date, transactionSequenceNumber: 789, subOperations: [], }, signature: "7369676e65645478", }, }, ]; let i = 0; observable.forEach(signOperationEvent => { expect(signOperationEvent).toEqual(expectedValues[i]); i++; }); }); it("should return 3 operations with all amount", async () => { mockedSignTransaction.mockReturnValue("signedTx"); const date = new Date("2020-01-01"); jest.useFakeTimers().setSystemTime(date); const account = (0, bridge_fixture_1.createFixtureAccount)(); const transaction = (0, bridge_fixture_1.createFixtureTransaction)({ amount: (0, bignumber_js_1.default)(5) }); account.balance = new bignumber_js_1.default(40); transaction.fees = new bignumber_js_1.default(30); transaction.useAllAmount = true; account.id = "js:2:aptos:0x000:"; transaction.mode = "send"; const observable = await (0, signOperation_1.default)({})({ account, deviceId: "1", transaction, }); expect(observable).toBeInstanceOf(rxjs_1.Observable); const expectedValues = [ { type: "device-signature-requested" }, { type: "device-signature-granted" }, { type: "signed", signedOperation: { operation: { id: "js:2:aptos:0x000:--OUT", hash: "", type: "OUT", value: new bignumber_js_1.default(35), fee: transaction.fees, extra: {}, blockHash: null, blockHeight: null, senders: [account.freshAddress], recipients: [transaction.recipient], accountId: "js:2:aptos:0x000:", date, transactionSequenceNumber: 789, subOperations: [], }, signature: "7369676e65645478", }, }, ]; let i = 0; observable.forEach(signOperationEvent => { expect(signOperationEvent).toEqual(expectedValues[i]); i++; }); }); it("should have sub operations if it has subaccount", async () => { mockedSignTransaction.mockReturnValue("signedTx"); const date = new Date("2020-01-01"); jest.useFakeTimers().setSystemTime(date); const id = faker_1.faker.string.uuid(); const account = (0, bridge_fixture_1.createFixtureAccount)({ id, subAccounts: [ { type: "TokenAccount", id: "subAccountId", parentId: id, token: { type: "TokenCurrency", id: "aptos/coin/dstapt_0xd11107bdf0d6d7040c6c0bfbdecb6545191fdf13e8d8d259952f53e1713f61b5::staked_coin::stakedaptos", contractAddress: "0xd11107bdf0d6d7040c6c0bfbdecb6545191fdf13e8d8d259952f53e1713f61b5::staked_coin::StakedAptos", parentCurrency: { type: "CryptoCurrency", id: "aptos", coinType: 637, name: "Aptos", managerAppName: "Aptos", ticker: "APT", scheme: "aptos", color: "#231F20", family: "aptos", units: [ { name: "APT", code: "APT", magnitude: 8, }, ], explorerViews: [ { address: "https://explorer.aptoslabs.com/account/$address?network=mainnet", tx: "https://explorer.aptoslabs.com/txn/$hash?network=mainnet", }, ], }, name: "dstAPT", tokenType: "coin", ticker: "dstAPT", disableCountervalue: false, delisted: false, units: [ { name: "dstAPT", code: "dstAPT", magnitude: 8, }, ], }, balance: (0, bignumber_js_1.default)(100), spendableBalance: (0, bignumber_js_1.default)(100), creationDate: date, operationsCount: 0, operations: [], pendingOperations: [], balanceHistoryCache: index_1.emptyHistoryCache, swapHistory: [], }, ], }); const transaction = (0, bridge_fixture_1.createFixtureTransaction)({ amount: (0, bignumber_js_1.default)(5), subAccountId: "subAccountId", }); account.balance = new bignumber_js_1.default(40); transaction.fees = new bignumber_js_1.default(30); transaction.useAllAmount = true; account.id = "js:2:aptos:0x000:"; transaction.mode = "send"; const observable = await (0, signOperation_1.default)({})({ account, deviceId: "1", transaction, }); expect(observable).toBeInstanceOf(rxjs_1.Observable); const expectedValues = [ { type: "device-signature-requested" }, { type: "device-signature-granted" }, { type: "signed", signedOperation: { operation: { id: "js:2:aptos:0x000:--OUT", hash: "", type: "OUT", value: new bignumber_js_1.default(30), fee: transaction.fees, extra: {}, blockHash: null, blockHeight: null, senders: [account.freshAddress], recipients: [transaction.recipient], accountId: "js:2:aptos:0x000:", date, transactionSequenceNumber: 789, subOperations: [ { accountId: "subAccountId", date: new Date("2020-01-01"), fee: (0, bignumber_js_1.default)(30), id: "subAccountId--OUT", recipients: ["recipient"], senders: ["address"], type: "OUT", value: (0, bignumber_js_1.default)(5), }, ], }, signature: "7369676e65645478", }, }, ]; let i = 0; observable.forEach(signOperationEvent => { expect(signOperationEvent).toEqual(expectedValues[i]); i++; }); }); }); //# sourceMappingURL=signOperation.test.js.map