import { BNString , decrypt as decryptUtils , encrypt as encryptUtils , EncryptedMessage , getPubKeyECC , IServiceProvider , PubKeyType , ServiceProviderArgs , StringifiedType , toPrivKeyEC , toPrivKeyECC , } from "@tkey/common-types" ; import BN from "bn.js" ; import { curve } from "elliptic" ; class ServiceProviderBase implements IServiceProvider { enableLogging : boolean ; postboxKey : BN ; serviceProviderName : string ; constructor ( { enableLogging = false , postboxKey } ) { this . enableLogging = enableLogging ; this . postboxKey = new BN ( postboxKey , "hex" ) ; this . serviceProviderName = "ServiceProviderBase" ; } static fromJSON ( value ) { const { enableLogging , postboxKey , serviceProviderName } = value ; if ( serviceProviderName !== "ServiceProviderBase" ) return undefined ; return new ServiceProviderBase ( { enableLogging , postboxKey } ) ; } async encrypt ( msg ) { const publicKey = this . retrievePubKey ( "ecc" ) ; return encryptUtils ( publicKey , msg ) ; } async decrypt ( msg ) { return decryptUtils ( toPrivKeyECC ( this . postboxKey ) , msg ) ; } retrievePubKeyPoint ( ) { return toPrivKeyEC ( this . postboxKey ) . getPublic ( ) ; } retrievePubKey ( type ) { if ( type === "ecc" ) { return getPubKeyECC ( this . postboxKey ) ; } throw new Error ( "Unsupported pub key type" ) ; } sign ( msg ) { const tmp = new BN ( msg , "hex" ) ; const sig = toPrivKeyEC ( this . postboxKey ) . sign ( tmp . toString ( "hex" ) ) ; return Buffer . from ( sig . r . toString ( 16 , 64 ) + sig . s . toString ( 16 , 64 ) + new BN ( 0 ) . toString ( 16 , 2 ) , "hex" ) . toString ( "base64" ) ; } toJSON ( ) { return { enableLogging : this . enableLogging , postboxKey : this . postboxKey . toString ( "hex" ) , serviceProviderName : this . serviceProviderName , } ; } } export default ServiceProviderBase ;