Skip to content

Commit

Permalink
refactor adapters pairDevice()/initialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnerdhair committed Oct 19, 2021
1 parent 5d09496 commit a7b8d10
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/hdwallet-keepkey/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Adapter<DelegateType extends AdapterDelegate<any>> {
} catch {}
}

async initialize(
private async initialize(
device: DeviceType<DelegateType>,
tryDebugLink?: boolean,
autoConnect?: boolean
Expand Down
24 changes: 10 additions & 14 deletions packages/hdwallet-ledger-webusb/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,23 @@ export class WebUSBLedgerAdapter {
}

// without unique device identifiers, we should only ever have one ledger device on the keyring at a time
public async initialize(usbDevice?: USBDevice): Promise<void> {
const device = usbDevice ?? (await getFirstLedgerDevice());
private async initialize(device: USBDevice): Promise<ledger.LedgerHDWallet> {
await this.keyring.delete(core.mustBeDefined(device.serialNumber));

if (device) {
await this.keyring.delete(core.mustBeDefined(device.serialNumber));
const ledgerTransport = await openTransport(device);

const ledgerTransport = await openTransport(device);
const wallet = ledger.create(new LedgerWebUsbTransport(device, ledgerTransport, this.keyring));

const wallet = ledger.create(new LedgerWebUsbTransport(device, ledgerTransport, this.keyring));
await this.keyring.add(wallet, device.serialNumber);

await this.keyring.add(wallet, device.serialNumber);
}
return wallet
}

public async pairDevice(): Promise<core.HDWallet> {
const ledgerTransport = await getTransport();

const device = ledgerTransport.device;
public async pairDevice(usbDevice?: USBDevice): Promise<ledger.LedgerHDWallet> {
const device = usbDevice ?? (await getTransport()).device ?? (await getFirstLedgerDevice());

await this.initialize(device);
const wallet = await this.initialize(device);

return core.mustBeDefined(this.keyring.get(device.serialNumber));
return core.mustBeDefined(wallet);
}
}
1 change: 0 additions & 1 deletion packages/hdwallet-native/src/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ describe("NativeAdapter", () => {
it("creates a unique wallet per deviceId", async () => {
const keyring = new core.Keyring();
const adapter = NativeAdapter.useKeyring(keyring);
expect(await adapter.initialize()).toBe(0);
const wallet = await adapter.pairDevice("foobar");
expect(wallet).toBeInstanceOf(NativeHDWallet);
expect(await adapter.pairDevice("foobar")).toBe(wallet);
Expand Down
4 changes: 0 additions & 4 deletions packages/hdwallet-native/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ export class NativeAdapter {
return new NativeAdapter(keyring);
}

async initialize(): Promise<number> {
return 0;
}

async pairDevice(deviceId: string): Promise<core.HDWallet | null> {
const oldWallet: core.HDWallet | undefined = this.keyring.get(deviceId);
if (oldWallet && oldWallet instanceof native.NativeHDWallet) return oldWallet;
Expand Down
10 changes: 4 additions & 6 deletions packages/hdwallet-portis/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ export class PortisAdapter {
return new PortisAdapter(keyring, args);
}

public async initialize(): Promise<void> { }

public async pairDevice(): Promise<core.HDWallet> {
public async pairDevice(): Promise<PortisHDWallet> {
try {
const wallet = await this.pairPortisDevice();
const wallet = await this.initialize();
this.portis.onActiveWalletChanged(async (wallAddr: string) => {
// check if currentDeviceId has changed
const walletAddress = "portis:" + wallAddr;
Expand All @@ -38,7 +36,7 @@ export class PortisAdapter {
if (currentDeviceId) {
this.keyring.delete(currentDeviceId);
}
this.pairPortisDevice();
await this.initialize();
}
});
this.portis.onLogout(() => {
Expand All @@ -55,7 +53,7 @@ export class PortisAdapter {
}
}

private async pairPortisDevice(): Promise<core.HDWallet> {
private async initialize(): Promise<PortisHDWallet> {
this.portis = new Portis(this.portisAppId, "mainnet");
const wallet = new PortisHDWallet(this.portis);
await wallet.initialize();
Expand Down
19 changes: 6 additions & 13 deletions packages/hdwallet-trezor-connect/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class TrezorAdapter extends EventEmitter2 {

public async addDevice(deviceID: string, path: string): Promise<void> {
this._deviceIDToPath.set(deviceID, path);
await this.initialize([{ path: path, deviceID: deviceID }]);
await this.initialize({ path: path, deviceID: deviceID });
}

public static useKeyring(keyring: core.Keyring, args: TrezorConnectArgs) {
Expand Down Expand Up @@ -142,14 +142,10 @@ export class TrezorAdapter extends EventEmitter2 {
this.connectCacheFeatures(event);
}

public async initialize(devices?: TrezorDevice[]): Promise<void> {
private async initialize(device: TrezorDevice): Promise<void> {
const init = await _initialization;
if (!init) throw new Error("Could not initialize TrezorAdapter: TrezorConnect not initialized");

const devicesToInitialize = devices || [];

for (let i = 0; i < devicesToInitialize.length; i++) {
const device = devicesToInitialize[i];
let wallet = this.keyring.get(device.deviceID);
if (wallet) {
if (device.path && !(wallet.transport as TrezorConnectTransport).device.path)
Expand All @@ -160,7 +156,6 @@ export class TrezorAdapter extends EventEmitter2 {

await wallet.initialize();
await this.keyring.add(wallet, device.deviceID);
}
}

public async pairDevice(): Promise<core.HDWallet> {
Expand All @@ -176,12 +171,10 @@ export class TrezorAdapter extends EventEmitter2 {

const deviceID = payload.device_id;

await this.initialize([
{
path: this._deviceIDToPath.get(deviceID),
deviceID: deviceID,
},
]);
await this.initialize({
path: this._deviceIDToPath.get(deviceID),
deviceID: deviceID,
});

const wallet = this.keyring.get(deviceID);
if (!(wallet instanceof trezor.TrezorHDWallet)) throw new Error("expected TrezorHDWallet");
Expand Down

0 comments on commit a7b8d10

Please sign in to comment.