ledger.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2017-2020 @polkadot/react-api authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { Ledger } from '@polkadot/ui-keyring';
  5. import uiSettings from '@polkadot/ui-settings';
  6. import { assert } from '@polkadot/util';
  7. import { api } from './Api';
  8. const ALLOWED_CHAINS: [string, 'kusama' | 'polkadot'][] = [
  9. ['0xb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe', 'kusama'],
  10. ['0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3', 'polkadot']
  11. ];
  12. let ledger: Ledger | null = null;
  13. export function isLedgerCapable (): boolean {
  14. try {
  15. return !!api && ALLOWED_CHAINS.map(([g]) => g).includes(api.genesisHash.toHex());
  16. } catch (error) {
  17. return false;
  18. }
  19. }
  20. export function isLedger (): boolean {
  21. return isLedgerCapable() && uiSettings.ledgerConn !== 'none';
  22. }
  23. export function clearLedger (): void {
  24. ledger = null;
  25. }
  26. export function getLedger (): Ledger {
  27. if (!ledger) {
  28. const def = api && ALLOWED_CHAINS.find(([g]) => g === api.genesisHash.toHex());
  29. assert(def, `Unable to find supported chain for ${api.genesisHash.toHex()}`);
  30. ledger = new Ledger(uiSettings.ledgerConn as 'u2f', def[1]);
  31. }
  32. return ledger;
  33. }