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.
- [x] Add / verify Ledger BCH address - [x] Update CHANGELOG / README
- Loading branch information
Showing
9 changed files
with
142 additions
and
10 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
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,47 @@ | ||
import AppBTC from '@ledgerhq/hw-app-btc' | ||
import type Transport from '@ledgerhq/hw-transport' | ||
import { BCHChain } from '@xchainjs/xchain-util' | ||
import * as E from 'fp-ts/Either' | ||
|
||
import { LedgerError, LedgerErrorId, Network } from '../../../../shared/api/types' | ||
import { toClientNetwork } from '../../../../shared/utils/client' | ||
import { isError } from '../../../../shared/utils/guard' | ||
import { WalletAddress } from '../../../../shared/wallet/types' | ||
import { getDerivationPath } from './common' | ||
|
||
export const getAddress = async ( | ||
transport: Transport, | ||
network: Network, | ||
walletIndex: number | ||
): Promise<E.Either<LedgerError, WalletAddress>> => { | ||
try { | ||
const app = new AppBTC(transport) | ||
const clientNetwork = toClientNetwork(network) | ||
const derivePath = getDerivationPath(walletIndex, clientNetwork) | ||
const { bitcoinAddress: bchAddress } = await app.getWalletPublicKey(derivePath, { | ||
// 'cashaddr' in case of Bitcoin Cash | ||
// @see https://github.com/LedgerHQ/ledgerjs/blob/master/packages/hw-app-btc/README.md#parameters-2 | ||
format: 'cashaddr' | ||
}) | ||
return E.right({ address: bchAddress, chain: BCHChain, type: 'ledger', walletIndex }) | ||
} catch (error) { | ||
return E.left({ | ||
errorId: LedgerErrorId.GET_ADDRESS_FAILED, | ||
msg: `Could not get address from Ledger's BCH app: ${ | ||
isError(error) ? error?.message ?? error.toString() : `${error}` | ||
}` | ||
}) | ||
} | ||
} | ||
|
||
export const verifyAddress = async (transport: Transport, network: Network, walletIndex: number): Promise<void> => { | ||
const app = new AppBTC(transport) | ||
const clientNetwork = toClientNetwork(network) | ||
const derivePath = getDerivationPath(walletIndex, clientNetwork) | ||
const _ = await app.getWalletPublicKey(derivePath, { | ||
// 'cashaddr' in case of Bitcoin Cash | ||
// @see https://github.com/LedgerHQ/ledgerjs/blob/master/packages/hw-app-btc/README.md#parameters-2 | ||
format: 'cashaddr', | ||
verify: true // confirm the address on the device | ||
}) | ||
} |
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,23 @@ | ||
import { Network } from '@xchainjs/xchain-client' | ||
|
||
import { LedgerErrorId } from '../../../../shared/api/types' | ||
|
||
// TODO(@veado) Extend`xchain-bitcoincash` to get derivation path from it | ||
// Similar to default values in `Client` of `xchain-bitcoincash` | ||
// see https://github.com/xchainjs/xchainjs-lib/blob/56adf1e0d6ceab0bdf93f53fe808fe45bf79930f/packages/xchain-bitcoincash/src/client.ts#L65-L69 | ||
export const getDerivationPath = (walletIndex: number, network: Network): string => { | ||
const DERIVATION_PATHES = { | ||
[Network.Mainnet]: ["44'", "145'", "0'", 0, walletIndex], | ||
[Network.Testnet]: ["44'", "1'", "0'", 0, walletIndex], | ||
[Network.Stagenet]: ["44'", "145'", "0'", 0, walletIndex] | ||
} | ||
const path = DERIVATION_PATHES[network].join('/') | ||
return path | ||
} | ||
|
||
export const fromLedgerErrorType = (error: number): LedgerErrorId => { | ||
switch (error) { | ||
default: | ||
return LedgerErrorId.UNKNOWN | ||
} | ||
} |
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