Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getAvailableKeys, optional validation and key permission #38

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/Ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,16 +71,28 @@ export class Ledger extends Authenticator {
*
* @param accountName Account Name is an optional paramter
*/
public async login(accountName?: string): Promise<User[]> {

public async getAvailableKeys( requestPermission: boolean = false, indexArray: number[] = [0])
: Promise<string[]> {
return await signatureProvider.getAvailableKeys(requestPermission, indexArray)
}

public async login(
accountName?: string,
addressIndex: number = 0,
requestPermission: boolean = true,
validate: boolean = true): Promise<User[]> {
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)
}
Expand Down
3 changes: 2 additions & 1 deletion src/LedgerUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down