@xourse/tpay-transaction-helper

3.3.6 • Public • Published

TokenpayJS

js-standard-style

A javascript TokenPay library for node.js and browsers.

Released under the terms of the MIT LICENSE.

Installation

npm i @xourse/tpay-transaction-helper

Typically we support the Node Maintenance LTS version. If in doubt, see the .travis.yml for what versions are used by our continuous integration tests.

WARNING: We presently don't provide any tooling to verify that the release on npm matches GitHub. As such, you should verify anything downloaded by npm against your own verified copy.

Usage

TokenPay Transaction Example

import transactionHelper from '@xourse/tpay-transaction-helper';
import bitcore, { PrivateKey, PublicKey } from 'bitcore-lib';

const sendNetwork = {
    messagePrefix: '\x19TokenPay Signed Message:\n',
    bip32: {
      public: 0x0488b21e,
      private: 0x0488ade4,
    },
    pubKeyHash: 0x41,
    scriptHash: 0x7e,
    wif: 0xb3,
  };

  // array of privatekeys for all address involved in the utxos
  // in this case privatekey is an instance of bitcore.PrivateKey
  const privateKeys = [];

  // utxos array, single utxo for example:
  //{
  //  txid: "952131742ee54457f7038dc38f0fc826ba89af93cccbb0e315290f4e2592be16",
  //  vout: 1,
  //  amount: 0.0028,
  //  address: "sv6tQJHUhLiQkTRLNNvvjPksifupJ3AArJ"
  //}
  const utxos = [];

  const tx = new transactionHelper.TransactionBuilder(sendNetwork);
  tx.setVersion(1);
  // add main output, multiple if required
  tx.addOutput(<address string>, <satoshiAmount number>);
  // add inputs, as many as necesary
  tx.addInput(<utxo.txid string>, <utxo.vout number>);
  // choose fee and calculate change amount
  const fee = 0.0005 // for example
  const changeAmount = <totalInputsAmount> - <totalAmountToSend> - fee
  if(changeAmount > 0){
    const changeAddress = 'string';
    tx.addOutput(<changeAddress string>, <satoshiAmount number>);
  }
  // sign each input with the private key from the address which owns the utxo
  for (let j = 0; j < utxos.length; j++) {
    for (const privateKey of privateKeys) {
      if (utxos[j].address === privateKey.toAddress().toString()) {
        const tempPair = transactionHelper.ECPair.fromWIF(privateKey.toWIF(), sendNetwork);
        tx.sign(j, tempPair);
      }
    }
  }

  // build the transaction
  const hextosend = tx.build().toHex();

  // broadcast the hextosend

Complementing Libraries

  • BIP21 - A BIP21 compatible URL encoding library
  • BIP38 - Passphrase-protected private keys
  • BIP39 - Mnemonic generation for deterministic keys
  • BIP32-Utils - A set of utilities for working with BIP32
  • BIP66 - Strict DER signature decoding
  • BIP68 - Relative lock-time encoding library
  • BIP69 - Lexicographical Indexing of Transaction Inputs and Outputs
  • Base58 - Base58 encoding/decoding
  • Base58 Check - Base58 check encoding/decoding
  • Bech32 - A BIP173 compliant Bech32 encoding library
  • coinselect - A fee-optimizing, transaction input selection module for bitcoinjs-lib.
  • merkle-lib - A performance conscious library for merkle root and tree calculations.
  • minimaldata - A module to check bitcoin policy: SCRIPT_VERIFY_MINIMALDATA

Alternatives

LICENSE MIT

Dependencies (15)

Dev Dependencies (12)

Package Sidebar

Install

npm i @xourse/tpay-transaction-helper

Weekly Downloads

3

Version

3.3.6

License

MIT

Unpacked Size

121 kB

Total Files

48

Last publish

Collaborators

  • cerratoa