Skip to content

Commit

Permalink
Merge branch 'main' into feat/LIVE-8078-doc-simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
RamyEB authored Oct 18, 2023
2 parents 1ee4b11 + eae3598 commit e7865ad
Show file tree
Hide file tree
Showing 7 changed files with 479 additions and 16 deletions.
109 changes: 93 additions & 16 deletions apps/docs/pages/appendix/manifest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ A Manifest is a config file that allows external applications and decentralized

If you have followed instructions on the [How to create a Live app(comming soon)](/) page, you should now be ready to interact with the Dapp directly from Ledger Live interface to make sure all the basic features work as expected.

### Ethereum Dapp Browser

Your DApp will be ran inside the Ethereum Dapp Browser, which is a separate application loaded inside Ledger Live to handle interactions between a Dapp and the Ledger Live application. Don’t hesitate to have a look at this project’s Readme to have more information on how to use it and what are the currently supported RPC calls.

### Manifest properties

To test and integrate your application, you first need to write your application Manifest file. This file must contain some mandatory information, such as the app package names, the components, the permissions needed, the hardware and software features, etc.
Expand Down Expand Up @@ -120,22 +116,13 @@ _Complete manifest.json file with default or examples values :_
{
"id": "ReplaceAppName",
"name": "ReplaceAppName",
"url": "http://localhost:3000/",
"url": "http://localhost:3000",
"params": {
"dappUrl": "http://localhost:3000/",
"nanoApp": "ReplaceAppName",
"dappName": "ReplaceAppName",
"networks": [
{
"currency": "ethereum",
"chainID": 1,
"nodeURL": "..."
}
]
"exampleParam": "value"
},
"homepageUrl": "http://localhost:3000/",
"platform": ["ios","android","desktop"],
"apiVersion": "^1.0.0",
"apiVersion": "^2.0.0",
"manifestVersion": "2",
"branch": "stable",
"categories": ["NFT", "Swap", "YourAppCategory"],
Expand All @@ -153,3 +140,93 @@ _Complete manifest.json file with default or examples values :_
"visibility": "complete"
}
```

## Manifest Overrides Guide

### Introduction to Overrides

In the context of app manifests, the **overrides** feature offers a powerful mechanism to create conditional modifications to your manifest. With this feature, you can specify different values for specific conditions, such as Ledger Live versions (`llVersion`) and device (`platform`). This ensures a tailored experience across multiple versions and platforms without the need for separate manifest files.

### Structure of Overrides

Inside your manifest, you can include an `overrides` section to define conditions and their corresponding modifications. Here's how you structure them:

1. **ledgerLiveVersion Overrides**: Allows you to define variations of the manifest specific to different Ledger Live versions.

**Example**:
```json
"overrides": {
"ledgerLiveVersion": {
"1.0.0": {
"name": "overrided manifest"
}
}
}
```
In this example, users with Ledger Live version `1.0.0` will see the app name as "overrided manifest".

2. **Platform Overrides**: Enables you to tailor the manifest for specific platforms like iOS, Android, etc.

**Example**:
```json
"overrides": {
"platform": {
"ios": {
"name": "overrided for iOS"
},
"android": {
"name": "overrided for Android"
}
}
}
```
Depending on the user's device platform, they will see a different app name – "overrided for iOS" on Apple devices and "overrided for Android" on Android devices.

### Depth Limitation in Overrides

It's important to note that only the first depth of fields can be overridden. This means you can't make granular changes to nested properties. If you need to override a field with nested properties, you must redefine the entire field, including all nested properties.

**For example**, you can't override just a nested `url` inside a `params` field like this:
```json
"overrides": {
"platform": {
"ios": {
"params": {
"url": "new-url"
}
}
}
}
```
Instead, you need to redefine the entire `params` field:
```json
"overrides": {
"platform": {
"ios": {
"params": {
"url": "new-url",
"anotherField": "value",
"yetAnotherField": "anotherValue"
}
}
}
}
```

### Activating Overrides

Overrides come into play when the manifest is requested with specific parameters. Here are the supported parameters and how they activate the overrides:

1. **ledgerLiveVersion**: Activated by calling with the `llVersion` parameter.
- **Example**: `http://localhost:3000/api/v1/apps?llVersion=1.0.0`

