<commit_before>/********************************************************************************
 *   Ledger Node JS API
 *   (c) 2017-2018 Ledger
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 ********************************************************************************/
//@flow

import type Transport from "@ledgerhq/hw-transport";
import {
  splitPath,
  foreach,
  encodeEd25519PublicKey,
  verifyEd25519Signature,
  checkStellarBip32Path,
  hash
} from "./utils";

const CLA = 0xe0;
const INS_GET_PK = 0x02;
const INS_SIGN_TX = 0x04;
const INS_GET_CONF = 0x06;
const INS_SIGN_TX_HASH = 0x08;

const APDU_MAX_SIZE = 150;
const P1_FIRST_APDU = 0x00;
const P1_MORE_APDU = 0x80;
const P2_LAST_APDU = 0x00;
const P2_MORE_APDU = 0x80;

const SW_OK = 0x9000;
const SW_CANCEL = 0x6985;
const SW_UNKNOWN_OP = 0x6c24;
const SW_MULTI_OP = 0x6c25;
const SW_SAFE_MODE = 0x6c66;
const SW_UNSUPPORTED = 0x6d00;

/**
 * Stellar API
 *
 * @example
 * import Str from "@ledgerhq/hw-app-str";
 * const str = new Str(transport)
 */
export default class Str {
  transport: Transport<*>;

  constructor(transport: Transport<*>) {
    this.transport = transport;
    transport.decorateAppAPIMethods(
      this,
      ["getAppConfiguration", "getPublicKey", "signTransaction", "signHash"],
      "l0v"
    );
  }

  getAppConfiguration(): Promise<{
    version: string
  }> {
    return this.transport.send(CLA, INS_GET_CONF, 0x00, 0x00).then(response => {
      let multiOpsEnabled = response[0] || response[1] < 0x02;
      let version = "" + response[1] + "." + response[2] + "." + response[3];
      return {
        version: version,
        multiOpsEnabled: multiOpsEnabled
      };
    });
  }

  /**
   * get Stellar public key for a given BIP 32 path.
   * @param path a path in BIP 32 format
   * @option boolValidate optionally enable key pair validation
   * @option boolDisplay optionally enable or not the display
   * @return an object with the publicKey
   * @example
   * str.getPublicKey("44'/148'/0'").then(o => o.publicKey)
   */
  getPublicKey(
    path: string,
    boolValidate?: boolean,
    boolDisplay?: boolean<commit_msg>report configuration setting added in Stellar app version 2.1<commit_after>/********************************************************************************
 *   Ledger Node JS API
 *   (c) 2017-2018 Ledger
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 ********************************************************************************/
//@flow

import type Transport from "@ledgerhq/hw-transport";
import {
  splitPath,
  foreach,
  encodeEd25519PublicKey,
  verifyEd25519Signature,
  checkStellarBip32Path,
  hash
} from "./utils";

const CLA = 0xe0;
const INS_GET_PK = 0x02;
const INS_SIGN_TX = 0x04;
const INS_GET_CONF = 0x06;
const INS_SIGN_TX_HASH = 0x08;

const APDU_MAX_SIZE = 150;
const P1_FIRST_APDU = 0x00;
const P1_MORE_APDU = 0x80;
const P2_LAST_APDU = 0x00;
const P2_MORE_APDU = 0x80;

const SW_OK = 0x9000;
const SW_CANCEL = 0x6985;
const SW_UNKNOWN_OP = 0x6c24;
const SW_MULTI_OP = 0x6c25;
const SW_SAFE_MODE = 0x6c66;
const SW_UNSUPPORTED = 0x6d00;

/**
 * Stellar API
 *
 * @example
 * import Str from "@ledgerhq/hw-app-str";
 * const str = new Str(transport)
 */
export default class Str {
  transport: Transport<*>;

  constructor(transport: Transport<*>) {
    this.transport = transport;
    transport.decorateAppAPIMethods(
      this,
      ["getAppConfiguration", "getPublicKey", "signTransaction", "signHash"],
      "l0v"
    );
  }

  getAppConfiguration(): Promise<{
    version: string
  }> {
    return this.transport.send(CLA, INS_GET_CONF, 0x00, 0x00).then(response => {
      let multiOpsEnabled = response[0] === 0x01 || response[1] < 0x02;
      let version = "" + response[1] + "." + response[2] + "." + response[3];
      return {
        version: version,
        multiOpsEnabled: multiOpsEnabled
      };
    });
  }

  /**
   * get Stellar public key for a given BIP 32 path.
   * @param path a path in BIP 32 format
   * @option boolValidate optionally enable key pair validation
   * @option boolDisplay optionally enable or not the display
   * @return an object with the publicKey
   * @example
   * str.getPublicKey("44'/148'/0'").then(o => o.publicKey)
   */
  getPublicKey(
    path: string,
    boolValidate?: boolean,
    boolDisplay?: boolean
{'fraction_non_alphanumeric': 0.15757124231700503, 'fraction_numerical': 0.04116222760290557, 'mean_word_length': 3.7606382978723403, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2337983', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1683, 'n_words': 508}