add ecdsa utils

This commit is contained in:
Barry Gitarts 2018-08-10 19:48:44 -04:00
parent c79e1c36ab
commit 0fde3e1c1c
1 changed files with 19 additions and 0 deletions

19
app/utils/ecdsa.js Normal file
View File

@ -0,0 +1,19 @@
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')}`;
}