Instantly share code, notes, and snippets.

Embed
What would you like to do?

Bitcoin Hackathon # 2 Hands-on for BITBOX Dev.

This hands-on will be divided in to 2 parts. In the first part you'll learn BITBOX.

In the second part you'll learn how to create tokens w/ Wormhole.

BITBOX

BITBOX is a fully featured javascript framework which allows developers to begin interacting with the Bitcoin Cash blockchain. It offers Mnemonics, HDNodes, ECPairs, Crypto, Addresses, Transactions and much more.

Prerequisites

Install NodeJS

Install v8.12.x from the NodeJS homepage. This includes npm which is the "Node Package Manager."

Install bitbox-cli

npm install bitbox-cli --global
bitbox --version
1.6.5

Create a new React app

We have scaffolds which let you bootstrap a $BCH app in under a minute in react, angular, node, next and vue.

bitbox new hackathon --scaffold react
cd hackathon
npm install
bitbox console

Mnemonic

128–256 BIP39 Mnemonics in english, spanish, french, italian, korean, japanese and chinese traditional/simplified.

// 12 word english
BITBOX.Mnemonic.generate(128);
// 'image wave furnace below ring undo ship cable then swarm unit teach'

// 256 bit hiragana
let mnemonic = Wormhole.Mnemonic.generate(
  256,
  Wormhole.Mnemonic.wordLists().japanese
);
// 'びんぼう ぬいくぎ たいわん やすい やめる たいる しゃおん ふじみ てほどき あんぜん ふとん しねま ばいか はおる てれび すくない りこう いこく もうしあげる うろこ つける ぬかす いれい ぜんあく'

BIP44 HDNode

BIP44 HDNodes

// root seed buffer
let rootSeed = Wormhole.Mnemonic.toSeed(mnemonic);

// master HDNode
let masterHDNode = Wormhole.HDNode.fromSeed(rootSeed, "bitcoincash");

// HDNode of BIP44 account
let account = Wormhole.HDNode.derivePath(masterHDNode, "m/44'/145'/0'");

// derive the first external change address HDNode
let change = Wormhole.HDNode.derivePath(account, "0/0");

More Info

Address

// get the cash address
let cashAddress = Wormhole.HDNode.toCashAddress(change);
// 'bitcoincash:qzfrquuztqp86zp0qct7ga2g35zfh0wwjyv6h4eyw9'

// cashaddr to legacy
let legacyAddress = Wormhole.Address.toLegacyAddress(cashAddress);
// '1EKyhTTYRPcoQYBRBPcX59yrWuEvs92UCf'

// detect legacy
Wormhole.Address.isLegacyAddress(legacyAddress);
// true

// detect cashaddr
Wormhole.Address.isCashAddress(legacyAddress);
// false

// detect mainnet
Wormhole.Address.isMainnetAddress(legacyAddress);
// true

// detect testnet
Wormhole.Address.isTestnetAddress(legacyAddress);
// false

// detect p2pkh
Wormhole.Address.isP2PKHAddress(legacyAddress);
// true

// detect p2sh
Wormhole.Address.isP2SHAddress(legacyAddress);
// false

// detect format
Wormhole.Address.detectAddressFormat(legacyAddress);
// 'legacy'

// detect network
Wormhole.Address.detectAddressNetwork(legacyAddress);
// 'mainnet'

// detect type
Wormhole.Address.detectAddressType(legacyAddress);
// 'p2pkh'

You can also get the details such as balance and list of utxo for an address

