diff --git a/src/Ledger.ts b/src/Ledger.ts index 2c2f864..c2100ab 100644 --- a/src/Ledger.ts +++ b/src/Ledger.ts @@ -13,6 +13,8 @@ import { LedgerOptions, Name } from './interfaces' import { ledgerLogo } from './ledgerLogo' import { LedgerUser } from './LedgerUser' import { UALLedgerError } from './UALLedgerError' +import { SignatureProvider } from 'eosjs-ledger-signature-provider' +const signatureProvider = new SignatureProvider() export class Ledger extends Authenticator { private onBoardingLink: string = CONSTANTS.onBoardingLink @@ -69,16 +71,28 @@ export class Ledger extends Authenticator { * * @param accountName Account Name is an optional paramter */ - public async login(accountName?: string): Promise { + + public async getAvailableKeys( requestPermission: boolean = false, indexArray: number[] = [0]) + : Promise { + return await signatureProvider.getAvailableKeys(requestPermission, indexArray) + } + + public async login( + accountName?: string, + addressIndex: number = 0, + requestPermission: boolean = true, + validate: boolean = true): Promise { for (const chain of this.chains) { - const user = new LedgerUser(chain, accountName, this.requiresGetKeyConfirmation(accountName)) - await user.init() - const isValid = await user.isAccountValid() - if (!isValid) { - const message = `Error logging into account "${accountName}"` - const type = UALErrorType.Login - const cause = null - throw new UALLedgerError(message, type, cause) + const user = new LedgerUser(chain, accountName, this.requiresGetKeyConfirmation(accountName) && requestPermission) + await user.init(addressIndex) + if (validate) { + const isValid = await user.isAccountValid() + if (!isValid) { + const message = `Error logging into account "${accountName}"` + const type = UALErrorType.Login + const cause = null + throw new UALLedgerError(message, type, cause) + } } this.users.push(user) } diff --git a/src/LedgerUser.ts b/src/LedgerUser.ts index 45e9d8d..94be8bc 100644 --- a/src/LedgerUser.ts +++ b/src/LedgerUser.ts @@ -33,8 +33,9 @@ export class LedgerUser extends User { } } - public async init() { + public async init(addressIndex: number = 0) { this.signatureProvider = new SignatureProvider() + this.signatureProvider.addressIndex = addressIndex const rpcEndpoint = this.chain.rpcEndpoints[0] const rpcEndpointString = `${rpcEndpoint.protocol}://${rpcEndpoint.host}:${rpcEndpoint.port}` this.rpc = new JsonRpc(rpcEndpointString)