diff --git a/.changeset/tasty-ducks-heal.md b/.changeset/tasty-ducks-heal.md new file mode 100644 index 000000000..5525a260f --- /dev/null +++ b/.changeset/tasty-ducks-heal.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/device-sdk-sample": patch +--- + +Add support of calUrl and testing mode for the keyring-eth and context-module from the sample. diff --git a/apps/sample/package.json b/apps/sample/package.json index 61f57d2d0..785ff1660 100644 --- a/apps/sample/package.json +++ b/apps/sample/package.json @@ -13,6 +13,7 @@ "typecheck": "tsc --noEmit" }, "dependencies": { + "@ledgerhq/context-module": "workspace:*", "@ledgerhq/device-management-kit": "workspace:*", "@ledgerhq/device-signer-kit-ethereum": "workspace:*", "@ledgerhq/react-ui": "^0.16.0", diff --git a/apps/sample/src/components/KeyringEthView/index.tsx b/apps/sample/src/components/KeyringEthView/index.tsx index 428ecdc85..deeb931c6 100644 --- a/apps/sample/src/components/KeyringEthView/index.tsx +++ b/apps/sample/src/components/KeyringEthView/index.tsx @@ -1,4 +1,4 @@ -import React, { useMemo } from "react"; +import React, { useCallback, useMemo } from "react"; import { useSdk } from "@/providers/DeviceSdkProvider"; import { @@ -20,12 +20,34 @@ import { import { DeviceActionProps } from "@/components/DeviceActionsView/DeviceActionTester"; import { ethers } from "ethers"; import { DeviceActionsList } from "@/components/DeviceActionsView/DeviceActionsList"; +import { ContextModuleBuilder } from "@ledgerhq/context-module"; export const KeyringEthView: React.FC<{ sessionId: string }> = ({ sessionId, }) => { const sdk = useSdk(); - const keyring = new KeyringEthBuilder({ sdk, sessionId }).build(); + + const getKeyringEth = useCallback( + (test: boolean = false, calUrl?: string) => { + const builder = new ContextModuleBuilder(); + if (test) { + builder.withConfig({ + cal: { + url: + calUrl && calUrl.length + ? calUrl + : "https://crypto-assets-service.api.ledger-test.com/v1", + mode: "test", + }, + }); + } + const contextModule = builder.build(); + return new KeyringEthBuilder({ sdk, sessionId }) + .withContextModule(contextModule) + .build(); + }, + [sdk, sessionId], + ); const deviceModelId = sdk.getConnectedDevice({ sessionId, @@ -43,7 +65,7 @@ export const KeyringEthView: React.FC<{ sessionId: string }> = ({ checkOnDevice, returnChainCode, }) => { - return keyring.getAddress(derivationPath, { + return getKeyringEth().getAddress(derivationPath, { checkOnDevice, returnChainCode, }); @@ -69,7 +91,7 @@ export const KeyringEthView: React.FC<{ sessionId: string }> = ({ description: "Perform all the actions necessary to sign a message with the device", executeDeviceAction: ({ derivationPath, message }) => { - return keyring.signMessage(derivationPath, message); + return getKeyringEth().signMessage(derivationPath, message); }, initialValues: { derivationPath: "44'/60'/0'/0/0", @@ -94,7 +116,7 @@ export const KeyringEthView: React.FC<{ sessionId: string }> = ({ transaction, recipientDomain, }) => { - return keyring.signTransaction( + return getKeyringEth().signTransaction( derivationPath, ethers.Transaction.from(transaction), { domain: recipientDomain }, @@ -120,13 +142,18 @@ export const KeyringEthView: React.FC<{ sessionId: string }> = ({ title: "Sign typed message", description: "Perform all the actions necessary to sign a typed message on the device", - executeDeviceAction: ({ derivationPath, message }) => { + executeDeviceAction: ({ derivationPath, message, test, calUrl }) => { const typedData = JSON.parse(message) as TypedData; - return keyring.signTypedData(derivationPath, typedData); + return getKeyringEth(test, calUrl).signTypedData( + derivationPath, + typedData, + ); }, initialValues: { derivationPath: "44'/60'/0'/0/0", message: `{"domain":{"name":"USD Coin","verifyingContract":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","chainId":1,"version":"2"},"primaryType":"Permit","message":{"deadline":1718992051,"nonce":0,"spender":"0x111111125421ca6dc452d289314280a0f8842a65","owner":"0x6cbcd73cd8e8a42844662f0a0e76d7f79afd933d","value":"115792089237316195423570985008687907853269984665640564039457584007913129639935"},"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Permit":[{"name":"owner","type":"address"},{"name":"spender","type":"address"},{"name":"value","type":"uint256"},{"name":"nonce","type":"uint256"},{"name":"deadline","type":"uint256"}]}}`, + test: false, + calUrl: "", }, validateValues: ({ message }) => { try { @@ -155,6 +182,8 @@ export const KeyringEthView: React.FC<{ sessionId: string }> = ({ { derivationPath: string; message: string; + test: boolean; + calUrl: string; }, SignTypedDataDAError, SignTypedDataDAIntermediateValue diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dc7c68e1c..f8bf14039 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,6 +62,9 @@ importers: apps/sample: dependencies: + '@ledgerhq/context-module': + specifier: workspace:* + version: link:../../packages/signer/context-module '@ledgerhq/device-management-kit': specifier: workspace:* version: link:../../packages/core