当前位置: 首页>>代码示例>>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;未经允许,请勿转载。