Guest User

Untitled

a guest
Jun 22nd, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import logo from './logo.png';
  3.  
  4. import './App.css';
  5. let BITBOXCli = require('bitbox-cli/lib/bitbox-cli').default;
  6. let BITBOX = new BITBOXCli();
  7.  
  8. let langs = [
  9. 'english',
  10. 'chinese_simplified',
  11. 'chinese_traditional',
  12. 'korean',
  13. 'japanese',
  14. 'french',
  15. 'italian',
  16. 'spanish'
  17. ]
  18.  
  19. let lang = langs[Math.floor(Math.random() * langs.length)];
  20.  
  21.  
  22. // let newCash = BITBOX.Address.toCashAddress('1MyGmu9PjKYZVgA3nKjk7bZxvuMbzZdhbR');
  23.  
  24. // let wif = '*';
  25. // let fWif = BITBOX.ECPair.fromWIF(wif);
  26. // console.log("fWif")
  27. // console.log(fWif)
  28.  
  29. // mainnet wif
  30. let wif = 'L2***nTWK4';
  31. //5K4HNP3Vwp**
  32. //L2hVFNJ2ky**
  33. // ecpair from wif
  34. let ecpair = BITBOX.ECPair.fromWIF(wif);
  35. console.log("ecpair");
  36. console.log(ecpair);
  37. // wif from ecpair
  38. let privK = BITBOX.ECPair.toWIF(ecpair);
  39. // *
  40. console.log("privK");
  41. console.log(privK);
  42.  
  43. // create 256 bit BIP39 mnemonic
  44. //let mnemonic = BITBOX.Mnemonic.generate(256, BITBOX.Mnemonic.wordLists()[lang])
  45. //let mnemonic = "b***";
  46.  
  47. // root seed buffer
  48. let rootSeed = BITBOX.Mnemonic.toSeed(mnemonic);
  49. console.log("rootSeed");
  50. console.log(rootSeed);
  51.  
  52. // master HDNode
  53. let masterHDNode = BITBOX.HDNode.fromSeed(rootSeed, 'bitcoincash');
  54.  
  55. // HDNode of BIP44 account
  56. let account = BITBOX.HDNode.derivePath(masterHDNode, "m/44'/145'/0'");
  57.  
  58. // derive the first external change address HDNode which is going to spend utxo
  59. let change = BITBOX.HDNode.derivePath(account, "0/0");
  60. console.log("change");
  61. console.log(change);
  62.  
  63. // get the cash address
  64.  
  65. let cashAddress = BITBOX.HDNode.toCashAddress(change);
  66. console.log(change)
  67.  
  68. class App extends Component {
  69. constructor(props) {
  70. super(props);
  71. this.state = {
  72. mnemonic: mnemonic,
  73. lang: lang,
  74. hex: '',
  75. txid: ''
  76. }
  77. }
  78.  
  79. componentDidMount() {
  80. BITBOX.Address.utxo(cashAddress).then((result) => {
  81. if (!result[0]) {
  82. return;
  83. }
  84. console.log("result");
  85. console.log(result);
  86. // instance of transaction builder
  87. let transactionBuilder = new BITBOX.TransactionBuilder('bitcoincash');
  88. // original amount of satoshis in vin
  89. //let originalAmount = result[0].satoshis;
  90. let originalAmount = 249176;
  91. console.log("originalAmount");
  92. console.log(originalAmount);
  93.  
  94. // index of vout
  95. // let vout = result[0].vout;
  96. let vout = 2;
  97. //console.log("vout")
  98. //console.log(vout)
  99.  
  100. // txid of vout
  101. // let txid = result[0].txid;
  102. let txid = "9b0e3493c5d8908c086f8714ac6b650c91cd4860b3b2c8e6cf1c7dd01591e0f5";
  103.  
  104. // add input with txid and index of vout
  105. transactionBuilder.addInput(txid, vout);
  106.  
  107. // get byte count to calculate fee. paying 1 sat/byte
  108. let byteCount = BITBOX.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 1 });
  109. // 192
  110. // amount to send to receiver. It's the original amount - 1 sat/byte for tx size
  111. let sendAmount = originalAmount - byteCount;
  112.  
  113. // add output w/ address and amount to send
  114. // transactionBuilder.addOutput(cashAddress, sendAmount);
  115. transactionBuilder.addOutput("bitcoincash:qzcet6s09rdkzc2fjed4kkcclvu89lez5vrp4eaazm", sendAmount);
  116.  
  117. // keypair
  118. let keyPair = BITBOX.HDNode.toKeyPair(change);
  119. console.log("keyPair");
  120. console.log(keyPair);
  121.  
  122.  
  123. // sign w/ HDNode
  124. let redeemScript;
  125. transactionBuilder.sign(0, ecpair, redeemScript, transactionBuilder.hashTypes.SIGHASH_ALL, originalAmount);
  126.  
  127. // build tx
  128. let tx = transactionBuilder.build();
  129. // output rawhex
  130. let hex = tx.toHex();
  131. this.setState({
  132. hex: hex
  133. });
  134.  
  135. // sendRawTransaction to running BCH node
  136. // BITBOX.RawTransactions.sendRawTransaction().then((result) => {
  137. BITBOX.RawTransactions.sendRawTransaction(hex).then((result) => {
  138. this.setState({
  139. txid: result
  140. });
  141. }, (err) => {
  142. console.log(err);
  143. });
  144. }, (err) => {
  145. console.log(err);
  146. });
  147. }
  148.  
  149. render() {
  150. let addresses = [];
  151. for (let i = 0; i < 10; i++) {
  152. let account = masterHDNode.derivePath(`m/44'/145'/0'/0/${i}`);
  153. addresses.push(<li key={i}>m/44&rsquo;/145&rsquo;/0&rsquo;/0/{i}: {BITBOX.HDNode.toCashAddress(account)}</li>);
  154. }
  155. return (
  156. <div className="App">
  157. <header className="App-header">
  158. <img src={logo} className="App-logo" alt="logo" />
  159. <h1 className="App-title">Hello BITBOX</h1>
  160. </header>
  161. <div className='App-content'>
  162. <h2>BIP44 $BCH Wallet</h2>
  163. <h3>256 bit {lang} BIP39 Mnemonic:</h3> <p>{this.state.mnemonic}</p>
  164. <h3>BIP44 Account</h3>
  165. <p>
  166. <code>
  167. "m/44'/145'/0'"
  168. </code>
  169. </p>
  170. <h3>BIP44 external change addresses</h3>
  171. <ul>
  172. {addresses}
  173. </ul>
  174. <h3>Transaction raw hex</h3>
  175. <p>{this.state.hex}</p>
  176. <h3>Transaction ID</h3>
  177. <p>{this.state.txid}</p>
  178. </div>
  179. </div>
  180. );
  181. }
  182. }
  183.  
  184. export default App;
Add Comment
Please, Sign In to add comment