import { ecdsa , jwk , parse } from './settings' ; import { sha256 } from './sha256' ; import { crypto } from './shims' ; const DEFAULT_OPTS = { encode : 'base64' } ; function importKey ( pub ) { const token = jwk ( pub ) ; const promise = crypto . subtle . importKey ( 'jwk' , token , ecdsa . pair , false , [ 'verify' ] ) ; return promise ; } export async function verifyHashSignature ( hash , signature , pub , opt = DEFAULT_OPTS ) { const encoding = opt . encode || DEFAULT_OPTS . encode ; const key = await importKey ( pub ) ; const buf = Buffer . from ( signature , encoding ) ; const sig = new Uint8Array ( buf ) ; if ( await crypto . subtle . verify ( ecdsa . sign , key , sig , new Uint8Array ( Buffer . from ( hash , 'hex' ) ) ) ) { return true ; } return false ; } export async function verifySignature ( text , signature , pub , opt = DEFAULT_OPTS ) { const hash = await sha256 ( typeof text === 'string' ? text : JSON . stringify ( text ) ) ; return verifyHashSignature ( hash . toString ( 'hex' ) , signature , pub , opt ) ; } export async function verify ( data , pub , opt = DEFAULT_OPTS ) { const json = parse ( data ) ; if ( await verifySignature ( json . m , json . s , pub , opt ) ) { return true ; } if ( opt . fallback ) { return oldVerify ( data , pub , opt ) ; } return false ; } export async function oldVerify ( _data , _pub , _opt = DEFAULT_OPTS ) { throw new Error ( 'Legacy fallback validation not yet supported' ) ; }