ens-usernames/app/utils/ecdsa.js

20 lines
570 B
JavaScript
Raw Normal View History

2018-08-10 23:48:44 +00:00
import { ec } from 'elliptic';
const EC = new ec('secp256k1');
export const generateXY = pub => {
const stripped = pub.slice(2);
const key = EC.keyFromPublic(stripped, 'hex');
const pubPoint = key.getPublic();
const x = '0x' + pubPoint.getX().toString(16);
const y = '0x'+ pubPoint.getY().toString(16);
return { x, y };
}
export const keyFromXY = (X, Y) => {
const x = Buffer.from(X.substring(2), 'hex');
const y = Buffer.from(Y.substring(2), 'hex');
const keys = EC.keyFromPublic({ x, y }, 'hex');
return `0x${keys.getPublic().encode('hex')}`;
}