diff --git a/packages/ui-keyring/src/Keyring.ts b/packages/ui-keyring/src/Keyring.ts index 462a601a..5d088432 100644 --- a/packages/ui-keyring/src/Keyring.ts +++ b/packages/ui-keyring/src/Keyring.ts @@ -195,12 +195,17 @@ export class Keyring extends Base implements KeyringStruct { return; } - const address = this.encodeAddress( - isHex(json.address) - ? hexToU8a(json.address) - // FIXME Just for the transition period (ignoreChecksum) - : this.decodeAddress(json.address, true) - ); + // We assume anything hex that is not 32bytes (64 + 2 bytes hex) is an Ethereum-like address + // (this caters for both H160 addresses as well as full or compressed publicKeys) - in the case + // of both ecdsa and ethereum, we keep it as-is + const address = isHex(json.address) && json.address.length !== 66 + ? json.address + : this.encodeAddress( + isHex(json.address) + ? hexToU8a(json.address) + // FIXME Just for the transition period (ignoreChecksum) + : this.decodeAddress(json.address, true) + ); const [, hexAddr] = key.split(':'); this.addresses.add(this._store, address, json); diff --git a/packages/ui-keyring/src/ledger/index.ts b/packages/ui-keyring/src/ledger/index.ts index 4b7888f4..64b45840 100644 --- a/packages/ui-keyring/src/ledger/index.ts +++ b/packages/ui-keyring/src/ledger/index.ts @@ -46,7 +46,41 @@ export default class Ledger { this.#transport = transport; } - private async _getApp (): Promise { + public async getAddress (confirm = false, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial = {}): Promise { + return this.#withApp(async (app: SubstrateApp): Promise => { + const { address, pubKey } = await this.#wrapError(app.getAddress(account + accountOffset, change, addressIndex + addressOffset, confirm)); + + return { + address, + publicKey: `0x${pubKey}` + }; + }); + } + + public async getVersion (): Promise { + return this.#withApp(async (app: SubstrateApp): Promise => { + const { device_locked: isLocked, major, minor, patch, test_mode: isTestMode } = await this.#wrapError(app.getVersion()); + + return { + isLocked, + isTestMode, + version: [major, minor, patch] + }; + }); + } + + public async sign (message: Uint8Array, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial = {}): Promise { + return this.#withApp(async (app: SubstrateApp): Promise => { + const buffer = u8aToBuffer(message); + const { signature } = await this.#wrapError(app.sign(account + accountOffset, change, addressIndex + addressOffset, buffer)); + + return { + signature: u8aToHex(bufferToU8a(signature)) + }; + }); + } + + #getApp = async (): Promise => { if (!this.#app) { const def = transports.find(({ type }) => type === this.#transport); @@ -58,11 +92,11 @@ export default class Ledger { } return this.#app; - } + }; - private async _withApp (fn: (app: SubstrateApp) => Promise): Promise { + #withApp = async (fn: (app: SubstrateApp) => Promise): Promise => { try { - const app = await this._getApp(); + const app = await this.#getApp(); return await fn(app); } catch (error) { @@ -70,47 +104,13 @@ export default class Ledger { throw error; } - } + }; - private async _wrapError (promise: Promise): Promise { + #wrapError = async (promise: Promise): Promise => { const result = await promise; assert(result.return_code === SUCCESS_CODE, result.error_message); return result; - } - - public async getAddress (confirm = false, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial = {}): Promise { - return this._withApp(async (app: SubstrateApp): Promise => { - const { address, pubKey } = await this._wrapError(app.getAddress(account + accountOffset, change, addressIndex + addressOffset, confirm)); - - return { - address, - publicKey: `0x${pubKey}` - }; - }); - } - - public async getVersion (): Promise { - return this._withApp(async (app: SubstrateApp): Promise => { - const { device_locked: isLocked, major, minor, patch, test_mode: isTestMode } = await this._wrapError(app.getVersion()); - - return { - isLocked, - isTestMode, - version: [major, minor, patch] - }; - }); - } - - public async sign (message: Uint8Array, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial = {}): Promise { - return this._withApp(async (app: SubstrateApp): Promise => { - const buffer = u8aToBuffer(message); - const { signature } = await this._wrapError(app.sign(account + accountOffset, change, addressIndex + addressOffset, buffer)); - - return { - signature: u8aToHex(bufferToU8a(signature)) - }; - }); - } + }; }