From ddc84830b7bccdb7820969d508d865feccc19d1d Mon Sep 17 00:00:00 2001 From: MrNerdHair Date: Sat, 12 Feb 2022 00:22:53 -0500 Subject: [PATCH] refactor: consolidate various describePath functions in core --- packages/hdwallet-core/src/bitcoin.ts | 2 +- packages/hdwallet-core/src/ethereum.test.ts | 12 ++--- packages/hdwallet-core/src/ethereum.ts | 2 +- packages/hdwallet-core/src/wallet.ts | 38 +++++++++++++++ packages/hdwallet-keepkey/src/keepkey.ts | 19 ++------ 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 | 5 +- packages/hdwallet-native/src/native.ts | 51 +------------------- 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 +-- 15 files changed, 71 insertions(+), 114 deletions(-) diff --git a/packages/hdwallet-core/src/bitcoin.ts b/packages/hdwallet-core/src/bitcoin.ts index 771cfaccd..586943c87 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/ethereum.test.ts b/packages/hdwallet-core/src/ethereum.test.ts index 2886d2e72..b73eda611 100644 --- a/packages/hdwallet-core/src/ethereum.test.ts +++ b/packages/hdwallet-core/src/ethereum.test.ts @@ -1,9 +1,9 @@ import { bip32ToAddressNList } from "."; -import { describeETHPath, ETHAddressDerivationScheme } from "./ethereum"; +import { ETHAddressDerivationScheme, ethDescribePath } from "./ethereum"; -describe("describeETHPath", () => { +describe("ethDescribePath", () => { it("works with BIP44 derivation", async () => { - const test = (x: string) => describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.BIP44).verbose; + const test = (x: string) => ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.BIP44).verbose; expect(test("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`); expect(test("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`); @@ -34,7 +34,7 @@ describe("describeETHPath", () => { }); it("works with Metamask derivation", async () => { - const test = (x: string) => describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Metamask).verbose; + const test = (x: string) => ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Metamask).verbose; expect(test("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`); expect(test("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`); @@ -65,7 +65,7 @@ describe("describeETHPath", () => { }); it("works with OldLedger derivation", async () => { - const test = (x: string) => describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.OldLedger).verbose; + const test = (x: string) => ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.OldLedger).verbose; expect(test("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`); expect(test("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`); @@ -96,7 +96,7 @@ describe("describeETHPath", () => { }); it("works with Ledger derivation", async () => { - const test = (x: string) => describeETHPath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Ledger).verbose; + const test = (x: string) => ethDescribePath(bip32ToAddressNList(x), ETHAddressDerivationScheme.Ledger).verbose; expect(test("m/1/2/3")).toMatchInlineSnapshot(`"m/1/2/3"`); expect(test("m/44'/123")).toMatchInlineSnapshot(`"m/44'/123"`); diff --git a/packages/hdwallet-core/src/ethereum.ts b/packages/hdwallet-core/src/ethereum.ts index a6fccef01..f0a5b35f1 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(path: BIP32Path, addressDerivationScheme = ETHAddressDerivationScheme.BIP44): PathDescription { +export function ethDescribePath(path: BIP32Path, addressDerivationScheme = ETHAddressDerivationScheme.BIP44): PathDescription { let pathStr = addressNListToBIP32(path); let unknown: PathDescription = { verbose: pathStr, diff --git a/packages/hdwallet-core/src/wallet.ts b/packages/hdwallet-core/src/wallet.ts index a50d2bfee..6e928e938 100644 --- a/packages/hdwallet-core/src/wallet.ts +++ b/packages/hdwallet-core/src/wallet.ts @@ -14,6 +14,7 @@ import { SecretWallet, SecretWalletInfo } from "./secret"; import { TerraWallet, TerraWalletInfo } from "./terra"; import { ThorchainWallet, ThorchainWalletInfo } from "./thorchain"; import { Transport } from "./transport"; +import { binanceDescribePath, btcDescribePath, cosmosDescribePath, eosDescribePath, ETHAddressDerivationScheme, ethDescribePath, fioDescribePath, kavaDescribePath, osmosisDescribePath, rippleDescribePath, secretDescribePath, terraDescribePath, thorchainDescribePath } from "."; export type BIP32Path = Array; @@ -385,3 +386,40 @@ 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 f0ccce1d7..007cc4985 100644 --- a/packages/hdwallet-keepkey/src/keepkey.ts +++ b/packages/hdwallet-keepkey/src/keepkey.ts @@ -135,24 +135,11 @@ export class KeepKeyHDWalletInfo } public describePath(msg: core.DescribePath): core.PathDescription { - switch (msg.coin) { - case "Ethereum": - return core.describeETHPath(msg.path); - case "Atom": - return core.cosmosDescribePath(msg.path); - case "Binance": - return core.binanceDescribePath(msg.path); - case "Ripple": - return core.rippleDescribePath(msg.path); - case "Eos": - return core.eosDescribePath(msg.path); - default: - return core.describeUTXOPath(msg.path, msg.coin, msg.scriptType); - } + return core.describePath(msg) } public btcNextAccountPath(msg: core.BTCAccountPath): core.BTCAccountPath | undefined { - let description = core.describeUTXOPath(msg.addressNList, msg.coin, msg.scriptType); + let description = core.btcDescribePath(msg.addressNList, msg.coin, msg.scriptType); if (!description.isKnown) { return undefined; } @@ -176,7 +163,7 @@ export class KeepKeyHDWalletInfo public ethNextAccountPath(msg: core.ETHAccountPath): core.ETHAccountPath | undefined { let addressNList = msg.hardenedPath.concat(msg.relPath); - let description = core.describeETHPath(addressNList); + let description = core.ethDescribePath(addressNList); if (!description.isKnown) { return undefined; } diff --git a/packages/hdwallet-ledger/src/ledger.ts b/packages/hdwallet-ledger/src/ledger.ts index e29f338bb..390fec9ee 100644 --- a/packages/hdwallet-ledger/src/ledger.ts +++ b/packages/hdwallet-ledger/src/ledger.ts @@ -91,16 +91,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 { - let description = core.describeUTXOPath(msg.addressNList, msg.coin, msg.scriptType); + let description = core.btcDescribePath(msg.addressNList, msg.coin, msg.scriptType); if (!description.isKnown) { return undefined; } @@ -124,7 +119,7 @@ export class LedgerHDWalletInfo implements core.HDWalletInfo, core.BTCWalletInfo public ethNextAccountPath(msg: core.ETHAccountPath): core.ETHAccountPath | undefined { let addressNList = msg.hardenedPath.concat(msg.relPath); - let description = core.describeETHPath(addressNList, core.ETHAddressDerivationScheme.Ledger) + let 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 11da4a77b..d8d5d2c58 100644 --- a/packages/hdwallet-metamask/src/metamask.ts +++ b/packages/hdwallet-metamask/src/metamask.ts @@ -297,12 +297,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, core.ETHAddressDerivationScheme.Metamask); } public ethNextAccountPath(msg: core.ETHAccountPath): core.ETHAccountPath | undefined { diff --git a/packages/hdwallet-native/src/bitcoin.test.ts b/packages/hdwallet-native/src/bitcoin.test.ts index bfa2a0a74..78ad0017a 100644 --- a/packages/hdwallet-native/src/bitcoin.test.ts +++ b/packages/hdwallet-native/src/bitcoin.test.ts @@ -284,8 +284,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 582ec9815..571a9aea4 100644 --- a/packages/hdwallet-native/src/bitcoin.ts +++ b/packages/hdwallet-native/src/bitcoin.ts @@ -106,7 +106,7 @@ export function MixinNativeBTCWalletInfo { 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 fc235f405..57e3f5261 100644 --- a/packages/hdwallet-native/src/native.ts +++ b/packages/hdwallet-native/src/native.ts @@ -72,7 +72,7 @@ export class NativeHDWalletInfoBase implements core.HDWalletInfo { } describePath(msg: core.DescribePath): core.PathDescription { - throw new Error("unreachable"); + return core.describePath(msg) } } @@ -125,54 +125,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 diff --git a/packages/hdwallet-portis/src/portis.ts b/packages/hdwallet-portis/src/portis.ts index 1418b3ceb..29a34eaec 100644 --- a/packages/hdwallet-portis/src/portis.ts +++ b/packages/hdwallet-portis/src/portis.ts @@ -336,14 +336,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 f9b156591..493c12933 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 { - let description = core.describeUTXOPath(msg.addressNList, msg.coin, msg.scriptType); + let 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 { let addressNList = msg.hardenedPath.concat(msg.relPath); - let description = core.describeETHPath(addressNList, core.ETHAddressDerivationScheme.Metamask); + let 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 cc8211a40..0b5242ced 100644 --- a/packages/hdwallet-xdefi/src/xdefi.test.ts +++ b/packages/hdwallet-xdefi/src/xdefi.test.ts @@ -26,8 +26,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 961c1549e..8c8370051 100644 --- a/packages/hdwallet-xdefi/src/xdefi.ts +++ b/packages/hdwallet-xdefi/src/xdefi.ts @@ -241,12 +241,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); } public ethNextAccountPath(msg: core.ETHAccountPath): core.ETHAccountPath | undefined {