import { ec as EC , BNInput } from 'elliptic' ; import { derToJose , joseToDer } from 'ecdsa-sig-formatter' ; import { MissingParametersError } from '../errors' ; export class SECP256K1Client { static ec = new EC ( 'secp256k1' ) ; static algorithmName = 'ES256K' ; constructor ( ) { } static loadPrivateKey ( rawPrivateKey ) { if ( rawPrivateKey . length === 66 ) { rawPrivateKey = rawPrivateKey . slice ( 0 , 64 ) ; } return SECP256K1Client . ec . keyFromPrivate ( rawPrivateKey ) ; } static loadPublicKey ( rawPublicKey ) { return SECP256K1Client . ec . keyFromPublic ( rawPublicKey , 'hex' ) ; } static derivePublicKey ( privateKey , compressed = true ) { if ( typeof privateKey !== 'string' ) { throw Error ( 'private key must be a string' ) ; } if ( ! ( /^[0-9A-F]+$/i . test ( privateKey ) ) ) { throw Error ( 'private key must be a hex string' ) ; } if ( privateKey . length == 66 ) { privateKey = privateKey . slice ( 0 , 64 ) ; } else if ( privateKey . length <= 64 ) { } else { throw Error ( 'private key must be 66 characters or less' ) ; } const keypair = SECP256K1Client . ec . keyFromPrivate ( privateKey ) ; return keypair . getPublic ( compressed , 'hex' ) ; } static signHash ( signingInputHash , rawPrivateKey , format = 'jose' ) { if ( ! ( signingInputHash && rawPrivateKey ) ) { throw new MissingParametersError ( 'a signing input hash and private key are all required' ) ; } const privateKeyObject = SECP256K1Client . loadPrivateKey ( rawPrivateKey ) ; const signatureObject = privateKeyObject . sign ( signingInputHash ) ; const derSignature = Buffer . from ( signatureObject . toDER ( ) ) ; if ( format === 'der' ) { return derSignature . toString ( 'hex' ) ; } else if ( format === 'jose' ) { return derToJose ( derSignature , 'ES256' ) ; } else { throw Error ( 'Invalid signature format' ) ; } } static loadSignature ( joseSignature ) { return joseToDer ( joseSignature , 'ES256' ) ; } static verifyHash ( signingInputHash , derSignatureBuffer , rawPublicKey ) { if ( ! ( signingInputHash && derSignatureBuffer && rawPublicKey ) ) { throw new MissingParametersError ( 'a signing input hash, der signature, and public key are all required' ) ; } const publicKeyObject = SECP256K1Client . loadPublicKey ( rawPublicKey ) ; return publicKeyObject . verify ( signingInputHash , derSignatureBuffer as any ) ; } }