Skip to content

Commit

Permalink
Move functionality to bitbox-cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloneearth committed Mar 6, 2018
1 parent 753ea79 commit a2803c3
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 154 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "BITBOX",
"version": "0.2.4",
"version": "0.2.5",
"description": "A development framework for Bitcoin Cash",
"main": "main.js",
"productName": "BITBOX",
Expand Down Expand Up @@ -30,8 +30,7 @@
"babel-preset-react": "^6.24.1",
"babel-register": "^6.26.0",
"bchaddrjs": "^0.1.4",
"bip39": "^2.5.0",
"bitbox-cli": "0.2.5",
"bitbox-cli": "0.2.7",
"bitcoinjs-lib": "^3.3.2",
"bitcoinjs-message": "^2.0.0",
"body-parser": "^1.18.2",
Expand Down
14 changes: 7 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class App extends Component {

createHDWallet() {
let walletConfig = reduxStore.getState().configuration.wallet;
let [rootSeed, masterPrivateKey, mnemonic, HDPath, accounts] = BitcoinCash.createHDWallet(walletConfig);
let [rootSeed, masterPrivateKey, mnemonic, HDPath, accounts] = bitbox.BitcoinCash.createHDWallet(walletConfig);
reduxStore.dispatch(createWallet());
reduxStore.dispatch(addRootSeed(rootSeed));
reduxStore.dispatch(addMasterPrivateKey(masterPrivateKey.chainCode));
Expand All @@ -129,7 +129,7 @@ class App extends Component {

accounts.forEach((account, index) => {

let address = BitcoinCash.fromWIF(account.privateKeyWIF, walletConfig.network).getAddress();
let address = bitbox.BitcoinCash.fromWIF(account.privateKeyWIF, walletConfig.network).getAddress();
let formattedAccount = {
title: account.title,
index: account.index,
Expand All @@ -147,16 +147,16 @@ class App extends Component {

createBlockchain(addresses) {
// create genesis tx
// This is a hack because I've not yet figured out how to properly sign coinbase txs w/ BitcoinCash.transaction
// This is a hack because I've not yet figured out how to properly sign coinbase txs w/ bitbox.BitcoinCash.transaction
let genesisBlock = [];
addresses.forEach((address, index) => {
let privkey = BitcoinCash.fromWIF(addresses[0].privateKeyWIF, this.wallet.network);
let tx = BitcoinCash.transactionBuilder(this.wallet.network);
let privkey = bitbox.BitcoinCash.fromWIF(addresses[0].privateKeyWIF, this.wallet.network);
let tx = bitbox.BitcoinCash.transactionBuilder(this.wallet.network);

// Hardcode the input hash
tx.addInput("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", 0);

let addy = BitcoinCash.fromWIF(address.privateKeyWIF, this.wallet.network).getAddress();
let addy = bitbox.BitcoinCash.fromWIF(address.privateKeyWIF, this.wallet.network).getAddress();
// send 12.5 BCH to the first newly generated account
let value = BitcoinCash.toSatoshi(12.5);

Expand Down Expand Up @@ -195,7 +195,7 @@ class App extends Component {
createBlock() {
let blockchainInstance = this.state.blockchainInstance;

let keyPair = BitcoinCash.fromWIF(this.state.addresses[0].privateKeyWIF, this.wallet.network);
let keyPair = bitbox.BitcoinCash.fromWIF(this.state.addresses[0].privateKeyWIF, this.wallet.network);
let address = keyPair.getAddress();
let ripemd160 = bitbox.Crypto.createRIPEMD160Hash(address);

Expand Down
8 changes: 4 additions & 4 deletions src/components/BlockDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class BlockDetails extends Component {

let txs = this.state.transactions;
this.state.block.transactions.forEach((tx, index) => {
let t = BitcoinCash.transaction();
let t = bitbox.BitcoinCash.transaction();
let decodedTx = t.fromHex(tx.rawHex);
let a = BitcoinCash.address();
let a = bitbox.BitcoinCash.address();

let s = BitcoinCash.script();
let s = bitbox.BitcoinCash.script();
let ins = [];
let ecpair = BitcoinCash.ECPair();
let ecpair = bitbox.BitcoinCash.ECPair();
decodedTx.ins.forEach((input, index) => {
let chunksIn = s.decompile(input.script);
let inputPubKey = ecpair.fromPublicKeyBuffer(chunksIn[1], Bitcoin.networks[this.props.wallet.network]).getAddress();
Expand Down
2 changes: 1 addition & 1 deletion src/components/Convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Convert extends Component {
this.props.updateValue('error', null);
this.props.updateValue('errorMsg', '');
try {
keyPair = BitcoinCash.fromWIF(inputValue, this.props.configuration.network);
keyPair = bitbox.BitcoinCash.fromWIF(inputValue, this.props.configuration.network);
privateKeyWIF = inputValue;
this.props.updateValue('privateKeyWIF', inputValue);

Expand Down
8 changes: 4 additions & 4 deletions src/components/TransactionsDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class Transactions extends Component {
}

componentDidMount() {
let t = BitcoinCash.transaction();
let t = bitbox.BitcoinCash.transaction();
let decodedTx = t.fromHex(this.state.transaction.rawHex);
let a = BitcoinCash.address();
let s = BitcoinCash.script();
let a = bitbox.BitcoinCash.address();
let s = bitbox.BitcoinCash.script();
let ins = [];
let ecpair = BitcoinCash.ECPair();
let ecpair = bitbox.BitcoinCash.ECPair();
decodedTx.ins.forEach((input, index) => {
let chunksIn = s.decompile(input.script);
let inputPubKey = ecpair.fromPublicKeyBuffer(chunksIn[1], Bitcoin.networks[this.props.wallet.network]).getAddress();
Expand Down
2 changes: 1 addition & 1 deletion src/components/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Wallet extends Component {
// let transactions = [];
// underscore.each(this.props.blockchainInstance.chain, (block, index) => {
// underscore.each(block.transactions, (transaction, index) => {
// let t = BitcoinCash.transaction();
// let t = bitbox.BitcoinCash.transaction();
// let decodedTx = t.fromHex(transaction.rawHex);
// let a = BitcoinCash.address();
//
Expand Down
4 changes: 2 additions & 2 deletions src/containers/WalletConfigurationContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const mapDispatchToProps = (dispatch) => {
dispatch(updateWalletConfig('entropy', value))
},
resetBitbox: (configuration) => {
let [rootSeed, masterPrivateKey, mnemonic, HDPath, accounts] = BitcoinCash.createHDWallet(configuration);
let [rootSeed, masterPrivateKey, mnemonic, HDPath, accounts] = bitbox.BitcoinCash.createHDWallet(configuration);

dispatch(resetWallet());
dispatch(addRootSeed(rootSeed));
Expand All @@ -47,7 +47,7 @@ const mapDispatchToProps = (dispatch) => {

accounts.forEach((account, index) => {

let address = BitcoinCash.fromWIF(account.privateKeyWIF, configuration.network).getAddress();
let address = bitbox.BitcoinCash.fromWIF(account.privateKeyWIF, configuration.network).getAddress();

dispatch(createAccount({
title: account.title,
Expand Down
2 changes: 1 addition & 1 deletion src/models/Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Transaction {


createCoinbaseTransaction(address) {
let coinbaseTransaction = new BitcoinCash.transaction();
let coinbaseTransaction = new bitbox.BitcoinCash.transaction();
coinbaseTransaction.addInput(new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex'), 0);
coinbaseTransaction.addOutput(new Buffer(address, 'hex'), 5000000000);
// let coinbaseTransactionHash = bitbox.Crypto.createSHA256Hash(coinbaseTransaction.toHex());
Expand Down
117 changes: 3 additions & 114 deletions src/utilities/BitcoinCash.js
Original file line number Diff line number Diff line change
@@ -1,128 +1,17 @@
import Address from '../models/Address';

import Bitcoin from 'bitcoinjs-lib';
import BIP39 from 'bip39';
let BITBOXCli = require('bitbox-cli/lib/bitboxcli').default;
let bitbox = new BITBOXCli();


class BitcoinCash {
static entropyToMnemonic(bytes = 16) {
// Generate cryptographically strong pseudo-random data.
// The bytes argument is a number indicating the number of bytes to generate.
// Uses the NodeJS crypto lib. More info: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
let randomBytes = bitbox.Crypto.randomBytes(bytes);

// Create BIP 39 compliant mnemonic w/ entropy
// Entropy (bits/bytes) Checksum (bits) Entropy + checksum (bits) Mnemonic length (words)
// 128/16 4 132 12
//
// 160/20 5 165 15
//
// 192/24 6 198 18
//
// 224/28 7 231 21
//
// 256/32 8 264 24

return BIP39.entropyToMnemonic(randomBytes);
}

static mnemonicToSeed(mnemonic, password = '') {
// create BIP 39 compliant
return BIP39.mnemonicToSeed(mnemonic, password);
}

static fromSeedBuffer(rootSeed, network = 'bitcoin') {
return Bitcoin.HDNode.fromSeedBuffer(rootSeed, Bitcoin.networks[network]);
}

static fromWIF(privateKeyWIF, network = 'bitcoin') {
return Bitcoin.ECPair.fromWIF(privateKeyWIF, Bitcoin.networks[network]);
}

static ECPair() {
return Bitcoin.ECPair;
}

static address() {
return Bitcoin.address;
}

static script() {
return Bitcoin.script;
}

static transaction() {
return Bitcoin.Transaction;
}

static transactionBuilder(network = 'bitcoin') {
return new Bitcoin.TransactionBuilder(Bitcoin.networks[network]);
}

static fromTransaction() {
return Bitcoin.TransactionBuilder;
}


static createHDWallet(config) {
// nore info: https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch05.asciidoc

let mnemonic = config.mnemonic;
if(config.autogenerateHDMnemonic) {
// create a random mnemonic w/ user provided entropy size
mnemonic = BitcoinCash.entropyToMnemonic(config.entropy);
}

// create 512 bit HMAC-SHA512 root seed
let rootSeed = BitcoinCash.mnemonicToSeed(mnemonic, config.password);

// create master private key
let masterPrivateKey = BitcoinCash.fromSeedBuffer(rootSeed, config.network);

let HDPath = `m/${config.HDPath.purpose}/${config.HDPath.coinCode}`

let accounts = [];

for (let i = 0; i < config.totalAccounts; i++) {
// create accounts
// follow BIP 44 account discovery algo https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#account-discovery
let account = masterPrivateKey.derivePath(`${HDPath.replace(/\/$/, "")}/${i}'`);
// console.log('account', account);
let xpriv = account.toBase58();
let xpub = account.neutered().toBase58();
let address = masterPrivateKey.derivePath(`${HDPath.replace(/\/$/, "")}/${i}'/${config.HDPath.change}/${config.HDPath.address_index}`);
// let xPubNode = Bitcoin.HDNode.fromBase58(xpub);

// for (let j = 0; j < 1; j++) {
// // console.log('asdasfd', j)
// var derivedPublicKey = HdPublicKey.derive("m/0/"+j).publicKey;
// var addy = derivedPublicKey.toAddress();
// console.log('addy', BitcoinCash.toCashAddress(addy.toString()));
// }
// console.log('xPubNode', xPubNode);
// console.log('yay', xPubNode.derive(xPubNode.chainCode));
// console.log('---------')
accounts.push({
title: '',
privateKeyWIF: address.keyPair.toWIF(),
xpriv: xpriv,
xpub: xpub,
index: i
});
// addresses.push(new Address(BitcoinCash.toCashAddress(account.derive(i).getAddress()), account.derive(i).keyPair.toWIF()));
};

return [rootSeed, masterPrivateKey, mnemonic, config.HDPath, accounts];
}

static signMessage(message, privateKeyWIF) {

let keyPair;
let errorMsg = '';
try {
keyPair = BitcoinCash.fromWIF(privateKeyWIF);
keyPair = bitbox.BitcoinCash.fromWIF(privateKeyWIF);
} catch (e) {
errorMsg = e.message;
}
Expand All @@ -146,7 +35,7 @@ class BitcoinCash {
let errorMsg = '';
try {
addresses.forEach((address, index) => {
if(bitbox.BitcoinCash.toLegacyAddress(pubAddress) === BitcoinCash.fromWIF(address.privateKeyWIF).getAddress()) {
if(bitbox.BitcoinCash.toLegacyAddress(pubAddress) === bitbox.BitcoinCash.fromWIF(address.privateKeyWIF).getAddress()) {
privateKeyWIF = address.privateKeyWIF;
}
});
Expand All @@ -169,7 +58,7 @@ class BitcoinCash {
pubKeys.push(key);
} else {
let privkeyWIF = BitcoinCash.returnPrivateKeyWIF(key, addresses);
keyPairs.push(BitcoinCash.fromWIF(privkeyWIF, wallet.network))
keyPairs.push(bitbox.BitcoinCash.fromWIF(privkeyWIF, wallet.network))
}
})

Expand Down
4 changes: 2 additions & 2 deletions src/utilities/Miner.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Miner {

pushGenesisBlock(genesisBlock) {
genesisBlock.forEach((transaction, index) => {
let t = BitcoinCash.transaction();
let t = bitbox.BitcoinCash.transaction();
let decodedTx = t.fromHex(transaction.rawHex);
let a = BitcoinCash.address();
let a = bitbox.BitcoinCash.address();

decodedTx.outs.forEach((output, index) => {
let outputPubKey = a.fromOutputScript(output.script, Bitcoin.networks[this.network]);
Expand Down
12 changes: 6 additions & 6 deletions src/utilities/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ class Server {
server.post('/decoderawtransaction', (req, res) => {
res.setHeader('Content-Type', 'application/json');

let t = BitcoinCash.transaction();
let t = bitbox.BitcoinCash.transaction();
let decodedTx = t.fromHex(req.body.params[0]);

let a = BitcoinCash.address();
let s = BitcoinCash.script();
let a = bitbox.BitcoinCash.address();
let s = bitbox.BitcoinCash.script();
let ins = [];
let ecpair = BitcoinCash.ECPair();
let ecpair = bitbox.BitcoinCash.ECPair();
decodedTx.ins.forEach((input, index) => {
let chunksIn = s.decompile(input.script);
let inputPubKey = ecpair.fromPublicKeyBuffer(chunksIn[1]).getAddress();
Expand Down Expand Up @@ -159,7 +159,7 @@ class Server {
let accounts = state.wallet.accounts;

accounts.forEach((account, index) => {
let tmp = BitcoinCash.fromWIF(account.privateKeyWIF).getAddress();
let tmp = bitbox.BitcoinCash.fromWIF(account.privateKeyWIF).getAddress();
if(tmp === bitbox.BitcoinCash.toLegacyAddress(req.body.params[0])) {
res.send(account.privateKeyWIF);
}
Expand Down Expand Up @@ -250,7 +250,7 @@ class Server {
server.post('/getaccountaddress', (req, res) => {
res.setHeader('Content-Type', 'application/json');

res.send(bitbox.BitcoinCash.toCashAddress(BitcoinCash.fromWIF(store.get('addresses')[0].privateKeyWIF).getAddress()));
res.send(bitbox.BitcoinCash.toCashAddress(bitbox.BitcoinCash.fromWIF(store.get('addresses')[0].privateKeyWIF).getAddress()));
});

server.post('/getaccount', (req, res) => {
Expand Down
Loading

0 comments on commit a2803c3

Please sign in to comment.