ensure proper XY coordinate generation from key for contact code

This commit is contained in:
Barry Gitarts 2018-11-06 12:57:57 -05:00 committed by Barry G
parent 02b9461d68
commit 6dc98d1db9
1 changed files with 12 additions and 12 deletions

View File

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