-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetKeys.ts
49 lines (39 loc) · 1.2 KB
/
getKeys.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
import BigInteger from 'bigi';
import ecurve from 'ecurve';
const curve = ecurve.getCurveByName('secp256k1');
import { checkDecode, checkEncode } from './encrypt/key_utils';
import { FIO_CHAIN_NAME } from '../constants';
const fromBuffer = (buf: Buffer) => {
if (!Buffer.isBuffer(buf)) {
throw new Error('Expecting parameter to be a Buffer type');
}
if (buf.length === 33 && buf[32] === 1) {
// remove compression flag
buf = buf.slice(0, -1);
}
if (32 !== buf.length) {
throw new Error(`Expecting 32 bytes, instead got ${buf.length}`);
}
return BigInteger.fromBuffer(buf);
};
export const getPrivateKeyInt = ({
privateKey,
}: {
privateKey: string;
}): BigInteger => {
const versionKey = checkDecode({
keyString: privateKey,
keyType: 'sha256x2',
});
const privateKeyInt = fromBuffer(
Buffer.isBuffer(versionKey) ? versionKey.subarray(1) : versionKey
);
return privateKeyInt;
};
export const getPublicKey = ({ privateKey }: { privateKey: string }) => {
const privateKeyInt = getPrivateKeyInt({ privateKey });
const Q = curve.G.multiply(privateKeyInt);
const publicKey =
FIO_CHAIN_NAME + checkEncode({ keyBuffer: Q.getEncoded(true) });
return publicKey;
};