-
Notifications
You must be signed in to change notification settings - Fork 107
/
address-explorer.page.ts
101 lines (81 loc) · 2.7 KB
/
address-explorer.page.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { AirGapMarketWallet, MainProtocolSymbols } from '@airgap/coinlib-core'
import { Component, OnInit } from '@angular/core'
import { NavigationService } from 'src/app/services/navigation/navigation.service'
import * as bip32 from 'bip32'
import * as bs58check from 'bs58check'
// https://github.com/satoshilabs/slips/blob/master/slip-0132.md
class ExtendedPublicKey {
private readonly rawKey: Buffer
constructor(extendedPublicKey: string) {
this.rawKey = bs58check.decode(extendedPublicKey).slice(4)
}
toXpub() {
return this.addPrefix('0488b21e')
}
toYPub() {
return this.addPrefix('049d7cb2')
}
toZPub() {
return this.addPrefix('04b24746')
}
private addPrefix(prefix: string) {
const data = Buffer.concat([Buffer.from(prefix, 'hex'), this.rawKey])
return bs58check.encode(data)
}
}
interface AddressInfo {
address: string
derivationPath: string
isExpanded: boolean
}
@Component({
selector: 'airgap-address-explorer',
templateUrl: './address-explorer.page.html',
styleUrls: ['./address-explorer.page.scss']
})
export class AddressExplorerPage implements OnInit {
public wallet: AirGapMarketWallet
// public fingerprint: string = ''
public selectedTab: string = 'external'
public xpub: string = ''
public addresses: AddressInfo[] = []
public showChange: boolean = true
constructor(private readonly navigationService: NavigationService) {
this.wallet = this.navigationService.getState().wallet
}
async ngOnInit() {
if (this.wallet) {
// this.fingerprint = this.wallet.masterFingerprint
this.xpub = bip32.fromBase58(new ExtendedPublicKey(this.wallet.publicKey).toXpub()).toBase58()
this.clearAddresses()
if ((await this.wallet.protocol.getIdentifier()) === MainProtocolSymbols.ETH) {
this.showChange = false
}
}
}
segmentChanged(event: any) {
this.selectedTab = event.detail.value
this.clearAddresses()
}
public async clearAddresses() {
this.addresses = []
this.addAddresses()
}
public async loadData(event: any) {
await this.addAddresses()
event.target.complete()
}
public toggle(address: AddressInfo) {
address.isExpanded = !address.isExpanded
}
public async addAddresses() {
const nofAddresses = this.addresses.length
for (let i = 0; i < 20; i++) {
const visibilityIndex = this.selectedTab === 'external' ? 0 : 1
const index = nofAddresses + i
const address = (await this.wallet.protocol.getAddressFromExtendedPublicKey(this.xpub, visibilityIndex, index)).address
const derivationPath = `${this.wallet.derivationPath}/${visibilityIndex}/${index}`
this.addresses.push({ address, derivationPath, isExpanded: false })
}
}
}