Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use legder info from defined genesisHashes #752

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/extension-chains/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"main": "index.js",
"sideEffects": false,
"dependencies": {
"@babel/runtime": "^7.14.5",
"@polkadot/networks": "^6.8.1"
"@babel/runtime": "^7.14.5"
},
"peerDependencies": {
"@polkadot/api": "*"
Expand Down
20 changes: 12 additions & 8 deletions packages/extension-ui/src/Popup/ImportLedger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
import styled from 'styled-components';

import networks from '@polkadot/networks';
import settings from '@polkadot/ui-settings';

import { ActionContext, Address, Button, ButtonArea, Dropdown, VerticalSpace, Warning } from '../components';
Expand All @@ -14,7 +15,6 @@ import useTranslation from '../hooks/useTranslation';
import { createAccountHardware } from '../messaging';
import { Header, Name } from '../partials';
import { ThemeProps } from '../types';
import ledgerChains from '../util/legerChains';

interface AccOption {
text: string;
Expand Down Expand Up @@ -59,16 +59,20 @@ function ImportLedger ({ className }: Props): React.ReactElement {
value
})));

const networkOps = useRef(
[{
const networkOps = useRef([
{
text: t('Select network'),
value: ''
},
...ledgerChains.map(({ displayName, genesisHash }): NetworkOption => ({
text: displayName,
value: genesisHash[0]
}))]
);
...(
networks
.filter((n) => n.hasLedgerSupport)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still problematic, i.e. we don't pull in the data from non-substrate ss58 chains, while we may have a network

.map(({ displayName, genesisHash }): NetworkOption => ({
text: displayName,
value: genesisHash[0]
}))
)
]);

const _onSave = useCallback(
() => {
Expand Down
35 changes: 17 additions & 18 deletions packages/extension-ui/src/hooks/useLedger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import { useCallback, useEffect, useMemo, useState } from 'react';

import { Ledger } from '@polkadot/hw-ledger';
import { Network } from '@polkadot/networks/types';
import { knownGenesis, knownLedger } from '@polkadot/networks/defaults';
import uiSettings from '@polkadot/ui-settings';
import { assert } from '@polkadot/util';

import ledgerChains from '../util/legerChains';
import useTranslation from './useTranslation';

interface StateBase {
Expand All @@ -28,35 +27,35 @@ interface State extends StateBase {
warning: string | null;
}

function getNetwork (genesis: string): Network | undefined {
return ledgerChains.find(({ genesisHash }) => genesisHash[0] === genesis);
function getState (): StateBase {
const isLedgerCapable = !!(window as unknown as { USB?: unknown }).USB;

return {
isLedgerCapable,
isLedgerEnabled: isLedgerCapable && uiSettings.ledgerConn !== 'none'
};
}

function getNetwork (genesisHash: string): string | undefined {
return Object.keys(knownGenesis).find((n) => knownGenesis[n].includes(genesisHash));
}

function retrieveLedger (genesis: string): Ledger {
function retrieveLedger (genesisHash: string): Ledger {
let ledger: Ledger | null = null;

const { isLedgerCapable } = getState();

assert(isLedgerCapable, 'Incompatible browser, only Chrome is supported');

const def = getNetwork(genesis);
const network = getNetwork(genesisHash);

assert(def, `Unable to find supported chain for ${genesis}`);
assert(network && knownLedger[network], `Unable to find Ledger config for genesisHash ${genesisHash}`);

ledger = new Ledger('webusb', def.network);
ledger = new Ledger('webusb', network);

return ledger;
}

function getState (): StateBase {
const isLedgerCapable = !!(window as unknown as { USB?: unknown }).USB;

return {
isLedgerCapable,
isLedgerEnabled: isLedgerCapable && uiSettings.ledgerConn !== 'none'
};
}

export function useLedger (genesis?: string | null, accountIndex = 0, addressOffset = 0): State {
const [isLoading, setIsLoading] = useState(false);
const [isLocked, setIsLocked] = useState(false);
Expand Down Expand Up @@ -100,7 +99,7 @@ export function useLedger (genesis?: string | null, accountIndex = 0, addressOff
setAddress(res.address);
}).catch((e: Error) => {
setIsLoading(false);
const { network } = getNetwork(genesis) || { network: 'unknown network' };
const network = getNetwork(genesis) || 'unknown network';

const warningMessage = e.message.includes('Code: 26628')
? t<string>('Is your ledger locked?')
Expand Down
6 changes: 0 additions & 6 deletions packages/extension-ui/src/util/legerChains.ts

This file was deleted.

1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,6 @@ __metadata:
resolution: "@polkadot/extension-chains@workspace:packages/extension-chains"
dependencies:
"@babel/runtime": ^7.14.5
"@polkadot/networks": ^6.8.1
peerDependencies:
"@polkadot/api": "*"
languageName: unknown
Expand Down