-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
89 lines (72 loc) · 2.92 KB
/
index.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
/* eslint-disable import/no-extraneous-dependencies */
import * as crypto from 'crypto';
import { ec as EC } from 'elliptic';
import BN from 'bn.js';
import ICryptoInterface, { ICryptoMathInterface } from 'abgp-js/dist/consensus/interfaces/ICryptoInterface';
const ec = new EC('secp256k1');
class CryptoMath implements ICryptoMathInterface {
addMod(hash1: string, hash2: string): string {
return new BN(hash1, 16).add(new BN(hash2, 16)).mod(ec.n).toString(16);
};
}
export default class implements ICryptoInterface {
public math: ICryptoMathInterface;
public constructor() {
this.math = new CryptoMath();
}
public async generatePrivateKey(): Promise<string> {
const node = crypto.createECDH('secp256k1');
node.generateKeys();
return node.getPrivateKey().toString('hex');
}
public async getPublicKey(privateKeyHex: string): Promise<string> {
const pg = ec.g.mul(privateKeyHex);
return this.pointToPublicKey(pg).toString('hex');
}
public async buildPartialSignature(privateKeyK: string, dataHash: string): Promise<string> {
return new BN(privateKeyK, 16)
.mul(new BN(dataHash, 16))
.mod(ec.n)
.toString(16);
}
public async buildSharedPublicKeyX(publicKeys: string[], hash: string): Promise<string> {
let X = null;
for (const publicKey of publicKeys) {
const XI = this.pubKeyToPoint(Buffer.from(publicKey, 'hex')).mul(new BN(hash, 16));
X = X === null ? XI : X.add(XI);
}
return this.pointToPublicKey(X).toString('hex');
}
public async buildSharedSignature(partialSignatures: string[]): Promise<string> {
let signature = new BN(0);
for (const sig of partialSignatures) {
signature = signature.add(new BN(sig, 16));
}
return signature.toString(16);
}
public async partialSignatureVerify(partialSignature: string, publicKeyHex: string, hash: string): Promise<boolean> {
const spG = ec.g.mul(partialSignature);
const check = this.pubKeyToPoint(Buffer.from(publicKeyHex, 'hex')).mul(hash);
return this.pointToPublicKey(spG).toString('hex') === this.pointToPublicKey(check).toString('hex');
}
public async verify(signature: string, sharedPublicKeyX: string): Promise<boolean> {
const sg = ec.g.mul(signature);
const check = this.pubKeyToPoint(Buffer.from(sharedPublicKeyX, 'hex'));
return this.pointToPublicKey(sg).toString('hex') === this.pointToPublicKey(check).toString('hex');
}
public hash(message: string): string {
return crypto.createHash('sha256')
.update(message)
.digest('hex');
}
private pubKeyToPoint(pubKey) {
const pubKeyEven = (pubKey[0] - 0x02) === 0;
return ec.curve.pointFromX(pubKey.slice(1, 33).toString('hex'), !pubKeyEven);
}
private pointToPublicKey(P): Buffer {
const buffer = Buffer.allocUnsafe(1);
// keep sign, if is odd
buffer.writeUInt8(P.getY().isEven() ? 0x02 : 0x03, 0);
return Buffer.concat([buffer, P.getX().toArrayLike(Buffer)]);
}
}