This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- `thorchain-ledger` needs to be linked locally (via `yarn link`) to run / build all code
- Loading branch information
Showing
16 changed files
with
231 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,27 @@ | ||
// import TransportNodeHid from '@ledgerhq/hw-transport-node-hid' | ||
// import { BNBChain, BTCChain, Chain } from '@xchainjs/xchain-util' | ||
import { Chain } from '@xchainjs/xchain-util' | ||
// import * as E from 'fp-ts/Either' | ||
import TransportWebUSB from '@ledgerhq/hw-transport-webusb' | ||
import { Chain, THORChain } from '@xchainjs/xchain-util' | ||
import * as E from 'fp-ts/Either' | ||
|
||
import { Network } from '../../../shared/api/types' | ||
// import { LedgerErrorId, Network } from '../../../shared/api/types' | ||
import { LedgerErrorId, Network } from '../../../shared/api/types' | ||
// import { getAddress as getBNBAddress } from './binance' | ||
// import { getAddress as getBTCAddress } from './bitcoin' | ||
// import { getErrorId } from './utils' | ||
import { getAddress as getTHORAddress } from './thorchain' | ||
import { getErrorId } from './utils' | ||
|
||
export const getAddress = async (chain: Chain, network: Network) => { | ||
const _disabled = { chain, network } | ||
// try { | ||
// const transport = await TransportNodeHid.open('') | ||
// let res: E.Either<LedgerErrorId, string> | ||
// switch (chain) { | ||
// case BNBChain: | ||
// res = await getBNBAddress(transport, network) | ||
// break | ||
// case BTCChain: | ||
// res = await getBTCAddress(transport, network) | ||
// break | ||
// default: | ||
// res = E.left(LedgerErrorId.NO_APP) | ||
// } | ||
// await transport.close() | ||
// return res | ||
// } catch (error) { | ||
// return E.left(getErrorId(error.toString())) | ||
// } | ||
try { | ||
const transport = await TransportWebUSB.create() | ||
let res: E.Either<LedgerErrorId, string> | ||
switch (chain) { | ||
case THORChain: | ||
res = await getTHORAddress(transport, network) | ||
break | ||
default: | ||
res = E.left(LedgerErrorId.NO_APP) | ||
} | ||
await transport.close() | ||
return res | ||
} catch (error) { | ||
return E.left(getErrorId(error.toString())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import TransportWebUSB from '@ledgerhq/hw-transport-webusb' | ||
|
||
export const getTransport = async () => await TransportWebUSB.create() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './address' | ||
export * from './transaction' | ||
export * from './common' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Transport from '@ledgerhq/hw-transport' | ||
import THORChainApp from '@thorchain/ledger-thorchain' | ||
import * as Client from '@xchainjs/xchain-client' | ||
import { getPrefix } from '@xchainjs/xchain-thorchain' | ||
import * as E from 'fp-ts/Either' | ||
|
||
import { LedgerErrorId, Network } from '../../../shared/api/types' | ||
import { getErrorId } from './utils' | ||
|
||
// TODO(@Veado) Move `toClientNetwork` from `renderer/services/clients` to `main/util` or so | ||
const toClientNetwork = (network: Network): Client.Network => | ||
network === 'mainnet' ? Client.Network.Mainnet : Client.Network.Testnet | ||
// TODO(@veado) Get path by using `xchain-thorchain` | ||
const PATH = [44, 931, 0, 0, 0] | ||
|
||
export const getAddress = async (transport: Transport, network: Network) => { | ||
try { | ||
const app = new THORChainApp(transport) | ||
const clientNetwork = toClientNetwork(network) | ||
const response = await app.getAddressAndPubKey(PATH, getPrefix(clientNetwork)) | ||
if (response.return_code !== 0x9000) { | ||
// TODO(@Veado) get address from pubkey | ||
return E.right('my-address') | ||
} else { | ||
return E.left(LedgerErrorId.UNKNOWN) | ||
} | ||
} catch (error) { | ||
return E.left(getErrorId(error.toString())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useCallback, useMemo } from 'react' | ||
|
||
import * as RD from '@devexperts/remote-data-ts' | ||
import Transport from '@ledgerhq/hw-transport' | ||
import THORChainApp from '@thorchain/ledger-thorchain' | ||
import { getPrefix as getTHORPrefix } from '@xchainjs/xchain-thorchain' | ||
import { Chain } from '@xchainjs/xchain-util' | ||
import * as E from 'fp-ts/Either' | ||
import * as FP from 'fp-ts/function' | ||
import { useObservableState } from 'observable-hooks' | ||
import * as Rx from 'rxjs' | ||
import * as RxOp from 'rxjs/operators' | ||
|
||
import { LedgerErrorId, Network } from '../../shared/api/types' | ||
import { useAppContext } from '../contexts/AppContext' | ||
import { observableState } from '../helpers/stateHelper' | ||
import { toClientNetwork } from '../services/clients' | ||
import { DEFAULT_NETWORK } from '../services/const' | ||
import { LedgerAddressRD } from '../services/wallet/types' | ||
|
||
// TODO(@veado) Get path by using `xchain-thorchain` | ||
const PATH = [44, 931, 0, 0, 0] | ||
|
||
export const getTHORAddress = async (transport: Transport, network: Network) => { | ||
try { | ||
const app = new THORChainApp(transport) | ||
const clientNetwork = toClientNetwork(network) | ||
const response = await app.getAddressAndPubKey(PATH, getTHORPrefix(clientNetwork)) | ||
if (response.return_code !== 0x9000) { | ||
// TODO(@Veado) get address from pubkey | ||
return E.right('my-address') | ||
} else { | ||
return E.left(LedgerErrorId.UNKNOWN) | ||
} | ||
} catch (error) { | ||
return E.left(LedgerErrorId.WRONG_APP) | ||
} | ||
} | ||
|
||
export const useLedger = () => { | ||
const { network$ } = useAppContext() | ||
const network = useObservableState<Network>(network$, DEFAULT_NETWORK) | ||
|
||
const { get$: address$, set: setAddress } = useMemo(() => observableState<LedgerAddressRD>(RD.initial), []) | ||
|
||
const addressRD = useObservableState(FP.pipe(address$, RxOp.shareReplay(1)), RD.initial) | ||
|
||
const getAddress = useCallback( | ||
(_chain: Chain) => { | ||
FP.pipe( | ||
Rx.from(window.apiHDWallet.getTransport()), | ||
RxOp.switchMap((transport) => Rx.from(getTHORAddress(transport, network))), | ||
RxOp.map(RD.fromEither), | ||
RxOp.startWith(RD.pending), | ||
RxOp.catchError((error) => Rx.of(RD.failure(error))) | ||
).subscribe(setAddress) | ||
}, | ||
[network, setAddress] | ||
) | ||
|
||
// const getAddress = useCallback( | ||
// (chain: Chain) => { | ||
// FP.pipe( | ||
// Rx.from(window.apiHDWallet.getLedgerAddress(chain, network)), | ||
// RxOp.map(RD.fromEither), | ||
// RxOp.startWith(RD.pending), | ||
// RxOp.catchError((error) => Rx.of(RD.failure(error))) | ||
// ).subscribe(setAddress) | ||
// }, | ||
// [network, setAddress] | ||
// ) | ||
|
||
return { | ||
getAddress, | ||
address: addressRD | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module '@thorchain/ledger-thorchain' |
Oops, something went wrong.