2. **Platform**: Activated by calling with the `platform` parameter. The `platforms` field inside the manifest will be replaced accordingly.
- **Example**: `http://localhost:3000/api/v1/apps?platform=ios` will generate all manifests for iOS and replace the `platforms` field in the manifest with `["ios"]`.

3. **Combining Parameters**: You can combine multiple parameters to activate multiple overrides.
- **Example**: `http://localhost:3000/api/v1/apps?llVersion=1.0.0&platform=ios` will apply overrides for both Ledger Live version `1.0.0` and the `iOS` platform.


### Priority in Overrides

In cases where both `ledgerLiveVersion` and `platform` might have overriding values for the same field, the priority is determined by their order in the manifest. Whichever is higher takes precedence.
3 changes: 3 additions & 0 deletions apps/docs/pages/examples/_meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"live-app-creation": {
"title": "Build a live app from scratch"
},
"test-live-app": {
"title": "Test your live app"
}
Expand Down
6 changes: 6 additions & 0 deletions apps/docs/pages/examples/live-app-creation/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"start": "Start here",
"configuration": "Configuration",
"hooking-up": "Hooking up",
"manifest": "Import your app in Ledger Live"
}
105 changes: 105 additions & 0 deletions apps/docs/pages/examples/live-app-creation/configuration.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Callout } from "nextra/components";

### 4. **Configuring Ledger's Wallet API**

To interact with Ledger Live, you'll need to configure the Ledger's Wallet API. The first step is to install the necessary packages, and then set up the appropriate configuration through a transport layer named "Transport". This transport layer will enable communication with the Wallet API.

- **Installing Packages**:
Install the necessary packages using npm:

```sh
npm install @ledgerhq/wallet-api-client @ledgerhq/wallet-api-client-react
```

- **Creating Transport Provider**:
Create a file named `TransportProvider.tsx`. This file will contain a component that utilizes the Ledger Wallet API client to create a communication transport Higher Order Component (HOC).

```jsx
// src/TransportProvider.tsx
import { WalletAPIProvider } from "@ledgerhq/wallet-api-client-react";
import { Transport, WindowMessageTransport } from "@ledgerhq/wallet-api-client";

function TransportProvider({ children }) {
function getWalletAPITransport(): Transport {
if (typeof window === "undefined") {
return {
onMessage: undefined,
send: () => {}
};
}

const transport = new WindowMessageTransport();
transport.connect();
return transport;
}

const transport = getWalletAPITransport();

return (
<WalletAPIProvider transport={transport}>{children}</WalletAPIProvider>
);
}

export default TransportProvider;
```

- **Wrapping App with TransportProvider**:
In your root file, wrap your `<App />` with the `TransportProvider`:

```jsx
// src/index.tsx or src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import TransportProvider from './TransportProvider';

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<TransportProvider>
<App />
</TransportProvider>
</React.StrictMode>
);
```

With this setup, your entire application can access Ledger's Wallet API and communicate with it via the `TransportProvider`.

### 5. **Setting Up The Simulator**

During the development and testing phases, the Ledger Wallet API simulator can help you develop without requiring to run Ledger Live in parallel.
You can modify your `TransportProvider.tsx` file to use the `getSimulatorTransport` function provided by the simulator library.

```jsx
// src/TransportProvider.tsx
import { WalletAPIProvider } from "@ledgerhq/wallet-api-client-react";
import { getSimulatorTransport, profiles } from "@ledgerhq/wallet-api-simulator";
import type { Transport } from "@ledgerhq/wallet-api-core";

function TransportProvider({ children }) {
function getWalletAPITransport(): Transport {
if (typeof window === "undefined") {
return {
onMessage: undefined,
send: () => {},
};
}

// Use Simulator transport
const transport = getSimulatorTransport(profiles.STANDARD);

return transport;
}

const transport = getWalletAPITransport();

return (
<WalletAPIProvider transport={transport}>{children}</WalletAPIProvider>
);
}

export default TransportProvider;
```

