-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathsign-expr.component.ts
136 lines (135 loc) · 4.96 KB
/
sign-expr.component.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Component, OnInit, SimpleChanges, Input, Output, EventEmitter, OnChanges } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { WalletService } from '../../services/wallet/wallet.service';
import { MessageService } from '../../services/message/message.service';
import { TranslateService } from '@ngx-translate/core';
import { Account } from '../../services/wallet/wallet';
import { OperationService } from '../../services/operation/operation.service';
import { emitMicheline } from '@taquito/michel-codec';
import { valueDecoder } from '@taquito/local-forging/dist/lib/michelson/codec';
import { Uint8ArrayConsumer } from '@taquito/local-forging/dist/lib/uint8array-consumer';
import { LedgerService } from '../../services/ledger/ledger.service';
import { InputValidationService } from '../../services/input-validation/input-validation.service';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-sign-expr',
templateUrl: './sign-expr.component.html',
styleUrls: ['./sign-expr.component.scss']
})
export class SignExprComponent implements OnInit, OnChanges {
@Input() signRequest: any;
@Input() activeAccount: Account;
@Output() signResponse = new EventEmitter();
syncSub: Subscription;
password = '';
pwdInvalid = '';
payload = '';
isMessage = false;
constructor(
public walletService: WalletService,
private messageService: MessageService,
public translate: TranslateService,
private operationService: OperationService,
private ledgerService: LedgerService,
private inputValidationService: InputValidationService
) { }
modalRef1: BsModalRef;
ngOnInit(): void {
}
ngOnChanges(changes: SimpleChanges): void {
if (this.signRequest) {
const scrollBarWidth = window.innerWidth - document.body.offsetWidth;
document.body.style.marginRight = scrollBarWidth.toString();
document.body.style.overflow = 'hidden';
this.isMessage = this.inputValidationService.isMessageSigning(this.signRequest.payload);
const value = valueDecoder(Uint8ArrayConsumer.fromHexString(this.signRequest.payload.slice(2)));
const payload = emitMicheline(value, { indent: ' ', newline: '\n' });
this.payload = this.isMessage ? value.string : payload;
this.syncSub = this.messageService.beaconResponse.subscribe((response) => {
if (response) {
this.signResponse.emit('silent');
this.closeModal();
}
});
}
}
async sign() {
if (this.walletService.isLedgerWallet()) {
this.requestLedgerSignature();
} else {
const pwd = this.password;
this.password = '';
await this.messageService.startSpinner(`Signing ${this.isMessage ? 'message' : 'payload'}...`);
let keys;
try {
keys = await this.walletService.getKeys(pwd, this.activeAccount.pkh);
} catch (e) {
console.warn(e);
this.messageService.stopSpinner();
}
if (keys) {
this.pwdInvalid = '';
try {
const signature = this.operationService.sign(this.signRequest.payload, keys.sk).edsig;
this.acceptSigning(signature);
} catch (e) {
this.pwdInvalid = 'Signing failed';
console.warn(e);
} finally {
this.messageService.stopSpinner();
}
} else {
this.messageService.stopSpinner();
if (this.walletService.isTorusWallet()) {
this.pwdInvalid = `Authorization failed`;
} else {
this.pwdInvalid = this.translate.instant('SENDCOMPONENT.WRONGPASSWORD');
}
}
}
}
async requestLedgerSignature() {
await this.messageService.startSpinner('Waiting for Ledger signature...');
try {
const payload = this.signRequest.payload;
let signature = '';
if (payload.length <= 2290) {
signature = await this.ledgerService.signOperation(payload, this.walletService.wallet.implicitAccounts[0].derivationPath);
} else {
signature = await this.ledgerService.signHash(this.operationService.ledgerPreHash(payload), this.walletService.wallet.implicitAccounts[0].derivationPath);
}
if (signature) {
this.acceptSigning(this.operationService.hexsigToEdsig(signature));
} else {
this.pwdInvalid = 'Failed to sign transaction';
}
} finally {
this.messageService.stopSpinner();
}
}
rejectSigning() {
this.closeModal();
this.signResponse.emit(null);
}
acceptSigning(signature: string) {
this.messageService.addSuccess(this.isMessage ? 'Message signed!' : 'Payload signed!');
this.closeModal();
this.signResponse.emit(signature);
}
closeModal() {
// restore body scrollbar
document.body.style.marginRight = '';
document.body.style.overflow = '';
this.clear();
}
clear() {
this.password = '';
this.pwdInvalid = '';
this.payload = '';
this.isMessage = false;
if (this.syncSub) {
this.syncSub.unsubscribe();
this.syncSub = undefined;
}
}
}