"use strict";const Tag=5,StatusCodes={ACCESS_CONDITION_NOT_FULFILLED:38916,ALGORITHM_NOT_SUPPORTED:38020,CLA_NOT_SUPPORTED:28160,CODE_BLOCKED:38976,CODE_NOT_INITIALIZED:38914,COMMAND_INCOMPATIBLE_FILE_STRUCTURE:27009,CONDITIONS_OF_USE_NOT_SATISFIED:27013,CONTRADICTION_INVALIDATION:38928,CONTRADICTION_SECRET_CODE_STATUS:38920,DEVICE_IN_RECOVERY_MODE:26159,CUSTOM_IMAGE_EMPTY:26158,FILE_ALREADY_EXISTS:27273,FILE_NOT_FOUND:37892,GP_AUTH_FAILED:25344,HALTED:28586,INCONSISTENT_FILE:37896,INCORRECT_DATA:27264,INCORRECT_LENGTH:26368,INCORRECT_P1_P2:27392,INS_NOT_SUPPORTED:27904,DEVICE_NOT_ONBOARDED:27911,DEVICE_NOT_ONBOARDED_2:26129,INVALID_KCV:38021,INVALID_OFFSET:37890,LICENSING:28482,LOCKED_DEVICE:21781,MAX_VALUE_REACHED:38992,MEMORY_PROBLEM:37440,MISSING_CRITICAL_PARAMETER:26624,NO_EF_SELECTED:37888,NOT_ENOUGH_MEMORY_SPACE:27268,OK:36864,PIN_REMAINING_ATTEMPTS:25536,REFERENCED_DATA_NOT_FOUND:27272,SECURITY_STATUS_NOT_SATISFIED:27010,TECHNICAL_PROBLEM:28416,UNKNOWN_APDU:27906,USER_REFUSED_ON_DEVICE:21761,NOT_ENOUGH_SPACE:20738,APP_NOT_FOUND_OR_INVALID_CONTEXT:20771,INVALID_APP_NAME_LENGTH:26378,GEN_AES_KEY_FAILED:21529,INTERNAL_CRYPTO_OPERATION_FAILED:21530,INTERNAL_COMPUTE_AES_CMAC_FAILED:21531,ENCRYPT_APP_STORAGE_FAILED:21532,INVALID_BACKUP_STATE:26178,PIN_NOT_SET:21762,INVALID_BACKUP_LENGTH:26419,INVALID_RESTORE_STATE:26179,INVALID_CHUNK_LENGTH:26420,INVALID_BACKUP_HEADER:26698};function concatUint8Array(...o){const t=o.reduce((i,r)=>i+r.byteLength,0),e=new Uint8Array(t);let n=0;for(const i of o)e.set(new Uint8Array(i),n),n+=i.byteLength;return e}function getCodeErrorMessage(o){const t=Object.entries(StatusCodes).find(([e,n])=>o===n);return`Error status code: ${t?t[0]+" / ":""}${o} / 0x${o.toString(16)}`}var w=Object.defineProperty,U=(o,t,e)=>t in o?w(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e,b=(o,t,e)=>(U(o,typeof t!="symbol"?t+"":t,e),e);const l={NONE:0,INFO:1,DEBUG:2};class LedgerTransport{constructor(t,e="NONE"){this.device=t,b(this,"logLevel"),b(this,"packetSize",64),b(this,"sendBuffer",[]),b(this,"inputs",{}),b(this,"inputCallback",{}),this.logLevel=l[e]??0,this.onInputReport=this.onInputReport.bind(this),this.onConnect=this.onConnect.bind(this),this.onDisconnect=this.onDisconnect.bind(this),t.addEventListener("inputreport",this.onInputReport),navigator.hid.addEventListener("connect",this.onConnect),navigator.hid.addEventListener("disconnect",this.onDisconnect)}close(){this.device.removeEventListener("inputreport",this.onInputReport),this.device.close(),navigator.hid.removeEventListener("connect",this.onConnect),navigator.hid.removeEventListener("disconnect",this.onDisconnect)}async send(t,e,n,i,r=new Uint8Array(0),I=[StatusCodes.OK]){const a=Promise.allSettled(this.sendBuffer.map(s=>s.result)),E=new Promise(s=>a.finally(async()=>{const _=await this._send(t,e,n,i,r,I),N=this.sendBuffer.findIndex(h=>h.cla===t);this.sendBuffer.splice(N,1),s(_)}));return this.sendBuffer.push({cla:t,ins:e,p1:n,p2:i,allowedStatusCodes:I,input:r,result:E}),E}async _send(t,e,n,i,r=new Uint8Array(0),I=[StatusCodes.OK]){const a=concatUint8Array(new Uint8Array([t,e,n,i,r.byteLength]),r),E=new DataView(new ArrayBuffer(2));E.setUint16(0,a.length,!1);const s=concatUint8Array(E.buffer,a),_=this.packetSize-5,N=Math.ceil(s.length/_),h=concatUint8Array(s,new Uint8Array(N*_-s.length+1)),A=[],T=Math.floor(Math.random()*65535);for(let c=0;c=l.INFO&&(console.log("%cMessage to hardware device","color:white;font-weight:bold"),this.logLevel>=l.DEBUG?console.log(`[SEND] > (channel ${T}): ${h.length} bytes`,[...h]):console.log(`[SEND] > (channel ${T}): ${h.length} bytes`));for(let c=0;c=l.DEBUG?console.log(`[RECV] < (channel ${e}): ${t.data.byteLength} bytes`,Array.from(new Uint8Array(t.data.buffer))):this.logLevel>=l.INFO&&console.log(`[RECV] < (channel ${e}): ${t.data.byteLength} bytes`),this.inputCallback[e]?(this.inputCallback[e](t),delete this.inputCallback[e]):(this.inputs[e]||(this.inputs[e]=[]),this.inputs[e].push(t))}async read(t){return this.inputs[t]?.length?this.inputs[t].shift():new Promise(e=>{this.inputCallback[t]=e})}async getResponse(t,e){let n=0,i=new Uint8Array,r=e;const I=async()=>{const s=await this.read(r),_=s.data.getUint16(0,!1),N=s.data.getUint8(2),h=s.data.getUint16(3,!1),A=n===0?7:5;n===0&&(n=s.data.getUint16(5,!1)),this.logLevel>=l.DEBUG&&console.log("response",{channel:_,tag:N,sequence:h,length:s.data.byteLength,dataLength:n,bytes:[...new Uint8Array(s.data.buffer)]}),i=concatUint8Array(i,s.data.buffer.slice(A))};for(await I();n>i.length;)await I();const a=i.slice(0,n),E=new DataView(a.buffer).getUint16(a.length-2);if(!t.includes(E))throw new Error(getCodeErrorMessage(E));return{code:E,data:a.slice(0,a.length-2)}}onConnect(t){this.logLevel>=l.DEBUG&&console.log("connect",t),this.device=t.device,t.device.addEventListener("inputreport",this.onInputReport)}onDisconnect(t){this.logLevel>=l.DEBUG&&console.log("disconnect",t),t.device.removeEventListener("inputreport",this.onInputReport)}}exports.LedgerTransport=LedgerTransport,exports.StatusCodes=StatusCodes;