-
Notifications
You must be signed in to change notification settings - Fork 69
/
EvmLedgerProvider.ts
71 lines (58 loc) · 2.55 KB
/
EvmLedgerProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import HwAppEthereum from '@ledgerhq/hw-app-eth';
import { Chain } from '@liquality/client';
import { UnimplementedMethodError } from '@liquality/errors';
import { EvmBaseWalletProvider } from '@liquality/evm';
import { LedgerProvider } from '@liquality/hw-ledger';
import { Address, Network } from '@liquality/types';
import { EvmLedgerSigner } from './EvmLedgerSigner';
import { EvmLedgerCreateOptions } from './types';
const defaultPath = "m/44'/60'/0'/0/0";
export class EvmLedgerProvider extends EvmBaseWalletProvider<StaticJsonRpcProvider, EvmLedgerSigner> {
private _ledgerSigner: EvmLedgerSigner;
private _ledgerProvider: LedgerProvider<HwAppEthereum>;
private _derivationPath: string;
constructor(walletOptions: EvmLedgerCreateOptions, chainProvider?: Chain<StaticJsonRpcProvider>) {
super(chainProvider);
this._ledgerProvider = new LedgerProvider<HwAppEthereum>({ ...walletOptions, App: HwAppEthereum });
this._derivationPath = walletOptions.derivationPath ? walletOptions.derivationPath + walletOptions.index : defaultPath;
this._ledgerSigner = new EvmLedgerSigner(this._ledgerProvider.getApp.bind(this._ledgerProvider), this._derivationPath);
if (chainProvider) {
this._ledgerSigner = this._ledgerSigner.connect(chainProvider.getProvider());
}
this.signer = this._ledgerSigner;
}
public async getAddress(): Promise<Address> {
const addresses = await this.getAddresses();
return addresses[0];
}
public async getUnusedAddress(): Promise<Address> {
return this.getAddress();
}
public async getUsedAddresses(): Promise<Address[]> {
return this.getAddresses();
}
public async getAddresses(): Promise<Address[]> {
const app = await this._ledgerProvider.getApp();
const address = await app.getAddress(this._derivationPath);
return [
new Address({
address: address.address,
derivationPath: this._derivationPath,
publicKey: address.publicKey,
}),
];
}
public async exportPrivateKey(): Promise<string> {
throw new UnimplementedMethodError('Method not supported.');
}
public async isWalletAvailable(): Promise<boolean> {
return Boolean(this.getAddress());
}
public async getConnectedNetwork(): Promise<Network> {
return this._ledgerProvider.getConnectedNetwork();
}
public canUpdateFee(): boolean {
return true;
}
}