GithubHelp home page GithubHelp logo

txref-conversion-js's Introduction

TXRef conversion javascript library

About

Javascript library for Bech32 Encoded TX References, ported from Jonas Schnelli's reference implementation for the Rebooting Web Of Trust's BTCR Hackathon. It uses Peter Wuille's Bech32 library for Bech32 encoding and decoding.

For more details, see the Bech32 Encoded Transaction Position References BIP.

This implementation works as follows:

  • Encoding:

    • Given a txid and chain (testnet/mainnet), fetch the block height and position from a blockchain explorer
    • Convert to a short id as shown in btc_txref_encode
  • Decoding:

    • Decodes the bech32-encoded txref
    • Extracts the block height and position as shown in btc_txref_decode
    • Find the txid corresponding to the blockheight and position from a blockchain explorer

This library is for prototype use only. Some future improvements would include:

  • Checking confirmation count from the API results; warn if less than 6 (or some other threshold).
  • Compare results from multiple blockchain explorer APIs
  • Flexible accessor if a local bitcoin node is available.
  • Robust error checking

You can use this as a node package or in a browser. The browserified script is available as txrefConverter-browserified.js.

Preview

You can experiment with this library in the BTCR TX Playground

Examples

In these examples, note the following:

  • Prefixes: mainnet tx refs start with the tx1 prefix, whereas testnet tx refs start with txtest1

Convert a TXID to a TXref

let txrefConverter = require('./txrefConverter');

txrefConverter.txidToTxref("016b71d9ec62709656504f1282bb81f7acf998df025e54bd68ea33129d8a425b", 
    txrefConverter.CHAIN_MAINNET)
  .then(result => {
    console.log(result); // expect "tx1:rk63-uqnf-z08h-t4q"
  });
  

Convert a TXref to a TXID

let txrefConverter = require('./txrefConverter');

txrefToTxid("tx1:rk63-uqnf-z08h-t4q", "mainnet")
  .then(result => {
    console.log(result)
  });

Expected output:

{
  txid: '016b71d9ec62709656504f1282bb81f7acf998df025e54bd68ea33129d8a425b',
  chain: 'mainnet',
  utxoIndex: 0
}

Finer grained functions

Given the chain, block height and position, encode as a TXref

Mainnet:

let txrefConverter = require('./txrefConverter');

let result = txrefConverter.txrefEncode("mainnet", 0, 0);
console.log(result); // expect "tx1:rqqq-qqqq-qygr-lgl"

Testnet:

let txrefConverter = require('./txrefConverter');

let result = txrefConverter.txrefEncode("testnet", 1152194, 1);
console.log(result); // expect "txtest1:xyv2-xzpq-q63z-7p4"

Given a TXref, extract the chain, block height and position

let txrefConverter = require('./txrefConverter');

let result = txrefConverter.txrefDecode('tx1:rzqq-qqqq-qhlr-5ct');
console.log(result);

// Expected: { blockHeight: 1, blockIndex: 0, chain: 'mainnet', utxoIndex: 0 }

Get transaction details

Given a txid and chain, lookup the transaction details:

let txrefConverter = require('./txrefConverter');

getTxDetails("f8cdaff3ebd9e862ed5885f8975489090595abe1470397f79780ead1c7528107", "testnet")
    .then(data => {
      console.log(data.numConfirmations); // and other transaction data obtained from the explorer
      var result = txrefEncode("testnet", data.blockHeight, data.blockIndex);
      return result
    }, error => {
      console.error(error);
    });

Install

npm install

Using in a browser

npm run build generates the browserified script txrefConverter-browserified.js, which you can include in your web project.

The following shows how you can use it:

<script src="./txrefConverter-browserified.js"></script>

txrefConverter.txidToTxref(txid, chain)
  .then(function (result, err) {
      // populate widget with result
});

See the BTCR playground code repository btcr-tx-playground for working code samples.

Running tests

npm run test

txref-conversion-js's People

Contributors

christophera avatar danpape avatar dependabot[bot] avatar kimdhamilton avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

txref-conversion-js's Issues

SLIP-0173 - Human readable prefixes

The prefixes for the Bech32 encoded txref's are tx for BTC Mainnet and txtest for BTC Testnet. Should these instead conform to the human readable parts in SLIP-0173 that is referred to in BIP-0173 that defines Bech32?
Changing them to bc for BTC Mainnet and tb for BTC Testnet would bring the code in line with the existing conventions.

How defaulting to testnet

I agree that the playground should default to testnet3.

However, I think what the playground should do is have a button "testnet3 txid" with a checkbox that is default on.

There should probably also be a static field just before it that just says "bitcoin"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.