import BN from "bn.js" ; import { BNString , IPoint , StringifiedType } from "../baseTypes/commonTypes" ; import { ecCurve } from "../utils" ; class Point implements IPoint { x : BN ; y : BN ; constructor ( x , y ) { this . x = new BN ( x , "hex" ) ; this . y = new BN ( y , "hex" ) ; } static fromCompressedPub ( value ) { const key = ecCurve . keyFromPublic ( value , "hex" ) ; const pt = key . getPublic ( ) ; return new Point ( pt . getX ( ) , pt . getY ( ) ) ; } static fromJSON ( value ) { const { x , y } = value ; return new Point ( x , y ) ; } encode ( enc , params ? ) { switch ( enc ) { case "arr" : return Buffer . concat ( [ Buffer . from ( "0x04" , "hex" ) , Buffer . from ( this . x . toString ( "hex" ) , "hex" ) , Buffer . from ( this . y . toString ( "hex" ) , "hex" ) ] ) ; case "elliptic-compressed" : { let ec = params ; ec = ecCurve ; const key = ec . keyFromPublic ( { x : this . x . toString ( "hex" ) , y : this . y . toString ( "hex" ) } , "hex" ) ; return Buffer . from ( key . getPublic ( true , "hex" ) ) ; } default : throw new Error ( "encoding doesnt exist in Point" ) ; } } toJSON ( ) { return { x : this . x . toString ( "hex" ) , y : this . y . toString ( "hex" ) , } ; } } export default Point ;