How to use @liquality/utils - 10 common examples

To help you get started, we’ve selected a few @liquality/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github liquality / chainabstractionlayer / packages / ethereum-swap-provider / lib / EthereumSwapProvider.js View on Github external
createSwapScript (recipientAddress, refundAddress, secretHash, expiration) {
    recipientAddress = remove0x(addressToString(recipientAddress))
    refundAddress = remove0x(addressToString(refundAddress))

    const dataSizeBase = 112
    const redeemDestinationBase = 66
    const refundDestinationBase = 89
    const expirationHex = expiration.toString(16)
    const expirationEncoded = padHexStart(expirationHex) // Pad with 0
    const expirationSize = expirationEncoded.length / 2
    const expirationPushOpcode = (0x60 - 1 + expirationSize).toString(16)
    const redeemDestinationEncoded = (redeemDestinationBase + expirationSize).toString(16)
    const refundDestinationEncoded = (refundDestinationBase + expirationSize).toString(16)
    const dataSizeEncoded = (dataSizeBase + expirationSize).toString(16)

    return [
      // Constructor
      '60', dataSizeEncoded, // PUSH1 {dataSizeEncoded}
github liquality / chainabstractionlayer / packages / bitcoin-swap-provider / lib / BitcoinSwapProvider.js View on Github external
swapVout.txid = initiationTxHash
    swapVout.vSat = swapVout.value * 1e8

    if (swapVout.vSat - txfee < 0) {
      throw new Error('Transaction amount does not cover fee.')
    }

    const txb = new bitcoin.TransactionBuilder(network)

    if (!isRedeem) txb.setLockTime(expiration)

    const prevOutScript = paymentVariant.output

    txb.addInput(swapVout.txid, swapVout.n, 0, prevOutScript)
    txb.addOutput(addressToString(address), swapVout.vSat - txfee)

    const tx = txb.buildIncomplete()

    const isSegwit = paymentVariantName === 'p2wsh' || paymentVariantName === 'p2shSegwit'

    const sig = await this.getMethod('signP2SHTransaction')(
      initiationTxRaw, // TODO: Why raw? can't it be a bitcoinjs-lib TX like the next one?
      tx,
      address,
      swapVout,
      isSegwit ? swapPaymentVariants.p2wsh.redeem.output : swapPaymentVariants.p2sh.redeem.output,
      isRedeem ? 0 : expiration,
      isSegwit
    )

    const walletAddress = await this.getMethod('getWalletAddress')(address)
github liquality / chainabstractionlayer / packages / bitcoin-bitcore-rpc-provider / lib / BitcoinBitcoreRpcProvider.js View on Github external
createScript (address) {
    address = addressToString(address)
    const type = base58.decode(address).toString('hex').substring(0, 2).toUpperCase()
    const pubKeyHash = addressToPubKeyHash(address)
    if (type === networks.bitcoin_testnet.pubKeyHash) {
      return [
        '76', // OP_DUP
        'a9', // OP_HASH160
        '14', // data size to be pushed
        pubKeyHash, // 
        '88', // OP_EQUALVERIFY
        'ac' // OP_CHECKSIG
      ].join('')
    } else if (type === networks.bitcoin_testnet.scriptHash) {
      return [
        'a9', // OP_HASH160
        '14', // data size to be pushed
        pubKeyHash, // 
github liquality / chainabstractionlayer / packages / bitcoin-swap-provider / lib / BitcoinSwapProvider.js View on Github external
let blockNumber = startBlock || await this.getMethod('getBlockHeight')() // TODO: Are mempool TXs possible?
    let swapTransaction = null
    let arrivedAtTip = false
    while (!swapTransaction) {
      let block
      try {
        block = await this.getMethod('getBlockByNumber')(blockNumber, true)
      } catch (e) {
        arrivedAtTip = true
      }
      if (block) {
        swapTransaction = block.transactions.find(predicate)
        blockNumber++
      }
      if (arrivedAtTip) {
        await sleep(5000)
      }
    }
    return swapTransaction
  }
github liquality / chainabstractionlayer / packages / ethereum-metamask-provider / lib / EthereumMetaMaskProvider.js View on Github external
async signMessage (message) {
    const hex = Buffer.from(message).toString('hex')

    const addresses = await this.getAddresses()
    const address = addressToString(addresses[0])

    return this.metamask('personal_sign', ensure0x(hex), ensure0x(address))
  }
github liquality / chainabstractionlayer / packages / bitcoin-js-wallet-provider / lib / BitcoinJsWalletProvider.js View on Github external
}, BigNumber(0)).toNumber()

    const unusedAddress = await this.getUnusedAddress(true)
    const { inputs, change } = await this.getInputsForAmount(totalValue)

    if (change) {
      outputs.push({
        to: unusedAddress,
        value: change.value
      })
    }

    const txb = new bitcoin.TransactionBuilder(network)

    for (const output of outputs) {
      txb.addOutput(addressToString(output.to), output.value)
    }

    const prevOutScriptType = this.getScriptType()

    for (let i = 0; i < inputs.length; i++) {
      const wallet = await this.getWalletAddress(inputs[i].address)
      const keyPair = await this.keyPair(wallet.derivationPath)
      const paymentVariant = this.getPaymentVariantFromPublicKey(keyPair.publicKey)

      txb.addInput(inputs[i].txid, inputs[i].vout, 0, paymentVariant.output)
    }

    for (let i = 0; i < inputs.length; i++) {
      const wallet = await this.getWalletAddress(inputs[i].address)
      const keyPair = await this.keyPair(wallet.derivationPath)
      const paymentVariant = this.getPaymentVariantFromPublicKey(keyPair.publicKey)
github liquality / chainabstractionlayer / packages / ethereum-js-wallet-provider / lib / EthereumJsWalletProvider.js View on Github external
async buildTransaction (to, value, data) {
    const derivationPath = this._derivationPath + '0/0'
    const hdKey = await this.hdKey(derivationPath)

    const addresses = await this.getAddresses()
    const address = addresses[0]
    const from = addressToString(address)

    const txData = {
      to: to ? ensure0x(to) : null,
      from: ensure0x(from),
      value: ensure0x(BigNumber(value).toString(16)),
      data: data ? ensure0x(data) : undefined
    }

    const [ nonce, gasPrice, gasLimit ] = await Promise.all([
      this.getMethod('getTransactionCount')(remove0x(from)),
      this.getMethod('getGasPrice')(),
      this.getMethod('estimateGas')(txData)
    ])

    txData.nonce = nonce
    txData.gasPrice = gasPrice
github liquality / chainabstractionlayer / packages / ethereum-metamask-provider / lib / EthereumMetaMaskProvider.js View on Github external
async signMessage (message) {
    const hex = Buffer.from(message).toString('hex')

    const addresses = await this.getAddresses()
    const address = addressToString(addresses[0])

    return this.metamask('personal_sign', ensure0x(hex), ensure0x(address))
  }
github liquality / chainabstractionlayer / packages / bitcoin-bitcore-rpc-provider / lib / BitcoinBitcoreRpcProvider.js View on Github external
async getUnspentTransactions (address) {
    address = addressToString(address)
    return this.jsonrpc('getaddressutxos', { 'addresses': [address] })
  }
github liquality / chainabstractionlayer / packages / bitcoin-rpc-provider / lib / BitcoinRpcProvider.js View on Github external
async sendBatchTransaction (transactions) {
    let outputs = {}
    for (const tx of transactions) {
      outputs[addressToString(tx.to)] = BigNumber(tx.value).dividedBy(1e8).toNumber()
    }
    const rawTxOutputs = await this.createRawTransaction([], outputs)
    const rawTxFunded = await this.fundRawTransaction(rawTxOutputs)
    const rawTxSigned = await this.signRawTransaction(rawTxFunded.hex)
    return this.sendRawTransaction(rawTxSigned.hex)
  }

@liquality/utils

[![Build Status](https://travis-ci.com/liquality/chainabstractionlayer.svg?branch=master)](https://travis-ci.com/liquality/chainabstractionlayer) [![Coverage Status](https://coveralls.io/repos/github/liquality/chainabstractionlayer/badge.svg?branch=master

MIT
Latest version published 3 years ago

Package Health Score

52 / 100
Full package analysis

Similar packages