<Callout emoji="💡">
For more informations regarding the simulator, please checkout out [the simulator subsection](/react/simulator)
</Callout>
123 changes: 123 additions & 0 deletions apps/docs/pages/examples/live-app-creation/hooking-up.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
### 6. **Fetching Account Information**

With your environment set up, you can now start interacting with the Ledger Wallet API. Create a new file under the `/hooks` directory named `useAccounts.tsx`. This file will house a custom hook to fetch user account information using the `useAccounts` hook provided by Ledger.

```jsx
// src/hooks/useAccounts.tsx
import { useAccounts } from "@ledgerhq/wallet-api-client-react";

function useUserAccounts() {
const { accounts, loading, error } = useAccounts();

return {
accounts,
loading,
error,
};
}

export default useUserAccounts;
```

Now, you can use this custom hook in your components to fetch and display user account information.

### 7. **Creating Transaction Signing Functionality**

Now that you can fetch account information, the next step is to facilitate transaction signing within your app. Create a new file under the `/hooks` directory named `useSignTransaction.tsx`. This file will contain a custom hook to sign transactions using the `useSignTransaction` hook provided by Ledger.

```jsx
// src/hooks/useSignTransaction.tsx
import { useSignTransaction, useRequestAccount } from '@ledgerhq/wallet-api-client-react';
import { useCallback, useState, useEffect } from 'react';
import BigNumber from 'bignumber.js';

function useTransactionSigner() {
const { requestAccount, account } = useRequestAccount();
const { signTransaction, pending, signature, error } = useSignTransaction();
const [response, setResponse] = useState(null);

useEffect(() => {
requestAccount();
}, [requestAccount]);

const handleSignTransaction = useCallback(async () => {
if (!account) return;

const ethereumTransaction = {
family: 'ethereum',
amount: new BigNumber(1000000000000000), // 0.001 ETH in wei
recipient: '0xRecipientAddressHere',
gasPrice: new BigNumber(20000000000), // 20 Gwei
gasLimit: new BigNumber(21000),
nonce: 0, // Replace with the correct nonce
};

try {
const signature = await signTransaction(account.id, ethereumTransaction);
setResponse(signature);
} catch (e) {
console.error(e);
}
}, [account, signTransaction]);

return {
handleSignTransaction,
pending,
response,
error
};
}

export default useTransactionSigner;
```

### 8. **Building User Interface**

Now it's time to create the UI where users will interact with your app. Create a new file named `App.tsx` under the `/src` directory.

```jsx
// src/App.tsx
import React from 'react';
import useUserAccounts from './hooks/useAccounts';
import useTransactionSigner from './hooks/useSignTransaction';

function App() {
const { accounts, loading, error } = useUserAccounts();
const { handleSignTransaction, pending, response, error: signError } = useTransactionSigner();

return (
<>
<h1>User's Crypto Accounts</h1>
{loading ? (
<p>Loading...</p>
) : error ? (
<p>Error: {error.message}</p>
) : (
<ul>
{(accounts ?? []).map(({ id, name, address, currency, balance }) => (
<li key={id}>
<p>id: {id}</p>
<p>name: {name}</p>
<p>address: {address}</p>
<p>currency: {currency}</p>
{/* Make sure to parse BigNumber */}
<p>balance: {balance.toString()}</p>
</li>
))}
</ul>
)}
<button onClick={handleSignTransaction} disabled={pending}>
Sign Ethereum Transaction
</button>
{pending && <p>Signing...</p>}
{signError && <p>Error: {signError.toString()}</p>}
{response && <p>Transaction signed successfully: {response.toString('hex')}</p>}
</>
);
}
export default App;
```
Loading

0 comments on commit e7865ad

Please sign in to comment.