當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Keypair.fromPublicKey方法代碼示例

本文整理匯總了TypeScript中stellar-sdk.Keypair.fromPublicKey方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Keypair.fromPublicKey方法的具體用法?TypeScript Keypair.fromPublicKey怎麽用?TypeScript Keypair.fromPublicKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在stellar-sdk.Keypair的用法示例。


在下文中一共展示了Keypair.fromPublicKey方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: initiateRecovery

  /**
   * Generates Stellar keypairs from the user key and backup key
   * @param params
   *  - userKey: user keypair private key (encrypted or plaintext)
   *  - backupKey: backup keypair public key (plaintext) or private key (encrypted or plaintext)
   * @returns {stellar.Keypair[]} array of user and backup keypairs
   */
  initiateRecovery(params) {
    const keys = [];
    let userKey = params.userKey;
    let backupKey = params.backupKey;

    // Stellar's Ed25519 public keys start with a G, while private keys start with an S
    const isKrsRecovery = backupKey.startsWith('G') && !userKey.startsWith('G');
    const isUnsignedSweep = backupKey.startsWith('G') && userKey.startsWith('G');


    if (isKrsRecovery && _.isUndefined(config.krsProviders[params.krsProvider])) {
      throw new Error(`Unknown key recovery service provider - ${params.krsProvider}`);
    }

    if (isKrsRecovery && !config.krsProviders[params.krsProvider].supportedCoins.includes(this.getFamily())) {
      throw new Error(`Specified key recovery service does not support recoveries for ${this.getChain()}`);
    }

    if (!this.isValidAddress(params.recoveryDestination)) {
      throw new Error('Invalid destination address!');
    }

    try {
      if (!userKey.startsWith('S') && !userKey.startsWith('G')) {
        userKey = this.bitgo.decrypt({
          input: userKey,
          password: params.walletPassphrase
        });
      }

      const userKeyPair = isUnsignedSweep ? stellar.Keypair.fromPublicKey(userKey) : stellar.Keypair.fromSecret(userKey);
      keys.push(userKeyPair);
    } catch (e) {
      throw new Error('Failed to decrypt user key with passcode - try again!');
    }

    try {
      if (!backupKey.startsWith('S') && !isKrsRecovery && !isUnsignedSweep) {
        backupKey = this.bitgo.decrypt({
          input: backupKey,
          password: params.walletPassphrase
        });
      }

      if (isKrsRecovery || isUnsignedSweep) {
        keys.push(stellar.Keypair.fromPublicKey(backupKey));
      } else {
        keys.push(stellar.Keypair.fromSecret(backupKey));
      }
    } catch (e) {
      throw new Error('Failed to decrypt backup key with passcode - try again!');
    }

    return keys;
  }
開發者ID:BitGo,項目名稱:BitGoJS,代碼行數:62,代碼來源:xlm.ts

示例2: verifySignature

 /**
  * Verifies if signature for message is valid.
  *
  * @param pub {String} public key
  * @param message {Buffer|String} signed message
  * @param signature {Buffer} signature to verify
  * @returns {Boolean} true if signature is valid.
  */
 verifySignature(pub, message, signature) {
   if (!this.isValidPub(pub)) {
     throw new Error(`invalid pub: ${pub}`);
   }
   if (!Buffer.isBuffer(message)) {
     message = Buffer.from(message);
   }
   const keyPair = stellar.Keypair.fromPublicKey(pub);
   return keyPair.verify(message, signature);
 }
開發者ID:BitGo,項目名稱:BitGoJS,代碼行數:18,代碼來源:xlm.ts


注:本文中的stellar-sdk.Keypair.fromPublicKey方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。