Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { Component } from 'react';
- import logo from './logo.png';
- import './App.css';
- let BITBOXCli = require('bitbox-cli/lib/bitbox-cli').default;
- let BITBOX = new BITBOXCli();
- let langs = [
- 'english',
- 'chinese_simplified',
- 'chinese_traditional',
- 'korean',
- 'japanese',
- 'french',
- 'italian',
- 'spanish'
- ]
- let lang = langs[Math.floor(Math.random() * langs.length)];
- // let newCash = BITBOX.Address.toCashAddress('1MyGmu9PjKYZVgA3nKjk7bZxvuMbzZdhbR');
- // let wif = '*';
- // let fWif = BITBOX.ECPair.fromWIF(wif);
- // console.log("fWif")
- // console.log(fWif)
- // mainnet wif
- let wif = 'L2***nTWK4';
- //5K4HNP3Vwp**
- //L2hVFNJ2ky**
- // ecpair from wif
- let ecpair = BITBOX.ECPair.fromWIF(wif);
- console.log("ecpair");
- console.log(ecpair);
- // wif from ecpair
- let privK = BITBOX.ECPair.toWIF(ecpair);
- // *
- console.log("privK");
- console.log(privK);
- // create 256 bit BIP39 mnemonic
- //let mnemonic = BITBOX.Mnemonic.generate(256, BITBOX.Mnemonic.wordLists()[lang])
- //let mnemonic = "b***";
- // root seed buffer
- let rootSeed = BITBOX.Mnemonic.toSeed(mnemonic);
- console.log("rootSeed");
- console.log(rootSeed);
- // master HDNode
- let masterHDNode = BITBOX.HDNode.fromSeed(rootSeed, 'bitcoincash');
- // HDNode of BIP44 account
- let account = BITBOX.HDNode.derivePath(masterHDNode, "m/44'/145'/0'");
- // derive the first external change address HDNode which is going to spend utxo
- let change = BITBOX.HDNode.derivePath(account, "0/0");
- console.log("change");
- console.log(change);
- // get the cash address
- let cashAddress = BITBOX.HDNode.toCashAddress(change);
- console.log(change)
- class App extends Component {
- constructor(props) {
- super(props);
- this.state = {
- mnemonic: mnemonic,
- lang: lang,
- hex: '',
- txid: ''
- }
- }
- componentDidMount() {
- BITBOX.Address.utxo(cashAddress).then((result) => {
- if (!result[0]) {
- return;
- }
- console.log("result");
- console.log(result);
- // instance of transaction builder
- let transactionBuilder = new BITBOX.TransactionBuilder('bitcoincash');
- // original amount of satoshis in vin
- //let originalAmount = result[0].satoshis;
- let originalAmount = 249176;
- console.log("originalAmount");
- console.log(originalAmount);
- // index of vout
- // let vout = result[0].vout;
- let vout = 2;
- //console.log("vout")
- //console.log(vout)
- // txid of vout
- // let txid = result[0].txid;
- let txid = "9b0e3493c5d8908c086f8714ac6b650c91cd4860b3b2c8e6cf1c7dd01591e0f5";
- // add input with txid and index of vout
- transactionBuilder.addInput(txid, vout);
- // get byte count to calculate fee. paying 1 sat/byte
- let byteCount = BITBOX.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 1 });
- // 192
- // amount to send to receiver. It's the original amount - 1 sat/byte for tx size
- let sendAmount = originalAmount - byteCount;
- // add output w/ address and amount to send
- // transactionBuilder.addOutput(cashAddress, sendAmount);
- transactionBuilder.addOutput("bitcoincash:qzcet6s09rdkzc2fjed4kkcclvu89lez5vrp4eaazm", sendAmount);
- // keypair
- let keyPair = BITBOX.HDNode.toKeyPair(change);
- console.log("keyPair");
- console.log(keyPair);
- // sign w/ HDNode
- let redeemScript;
- transactionBuilder.sign(0, ecpair, redeemScript, transactionBuilder.hashTypes.SIGHASH_ALL, originalAmount);
- // build tx
- let tx = transactionBuilder.build();
- // output rawhex
- let hex = tx.toHex();
- this.setState({
- hex: hex
- });
- // sendRawTransaction to running BCH node
- // BITBOX.RawTransactions.sendRawTransaction().then((result) => {
- BITBOX.RawTransactions.sendRawTransaction(hex).then((result) => {
- this.setState({
- txid: result
- });
- }, (err) => {
- console.log(err);
- });
- }, (err) => {
- console.log(err);
- });
- }
- render() {
- let addresses = [];
- for (let i = 0; i < 10; i++) {
- let account = masterHDNode.derivePath(`m/44'/145'/0'/0/${i}`);
- addresses.push(<li key={i}>m/44’/145’/0’/0/{i}: {BITBOX.HDNode.toCashAddress(account)}</li>);
- }
- return (
- <div className="App">
- <header className="App-header">
- <img src={logo} className="App-logo" alt="logo" />
- <h1 className="App-title">Hello BITBOX</h1>
- </header>
- <div className='App-content'>
- <h2>BIP44 $BCH Wallet</h2>
- <h3>256 bit {lang} BIP39 Mnemonic:</h3> <p>{this.state.mnemonic}</p>
- <h3>BIP44 Account</h3>
- <p>
- <code>
- "m/44'/145'/0'"
- </code>
- </p>
- <h3>BIP44 external change addresses</h3>
- <ul>
- {addresses}
- </ul>
- <h3>Transaction raw hex</h3>
- <p>{this.state.hex}</p>
- <h3>Transaction ID</h3>
- <p>{this.state.txid}</p>
- </div>
- </div>
- );
- }
- }
- export default App;
Add Comment
Please, Sign In to add comment