text: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
      function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
      return new (P || (P = Promise))(function (resolve, reject) {
          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
          function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
          step((generator = generator.apply(thisArg, _arguments || [])).next());
      });
  };
  import TransportWebHid from '@ledgerhq/hw-transport-webhid';
  import { EventEmitter, WalletConnectionError, WalletDisconnectedError, WalletDisconnectionError, WalletNotConnectedError, WalletPublicKeyError, WalletSignatureError, } from '@solana/wallet-adapter-base';
  import { getDerivationPath, getPublicKey, signTransaction } from './util';
  export class LedgerWalletAdapter extends EventEmitter {
      constructor(config = {}) {
          super();
          this._disconnected = () => {
              const transport = this._transport;
              if (transport) {
                  transport.off('disconnect', this._disconnected);
                  this._transport = null;
                  this._publicKey = null;
                  this.emit('error', new WalletDisconnectedError());
                  this.emit('disconnect');
              }
          };
          this._derivationPath = config.derivationPath || getDerivationPath(0, 0);
          this._connecting = false;
          this._transport = null;
          this._publicKey = null;
      }
      get publicKey() {
          return this._publicKey;
      }
      get ready() {
          // @FIXME: could return !!navigator.hid
          return true;
      }
      get connecting() {
          return this._connecting;
      }
      get connected() {
          return !!this._transport;
      }
      get autoApprove() {
          return false;
      }
      connect() {
          return __awaiter(this, void 0, void 0, function* () {
              try {
                  if (this.connected || this.connecting)
                      return;
                  this._connecting = true;
                  let transport;
                  try {
                      transport = yield TransportWebHid.create();
                  }
                  catch (error) {
                      throw new WalletConnectionError(error === null || error === void 0 ? void 0 : error.message, error);
                  }
                  let publicKey;
                  try {
                      publicKey = yield getPublicKey(transport, this._derivationPath);
                  }
                  catch (error) {
                      throw new WalletPublicKeyError(error === null || error === void 0 ? void 0 : error.message, error);
                  }
                  transport.on('disconnect', this._disconnected);
                  this._transport = transport;
                  this._publicKey = publicKey;
                  this.emit('connect');
              }
              catch (error) {
                  this.emit('error', error);
                  throw error;
              }
              finally {
                  this._connecting = false;
              }
          });
      }
      disconnect() {
          return __awaiter(this, void 0, void 0, function* () {
              const transport = this._transport;
              if (transport) {
                  transport.off('disconnect', this._disconnected);
                  this._transport = null;
                  this._publicKey = null;
                  try {
                      yield transport.close();
                  }
                  catch (error) {
                      this.emit('error', new WalletDisconnectionError(error.message, error));
                  }
                  this.emit('disconnect');
              }
          });
      }
      signTransaction(transaction) {
          return __awaiter(this, void 0, void 0, function* () {
              try {
                  const transport = this._transport;
                  const publicKey = this._publicKey;
                  if (!transport || !publicKey)
                      throw new WalletNotConnectedError();
                  try {
                      const signature = yield signTransaction(transport, transaction, this._derivationPath);
                      transaction.addSignature(publicKey, signature);
                  }
                  catch (error) {
                      throw new WalletSignatureError(error === null || error === void 0 ? void 0 : error.message, error);
                  }
                  return transaction;
              }
              catch (error) {
                  this.emit('error', error);
                  throw error;
              }
          });
      }
      signAllTransactions(transactions) {
          return __awaiter(this, void 0, void 0, function* () {
              try {
                  const transport = this._transport;
                  const publicKey = this._publicKey;
                  if (!transport || !publicKey)
                      throw new WalletNotConnectedError();
                  try {
                      const derivationPath = this._derivationPath;
                      for (const transaction of transactions) {
                          const signature = yield signTransaction(transport, transaction, derivationPath);
                          transaction.addSignature(publicKey, signature);
                      }
                  }
                  catch (error) {
                      throw new WalletSignatureError(error === null || error === void 0 ? void 0 : error.message, error);
                  }
                  return transactions;
              }
              catch (error) {
                  this.emit('error', error);
                  throw error;
              }
          });
      }
  }
  //# sourceMappingURL=adapter.js.map