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


TypeScript bcrypto.secp256k1類代碼示例

本文整理匯總了TypeScript中bcrypto.secp256k1的典型用法代碼示例。如果您正苦於以下問題:TypeScript secp256k1類的具體用法?TypeScript secp256k1怎麽用?TypeScript secp256k1使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: getAddressFromPrivateKey

  /**
   * get the public address of an account using its private key
   * @param {string} privateKey - the private key of the account
   * @returns {string} public address of the account
   */
  getAddressFromPrivateKey(privateKey): string {
    if (typeof(privateKey) == 'string') privateKey = new Buffer(privateKey, 'hex')

    let pubKey = secp256k1.publicKeyCreate(privateKey, true)
    let pubKeyHash = sha256.digest(pubKey) // sha256 hash of the public key
    let address = pubKeyHash.toString('hex', 12) // rightmost 160 bits/20 bytes of the hash

    return address
  }
開發者ID:JohanSweden,項目名稱:Zilliqa-Wallet,代碼行數:14,代碼來源:zilliqa.service.ts

示例2: createWallet

  /**
   * generate a new public-private keypair and use it to populate userWallet
   * @returns {string} the newly created private key
   */
  createWallet(): string {
    let key = secp256k1.generatePrivateKey()

    // account will be registered only when it receives ZIL
    this.userWallet = {
      address: this.getAddressFromPrivateKey(key),
      privateKey: key.toString('hex'),
      balance: 0,
      nonce: 0
    }

    return key.toString('hex')
  }
開發者ID:JohanSweden,項目名稱:Zilliqa-Wallet,代碼行數:17,代碼來源:zilliqa.service.ts

示例3: importWallet

  /**
   * import an account wallet from server using a private key
   * @param {string} privateKey - the private key of the account being imported
   * @returns {Promise} Promise object containing boolean - if imported successfully or not
   */
  importWallet(privateKey): Promise<any> {
    this.startLoading()
    var deferred = new $.Deferred()

    if (!!(privateKey.match(/[0-9a-fA-F]{64}/)) == false) {
      deferred.reject({
        error: 'Invalid private key.'
      })
      return deferred.promise()
    }

    if (typeof(privateKey) == 'string') privateKey = new Buffer(privateKey, 'hex')

    // check if private key valid
    try {
      if (secp256k1.privateKeyVerify(privateKey)) {
        let addr = this.getAddressFromPrivateKey(privateKey)

        // get balance from API
        let that = this
        this.node.getBalance({address: addr}, function(err, data) {
          if (err || data.error) {
            deferred.reject({error: err})
          } else {
            that.userWallet = {
              address: addr,
              balance: data.result.balance,
              nonce: data.result.nonce,
              privateKey: privateKey.toString('hex')
            }

            deferred.resolve({
              result: true
            })
          }
          that.endLoading()
        })
      } else {
        deferred.reject({
          error: 'Invalid private key.'
        })
        this.endLoading()
      }
    } catch (e) {
      deferred.reject({
        error: e
      })
      this.endLoading()
    }
    return deferred.promise()
  }
開發者ID:JohanSweden,項目名稱:Zilliqa-Wallet,代碼行數:56,代碼來源:zilliqa.service.ts

示例4: sendPayment

  /**
   * create a new transaction
   * @param {Object} payment - the details of the transaction
   * @param {string} payment.to - address to which transaction is sent to
   * @param {number} payment.amount - number of zils sent
   * @param {number} payment.gasPrice - gas price for this transaction
   * @param {number} payment.gasLimit - gas limit for this transaction
   * @returns {Promise} Promise object containing the newly created transaction id
   */
  sendPayment(payment: any): Promise<any> {
    this.startLoading()
    var deferred = new $.Deferred()

    let pubKey = secp256k1.publicKeyCreate(new Buffer(this.userWallet.privateKey, 'hex'), true)

    let txn = {
      version: 0,
      nonce: this.userWallet.nonce + 1,
      to: payment.address,
      amount: payment.amount,
      pubKey: pubKey.toString('hex'),
      gasPrice: payment.gasPrice,
      gasLimit: payment.gasLimit
    }

    var msg = this.intToByteArray(txn.version, 8).join('') +
              this.intToByteArray(txn.nonce, 64).join('') +
              txn.to +
              txn.pubKey +
              this.intToByteArray(txn.amount, 64).join('')

    let sig = this.zlib.schnorr.sign(new Buffer(msg, 'hex'), new Buffer(this.userWallet.privateKey, 'hex'), pubKey)
    let r = sig.r.toString('hex')
    let s = sig.s.toString('hex')
    while (r.length < 64) {
      r = '0' + r
    }
    while (s.length < 64) {
      s = '0' + s
    }
    txn['signature'] = r + s

    let that = this
    this.node.createTransaction(txn, function(err, data) {
      if (err || data.error) {
        deferred.reject(err)
      } else {
        deferred.resolve({
          txId: data.result
        })
      }
      that.endLoading()
    })

    return deferred.promise()
  }
開發者ID:JohanSweden,項目名稱:Zilliqa-Wallet,代碼行數:56,代碼來源:zilliqa.service.ts


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