forked from MyEtherWallet/MEWconnect-web-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MewConnectCryptoMock.js
84 lines (73 loc) · 1.89 KB
/
MewConnectCryptoMock.js
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
import ethUtils from 'ethereumjs-utils';
import crypto from 'crypto';
import secp256k1 from 'secp256k1';
const privateStr =
'6d96ff74ad12467b3ebd993f6b0927d6996d464900bbd63de866b3090cb36d2b';
const pubStr =
'035892d28938ebe58339649fdaef273953b0b1f5d68bfb7422adf41d5993d9e20d';
export default class MewConnectCrypto {
static create() {
return new MewConnectCrypto();
}
setPrivate(pvtKey) {
this.prvt = Buffer.from(pvtKey, 'hex');
}
generateMessage() {
return crypto.randomBytes(32).toString('hex');
}
// Not for the Address, but generate them for the connection check
prepareKey() {
this.prvt = Buffer.from(privateStr, 'hex');
this.pub = Buffer.from(pubStr, 'hex');
return { pub: this.pub, pvt: this.prvt };
}
generatePrivate() {
return Buffer.from(privateStr, 'hex');
}
generatePublic() {
const pvt = Buffer.from(privateStr, 'hex');
this.prvt = pvt;
return secp256k1.publicKeyCreate(pvt);
}
encrypt(dataToSend) {
return new Promise(resolve => {
resolve(dataToSend);
});
}
decrypt(dataToSee) {
return new Promise(resolve => {
resolve(dataToSee);
});
}
signMessage(msgToSign) {
return new Promise((resolve, reject) => {
try {
const msg = ethUtils.hashPersonalMessage(ethUtils.toBuffer(msgToSign));
const signed = ethUtils.ecsign(
Buffer.from(msg),
Buffer.from(this.prvt, 'hex')
);
const combined = Buffer.concat([
Buffer.from([signed.v]),
Buffer.from(signed.r),
Buffer.from(signed.s)
]);
const combinedHex = combined.toString('hex');
resolve(combinedHex);
} catch (e) {
reject(e);
}
});
}
bufferToConnId(buf) {
return buf.toString('hex').slice(32);
}
isJSON(arg) {
try {
JSON.parse(arg);
return true;
} catch (e) {
return false;
}
}
}