UNPKG

@ledgerhq/coin-tezos

Version:
283 lines • 11.4 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 taquito_1 = require("@taquito/taquito"); const signOperation_1 = __importStar(require("./signOperation")); const config_1 = __importDefault(require("../config")); const bridge_fixture_1 = require("../types/bridge.fixture"); const config_2 = require("../test/config"); const mockForgeOperations = jest.fn(); const mockEstimateReveal = jest.fn(); jest.mock("@taquito/taquito", () => ({ ...jest.requireActual("@taquito/taquito"), TezosToolkit: jest.fn().mockReturnValue({ setProvider: jest.fn(), rpc: { getBlock: jest.fn().mockResolvedValue({ hash: "hash" }), getContract: jest.fn().mockResolvedValue({ counter: "12" }), forgeOperations: () => mockForgeOperations(), }, estimate: { reveal: () => mockEstimateReveal(), }, }), })); describe("signOperation", () => { const mockSign = jest.fn().mockResolvedValue({ bytes: "SIG_BYTES", sig: "SIG", prefixSig: "PREFIX_SIG", sbytes: "03SBYTES", }); const fakeSigner = { getAddress: jest.fn(), signOperation: jest.fn(), createLedgerSigner: () => ({ publicKey: () => Promise.resolve("PUBKEY"), publicKeyHash: () => Promise.resolve("PUBKEYHASH"), sign: (arg) => mockSign(arg), secretKey: () => Promise.resolve(undefined), }), }; const signerContext = (_deviceId, fn) => fn(fakeSigner); const signOperation = (0, signOperation_1.default)(signerContext); const deviceId = "dummyDeviceId"; beforeAll(() => { config_1.default.setCoinConfig(() => config_2.mockConfig); }); afterEach(() => { mockForgeOperations.mockClear(); mockEstimateReveal.mockClear(); mockSign.mockClear(); }); it("returns events in the right order", done => { // GIVEN const account = (0, bridge_fixture_1.createFixtureAccount)(); const transaction = (0, bridge_fixture_1.createFixtureTransaction)({ fees: (0, bignumber_js_1.default)(0) }); mockForgeOperations.mockResolvedValue("FORGED_OP"); // WHEN & THEN const expectedEvent = [ { type: "device-signature-requested", }, { type: "device-signature-granted", }, { type: "signed", }, ]; let eventIdx = 0; signOperation({ account, deviceId, transaction }).forEach(e => { try { expect(e.type).toEqual(expectedEvent[eventIdx].type); eventIdx++; if (eventIdx === expectedEvent.length) { done(); } } catch (err) { done(err); } }); }); it("returns signature value from LedgerSigner", done => { // GIVEN const account = (0, bridge_fixture_1.createFixtureAccount)(); const transaction = (0, bridge_fixture_1.createFixtureTransaction)({ fees: new bignumber_js_1.default(0) }); mockForgeOperations.mockResolvedValue("f0463d"); // WHEN & THEN const subscriber = signOperation({ account, deviceId, transaction }).subscribe((e) => { if (e.type === "signed") { const signature = e.signedOperation.signature; expect(signature).toEqual("SBYTES"); expect(mockForgeOperations).toHaveBeenCalledTimes(1); expect(mockSign).toHaveBeenCalledTimes(1); expect(mockSign.mock.lastCall[0]).toEqual(Buffer.concat([Buffer.from("03", "hex"), Buffer.from("f0463d", "hex")]).toString("hex")); // Cleanup subscriber.unsubscribe(); done(); } }); }); }); describe("getOperationContents - revealed account", () => { const account = (0, bridge_fixture_1.createFixtureAccount)({ freshAddress: "tz1addr" }); it("mode - send", async () => { const transaction = (0, bridge_fixture_1.createFixtureTransaction)({ family: "tezos", amount: new bignumber_js_1.default(0), recipient: "RECIPIENT_ADD", fees: new bignumber_js_1.default(0), mode: "send", gasLimit: new bignumber_js_1.default(0), storageLimit: new bignumber_js_1.default(0), }); const { type, contents } = await (0, signOperation_1.getOperationContents)({ account, transaction, counter: 0, public_key: "pk", public_key_hash: "pkh", }); expect(type).toBe("OUT"); expect(contents.length).toEqual(1); expect(contents).toStrictEqual([ { kind: taquito_1.OpKind.TRANSACTION, amount: transaction.amount.toString(), destination: transaction.recipient, source: "tz1addr", counter: "1", fee: new bignumber_js_1.default(0).toString(), gas_limit: new bignumber_js_1.default(0).toString(), storage_limit: new bignumber_js_1.default(0).toString(), }, ]); expect(mockEstimateReveal).not.toHaveBeenCalled(); }); it("mode - delegate", async () => { const transaction = (0, bridge_fixture_1.createFixtureTransaction)({ family: "tezos", amount: new bignumber_js_1.default(0), recipient: "RECIPIENT_ADD", fees: new bignumber_js_1.default(0), mode: "delegate", gasLimit: new bignumber_js_1.default(0), storageLimit: new bignumber_js_1.default(0), }); const { type, contents } = await (0, signOperation_1.getOperationContents)({ account, transaction, counter: 0, public_key: "pk", public_key_hash: "pkh", }); expect(type).toBe("DELEGATE"); expect(contents.length).toEqual(1); expect(contents).toStrictEqual([ { kind: taquito_1.OpKind.DELEGATION, source: "tz1addr", counter: "1", fee: new bignumber_js_1.default(0).toString(), gas_limit: new bignumber_js_1.default(0).toString(), storage_limit: new bignumber_js_1.default(0).toString(), delegate: transaction.recipient, }, ]); expect(mockEstimateReveal).not.toHaveBeenCalled(); }); it("mode - undelegate", async () => { const transaction = (0, bridge_fixture_1.createFixtureTransaction)({ family: "tezos", amount: new bignumber_js_1.default(0), recipient: "", fees: new bignumber_js_1.default(0), mode: "undelegate", gasLimit: new bignumber_js_1.default(0), storageLimit: new bignumber_js_1.default(0), }); const { type, contents } = await (0, signOperation_1.getOperationContents)({ account, transaction, counter: 0, public_key: "pk", public_key_hash: "pkh", }); expect(type).toBe("UNDELEGATE"); expect(contents.length).toEqual(1); expect(contents).toStrictEqual([ { kind: taquito_1.OpKind.DELEGATION, source: "tz1addr", counter: "1", fee: new bignumber_js_1.default(0).toString(), gas_limit: new bignumber_js_1.default(0).toString(), storage_limit: new bignumber_js_1.default(0).toString(), }, ]); expect(mockEstimateReveal).not.toHaveBeenCalled(); }); describe("getOperationContents - not revealed account", () => { const account = (0, bridge_fixture_1.createFixtureAccount)({ freshAddress: "tz1addr", // We don't use `counter` in the OperationContents computation tezosResources: { revealed: false, counter: -10 }, }); it("mode - send", async () => { // Given mockEstimateReveal.mockResolvedValue({ gasLimit: 100, storageLimit: 200, }); const transaction = (0, bridge_fixture_1.createFixtureTransaction)({ family: "tezos", amount: new bignumber_js_1.default(0), recipient: "RECIPIENT_ADD", fees: new bignumber_js_1.default(0), mode: "send", gasLimit: new bignumber_js_1.default(0), storageLimit: new bignumber_js_1.default(0), }); const { type, contents } = await (0, signOperation_1.getOperationContents)({ account, transaction, counter: 0, public_key: "pk", public_key_hash: "pkh", }); expect(type).toBe("OUT"); expect(contents.length).toEqual(2); expect(contents).toStrictEqual([ { kind: taquito_1.OpKind.REVEAL, fee: taquito_1.DEFAULT_FEE.REVEAL.toString(), gas_limit: "100", storage_limit: "200", source: "pkh", counter: "1", public_key: "pk", }, { kind: taquito_1.OpKind.TRANSACTION, amount: transaction.amount.toString(), destination: transaction.recipient, source: "tz1addr", counter: "2", fee: new bignumber_js_1.default(0).toString(), gas_limit: new bignumber_js_1.default(0).toString(), storage_limit: new bignumber_js_1.default(0).toString(), }, ]); expect(mockEstimateReveal).toHaveBeenCalledTimes(1); }); }); }); //# sourceMappingURL=signOperation.test.js.map