Skip to content

Commit

Permalink
refactor: consolidate various describePath functions in core
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnerdhair committed Feb 12, 2022
1 parent f7c22bf commit e967407
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 114 deletions.
2 changes: 1 addition & 1 deletion packages/hdwallet-core/src/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions packages/hdwallet-core/src/ethereum.test.ts
Original file line number Diff line number Diff line change
@@ -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"`);
Expand Down Expand Up @@ -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"`);
Expand Down Expand Up @@ -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"`);
Expand Down Expand Up @@ -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"`);
Expand Down
2 changes: 1 addition & 1 deletion packages/hdwallet-core/src/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions packages/hdwallet-core/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>;

Expand Down Expand Up @@ -385,3 +386,40 @@ export interface HDWallet extends HDWalletInfo {
*/
disconnect(): Promise<void>;
}

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);
}
}
19 changes: 3 additions & 16 deletions packages/hdwallet-keepkey/src/keepkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
11 changes: 3 additions & 8 deletions packages/hdwallet-ledger/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
7 changes: 1 addition & 6 deletions packages/hdwallet-metamask/src/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

public ethNextAccountPath(msg: core.ETHAccountPath): core.ETHAccountPath | undefined {
Expand Down
4 changes: 2 additions & 2 deletions packages/hdwallet-native/src/bitcoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/hdwallet-native/src/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function MixinNativeBTCWalletInfo<TBase extends core.Constructor<core.HDW
}

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;
Expand Down
5 changes: 4 additions & 1 deletion packages/hdwallet-native/src/native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,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");
});
});

Expand Down
51 changes: 2 additions & 49 deletions packages/hdwallet-native/src/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class NativeHDWalletInfoBase implements core.HDWalletInfo {
}

describePath(msg: core.DescribePath): core.PathDescription {
throw new Error("unreachable");
return core.describePath(msg)
}
}

Expand Down Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions packages/hdwallet-portis/src/portis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
Expand Down
11 changes: 3 additions & 8 deletions packages/hdwallet-trezor/src/trezor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/hdwallet-xdefi/src/xdefi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

Expand Down
7 changes: 1 addition & 6 deletions packages/hdwallet-xdefi/src/xdefi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e967407

Please sign in to comment.