Skip to content

Commit

Permalink
feat: add dependencies support on wallet-api and dapp browser for tra…
Browse files Browse the repository at this point in the history
…nsaction

Also bump the wallet-api to add dependencies option
  • Loading branch information
Justkant committed Aug 20, 2024
1 parent d75616a commit 940d807
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 61 deletions.
7 changes: 7 additions & 0 deletions .changeset/brave-laws-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"ledger-live-desktop": minor
"live-mobile": minor
"@ledgerhq/live-common": minor
---

feat: add dependencies support on wallet-api and dapp browser for transaction
5 changes: 5 additions & 0 deletions .changeset/mighty-pillows-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/wallet-api-exchange-module": patch
---

chore: bump wallet-api deps
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function useUiHook(manifest: AppManifest, tracking: Record<string, TrackFunction
stepId: canEditFees && !hasFeesProvided ? "amount" : "summary",
transactionData: liveTx,
useApp: options?.hwAppId,
dependencies: options?.dependencies,
account,
parentAccount,
onResult: onSuccess,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type Params = {
canEditFees: boolean;
stepId?: StepId;
useApp?: string;
dependencies?: string[];
account: AccountLike;
transactionData: Partial<Transaction>;
onResult: (signedOperation: SignedOperation) => void;
Expand Down Expand Up @@ -223,6 +224,7 @@ export default function Body({ onChangeStepId, onClose, setError, stepId, params
manifestId: params.manifestId,
manifestName: params.manifestName,
useApp: params.useApp,
dependencies: params.dependencies,
steps,
errorSteps,
device,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
import React from "react";
import React, { useMemo } from "react";
import TrackPage from "~/renderer/analytics/TrackPage";
import { getMainAccount } from "@ledgerhq/live-common/account/index";
import GenericStepConnectDevice from "./GenericStepConnectDevice";
import { StepProps } from "../types";

function dependenciesToAppRequests(dependencies?: string[]) {
if (!dependencies) {
return;
}

return dependencies.map(appName => ({ appName }));
}

export default function StepConnectDevice({
account,
parentAccount,
transaction,
status,
transitionTo,
useApp,
dependencies,
onTransactionError,
onTransactionSigned,
manifestId,
manifestName,
}: StepProps) {
// Nb setting the mainAccount as a dependency will ensure latest versions of plugins.
const dependencies = (account && [getMainAccount(account, parentAccount)]) || [];
const connectDependencies = useMemo(() => {
const appRequests = dependenciesToAppRequests(dependencies);

if (account) {
// Nb setting the mainAccount as a dependency will ensure latest versions of plugins.
return [
{ currency: getMainAccount(account, parentAccount).currency },
...(appRequests || []),
];
}
return appRequests;
}, [account, dependencies, parentAccount]);

return (
<>
<TrackPage category="Sign Transaction Flow" name="Step ConnectDevice" />
Expand All @@ -29,7 +50,7 @@ export default function StepConnectDevice({
transitionTo={transitionTo}
onTransactionError={onTransactionError}
onTransactionSigned={onTransactionSigned}
dependencies={dependencies}
dependencies={connectDependencies}
manifestId={manifestId}
manifestName={manifestName}
requireLatestFirmware
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type StepProps = {
transitionTo: (a: string) => void;
openedFromAccount: boolean;
useApp?: string;
dependencies?: string[];
device: Device | undefined | null;
account: AccountLike | undefined | null;
parentAccount: Account | undefined | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type SignTransactionNavigatorParamList = {
overrideAmountLabel?: string;
hideTotal?: boolean;
appName?: string;
dependencies?: string[];
currentNavigation:
| ScreenName.SignTransactionSummary
| ScreenName.SendSummary
Expand All @@ -69,6 +70,7 @@ export type SignTransactionNavigatorParamList = {
transaction: Transaction;
status: TransactionStatus;
appName?: string;
dependencies?: string[];
onSuccess: (payload: unknown) => void;
onError: (_: Error) => void;
analyticsPropertyFlow?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ function useUiHook(): UiHook {
accountId: account.id,
parentId: parentAccount ? parentAccount.id : undefined,
appName: options?.hwAppId,
dependencies: options?.dependencies,
onSuccess: ({
signedOperation,
transactionSignError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import { ScreenName } from "~/const";
import { StackNavigatorProps } from "~/components/RootNavigator/types/helpers";
import { useTransactionDeviceAction } from "~/hooks/deviceActions";

function dependenciesToAppRequests(dependencies?: string[]) {
if (!dependencies) {
return [];
}

return dependencies.map(appName => ({ appName }));
}

function ConnectDevice({
navigation,
route,
Expand All @@ -27,7 +35,7 @@ function ConnectDevice({
const { colors } = useTheme();
const { account, parentAccount } = useSelector(accountScreenSelector(route));
invariant(account, "account is required");
const { appName, onSuccess } = route.params;
const { appName, dependencies, onSuccess } = route.params;
const mainAccount = getMainAccount(account, parentAccount);
const { transaction, status } = useBridgeTransaction(() => ({
account: mainAccount,
Expand All @@ -46,10 +54,22 @@ function ConnectDevice({
transaction,
status,
tokenCurrency,
dependencies: [mainAccount],
dependencies: [
{ currency: mainAccount.currency },
...dependenciesToAppRequests(dependencies),
],
requireLatestFirmware: true,
}),
[account, appName, mainAccount, parentAccount, status, tokenCurrency, transaction],
[
account,
appName,
dependencies,
mainAccount,
parentAccount,
status,
tokenCurrency,
transaction,
],
);
return transaction ? (
<SafeAreaView
Expand Down
4 changes: 2 additions & 2 deletions libs/exchange-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"lint:fix": "pnpm lint --fix"
},
"dependencies": {
"@ledgerhq/wallet-api-client": "^1.5.9",
"@ledgerhq/wallet-api-core": "^1.10.1"
"@ledgerhq/wallet-api-client": "^1.5.10",
"@ledgerhq/wallet-api-core": "^1.11.0"
},
"devDependencies": {
"@types/node": "^20.2.5"
Expand Down
8 changes: 4 additions & 4 deletions libs/ledger-live-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@
"@ledgerhq/live-wallet": "workspace:^",
"@ledgerhq/logs": "workspace:^",
"@ledgerhq/speculos-transport": "workspace:^",
"@ledgerhq/wallet-api-client": "^1.5.9",
"@ledgerhq/wallet-api-core": "^1.10.1",
"@ledgerhq/wallet-api-client": "^1.5.10",
"@ledgerhq/wallet-api-core": "^1.11.0",
"@ledgerhq/wallet-api-exchange-module": "workspace:^",
"@ledgerhq/wallet-api-server": "^1.5.10",
"@ledgerhq/wallet-api-server": "^1.5.11",
"@stacks/network": "6.10.0",
"@stacks/transactions": "6.11.0",
"@stricahq/typhonjs": "^2.0.0",
Expand Down Expand Up @@ -301,4 +301,4 @@
"uuid": "^8.3.2",
"ws": "7"
}
}
}
18 changes: 12 additions & 6 deletions libs/ledger-live-common/src/hw/actions/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,17 @@ const reducer = (state: State, e: Event): State => {
* specify an account or a currency without resolving manually the actual
* applications we depend on in order to access the flow.
*/
function inferCommandParams(appRequest: AppRequest) {
function inferCommandParams(appRequest: AppRequest): ConnectAppRequest {
let derivationMode;
let derivationPath;

const { account, requireLatestFirmware, allowPartialDependencies } = appRequest;
let { appName, currency, dependencies } = appRequest;
const {
account,
requireLatestFirmware,
allowPartialDependencies = false,
dependencies: appDependencies,
} = appRequest;
let { appName, currency } = appRequest;

if (!currency && account) {
currency = account.currency;
Expand All @@ -388,8 +393,9 @@ function inferCommandParams(appRequest: AppRequest) {

invariant(appName, "appName or currency or account is missing");

if (dependencies) {
dependencies = dependencies.map(d => inferCommandParams(d).appName);
let dependencies: string[] | undefined = undefined;
if (appDependencies) {
dependencies = appDependencies.map(d => inferCommandParams(d).appName);
}

if (!currency) {
Expand Down Expand Up @@ -450,7 +456,7 @@ export const createAction = (
const firmwareResolvedRef = useRef(false);
const outdatedAppRef = useRef<AppAndVersion>();

const request: ConnectAppRequest = useMemo(
const request = useMemo(
() => inferCommandParams(appRequest), // for now i don't have better
// eslint-disable-next-line react-hooks/exhaustive-deps
[
Expand Down
1 change: 1 addition & 0 deletions libs/ledger-live-common/src/platform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export type LiveAppManifestDapp = {
provider: DappProviders;
networks: Array<LiveAppManifestParamsNetwork>;
nanoApp: string;
dependencies?: string[];
};

export type LiveAppManifest = {
Expand Down
5 changes: 4 additions & 1 deletion libs/ledger-live-common/src/wallet-api/useDappLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export function useDappLogic({
currentAccountHistDb?: CurrentAccountHistDB;
}) {
const nanoApp = manifest.dapp?.nanoApp;
const dependencies = manifest.dapp?.dependencies;
const ws = useRef<SmartWebsocket>();
const { currentAccount, currentParentAccount, setCurrentAccountHist } = useDappAccountLogic({
manifest,
Expand Down Expand Up @@ -427,7 +428,9 @@ export function useDappLogic({

if (address.toLowerCase() === ethTX.from.toLowerCase()) {
try {
const options = nanoApp ? { hwAppId: nanoApp } : undefined;
const options = nanoApp
? { hwAppId: nanoApp, dependencies: dependencies }
: undefined;
tracking.dappSendTransactionRequested(manifest);

const signFlowInfos = getWalletAPITransactionSignFlowInfos({
Expand Down
Loading

0 comments on commit 940d807

Please sign in to comment.