### Install Dependencies and Run Development Server Source: https://developers.ledger.com/docs/ledger-live/discover/integration/dapp-browser/dapp-example Standard commands for installing project dependencies using pnpm and starting the local development environment. ```bash pnpm install pnpm dev ``` -------------------------------- ### Complete package.json Example Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/integration/web-application/web-bluetooth An example of a complete package.json file after installing dependencies and configuring scripts. It lists the project's name, version, private status, scripts, and all production dependencies. ```json { "name": "test-bluetooth", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@ledgerhq/hw-app-eth": "^6.35.1", "@ledgerhq/hw-transport-web-ble": "^6.28.1", "eip55": "^2.1.1", "next": "14.0.4", "react": "^18", "react-dom": "^18" } } ``` -------------------------------- ### Verify Node.js and NPM Installation Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/info Commands to verify that Node.js and NPM are correctly installed in the development environment. These tools are required for building Ledger-integrated applications. ```shell $ node -v v20.9.0 $ npm -v 10.5.0 ``` -------------------------------- ### Start Development Server (npm) Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/beginner/personal-message Starts the Parcel development server for the project. This command should be run after the package.json has been configured and dependencies are installed. ```bash npm run start ``` -------------------------------- ### Install Ledger Wallet API Client Packages Source: https://developers.ledger.com/docs/ledger-live/discover/integration/wallet-api/examples/live-app-creation/configuration Installs the necessary packages for the Ledger Wallet API client and its React integration using npm, pnpm, yarn, or bun. These packages enable communication with the Ledger Wallet. ```bash npm install @ledgerhq/wallet-api-client @ledgerhq/wallet-api-client-react ``` ```bash pnpm add @ledgerhq/wallet-api-client @ledgerhq/wallet-api-client-react ``` ```bash yarn add @ledgerhq/wallet-api-client @ledgerhq/wallet-api-client-react ``` ```bash bun add @ledgerhq/wallet-api-client @ledgerhq/wallet-api-client-react ``` -------------------------------- ### Install Project Dependencies (npm) Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/beginner/transfer-sol This command installs the necessary npm packages for the project. It includes Parcel for building, and specific Ledger and Solana SDKs for device interaction and blockchain communication. Ensure Node.js and npm are installed before running. ```bash npm install --save-dev parcel npm install --save @ledgerhq/hw-app-solana npm install --save @ledgerhq/hw-transport-webusb npm install --save @ledgerhq/logs npm install --save @solana/web3.js npm install --save bs58 ``` -------------------------------- ### Install Ledger Wallet API Client Source: https://developers.ledger.com/docs/ledger-live/discover/integration/wallet-api/core/configuration Commands to install the @ledgerhq/wallet-api-client package using common JavaScript package managers. ```bash npm install @ledgerhq/wallet-api-client ``` ```bash pnpm add @ledgerhq/wallet-api-client ``` ```bash yarn add @ledgerhq/wallet-api-client ``` ```bash bun add @ledgerhq/wallet-api-client ``` -------------------------------- ### Install Ledger Wallet API Simulator Source: https://developers.ledger.com/docs/ledger-live/discover/integration/wallet-api/examples/live-app-creation/start Installs the @ledgerhq/wallet-api-simulator package to provide a mock environment for testing Wallet API interactions without requiring a physical Ledger device. ```shell npm install @ledgerhq/wallet-api-simulator ``` -------------------------------- ### Install Ledger DMK package Source: https://developers.ledger.com/docs/device-interaction/beginner/setup Command to add the Device Management Kit core package to a JavaScript or TypeScript project using the npm package manager. ```bash npm install @ledgerhq/device-management-kit ``` -------------------------------- ### Install Project Dependencies Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/beginner/smart-contract Commands to install necessary packages including ethers, Ledger hardware wallet transport libraries, and Bootstrap for the frontend. ```bash npm install --save bootstrap npm install --save ethers@5.4.7 npm install --save @ledgerhq/hw-app-eth npm install --save @ledgerhq/hw-transport-web-ble npm install --save buffer npm install --save next ``` -------------------------------- ### Install Ledger Wallet API Simulator Source: https://developers.ledger.com/docs/ledger-live/discover/integration/wallet-api/simulator/usage Commands to install the simulator package using various Node.js package managers. ```npm npm install @ledgerhq/wallet-api-simulator ``` ```pnpm pnpm add @ledgerhq/wallet-api-simulator ``` ```yarn yarn add @ledgerhq/wallet-api-simulator ``` ```bun bun add @ledgerhq/wallet-api-simulator ``` -------------------------------- ### Install Project Dependencies Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/beginner/transfer-eth Commands to install necessary packages for building the application, interacting with the Ledger device, and communicating with the Ethereum blockchain. ```bash npm install --save-dev parcel npm install --save @ledgerhq/hw-app-eth npm install --save @ledgerhq/hw-transport-webhid npm install --save ethers ``` -------------------------------- ### Install Ledger Wallet API packages Source: https://developers.ledger.com/docs/ledger-live/discover/integration/wallet-api/react/usage Commands to install the base client, React helpers, and the simulator for testing. These packages are required to enable communication between your dApp and the Ledger Wallet. ```npm npm install @ledgerhq/wallet-api-client @ledgerhq/wallet-api-client-react @ledgerhq/wallet-api-simulator ``` ```pnpm pnpm add @ledgerhq/wallet-api-client @ledgerhq/wallet-api-client-react @ledgerhq/wallet-api-simulator ``` ```yarn yarn add @ledgerhq/wallet-api-client @ledgerhq/wallet-api-client-react @ledgerhq/wallet-api-simulator ``` ```bun bun add @ledgerhq/wallet-api-client @ledgerhq/wallet-api-client-react @ledgerhq/wallet-api-simulator ``` -------------------------------- ### Project Setup Commands Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/integration/web-application/web-hid-usb Shell commands to create a new project directory, navigate into it, and initialize a Node.js project using npm. These commands set up the basic structure for the web application. ```bash mkdir example-web-hid-usb cd example-web-hid-usb npm init ``` -------------------------------- ### CodeQL Workflow Steps: Checkout, Init, Build, Analyze Source: https://developers.ledger.com/docs/device-app/integration/requirements/security This YAML snippet outlines the sequential steps within a CodeQL GitHub Actions workflow. It includes checking out the repository, initializing CodeQL with specified languages and queries, building the app using the appropriate SDK, and performing the CodeQL analysis. ```yaml steps: - name: Clone uses: actions/checkout@v4 with: submodules: true - name: Initialize CodeQL uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} queries: security-and-quality - name: Build run: | make BOLOS_SDK=${{ matrix.sdk }} - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v3 ``` -------------------------------- ### GET /device/apps Source: https://developers.ledger.com/docs/device-interaction/references/device-management-kit/secure-channel Retrieves a list of all applications currently installed on the connected Ledger device. ```APIDOC ## GET /device/apps ### Description Lists all applications installed on the device. This action does not require specific user permission prompts. ### Method GET ### Endpoint /device/apps ### Parameters #### Query Parameters - **unlockTimeout** (number) - Optional - The maximum waiting time in milliseconds for the user to unlock the device. ### Response #### Success Response (200) - **installedApps** (Array) - A list of objects containing details of installed applications. ### Response Example { "installedApps": [ { "name": "Bitcoin", "version": "2.1.0" }, { "name": "Ethereum", "version": "1.9.5" } ] } ``` -------------------------------- ### Start Development Server Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/beginner/smart-contract Command to launch the Next.js development server to host the application locally. ```bash npm run dev ``` -------------------------------- ### Install Ledger Device Management Kit Packages Source: https://developers.ledger.com/docs/clear-signing/for-wallets Install the necessary core packages and Ethereum signer modules to enable Clear Signing. These commands are provided for npm, yarn, and pnpm package managers. ```npm npm install @ledgerhq/device-management-kit @ledgerhq/device-transport-kit-web-hid @ledgerhq/device-signer-kit-ethereum ``` ```yarn yarn add @ledgerhq/device-management-kit @ledgerhq/device-transport-kit-web-hid @ledgerhq/device-signer-kit-ethereum ``` ```pnpm pnpm add @ledgerhq/device-management-kit @ledgerhq/device-transport-kit-web-hid @ledgerhq/device-signer-kit-ethereum ``` -------------------------------- ### Initialize React App and Project Structure Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/beginner/smart-contract Sets up a new React project using create-react-app and organizes the project structure by creating necessary directories and files for the Ledger interaction tutorial. This involves creating pages, styles, and specific component files. ```bash npx create-react-app e2e-tutorial-contract cd e2e-tutorial-contract mkdir pages mkdir styles touch ./ConnectLedger.js touch ./SmartContract.js touch ./ethereum.js touch ./styles/global.css touch ./styles/Home.module.css mv src/index.js pages mv src/App.js ./ rm -r src ``` -------------------------------- ### GET /transaction/{sellId}/status - Get Transaction Status Source: https://developers.ledger.com/docs/ledger-live/exchange/card/sell-card/providers-backend This optional endpoint allows your backend to query the transaction status from Ledger's system, for example, to check if a transaction was approved or rejected by the user's device. ```APIDOC ## GET /transaction/{sellId}/status ### Description This optional endpoint allows your backend to query the transaction status from Ledger's system, for example, to check if a transaction was approved or rejected by the user's device. ### Method GET ### Endpoint /transaction/{sellId}/status ### Parameters #### Path Parameters - **sellId** (string) - Required - The unique identifier for the sell transaction. ### Response #### Success Response (200) - **status** (string) - The current status of the transaction (e.g., APPROVED, REJECTED). #### Response Example ```json { "status": "APPROVED" } ``` ``` -------------------------------- ### Install Ledger Eth and Web BLE Dependencies Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/integration/web-application/web-bluetooth Installs the necessary npm packages for interacting with the Ledger Nano X via Bluetooth and accessing Ethereum addresses. These packages are essential for building applications that communicate with a Ledger device. ```bash npm install --save @ledgerhq/hw-app-eth npm install --save @ledgerhq/hw-transport-web-ble npm install --save eip55 ``` -------------------------------- ### Create Project Directory and Files (Bash) Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/beginner/transfer-eth Initializes a new project directory named 'e2e-eth-tutorial' and creates the necessary HTML, JavaScript, and CSS files, along with an assets folder. This sets up the basic structure for the web application. ```bash mkdir e2e-eth-tutorial cd e2e-eth-tutorial touch index.html touch index.js touch style.css mkdir assets ``` -------------------------------- ### LiveApp URL Structure Source: https://developers.ledger.com/docs/ledger-live/exchange/swap/providers-liveapp Example of a deep-link URL used to open a LiveApp with pre-filled quote parameters from Ledger Wallet. ```text ledgerlive://discover/swapprovidername?quoteId=1234&fromAmount=0.09&fromAccountId=82f48c06-6170-536e-a21b-56b30b02b58d&toAccountId=c0c4493e-fc4b-50b6-b1a0-566f1a751521&rate=79.09405555555555555556&feeStrategy=CUSTOM&initFeeTotalValue=878198522989412&customFeeConfig=%7B%22maxFeePerGas%22%3A%2212000000000%22%2C%22maxPriorityFeePerGas%22%3A%221000000000%22%2C%22userGasLimit%22%3Anull%2C%22gasLimit%22%3A%2221000%22%7D ``` -------------------------------- ### LiveApp URL Structure Source: https://developers.ledger.com/docs/ledger-live/exchange/sell/providers-liveapp Example of the URL structure used to open a LiveApp with pre-filled transaction parameters from Ledger Wallet. ```url https://sell.provider.widget.com?[accountId]=ua11-…&[cryptoCurrency]=bitcoin&[fiatCurrency]=EUR&[cryptoAmount]=0.04 ``` -------------------------------- ### Initialize Ethereum Signer Instance Source: https://developers.ledger.com/docs/device-interaction/references/signers/eth Demonstrates how to instantiate the SignerEthBuilder. It shows both the default configuration using an origin token and a custom configuration using a user-defined context module. ```javascript // Initialise an Ethereum signer instance using default context module const signerEth = new SignerEthBuilder({ sdk, sessionId, originToken: "origin-token", // replace with your origin token }).build(); // Initialise an Ethereum signer instance using customized context module const signerEth = new SignerEthBuilder({ sdk, sessionId }) .withContextModule(customContextModule) .build(); ``` -------------------------------- ### Instantiate WalletAPIServer with Constructor Source: https://developers.ledger.com/docs/ledger-live/discover/integration/wallet-api/server/usage-examples/with-constructor Demonstrates how to create a new instance of the WalletAPIServer using its constructor. It requires a transport object for communication and a configuration object with application-specific settings. ```typescript const transport: Transport = { onMessage: undefined, send: (message: string) => { if (wapiView) { wapiView.webContents.postMessage("message", message); } }, }; const config = { appId: "fake-manifest-id", // should be the real manifest id, used in the storage handler code wallet: { name: "browserview-wallet", version: "0.1.0", }, tracking: false, userId: "fake", }; const wapiServer = new WalletAPIServer(transport, config); ``` -------------------------------- ### Configure package.json Scripts Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/integration/web-application/web-bluetooth Defines the scripts section within the package.json file. These scripts are used to manage the development, build, and start processes of a Next.js application. ```json "scripts": { "dev": "next dev", "build": "next build", "start": "next start" } ``` -------------------------------- ### Initialize Device Management Kit Source: https://developers.ledger.com/docs/clear-signing/for-wallets Configure the DMK instance with a console logger and WebHID transport factory to enable hardware device communication. ```typescript import { DeviceManagementKitBuilder, ConsoleLogger } from "@ledgerhq/device-management-kit"; import { webHidTransportFactory } from "@ledgerhq/device-transport-kit-web-hid"; const dmk = new DeviceManagementKitBuilder() .addLogger(new ConsoleLogger()) .addTransport(webHidTransportFactory) .build(); ``` -------------------------------- ### Define DApp Manifest for Ledger Wallet Source: https://developers.ledger.com/docs/ledger-live/discover/integration/dapp-browser/migration Examples of JSON manifest files for DApps integrating with Ledger Wallet. These manifests utilize the updated 'dapp' field structure for network configurations and application metadata. ```JSON { "id": "metamask-test-dapp", "name": "Metamask Test dApp", "private": false, "url": "https://metamask.github.io/test-dapp/", "dapp": { "networks": [ { "currency": "ethereum", "chainID": 1, "nodeURL": "https://eth-dapps.api.live.ledger.com" }, { "currency": "bsc", "chainID": 56, "nodeURL": "https://bsc-dataseed.binance.org/" } ] }, "apiVersion": "^2.0.0", "manifestVersion": "1" } ``` ```JSON { "id": "1inch-local", "name": "1inch", "private": false, "url": "https://app.1inch.io/", "dapp": { "nanoApp": "1inch", "networks": [ { "currency": "ethereum", "chainID": 1, "nodeURL": "https://eth-dapps.api.live.ledger.com" } ] }, "apiVersion": "^2.0.0", "manifestVersion": "1" } ``` -------------------------------- ### Install App Device Action Source: https://developers.ledger.com/docs/device-interaction/references/device-management-kit/secure-channel Shows how to install an application on a Ledger device by name. This action provides real-time progress updates via the intermediate value and requires handling specific user interaction states. ```typescript import { DeviceActionStatus, UserInteractionRequired, InstallAppDAState, InstallAppDeviceAction, } from "@ledgerhq/device-management-kit"; const deviceAction = new InstallAppDeviceAction({ input: { unlockTimeout: 60000, appName: "Solana" }, }); const { observable, cancel } = await dmk.executeDeviceAction({ deviceAction, }); observable.subscribe({ next: (state: InstallAppDAState) => { switch (state.status) { case DeviceActionStatus.Pending: const { intermediateValue: { requiredUserInteraction, progress }, } = state; switch (requiredUserInteraction) { case UserInteractionRequired.None: console.log(`Installing app progress ${progress}, no user action required`); break; case UserInteractionRequired.AllowSecureConnection: console.log("User should allow the secure connection on the device"); break; case UserInteractionRequired.UnlockDevice: console.log("User should unlock the device"); break; default: throw new Error("Unknown user interaction required"); } break; case DeviceActionStatus.Completed: const { output } = state; console.log("Device action completed", output); break; case DeviceActionStatus.Error: const { error } = state; console.log("Error occurred during the execution", error); break; } }, }); ``` -------------------------------- ### Initialize Next.js Project for Ledger Bluetooth Source: https://developers.ledger.com/docs/device-interaction/ledgerjs/integration/web-application/web-bluetooth Commands to scaffold a new Next.js project environment suitable for Ledger device integration. This setup assumes a standard environment without TypeScript or Tailwind CSS. ```bash npx create-next-app@latest examples-web-bluetooth cd examples-web-bluetooth ``` -------------------------------- ### Configure WalletAPIServer with Setters Source: https://developers.ledger.com/docs/ledger-live/discover/integration/wallet-api/server/usage-examples/with-constructor Shows how to configure the WalletAPIServer after instantiation using setter methods. This includes setting accounts, supported currencies, and specific permissions for dApp interactions. ```typescript wapiServer.setAccounts(accounts); wapiServer.setCurrencies(currencies); wapiServer.setPermissions({ currencyIds: ["**"], methodIds: ["account.list", "account.receive", "account.request"], }); ``` -------------------------------- ### Connect to Device via BLE and USB with LedgerJS Source: https://developers.ledger.com/docs/device-interaction/beginner/ble_flex_stax This example shows how to establish a connection to a Ledger device using both Bluetooth Low Energy (BLE) and USB. It utilizes the hw-transport-web-ble and hw-transport-web-usb libraries from LedgerJS. Ensure you have the necessary LedgerJS transport libraries installed. ```javascript import TransportBLE from "@ledgerhq/hw-transport-web-ble"; import TransportUSB from "@ledgerhq/hw-transport-web-usb"; // Example of connecting to a device via BLE const transportBLE = await TransportBLE.create(); const ethAppBLE = new AppEth(transportBLE); // Example of connecting to a device via USB const transportUSB = await TransportUSB.create(); const ethAppUSB = new AppEth(transportUSB); ``` -------------------------------- ### Install Aleo Signer Kit Source: https://developers.ledger.com/docs/device-interaction/references/signers/aleo Installs the `device-signer-kit-aleo` package using npm. This package is a dependency of the `@ledgerhq/device-management-kit` package, which must be installed first. ```bash npm install @ledgerhq/device-signer-kit-aleo ``` -------------------------------- ### Register Bitcoin Wallet Example Source: https://developers.ledger.com/docs/device-interaction/references/signers/btc Provides a complete TypeScript example of registering a 2-of-2 multisig Bitcoin wallet using the Ledger device signer kit. It demonstrates creating a `WalletPolicy`, calling the `registerWallet` function, and subscribing to the observable to handle device action states and retrieve the `RegisteredWallet`. ```typescript import { WalletPolicy, RegisteredWallet, } from "@ledgerhq/device-signer-kit-bitcoin"; // Define a 2-of-2 multisig wallet policy const walletPolicy = new WalletPolicy( "My Multisig", "wsh(sortedmulti(2,@0/**,@1/**))", [ "[76223a6e/48'/1'/0'/2']tpubDE7NQymr4AFtewpAsWtnreyq9ghkzQBXpCZjWLFVRAvnbf7vya2eMTvT2fPapNqL8SuVvLQdbUbMfWLVDCZKnsEBqp6UK93QEzL8Ck23AwF", "[f5acc2fd/48'/1'/0'/2']tpubDFAqEGNyad35aBCKUAXbQGDjdVhNueno5ZZVEn3sQbW5ci457gLR7HyTmHBg93oourBssgUxuWz1jX5uhc1qaqFo9VsybY1J5FuedLfm4dK", ], ); // Register the wallet const { observable } = signerBitcoin.registerWallet(walletPolicy); observable.subscribe({ next: (state) => { if (state.status === DeviceActionStatus.Completed) { const registeredWallet: RegisteredWallet = state.output; // Store the HMAC securely for future use console.log("Wallet registered! HMAC:", registeredWallet.hmac); } }, }); ``` -------------------------------- ### Use DMK Instance to Listen for Devices (TypeScript) Source: https://developers.ledger.com/docs/device-interaction/beginner/init_dmk Demonstrates how to import and use the initialized DMK instance to listen for available hardware devices. It includes handling device discovery, errors, and completion of the subscription. ```typescript import { dmk } from "./dmk"; // Now you can use the DMK for device operations async function connectToDevice() { const subscription = dmk.listenToAvailableDevices().subscribe({ next: (devices) => { // Handle the available devices here console.log("Available devices:", devices); }, error: (error) => { console.error("Error:", error); }, complete: () => { console.log("Completed"); }, }); // Stop listening to available devices subscription.unsubscribe(); } ``` -------------------------------- ### Install Cosmos Signer Kit using npm Source: https://developers.ledger.com/docs/device-interaction/references/signers/cosmos This command installs the `device-signer-kit-cosmos` package, which is a dependency for interacting with the Cosmos application on a Ledger device. It requires the `@ledgerhq/device-management-kit` package to be installed first. ```bash npm install @ledgerhq/device-signer-kit-cosmos ``` -------------------------------- ### Built-in Command Examples Source: https://developers.ledger.com/docs/device-interaction/integration/how_to/build_custom_command References to built-in commands that can be studied for examples of APDU parsing and command implementation. ```APIDOC ## Examples and References ### Built-in Command Examples Study these existing commands for reference: * `ListAppsCommand` - Simple command with parsing * `GetOsVersionCommand` - Complex response parsing * `OpenAppCommand` - Command with parameters ``` -------------------------------- ### Install Ethereum Signer Kit Source: https://developers.ledger.com/docs/device-interaction/integration/how_to/how_to_use_a_signer Installs the device-signer-kit-ethereum package using npm. This package is a dependency of the @ledgerhq/device-management-kit package. ```bash npm install @ledgerhq/device-signer-kit-ethereum ```