import { base64ToBin } from '../../format/format' ; import { CompressionFlag , ContextFlag , Secp256k1Wasm , } from './secp256k1-wasm-types' ; import { secp256k1Base64Bytes } from './secp256k1.base64' ; export { ContextFlag , CompressionFlag , Secp256k1Wasm } ; const wrapSecp256k1Wasm = ( instance , heapU8 , heapU32 ) : Secp256k1Wasm => ( { contextCreate : ( context ) => ( instance . exports as any ) . _secp256k1_context_create ( context ) , contextRandomize : ( contextPtr , seedPtr ) => ( instance . exports as any ) . _secp256k1_context_randomize ( contextPtr , seedPtr ) , free : ( pointer ) => ( instance . exports as any ) . _free ( pointer ) , heapU32 , heapU8 , instance , malloc : ( bytes ) => ( instance . exports as any ) . _malloc ( bytes ) , mallocSizeT : ( num ) => { const pointer = ( instance . exports as any ) . _malloc ( 4 ) ; const pointerView32 = pointer >> 2 ; heapU32 . set ( [ num ] , pointerView32 ) ; return pointer ; } , mallocUint8Array : ( array ) => { const pointer = ( instance . exports as any ) . _malloc ( array . length ) ; heapU8 . set ( array , pointer ) ; return pointer ; } , privkeyTweakAdd : ( contextPtr , secretKeyPtr , tweakNum256Ptr ) => ( instance . exports as any ) . _secp256k1_ec_privkey_tweak_add ( contextPtr , secretKeyPtr , tweakNum256Ptr ) , privkeyTweakMul : ( contextPtr , secretKeyPtr , tweakNum256Ptr ) => ( instance . exports as any ) . _secp256k1_ec_privkey_tweak_mul ( contextPtr , secretKeyPtr , tweakNum256Ptr ) , pubkeyCreate : ( contextPtr , publicKeyPtr , secretKeyPtr ) => ( instance . exports as any ) . _secp256k1_ec_pubkey_create ( contextPtr , publicKeyPtr , secretKeyPtr ) , pubkeyParse : ( contextPtr , publicKeyOutPtr , publicKeyInPtr , publicKeyInLength ) => ( instance . exports as any ) . _secp256k1_ec_pubkey_parse ( contextPtr , publicKeyOutPtr , publicKeyInPtr , publicKeyInLength ) , pubkeySerialize : ( contextPtr , outputPtr , outputLengthPtr , publicKeyPtr , compression ) => ( instance . exports as any ) . _secp256k1_ec_pubkey_serialize ( contextPtr , outputPtr , outputLengthPtr , publicKeyPtr , compression ) , pubkeyTweakAdd : ( contextPtr , publicKeyPtr , tweakNum256Ptr ) => ( instance . exports as any ) . _secp256k1_ec_pubkey_tweak_add ( contextPtr , publicKeyPtr , tweakNum256Ptr ) , pubkeyTweakMul : ( contextPtr , publicKeyPtr , tweakNum256Ptr ) => ( instance . exports as any ) . _secp256k1_ec_pubkey_tweak_mul ( contextPtr , publicKeyPtr , tweakNum256Ptr ) , readHeapU8 : ( pointer , bytes ) => new Uint8Array ( heapU8 . buffer , pointer , bytes ) , readSizeT : ( pointer ) => { const pointerView32 = pointer >> 2 ; return heapU32 [ pointerView32 ] ; } , recover : ( contextPtr , outputPubkeyPointer , rSigPtr , msg32Ptr ) => ( instance . exports as any ) . _secp256k1_ecdsa_recover ( contextPtr , outputPubkeyPointer , rSigPtr , msg32Ptr ) , recoverableSignatureParse : ( contextPtr , outputRSigPtr , inputSigPtr , rid ) => ( instance . exports as any ) . _secp256k1_ecdsa_recoverable_signature_parse_compact ( contextPtr , outputRSigPtr , inputSigPtr , rid ) , recoverableSignatureSerialize : ( contextPtr , sigOutPtr , recIDOutPtr , rSigPtr ) => ( instance . exports as any ) . _secp256k1_ecdsa_recoverable_signature_serialize_compact ( contextPtr , sigOutPtr , recIDOutPtr , rSigPtr ) , schnorrSign : ( contextPtr , outputSigPtr , msg32Ptr , secretKeyPtr ) => ( instance . exports as any ) . _secp256k1_schnorr_sign ( contextPtr , outputSigPtr , msg32Ptr , secretKeyPtr ) , schnorrVerify : ( contextPtr , sigPtr , msg32Ptr , publicKeyPtr ) => ( instance . exports as any ) . _secp256k1_schnorr_verify ( contextPtr , sigPtr , msg32Ptr , publicKeyPtr ) , seckeyVerify : ( contextPtr , secretKeyPtr ) => ( instance . exports as any ) . _secp256k1_ec_seckey_verify ( contextPtr , secretKeyPtr ) , sign : ( contextPtr , outputSigPtr , msg32Ptr , secretKeyPtr ) => ( instance . exports as any ) . _secp256k1_ecdsa_sign ( contextPtr , outputSigPtr , msg32Ptr , secretKeyPtr ) , signRecoverable : ( contextPtr , outputRSigPtr , msg32Ptr , secretKeyPtr ) => ( instance . exports as any ) . _secp256k1_ecdsa_sign_recoverable ( contextPtr , outputRSigPtr , msg32Ptr , secretKeyPtr ) , signatureMalleate : ( contextPtr , outputSigPtr , inputSigPtr ) => ( instance . exports as any ) . _secp256k1_ecdsa_signature_malleate ( contextPtr , outputSigPtr , inputSigPtr ) , signatureNormalize : ( contextPtr , outputSigPtr , inputSigPtr ) => ( instance . exports as any ) . _secp256k1_ecdsa_signature_normalize ( contextPtr , outputSigPtr , inputSigPtr ) , signatureParseCompact : ( contextPtr , sigOutPtr , compactSigInPtr ) => ( instance . exports as any ) . _secp256k1_ecdsa_signature_parse_compact ( contextPtr , sigOutPtr , compactSigInPtr ) , signatureParseDER : ( contextPtr , sigOutPtr , sigDERInPtr , sigDERInLength ) => ( instance . exports as any ) . _secp256k1_ecdsa_signature_parse_der ( contextPtr , sigOutPtr , sigDERInPtr , sigDERInLength ) , signatureSerializeCompact : ( contextPtr , outputCompactSigPtr , inputSigPtr ) => ( instance . exports as any ) . _secp256k1_ecdsa_signature_serialize_compact ( contextPtr , outputCompactSigPtr , inputSigPtr ) , signatureSerializeDER : ( contextPtr , outputDERSigPtr , outputDERSigLengthPtr , inputSigPtr ) => ( instance . exports as any ) . _secp256k1_ecdsa_signature_serialize_der ( contextPtr , outputDERSigPtr , outputDERSigLengthPtr , inputSigPtr ) , verify : ( contextPtr , sigPtr , msg32Ptr , pubkeyPtr ) => ( instance . exports as any ) . _secp256k1_ecdsa_verify ( contextPtr , sigPtr , msg32Ptr , pubkeyPtr ) , } ) ; /**
 * Method extracted from Emscripten's preamble.js
 */ const isLittleEndian = ( buffer ) : boolean => { const littleEndian = true ; const notLittleEndian = false ; const heap16 = new Int16Array ( buffer ) ; const heap32 = new Int32Array ( buffer ) ; const heapU8 = new Uint8Array ( buffer ) ; heap32 [ 0 ] = 1668509029 ; heap16 [ 1 ] = 25459 ; return heapU8 [ 2 ] !== 115 || heapU8 [ 3 ] !== 99 ? notLittleEndian : littleEndian ; } ; /**
 * Method derived from Emscripten's preamble.js
 */ const alignMemory = ( factor , size ) => Math . ceil ( size / factor ) * factor ; param webassemblyBytes export const instantiateSecp256k1WasmBytes = async ( webassemblyBytes ) : Promise < Secp256k1Wasm > => { const STACK_ALIGN = 16 ; const GLOBAL_BASE = 1024 ; const WASM_PAGE_SIZE = 65536 ; const TOTAL_STACK = 5242880 ; const TOTAL_MEMORY = 16777216 ; const wasmMemory = new WebAssembly . Memory ( { initial : TOTAL_MEMORY / WASM_PAGE_SIZE , maximum : TOTAL_MEMORY / WASM_PAGE_SIZE , } ) ; if ( ! isLittleEndian ( wasmMemory . buffer ) ) { throw new Error ( 'Runtime error: expected the system to be little-endian.' ) ; } const STATIC_BASE = GLOBAL_BASE ; const STATICTOP_INITIAL = STATIC_BASE + 67696 + 16 ; const DYNAMICTOP_PTR = STATICTOP_INITIAL ; const DYNAMICTOP_PTR_SIZE = 4 ; const STATICTOP = ( STATICTOP_INITIAL + DYNAMICTOP_PTR_SIZE + 15 ) & - 16 ; const STACKTOP = alignMemory ( STACK_ALIGN , STATICTOP ) ; const STACK_BASE = STACKTOP ; const STACK_MAX = STACK_BASE + TOTAL_STACK ; const DYNAMIC_BASE = alignMemory ( STACK_ALIGN , STACK_MAX ) ; const heapU8 = new Uint8Array ( wasmMemory . buffer ) ; const heap32 = new Int32Array ( wasmMemory . buffer ) ; const heapU32 = new Uint32Array ( wasmMemory . buffer ) ; heap32 [ DYNAMICTOP_PTR >> 2 ] = DYNAMIC_BASE ; const TABLE_SIZE = 6 ; const MAX_TABLE_SIZE = 6 ; let getErrNoLocation ; const env = { DYNAMICTOP_PTR , STACKTOP , ___setErrNo : ( value ) => { if ( getErrNoLocation !== undefined ) { heap32 [ getErrNoLocation ( ) >> 2 ] = value ; } return value ; } , _abort : ( err = 'Secp256k1 Error' ) => { throw new Error ( err ) ; } , _emscripten_memcpy_big : ( dest , src , num ) => { heapU8 . set ( heapU8 . subarray ( src , src + num ) , dest ) ; return dest ; } , abort : ( err = 'Secp256k1 Error' ) => { throw new Error ( err ) ; } , abortOnCannotGrowMemory : ( ) => { throw new Error ( 'Secp256k1 Error: abortOnCannotGrowMemory was called.' ) ; } , enlargeMemory : ( ) => { throw new Error ( 'Secp256k1 Error: enlargeMemory was called.' ) ; } , getTotalMemory : ( ) => TOTAL_MEMORY , } ; const info = { env : { ... env , memory : wasmMemory , memoryBase : STATIC_BASE , table : new WebAssembly . Table ( { element : 'anyfunc' , initial : TABLE_SIZE , maximum : MAX_TABLE_SIZE , } ) , tableBase : 0 , } , global : { Infinity , NaN } , } ; return WebAssembly . instantiate ( webassemblyBytes , info ) . then ( ( result ) => { getErrNoLocation = result . instance . exports . ___errno_location as any ; return wrapSecp256k1Wasm ( result . instance , heapU8 , heapU32 ) ; } ) ; } ; export const getEmbeddedSecp256k1Binary = ( ) => base64ToBin ( secp256k1Base64Bytes ) . buffer ; /**
 * An ultimately-portable (but slower) version of `instantiateSecp256k1Bytes`
 * which does not require the consumer to provide the secp256k1 binary buffer.
 */ export const instantiateSecp256k1Wasm = async ( ) : Promise < Secp256k1Wasm > => instantiateSecp256k1WasmBytes ( getEmbeddedSecp256k1Binary ( ) ) ;