Skip to content

Commit

Permalink
Add Ledger demo in Toolshed (#107)
Browse files Browse the repository at this point in the history
* Feat: There were no ledger demo in Toolshed

Solution: Add Ledger connection example in Toolshed
  • Loading branch information
Rgascoin authored Dec 12, 2022
1 parent 6ad77c7 commit a2c6ba5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 21 deletions.
6 changes: 4 additions & 2 deletions examples/toolshed/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ This tool is intended as a quick demo to send message on the Aleph.im network us
## Setup

```
You must install all the dependencies from the typescript sdk at the root of the project before running it
# install dependencies
npm install
npm install
# Launches the app in your default browser
npm run dev
```
```
38 changes: 22 additions & 16 deletions examples/toolshed/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@ import SelectProvider from './components/SelectProvider'
import KeypairConfig from './components/KeypairConfig'
import WalletConfig from './components/WalletConfig'
import MessageConfig from './components/MessageConfig'
import HardwareConfig from "./components/HardwareConfig";


function App() {
const [state, dispatch] = useReducer(reducer, initState)


const connection = () => {
if (state.account) {
return (
<div>
<p>Your address is:</p>
<span>{state.account.address}</span>
</div>
)
}

if (state.selectedChain.endsWith('_KP')) {
return (<KeypairConfig state={state} dispatch={dispatch} />)
} else if (state.selectedChain.endsWith('_HW')) {
return (<HardwareConfig state={state} dispatch={dispatch} />)
}

return (<WalletConfig state={state} dispatch={dispatch} />)
}

return (
<main>
<h1>Aleph.im | Message toolshed</h1>
Expand All @@ -26,21 +46,7 @@ function App() {
</section>
<section className="halfpage">
<h2>Config</h2>
{ !state.account ?
(state.selectedChain.endsWith('_KP') ?
<KeypairConfig state={state} dispatch={dispatch} />
:
<WalletConfig state={state} dispatch={dispatch} />
)

:

<div>
<p>Your address is:</p>
<span>{state.account.address}</span>
</div>

}
{ connection() }
</section>
</section>

Expand Down
38 changes: 38 additions & 0 deletions examples/toolshed/src/components/HardwareConfig.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ethereum } from '../../../../src/accounts/providers/Ledger'
import { HardwareChains } from '../model/chains'
import { dispatchAndConsume } from '../model/componentProps'
import { Actions } from '../reducer'


function HardwareConfig({ dispatch, state } : dispatchAndConsume) {
const getAccountClass = () => (
state.selectedChain === HardwareChains.Ethereum ? [ethereum, null]
: [null, null]
)

const connectToHardware = async () => {
const [_account, provider] = getAccountClass();

if(_account === null)
return

try{
const account = await _account.GetAccountFromLedger()
dispatch({
type: Actions.SET_ACCOUNT,
payload: account
})
}
catch(err){
alert(err)
}
}

return (
<div>
<button onClick={connectToHardware}>Connect to Hardware</button>
</div>
)
}

export default HardwareConfig
7 changes: 6 additions & 1 deletion examples/toolshed/src/components/SelectProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Select, { SingleValue } from 'react-select'
import { KeypairChains, WalletChains } from '../model/chains'
import {HardwareChains, KeypairChains, WalletChains} from '../model/chains'
import { dispatchProps } from '../model/componentProps'
import { Actions } from '../reducer'

Expand All @@ -26,7 +26,12 @@ export const availableWallets: Option[] = [
{ label: 'Solana (via Phantom)', value: WalletChains.Solana },
]

export const availableHardware: Option[] = [
{ label: 'Ethereum (via Ledger)', value: HardwareChains.Ethereum },
]

export const options = [
{ label: 'Hardware', options: availableHardware },
{ label: 'Wallets', options: availableWallets },
{ label: 'Keypairs', options: availableKeypairs },
]
Expand Down
4 changes: 4 additions & 0 deletions examples/toolshed/src/model/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export enum KeypairChains {
Tezos = "XTC_KP",
}

export enum HardwareChains {
Ethereum = "ETH_HW",
}

export enum WalletChains {
Avalanche = "AVAX",
Ethereum = "ETH",
Expand Down
4 changes: 2 additions & 2 deletions examples/toolshed/src/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Account } from "../../../src/accounts/account";
import { KeypairChains, WalletChains } from "./model/chains";
import { KeypairChains, WalletChains, HardwareChains } from "./model/chains";

export enum Actions {
SELECT_CHAIN,
Expand All @@ -12,7 +12,7 @@ export type ActionType = {
};

export type AppStateType = {
selectedChain: KeypairChains | WalletChains;
selectedChain: KeypairChains | WalletChains | HardwareChains;
account: null | Account;
};

Expand Down

0 comments on commit a2c6ba5

Please sign in to comment.