Skip to content

Commit

Permalink
✨ (sample) [DSDK-519]: Add support of calUrl and testing endpoint (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
aussedatlo authored Oct 8, 2024
2 parents 00f6a78 + f961e3b commit b078b3e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-ducks-heal.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions apps/sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 36 additions & 7 deletions apps/sample/src/components/KeyringEthView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from "react";
import React, { useCallback, useMemo } from "react";

import { useSdk } from "@/providers/DeviceSdkProvider";
import {
Expand All @@ -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,
Expand All @@ -43,7 +65,7 @@ export const KeyringEthView: React.FC<{ sessionId: string }> = ({
checkOnDevice,
returnChainCode,
}) => {
return keyring.getAddress(derivationPath, {
return getKeyringEth().getAddress(derivationPath, {
checkOnDevice,
returnChainCode,
});
Expand All @@ -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",
Expand All @@ -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 },
Expand All @@ -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 {
Expand Down Expand Up @@ -155,6 +182,8 @@ export const KeyringEthView: React.FC<{ sessionId: string }> = ({
{
derivationPath: string;
message: string;
test: boolean;
calUrl: string;
},
SignTypedDataDAError,
SignTypedDataDAIntermediateValue
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b078b3e

Please sign in to comment.