(async () => {
  try {
    let details = await Wormhole.Address.details([
      "1BFHGm4HzqgXXyNX8n7DsQno5DAC4iLMRA"
    ]);
    console.log(details);
    // [{
    //   "addrStr": "1BFHGm4HzqgXXyNX8n7DsQno5DAC4iLMRA",
    //   "balance": 0.36781097,
    //   "balanceSat": 36781097,
    //   "totalReceived": 0.36781097,
    //   "totalReceivedSat": 36781097,
    //   "totalSent": 0,
    //   "totalSentSat": 0,
    //   "unconfirmedBalance": 0,
    //   "unconfirmedBalanceSat": 0,
    //   "unconfirmedTxApperances": 0,
    //   "txApperances": 7,
    //   "transactions": [
    //     "f737485aaee3c10b13013fa109bb6294b099246134ca9885f4cc332dbc6c9bb4",
    //     "decd5b9c0c959e4e543182093e8f7f8bc7a6ecd96a8a062daaeff3667f8feca7",
    //     "94e69a627a34ae27fca81d15fff4323a7ce1f7c275c7485762ce018221017632",
    //     "e67c70787af7f3506263c9eda007f3d2d24bd750ff95b5c50a120d9118dfd807",
    //     "8e5e00704a147d54028f94d52df7730e821b9c6cd4bd29494e5636f49c199d6a",
    //     "15102827c108566ea5daf725c09079c1a3f42ef99d1eb68ea8c584f7b16ab87a",
    //     "cc27be8846276612dfce5924b7be96556212f0f0e62bd17641732175edb9911e"
    //   ]
    // }]
    let utxo = await Wormhole.Address.utxo([
      "1BFHGm4HzqgXXyNX8n7DsQno5DAC4iLMRA"
    ]);
    console.log(utxo);
    // [[
    //   {
    //     "address": "1BFHGm4HzqgXXyNX8n7DsQno5DAC4iLMRA",
    //     "txid": "f737485aaee3c10b13013fa109bb6294b099246134ca9885f4cc332dbc6c9bb4",
    //     "vout": 0,
    //     "scriptPubKey": "76a9147064aa0c2de4e79e6c0a1290f769e40b3dda1b8e88ac",
    //     "amount": 0.299,
    //     "satoshis": 29900000,
    //     "height": 528796,
    //     "confirmations": 16
    //   },
    //   {
    //     "address": "1BFHGm4HzqgXXyNX8n7DsQno5DAC4iLMRA",
    //     "txid": "decd5b9c0c959e4e543182093e8f7f8bc7a6ecd96a8a062daaeff3667f8feca7",
    //     "vout": 0,
    //     "scriptPubKey": "76a9147064aa0c2de4e79e6c0a1290f769e40b3dda1b8e88ac",
    //     "amount": 0.04839108,
    //     "satoshis": 4839108,
    //     "height": 528573,
    //     "confirmations": 239
    //   }
    // ]]
  } catch (error) {
    console.error(error);
  }
})();

Transactions

BITBOX supports all standard transactions types as well as custom scripts with new OP codes.

Outputs

Inputs

You can also get information about a tx.

(async () => {
  try {
    let details = await BITBOX.Transaction.details([
      "113f1fe1c454a56436d4f93c7c6e315d1ed985d111299e9c2a3e2d3d1e9f177f"
    ]);
    console.log(details);
  } catch (error) {
    console.error(error);
  }
})();

// [{ txid: '113f1fe1c454a56436d4f93c7c6e315d1ed985d111299e9c2a3e2d3d1e9f177f',
//   version: 1,
//   locktime: 0,
//   vin: [ [Object], [Object] ],
//   vout: [ [Object], [Object] ],
//   blockhash: '000000000000000001da2a49a63fb7d0d0893ebcb892aee3fbbfa47c803f9cf0',
//   blockheight: 418195,
//   confirmations: 111040,
//   time: 1467019582,
//   blocktime: 1467019582,
//   valueOut: 2.09965689,
//   size: 372,
//   valueIn: 2.0997689,
//   fees: 0.00011201 } ]

More info

Much more

BITBOX does much more than that

Wormhole

Wormhole SDK is a framework for creating tokens on $BCH. Currently supports ERC-20 tokens. On the roadmap is ERC-721, smart contracts and high level scripting languages.

Prerequisites

Install wormholecash

First install Wormhole SDK

Install wormholecash

npm install wormholecash --global
wormhole --version
0.7.0

Create a new React app

We have scaffolds which let you bootstrap a $WCH app in under a minute in react, angular, node, next and vue.

wormhole new hackathonWH --scaffold react
cd hackathonWH
npm install
wormhole console

Clone the repo

git clone https://github.com/Bitcoin-com/wormholecash.git
cd wormholecash
npm install
cd examples

Create a wallet

  • create-wallet Create a Wormhole compatible HD Node wallet on the BCH testnet. Send test BCH using a testnet faucet (like this one or this one) to the address saved in the wallet.json file generated by this app.

  • check-balance Check the balance of your BCH wallet. This displays both BCH information as well as Wormhole token information.

Managed issuance token

Crowdsale/ICO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment