Skip to content

Commit

Permalink
chore: feedbacks
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr>
  • Loading branch information
sprohaszka-ledger committed May 14, 2024
1 parent 6663991 commit db91ef9
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 84 deletions.
7 changes: 7 additions & 0 deletions libs/coin-modules/coin-cardano/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"lib-es/*": [
"lib-es/*"
],
"bridge": [
"lib/bridge/index"
],
"*": [
"lib/*"
]
Expand All @@ -36,6 +39,10 @@
"exports": {
"./lib/*": "./lib/*.js",
"./lib-es/*": "./lib-es/*.js",
"./bridge": {
"require": "./lib/bridge/index.js",
"default": "./lib-es/bridge/index.js"
},
"./*": {
"require": "./lib/*.js",
"default": "./lib-es/*.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,11 @@ import buildSignOperation from "../js-signOperation";
import broadcast from "../js-broadcast";
import type { AccountBridge, CurrencyBridge } from "@ledgerhq/types-live";
import { assignToAccountRaw, assignFromAccountRaw } from "../serialization";
import {
CardanoAddress,
CardanoExtendedPublicKey,
CardanoSignature,
CardanoSigner,
} from "../signer";
import { CardanoSigner } from "../signer";
import resolver from "../hw-getAddress";
import postSyncPatch from "../postSyncPatch";

export function buildCurrencyBridge(
signerContext: SignerContext<
CardanoSigner,
CardanoAddress | CardanoExtendedPublicKey | CardanoSignature
>,
): CurrencyBridge {
export function buildCurrencyBridge(signerContext: SignerContext<CardanoSigner>): CurrencyBridge {
const getAddress = resolver(signerContext);
const scanAccounts = makeScanAccounts({
getAccountShape: makeGetAccountShape(signerContext),
Expand All @@ -44,10 +34,7 @@ export function buildCurrencyBridge(
}

export function buildAccountBridge(
signerContext: SignerContext<
CardanoSigner,
CardanoAddress | CardanoExtendedPublicKey | CardanoSignature
>,
signerContext: SignerContext<CardanoSigner>,
): AccountBridge<Transaction> {
const sync = makeSync({
getAccountShape: makeGetAccountShape(signerContext),
Expand All @@ -73,12 +60,7 @@ export function buildAccountBridge(
};
}

export function createBridges(
signerContext: SignerContext<
CardanoSigner,
CardanoAddress | CardanoExtendedPublicKey | CardanoSignature
>,
) {
export function createBridges(signerContext: SignerContext<CardanoSigner>) {
return {
currencyBridge: buildCurrencyBridge(signerContext),
accountBridge: buildAccountBridge(signerContext),
Expand Down
18 changes: 4 additions & 14 deletions libs/coin-modules/coin-cardano/src/hw-getAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,9 @@ import { getBipPathFromString, getBipPathString } from "./logic";
import { StakeChain } from "./types";
import { STAKING_ADDRESS_INDEX } from "./constants";
import { getNetworkParameters } from "./networks";
import {
CardanoAddress,
CardanoExtendedPublicKey,
CardanoSignature,
CardanoSigner,
} from "./signer";
import { CardanoSigner } from "./signer";

const resolver = (
signerContext: SignerContext<
CardanoSigner,
CardanoAddress | CardanoExtendedPublicKey | CardanoSignature
>,
): GetAddressFn => {
const resolver = (signerContext: SignerContext<CardanoSigner>): GetAddressFn => {
return async (deviceId: string, { path, verify, currency }: GetAddressOptions) => {
const spendingPath = getBipPathFromString(path);
const stakingPathString = getBipPathString({
Expand All @@ -27,9 +17,9 @@ const resolver = (
});
const networkParams = getNetworkParameters(currency.id);

const r = (await signerContext(deviceId, signer =>
const r = await signerContext(deviceId, signer =>
signer.getAddress({ path, stakingPathString, networkParams, verify }),
)) as CardanoAddress;
);
return {
address: r.address,
publicKey: r.publicKey,
Expand Down
18 changes: 4 additions & 14 deletions libs/coin-modules/coin-cardano/src/js-signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ import { OperationType, SignOperationEvent, SignOperationFnSignature } from "@le
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
import { HashType } from "@stricahq/typhonjs/dist/types";
import { SignerContext } from "@ledgerhq/coin-framework/signer";
import {
CardanoAddress,
CardanoExtendedPublicKey,
CardanoSignature,
CardanoSigner,
} from "./signer";
import { CardanoSigner } from "./signer";

const buildOptimisticOperation = (
account: CardanoAccount,
Expand Down Expand Up @@ -195,12 +190,7 @@ const buildOptimisticOperation = (
* Sign Transaction with Ledger hardware
*/
const buildSignOperation =
(
signerContext: SignerContext<
CardanoSigner,
CardanoAddress | CardanoExtendedPublicKey | CardanoSignature
>,
): SignOperationFnSignature<Transaction> =>
(signerContext: SignerContext<CardanoSigner>): SignOperationFnSignature<Transaction> =>
({ account, deviceId, transaction }): Observable<SignOperationEvent> =>
new Observable(o => {
async function main() {
Expand All @@ -215,14 +205,14 @@ const buildSignOperation =
const accountPubKey = getExtendedPublicKeyFromHex(account.xpub as string);
const networkParams = getNetworkParameters(account.currency.id);

const signed = (await signerContext(deviceId, signer =>
const signed = await signerContext(deviceId, signer =>
signer.sign({
unsignedTransaction,
accountPubKey,
accountIndex: account.index,
networkParams,
}),
)) as CardanoSignature;
);

o.next({ type: "device-signature-granted" });

Expand Down
24 changes: 6 additions & 18 deletions libs/coin-modules/coin-cardano/src/js-synchronisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import uniqBy from "lodash/uniqBy";
import { utils as TyphonUtils } from "@stricahq/typhonjs";
import { calculateMinUtxoAmount } from "@stricahq/typhonjs/dist/utils/utils";
import type { Operation, OperationType, TokenAccount } from "@ledgerhq/types-live";
import { listTokensForCryptoCurrency } from "@ledgerhq/cryptoassets";
import {
AccountShapeInfo,
GetAccountShape,
Expand All @@ -12,10 +13,7 @@ import { encodeAccountId } from "@ledgerhq/coin-framework/account/index";
import { inferSubOperations } from "@ledgerhq/coin-framework/serialization/index";
import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
import { SignerContext } from "@ledgerhq/coin-framework/signer";
import {
formatCurrencyUnit,
listTokensForCryptoCurrency,
} from "@ledgerhq/coin-framework/currencies/index";
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
import { APITransaction, HashType } from "./api/api-types";
import {
CardanoAccount,
Expand All @@ -41,12 +39,7 @@ import { getNetworkInfo } from "./api/getNetworkInfo";
import { getTransactions } from "./api/getTransactions";
import { buildSubAccounts } from "./buildSubAccounts";
import { getDelegationInfo } from "./api/getDelegationInfo";
import {
CardanoAddress,
CardanoExtendedPublicKey,
CardanoSignature,
CardanoSigner,
} from "./signer";
import { CardanoSigner } from "./signer";

function mapTxToAccountOperation(
tx: APITransaction,
Expand Down Expand Up @@ -199,12 +192,7 @@ function prepareUtxos(
}

export const makeGetAccountShape =
(
signerContext: SignerContext<
CardanoSigner,
CardanoAddress | CardanoExtendedPublicKey | CardanoSignature
>,
): GetAccountShape =>
(signerContext: SignerContext<CardanoSigner>): GetAccountShape =>
async (info, { blacklistedTokenIds }) => {
const {
currency,
Expand All @@ -225,9 +213,9 @@ export const makeGetAccountShape =
// deviceId not provided
throw new Error("deviceId required to generate the xpub");
}
const extendedPubKeyRes = (await signerContext(deviceId, signer =>
const extendedPubKeyRes = await signerContext(deviceId, signer =>
signer.getPublicKey(accountPath),
)) as CardanoExtendedPublicKey;
);
xpub = `${extendedPubKeyRes.publicKeyHex}${extendedPubKeyRes.chainCodeHex}`;
} else {
xpub = paramXpub;
Expand Down
22 changes: 9 additions & 13 deletions libs/coin-modules/coin-cardano/src/js-synchronisation.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,31 @@ import { getTransactions } from "./api/getTransactions";
import { buildSubAccounts } from "./buildSubAccounts";
import { makeGetAccountShape } from "./js-synchronisation";
import { BipPath, CardanoAccount, CardanoDelegation, PaymentCredential } from "./types";
import { SignerContext } from "@ledgerhq/coin-framework/signer";
import {
CardanoAddress,
CardanoExtendedPublicKey,
CardanoSignature,
CardanoSigner,
} from "./signer";
import type { SignerContext } from "@ledgerhq/coin-framework/signer";
import { CardanoSigner } from "./signer";

jest.mock("./buildSubAccounts");
jest.mock("./api/getTransactions");
jest.mock("./api/getNetworkInfo");
jest.mock("./api/getDelegationInfo");

describe("makeGetAccountShape", () => {
let signerContext: SignerContext<
CardanoSigner,
CardanoAddress | CardanoExtendedPublicKey | CardanoSignature
>;
let signerContext: SignerContext<CardanoSigner>;
let shape: GetAccountShape;
let accountShapeInfo: AccountShapeInfo;
let getTransactionsMock: jest.MaybeMockedDeep<typeof getTransactions>;

beforeEach(() => {
const pubKeyMock = {
extendedPubKeyHex: "extendedPubKeyHex",
chainCodeHex: "chainCodeHex",
publicKeyHex: "publicKeyHex",
};
signerContext = () => Promise.resolve(pubKeyMock);
const fakeSigner: CardanoSigner = {
getAddress: jest.fn(),
sign: jest.fn(),
getPublicKey: jest.fn().mockResolvedValue(pubKeyMock),
};
signerContext = <T>(_: string, fn: (signer: CardanoSigner) => Promise<T>) => fn(fakeSigner);
shape = makeGetAccountShape(signerContext);
accountShapeInfo = {
currency: { id: "cardano" } as any,
Expand Down
4 changes: 2 additions & 2 deletions libs/coin-modules/coin-cardano/src/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ export function getBipPathFromString(path: string): BipPath {
* @returns true if the account can stake, false otherwise
*/
export function canStake(account: CardanoAccount): boolean {
return !!account.balance?.gt(0);
return account.balance.gt(0);
}

/**
*
* @returns true if account is staked, false otherwise
*/
export function isAlreadyStaking(account: CardanoAccount): boolean {
return !!account?.cardanoResources?.delegation?.poolId;
return !!account.cardanoResources?.delegation?.poolId;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion libs/ledger-live-common/src/families/cardano/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { utils as TyphonUtils } from "@stricahq/typhonjs";
import { address as TyphonAddress } from "@stricahq/typhonjs";
import Transport from "@ledgerhq/hw-transport";
import type { Bridge } from "@ledgerhq/types-live";
import { createBridges } from "@ledgerhq/coin-cardano/bridge/js";
import { createBridges } from "@ledgerhq/coin-cardano/bridge";
import makeCliTools from "@ledgerhq/coin-cardano/cli-transaction";
import cardanoResolver from "@ledgerhq/coin-cardano/hw-getAddress";
import type { Transaction } from "@ledgerhq/coin-cardano/types";
Expand Down

0 comments on commit db91ef9

Please sign in to comment.