-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: bootstrap wallet-api custom ACRE module and handlers [LIVE-13948]
- Loading branch information
Showing
26 changed files
with
916 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"ledger-live-desktop": minor | ||
"live-mobile": minor | ||
"@ledgerhq/live-common": minor | ||
"@ledgerhq/wallet-api-acre-module": minor | ||
--- | ||
|
||
feat: bootstrap wallet-api custom ACRE module and handlers |
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
apps/ledger-live-desktop/src/renderer/components/WebPlatformPlayer/CustomHandlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { useMemo } from "react"; | ||
import { ipcRenderer } from "electron"; | ||
import { useTranslation } from "react-i18next"; | ||
import { useDispatch } from "react-redux"; | ||
import { AccountLike } from "@ledgerhq/types-live"; | ||
import { useToasts } from "@ledgerhq/live-common/notifications/ToastProvider/index"; | ||
import { addPendingOperation } from "@ledgerhq/live-common/account/index"; | ||
import { WalletAPICustomHandlers } from "@ledgerhq/live-common/wallet-api/types"; | ||
import { handlers as acreHandlers } from "@ledgerhq/live-common/wallet-api/ACRE/server"; | ||
import trackingWrapper from "@ledgerhq/live-common/wallet-api/ACRE/tracking"; | ||
import { track } from "~/renderer/analytics/segment"; | ||
import { openModal } from "~/renderer/actions/modals"; | ||
import { setDrawer } from "~/renderer/drawers/Provider"; | ||
import { OperationDetails } from "~/renderer/drawers/OperationDetails"; | ||
import { currentRouteNameRef } from "~/renderer/analytics/screenRefs"; | ||
import { updateAccountWithUpdater } from "~/renderer/actions/accounts"; | ||
import { WebviewProps } from "../Web3AppWebview/types"; | ||
|
||
export function useACRECustomHandlers(manifest: WebviewProps["manifest"], accounts: AccountLike[]) { | ||
const { pushToast } = useToasts(); | ||
const { t } = useTranslation(); | ||
const dispatch = useDispatch(); | ||
|
||
const tracking = useMemo( | ||
() => | ||
trackingWrapper( | ||
( | ||
eventName: string, | ||
properties?: Record<string, unknown> | null, | ||
mandatory?: boolean | null, | ||
) => | ||
track( | ||
eventName, | ||
{ | ||
...properties, | ||
flowInitiatedFrom: | ||
currentRouteNameRef.current === "Platform Catalog" | ||
? "Discover" | ||
: currentRouteNameRef.current, | ||
}, | ||
mandatory, | ||
), | ||
), | ||
[], | ||
); | ||
|
||
return useMemo<WalletAPICustomHandlers>(() => { | ||
return { | ||
...acreHandlers({ | ||
accounts, | ||
tracking, | ||
manifest, | ||
uiHooks: { | ||
"custom.acre.messageSign": ({ account, message, onSuccess, onError, onCancel }) => { | ||
ipcRenderer.send("show-app", {}); | ||
dispatch( | ||
openModal("MODAL_SIGN_MESSAGE", { | ||
account, | ||
message, | ||
onConfirmationHandler: onSuccess, | ||
onFailHandler: onError, | ||
onClose: onCancel, | ||
}), | ||
); | ||
}, | ||
"custom.acre.transactionSign": ({ | ||
account, | ||
parentAccount, | ||
signFlowInfos: { canEditFees, hasFeesProvided, liveTx }, | ||
options, | ||
onSuccess, | ||
onError, | ||
}) => { | ||
ipcRenderer.send("show-app", {}); | ||
dispatch( | ||
openModal("MODAL_SIGN_TRANSACTION", { | ||
canEditFees, | ||
stepId: canEditFees && !hasFeesProvided ? "amount" : "summary", | ||
transactionData: liveTx, | ||
useApp: options?.hwAppId, | ||
dependencies: options?.dependencies, | ||
account, | ||
parentAccount, | ||
onResult: onSuccess, | ||
onCancel: onError, | ||
manifestId: manifest.id, | ||
manifestName: manifest.name, | ||
}), | ||
); | ||
}, | ||
"custom.acre.transactionBroadcast": ( | ||
account, | ||
parentAccount, | ||
mainAccount, | ||
optimisticOperation, | ||
) => { | ||
dispatch( | ||
updateAccountWithUpdater(mainAccount.id, account => | ||
addPendingOperation(account, optimisticOperation), | ||
), | ||
); | ||
|
||
pushToast({ | ||
id: optimisticOperation.id, | ||
type: "operation", | ||
title: t("platform.flows.broadcast.toast.title"), | ||
text: t("platform.flows.broadcast.toast.text"), | ||
icon: "info", | ||
callback: () => { | ||
tracking.broadcastOperationDetailsClick(manifest); | ||
setDrawer(OperationDetails, { | ||
operationId: optimisticOperation.id, | ||
accountId: account.id, | ||
parentId: parentAccount?.id as string | undefined | null, | ||
}); | ||
}, | ||
}); | ||
}, | ||
}, | ||
}), | ||
}; | ||
}, [accounts, tracking, manifest, dispatch, pushToast, t]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
apps/ledger-live-mobile/src/components/WebPlatformPlayer/CustomHandlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { useMemo } from "react"; | ||
import { useNavigation } from "@react-navigation/native"; | ||
import { AccountLike, SignedOperation } from "@ledgerhq/types-live"; | ||
import type { Transaction } from "@ledgerhq/live-common/generated/types"; | ||
import { WalletAPICustomHandlers } from "@ledgerhq/live-common/wallet-api/types"; | ||
import { handlers as acreHandlers } from "@ledgerhq/live-common/wallet-api/ACRE/server"; | ||
import trackingWrapper from "@ledgerhq/live-common/wallet-api/ACRE/tracking"; | ||
import { track } from "~/analytics"; | ||
import { NavigatorName, ScreenName } from "~/const"; | ||
import { currentRouteNameRef } from "~/analytics/screenRefs"; | ||
import { StackNavigatorNavigation } from "../RootNavigator/types/helpers"; | ||
import { BaseNavigatorStackParamList } from "../RootNavigator/types/BaseNavigator"; | ||
import { WebviewProps } from "../Web3AppWebview/types"; | ||
import prepareSignTransaction from "../Web3AppWebview/liveSDKLogic"; | ||
|
||
export function useACRECustomHandlers(manifest: WebviewProps["manifest"], accounts: AccountLike[]) { | ||
const navigation = useNavigation<StackNavigatorNavigation<BaseNavigatorStackParamList>>(); | ||
|
||
const tracking = useMemo( | ||
() => | ||
trackingWrapper((eventName: string, properties?: Record<string, unknown> | null) => | ||
track(eventName, { | ||
...properties, | ||
flowInitiatedFrom: | ||
currentRouteNameRef.current === "Platform Catalog" | ||
? "Discover" | ||
: currentRouteNameRef.current, | ||
}), | ||
), | ||
[], | ||
); | ||
|
||
return useMemo<WalletAPICustomHandlers>(() => { | ||
return { | ||
...acreHandlers({ | ||
accounts, | ||
tracking, | ||
manifest, | ||
uiHooks: { | ||
"custom.acre.messageSign": ({ account, message, onSuccess, onError, onCancel }) => { | ||
navigation.navigate(NavigatorName.SignMessage, { | ||
screen: ScreenName.SignSummary, | ||
params: { | ||
message, | ||
accountId: account.id, | ||
onConfirmationHandler: onSuccess, | ||
onFailHandler: onError, | ||
}, | ||
onClose: onCancel, | ||
}); | ||
}, | ||
"custom.acre.transactionSign": ({ | ||
account, | ||
parentAccount, | ||
signFlowInfos: { liveTx }, | ||
options, | ||
onSuccess, | ||
onError, | ||
}) => { | ||
const tx = prepareSignTransaction(account, parentAccount, liveTx); | ||
|
||
navigation.navigate(NavigatorName.SignTransaction, { | ||
screen: ScreenName.SignTransactionSummary, | ||
params: { | ||
currentNavigation: ScreenName.SignTransactionSummary, | ||
nextNavigation: ScreenName.SignTransactionSelectDevice, | ||
transaction: tx as Transaction, | ||
accountId: account.id, | ||
parentId: parentAccount ? parentAccount.id : undefined, | ||
appName: options?.hwAppId, | ||
dependencies: options?.dependencies, | ||
onSuccess: ({ | ||
signedOperation, | ||
transactionSignError, | ||
}: { | ||
signedOperation: SignedOperation; | ||
transactionSignError: Error; | ||
}) => { | ||
if (transactionSignError) { | ||
onError(transactionSignError); | ||
} else { | ||
onSuccess(signedOperation); | ||
|
||
const n = | ||
navigation.getParent< | ||
StackNavigatorNavigation<BaseNavigatorStackParamList> | ||
>() || navigation; | ||
n.pop(); | ||
} | ||
}, | ||
onError, | ||
}, | ||
}); | ||
}, | ||
}, | ||
}), | ||
}; | ||
}, [accounts, tracking, manifest, navigation]); | ||
} |
Oops, something went wrong.