generalize ledger app name and allow customizing ledger app version

This commit is contained in:
Calvin Kei 2021-10-06 23:15:41 +08:00
parent edf7210898
commit b8ea9a6f08

View File

@ -32,6 +32,7 @@ export interface LaunchpadLedgerOptions {
readonly prefix?: string;
readonly testModeAllowed?: boolean;
readonly ledgerAppName?: string
readonly requiredLedgerAppVersion?: string
}
export class LaunchpadLedger {
@ -39,6 +40,7 @@ export class LaunchpadLedger {
private readonly hdPaths: readonly HdPath[];
private readonly prefix: string;
private readonly ledgerAppName: string;
private readonly requiredLedgerAppVersion: string;
private readonly app: CosmosApp;
public constructor(transport: Transport, options: LaunchpadLedgerOptions = {}) {
@ -46,19 +48,21 @@ export class LaunchpadLedger {
hdPaths: [cosmosHdPath],
prefix: cosmosBech32Prefix,
testModeAllowed: false,
ledgerAppName: cosmosLedgerAppName
ledgerAppName: cosmosLedgerAppName,
requiredLedgerAppVersion: requiredCosmosAppVersion
};
this.testModeAllowed = options.testModeAllowed ?? defaultOptions.testModeAllowed;
this.hdPaths = options.hdPaths ?? defaultOptions.hdPaths;
this.prefix = options.prefix ?? defaultOptions.prefix;
this.ledgerAppName = options.ledgerAppName ?? defaultOptions.ledgerAppName
this.requiredLedgerAppVersion = options.requiredLedgerAppVersion ?? defaultOptions.requiredLedgerAppVersion
this.app = new CosmosApp(transport);
}
public async getCosmosAppVersion(): Promise<string> {
await this.verifyCosmosAppIsOpen();
assert(this.app, "Cosmos Ledger App is not connected");
assert(this.app, `${this.ledgerAppName} Ledger App is not connected`);
const response = await this.app.getVersion();
this.handleLedgerErrors(response);
@ -70,7 +74,7 @@ export class LaunchpadLedger {
public async getPubkey(hdPath?: HdPath): Promise<Uint8Array> {
await this.verifyDeviceIsReady();
assert(this.app, "Cosmos Ledger App is not connected");
assert(this.app, `${this.ledgerAppName} Ledger App is not connected`);
const hdPathToUse = hdPath || this.hdPaths[0];
// ledger-cosmos-js hardens the first three indices
@ -94,7 +98,7 @@ export class LaunchpadLedger {
public async sign(message: Uint8Array, hdPath?: HdPath): Promise<Uint8Array> {
await this.verifyDeviceIsReady();
assert(this.app, "Cosmos Ledger App is not connected");
assert(this.app, `${this.ledgerAppName} Ledger App is not connected`);
const hdPathToUse = hdPath || this.hdPaths[0];
// ledger-cosmos-js hardens the first three indices
@ -105,12 +109,12 @@ export class LaunchpadLedger {
private verifyAppMode(testMode: boolean): void {
if (testMode && !this.testModeAllowed) {
throw new Error(`DANGER: The Cosmos Ledger app is in test mode and should not be used on mainnet!`);
throw new Error(`DANGER: The ${this.ledgerAppName} Ledger app is in test mode and should not be used on mainnet!`);
}
}
private async getOpenAppName(): Promise<string> {
assert(this.app, "Cosmos Ledger App is not connected");
assert(this.app, `${this.ledgerAppName} Ledger App is not connected`);
const response = await this.app.appInfo();
this.handleLedgerErrors(response);
@ -119,8 +123,8 @@ export class LaunchpadLedger {
private async verifyAppVersion(): Promise<void> {
const version = await this.getCosmosAppVersion();
if (!semver.gte(version, requiredCosmosAppVersion)) {
throw new Error("Outdated version: Please update Cosmos Ledger App to the latest version.");
if (!semver.gte(version, this.requiredLedgerAppVersion)) {
throw new Error(`Outdated version: Please update ${this.ledgerAppName} Ledger App to the latest version.`);
}
}
@ -128,7 +132,7 @@ export class LaunchpadLedger {
const appName = await this.getOpenAppName();
if (appName.toLowerCase() === `dashboard`) {
throw new Error(`Please open the Cosmos Ledger app on your Ledger device.`);
throw new Error(`Please open the ${this.ledgerAppName} Ledger app on your Ledger device.`);
}
if (appName.toLowerCase() !== this.ledgerAppName.toLowerCase()) {
throw new Error(`Please close ${appName} and open the ${this.ledgerAppName} Ledger app on your Ledger device.`);
@ -156,7 +160,7 @@ export class LaunchpadLedger {
case "U2F: Timeout":
throw new Error("Connection timed out. Please try again.");
case "Cosmos app does not seem to be open":
throw new Error("Cosmos app is not open");
throw new Error(`${this.ledgerAppName} app is not open`);
case "Command not allowed":
throw new Error("Transaction rejected");
case "Transaction rejected":
@ -165,7 +169,7 @@ export class LaunchpadLedger {
throw new Error("Ledgers screensaver mode is on");
case "Instruction not supported":
throw new Error(
`Your Cosmos Ledger App is not up to date. Please update to version ${requiredCosmosAppVersion}.`,
`Your ${this.ledgerAppName} Ledger App is not up to date. Please update to version ${this.requiredLedgerAppVersion}.`,
);
case "No errors":
break;