Refactor ensureConnected
This commit is contained in:
parent
d79dd01711
commit
53b20f5338
@ -81,25 +81,8 @@ export class LaunchpadLedger {
|
||||
}
|
||||
}
|
||||
|
||||
public async connect(timeout = defaultInteractionTimeout): Promise<LaunchpadLedger> {
|
||||
// assume good connection if connected once
|
||||
if (this.cosmosApp) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (this.platform !== "node") {
|
||||
verifyBrowserIsSupported(this.platform, this.userAgent);
|
||||
}
|
||||
|
||||
const transport = await this.createTransport(timeout * 1000);
|
||||
this.cosmosApp = new CosmosApp(transport);
|
||||
|
||||
await this.verifyDeviceIsReady();
|
||||
return this;
|
||||
}
|
||||
|
||||
public async getCosmosAppVersion(): Promise<string> {
|
||||
await this.connect();
|
||||
await this.ensureConnected();
|
||||
assert(this.cosmosApp, "Cosmos Ledger App is not connected");
|
||||
|
||||
const response = await this.cosmosApp.getVersion();
|
||||
@ -111,7 +94,7 @@ export class LaunchpadLedger {
|
||||
}
|
||||
|
||||
public async getPubkey(hdPath?: HdPath): Promise<Uint8Array> {
|
||||
await this.connect();
|
||||
await this.ensureConnected();
|
||||
assert(this.cosmosApp, "Cosmos Ledger App is not connected");
|
||||
|
||||
const hdPathToUse = hdPath || this.hdPaths[0];
|
||||
@ -135,7 +118,7 @@ export class LaunchpadLedger {
|
||||
}
|
||||
|
||||
public async sign(message: Uint8Array, hdPath?: HdPath): Promise<Uint8Array> {
|
||||
await this.connect();
|
||||
await this.ensureConnected();
|
||||
assert(this.cosmosApp, "Cosmos Ledger App is not connected");
|
||||
|
||||
const hdPathToUse = hdPath || this.hdPaths[0];
|
||||
@ -145,6 +128,22 @@ export class LaunchpadLedger {
|
||||
return Secp256k1Signature.fromDer((response as SignResponse).signature).toFixedLength();
|
||||
}
|
||||
|
||||
private async ensureConnected(timeout = defaultInteractionTimeout): Promise<void> {
|
||||
// assume good connection if connected once
|
||||
if (this.cosmosApp) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.platform !== "node") {
|
||||
verifyBrowserIsSupported(this.platform, this.userAgent);
|
||||
}
|
||||
|
||||
const transport = await this.createTransport(timeout * 1000);
|
||||
this.cosmosApp = new CosmosApp(transport);
|
||||
|
||||
await this.verifyDeviceIsReady();
|
||||
}
|
||||
|
||||
private async createTransport(timeout: number): Promise<Transport> {
|
||||
// HACK: Use a variable to get webpack to ignore this
|
||||
const nodeJsTransportPackageName = "@ledgerhq/hw-transport-node-hid";
|
||||
@ -189,7 +188,7 @@ export class LaunchpadLedger {
|
||||
}
|
||||
|
||||
private async getOpenAppName(): Promise<string> {
|
||||
await this.connect();
|
||||
await this.ensureConnected();
|
||||
assert(this.cosmosApp, "Cosmos Ledger App is not connected");
|
||||
|
||||
const response = await this.cosmosApp.appInfo();
|
||||
|
||||
@ -16,12 +16,12 @@ export declare class LaunchpadLedger {
|
||||
readonly platform: string;
|
||||
readonly userAgent: string | null;
|
||||
constructor(options?: LaunchpadLedgerOptions);
|
||||
connect(timeout?: number): Promise<LaunchpadLedger>;
|
||||
getCosmosAppVersion(): Promise<string>;
|
||||
getPubkey(hdPath?: HdPath): Promise<Uint8Array>;
|
||||
getPubkeys(): Promise<readonly Uint8Array[]>;
|
||||
getCosmosAddress(pubkey?: Uint8Array): Promise<string>;
|
||||
sign(message: Uint8Array, hdPath?: HdPath): Promise<Uint8Array>;
|
||||
private ensureConnected;
|
||||
private createTransport;
|
||||
private verifyAppMode;
|
||||
private getOpenAppName;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user