Hoist Ledger interface wrapError (#1638)
This commit is contained in:
@@ -22,12 +22,12 @@
|
||||
"react-native": "react-native.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.18.9",
|
||||
"@ledgerhq/hw-transport": "^6.27.3",
|
||||
"@ledgerhq/hw-transport-webhid": "^6.27.3",
|
||||
"@ledgerhq/hw-transport-webusb": "^6.27.3",
|
||||
"@ledgerhq/hw-transport": "^6.27.2",
|
||||
"@ledgerhq/hw-transport-webhid": "^6.27.2",
|
||||
"@ledgerhq/hw-transport-webusb": "^6.27.2",
|
||||
"@polkadot/util": "10.1.7-2-x"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@ledgerhq/hw-transport-node-hid-singleton": "^6.27.4"
|
||||
"@ledgerhq/hw-transport-node-hid-singleton": "^6.27.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.18.9",
|
||||
"@ledgerhq/hw-transport": "^6.27.3",
|
||||
"@ledgerhq/hw-transport": "^6.27.2",
|
||||
"@polkadot/hw-ledger-transports": "10.1.7-2-x",
|
||||
"@polkadot/util": "10.1.7-2-x",
|
||||
"@zondax/ledger-substrate": "^0.35.4"
|
||||
|
||||
@@ -17,6 +17,16 @@ export { packageInfo } from './packageInfo';
|
||||
|
||||
type Chain = keyof typeof ledgerApps;
|
||||
|
||||
async function wrapError <T extends ResponseBase> (promise: Promise<T>): Promise<T> {
|
||||
const result = await promise;
|
||||
|
||||
if (result.return_code !== LEDGER_SUCCESS_CODE) {
|
||||
throw new Error(result.error_message);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// A very basic wrapper for a ledger app -
|
||||
// - it connects automatically, creating an app as required
|
||||
// - Promises return errors (instead of wrapper errors)
|
||||
@@ -41,7 +51,7 @@ export class Ledger {
|
||||
|
||||
public async getAddress (confirm = false, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial<AccountOptions> = {}): Promise<LedgerAddress> {
|
||||
return this.#withApp(async (app: SubstrateApp): Promise<LedgerAddress> => {
|
||||
const { address, pubKey } = await this.#wrapError(app.getAddress(account + accountOffset, change, addressIndex + addressOffset, confirm));
|
||||
const { address, pubKey } = await wrapError(app.getAddress(account + accountOffset, change, addressIndex + addressOffset, confirm));
|
||||
|
||||
return {
|
||||
address,
|
||||
@@ -52,7 +62,7 @@ export class Ledger {
|
||||
|
||||
public async getVersion (): Promise<LedgerVersion> {
|
||||
return this.#withApp(async (app: SubstrateApp): Promise<LedgerVersion> => {
|
||||
const { device_locked: isLocked, major, minor, patch, test_mode: isTestMode } = await this.#wrapError(app.getVersion());
|
||||
const { device_locked: isLocked, major, minor, patch, test_mode: isTestMode } = await wrapError(app.getVersion());
|
||||
|
||||
return {
|
||||
isLocked,
|
||||
@@ -65,7 +75,7 @@ export class Ledger {
|
||||
public async sign (message: Uint8Array, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial<AccountOptions> = {}): Promise<LedgerSignature> {
|
||||
return this.#withApp(async (app: SubstrateApp): Promise<LedgerSignature> => {
|
||||
const buffer = u8aToBuffer(message);
|
||||
const { signature } = await this.#wrapError(app.sign(account + accountOffset, change, addressIndex + addressOffset, buffer));
|
||||
const { signature } = await wrapError(app.sign(account + accountOffset, change, addressIndex + addressOffset, buffer));
|
||||
|
||||
return {
|
||||
signature: `0x${signature.toString('hex')}`
|
||||
@@ -100,14 +110,4 @@ export class Ledger {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
#wrapError = async <T extends ResponseBase> (promise: Promise<T>): Promise<T> => {
|
||||
const result = await promise;
|
||||
|
||||
if (result.return_code !== LEDGER_SUCCESS_CODE) {
|
||||
throw new Error(result.error_message);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user