From 003f218028b3198fdf9e16e4169a7730fdb32409 Mon Sep 17 00:00:00 2001 From: MrNerdHair Date: Fri, 11 Feb 2022 21:06:12 -0500 Subject: [PATCH] refactor: dry out remaining describe path functions --- integration/src/thorchain/thorchain.ts | 31 +-- packages/hdwallet-core/src/bitcoin.ts | 2 +- packages/hdwallet-core/src/eos.ts | 40 ++++ packages/hdwallet-core/src/ethereum.test.ts | 12 +- packages/hdwallet-core/src/ethereum.ts | 2 +- packages/hdwallet-core/src/ripple.ts | 40 ++++ packages/hdwallet-core/src/thorchain.ts | 4 +- packages/hdwallet-core/src/wallet.ts | 55 +++++ packages/hdwallet-keepkey/src/keepkey.ts | 224 +------------------ packages/hdwallet-ledger/src/ledger.ts | 11 +- packages/hdwallet-metamask/src/metamask.ts | 7 +- packages/hdwallet-native/src/bitcoin.test.ts | 4 +- packages/hdwallet-native/src/bitcoin.ts | 2 +- packages/hdwallet-native/src/native.test.ts | 7 +- packages/hdwallet-native/src/native.ts | 53 +---- packages/hdwallet-portis/src/portis.ts | 9 +- packages/hdwallet-trezor/src/trezor.ts | 11 +- packages/hdwallet-xdefi/src/xdefi.test.ts | 5 +- packages/hdwallet-xdefi/src/xdefi.ts | 7 +- 19 files changed, 185 insertions(+), 341 deletions(-) diff --git a/integration/src/thorchain/thorchain.ts b/integration/src/thorchain/thorchain.ts index be1dbbbe9..1aac7bfd4 100644 --- a/integration/src/thorchain/thorchain.ts +++ b/integration/src/thorchain/thorchain.ts @@ -52,30 +52,17 @@ export function thorchainTests(get: () => { wallet: core.HDWallet; info: core.HD coin: "Thorchain", }); - // This is strange, and probably wrong, behavior... but it's what happens. - if (wallet.getVendor() === "KeepKey") { - // eslint-disable-next-line jest/no-conditional-expect - expect(out).toMatchInlineSnapshot(` + // eslint-disable-next-line jest/no-conditional-expect + expect(out).toMatchInlineSnapshot(` Object { - "coin": "Thorchain", - "isKnown": false, - "scriptType": undefined, - "verbose": "m/44'/931'/0'/0/0", + "accountIdx": 0, + "coin": "Rune", + "isKnown": true, + "isPrefork": false, + "verbose": "THORChain Account #0", + "wholeAccount": true, } - `); - } else { - // eslint-disable-next-line jest/no-conditional-expect - expect(out).toMatchInlineSnapshot(` - Object { - "accountIdx": 0, - "coin": "Thorchain", - "isKnown": true, - "isPrefork": false, - "verbose": "Thorchain Account #0", - "wholeAccount": true, - } - `); - } + `); }, TIMEOUT ); diff --git a/packages/hdwallet-core/src/bitcoin.ts b/packages/hdwallet-core/src/bitcoin.ts index e9456fe64..dfd47d936 100644 --- a/packages/hdwallet-core/src/bitcoin.ts +++ b/packages/hdwallet-core/src/bitcoin.ts @@ -381,7 +381,7 @@ export function unknownUTXOPath(path: BIP32Path, coin: Coin, scriptType?: BTCInp }; } -export function describeUTXOPath(path: BIP32Path, coin: Coin, scriptType?: BTCInputScriptType): PathDescription { +export function btcDescribePath(path: BIP32Path, coin: Coin, scriptType?: BTCInputScriptType): PathDescription { const unknown = unknownUTXOPath(path, coin, scriptType); if (path.length !== 3 && path.length !== 5) return unknown; diff --git a/packages/hdwallet-core/src/eos.ts b/packages/hdwallet-core/src/eos.ts index 42548568d..272c519d5 100644 --- a/packages/hdwallet-core/src/eos.ts +++ b/packages/hdwallet-core/src/eos.ts @@ -1,3 +1,4 @@ +import { addressNListToBIP32, PathDescription, slip44ByCoin } from "."; import { BIP32Path, HDWallet, HDWalletInfo } from "./wallet"; export interface EosGetPublicKey { @@ -93,3 +94,42 @@ export interface EosWallet extends EosWalletInfo, HDWallet { eosGetPublicKey(msg: EosGetPublicKey): Promise; eosSignTx(msg: EosToSignTx): Promise; } + +export function eosDescribePath(path: BIP32Path): PathDescription { + const pathStr = addressNListToBIP32(path); + const unknown: PathDescription = { + verbose: pathStr, + coin: "Eos", + isKnown: false, + }; + + if (path.length != 5) { + return unknown; + } + + if (path[0] != 0x80000000 + 44) { + return unknown; + } + + if (path[1] != 0x80000000 + slip44ByCoin("Eos")) { + return unknown; + } + + if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) { + return unknown; + } + + if (path[3] !== 0 || path[4] !== 0) { + return unknown; + } + + const index = path[2] & 0x7fffffff; + return { + verbose: `Eos Account #${index}`, + accountIdx: index, + wholeAccount: true, + coin: "Eos", + isKnown: true, + isPrefork: false, + }; +} diff --git a/packages/hdwallet-core/src/ethereum.test.ts b/packages/hdwallet-core/src/ethereum.test.ts index e5cce71d9..1a3eb81d8 100644 --- a/packages/hdwallet-core/src/ethereum.test.ts +++ b/packages/hdwallet-core/src/ethereum.test.ts @@ -1,10 +1,10 @@ import { bip32ToAddressNList } from "."; -import { describeETHPath, ETHAddressDerivationScheme } from "./ethereum"; +import { ETHAddressDerivationScheme, ethDescribePath } from "./ethereum"; -describe("describeETHPath", () => { +describe("ethDescribePath", () => { it("works with BIP44 derivation", async () => { const describePath = (x: string) => - describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.BIP44).verbose; + ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.BIP44).verbose; expect(describePath("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`); expect(describePath("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`); @@ -36,7 +36,7 @@ describe("describeETHPath", () => { it("works with Metamask derivation", async () => { const describePath = (x: string) => - describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Metamask).verbose; + ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Metamask).verbose; expect(describePath("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`); expect(describePath("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`); @@ -68,7 +68,7 @@ describe("describeETHPath", () => { it("works with OldLedger derivation", async () => { const describePath = (x: string) => - describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.OldLedger).verbose; + ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.OldLedger).verbose; expect(describePath("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`); expect(describePath("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`); @@ -100,7 +100,7 @@ describe("describeETHPath", () => { it("works with Ledger derivation", async () => { const describePath = (x: string) => - describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Ledger).verbose; + ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Ledger).verbose; expect(describePath("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`); expect(describePath("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`); diff --git a/packages/hdwallet-core/src/ethereum.ts b/packages/hdwallet-core/src/ethereum.ts index a663139d2..db532b28d 100644 --- a/packages/hdwallet-core/src/ethereum.ts +++ b/packages/hdwallet-core/src/ethereum.ts @@ -148,7 +148,7 @@ export enum ETHAddressDerivationScheme { Ledger = "ledger", } -export function describeETHPath( +export function ethDescribePath( path: BIP32Path, addressDerivationScheme = ETHAddressDerivationScheme.BIP44 ): PathDescription { diff --git a/packages/hdwallet-core/src/ripple.ts b/packages/hdwallet-core/src/ripple.ts index 41d437f6c..b78829a3a 100644 --- a/packages/hdwallet-core/src/ripple.ts +++ b/packages/hdwallet-core/src/ripple.ts @@ -1,3 +1,4 @@ +import { addressNListToBIP32, PathDescription, slip44ByCoin } from "."; import { BIP32Path, HDWallet, HDWalletInfo } from "./wallet"; export interface RippleGetAddress { @@ -97,3 +98,42 @@ export interface RippleWallet extends RippleWalletInfo, HDWallet { rippleGetAddress(msg: RippleGetAddress): Promise; rippleSignTx(msg: RippleSignTx): Promise; } + +export function rippleDescribePath(path: BIP32Path): PathDescription { + const pathStr = addressNListToBIP32(path); + const unknown: PathDescription = { + verbose: pathStr, + coin: "Ripple", + isKnown: false, + }; + + if (path.length != 5) { + return unknown; + } + + if (path[0] != 0x80000000 + 44) { + return unknown; + } + + if (path[1] != 0x80000000 + slip44ByCoin("Ripple")) { + return unknown; + } + + if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) { + return unknown; + } + + if (path[3] !== 0 || path[4] !== 0) { + return unknown; + } + + const index = path[2] & 0x7fffffff; + return { + verbose: `Ripple Account #${index}`, + accountIdx: index, + wholeAccount: true, + coin: "Ripple", + isKnown: true, + isPrefork: false, + }; +} diff --git a/packages/hdwallet-core/src/thorchain.ts b/packages/hdwallet-core/src/thorchain.ts index 66a4bcd87..5220b07cd 100644 --- a/packages/hdwallet-core/src/thorchain.ts +++ b/packages/hdwallet-core/src/thorchain.ts @@ -126,10 +126,10 @@ export function thorchainDescribePath(path: BIP32Path): PathDescription { const index = path[2] & 0x7fffffff; return { - verbose: `Thorchain Account #${index}`, + verbose: `THORChain Account #${index}`, accountIdx: index, wholeAccount: true, - coin: "Thorchain", + coin: "Rune", isKnown: true, isPrefork: false, }; diff --git a/packages/hdwallet-core/src/wallet.ts b/packages/hdwallet-core/src/wallet.ts index 3ed2ec9b7..da3b832ed 100644 --- a/packages/hdwallet-core/src/wallet.ts +++ b/packages/hdwallet-core/src/wallet.ts @@ -1,5 +1,20 @@ import _ from "lodash"; +import { + binanceDescribePath, + btcDescribePath, + cosmosDescribePath, + eosDescribePath, + ETHAddressDerivationScheme, + ethDescribePath, + fioDescribePath, + kavaDescribePath, + osmosisDescribePath, + rippleDescribePath, + secretDescribePath, + terraDescribePath, + thorchainDescribePath, +} from "."; import { BinanceWallet, BinanceWalletInfo } from "./binance"; import { BTCInputScriptType, BTCWallet, BTCWalletInfo } from "./bitcoin"; import { CosmosWallet, CosmosWalletInfo } from "./cosmos"; @@ -385,3 +400,43 @@ export interface HDWallet extends HDWalletInfo { */ disconnect(): Promise; } + +export function describePath( + msg: DescribePath, + ethAddressDerivationScheme?: ETHAddressDerivationScheme +): PathDescription { + switch (msg.coin.toLowerCase()) { + case "ethereum": + return ethDescribePath(msg.path, ethAddressDerivationScheme); + case "atom": + return cosmosDescribePath(msg.path); + case "rune": + case "trune": + case "thorchain": + return thorchainDescribePath(msg.path); + case "secret": + case "scrt": + case "tscrt": + return secretDescribePath(msg.path); + case "luna": + case "terra": + case "tluna": + return terraDescribePath(msg.path); + case "kava": + case "tkava": + return kavaDescribePath(msg.path); + case "binance": + return binanceDescribePath(msg.path); + case "osmosis": + case "osmo": + return osmosisDescribePath(msg.path); + case "fio": + return fioDescribePath(msg.path); + case "ripple": + return rippleDescribePath(msg.path); + case "eos": + return eosDescribePath(msg.path); + default: + return btcDescribePath(msg.path, msg.coin, msg.scriptType); + } +} diff --git a/packages/hdwallet-keepkey/src/keepkey.ts b/packages/hdwallet-keepkey/src/keepkey.ts index d968e8dfe..6e61ec30b 100644 --- a/packages/hdwallet-keepkey/src/keepkey.ts +++ b/packages/hdwallet-keepkey/src/keepkey.ts @@ -19,201 +19,6 @@ export function isKeepKey(wallet: core.HDWallet): wallet is KeepKeyHDWallet { return _.isObject(wallet) && (wallet as any)._isKeepKey; } -function describeCosmosPath(path: core.BIP32Path): core.PathDescription { - const pathStr = core.addressNListToBIP32(path); - const unknown: core.PathDescription = { - verbose: pathStr, - coin: "Atom", - isKnown: false, - }; - - if (path.length != 5) { - return unknown; - } - - if (path[0] != 0x80000000 + 44) { - return unknown; - } - - if (path[1] != 0x80000000 + core.slip44ByCoin("Atom")) { - return unknown; - } - - if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) { - return unknown; - } - - if (path[3] !== 0 || path[4] !== 0) { - return unknown; - } - - const index = path[2] & 0x7fffffff; - return { - verbose: `Cosmos Account #${index}`, - accountIdx: index, - wholeAccount: true, - coin: "Atom", - isKnown: true, - isPrefork: false, - }; -} - -function describeThorchainPath(path: core.BIP32Path): core.PathDescription { - const pathStr = core.addressNListToBIP32(path); - const unknown: core.PathDescription = { - verbose: pathStr, - coin: "Rune", - isKnown: false, - }; - - if (path.length != 5) { - return unknown; - } - - if (path[0] != 0x80000000 + 44) { - return unknown; - } - - if (path[1] != 0x80000000 + core.slip44ByCoin("Rune")) { - return unknown; - } - - if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) { - return unknown; - } - - if (path[3] !== 0 || path[4] !== 0) { - return unknown; - } - - const index = path[2] & 0x7fffffff; - return { - verbose: `THORChain Account #${index}`, - accountIdx: index, - wholeAccount: true, - coin: "Rune", - isKnown: true, - isPrefork: false, - }; -} - -function describeEosPath(path: core.BIP32Path): core.PathDescription { - const pathStr = core.addressNListToBIP32(path); - const unknown: core.PathDescription = { - verbose: pathStr, - coin: "Eos", - isKnown: false, - }; - - if (path.length != 5) { - return unknown; - } - - if (path[0] != 0x80000000 + 44) { - return unknown; - } - - if (path[1] != 0x80000000 + core.slip44ByCoin("Eos")) { - return unknown; - } - - if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) { - return unknown; - } - - if (path[3] !== 0 || path[4] !== 0) { - return unknown; - } - - const index = path[2] & 0x7fffffff; - return { - verbose: `Eos Account #${index}`, - accountIdx: index, - wholeAccount: true, - coin: "Eos", - isKnown: true, - isPrefork: false, - }; -} - -function describeRipplePath(path: core.BIP32Path): core.PathDescription { - const pathStr = core.addressNListToBIP32(path); - const unknown: core.PathDescription = { - verbose: pathStr, - coin: "Ripple", - isKnown: false, - }; - - if (path.length != 5) { - return unknown; - } - - if (path[0] != 0x80000000 + 44) { - return unknown; - } - - if (path[1] != 0x80000000 + core.slip44ByCoin("Ripple")) { - return unknown; - } - - if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) { - return unknown; - } - - if (path[3] !== 0 || path[4] !== 0) { - return unknown; - } - - const index = path[2] & 0x7fffffff; - return { - verbose: `Ripple Account #${index}`, - accountIdx: index, - wholeAccount: true, - coin: "Ripple", - isKnown: true, - isPrefork: false, - }; -} - -function describeBinancePath(path: core.BIP32Path): core.PathDescription { - const pathStr = core.addressNListToBIP32(path); - const unknown: core.PathDescription = { - verbose: pathStr, - coin: "Binance", - isKnown: false, - }; - - if (path.length != 5) { - return unknown; - } - - if (path[0] != 0x80000000 + 44) { - return unknown; - } - - if (path[1] != 0x80000000 + core.slip44ByCoin("Binance")) { - return unknown; - } - - if ((path[2] & 0x80000000) >>> 0 !== 0x80000000) { - return unknown; - } - - if (path[3] !== 0 || path[4] !== 0) { - return unknown; - } - - const index = path[2] & 0x7fffffff; - return { - verbose: `Binance Account #${index}`, - accountIdx: index, - wholeAccount: true, - coin: "Binance", - isKnown: true, - isPrefork: false, - }; -} - export class KeepKeyHDWalletInfo implements core.HDWalletInfo, @@ -331,24 +136,11 @@ export class KeepKeyHDWalletInfo } public describePath(msg: core.DescribePath): core.PathDescription { - switch (msg.coin) { - case "Ethereum": - return core.describeETHPath(msg.path); - case "Atom": - return describeCosmosPath(msg.path); - case "Binance": - return describeBinancePath(msg.path); - case "Ripple": - return describeRipplePath(msg.path); - case "Eos": - return describeEosPath(msg.path); - default: - return core.describeUTXOPath(msg.path, msg.coin, msg.scriptType); - } + return core.describePath(msg); } public btcNextAccountPath(msg: core.BTCAccountPath): core.BTCAccountPath | undefined { - const description = core.describeUTXOPath(msg.addressNList, msg.coin, msg.scriptType); + const description = core.btcDescribePath(msg.addressNList, msg.coin, msg.scriptType); if (!description.isKnown) { return undefined; } @@ -372,7 +164,7 @@ export class KeepKeyHDWalletInfo public ethNextAccountPath(msg: core.ETHAccountPath): core.ETHAccountPath | undefined { const addressNList = msg.hardenedPath.concat(msg.relPath); - const description = core.describeETHPath(addressNList); + const description = core.ethDescribePath(addressNList); if (!description.isKnown) { return undefined; } @@ -391,7 +183,7 @@ export class KeepKeyHDWalletInfo } public cosmosNextAccountPath(msg: core.CosmosAccountPath): core.CosmosAccountPath | undefined { - const description = describeCosmosPath(msg.addressNList); + const description = core.cosmosDescribePath(msg.addressNList); if (!description.isKnown) { return undefined; } @@ -406,7 +198,7 @@ export class KeepKeyHDWalletInfo } public thorchainNextAccountPath(msg: core.ThorchainAccountPath): core.ThorchainAccountPath | undefined { - const description = describeThorchainPath(msg.addressNList); + const description = core.thorchainDescribePath(msg.addressNList); if (!description.isKnown) { return undefined; } @@ -421,7 +213,7 @@ export class KeepKeyHDWalletInfo } public rippleNextAccountPath(msg: core.RippleAccountPath): core.RippleAccountPath | undefined { - const description = describeRipplePath(msg.addressNList); + const description = core.rippleDescribePath(msg.addressNList); if (!description.isKnown) { return undefined; } @@ -435,7 +227,7 @@ export class KeepKeyHDWalletInfo } public binanceNextAccountPath(msg: core.BinanceAccountPath): core.BinanceAccountPath | undefined { - const description = describeBinancePath(msg.addressNList); + const description = core.binanceDescribePath(msg.addressNList); if (!description.isKnown) { return undefined; } @@ -450,7 +242,7 @@ export class KeepKeyHDWalletInfo } public eosNextAccountPath(msg: core.EosAccountPath): core.EosAccountPath | undefined { - const description = describeEosPath(msg.addressNList); + const description = core.eosDescribePath(msg.addressNList); if (!description.isKnown) { return undefined; } diff --git a/packages/hdwallet-ledger/src/ledger.ts b/packages/hdwallet-ledger/src/ledger.ts index 6b6bfb74c..27c9253c4 100644 --- a/packages/hdwallet-ledger/src/ledger.ts +++ b/packages/hdwallet-ledger/src/ledger.ts @@ -92,16 +92,11 @@ export class LedgerHDWalletInfo implements core.HDWalletInfo, core.BTCWalletInfo } public describePath(msg: core.DescribePath): core.PathDescription { - switch (msg.coin) { - case "Ethereum": - return core.describeETHPath(msg.path, core.ETHAddressDerivationScheme.Ledger); - default: - return core.describeUTXOPath(msg.path, msg.coin, msg.scriptType); - } + return core.describePath(msg, core.ETHAddressDerivationScheme.Ledger); } public btcNextAccountPath(msg: core.BTCAccountPath): core.BTCAccountPath | undefined { - const description = core.describeUTXOPath(msg.addressNList, msg.coin, msg.scriptType); + const description = core.btcDescribePath(msg.addressNList, msg.coin, msg.scriptType); if (!description.isKnown) { return undefined; } @@ -125,7 +120,7 @@ export class LedgerHDWalletInfo implements core.HDWalletInfo, core.BTCWalletInfo public ethNextAccountPath(msg: core.ETHAccountPath): core.ETHAccountPath | undefined { const addressNList = msg.hardenedPath.concat(msg.relPath); - const description = core.describeETHPath(addressNList, core.ETHAddressDerivationScheme.Ledger); + const description = core.ethDescribePath(addressNList, core.ETHAddressDerivationScheme.Ledger); if (!description.isKnown) { return undefined; } diff --git a/packages/hdwallet-metamask/src/metamask.ts b/packages/hdwallet-metamask/src/metamask.ts index 7dab98b83..caa8cf993 100644 --- a/packages/hdwallet-metamask/src/metamask.ts +++ b/packages/hdwallet-metamask/src/metamask.ts @@ -66,12 +66,7 @@ export class MetaMaskHDWalletInfo implements core.HDWalletInfo, core.ETHWalletIn } public describePath(msg: core.DescribePath): core.PathDescription { - switch (msg.coin) { - case "Ethereum": - return core.describeETHPath(msg.path); - default: - throw new Error("Unsupported path"); - } + return core.describePath(msg); } // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/packages/hdwallet-native/src/bitcoin.test.ts b/packages/hdwallet-native/src/bitcoin.test.ts index e51c9199b..b19c26029 100644 --- a/packages/hdwallet-native/src/bitcoin.test.ts +++ b/packages/hdwallet-native/src/bitcoin.test.ts @@ -286,8 +286,8 @@ describe("NativeBTCWalletInfo", () => { ["BIP84", "m/84'/0'/0'/0/0", "p2wpkh"], ])("should not work for a %s path with an unrecognized purpose field", (_, path, scriptType: any) => { const mock = jest - .spyOn(core, "describeUTXOPath") - .mockReturnValue(core.describeUTXOPath(core.bip32ToAddressNList(path), "Bitcoin", scriptType)); + .spyOn(core, "btcDescribePath") + .mockReturnValue(core.btcDescribePath(core.bip32ToAddressNList(path), "Bitcoin", scriptType)); expect( info.btcNextAccountPath({ coin: "Bitcoin", diff --git a/packages/hdwallet-native/src/bitcoin.ts b/packages/hdwallet-native/src/bitcoin.ts index 182ea6bbd..c471a10b2 100644 --- a/packages/hdwallet-native/src/bitcoin.ts +++ b/packages/hdwallet-native/src/bitcoin.ts @@ -108,7 +108,7 @@ export function MixinNativeBTCWalletInfo { }, { msg: { coin: "rune", path: [44 + 0x80000000, 931 + 0x80000000, 0 + 0x80000000, 0, 0] }, - out: { coin: "Thorchain", verbose: "Thorchain Account #0", isKnown: true }, + out: { coin: "Rune", verbose: "THORChain Account #0", isKnown: true }, }, { msg: { coin: "secret", path: [44 + 0x80000000, 529 + 0x80000000, 0 + 0x80000000, 0, 0] }, @@ -94,8 +94,11 @@ describe("NativeHDWalletInfo", () => { msg: { coin: "Osmo", path: [44 + 0x80000000, 118 + 0x80000000, 0 + 0x80000000, 0, 0] }, out: { coin: "Osmo", verbose: "Osmosis Account #0", isKnown: true }, }, + { + msg: { coin: "foobar", path: [1, 2, 3] }, + out: { coin: "foobar", verbose: "m/1/2/3", isKnown: false }, + }, ].forEach((x) => expect(info.describePath(x.msg)).toMatchObject(x.out)); - expect(() => info.describePath({ coin: "foobar", path: [1, 2, 3] })).toThrowError("Unsupported path"); }); }); diff --git a/packages/hdwallet-native/src/native.ts b/packages/hdwallet-native/src/native.ts index 9f449b0c2..2c506aef9 100644 --- a/packages/hdwallet-native/src/native.ts +++ b/packages/hdwallet-native/src/native.ts @@ -75,7 +75,7 @@ export class NativeHDWalletInfoBase implements core.HDWalletInfo { // eslint-disable-next-line @typescript-eslint/no-unused-vars describePath(msg: core.DescribePath): core.PathDescription { - throw new Error("unreachable"); + return core.describePath(msg); } } @@ -128,56 +128,7 @@ class NativeHDWalletInfo ) ) ) - implements core.HDWalletInfo -{ - describePath(msg: core.DescribePath): core.PathDescription { - switch (msg.coin.toLowerCase()) { - case "bitcoin": - case "bitcoincash": - case "dash": - case "digibyte": - case "dogecoin": - case "litecoin": - case "testnet": { - const unknown = core.unknownUTXOPath(msg.path, msg.coin, msg.scriptType); - - if (!msg.scriptType) return unknown; - if (!super.btcSupportsCoinSync(msg.coin)) return unknown; - if (!super.btcSupportsScriptTypeSync(msg.coin, msg.scriptType)) return unknown; - - return core.describeUTXOPath(msg.path, msg.coin, msg.scriptType); - } - case "ethereum": - return core.describeETHPath(msg.path); - case "atom": - return core.cosmosDescribePath(msg.path); - case "rune": - case "trune": - case "thorchain": - return core.thorchainDescribePath(msg.path); - case "secret": - case "scrt": - case "tscrt": - return core.secretDescribePath(msg.path); - case "luna": - case "terra": - case "tluna": - return core.terraDescribePath(msg.path); - case "kava": - case "tkava": - return core.kavaDescribePath(msg.path); - case "binance": - return core.binanceDescribePath(msg.path); - case "osmosis": - case "osmo": - return core.osmosisDescribePath(msg.path); - case "fio": - return core.fioDescribePath(msg.path); - default: - throw new Error("Unsupported path"); - } - } -} + implements core.HDWalletInfo {} export class NativeHDWallet extends MixinNativeBTCWallet( diff --git a/packages/hdwallet-portis/src/portis.ts b/packages/hdwallet-portis/src/portis.ts index a47e0a0a0..7a4661ca8 100644 --- a/packages/hdwallet-portis/src/portis.ts +++ b/packages/hdwallet-portis/src/portis.ts @@ -63,14 +63,7 @@ export class PortisHDWalletInfo implements core.HDWalletInfo, core.ETHWalletInfo } public describePath(msg: core.DescribePath): core.PathDescription { - switch (msg.coin) { - case "Ethereum": - return core.describeETHPath(msg.path); - case "Bitcoin": - return core.describeUTXOPath(msg.path, msg.coin, msg.scriptType); - default: - throw new Error("Unsupported path"); - } + return core.describePath(msg); } public async btcSupportsCoin(coin: core.Coin): Promise { diff --git a/packages/hdwallet-trezor/src/trezor.ts b/packages/hdwallet-trezor/src/trezor.ts index 8852ce018..44ec67057 100644 --- a/packages/hdwallet-trezor/src/trezor.ts +++ b/packages/hdwallet-trezor/src/trezor.ts @@ -93,16 +93,11 @@ export class TrezorHDWalletInfo implements core.HDWalletInfo, core.BTCWalletInfo } public describePath(msg: core.DescribePath): core.PathDescription { - switch (msg.coin) { - case "Ethereum": - return core.describeETHPath(msg.path, core.ETHAddressDerivationScheme.Metamask); - default: - return core.describeUTXOPath(msg.path, msg.coin, msg.scriptType); - } + return core.describePath(msg, core.ETHAddressDerivationScheme.Metamask); } public btcNextAccountPath(msg: core.BTCAccountPath): core.BTCAccountPath | undefined { - const description = core.describeUTXOPath(msg.addressNList, msg.coin, msg.scriptType); + const description = core.btcDescribePath(msg.addressNList, msg.coin, msg.scriptType); if (!description.isKnown) { return undefined; } @@ -126,7 +121,7 @@ export class TrezorHDWalletInfo implements core.HDWalletInfo, core.BTCWalletInfo public ethNextAccountPath(msg: core.ETHAccountPath): core.ETHAccountPath | undefined { const addressNList = msg.hardenedPath.concat(msg.relPath); - const description = core.describeETHPath(addressNList, core.ETHAddressDerivationScheme.Metamask); + const description = core.ethDescribePath(addressNList, core.ETHAddressDerivationScheme.Metamask); if (!description.isKnown) { return undefined; } diff --git a/packages/hdwallet-xdefi/src/xdefi.test.ts b/packages/hdwallet-xdefi/src/xdefi.test.ts index 93ef810b7..0f31f806f 100644 --- a/packages/hdwallet-xdefi/src/xdefi.test.ts +++ b/packages/hdwallet-xdefi/src/xdefi.test.ts @@ -25,8 +25,11 @@ describe("XDeFIHDWalletInfo", () => { msg: { coin: "Ethereum", path: [44 + 0x80000000, 60 + 0x80000000, 0 + 0x80000000, 0, 0] }, out: { coin: "Ethereum", verbose: "Ethereum Account #0", isKnown: true }, }, + { + msg: { coin: "foobar", path: [1, 2, 3] }, + out: { coin: "foobar", verbose: "m/1/2/3", isKnown: false }, + }, ].forEach((x) => expect(info.describePath(x.msg)).toMatchObject(x.out)); - expect(() => info.describePath({ coin: "foobar", path: [1, 2, 3] })).toThrowError("Unsupported path"); }); }); diff --git a/packages/hdwallet-xdefi/src/xdefi.ts b/packages/hdwallet-xdefi/src/xdefi.ts index 4b578d32a..a7a6da6ee 100644 --- a/packages/hdwallet-xdefi/src/xdefi.ts +++ b/packages/hdwallet-xdefi/src/xdefi.ts @@ -52,12 +52,7 @@ export class XDeFiHDWalletInfo implements core.HDWalletInfo, core.ETHWalletInfo } public describePath(msg: core.DescribePath): core.PathDescription { - switch (msg.coin) { - case "Ethereum": - return core.describeETHPath(msg.path); - default: - throw new Error("Unsupported path"); - } + return core.describePath(msg); } // eslint-disable-next-line @typescript-eslint/no-unused-vars