Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove isWalletType introspection functions #435

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ You can import the generated bundle to use each of the component libraries:

```javascript
import { HDWallet } from "@shapeshiftoss/hdwallet-core";
import { isKeepKey, KeepKeyHDWallet } from "@shapeshiftoss/hdwallet-keepkey";
import { isLedger, LedgerHDWallet } from "@shapeshiftoss/hdwallet-ledger";
import { isTrezor, TrezorHDWallet } from "@shapeshiftoss/hdwallet-trezor";
import { KeepKeyHDWallet } from "@shapeshiftoss/hdwallet-keepkey";
import { LedgerHDWallet } from "@shapeshiftoss/hdwallet-ledger";
import { TrezorHDWallet } from "@shapeshiftoss/hdwallet-trezor";

import { WebUSBKeepKeyAdapter } from "@shapeshiftoss/hdwallet-keepkey-webusb";
import { WebUSBLedgerAdapter } from "@shapeshiftoss/hdwallet-ledger-webusb";
Expand Down
4 changes: 2 additions & 2 deletions examples/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ async function deviceConnected(deviceId) {
if (wallet) {
if (wallet.transport) {
await wallet.transport.connect();
if (keepkey.isKeepKey(wallet)) {
if (wallet instanceof keepkey.KeepKeyHDWallet) {
console.log("try connect debuglink");
await wallet.transport.tryConnectDebugLink();
}
Expand Down Expand Up @@ -451,7 +451,7 @@ $getXpubs.on("click", async (e) => {
addressNList: hardenedPath,
curve: "secp256k1",
showDisplay: true, // Not supported by TrezorConnect or Ledger, but KeepKey should do it
coin: portis.isPortis(wallet) ? "Bitcoin" : "Ethereum",
coin: wallet instanceof portis.PortisHDWallet ? "Bitcoin" : "Ethereum",
},
]);

Expand Down
36 changes: 12 additions & 24 deletions integration/src/bitcoin/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa
test(
"btcSupportsCoin()",
async () => {
if (!wallet || portis.isPortis(wallet)) return;
if (!wallet || wallet instanceof portis.PortisHDWallet) return;
expect(wallet.btcSupportsCoin("Bitcoin")).toBeTruthy();
expect(await info.btcSupportsCoin("Bitcoin")).toBeTruthy();
expect(wallet.btcSupportsCoin("Testnet")).toBeTruthy();
Expand All @@ -70,7 +70,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa
);

test("getPublicKeys", async () => {
if (!wallet || ledger.isLedger(wallet) || trezor.isTrezor(wallet) || portis.isPortis(wallet)) return;
if (!wallet || wallet instanceof ledger.LedgerHDWallet || wallet instanceof trezor.TrezorHDWallet || wallet instanceof portis.PortisHDWallet) return;

/* FIXME: Expected failure (trezor does not use scriptType in deriving public keys
and ledger's dependency bitcoinjs-lib/src/crypto.js throws a mysterious TypeError
Expand Down Expand Up @@ -148,7 +148,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa
test(
"btcGetAddress()",
async () => {
if (!wallet || portis.isPortis(wallet)) return;
if (!wallet || wallet instanceof portis.PortisHDWallet) return;
await each(
[
[
Expand Down Expand Up @@ -214,8 +214,8 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa
test(
"btcSignTx() - p2pkh",
async () => {
if (!wallet || portis.isPortis(wallet)) return;
if (ledger.isLedger(wallet)) return; // FIXME: Expected failure
if (!wallet || wallet instanceof portis.PortisHDWallet) return;
if (wallet instanceof ledger.LedgerHDWallet) return; // FIXME: Expected failure
const tx: core.BitcoinTx = {
version: 1,
locktime: 0,
Expand Down Expand Up @@ -289,9 +289,9 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa
test(
"btcSignTx() - thorchain swap",
async () => {
if (!wallet || portis.isPortis(wallet)) return;
if (ledger.isLedger(wallet)) return; // FIXME: Expected failure
if (trezor.isTrezor(wallet)) return; //TODO: Add trezor support for op return data passed at top level
if (!wallet || wallet instanceof portis.PortisHDWallet) return;
if (wallet instanceof ledger.LedgerHDWallet) return; // FIXME: Expected failure
if (wallet instanceof trezor.TrezorHDWallet) return; // FIXME: Expected failure
const tx: core.BitcoinTx = {
version: 1,
locktime: 0,
Expand Down Expand Up @@ -375,11 +375,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa
"btcSignMessage()",
async () => {
if (!wallet) return;

// not implemented for native
if (native.isNative(wallet)) {
return;
}
if (wallet instanceof native.NativeHDWallet) return; // TODO: not implemented for native

let res = wallet.btcSignMessage({
addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/0"),
Expand All @@ -389,7 +385,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa
});

// not implemented on portis
if (portis.isPortis(wallet)) {
if (wallet instanceof portis.PortisHDWallet) {
await expect(res).rejects.toThrowError("not supported");
return;
}
Expand All @@ -407,11 +403,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa
"btcVerifyMessage() - good",
async () => {
if (!wallet) return;

// not implemented for native
if (native.isNative(wallet)) {
return;
}
if (wallet instanceof native.NativeHDWallet) return; // TODO: not implemented for native

let res = await wallet.btcVerifyMessage({
address: "1FH6ehAd5ZFXCM1cLGzHxK1s4dGdq1JusM",
Expand All @@ -430,11 +422,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa
"btcVerifyMessage() - bad",
async () => {
if (!wallet) return;

// not implemented for native
if (native.isNative(wallet)) {
return;
}
if (wallet instanceof native.NativeHDWallet) return; // TODO: not implemented for native

let res = await wallet.btcVerifyMessage({
address: "1FH6ehAd5ZFXCM1cLGzHxK1s4dGdq1JusM",
Expand Down
8 changes: 4 additions & 4 deletions integration/src/bitcoin/testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export function testnetTests(get: () => { wallet: core.HDWallet; info: core.HDWa
test(
"btcSignTx() - p2sh-p2wpkh",
async () => {
if (!wallet || portis.isPortis(wallet)) return;
if (ledger.isLedger(wallet)) return; // FIXME: Expected failure
if (!wallet || wallet instanceof portis.PortisHDWallet) return;
if (wallet instanceof ledger.LedgerHDWallet) return; // FIXME: Expected failure
if (!wallet.btcSupportsCoin("Testnet")) return;
const inputs: core.BTCSignTxInputUnguarded[] = [
{
Expand Down Expand Up @@ -75,8 +75,8 @@ export function testnetTests(get: () => { wallet: core.HDWallet; info: core.HDWa
);

test("btcSignTx() - p2wpkh", async () => {
if (!wallet || portis.isPortis(wallet)) return;
if (ledger.isLedger(wallet)) return; // FIXME: Expected failure
if (!wallet || wallet instanceof portis.PortisHDWallet) return;
if (wallet instanceof ledger.LedgerHDWallet) return; // FIXME: Expected failure
if (!wallet.btcSupportsCoin("Testnet")) return;
const tx: core.BitcoinTx = {
version: core.untouchable("tx.version not provided by test"),
Expand Down
8 changes: 4 additions & 4 deletions integration/src/ethereum/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ export function ethereumTests(get: () => { wallet: core.HDWallet; info: core.HDW
"ethSignTx() - long contract data",
async () => {
if (!wallet) return;
if (ledger.isLedger(wallet)) return; // FIXME: just test kk for now
if (trezor.isTrezor(wallet)) return; // FIXME: just test kk for now
if (portis.isPortis(wallet)) return; // FIXME: just test kk for now
if (wallet instanceof ledger.LedgerHDWallet) return; // FIXME: just test kk for now
if (wallet instanceof trezor.TrezorHDWallet) return; // FIXME: just test kk for now
if (wallet instanceof portis.PortisHDWallet) return; // FIXME: just test kk for now

const txToSign = {
addressNList: core.bip32ToAddressNList("m/44'/60'/0'/0/0"),
Expand Down Expand Up @@ -371,7 +371,7 @@ export function ethereumTests(get: () => { wallet: core.HDWallet; info: core.HDW
"ethSignMessage()",
async () => {
if (!wallet) return;
if (ledger.isLedger(wallet)) return; // FIXME: Expected failure
if (wallet instanceof ledger.LedgerHDWallet) return; // FIXME: Expected failure
let res = await wallet.ethSignMessage({
addressNList: core.bip32ToAddressNList("m/44'/60'/0'/0/0"),
message: "Hello World",
Expand Down
19 changes: 0 additions & 19 deletions integration/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import * as core from "@shapeshiftoss/hdwallet-core";
import * as keepkey from "@shapeshiftoss/hdwallet-keepkey";
import * as ledger from "@shapeshiftoss/hdwallet-ledger";
import * as metamask from "@shapeshiftoss/hdwallet-metamask";
import * as native from "@shapeshiftoss/hdwallet-native";
import * as portis from "@shapeshiftoss/hdwallet-portis";
import * as trezor from "@shapeshiftoss/hdwallet-trezor";
import * as xdefi from "@shapeshiftoss/hdwallet-xdefi";

import { btcTests } from "./bitcoin";
import { ethTests } from "./ethereum";
Expand Down Expand Up @@ -49,18 +42,6 @@ export function integration(suite: WalletSuite): void {
beforeAll(async () => {
wallet = await suite.createWallet();
});

it("has only one vendor", () => {
expect(
(keepkey.isKeepKey(wallet) ? 1 : 0) +
(trezor.isTrezor(wallet) ? 1 : 0) +
(ledger.isLedger(wallet) ? 1 : 0) +
(portis.isPortis(wallet) ? 1 : 0) +
(native.isNative(wallet) ? 1 : 0) +
(metamask.isMetaMask(wallet) ? 1 : 0) +
(xdefi.isXDeFi(wallet) ? 1 : 0)
).toEqual(1);
});
});

describe("ETHWallet", () => {
Expand Down
2 changes: 1 addition & 1 deletion integration/src/wallets/keepkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function selfTest(get: () => core.HDWallet): void {

beforeAll(async () => {
let w = get();
if (keepkey.isKeepKey(w) && core.supportsBTC(w) && core.supportsETH(w)) wallet = w;
if (w instanceof keepkey.KeepKeyHDWallet && core.supportsBTC(w) && core.supportsETH(w)) wallet = w;
else fail("Wallet is not a KeepKey");

await wallet.wipe();
Expand Down
2 changes: 1 addition & 1 deletion integration/src/wallets/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export function selfTest(get: () => core.HDWallet): void {

beforeAll(async () => {
let w = get();
if (ledger.isLedger(w) && core.supportsBTC(w) && core.supportsETH(w)) wallet = w;
if (w instanceof ledger.LedgerHDWallet && core.supportsBTC(w) && core.supportsETH(w)) wallet = w;
else fail("Wallet is not a Ledger");
});

Expand Down
2 changes: 1 addition & 1 deletion integration/src/wallets/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function selfTest(get: () => core.HDWallet): void {
beforeAll(async () => {
let w = get() as metamask.MetaMaskHDWallet;

if (metamask.isMetaMask(w) && !core.supportsBTC(w) && core.supportsETH(w)) {
if (w instanceof metamask.MetaMaskHDWallet && !core.supportsBTC(w) && core.supportsETH(w)) {
wallet = w;
} else {
fail("Wallet is not a MetaMask");
Expand Down
4 changes: 2 additions & 2 deletions integration/src/wallets/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ export function selfTest(get: () => core.HDWallet): void {
let wallet: native.NativeHDWallet;

beforeAll(async () => {
let w = get() as native.NativeHDWallet;
let w = get();

if (native.isNative(w) && core.supportsBTC(w) && core.supportsETH(w)) {
if (w instanceof native.NativeHDWallet && core.supportsBTC(w) && core.supportsETH(w)) {
wallet = w;
} else {
fail("Wallet is not native");
Expand Down
2 changes: 1 addition & 1 deletion integration/src/wallets/portis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function selfTest(get: () => core.HDWallet): void {

beforeAll(() => {
let w = get();
if (portis.isPortis(w) && core.supportsETH(w)) wallet = w;
if (w instanceof portis.PortisHDWallet && core.supportsETH(w)) wallet = w;
else fail("Wallet is not Portis");
});

Expand Down
2 changes: 1 addition & 1 deletion integration/src/wallets/trezor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export function selfTest(get: () => core.HDWallet): void {

beforeAll(async () => {
let w = get();
if (trezor.isTrezor(w) && core.supportsBTC(w) && core.supportsETH(w)) wallet = w;
if (w instanceof trezor.TrezorHDWallet && core.supportsBTC(w) && core.supportsETH(w)) wallet = w;
else fail("Wallet is not a Trezor");
});

Expand Down
2 changes: 1 addition & 1 deletion integration/src/wallets/xdefi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function selfTest(get: () => core.HDWallet): void {

beforeAll(() => {
let w = get();
if (xdefi.isXDeFi(w) && core.supportsETH(w)) wallet = w;
if (w instanceof xdefi.XDeFiHDWallet && core.supportsETH(w)) wallet = w;
else fail("Wallet is not XDeFi");
});

Expand Down
13 changes: 0 additions & 13 deletions packages/hdwallet-keepkey/src/keepkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import { Transport } from "./transport";
import { messageTypeRegistry } from "./typeRegistry";
import { protoFieldToSetMethod, translateInputScriptType } from "./utils";

export function isKeepKey(wallet: core.HDWallet): wallet is KeepKeyHDWallet {
return _.isObject(wallet) && (wallet as any)._isKeepKey;
}

function describeETHPath(path: core.BIP32Path): core.PathDescription {
let pathStr = core.addressNListToBIP32(path);
let unknown: core.PathDescription = {
Expand Down Expand Up @@ -618,24 +614,15 @@ export class KeepKeyHDWallet implements core.HDWallet, core.BTCWallet, core.ETHW
readonly _supportsRippleInfo = true;
readonly _supportsBinanceInfo = true;
readonly _supportsEosInfo = true;
readonly _supportsFioInfo = false;
readonly _supportsDebugLink: boolean;
readonly _isKeepKey = true;
readonly _supportsETH = true;
readonly _supportsBTC = true;
_supportsCosmos = true;
_supportsRipple = true;
_supportsBinance = true;
_supportsEos = true;
readonly _supportsFio = false;
readonly _supportsThorchainInfo = true;
readonly _supportsThorchain = true;
readonly _supportsSecretInfo = false;
readonly _supportsSecret = false;
readonly _supportsKava = false;
readonly _supportsKavaInfo = false;
readonly _supportsTerra = false;
readonly _supportsTerraInfo = false;

transport: Transport;
features?: Messages.Features.AsObject;
Expand Down
6 changes: 0 additions & 6 deletions packages/hdwallet-ledger/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import * as eth from "./ethereum";
import { LedgerTransport } from "./transport";
import { coinToLedgerAppName, handleError } from "./utils";

export function isLedger(wallet: core.HDWallet): wallet is LedgerHDWallet {
return _.isObject(wallet) && (wallet as any)._isLedger;
}

function describeETHPath(path: core.BIP32Path): core.PathDescription {
let pathStr = core.addressNListToBIP32(path);
let unknown: core.PathDescription = {
Expand Down Expand Up @@ -291,8 +287,6 @@ export class LedgerHDWallet implements core.HDWallet, core.BTCWallet, core.ETHWa
readonly _supportsBTC = true;
readonly _supportsETH = true;

_isLedger: boolean = true;

transport: LedgerTransport;
info: LedgerHDWalletInfo & core.HDWalletInfo;

Expand Down
5 changes: 0 additions & 5 deletions packages/hdwallet-metamask/src/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ class MetaMaskTransport extends core.Transport {
}
}

export function isMetaMask(wallet: core.HDWallet): wallet is MetaMaskHDWallet {
return _.isObject(wallet) && (wallet as any)._isMetaMask;
}

type HasNonTrivialConstructor<T> = T extends { new (): any } ? never : T;

export class MetaMaskHDWallet implements core.HDWallet, core.ETHWallet {
Expand All @@ -32,7 +28,6 @@ export class MetaMaskHDWallet implements core.HDWallet, core.ETHWallet {
readonly _supportsBinance = false;
readonly _supportsDebugLink = false;
readonly _isPortis = false;
readonly _isMetaMask = true;
readonly _supportsRippleInfo = false;
readonly _supportsRipple = false;
readonly _supportsEosInfo = false;
Expand Down
4 changes: 2 additions & 2 deletions packages/hdwallet-native/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export class NativeAdapter {
if (!wallet && deviceId) {
// If a wallet with that ID hasn't been added to the keychain, then create it
wallet = await native.create({ deviceId });
if (!native.isNative(wallet)) throw new Error("expected native wallet");
if (!(wallet instanceof native.NativeHDWallet)) throw new Error("expected native wallet");
this.keyring.add(wallet, deviceId);
this.keyring.decorateEvents(deviceId, wallet.events);
}

if (wallet && native.isNative(wallet)) {
if (wallet && wallet instanceof native.NativeHDWallet) {
const id = await wallet.getDeviceID();
this.keyring.emit([wallet.getVendor(), id, core.Events.CONNECT], id);

Expand Down
5 changes: 0 additions & 5 deletions packages/hdwallet-native/src/native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,6 @@ describe("NativeHDWallet", () => {
expect(mock).toHaveBeenCalled();
});

it("should work with isNative", () => {
const wallet = native.create({ deviceId: "native" });
expect(native.isNative(wallet)).toBe(true);
});

it("should respond to .ping()", async () => {
const wallet = native.create({ deviceId: "native" });
expect(await wallet.ping({ msg: "pong" })).toEqual({ msg: "pong" });
Expand Down
5 changes: 0 additions & 5 deletions packages/hdwallet-native/src/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ export class NativeHDWallet
readonly _supportsSecret = true;
readonly _supportsTerra = true;
readonly _supportsKava = true;
readonly _isNative = true;

#deviceId: string;
#initialized: boolean = false;
Expand Down Expand Up @@ -392,10 +391,6 @@ export class NativeHDWallet
}
}

export function isNative(wallet: core.HDWallet): wallet is NativeHDWallet {
return _.isObject(wallet) && (wallet as any)._isNative;
}

export function info() {
return new NativeHDWalletInfo();
}
Expand Down
Loading