├── README.md ├── part10_Network ├── .idea │ └── workspace.xml ├── blockchain.sh ├── blockchain_3000.db │ ├── 000004.sst │ ├── 000013.sst │ ├── 000024.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── LOG.old.1539243911758499 │ ├── LOG.old.1539248616098712 │ ├── LOG.old.1539250846485204 │ ├── LOG.old.1539251140370495 │ ├── LOG.old.1539251173036149 │ ├── LOG.old.1539251188638910 │ ├── LOG.old.1539251385979187 │ ├── MANIFEST-000023 │ ├── OPTIONS-000023 │ └── OPTIONS-000026 ├── blockchain_3001.db │ ├── 000004.sst │ ├── 000013.sst │ ├── 000037.sst │ ├── 000042.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── LOG.old.1539243911758499 │ ├── LOG.old.1539248616098712 │ ├── LOG.old.1539250846485204 │ ├── LOG.old.1539251140370495 │ ├── LOG.old.1539251173036149 │ ├── LOG.old.1539251188638910 │ ├── LOG.old.1539251221988465 │ ├── LOG.old.1539251233226639 │ ├── LOG.old.1539251243733317 │ ├── LOG.old.1539251703715766 │ ├── LOG.old.1539251829426446 │ ├── LOG.old.1539251839962769 │ ├── LOG.old.1539251852716770 │ ├── MANIFEST-000041 │ ├── OPTIONS-000041 │ └── OPTIONS-000044 ├── blockchain_3002.db │ ├── 000004.sst │ ├── 000013.sst │ ├── 000021.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── LOG.old.1539243911758499 │ ├── LOG.old.1539251619432526 │ ├── LOG.old.1539251648812037 │ ├── LOG.old.1539251800004020 │ ├── LOG.old.1539251816788972 │ ├── LOG.old.1539252735123028 │ ├── MANIFEST-000020 │ ├── OPTIONS-000020 │ └── OPTIONS-000023 ├── blockchain_genesis.db │ ├── 000004.sst │ ├── 000006.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── LOG.old.1539243911758499 │ ├── MANIFEST-000005 │ ├── OPTIONS-000005 │ └── OPTIONS-000008 ├── pom.xml ├── readme.txt ├── src │ └── main │ │ ├── java │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.java │ │ │ ├── block │ │ │ ├── Block.java │ │ │ └── Blockchain.java │ │ │ ├── cli │ │ │ └── CLI.java │ │ │ ├── net │ │ │ ├── BlockData.java │ │ │ ├── GetBlocks.java │ │ │ ├── GetData.java │ │ │ ├── Inv.java │ │ │ ├── Server.java │ │ │ ├── ServerConst.java │ │ │ ├── ServerHandle.java │ │ │ ├── ServerSend.java │ │ │ ├── ServerSocketClient.java │ │ │ ├── ServerThread.java │ │ │ ├── TransactionData.java │ │ │ └── Version.java │ │ │ ├── pow │ │ │ ├── PowResult.java │ │ │ └── ProofOfWork.java │ │ │ ├── store │ │ │ └── RocksDBUtils.java │ │ │ ├── transaction │ │ │ ├── MerkleTree.java │ │ │ ├── SpendableOutputResult.java │ │ │ ├── TXInput.java │ │ │ ├── TXOutput.java │ │ │ ├── Transaction.java │ │ │ └── UTXOSet.java │ │ │ ├── util │ │ │ ├── AddressUtils.java │ │ │ ├── Base58Check.java │ │ │ ├── ByteUtils.java │ │ │ ├── CommandUtils.java │ │ │ └── SerializeUtils.java │ │ │ └── wallet │ │ │ ├── Wallet.java │ │ │ └── WalletUtils.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp ├── target │ ├── classes │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.class │ │ │ ├── block │ │ │ ├── Block.class │ │ │ ├── Blockchain$BlockchainIterator.class │ │ │ └── Blockchain.class │ │ │ ├── cli │ │ │ └── CLI.class │ │ │ ├── net │ │ │ ├── BlockData.class │ │ │ ├── GetBlocks.class │ │ │ ├── GetData.class │ │ │ ├── Inv.class │ │ │ ├── Server.class │ │ │ ├── ServerConst.class │ │ │ ├── ServerHandle.class │ │ │ ├── ServerSend.class │ │ │ ├── ServerSocketClient.class │ │ │ ├── ServerThread.class │ │ │ ├── TransactionData.class │ │ │ └── Version.class │ │ │ ├── pow │ │ │ ├── PowResult.class │ │ │ └── ProofOfWork.class │ │ │ ├── store │ │ │ └── RocksDBUtils.class │ │ │ ├── transaction │ │ │ ├── MerkleTree$Node.class │ │ │ ├── MerkleTree.class │ │ │ ├── SpendableOutputResult.class │ │ │ ├── TXInput.class │ │ │ ├── TXOutput.class │ │ │ ├── Transaction.class │ │ │ └── UTXOSet.class │ │ │ ├── util │ │ │ ├── AddressUtils.class │ │ │ ├── Base58Check.class │ │ │ ├── ByteUtils.class │ │ │ ├── CommandUtils.class │ │ │ └── SerializeUtils.class │ │ │ └── wallet │ │ │ ├── Wallet.class │ │ │ ├── WalletUtils$Wallets.class │ │ │ └── WalletUtils.class │ ├── maven-archiver │ │ └── pom.properties │ ├── maven-status │ │ └── maven-compiler-plugin │ │ │ └── compile │ │ │ └── default-compile │ │ │ ├── createdFiles.lst │ │ │ └── inputFiles.lst │ ├── part10_Network-jar-with-dependencies.jar │ └── part10_Network.jar ├── wallet_3000.dat ├── wallet_3001.dat └── wallet_3002.dat ├── part1_Basic_Prototype ├── .idea │ ├── compiler.xml │ ├── encodings.xml │ ├── misc.xml │ └── workspace.xml ├── pom.xml ├── src │ └── main │ │ ├── java │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.java │ │ │ ├── block │ │ │ ├── Block.java │ │ │ └── Blockchain.java │ │ │ └── util │ │ │ └── ByteUtils.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp └── target │ └── classes │ └── cldy │ └── hanru │ └── blockchain │ ├── Main.class │ ├── block │ ├── Block.class │ └── Blockchain.class │ └── util │ └── ByteUtils.class ├── part2_Proof_Of_Work ├── .idea │ ├── compiler.xml │ ├── encodings.xml │ ├── misc.xml │ └── workspace.xml ├── pom.xml ├── src │ └── main │ │ ├── java │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.java │ │ │ ├── block │ │ │ ├── Block.java │ │ │ └── Blockchain.java │ │ │ ├── pow │ │ │ ├── PowResult.java │ │ │ └── ProofOfWork.java │ │ │ └── util │ │ │ └── ByteUtils.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp └── target │ └── classes │ └── cldy │ └── hanru │ └── blockchain │ ├── Main.class │ ├── block │ ├── Block.class │ └── Blockchain.class │ ├── pow │ ├── PowResult.class │ └── ProofOfWork.class │ └── util │ └── ByteUtils.class ├── part3_Persistence ├── .idea │ ├── compiler.xml │ ├── encodings.xml │ ├── misc.xml │ └── workspace.xml ├── blockchain.db │ ├── 000003.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── MANIFEST-000001 │ └── OPTIONS-000005 ├── pom.xml ├── readme.txt ├── src │ └── main │ │ ├── java │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.java │ │ │ ├── block │ │ │ ├── Block.java │ │ │ └── Blockchain.java │ │ │ ├── pow │ │ │ ├── PowResult.java │ │ │ └── ProofOfWork.java │ │ │ ├── store │ │ │ └── RocksDBUtils.java │ │ │ └── util │ │ │ ├── ByteUtils.java │ │ │ └── SerializeUtils.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp └── target │ └── classes │ └── cldy │ └── hanru │ └── blockchain │ ├── Main.class │ ├── block │ ├── Block.class │ ├── Blockchain$BlockchainIterator.class │ └── Blockchain.class │ ├── pow │ ├── PowResult.class │ └── ProofOfWork.class │ ├── store │ └── RocksDBUtils.class │ └── util │ ├── ByteUtils.class │ └── SerializeUtils.class ├── part4_CLI ├── .idea │ ├── compiler.xml │ ├── encodings.xml │ ├── misc.xml │ └── workspace.xml ├── blockchain.db │ ├── 000004.sst │ ├── 000010.sst │ ├── 000013.sst │ ├── 000018.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── LOG.old.1539160112346530 │ ├── LOG.old.1539160130064450 │ ├── LOG.old.1539160144755018 │ ├── LOG.old.1539160165635257 │ ├── LOG.old.1539160306085811 │ ├── MANIFEST-000017 │ ├── OPTIONS-000017 │ └── OPTIONS-000020 ├── blockchain.sh ├── pom.xml ├── readme.txt ├── src │ └── main │ │ ├── java │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.java │ │ │ ├── block │ │ │ ├── Block.java │ │ │ └── Blockchain.java │ │ │ ├── cli │ │ │ └── CLI.java │ │ │ ├── pow │ │ │ ├── PowResult.java │ │ │ └── ProofOfWork.java │ │ │ ├── store │ │ │ └── RocksDBUtils.java │ │ │ └── util │ │ │ ├── ByteUtils.java │ │ │ └── SerializeUtils.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp └── target │ ├── blockchain.db │ ├── 000003.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── MANIFEST-000001 │ └── OPTIONS-000005 │ ├── maven-archiver │ └── pom.properties │ ├── maven-status │ └── maven-compiler-plugin │ │ └── compile │ │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst │ ├── part4_CLI-jar-with-dependencies.jar │ └── part4_CLI.jar ├── part5_Transaction ├── .idea │ ├── compiler.xml │ ├── encodings.xml │ ├── misc.xml │ └── workspace.xml ├── blockchain.db │ ├── 000004.sst │ ├── 000007.sst │ ├── 000016.sst │ ├── 000018.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── LOG.old.1539178149246332 │ ├── LOG.old.1539178167941477 │ ├── LOG.old.1539178635376268 │ ├── LOG.old.1539178650498545 │ ├── LOG.old.1539178656094823 │ ├── MANIFEST-000017 │ ├── OPTIONS-000017 │ └── OPTIONS-000020 ├── blockchain.sh ├── pom.xml ├── readme.txt ├── src │ └── main │ │ ├── java │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.java │ │ │ ├── block │ │ │ ├── Block.java │ │ │ └── Blockchain.java │ │ │ ├── cli │ │ │ └── CLI.java │ │ │ ├── pow │ │ │ ├── PowResult.java │ │ │ └── ProofOfWork.java │ │ │ ├── store │ │ │ └── RocksDBUtils.java │ │ │ ├── transaction │ │ │ ├── SpendableOutputResult.java │ │ │ ├── TXInput.java │ │ │ ├── TXOutput.java │ │ │ └── Transaction.java │ │ │ └── util │ │ │ ├── ByteUtils.java │ │ │ └── SerializeUtils.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp └── target │ ├── classes │ └── cldy │ │ └── hanru │ │ └── blockchain │ │ ├── Main.class │ │ ├── block │ │ ├── Block.class │ │ ├── Blockchain$BlockchainIterator.class │ │ └── Blockchain.class │ │ ├── cli │ │ └── CLI.class │ │ ├── pow │ │ ├── PowResult.class │ │ └── ProofOfWork.class │ │ ├── store │ │ └── RocksDBUtils.class │ │ ├── transaction │ │ ├── SpendableOutputResult.class │ │ ├── TXInput.class │ │ ├── TXOutput.class │ │ └── Transaction.class │ │ └── util │ │ ├── ByteUtils.class │ │ └── SerializeUtils.class │ ├── maven-archiver │ └── pom.properties │ ├── maven-status │ └── maven-compiler-plugin │ │ └── compile │ │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst │ ├── part5_Transaction-jar-with-dependencies.jar │ └── part5_Transaction.jar ├── part6_Wallet ├── .idea │ ├── compiler.xml │ ├── encodings.xml │ ├── inspectionProfiles │ │ └── Project_Default.xml │ ├── misc.xml │ ├── uiDesigner.xml │ └── workspace.xml ├── blockchain.sh ├── pom.xml ├── readme.txt ├── src │ └── main │ │ ├── java │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.java │ │ │ ├── block │ │ │ ├── Block.java │ │ │ └── Blockchain.java │ │ │ ├── cli │ │ │ └── CLI.java │ │ │ ├── pow │ │ │ ├── PowResult.java │ │ │ └── ProofOfWork.java │ │ │ ├── store │ │ │ └── RocksDBUtils.java │ │ │ ├── transaction │ │ │ ├── SpendableOutputResult.java │ │ │ ├── TXInput.java │ │ │ ├── TXOutput.java │ │ │ └── Transaction.java │ │ │ ├── util │ │ │ ├── AddressUtils.java │ │ │ ├── Base58Check.java │ │ │ ├── ByteUtils.java │ │ │ └── SerializeUtils.java │ │ │ └── wallet │ │ │ ├── Wallet.java │ │ │ └── WalletUtils.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp └── wallet.dat ├── part7_Signature ├── .idea │ ├── compiler.xml │ ├── encodings.xml │ ├── inspectionProfiles │ │ └── Project_Default.xml │ ├── misc.xml │ └── workspace.xml ├── blockchain.db │ ├── 000004.sst │ ├── 000010.sst │ ├── 000018.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── LOG.old.1539165566043238 │ ├── LOG.old.1539165585901994 │ ├── LOG.old.1539165593007385 │ ├── LOG.old.1539165602859106 │ ├── LOG.old.1539165608196518 │ ├── MANIFEST-000017 │ ├── OPTIONS-000017 │ └── OPTIONS-000020 ├── blockchain.sh ├── pom.xml ├── readme.txt ├── src │ └── main │ │ ├── java │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.java │ │ │ ├── block │ │ │ ├── Block.java │ │ │ └── Blockchain.java │ │ │ ├── cli │ │ │ └── CLI.java │ │ │ ├── pow │ │ │ ├── PowResult.java │ │ │ └── ProofOfWork.java │ │ │ ├── store │ │ │ └── RocksDBUtils.java │ │ │ ├── transaction │ │ │ ├── SpendableOutputResult.java │ │ │ ├── TXInput.java │ │ │ ├── TXOutput.java │ │ │ └── Transaction.java │ │ │ ├── util │ │ │ ├── AddressUtils.java │ │ │ ├── Base58Check.java │ │ │ ├── ByteUtils.java │ │ │ └── SerializeUtils.java │ │ │ └── wallet │ │ │ ├── Wallet.java │ │ │ └── WalletUtils.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp ├── target │ ├── classes │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.class │ │ │ ├── block │ │ │ ├── Block.class │ │ │ ├── Blockchain$BlockchainIterator.class │ │ │ └── Blockchain.class │ │ │ ├── cli │ │ │ └── CLI.class │ │ │ ├── pow │ │ │ ├── PowResult.class │ │ │ └── ProofOfWork.class │ │ │ ├── store │ │ │ └── RocksDBUtils.class │ │ │ ├── transaction │ │ │ ├── SpendableOutputResult.class │ │ │ ├── TXInput.class │ │ │ ├── TXOutput.class │ │ │ └── Transaction.class │ │ │ ├── util │ │ │ ├── AddressUtils.class │ │ │ ├── Base58Check.class │ │ │ ├── ByteUtils.class │ │ │ └── SerializeUtils.class │ │ │ └── wallet │ │ │ ├── Wallet.class │ │ │ ├── WalletUtils$Wallets.class │ │ │ └── WalletUtils.class │ ├── maven-archiver │ │ └── pom.properties │ ├── maven-status │ │ └── maven-compiler-plugin │ │ │ └── compile │ │ │ └── default-compile │ │ │ ├── createdFiles.lst │ │ │ └── inputFiles.lst │ ├── part7_Signature-jar-with-dependencies.jar │ └── part7_Signature.jar └── wallet.dat ├── part8_Transaction2 ├── .idea │ ├── compiler.xml │ ├── encodings.xml │ ├── inspectionProfiles │ │ └── Project_Default.xml │ ├── misc.xml │ └── workspace.xml ├── blockchain.db │ ├── 000033.sst │ ├── 000036.log │ ├── CURRENT │ ├── IDENTITY │ ├── LOCK │ ├── LOG │ ├── LOG.old.1539177628369343 │ ├── LOG.old.1539177640925313 │ ├── LOG.old.1539177672517674 │ ├── LOG.old.1539177682008406 │ ├── LOG.old.1539178944228142 │ ├── LOG.old.1539178980545229 │ ├── LOG.old.1539178989753741 │ ├── LOG.old.1539179330013779 │ ├── LOG.old.1539179358983232 │ ├── LOG.old.1539179361821278 │ ├── MANIFEST-000035 │ ├── OPTIONS-000032 │ └── OPTIONS-000038 ├── blockchain.sh ├── pom.xml ├── readme.txt ├── src │ └── main │ │ ├── java │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.java │ │ │ ├── block │ │ │ ├── Block.java │ │ │ └── Blockchain.java │ │ │ ├── cli │ │ │ └── CLI.java │ │ │ ├── pow │ │ │ ├── PowResult.java │ │ │ └── ProofOfWork.java │ │ │ ├── store │ │ │ └── RocksDBUtils.java │ │ │ ├── transaction │ │ │ ├── SpendableOutputResult.java │ │ │ ├── TXInput.java │ │ │ ├── TXOutput.java │ │ │ ├── Transaction.java │ │ │ └── UTXOSet.java │ │ │ ├── util │ │ │ ├── AddressUtils.java │ │ │ ├── Base58Check.java │ │ │ ├── ByteUtils.java │ │ │ └── SerializeUtils.java │ │ │ └── wallet │ │ │ ├── Wallet.java │ │ │ └── WalletUtils.java │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp ├── target │ ├── classes │ │ └── cldy │ │ │ └── hanru │ │ │ └── blockchain │ │ │ ├── Main.class │ │ │ ├── block │ │ │ ├── Block.class │ │ │ ├── Blockchain$BlockchainIterator.class │ │ │ └── Blockchain.class │ │ │ ├── cli │ │ │ └── CLI.class │ │ │ ├── pow │ │ │ ├── PowResult.class │ │ │ └── ProofOfWork.class │ │ │ ├── store │ │ │ └── RocksDBUtils.class │ │ │ ├── transaction │ │ │ ├── SpendableOutputResult.class │ │ │ ├── TXInput.class │ │ │ ├── TXOutput.class │ │ │ ├── Transaction.class │ │ │ └── UTXOSet.class │ │ │ ├── util │ │ │ ├── AddressUtils.class │ │ │ ├── Base58Check.class │ │ │ ├── ByteUtils.class │ │ │ └── SerializeUtils.class │ │ │ └── wallet │ │ │ ├── Wallet.class │ │ │ ├── WalletUtils$Wallets.class │ │ │ └── WalletUtils.class │ ├── maven-archiver │ │ └── pom.properties │ ├── maven-status │ │ └── maven-compiler-plugin │ │ │ └── compile │ │ │ └── default-compile │ │ │ ├── createdFiles.lst │ │ │ └── inputFiles.lst │ ├── part8_Transaction2-jar-with-dependencies.jar │ └── part8_Transaction2.jar └── wallet.dat └── part9_Merkle ├── .idea ├── compiler.xml ├── encodings.xml ├── inspectionProfiles │ └── Project_Default.xml ├── misc.xml └── workspace.xml ├── blockchain.db ├── 000004.sst ├── 000010.sst ├── 000015.log ├── CURRENT ├── IDENTITY ├── LOCK ├── LOG ├── LOG.old.1539221954058346 ├── LOG.old.1539221969646969 ├── LOG.old.1539221993800644 ├── LOG.old.1539222003874863 ├── MANIFEST-000014 ├── OPTIONS-000014 └── OPTIONS-000017 ├── blockchain.sh ├── pom.xml ├── readme.txt ├── src └── main │ ├── java │ └── cldy │ │ └── hanru │ │ └── blockchain │ │ ├── Main.java │ │ ├── block │ │ ├── Block.java │ │ └── Blockchain.java │ │ ├── cli │ │ └── CLI.java │ │ ├── pow │ │ ├── PowResult.java │ │ └── ProofOfWork.java │ │ ├── store │ │ └── RocksDBUtils.java │ │ ├── transaction │ │ ├── MerkleTree.java │ │ ├── SpendableOutputResult.java │ │ ├── TXInput.java │ │ ├── TXOutput.java │ │ ├── Transaction.java │ │ └── UTXOSet.java │ │ ├── util │ │ ├── AddressUtils.java │ │ ├── Base58Check.java │ │ ├── ByteUtils.java │ │ └── SerializeUtils.java │ │ └── wallet │ │ ├── Wallet.java │ │ └── WalletUtils.java │ └── webapp │ ├── WEB-INF │ └── web.xml │ └── index.jsp ├── target ├── classes │ └── cldy │ │ └── hanru │ │ └── blockchain │ │ ├── Main.class │ │ ├── block │ │ ├── Block.class │ │ ├── Blockchain$BlockchainIterator.class │ │ └── Blockchain.class │ │ ├── cli │ │ └── CLI.class │ │ ├── pow │ │ ├── PowResult.class │ │ └── ProofOfWork.class │ │ ├── store │ │ └── RocksDBUtils.class │ │ ├── transaction │ │ ├── MerkleTree$Node.class │ │ ├── MerkleTree.class │ │ ├── SpendableOutputResult.class │ │ ├── TXInput.class │ │ ├── TXOutput.class │ │ ├── Transaction.class │ │ └── UTXOSet.class │ │ ├── util │ │ ├── AddressUtils.class │ │ ├── Base58Check.class │ │ ├── ByteUtils.class │ │ └── SerializeUtils.class │ │ └── wallet │ │ ├── Wallet.class │ │ ├── WalletUtils$Wallets.class │ │ └── WalletUtils.class ├── maven-archiver │ └── pom.properties ├── maven-status │ └── maven-compiler-plugin │ │ └── compile │ │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst ├── part8_Transaction2-jar-with-dependencies.jar ├── part8_Transaction2.jar ├── part9_Merkle-jar-with-dependencies.jar └── part9_Merkle.jar └── wallet.dat /README.md: -------------------------------------------------------------------------------- 1 | # rose 2 | # rose 3 | # BitcoinForJava 4 | # BitcoinForJava 5 | -------------------------------------------------------------------------------- /part10_Network/blockchain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # Check if the jar has been built. 6 | if [ ! -e target/part10_Network-jar-with-dependencies.jar ]; then 7 | echo "Compiling blockchain project to a JAR" 8 | mvn package -DskipTests 9 | fi 10 | 11 | java -jar target/part10_Network-jar-with-dependencies.jar "$@" 12 | 13 | -------------------------------------------------------------------------------- /part10_Network/blockchain_3000.db/000004.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3000.db/000004.sst -------------------------------------------------------------------------------- /part10_Network/blockchain_3000.db/000013.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3000.db/000013.sst -------------------------------------------------------------------------------- /part10_Network/blockchain_3000.db/000024.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3000.db/000024.log -------------------------------------------------------------------------------- /part10_Network/blockchain_3000.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000023 2 | -------------------------------------------------------------------------------- /part10_Network/blockchain_3000.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c7e296b5531ca-79b7a18ed71ef1e8 -------------------------------------------------------------------------------- /part10_Network/blockchain_3000.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3000.db/LOCK -------------------------------------------------------------------------------- /part10_Network/blockchain_3000.db/MANIFEST-000023: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3000.db/MANIFEST-000023 -------------------------------------------------------------------------------- /part10_Network/blockchain_3001.db/000004.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3001.db/000004.sst -------------------------------------------------------------------------------- /part10_Network/blockchain_3001.db/000013.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3001.db/000013.sst -------------------------------------------------------------------------------- /part10_Network/blockchain_3001.db/000037.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3001.db/000037.sst -------------------------------------------------------------------------------- /part10_Network/blockchain_3001.db/000042.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3001.db/000042.log -------------------------------------------------------------------------------- /part10_Network/blockchain_3001.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000041 2 | -------------------------------------------------------------------------------- /part10_Network/blockchain_3001.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c7e296b5531ca-79b7a18ed71ef1e8 -------------------------------------------------------------------------------- /part10_Network/blockchain_3001.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3001.db/LOCK -------------------------------------------------------------------------------- /part10_Network/blockchain_3001.db/MANIFEST-000041: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3001.db/MANIFEST-000041 -------------------------------------------------------------------------------- /part10_Network/blockchain_3002.db/000004.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3002.db/000004.sst -------------------------------------------------------------------------------- /part10_Network/blockchain_3002.db/000013.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3002.db/000013.sst -------------------------------------------------------------------------------- /part10_Network/blockchain_3002.db/000021.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3002.db/000021.log -------------------------------------------------------------------------------- /part10_Network/blockchain_3002.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000020 2 | -------------------------------------------------------------------------------- /part10_Network/blockchain_3002.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c7e296b5531ca-79b7a18ed71ef1e8 -------------------------------------------------------------------------------- /part10_Network/blockchain_3002.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3002.db/LOCK -------------------------------------------------------------------------------- /part10_Network/blockchain_3002.db/MANIFEST-000020: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_3002.db/MANIFEST-000020 -------------------------------------------------------------------------------- /part10_Network/blockchain_genesis.db/000004.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_genesis.db/000004.sst -------------------------------------------------------------------------------- /part10_Network/blockchain_genesis.db/000006.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_genesis.db/000006.log -------------------------------------------------------------------------------- /part10_Network/blockchain_genesis.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000005 2 | -------------------------------------------------------------------------------- /part10_Network/blockchain_genesis.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c7e296b5531ca-79b7a18ed71ef1e8 -------------------------------------------------------------------------------- /part10_Network/blockchain_genesis.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_genesis.db/LOCK -------------------------------------------------------------------------------- /part10_Network/blockchain_genesis.db/MANIFEST-000005: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/blockchain_genesis.db/MANIFEST-000005 -------------------------------------------------------------------------------- /part10_Network/readme.txt: -------------------------------------------------------------------------------- 1 | step1:在CLI中设置NODE_ID 2 | 3 | step2:根据node_id创建,数据库文件,钱包文件等 4 | 5 | 先修改数据库的名字:blockchain_%s.db 6 | A:修改CLI中, 7 | 1.private void createBlockchain(String address,String nodeID) 8 | 9 | B:修改Blockchain中, 10 | 1.添加字段private String nodeID; 11 | 2.修改:public static Blockchain createBlockchain(String address,String nodeID) 12 | 3.修改:public static Blockchain initBlockchainFromDB(String nodeID) 13 | 14 | C:修改RocksDBUtils中, 15 | 1.getInstance(nodeID); 16 | 2.RocksDBUtils(nodeID);构造函数 17 | 3.private void openDB(String nodeID) 18 | 添加:DB_FILE = String.format(DB_FILE,nodeID); 19 | 20 | D:修改CLI中 21 | 1.修改打印区块信息: 22 | private void printChain(String nodeID) 23 | 2.修改查询余额: 24 | private void getBalance(String address,String nodeID) 25 | 26 | 27 | 28 | 修改钱包文件名称:WALLET_FILE = "wallet_%s.dat"; 29 | 30 | E:修改CLI中 31 | 1. private void createWallet(String nodeID) 32 | 2.private void printAddresses() 33 | 34 | 35 | F:修改WalletUtils中 36 | 1.public static WalletUtils getInstance(String nodeID) 37 | 2.构造函数:WalletUtils(nodeID) 38 | 3.private void initWalletFile(String nodeID) 39 | 40 | 41 | 42 | 43 | G:修改CLI中,转账 44 | 1. private void send(String from, String to, int amount,String nodeID) 45 | 46 | 47 | H:修改Transaction中 48 | 1. newUTXOTransaction() 49 | 50 | I:在UTXOSet中 51 | 52 | 此时项目中的数据库的创建和钱包地址的创建,文件名称都会带有端口号 53 | ----------------------- 54 | step3:在CLI中,添加一个终端命令,表示启动服务器 55 | 并添加方法:private void startServer(String nodeID, String minerAdd) { 56 | } 57 | 58 | step4:添加Version类 59 | 60 | step5:在uil包下,添加CommandUtils工具类 61 | 62 | 63 | step6:新建net包 64 | step7:新建Server类 65 | 66 | step8:新建ServerSend类,添加sendVersion方法 67 | 68 | step9:新建ServerSocketClient类,添加sendData()方法 69 | 70 | 71 | step10:新建GetBlocks类 72 | 73 | step11:在ServerSend类中,添加sendGetBlocks()方法 74 | 75 | step12:在ServerHandle中,添加handleGetblocks()方法 76 | 77 | step13:在Blockchain类中添加getBlockHashes()方法 78 | 79 | 80 | step14:新建Inv类 81 | 82 | step15:在ServerSend类中,添加sendInv()方法 83 | 84 | step16:在ServerHandle中,添加handleInv()方法 85 | 86 | 87 | step17:新建GetData类 88 | 89 | step18:在ServerSend类中,添加sendGetData()方法 90 | 91 | step19:在ServerHandle中,添加handleGetData()方法 92 | 93 | 94 | 95 | step20:新建BlockData类 96 | 97 | step21:在ServerSend类中,添加sendBlock()方法 98 | 99 | step22:在ServerHandle中,添加handleBlock()方法 100 | 101 | 102 | step23:新建TransactionData类 103 | 104 | step24:在ServerSend类中,添加sendTx()方法 105 | 106 | step25:在ServerHandle中,添加handleTx()方法 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/Main.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain; 2 | 3 | 4 | import cldy.hanru.blockchain.cli.CLI; 5 | 6 | 7 | 8 | /** 9 | * 测试 10 | * 11 | * @author hanru 12 | */ 13 | public class Main { 14 | 15 | public static void main(String[] args) { 16 | 17 | CLI cli = new CLI(args); 18 | cli.run(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/net/BlockData.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.net; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @AllArgsConstructor 8 | @NoArgsConstructor 9 | @Data 10 | /** 11 | * @author hanru 12 | */ 13 | public class BlockData { 14 | String addrFrom; 15 | byte[] blockData; 16 | } 17 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/net/GetBlocks.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.net; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @NoArgsConstructor 8 | @AllArgsConstructor 9 | @Data 10 | /** 11 | * @author hanru 12 | */ 13 | public class GetBlocks { 14 | //getblocks 意为 “给我看一下你有什么区块”(在比特币中,这会更加复杂) 15 | private String addrFrom ; 16 | } 17 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/net/GetData.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.net; 2 | 3 | 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | @AllArgsConstructor 9 | @NoArgsConstructor 10 | @Data 11 | /** 12 | * author hanru 13 | */ 14 | public class GetData { 15 | 16 | //用于某个块或交易的请求,它可以仅包含一个块或交易的 ID。 17 | String addrFrom; 18 | String type; 19 | String hash; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/net/Inv.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.net; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.List; 8 | 9 | @AllArgsConstructor 10 | @NoArgsConstructor 11 | @Data 12 | /** 13 | * @author hanru 14 | */ 15 | public class Inv { 16 | 17 | String addrFrom; //自己的地址 18 | String type; //类型 block tx 19 | List items; //hash的列表 20 | } 21 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/net/ServerConst.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.net; 2 | 3 | /** 4 | * 节点服务的常量 5 | * @author hanru 6 | */ 7 | public class ServerConst { 8 | 9 | public static int NODE_VERSION = 1; 10 | 11 | // 命令 12 | public static final String COMMAND_VERSION = "version"; 13 | public static final String COMMAND_GETBLOCKS = "getblocks"; 14 | public static final String COMMAND_ADDR = "addr"; 15 | public static final String COMMAND_BLOCK = "block"; 16 | public static final String COMMAND_INV = "inv"; 17 | public static final String COMMAND_GETDATA = "getdata"; 18 | public static final String COMMAND_TX = "tx"; 19 | 20 | 21 | // 类型 22 | public static final String BLOCK_TYPE = "block"; 23 | public static final String TX_TYPE = "tx"; 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/net/ServerSend.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.net; 2 | 3 | import cldy.hanru.blockchain.block.Block; 4 | import cldy.hanru.blockchain.block.Blockchain; 5 | import cldy.hanru.blockchain.transaction.Transaction; 6 | import cldy.hanru.blockchain.util.ByteUtils; 7 | import cldy.hanru.blockchain.util.CommandUtils; 8 | import cldy.hanru.blockchain.util.SerializeUtils; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * 发送消息 14 | * @author hanru 15 | */ 16 | public class ServerSend { 17 | 18 | 19 | //发送版本 20 | public static void sendVersion(String toAddress, Blockchain bc) { 21 | long bestHeight = bc.getBestHeight(); 22 | Version version = new Version(ServerConst.NODE_VERSION, bestHeight, Server.nodeAddress); 23 | byte[] payload = SerializeUtils.serialize(version); 24 | System.out.println("sendVersion:"+payload.length); 25 | //version 26 | ServerSocketClient.sendData(toAddress, ServerConst.COMMAND_VERSION, payload); 27 | } 28 | 29 | //COMMAND_GETBLOCKS 30 | public static void sendGetBlocks(String toAddress) { 31 | GetBlocks getBlocks = new GetBlocks(Server.nodeAddress); 32 | byte[] payload = SerializeUtils.serialize(getBlocks); 33 | System.out.println("sendGetBlocks:"+payload.length); 34 | ServerSocketClient.sendData(toAddress, ServerConst.COMMAND_GETBLOCKS, payload); 35 | 36 | } 37 | 38 | // 主节点将自己的所有的区块hash发送给钱包节点 39 | //COMMAND_BLOCK 40 | public static void sendInv(String toAddress, String kind, List blockHashs) { 41 | Inv inv = new Inv(Server.nodeAddress, kind, blockHashs); 42 | byte[] payload = SerializeUtils.serialize(inv); 43 | System.out.println("sendInv:"+payload.length); 44 | ServerSocketClient.sendData(toAddress, ServerConst.COMMAND_INV, payload); 45 | 46 | } 47 | 48 | //COMMAND_GETDATA 49 | public static void sendGetData(String toAddress, String kind, String blockHash) { 50 | GetData getData = new GetData(Server.nodeAddress, kind, blockHash); 51 | 52 | byte[] payload = SerializeUtils.serialize(getData); 53 | System.out.println("sendGetData:"+payload.length); 54 | ServerSocketClient.sendData(toAddress, ServerConst.COMMAND_GETDATA, payload); 55 | 56 | 57 | } 58 | 59 | public static void sendBlock(String toAddress, Block block) { 60 | BlockData blockData = new BlockData(Server.nodeAddress, SerializeUtils.serialize(block)); 61 | 62 | byte[] payload = SerializeUtils.serialize(blockData); 63 | 64 | ServerSocketClient.sendData(toAddress, ServerConst.COMMAND_BLOCK, payload); 65 | 66 | 67 | } 68 | 69 | public static void sendTx(String toAddress, Transaction tx) { 70 | TransactionData txData = new TransactionData(Server.nodeAddress, SerializeUtils.serialize(tx)); 71 | 72 | byte[] payload = SerializeUtils.serialize(txData); 73 | 74 | ServerSocketClient.sendData(toAddress, ServerConst.COMMAND_TX, payload); 75 | 76 | 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/net/ServerSocketClient.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.net; 2 | 3 | import cldy.hanru.blockchain.util.ByteUtils; 4 | import cldy.hanru.blockchain.util.CommandUtils; 5 | 6 | import java.io.IOException; 7 | import java.io.OutputStream; 8 | import java.net.Socket; 9 | 10 | public class ServerSocketClient { 11 | 12 | 13 | public static void sendData(String toAddress, String command, byte[] payload) { 14 | /* 15 | * 第一个参数:IP:InetAddress,String,要连接的服务端的ip 16 | * 17 | * 第二个参数:int port,要连接的服务端的程序的端口。 18 | */ 19 | Socket client = null; 20 | OutputStream outputStream = null; 21 | try { 22 | //step1:创建客户端对象 23 | //指定要链接的主节点的地址和端口 24 | String address = toAddress.substring(0, toAddress.indexOf(":")); 25 | int port = Integer.parseInt(toAddress.substring(toAddress.indexOf(":") + 1)); 26 | client = new Socket(address, port); 27 | System.out.println("客户端已经建立,同时向服务端发送连接请求。。" + address + "," + port); 28 | //step2:从Socket中获取输出流 29 | outputStream = client.getOutputStream(); 30 | outputStream.write(toData(command, payload)); 31 | 32 | } catch (IOException e) { 33 | e.printStackTrace(); 34 | } finally { 35 | //step3:关闭资源 36 | if (outputStream != null) { 37 | try { 38 | outputStream.close(); 39 | } catch (IOException e) { 40 | e.printStackTrace(); 41 | } 42 | } 43 | if (client != null) {//可以省略不写 44 | try { 45 | client.close(); 46 | } catch (IOException e) { 47 | e.printStackTrace(); 48 | } 49 | } 50 | } 51 | } 52 | 53 | 54 | /** 55 | * 将要发送的数据进行封装: 56 | * 格式为:type + data length + data 57 | * 58 | * @param payload 59 | * @return 60 | */ 61 | public static byte[] toData(String command, byte[] payload) { 62 | byte[] commandBytes = CommandUtils.commandToBytes(command); 63 | byte[] dataLenBytes = ByteUtils.toBytes(payload.length); 64 | System.out.println("写出payload长度:" + payload.length); 65 | byte[] bytes = ByteUtils.merge(commandBytes,dataLenBytes,payload); 66 | return bytes; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/net/TransactionData.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.net; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @AllArgsConstructor 8 | @NoArgsConstructor 9 | @Data 10 | /** 11 | * @author hanru 12 | */ 13 | public class TransactionData { 14 | 15 | String addrFrom; 16 | byte[] txData; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/net/Version.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.net; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | * 区块链版本 9 | * @author hanru 10 | */ 11 | @AllArgsConstructor 12 | @Data 13 | @NoArgsConstructor 14 | public class Version { 15 | /** 16 | * 版本 17 | */ 18 | public int version ; 19 | /** 20 | * 当前节点区块的高度 21 | */ 22 | private long bestHeight; 23 | /** 24 | * 当前节点的地址 25 | */ 26 | private String nodeAddress; 27 | } 28 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/pow/PowResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.pow; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * 工作量计算结果 8 | * @author ruby 9 | * 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class PowResult { 14 | 15 | /** 16 | * 计数器 17 | */ 18 | private long nonce; 19 | /** 20 | * hash值 21 | */ 22 | private String hash; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Map; 8 | 9 | /** 10 | * @author hanru 11 | */ 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | @Data 15 | public class SpendableOutputResult { 16 | 17 | /** 18 | * 交易时的支付金额 19 | */ 20 | private int accumulated; 21 | /** 22 | * 未花费的交易 23 | */ 24 | private Map unspentOuts; 25 | } 26 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import cldy.hanru.blockchain.util.AddressUtils; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.util.Arrays; 9 | 10 | /** 11 | * @author hanru 12 | */ 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | @Data 16 | public class TXInput { 17 | 18 | 19 | /** 20 | * 交易Id的hash值 21 | */ 22 | private byte[] txId; 23 | /** 24 | * 交易输出索引 25 | */ 26 | private int txOutputIndex; 27 | /** 28 | * 解锁脚本 29 | */ 30 | // private String scriptSig; 31 | 32 | 33 | /** 34 | * 签名 35 | */ 36 | private byte[] signature; 37 | /** 38 | * 公钥 39 | */ 40 | private byte[] pubKey; 41 | 42 | 43 | /** 44 | * 判断解锁数据是否能够解锁交易输出 45 | * 46 | * @param unlockingData 47 | * @return 48 | */ 49 | // public boolean canUnlockOutputWith(String unlockingData) { 50 | // return this.getScriptSig().endsWith(unlockingData); 51 | // } 52 | 53 | 54 | /** 55 | * 检查公钥hash是否用于交易输入 56 | * 57 | * @param pubKeyHash 58 | * @return 59 | */ 60 | public boolean usesKey(byte[] pubKeyHash) { 61 | byte[] lockingHash = AddressUtils.ripeMD160Hash(this.getPubKey()); 62 | return Arrays.equals(lockingHash, pubKeyHash); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | 4 | import cldy.hanru.blockchain.util.Base58Check; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | import java.util.Arrays; 10 | 11 | /** 12 | * @author hanru 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | public class TXOutput { 18 | 19 | /** 20 | * 数值金额 21 | */ 22 | private int value; 23 | /** 24 | * 锁定脚本 25 | */ 26 | // private String scriptPubKey; 27 | 28 | 29 | /** 30 | * 公钥Hash 31 | */ 32 | private byte[] pubKeyHash; 33 | /** 34 | * 判断解锁数据是否能够解锁交易输出 35 | * 36 | * @param unlockingData 37 | * @return 38 | */ 39 | // public boolean canBeUnlockedWith(String unlockingData) { 40 | // 41 | // return this.getScriptPubKey().endsWith(unlockingData); 42 | // } 43 | 44 | 45 | /** 46 | * 检查交易输出是否能够使用指定的公钥 47 | * 48 | * @param pubKeyHash 49 | * @return 50 | */ 51 | public boolean isLockedWithKey(byte[] pubKeyHash) { 52 | return Arrays.equals(this.getPubKeyHash(), pubKeyHash); 53 | } 54 | 55 | /** 56 | * 创建交易输出 57 | * 58 | * @param value 59 | * @param address 60 | * @return 61 | */ 62 | public static TXOutput newTXOutput(int value, String address) { 63 | // 反向转化为 byte 数组 64 | byte[] versionedPayload = Base58Check.base58ToBytes(address); 65 | byte[] pubKeyHash = Arrays.copyOfRange(versionedPayload, 1, versionedPayload.length); 66 | return new TXOutput(value, pubKeyHash); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/util/AddressUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import org.apache.commons.codec.digest.DigestUtils; 4 | import org.bouncycastle.crypto.digests.RIPEMD160Digest; 5 | import org.bouncycastle.util.Arrays; 6 | 7 | /** 8 | * 地址工具类 9 | * 10 | * @author hanru 11 | * 12 | */ 13 | public class AddressUtils { 14 | /** 15 | * 双重Hash 16 | * 17 | * @param data 18 | * @return 19 | */ 20 | public static byte[] doubleHash(byte[] data) { 21 | return DigestUtils.sha256(DigestUtils.sha256(data)); 22 | } 23 | 24 | /** 25 | * 计算公钥的 RIPEMD160 Hash值 26 | * 27 | * @param pubKey 公钥 28 | * @return ipeMD160Hash(sha256 ( pubkey)) 29 | */ 30 | public static byte[] ripeMD160Hash(byte[] pubKey) { 31 | //1. 先对公钥做 sha256 处理 32 | byte[] shaHashedKey = DigestUtils.sha256(pubKey); 33 | RIPEMD160Digest ripemd160 = new RIPEMD160Digest(); 34 | ripemd160.update(shaHashedKey, 0, shaHashedKey.length); 35 | byte[] output = new byte[ripemd160.getDigestSize()]; 36 | ripemd160.doFinal(output, 0); 37 | return output; 38 | } 39 | /** 40 | * 生成公钥的校验码 41 | * 42 | * @param payload 43 | * @return 44 | */ 45 | public static byte[] checksum(byte[] payload) { 46 | return Arrays.copyOfRange(doubleHash(payload), 0, 4); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.codec.binary.Hex; 8 | import org.apache.commons.lang3.ArrayUtils; 9 | 10 | /** 11 | * 字节数组工具类 12 | * @author hanru 13 | * 14 | */ 15 | public class ByteUtils { 16 | 17 | public static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 18 | 19 | 20 | /** 21 | * 将多个字节数组合并成一个字节数组 22 | * @param bytes 23 | * @return 24 | */ 25 | public static byte[] merge(byte[]... bytes) { 26 | Stream stream = Stream.of(); 27 | for (byte[] b : bytes) { 28 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 29 | } 30 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 31 | } 32 | 33 | /** 34 | * long 转化为 byte[] 35 | * @param val 36 | * @return 37 | */ 38 | public static byte[] toBytes(long val) { 39 | 40 | 41 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 42 | } 43 | 44 | 45 | /** 46 | * int 类型转 byte[] 47 | * 48 | * @param val 49 | * @return 50 | */ 51 | public static byte[] toBytes(int val) { 52 | return ByteBuffer.allocate(Integer.BYTES).putInt(val).array(); 53 | } 54 | 55 | /** 56 | * byte[] 转化为 int 57 | * 58 | * @param bytes 59 | * @return 60 | */ 61 | public static int toInt(byte[] bytes) { 62 | return ByteBuffer.wrap(bytes).getInt(); 63 | } 64 | 65 | 66 | /** 67 | * byte[] 转为16进制,String 68 | * @param bytes 69 | * @return 70 | */ 71 | public static String bytesToHexString(byte[] bytes) { 72 | StringBuilder buf = new StringBuilder(bytes.length * 2); 73 | for(byte b : bytes) { // 使用String的format方法进行转换 74 | buf.append(String.format("%02x", new Integer(b & 0xff))); 75 | } 76 | 77 | return buf.toString(); 78 | } 79 | 80 | 81 | } 82 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/util/CommandUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import org.apache.commons.lang3.ArrayUtils; 4 | 5 | public class CommandUtils { 6 | 7 | /** 8 | * 规定message type转化后的byte[]长度 9 | */ 10 | public final static int MESSAGE_TYPE_DATA_LENGTH = 12; 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | /** 19 | * 将消息类型字符串转化长度为12的字节数组 20 | * @param command 21 | * @return 22 | */ 23 | public static byte[] commandToBytes(String command ){ 24 | byte[] byteArray = new byte[MESSAGE_TYPE_DATA_LENGTH]; 25 | for (int i = 0; i < command.getBytes().length; i++) { 26 | byteArray[i] = command.getBytes()[i]; 27 | } 28 | return byteArray; 29 | } 30 | 31 | 32 | /** 33 | * 消息类型byte[] 转化为字符串 34 | * 35 | * @param bytes 36 | * @return 37 | */ 38 | public static String bytesToCommand(byte[] bytes) { 39 | byte[] typeBytes = ArrayUtils.removeAllOccurences(bytes, (byte) 0); 40 | return new String(typeBytes); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /part10_Network/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import com.esotericsoftware.kryo.Kryo; 4 | import com.esotericsoftware.kryo.io.Input; 5 | import com.esotericsoftware.kryo.io.Output; 6 | 7 | /** 8 | * 序列化工具类 9 | * @author hanru 10 | */ 11 | public class SerializeUtils { 12 | 13 | /** 14 | * 序列化 15 | * @param object 需要序列化的对象 16 | * @return 17 | */ 18 | public static byte[] serialize(Object object) { 19 | Output output = new Output(4096, -1); 20 | new Kryo().writeClassAndObject(output, object); 21 | byte[] bytes = output.toBytes(); 22 | output.close(); 23 | return bytes; 24 | } 25 | 26 | /** 27 | * 反序列化 28 | * @param bytes 对象对应的字节数组 29 | * @return 30 | */ 31 | public static Object deserialize(byte[] bytes) { 32 | Input input = new Input(bytes); 33 | Object obj = new Kryo().readClassAndObject(input); 34 | input.close(); 35 | return obj; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /part10_Network/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part10_Network/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/Main.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/block/Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/block/Block.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/block/Blockchain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/block/Blockchain.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/cli/CLI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/cli/CLI.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/BlockData.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/BlockData.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/GetBlocks.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/GetBlocks.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/GetData.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/GetData.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/Inv.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/Inv.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/Server.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/Server.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/ServerConst.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/ServerConst.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/ServerHandle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/ServerHandle.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/ServerSend.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/ServerSend.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/ServerSocketClient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/ServerSocketClient.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/ServerThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/ServerThread.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/TransactionData.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/TransactionData.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/net/Version.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/net/Version.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/pow/PowResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/pow/PowResult.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/transaction/MerkleTree$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/transaction/MerkleTree$Node.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/transaction/MerkleTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/transaction/MerkleTree.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/transaction/TXInput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/transaction/TXInput.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/transaction/Transaction.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/transaction/Transaction.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/transaction/UTXOSet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/transaction/UTXOSet.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/util/AddressUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/util/AddressUtils.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/util/Base58Check.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/util/Base58Check.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/util/ByteUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/util/ByteUtils.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/util/CommandUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/util/CommandUtils.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/wallet/Wallet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/wallet/Wallet.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class -------------------------------------------------------------------------------- /part10_Network/target/classes/cldy/hanru/blockchain/wallet/WalletUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/classes/cldy/hanru/blockchain/wallet/WalletUtils.class -------------------------------------------------------------------------------- /part10_Network/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Thu Oct 11 10:35:17 CST 2018 3 | version=1.0-SNAPSHOT 4 | groupId=cldy.hanru.blockchain 5 | artifactId=part10_Network 6 | -------------------------------------------------------------------------------- /part10_Network/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | cldy/hanru/blockchain/cli/CLI.class 2 | cldy/hanru/blockchain/wallet/WalletUtils.class 3 | cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class 4 | cldy/hanru/blockchain/net/Inv.class 5 | cldy/hanru/blockchain/net/Version.class 6 | cldy/hanru/blockchain/net/ServerThread.class 7 | cldy/hanru/blockchain/util/AddressUtils.class 8 | cldy/hanru/blockchain/store/RocksDBUtils.class 9 | cldy/hanru/blockchain/net/ServerHandle.class 10 | cldy/hanru/blockchain/wallet/Wallet.class 11 | cldy/hanru/blockchain/transaction/SpendableOutputResult.class 12 | cldy/hanru/blockchain/transaction/TXOutput.class 13 | cldy/hanru/blockchain/block/Block.class 14 | cldy/hanru/blockchain/transaction/MerkleTree$Node.class 15 | cldy/hanru/blockchain/pow/ProofOfWork.class 16 | cldy/hanru/blockchain/net/BlockData.class 17 | cldy/hanru/blockchain/block/Blockchain.class 18 | cldy/hanru/blockchain/net/ServerConst.class 19 | cldy/hanru/blockchain/net/ServerSend.class 20 | cldy/hanru/blockchain/util/SerializeUtils.class 21 | cldy/hanru/blockchain/net/TransactionData.class 22 | cldy/hanru/blockchain/net/GetBlocks.class 23 | cldy/hanru/blockchain/net/ServerSocketClient.class 24 | cldy/hanru/blockchain/Main.class 25 | cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class 26 | cldy/hanru/blockchain/net/GetData.class 27 | cldy/hanru/blockchain/util/CommandUtils.class 28 | cldy/hanru/blockchain/transaction/Transaction.class 29 | cldy/hanru/blockchain/transaction/TXInput.class 30 | cldy/hanru/blockchain/net/Server.class 31 | cldy/hanru/blockchain/util/Base58Check.class 32 | cldy/hanru/blockchain/pow/PowResult.class 33 | cldy/hanru/blockchain/util/ByteUtils.class 34 | cldy/hanru/blockchain/transaction/MerkleTree.class 35 | cldy/hanru/blockchain/transaction/UTXOSet.class 36 | -------------------------------------------------------------------------------- /part10_Network/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/block/Blockchain.java 2 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/transaction/MerkleTree.java 3 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java 4 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java 5 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/transaction/Transaction.java 6 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/wallet/WalletUtils.java 7 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java 8 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/cli/CLI.java 9 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/block/Block.java 10 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/util/Base58Check.java 11 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/wallet/Wallet.java 12 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/store/RocksDBUtils.java 13 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/Main.java 14 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/transaction/UTXOSet.java 15 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/pow/ProofOfWork.java 16 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java 17 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/util/AddressUtils.java 18 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java 19 | /Users/ruby/JavaProjects/part10_Network/src/main/java/cldy/hanru/blockchain/pow/PowResult.java 20 | -------------------------------------------------------------------------------- /part10_Network/target/part10_Network-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/part10_Network-jar-with-dependencies.jar -------------------------------------------------------------------------------- /part10_Network/target/part10_Network.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/target/part10_Network.jar -------------------------------------------------------------------------------- /part10_Network/wallet_3000.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/wallet_3000.dat -------------------------------------------------------------------------------- /part10_Network/wallet_3001.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/wallet_3001.dat -------------------------------------------------------------------------------- /part10_Network/wallet_3002.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part10_Network/wallet_3002.dat -------------------------------------------------------------------------------- /part1_Basic_Prototype/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /part1_Basic_Prototype/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /part1_Basic_Prototype/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /part1_Basic_Prototype/src/main/java/cldy/hanru/blockchain/Main.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Date; 5 | 6 | import cldy.hanru.blockchain.block.Block; 7 | import cldy.hanru.blockchain.block.Blockchain; 8 | 9 | 10 | /** 11 | * 测试 12 | * 13 | * @author hanru 14 | * 15 | */ 16 | public class Main { 17 | 18 | public static void main(String[] args) { 19 | 20 | // // 1.创建创世区块 21 | // Block genesisBlock = Block.newGenesisBlock(); 22 | // System.out.println("创世区块的信息:"); 23 | // System.out.println("\thash:" + genesisBlock.getHash()); 24 | // System.out.println("\tprevBlockHash:" + genesisBlock.getPrevBlockHash()); 25 | // System.out.println("\tdata:" + genesisBlock.getData()); 26 | // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 27 | // String date = sdf.format(new Date(genesisBlock.getTimeStamp()*1000L)); 28 | // 29 | // System.out.println("\ttimeStamp:" + date); 30 | // 31 | // //2.创建第二个区块 32 | // Block block2 = Block.newBlock(genesisBlock.getHash(), "I am hanru",1); 33 | // System.out.println("第二个区块的信息:"); 34 | // System.out.println("\thash:" + block2.getHash()); 35 | // System.out.println("\tprevBlockHash:" + block2.getPrevBlockHash()); 36 | // System.out.println("\tdata:" + block2.getData()); 37 | // String date2 = sdf.format(new Date(block2.getTimeStamp()*1000L)); 38 | // System.out.println("\ttimeStamp:" + date2); 39 | 40 | 41 | 42 | //3.测试Blockchain 43 | 44 | Blockchain blockchain = Blockchain.newBlockchain(); 45 | 46 | 47 | System.out.println("创世链的信息:"); 48 | System.out.println("区块的长度:"+blockchain.getBlockList().size()); 49 | 50 | //4.添加区块 51 | blockchain.addBlock("Send 1 BTC to 韩茹"); 52 | blockchain.addBlock("Send 2 more BTC to ruby"); 53 | blockchain.addBlock("Send 4 more BTC to 王二狗"); 54 | 55 | for(int i=0;i blockList; 22 | 23 | /** 24 | * 创建区块链 25 | * @return 26 | */ 27 | public static Blockchain newBlockchain() { 28 | List blocks = new LinkedList<>(); 29 | blocks.add(Block.newGenesisBlock()); 30 | return new Blockchain(blocks); 31 | } 32 | 33 | /** 34 | * 根据block,添加区块 35 | * @param block 36 | */ 37 | public void addBlock(Block block) { 38 | this.blockList.add(block); 39 | } 40 | 41 | /** 42 | * 根据data添加区块 43 | * @param data 44 | */ 45 | public void addBlock(String data) { 46 | Block previousBlock = blockList.get(blockList.size() - 1); 47 | this.addBlock(Block.newBlock(previousBlock.getHash(), data,previousBlock.getHeight()+1)); 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /part1_Basic_Prototype/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.lang3.ArrayUtils; 8 | 9 | /** 10 | * 字节数组工具类 11 | * @author hanru 12 | * 13 | */ 14 | public class ByteUtils { 15 | /** 16 | * 将多个字节数组合并成一个字节数组 17 | * @param bytes 18 | * @return 19 | */ 20 | public static byte[] merge(byte[]... bytes) { 21 | Stream stream = Stream.of(); 22 | for (byte[] b : bytes) { 23 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 24 | } 25 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 26 | } 27 | 28 | /** 29 | * long 转化为 byte[] 30 | * @param val 31 | * @return 32 | */ 33 | public static byte[] toBytes(long val) { 34 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /part1_Basic_Prototype/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part1_Basic_Prototype/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part1_Basic_Prototype/target/classes/cldy/hanru/blockchain/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part1_Basic_Prototype/target/classes/cldy/hanru/blockchain/Main.class -------------------------------------------------------------------------------- /part1_Basic_Prototype/target/classes/cldy/hanru/blockchain/block/Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part1_Basic_Prototype/target/classes/cldy/hanru/blockchain/block/Block.class -------------------------------------------------------------------------------- /part1_Basic_Prototype/target/classes/cldy/hanru/blockchain/block/Blockchain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part1_Basic_Prototype/target/classes/cldy/hanru/blockchain/block/Blockchain.class -------------------------------------------------------------------------------- /part1_Basic_Prototype/target/classes/cldy/hanru/blockchain/util/ByteUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part1_Basic_Prototype/target/classes/cldy/hanru/blockchain/util/ByteUtils.class -------------------------------------------------------------------------------- /part2_Proof_Of_Work/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /part2_Proof_Of_Work/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /part2_Proof_Of_Work/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /part2_Proof_Of_Work/src/main/java/cldy/hanru/blockchain/block/Block.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.block; 2 | 3 | import java.time.Instant; 4 | 5 | import org.apache.commons.codec.binary.Hex; 6 | 7 | import cldy.hanru.blockchain.pow.PowResult; 8 | import cldy.hanru.blockchain.pow.ProofOfWork; 9 | import lombok.AllArgsConstructor; 10 | import lombok.Data; 11 | /** 12 | * 区块 13 | * @author hanru 14 | * 15 | */ 16 | @AllArgsConstructor 17 | @Data 18 | public class Block { 19 | 20 | /** 21 | * 区块hash值 22 | */ 23 | private String hash; 24 | 25 | /** 26 | * 前一个区块的hash值 27 | */ 28 | private String prevBlockHash; 29 | 30 | /** 31 | * 区块数据,未来会替换为交易 32 | */ 33 | private String data; 34 | 35 | /** 36 | * 时间戳,单位秒 37 | */ 38 | private long timeStamp; 39 | 40 | /** 41 | * 区块的高度 42 | */ 43 | private long height; 44 | 45 | /** 46 | * 工作量证明计数器 47 | */ 48 | private long nonce; 49 | 50 | 51 | 52 | 53 | /** 54 | * 创建新的区块 55 | * 56 | * @param previousHash 57 | * @param data 58 | * @return 59 | */ 60 | public static Block newBlock(String previousHash, String data,long height) { 61 | Block block = new Block("", previousHash, data, Instant.now().getEpochSecond(),height,0); 62 | ProofOfWork pow = ProofOfWork.newProofOfWork(block); 63 | PowResult powResult = pow.run(); 64 | block.setHash(powResult.getHash()); 65 | block.setNonce(powResult.getNonce()); 66 | // block.setHash(); 67 | return block; 68 | } 69 | 70 | /** 71 | * 设置Hash 72 | * 注意:在准备区块数据时,一定要从原始数据类型转化为byte[],不能直接从字符串进行转换 73 | */ 74 | // private void setHash() { 75 | // byte[] prevBlockHashBytes = {}; 76 | // if (StringUtils.isNoneBlank(this.getPrevBlockHash())) { 77 | // prevBlockHashBytes = new BigInteger(this.getPrevBlockHash(), 16).toByteArray(); 78 | // } 79 | // 80 | // byte[] headers = ByteUtils.merge(prevBlockHashBytes, this.getData().getBytes(), 81 | // ByteUtils.toBytes(this.getTimeStamp())); 82 | // 83 | // this.setHash(DigestUtils.sha256Hex(headers)); 84 | // } 85 | 86 | private static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 87 | 88 | /** 89 | * 创建创世区块 90 | * @return 91 | */ 92 | public static Block newGenesisBlock() { 93 | return Block.newBlock(ZERO_HASH, "Genesis Block",0); 94 | } 95 | 96 | 97 | } 98 | -------------------------------------------------------------------------------- /part2_Proof_Of_Work/src/main/java/cldy/hanru/blockchain/block/Blockchain.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.block; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | import lombok.AllArgsConstructor; 7 | import lombok.Data; 8 | 9 | /** 10 | * 区块链 11 | * @author hanru 12 | * 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | public class Blockchain { 17 | 18 | /** 19 | * 存储区块的集合 20 | */ 21 | private List blockList; 22 | 23 | /** 24 | * 创建区块链 25 | * @return 26 | */ 27 | public static Blockchain newBlockchain() { 28 | List blocks = new LinkedList<>(); 29 | blocks.add(Block.newGenesisBlock()); 30 | return new Blockchain(blocks); 31 | } 32 | 33 | /** 34 | * 根据block,添加区块 35 | * @param block 36 | */ 37 | public void addBlock(Block block) { 38 | this.blockList.add(block); 39 | } 40 | 41 | /** 42 | * 根据data添加区块 43 | * @param data 44 | */ 45 | public void addBlock(String data) { 46 | Block previousBlock = blockList.get(blockList.size() - 1); 47 | this.addBlock(Block.newBlock(previousBlock.getHash(), data,previousBlock.getHeight()+1)); 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /part2_Proof_Of_Work/src/main/java/cldy/hanru/blockchain/pow/PowResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.pow; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * 工作量计算结果 8 | * @author ruby 9 | * 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class PowResult { 14 | 15 | /** 16 | * 计数器 17 | */ 18 | private long nonce; 19 | /** 20 | * hash值 21 | */ 22 | private String hash; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /part2_Proof_Of_Work/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.lang3.ArrayUtils; 8 | 9 | /** 10 | * 字节数组工具类 11 | * @author hanru 12 | * 13 | */ 14 | public class ByteUtils { 15 | /** 16 | * 将多个字节数组合并成一个字节数组 17 | * @param bytes 18 | * @return 19 | */ 20 | public static byte[] merge(byte[]... bytes) { 21 | Stream stream = Stream.of(); 22 | for (byte[] b : bytes) { 23 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 24 | } 25 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 26 | } 27 | 28 | /** 29 | * long 转化为 byte[] 30 | * @param val 31 | * @return 32 | */ 33 | public static byte[] toBytes(long val) { 34 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /part2_Proof_Of_Work/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part2_Proof_Of_Work/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/Main.class -------------------------------------------------------------------------------- /part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/block/Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/block/Block.class -------------------------------------------------------------------------------- /part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/block/Blockchain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/block/Blockchain.class -------------------------------------------------------------------------------- /part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/pow/PowResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/pow/PowResult.class -------------------------------------------------------------------------------- /part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class -------------------------------------------------------------------------------- /part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/util/ByteUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part2_Proof_Of_Work/target/classes/cldy/hanru/blockchain/util/ByteUtils.class -------------------------------------------------------------------------------- /part3_Persistence/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /part3_Persistence/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /part3_Persistence/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /part3_Persistence/blockchain.db/000003.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/blockchain.db/000003.log -------------------------------------------------------------------------------- /part3_Persistence/blockchain.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000001 2 | -------------------------------------------------------------------------------- /part3_Persistence/blockchain.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c2e652e136775-8544aa7aedb830b3 -------------------------------------------------------------------------------- /part3_Persistence/blockchain.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/blockchain.db/LOCK -------------------------------------------------------------------------------- /part3_Persistence/blockchain.db/MANIFEST-000001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/blockchain.db/MANIFEST-000001 -------------------------------------------------------------------------------- /part3_Persistence/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | step1:在uitl包下创建SerializeUtils.java工具类 3 | serialize() 4 | deserialize() 5 | 6 | step2:修改pow.xml文件,添加序列化的依赖包 7 | kryo 8 | 9 | step3新建包store,用于持久化 10 | 11 | 12 | step4:修改pow.xml文件,添加rocksdb的依赖包 13 | rocksdbjni 14 | 15 | 16 | step5:创建RocksDBUtils工具类 17 | A:创建RocksDBUtils类 18 | B:添加静态常量: 19 | 文件名: DB_FILE = "blockchain.db"; 20 | 区块桶:BLOCKS_BUCKET_KEY = "blocks"; 21 | 最后一个区块hash:LAST_BLOCK_KEY = "l"; 22 | 23 | C:单例获取该类实例 24 | D:添加两个字段属性 25 | private RocksDB db; 26 | private Map blocksBucket; 27 | 28 | E:添加方法openDB() 29 | F:initBlockBucket() 30 | 31 | step6:修改pow.xml,添加google的包 32 | 需要全局翻墙 33 | 34 | step7:继续添加方法 35 | G:putBlock(),存储区块 36 | H:getBlock(),查询区块 37 | I:putLastBlockHash(), 38 | J:getLastBlockHash(), 39 | K:closeDB() 40 | 41 | 42 | 43 | step8:修改blockchain类, 44 | A:删除lst集合,添加lastBlockHash字段 45 | 46 | B:修改newBlockchain() 47 | 48 | C:修改两个addBlock()方法 49 | 50 | 51 | step9:遍历区块 52 | -------------------------------------------------------------------------------- /part3_Persistence/src/main/java/cldy/hanru/blockchain/block/Block.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.block; 2 | 3 | import java.time.Instant; 4 | 5 | import lombok.NoArgsConstructor; 6 | import org.apache.commons.codec.binary.Hex; 7 | 8 | import cldy.hanru.blockchain.pow.PowResult; 9 | import cldy.hanru.blockchain.pow.ProofOfWork; 10 | import lombok.AllArgsConstructor; 11 | import lombok.Data; 12 | /** 13 | * 区块 14 | * @author hanru 15 | * 16 | */ 17 | @AllArgsConstructor 18 | @NoArgsConstructor 19 | @Data 20 | public class Block { 21 | 22 | /** 23 | * 区块hash值 24 | */ 25 | private String hash; 26 | 27 | /** 28 | * 前一个区块的hash值 29 | */ 30 | private String prevBlockHash; 31 | 32 | /** 33 | * 区块数据,未来会替换为交易 34 | */ 35 | private String data; 36 | 37 | /** 38 | * 时间戳,单位秒 39 | */ 40 | private long timeStamp; 41 | 42 | /** 43 | * 区块的高度 44 | */ 45 | private long height; 46 | 47 | /** 48 | * 工作量证明计数器 49 | */ 50 | private long nonce; 51 | 52 | 53 | 54 | 55 | /** 56 | * 创建新的区块 57 | * 58 | * @param previousHash 59 | * @param data 60 | * @return 61 | */ 62 | public static Block newBlock(String previousHash, String data,long height) { 63 | Block block = new Block("", previousHash, data, Instant.now().getEpochSecond(),height,0); 64 | ProofOfWork pow = ProofOfWork.newProofOfWork(block); 65 | PowResult powResult = pow.run(); 66 | block.setHash(powResult.getHash()); 67 | block.setNonce(powResult.getNonce()); 68 | // block.setHash(); 69 | return block; 70 | } 71 | 72 | /** 73 | * 设置Hash 74 | * 注意:在准备区块数据时,一定要从原始数据类型转化为byte[],不能直接从字符串进行转换 75 | */ 76 | // private void setHash() { 77 | // byte[] prevBlockHashBytes = {}; 78 | // if (StringUtils.isNoneBlank(this.getPrevBlockHash())) { 79 | // prevBlockHashBytes = new BigInteger(this.getPrevBlockHash(), 16).toByteArray(); 80 | // } 81 | // 82 | // byte[] headers = ByteUtils.merge(prevBlockHashBytes, this.getData().getBytes(), 83 | // ByteUtils.toBytes(this.getTimeStamp())); 84 | // 85 | // this.setHash(DigestUtils.sha256Hex(headers)); 86 | // } 87 | 88 | private static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 89 | 90 | /** 91 | * 创建创世区块 92 | * @return 93 | */ 94 | public static Block newGenesisBlock() { 95 | return Block.newBlock(ZERO_HASH, "Genesis Block",0); 96 | } 97 | 98 | 99 | } 100 | -------------------------------------------------------------------------------- /part3_Persistence/src/main/java/cldy/hanru/blockchain/pow/PowResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.pow; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * 工作量计算结果 8 | * @author ruby 9 | * 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class PowResult { 14 | 15 | /** 16 | * 计数器 17 | */ 18 | private long nonce; 19 | /** 20 | * hash值 21 | */ 22 | private String hash; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /part3_Persistence/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.codec.binary.Hex; 8 | import org.apache.commons.lang3.ArrayUtils; 9 | 10 | /** 11 | * 字节数组工具类 12 | * @author hanru 13 | * 14 | */ 15 | public class ByteUtils { 16 | 17 | public static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 18 | 19 | 20 | /** 21 | * 将多个字节数组合并成一个字节数组 22 | * @param bytes 23 | * @return 24 | */ 25 | public static byte[] merge(byte[]... bytes) { 26 | Stream stream = Stream.of(); 27 | for (byte[] b : bytes) { 28 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 29 | } 30 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 31 | } 32 | 33 | /** 34 | * long 转化为 byte[] 35 | * @param val 36 | * @return 37 | */ 38 | public static byte[] toBytes(long val) { 39 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /part3_Persistence/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import com.esotericsoftware.kryo.Kryo; 4 | import com.esotericsoftware.kryo.io.Input; 5 | import com.esotericsoftware.kryo.io.Output; 6 | 7 | /** 8 | * 序列化工具类 9 | * @author hanru 10 | */ 11 | public class SerializeUtils { 12 | 13 | /** 14 | * 序列化 15 | * @param object 需要序列化的对象 16 | * @return 17 | */ 18 | public static byte[] serialize(Object object) { 19 | Output output = new Output(4096, -1); 20 | new Kryo().writeClassAndObject(output, object); 21 | byte[] bytes = output.toBytes(); 22 | output.close(); 23 | return bytes; 24 | } 25 | 26 | /** 27 | * 反序列化 28 | * @param bytes 对象对应的字节数组 29 | * @return 30 | */ 31 | public static Object deserialize(byte[] bytes) { 32 | Input input = new Input(bytes); 33 | Object obj = new Kryo().readClassAndObject(input); 34 | input.close(); 35 | return obj; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /part3_Persistence/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part3_Persistence/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part3_Persistence/target/classes/cldy/hanru/blockchain/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/target/classes/cldy/hanru/blockchain/Main.class -------------------------------------------------------------------------------- /part3_Persistence/target/classes/cldy/hanru/blockchain/block/Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/target/classes/cldy/hanru/blockchain/block/Block.class -------------------------------------------------------------------------------- /part3_Persistence/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class -------------------------------------------------------------------------------- /part3_Persistence/target/classes/cldy/hanru/blockchain/block/Blockchain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/target/classes/cldy/hanru/blockchain/block/Blockchain.class -------------------------------------------------------------------------------- /part3_Persistence/target/classes/cldy/hanru/blockchain/pow/PowResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/target/classes/cldy/hanru/blockchain/pow/PowResult.class -------------------------------------------------------------------------------- /part3_Persistence/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class -------------------------------------------------------------------------------- /part3_Persistence/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class -------------------------------------------------------------------------------- /part3_Persistence/target/classes/cldy/hanru/blockchain/util/ByteUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/target/classes/cldy/hanru/blockchain/util/ByteUtils.class -------------------------------------------------------------------------------- /part3_Persistence/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part3_Persistence/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class -------------------------------------------------------------------------------- /part4_CLI/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /part4_CLI/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /part4_CLI/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /part4_CLI/blockchain.db/000004.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/blockchain.db/000004.sst -------------------------------------------------------------------------------- /part4_CLI/blockchain.db/000010.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/blockchain.db/000010.sst -------------------------------------------------------------------------------- /part4_CLI/blockchain.db/000013.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/blockchain.db/000013.sst -------------------------------------------------------------------------------- /part4_CLI/blockchain.db/000018.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/blockchain.db/000018.log -------------------------------------------------------------------------------- /part4_CLI/blockchain.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000017 2 | -------------------------------------------------------------------------------- /part4_CLI/blockchain.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c31e7442174f1-804cd93d4a28412e -------------------------------------------------------------------------------- /part4_CLI/blockchain.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/blockchain.db/LOCK -------------------------------------------------------------------------------- /part4_CLI/blockchain.db/MANIFEST-000017: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/blockchain.db/MANIFEST-000017 -------------------------------------------------------------------------------- /part4_CLI/blockchain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # Check if the jar has been built. 6 | if [ ! -e target/part4_CLI-jar-with-dependencies.jar ]; then 7 | echo "Compiling blockchain project to a JAR" 8 | mvn package -DskipTests 9 | fi 10 | 11 | java -jar target/part4_CLI-jar-with-dependencies.jar "$@" 12 | 13 | -------------------------------------------------------------------------------- /part4_CLI/readme.txt: -------------------------------------------------------------------------------- 1 | step1:修改pow.xml文件,添加cli的依赖包 2 | 3 | step2:添加blockchain.sh脚本 4 | 5 | step3:创建cli包 6 | 7 | step4.修改main文件 8 | 9 | step5:mvn package 打包 10 | 11 | 或者添加sh脚本文件 -------------------------------------------------------------------------------- /part4_CLI/src/main/java/cldy/hanru/blockchain/block/Block.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.block; 2 | 3 | import java.time.Instant; 4 | 5 | import lombok.NoArgsConstructor; 6 | import org.apache.commons.codec.binary.Hex; 7 | 8 | import cldy.hanru.blockchain.pow.PowResult; 9 | import cldy.hanru.blockchain.pow.ProofOfWork; 10 | import lombok.AllArgsConstructor; 11 | import lombok.Data; 12 | /** 13 | * 区块 14 | * @author hanru 15 | * 16 | */ 17 | @AllArgsConstructor 18 | @NoArgsConstructor 19 | @Data 20 | public class Block { 21 | 22 | /** 23 | * 区块hash值 24 | */ 25 | private String hash; 26 | 27 | /** 28 | * 前一个区块的hash值 29 | */ 30 | private String prevBlockHash; 31 | 32 | /** 33 | * 区块数据,未来会替换为交易 34 | */ 35 | private String data; 36 | 37 | /** 38 | * 时间戳,单位秒 39 | */ 40 | private long timeStamp; 41 | 42 | /** 43 | * 区块的高度 44 | */ 45 | private long height; 46 | 47 | /** 48 | * 工作量证明计数器 49 | */ 50 | private long nonce; 51 | 52 | 53 | 54 | 55 | /** 56 | * 创建新的区块 57 | * 58 | * @param previousHash 59 | * @param data 60 | * @return 61 | */ 62 | public static Block newBlock(String previousHash, String data,long height) { 63 | Block block = new Block("", previousHash, data, Instant.now().getEpochSecond(),height,0); 64 | ProofOfWork pow = ProofOfWork.newProofOfWork(block); 65 | PowResult powResult = pow.run(); 66 | block.setHash(powResult.getHash()); 67 | block.setNonce(powResult.getNonce()); 68 | // block.setHash(); 69 | return block; 70 | } 71 | 72 | /** 73 | * 设置Hash 74 | * 注意:在准备区块数据时,一定要从原始数据类型转化为byte[],不能直接从字符串进行转换 75 | */ 76 | // private void setHash() { 77 | // byte[] prevBlockHashBytes = {}; 78 | // if (StringUtils.isNoneBlank(this.getPrevBlockHash())) { 79 | // prevBlockHashBytes = new BigInteger(this.getPrevBlockHash(), 16).toByteArray(); 80 | // } 81 | // 82 | // byte[] headers = ByteUtils.merge(prevBlockHashBytes, this.getData().getBytes(), 83 | // ByteUtils.toBytes(this.getTimeStamp())); 84 | // 85 | // this.setHash(DigestUtils.sha256Hex(headers)); 86 | // } 87 | 88 | private static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 89 | 90 | /** 91 | * 创建创世区块 92 | * @return 93 | */ 94 | public static Block newGenesisBlock() { 95 | return Block.newBlock(ZERO_HASH, "Genesis Block",0); 96 | } 97 | 98 | 99 | } 100 | -------------------------------------------------------------------------------- /part4_CLI/src/main/java/cldy/hanru/blockchain/pow/PowResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.pow; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * 工作量计算结果 8 | * @author ruby 9 | * 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class PowResult { 14 | 15 | /** 16 | * 计数器 17 | */ 18 | private long nonce; 19 | /** 20 | * hash值 21 | */ 22 | private String hash; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /part4_CLI/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.codec.binary.Hex; 8 | import org.apache.commons.lang3.ArrayUtils; 9 | 10 | /** 11 | * 字节数组工具类 12 | * @author hanru 13 | * 14 | */ 15 | public class ByteUtils { 16 | 17 | public static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 18 | 19 | 20 | /** 21 | * 将多个字节数组合并成一个字节数组 22 | * @param bytes 23 | * @return 24 | */ 25 | public static byte[] merge(byte[]... bytes) { 26 | Stream stream = Stream.of(); 27 | for (byte[] b : bytes) { 28 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 29 | } 30 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 31 | } 32 | 33 | /** 34 | * long 转化为 byte[] 35 | * @param val 36 | * @return 37 | */ 38 | public static byte[] toBytes(long val) { 39 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /part4_CLI/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import com.esotericsoftware.kryo.Kryo; 4 | import com.esotericsoftware.kryo.io.Input; 5 | import com.esotericsoftware.kryo.io.Output; 6 | 7 | /** 8 | * 序列化工具类 9 | * @author hanru 10 | */ 11 | public class SerializeUtils { 12 | 13 | /** 14 | * 序列化 15 | * @param object 需要序列化的对象 16 | * @return 17 | */ 18 | public static byte[] serialize(Object object) { 19 | Output output = new Output(4096, -1); 20 | new Kryo().writeClassAndObject(output, object); 21 | byte[] bytes = output.toBytes(); 22 | output.close(); 23 | return bytes; 24 | } 25 | 26 | /** 27 | * 反序列化 28 | * @param bytes 对象对应的字节数组 29 | * @return 30 | */ 31 | public static Object deserialize(byte[] bytes) { 32 | Input input = new Input(bytes); 33 | Object obj = new Kryo().readClassAndObject(input); 34 | input.close(); 35 | return obj; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /part4_CLI/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part4_CLI/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part4_CLI/target/blockchain.db/000003.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/target/blockchain.db/000003.log -------------------------------------------------------------------------------- /part4_CLI/target/blockchain.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000001 2 | -------------------------------------------------------------------------------- /part4_CLI/target/blockchain.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c31c499366e15-8efc7c6abf4d336c -------------------------------------------------------------------------------- /part4_CLI/target/blockchain.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/target/blockchain.db/LOCK -------------------------------------------------------------------------------- /part4_CLI/target/blockchain.db/MANIFEST-000001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/target/blockchain.db/MANIFEST-000001 -------------------------------------------------------------------------------- /part4_CLI/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Wed Oct 10 15:53:34 CST 2018 3 | version=1.0-SNAPSHOT 4 | groupId=cldy.hanru.blockchain 5 | artifactId=part4_CLI 6 | -------------------------------------------------------------------------------- /part4_CLI/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | cldy/hanru/blockchain/cli/CLI.class 2 | cldy/hanru/blockchain/block/Blockchain.class 3 | cldy/hanru/blockchain/util/SerializeUtils.class 4 | cldy/hanru/blockchain/pow/PowResult.class 5 | cldy/hanru/blockchain/util/ByteUtils.class 6 | cldy/hanru/blockchain/Main.class 7 | cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class 8 | cldy/hanru/blockchain/block/Block.class 9 | cldy/hanru/blockchain/pow/ProofOfWork.class 10 | cldy/hanru/blockchain/store/RocksDBUtils.class 11 | -------------------------------------------------------------------------------- /part4_CLI/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/ruby/JavaProjects/part4_CLI/src/main/java/cldy/hanru/blockchain/store/RocksDBUtils.java 2 | /Users/ruby/JavaProjects/part4_CLI/src/main/java/cldy/hanru/blockchain/pow/PowResult.java 3 | /Users/ruby/JavaProjects/part4_CLI/src/main/java/cldy/hanru/blockchain/cli/CLI.java 4 | /Users/ruby/JavaProjects/part4_CLI/src/main/java/cldy/hanru/blockchain/Main.java 5 | /Users/ruby/JavaProjects/part4_CLI/src/main/java/cldy/hanru/blockchain/pow/ProofOfWork.java 6 | /Users/ruby/JavaProjects/part4_CLI/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java 7 | /Users/ruby/JavaProjects/part4_CLI/src/main/java/cldy/hanru/blockchain/block/Block.java 8 | /Users/ruby/JavaProjects/part4_CLI/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java 9 | /Users/ruby/JavaProjects/part4_CLI/src/main/java/cldy/hanru/blockchain/block/Blockchain.java 10 | -------------------------------------------------------------------------------- /part4_CLI/target/part4_CLI-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/target/part4_CLI-jar-with-dependencies.jar -------------------------------------------------------------------------------- /part4_CLI/target/part4_CLI.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part4_CLI/target/part4_CLI.jar -------------------------------------------------------------------------------- /part5_Transaction/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /part5_Transaction/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /part5_Transaction/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /part5_Transaction/blockchain.db/000004.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/blockchain.db/000004.sst -------------------------------------------------------------------------------- /part5_Transaction/blockchain.db/000007.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/blockchain.db/000007.sst -------------------------------------------------------------------------------- /part5_Transaction/blockchain.db/000016.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/blockchain.db/000016.sst -------------------------------------------------------------------------------- /part5_Transaction/blockchain.db/000018.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/blockchain.db/000018.log -------------------------------------------------------------------------------- /part5_Transaction/blockchain.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000017 2 | -------------------------------------------------------------------------------- /part5_Transaction/blockchain.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c425a88772c93-ea89d5c440265829 -------------------------------------------------------------------------------- /part5_Transaction/blockchain.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/blockchain.db/LOCK -------------------------------------------------------------------------------- /part5_Transaction/blockchain.db/MANIFEST-000017: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/blockchain.db/MANIFEST-000017 -------------------------------------------------------------------------------- /part5_Transaction/blockchain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # Check if the jar has been built. 6 | if [ ! -e target/part5_Transaction-jar-with-dependencies.jar ]; then 7 | echo "Compiling blockchain project to a JAR" 8 | mvn package -DskipTests 9 | fi 10 | 11 | java -jar target/part5_Transaction-jar-with-dependencies.jar "$@" 12 | 13 | -------------------------------------------------------------------------------- /part5_Transaction/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | step1:创建transaction包 3 | 4 | step2:创建TXOutput类 5 | 6 | 7 | step3:创建TXInput类 8 | 9 | step4:创建Transaction类 10 | 11 | A:设置字段属性 12 | 13 | B:添加方法,setTxId() 14 | 15 | C:添加方法,newCoinbaseTX(),创建Coinbase交易 16 | 17 | 18 | 19 | 20 | step5:修改Block类 21 | A:修改字段,修改data为transaction。 22 | 23 | B:修改方法newBlock() 24 | 25 | C:修改方法newGenesisBlock() 26 | 27 | D:增加方法,hashTransaction() 28 | 29 | 30 | step6:修改PoW类 31 | A:修改方法,run() 32 | 33 | B:修改方法,prepareData() 34 | 35 | 36 | 37 | step7:修改Blockchain类 38 | A:修改方法newBlockchain()内容后,并将方法名改为createBlockchain() 39 | 40 | B:添加方法mineBlock() 41 | 42 | 43 | step8:创建SpendableOutputResult类 44 | 45 | step9:在Blockchain类中 46 | 47 | A:添加方法,getAllSpentTXOs(),从交易输入中查询区块链中所有已被花费了的交易输出 48 | 49 | B:在Transaction类中,添加isCoinbase()方法 50 | 51 | C:在Blockchain中,添加方法findUnspentTransactions(),查找钱包地址对应的所有未花费的交易 52 | 53 | D:在Blockchain中,添加方法findUTXO(),查找钱包地址对应的所有UTXO 54 | 55 | E:在Blockchain中,添加方法findSpendableOutputs(),寻找能够花费的交易 56 | 57 | 58 | 59 | step10:修改CLI类 60 | 61 | A:修改构造函数 62 | 63 | B:修改help()方法 64 | 65 | C:修改run()方法 66 | 67 | D:修改createBlockchain()方法 68 | 69 | E:添加getBalance()方法 70 | 71 | F:添加send()方法 72 | 73 | G:修改printChain()方法 74 | 75 | H:在Blockchain中,添加initBlockchainFromDB()方法 76 | 77 | 78 | 最后修改sh脚本文件 -------------------------------------------------------------------------------- /part5_Transaction/src/main/java/cldy/hanru/blockchain/Main.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain; 2 | 3 | 4 | import cldy.hanru.blockchain.cli.CLI; 5 | 6 | 7 | 8 | /** 9 | * 测试 10 | * 11 | * @author hanru 12 | */ 13 | public class Main { 14 | 15 | public static void main(String[] args) { 16 | 17 | CLI cli = new CLI(args); 18 | cli.run(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /part5_Transaction/src/main/java/cldy/hanru/blockchain/pow/PowResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.pow; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * 工作量计算结果 8 | * @author ruby 9 | * 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class PowResult { 14 | 15 | /** 16 | * 计数器 17 | */ 18 | private long nonce; 19 | /** 20 | * hash值 21 | */ 22 | private String hash; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /part5_Transaction/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Map; 8 | 9 | /** 10 | * @author hanru 11 | */ 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | @Data 15 | public class SpendableOutputResult { 16 | 17 | /** 18 | * 交易时的支付金额 19 | */ 20 | private int accumulated; 21 | /** 22 | * 未花费的交易 23 | */ 24 | private Map unspentOuts; 25 | } 26 | -------------------------------------------------------------------------------- /part5_Transaction/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | * @author hanru 9 | */ 10 | @AllArgsConstructor 11 | @NoArgsConstructor 12 | @Data 13 | public class TXInput { 14 | 15 | 16 | /** 17 | * 交易Id的hash值 18 | */ 19 | private byte[] txId; 20 | /** 21 | * 交易输出索引 22 | */ 23 | private int txOutputIndex; 24 | /** 25 | * 解锁脚本 26 | */ 27 | private String scriptSig; 28 | 29 | /** 30 | * 判断解锁数据是否能够解锁交易输出 31 | * 32 | * @param unlockingData 33 | * @return 34 | */ 35 | public boolean canUnlockOutputWith(String unlockingData) { 36 | return this.getScriptSig().endsWith(unlockingData); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /part5_Transaction/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | /** 9 | * @author hanru 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class TXOutput { 15 | 16 | /** 17 | * 数值金额 18 | */ 19 | private int value; 20 | /** 21 | * 锁定脚本 22 | */ 23 | private String scriptPubKey; 24 | 25 | /** 26 | * 判断解锁数据是否能够解锁交易输出 27 | * 28 | * @param unlockingData 29 | * @return 30 | */ 31 | public boolean canBeUnlockedWith(String unlockingData) { 32 | return this.getScriptPubKey().endsWith(unlockingData); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /part5_Transaction/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.codec.binary.Hex; 8 | import org.apache.commons.lang3.ArrayUtils; 9 | 10 | /** 11 | * 字节数组工具类 12 | * @author hanru 13 | * 14 | */ 15 | public class ByteUtils { 16 | 17 | public static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 18 | 19 | 20 | /** 21 | * 将多个字节数组合并成一个字节数组 22 | * @param bytes 23 | * @return 24 | */ 25 | public static byte[] merge(byte[]... bytes) { 26 | Stream stream = Stream.of(); 27 | for (byte[] b : bytes) { 28 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 29 | } 30 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 31 | } 32 | 33 | /** 34 | * long 转化为 byte[] 35 | * @param val 36 | * @return 37 | */ 38 | public static byte[] toBytes(long val) { 39 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /part5_Transaction/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import com.esotericsoftware.kryo.Kryo; 4 | import com.esotericsoftware.kryo.io.Input; 5 | import com.esotericsoftware.kryo.io.Output; 6 | 7 | /** 8 | * 序列化工具类 9 | * @author hanru 10 | */ 11 | public class SerializeUtils { 12 | 13 | /** 14 | * 序列化 15 | * @param object 需要序列化的对象 16 | * @return 17 | */ 18 | public static byte[] serialize(Object object) { 19 | Output output = new Output(4096, -1); 20 | new Kryo().writeClassAndObject(output, object); 21 | byte[] bytes = output.toBytes(); 22 | output.close(); 23 | return bytes; 24 | } 25 | 26 | /** 27 | * 反序列化 28 | * @param bytes 对象对应的字节数组 29 | * @return 30 | */ 31 | public static Object deserialize(byte[] bytes) { 32 | Input input = new Input(bytes); 33 | Object obj = new Kryo().readClassAndObject(input); 34 | input.close(); 35 | return obj; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /part5_Transaction/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part5_Transaction/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/Main.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/block/Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/block/Block.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/block/Blockchain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/block/Blockchain.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/cli/CLI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/cli/CLI.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/pow/PowResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/pow/PowResult.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/transaction/TXInput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/transaction/TXInput.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/transaction/Transaction.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/transaction/Transaction.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/util/ByteUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/util/ByteUtils.class -------------------------------------------------------------------------------- /part5_Transaction/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class -------------------------------------------------------------------------------- /part5_Transaction/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Wed Oct 10 16:44:36 CST 2018 3 | version=1.0-SNAPSHOT 4 | groupId=cldy.hanru.blockchain 5 | artifactId=part5_Transaction 6 | -------------------------------------------------------------------------------- /part5_Transaction/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | cldy/hanru/blockchain/cli/CLI.class 2 | cldy/hanru/blockchain/block/Blockchain.class 3 | cldy/hanru/blockchain/util/SerializeUtils.class 4 | cldy/hanru/blockchain/Main.class 5 | cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class 6 | cldy/hanru/blockchain/store/RocksDBUtils.class 7 | cldy/hanru/blockchain/transaction/Transaction.class 8 | cldy/hanru/blockchain/transaction/TXInput.class 9 | cldy/hanru/blockchain/pow/PowResult.class 10 | cldy/hanru/blockchain/util/ByteUtils.class 11 | cldy/hanru/blockchain/transaction/SpendableOutputResult.class 12 | cldy/hanru/blockchain/transaction/TXOutput.class 13 | cldy/hanru/blockchain/block/Block.class 14 | cldy/hanru/blockchain/pow/ProofOfWork.class 15 | -------------------------------------------------------------------------------- /part5_Transaction/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/pow/PowResult.java 2 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/Main.java 3 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java 4 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/cli/CLI.java 5 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java 6 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/block/Block.java 7 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/block/Blockchain.java 8 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/pow/ProofOfWork.java 9 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java 10 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java 11 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/transaction/Transaction.java 12 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java 13 | /Users/ruby/JavaProjects/part5_Transaction/src/main/java/cldy/hanru/blockchain/store/RocksDBUtils.java 14 | -------------------------------------------------------------------------------- /part5_Transaction/target/part5_Transaction-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/part5_Transaction-jar-with-dependencies.jar -------------------------------------------------------------------------------- /part5_Transaction/target/part5_Transaction.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part5_Transaction/target/part5_Transaction.jar -------------------------------------------------------------------------------- /part6_Wallet/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /part6_Wallet/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /part6_Wallet/.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | -------------------------------------------------------------------------------- /part6_Wallet/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /part6_Wallet/blockchain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # Check if the jar has been built. 6 | if [ ! -e target/part6_Wallet-jar-with-dependencies.jar ]; then 7 | echo "Compiling blockchain project to a JAR" 8 | mvn package -DskipTests 9 | fi 10 | 11 | java -jar target/part6_Wallet-jar-with-dependencies.jar "$@" 12 | 13 | -------------------------------------------------------------------------------- /part6_Wallet/readme.txt: -------------------------------------------------------------------------------- 1 | step:修改pom.xml文件 2 | 添加加密的依赖包:org.bouncycastle 3 | 4 | step1:创建包wallet 5 | 6 | step2:创建wallet类 7 | 8 | 设置字段属性 9 | 10 | 添加方法:initWallet() 11 | 12 | 添加方法:newECKeyPair() 13 | 14 | 添加方法:初始化构造函数Wallet() 15 | 16 | 17 | step3:在util包下,新建BtcAddressUtils类 18 | 添加生成钱包的公钥方法 19 | 20 | 21 | step4:在util包下,新建Base58Check类 22 | 23 | step5:在wallet下,添加WalletUtils类 24 | 25 | 26 | 27 | 28 | 29 | step6:修改CLI 30 | A:修改help() 31 | B:修改run() 32 | C:修改printChain() 33 | D:修改getBalance() 34 | E:修改send() 35 | F:添加方法createWallet() 36 | G:printAddresses() 37 | 38 | 39 | 最后修改blockchain.sh脚本文件 -------------------------------------------------------------------------------- /part6_Wallet/src/main/java/cldy/hanru/blockchain/Main.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain; 2 | 3 | 4 | import cldy.hanru.blockchain.cli.CLI; 5 | 6 | /** 7 | * 测试 8 | * 9 | * @author hanru 10 | */ 11 | public class Main { 12 | 13 | public static void main(String[] args) { 14 | 15 | CLI cli = new CLI(args); 16 | cli.run(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/java/cldy/hanru/blockchain/block/Block.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.block; 2 | 3 | import java.time.Instant; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import cldy.hanru.blockchain.transaction.Transaction; 8 | import cldy.hanru.blockchain.util.ByteUtils; 9 | import lombok.NoArgsConstructor; 10 | import org.apache.commons.codec.binary.Hex; 11 | 12 | import cldy.hanru.blockchain.pow.PowResult; 13 | import cldy.hanru.blockchain.pow.ProofOfWork; 14 | import lombok.AllArgsConstructor; 15 | import lombok.Data; 16 | import org.apache.commons.codec.digest.DigestUtils; 17 | 18 | /** 19 | * 区块 20 | * @author hanru 21 | * 22 | */ 23 | @AllArgsConstructor 24 | @NoArgsConstructor 25 | @Data 26 | public class Block { 27 | 28 | /** 29 | * 区块hash值 30 | */ 31 | private String hash; 32 | 33 | /** 34 | * 前一个区块的hash值 35 | */ 36 | private String prevBlockHash; 37 | 38 | /** 39 | * 区块数据,未来会替换为交易 40 | */ 41 | private List transactions; 42 | 43 | /** 44 | * 时间戳,单位秒 45 | */ 46 | private long timeStamp; 47 | 48 | /** 49 | * 区块的高度 50 | */ 51 | private long height; 52 | 53 | /** 54 | * 工作量证明计数器 55 | */ 56 | private long nonce; 57 | 58 | 59 | 60 | 61 | /** 62 | * 创建新的区块 63 | * 64 | * @param previousHash 65 | * @param transactions 66 | * @return 67 | */ 68 | public static Block newBlock(String previousHash, List transactions,long height) { 69 | Block block = new Block("", previousHash, transactions, Instant.now().getEpochSecond(),height,0); 70 | ProofOfWork pow = ProofOfWork.newProofOfWork(block); 71 | PowResult powResult = pow.run(); 72 | block.setHash(powResult.getHash()); 73 | block.setNonce(powResult.getNonce()); 74 | // block.setHash(); 75 | return block; 76 | } 77 | 78 | /** 79 | * 设置Hash 80 | * 注意:在准备区块数据时,一定要从原始数据类型转化为byte[],不能直接从字符串进行转换 81 | */ 82 | // private void setHash() { 83 | // byte[] prevBlockHashBytes = {}; 84 | // if (StringUtils.isNoneBlank(this.getPrevBlockHash())) { 85 | // prevBlockHashBytes = new BigInteger(this.getPrevBlockHash(), 16).toByteArray(); 86 | // } 87 | // 88 | // byte[] headers = ByteUtils.merge(prevBlockHashBytes, this.getData().getBytes(), 89 | // ByteUtils.toBytes(this.getTimeStamp())); 90 | // 91 | // this.setHash(DigestUtils.sha256Hex(headers)); 92 | // } 93 | 94 | private static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 95 | 96 | /** 97 | * 创建创世区块 98 | * @param coinbase 99 | * @return 100 | */ 101 | public static Block newGenesisBlock(Transaction coinbase) { 102 | List transactions = new ArrayList<>(); 103 | transactions.add(coinbase); 104 | return Block.newBlock(ByteUtils.ZERO_HASH, transactions,0); 105 | } 106 | 107 | /** 108 | * 对区块中的交易信息进行Hash计算 109 | * 110 | * @return 111 | */ 112 | public byte[] hashTransaction() { 113 | byte[][] txIdArrays = new byte[this.getTransactions().size()][]; 114 | for (int i = 0; i < this.getTransactions().size(); i++) { 115 | txIdArrays[i] = this.getTransactions().get(i).getTxId(); 116 | } 117 | return DigestUtils.sha256(ByteUtils.merge(txIdArrays)); 118 | 119 | 120 | } 121 | 122 | 123 | } 124 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/java/cldy/hanru/blockchain/pow/PowResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.pow; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * 工作量计算结果 8 | * @author ruby 9 | * 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class PowResult { 14 | 15 | /** 16 | * 计数器 17 | */ 18 | private long nonce; 19 | /** 20 | * hash值 21 | */ 22 | private String hash; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Map; 8 | 9 | /** 10 | * @author hanru 11 | */ 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | @Data 15 | public class SpendableOutputResult { 16 | 17 | /** 18 | * 交易时的支付金额 19 | */ 20 | private int accumulated; 21 | /** 22 | * 未花费的交易 23 | */ 24 | private Map unspentOuts; 25 | } 26 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | * @author hanru 9 | */ 10 | @AllArgsConstructor 11 | @NoArgsConstructor 12 | @Data 13 | public class TXInput { 14 | 15 | 16 | /** 17 | * 交易Id的hash值 18 | */ 19 | private byte[] txId; 20 | /** 21 | * 交易输出索引 22 | */ 23 | private int txOutputIndex; 24 | /** 25 | * 解锁脚本 26 | */ 27 | private String scriptSig; 28 | 29 | /** 30 | * 判断解锁数据是否能够解锁交易输出 31 | * 32 | * @param unlockingData 33 | * @return 34 | */ 35 | public boolean canUnlockOutputWith(String unlockingData) { 36 | return this.getScriptSig().endsWith(unlockingData); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | /** 9 | * @author hanru 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class TXOutput { 15 | 16 | /** 17 | * 数值金额 18 | */ 19 | private int value; 20 | /** 21 | * 锁定脚本 22 | */ 23 | private String scriptPubKey; 24 | 25 | /** 26 | * 判断解锁数据是否能够解锁交易输出 27 | * 28 | * @param unlockingData 29 | * @return 30 | */ 31 | public boolean canBeUnlockedWith(String unlockingData) { 32 | return this.getScriptPubKey().endsWith(unlockingData); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/java/cldy/hanru/blockchain/util/AddressUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import org.apache.commons.codec.digest.DigestUtils; 4 | import org.bouncycastle.crypto.digests.RIPEMD160Digest; 5 | import org.bouncycastle.util.Arrays; 6 | 7 | /** 8 | * 地址工具类 9 | * 10 | * @author hanru 11 | * 12 | */ 13 | public class AddressUtils { 14 | /** 15 | * 双重Hash 16 | * 17 | * @param data 18 | * @return 19 | */ 20 | public static byte[] doubleHash(byte[] data) { 21 | return DigestUtils.sha256(DigestUtils.sha256(data)); 22 | } 23 | 24 | /** 25 | * 计算公钥的 RIPEMD160 Hash值 26 | * 27 | * @param pubKey 公钥 28 | * @return ipeMD160Hash(sha256 ( pubkey)) 29 | */ 30 | public static byte[] ripeMD160Hash(byte[] pubKey) { 31 | //1. 先对公钥做 sha256 处理 32 | byte[] shaHashedKey = DigestUtils.sha256(pubKey); 33 | RIPEMD160Digest ripemd160 = new RIPEMD160Digest(); 34 | ripemd160.update(shaHashedKey, 0, shaHashedKey.length); 35 | byte[] output = new byte[ripemd160.getDigestSize()]; 36 | ripemd160.doFinal(output, 0); 37 | return output; 38 | } 39 | /** 40 | * 生成公钥的校验码 41 | * 42 | * @param payload 43 | * @return 44 | */ 45 | public static byte[] checksum(byte[] payload) { 46 | return Arrays.copyOfRange(doubleHash(payload), 0, 4); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.codec.binary.Hex; 8 | import org.apache.commons.lang3.ArrayUtils; 9 | 10 | /** 11 | * 字节数组工具类 12 | * @author hanru 13 | * 14 | */ 15 | public class ByteUtils { 16 | 17 | public static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 18 | 19 | 20 | /** 21 | * 将多个字节数组合并成一个字节数组 22 | * @param bytes 23 | * @return 24 | */ 25 | public static byte[] merge(byte[]... bytes) { 26 | Stream stream = Stream.of(); 27 | for (byte[] b : bytes) { 28 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 29 | } 30 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 31 | } 32 | 33 | /** 34 | * long 转化为 byte[] 35 | * @param val 36 | * @return 37 | */ 38 | public static byte[] toBytes(long val) { 39 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import com.esotericsoftware.kryo.Kryo; 4 | import com.esotericsoftware.kryo.io.Input; 5 | import com.esotericsoftware.kryo.io.Output; 6 | 7 | /** 8 | * 序列化工具类 9 | * @author hanru 10 | */ 11 | public class SerializeUtils { 12 | 13 | /** 14 | * 序列化 15 | * @param object 需要序列化的对象 16 | * @return 17 | */ 18 | public static byte[] serialize(Object object) { 19 | Output output = new Output(4096, -1); 20 | new Kryo().writeClassAndObject(output, object); 21 | byte[] bytes = output.toBytes(); 22 | output.close(); 23 | return bytes; 24 | } 25 | 26 | /** 27 | * 反序列化 28 | * @param bytes 对象对应的字节数组 29 | * @return 30 | */ 31 | public static Object deserialize(byte[] bytes) { 32 | Input input = new Input(bytes); 33 | Object obj = new Kryo().readClassAndObject(input); 34 | input.close(); 35 | return obj; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part6_Wallet/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part6_Wallet/wallet.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part6_Wallet/wallet.dat -------------------------------------------------------------------------------- /part7_Signature/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /part7_Signature/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /part7_Signature/.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | -------------------------------------------------------------------------------- /part7_Signature/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /part7_Signature/blockchain.db/000004.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/blockchain.db/000004.sst -------------------------------------------------------------------------------- /part7_Signature/blockchain.db/000010.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/blockchain.db/000010.sst -------------------------------------------------------------------------------- /part7_Signature/blockchain.db/000018.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/blockchain.db/000018.log -------------------------------------------------------------------------------- /part7_Signature/blockchain.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000017 2 | -------------------------------------------------------------------------------- /part7_Signature/blockchain.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c36e6e0efba08-5677b680dfa016d3 -------------------------------------------------------------------------------- /part7_Signature/blockchain.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/blockchain.db/LOCK -------------------------------------------------------------------------------- /part7_Signature/blockchain.db/MANIFEST-000017: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/blockchain.db/MANIFEST-000017 -------------------------------------------------------------------------------- /part7_Signature/blockchain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # Check if the jar has been built. 6 | if [ ! -e target/part7_Signature-jar-with-dependencies.jar ]; then 7 | echo "Compiling blockchain project to a JAR" 8 | mvn package -DskipTests 9 | fi 10 | 11 | java -jar target/part7_Signature-jar-with-dependencies.jar "$@" 12 | 13 | -------------------------------------------------------------------------------- /part7_Signature/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | step1:修改TxOutput类 4 | 5 | 6 | step2:修改TxInput类 7 | 8 | step3:修改Transaction类 9 | 10 | 修改newUTXOTransaction() 11 | 添加getData() 12 | 添加trimmedCopy() 13 | 添加sign() 14 | 添加verify() 15 | 16 | 17 | 18 | step4:修改Blockchain类 19 | 20 | A:修改getAllSpentTXOs() 21 | 22 | B :修改findUnspentTransactions() 23 | 24 | C:修改findUTXO() 25 | 26 | D:修改findSpendableOutputs() 27 | 28 | E:添加方法findTransaction() 29 | 30 | 31 | F:添加signTransaction() 32 | 33 | G:添加verifyTransactions() 34 | 35 | H:修改mineBlock(),添加验证 36 | 37 | step5:修改CLI 38 | 39 | 40 | 最后修改blockchain.sh脚本文件 -------------------------------------------------------------------------------- /part7_Signature/src/main/java/cldy/hanru/blockchain/Main.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain; 2 | 3 | 4 | import cldy.hanru.blockchain.cli.CLI; 5 | 6 | 7 | 8 | /** 9 | * 测试 10 | * 11 | * @author hanru 12 | */ 13 | public class Main { 14 | 15 | public static void main(String[] args) { 16 | 17 | CLI cli = new CLI(args); 18 | cli.run(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /part7_Signature/src/main/java/cldy/hanru/blockchain/pow/PowResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.pow; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * 工作量计算结果 8 | * @author ruby 9 | * 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class PowResult { 14 | 15 | /** 16 | * 计数器 17 | */ 18 | private long nonce; 19 | /** 20 | * hash值 21 | */ 22 | private String hash; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /part7_Signature/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Map; 8 | 9 | /** 10 | * @author hanru 11 | */ 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | @Data 15 | public class SpendableOutputResult { 16 | 17 | /** 18 | * 交易时的支付金额 19 | */ 20 | private int accumulated; 21 | /** 22 | * 未花费的交易 23 | */ 24 | private Map unspentOuts; 25 | } 26 | -------------------------------------------------------------------------------- /part7_Signature/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import cldy.hanru.blockchain.util.AddressUtils; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.util.Arrays; 9 | 10 | /** 11 | * @author hanru 12 | */ 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | @Data 16 | public class TXInput { 17 | 18 | 19 | /** 20 | * 交易Id的hash值 21 | */ 22 | private byte[] txId; 23 | /** 24 | * 交易输出索引 25 | */ 26 | private int txOutputIndex; 27 | /** 28 | * 解锁脚本 29 | */ 30 | // private String scriptSig; 31 | 32 | 33 | /** 34 | * 签名 35 | */ 36 | private byte[] signature; 37 | /** 38 | * 公钥 39 | */ 40 | private byte[] pubKey; 41 | 42 | 43 | /** 44 | * 判断解锁数据是否能够解锁交易输出 45 | * 46 | * @param unlockingData 47 | * @return 48 | */ 49 | // public boolean canUnlockOutputWith(String unlockingData) { 50 | // return this.getScriptSig().endsWith(unlockingData); 51 | // } 52 | 53 | 54 | /** 55 | * 检查公钥hash是否用于交易输入 56 | * 57 | * @param pubKeyHash 58 | * @return 59 | */ 60 | public boolean usesKey(byte[] pubKeyHash) { 61 | byte[] lockingHash = AddressUtils.ripeMD160Hash(this.getPubKey()); 62 | return Arrays.equals(lockingHash, pubKeyHash); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /part7_Signature/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | 4 | import cldy.hanru.blockchain.util.Base58Check; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | import java.util.Arrays; 10 | 11 | /** 12 | * @author hanru 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | public class TXOutput { 18 | 19 | /** 20 | * 数值金额 21 | */ 22 | private int value; 23 | /** 24 | * 锁定脚本 25 | */ 26 | // private String scriptPubKey; 27 | 28 | 29 | /** 30 | * 公钥Hash 31 | */ 32 | private byte[] pubKeyHash; 33 | /** 34 | * 判断解锁数据是否能够解锁交易输出 35 | * 36 | * @param unlockingData 37 | * @return 38 | */ 39 | // public boolean canBeUnlockedWith(String unlockingData) { 40 | // 41 | // return this.getScriptPubKey().endsWith(unlockingData); 42 | // } 43 | 44 | 45 | /** 46 | * 检查交易输出是否能够使用指定的公钥 47 | * 48 | * @param pubKeyHash 49 | * @return 50 | */ 51 | public boolean isLockedWithKey(byte[] pubKeyHash) { 52 | return Arrays.equals(this.getPubKeyHash(), pubKeyHash); 53 | } 54 | 55 | /** 56 | * 创建交易输出 57 | * 58 | * @param value 59 | * @param address 60 | * @return 61 | */ 62 | public static TXOutput newTXOutput(int value, String address) { 63 | // 反向转化为 byte 数组 64 | byte[] versionedPayload = Base58Check.base58ToBytes(address); 65 | byte[] pubKeyHash = Arrays.copyOfRange(versionedPayload, 1, versionedPayload.length); 66 | return new TXOutput(value, pubKeyHash); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /part7_Signature/src/main/java/cldy/hanru/blockchain/util/AddressUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import org.apache.commons.codec.digest.DigestUtils; 4 | import org.bouncycastle.crypto.digests.RIPEMD160Digest; 5 | import org.bouncycastle.util.Arrays; 6 | 7 | /** 8 | * 地址工具类 9 | * 10 | * @author hanru 11 | * 12 | */ 13 | public class AddressUtils { 14 | /** 15 | * 双重Hash 16 | * 17 | * @param data 18 | * @return 19 | */ 20 | public static byte[] doubleHash(byte[] data) { 21 | return DigestUtils.sha256(DigestUtils.sha256(data)); 22 | } 23 | 24 | /** 25 | * 计算公钥的 RIPEMD160 Hash值 26 | * 27 | * @param pubKey 公钥 28 | * @return ipeMD160Hash(sha256 ( pubkey)) 29 | */ 30 | public static byte[] ripeMD160Hash(byte[] pubKey) { 31 | //1. 先对公钥做 sha256 处理 32 | byte[] shaHashedKey = DigestUtils.sha256(pubKey); 33 | RIPEMD160Digest ripemd160 = new RIPEMD160Digest(); 34 | ripemd160.update(shaHashedKey, 0, shaHashedKey.length); 35 | byte[] output = new byte[ripemd160.getDigestSize()]; 36 | ripemd160.doFinal(output, 0); 37 | return output; 38 | } 39 | /** 40 | * 生成公钥的校验码 41 | * 42 | * @param payload 43 | * @return 44 | */ 45 | public static byte[] checksum(byte[] payload) { 46 | return Arrays.copyOfRange(doubleHash(payload), 0, 4); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /part7_Signature/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.codec.binary.Hex; 8 | import org.apache.commons.lang3.ArrayUtils; 9 | 10 | /** 11 | * 字节数组工具类 12 | * @author hanru 13 | * 14 | */ 15 | public class ByteUtils { 16 | 17 | public static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 18 | 19 | 20 | /** 21 | * 将多个字节数组合并成一个字节数组 22 | * @param bytes 23 | * @return 24 | */ 25 | public static byte[] merge(byte[]... bytes) { 26 | Stream stream = Stream.of(); 27 | for (byte[] b : bytes) { 28 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 29 | } 30 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 31 | } 32 | 33 | /** 34 | * long 转化为 byte[] 35 | * @param val 36 | * @return 37 | */ 38 | public static byte[] toBytes(long val) { 39 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /part7_Signature/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import com.esotericsoftware.kryo.Kryo; 4 | import com.esotericsoftware.kryo.io.Input; 5 | import com.esotericsoftware.kryo.io.Output; 6 | 7 | /** 8 | * 序列化工具类 9 | * @author hanru 10 | */ 11 | public class SerializeUtils { 12 | 13 | /** 14 | * 序列化 15 | * @param object 需要序列化的对象 16 | * @return 17 | */ 18 | public static byte[] serialize(Object object) { 19 | Output output = new Output(4096, -1); 20 | new Kryo().writeClassAndObject(output, object); 21 | byte[] bytes = output.toBytes(); 22 | output.close(); 23 | return bytes; 24 | } 25 | 26 | /** 27 | * 反序列化 28 | * @param bytes 对象对应的字节数组 29 | * @return 30 | */ 31 | public static Object deserialize(byte[] bytes) { 32 | Input input = new Input(bytes); 33 | Object obj = new Kryo().readClassAndObject(input); 34 | input.close(); 35 | return obj; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /part7_Signature/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part7_Signature/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/Main.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/block/Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/block/Block.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/block/Blockchain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/block/Blockchain.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/cli/CLI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/cli/CLI.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/pow/PowResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/pow/PowResult.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/transaction/TXInput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/transaction/TXInput.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/transaction/Transaction.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/transaction/Transaction.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/util/AddressUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/util/AddressUtils.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/util/Base58Check.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/util/Base58Check.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/util/ByteUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/util/ByteUtils.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/wallet/Wallet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/wallet/Wallet.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class -------------------------------------------------------------------------------- /part7_Signature/target/classes/cldy/hanru/blockchain/wallet/WalletUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/classes/cldy/hanru/blockchain/wallet/WalletUtils.class -------------------------------------------------------------------------------- /part7_Signature/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Wed Oct 10 17:54:27 CST 2018 3 | version=1.0-SNAPSHOT 4 | groupId=cldy.hanru.blockchain 5 | artifactId=part7_Signature 6 | -------------------------------------------------------------------------------- /part7_Signature/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | cldy/hanru/blockchain/cli/CLI.class 2 | cldy/hanru/blockchain/block/Blockchain.class 3 | cldy/hanru/blockchain/util/SerializeUtils.class 4 | cldy/hanru/blockchain/wallet/WalletUtils.class 5 | cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class 6 | cldy/hanru/blockchain/Main.class 7 | cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class 8 | cldy/hanru/blockchain/util/AddressUtils.class 9 | cldy/hanru/blockchain/store/RocksDBUtils.class 10 | cldy/hanru/blockchain/transaction/Transaction.class 11 | cldy/hanru/blockchain/transaction/TXInput.class 12 | cldy/hanru/blockchain/util/Base58Check.class 13 | cldy/hanru/blockchain/pow/PowResult.class 14 | cldy/hanru/blockchain/util/ByteUtils.class 15 | cldy/hanru/blockchain/wallet/Wallet.class 16 | cldy/hanru/blockchain/transaction/SpendableOutputResult.class 17 | cldy/hanru/blockchain/transaction/TXOutput.class 18 | cldy/hanru/blockchain/block/Block.class 19 | cldy/hanru/blockchain/pow/ProofOfWork.class 20 | -------------------------------------------------------------------------------- /part7_Signature/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/pow/PowResult.java 2 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java 3 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java 4 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/pow/ProofOfWork.java 5 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/block/Block.java 6 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/block/Blockchain.java 7 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/transaction/Transaction.java 8 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java 9 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/store/RocksDBUtils.java 10 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/util/Base58Check.java 11 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/wallet/WalletUtils.java 12 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java 13 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/wallet/Wallet.java 14 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/util/AddressUtils.java 15 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java 16 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/cli/CLI.java 17 | /Users/ruby/JavaProjects/part7_Signature/src/main/java/cldy/hanru/blockchain/Main.java 18 | -------------------------------------------------------------------------------- /part7_Signature/target/part7_Signature-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/part7_Signature-jar-with-dependencies.jar -------------------------------------------------------------------------------- /part7_Signature/target/part7_Signature.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/target/part7_Signature.jar -------------------------------------------------------------------------------- /part7_Signature/wallet.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part7_Signature/wallet.dat -------------------------------------------------------------------------------- /part8_Transaction2/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /part8_Transaction2/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /part8_Transaction2/.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | -------------------------------------------------------------------------------- /part8_Transaction2/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /part8_Transaction2/blockchain.db/000033.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/blockchain.db/000033.sst -------------------------------------------------------------------------------- /part8_Transaction2/blockchain.db/000036.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/blockchain.db/000036.log -------------------------------------------------------------------------------- /part8_Transaction2/blockchain.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000035 2 | -------------------------------------------------------------------------------- /part8_Transaction2/blockchain.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c41db7e483264-782f2382870ce1c1 -------------------------------------------------------------------------------- /part8_Transaction2/blockchain.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/blockchain.db/LOCK -------------------------------------------------------------------------------- /part8_Transaction2/blockchain.db/MANIFEST-000035: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/blockchain.db/MANIFEST-000035 -------------------------------------------------------------------------------- /part8_Transaction2/blockchain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # Check if the jar has been built. 6 | if [ ! -e target/part8_Transaction2-jar-with-dependencies.jar ]; then 7 | echo "Compiling blockchain project to a JAR" 8 | mvn package -DskipTests 9 | fi 10 | 11 | java -jar target/part8_Transaction2-jar-with-dependencies.jar "$@" 12 | 13 | -------------------------------------------------------------------------------- /part8_Transaction2/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | step1:修改pom.xml 4 | 5 | 6 | 7 | step2:修改RocksDBUtils类 8 | 9 | A:添加String CHAINSTATE_BUCKET_KEY = "chainstate"; 10 | 11 | B:添加private Map chainstateBucket; 12 | 13 | C:添加initChainStateBucket() 14 | 15 | D:添加cleanChainStateBucket() 16 | 17 | E:添加putUTXOs() 18 | 19 | F:添加getUTXOs() 20 | 21 | G:添加deleteUTXOs() 22 | 23 | H:修改RocksDBUtils()构造函数 24 | 25 | 26 | step3:修改Blockchain类 27 | A:修改getAllSpentTXOs() 28 | 29 | B:添加findAllUTXOs() 30 | 31 | C:删除 32 | findUnspentTransactions() 33 | findUTXO() 34 | findSpendableOutputs() 35 | 36 | 37 | step4:新建UTXOSet.java文件 38 | A:添加reIndex() 39 | step5:修改CLI文件 40 | A:修改createBlockchain() 41 | 42 | 43 | step6:修改UTXOSet文件 44 | A:添加findUTXOs(),用于查找指定账户的所有的utxo 45 | step7:修改CLI文件 46 | A:修改getBalance() 47 | 48 | 49 | step8:在UTXOSet中,继续修改,实现转账 50 | A:添加findSpendableOutputs() 51 | B: 添加update() 52 | 53 | 54 | step9:修改Transaction类 55 | A:修改newUTXOTransaction() 56 | step10:修改Blockchain 57 | A:修改mineBlock(),添加返回值 58 | 59 | step11:修改CLI文件 60 | A:修改send() 61 | 62 | 63 | 64 | 添加奖励: 65 | step12:在Transaction中 66 | A:添加时间戳字段 67 | B:修改:newCoinbaseTX() 68 | C:修改:newUTXOTransaction() 69 | 70 | step13:在CLI中 71 | A:修改send()方法,添加给与奖励的coinbase交易 72 | 73 | step14:修改Blockchain中 74 | A:修改verifyTransactions()方法,添加判断是否是coinbase交易 -------------------------------------------------------------------------------- /part8_Transaction2/src/main/java/cldy/hanru/blockchain/Main.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain; 2 | 3 | 4 | import cldy.hanru.blockchain.cli.CLI; 5 | 6 | 7 | 8 | /** 9 | * 测试 10 | * 11 | * @author hanru 12 | */ 13 | public class Main { 14 | 15 | public static void main(String[] args) { 16 | 17 | CLI cli = new CLI(args); 18 | cli.run(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /part8_Transaction2/src/main/java/cldy/hanru/blockchain/pow/PowResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.pow; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * 工作量计算结果 8 | * @author ruby 9 | * 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class PowResult { 14 | 15 | /** 16 | * 计数器 17 | */ 18 | private long nonce; 19 | /** 20 | * hash值 21 | */ 22 | private String hash; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Map; 8 | 9 | /** 10 | * @author hanru 11 | */ 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | @Data 15 | public class SpendableOutputResult { 16 | 17 | /** 18 | * 交易时的支付金额 19 | */ 20 | private int accumulated; 21 | /** 22 | * 未花费的交易 23 | */ 24 | private Map unspentOuts; 25 | } 26 | -------------------------------------------------------------------------------- /part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import cldy.hanru.blockchain.util.AddressUtils; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.util.Arrays; 9 | 10 | /** 11 | * @author hanru 12 | */ 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | @Data 16 | public class TXInput { 17 | 18 | 19 | /** 20 | * 交易Id的hash值 21 | */ 22 | private byte[] txId; 23 | /** 24 | * 交易输出索引 25 | */ 26 | private int txOutputIndex; 27 | /** 28 | * 解锁脚本 29 | */ 30 | // private String scriptSig; 31 | 32 | 33 | /** 34 | * 签名 35 | */ 36 | private byte[] signature; 37 | /** 38 | * 公钥 39 | */ 40 | private byte[] pubKey; 41 | 42 | 43 | /** 44 | * 判断解锁数据是否能够解锁交易输出 45 | * 46 | * @param unlockingData 47 | * @return 48 | */ 49 | // public boolean canUnlockOutputWith(String unlockingData) { 50 | // return this.getScriptSig().endsWith(unlockingData); 51 | // } 52 | 53 | 54 | /** 55 | * 检查公钥hash是否用于交易输入 56 | * 57 | * @param pubKeyHash 58 | * @return 59 | */ 60 | public boolean usesKey(byte[] pubKeyHash) { 61 | byte[] lockingHash = AddressUtils.ripeMD160Hash(this.getPubKey()); 62 | return Arrays.equals(lockingHash, pubKeyHash); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | 4 | import cldy.hanru.blockchain.util.Base58Check; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | import java.util.Arrays; 10 | 11 | /** 12 | * @author hanru 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | public class TXOutput { 18 | 19 | /** 20 | * 数值金额 21 | */ 22 | private int value; 23 | /** 24 | * 锁定脚本 25 | */ 26 | // private String scriptPubKey; 27 | 28 | 29 | /** 30 | * 公钥Hash 31 | */ 32 | private byte[] pubKeyHash; 33 | /** 34 | * 判断解锁数据是否能够解锁交易输出 35 | * 36 | * @param unlockingData 37 | * @return 38 | */ 39 | // public boolean canBeUnlockedWith(String unlockingData) { 40 | // 41 | // return this.getScriptPubKey().endsWith(unlockingData); 42 | // } 43 | 44 | 45 | /** 46 | * 检查交易输出是否能够使用指定的公钥 47 | * 48 | * @param pubKeyHash 49 | * @return 50 | */ 51 | public boolean isLockedWithKey(byte[] pubKeyHash) { 52 | return Arrays.equals(this.getPubKeyHash(), pubKeyHash); 53 | } 54 | 55 | /** 56 | * 创建交易输出 57 | * 58 | * @param value 59 | * @param address 60 | * @return 61 | */ 62 | public static TXOutput newTXOutput(int value, String address) { 63 | // 反向转化为 byte 数组 64 | byte[] versionedPayload = Base58Check.base58ToBytes(address); 65 | byte[] pubKeyHash = Arrays.copyOfRange(versionedPayload, 1, versionedPayload.length); 66 | return new TXOutput(value, pubKeyHash); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/AddressUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import org.apache.commons.codec.digest.DigestUtils; 4 | import org.bouncycastle.crypto.digests.RIPEMD160Digest; 5 | import org.bouncycastle.util.Arrays; 6 | 7 | /** 8 | * 地址工具类 9 | * 10 | * @author hanru 11 | * 12 | */ 13 | public class AddressUtils { 14 | /** 15 | * 双重Hash 16 | * 17 | * @param data 18 | * @return 19 | */ 20 | public static byte[] doubleHash(byte[] data) { 21 | return DigestUtils.sha256(DigestUtils.sha256(data)); 22 | } 23 | 24 | /** 25 | * 计算公钥的 RIPEMD160 Hash值 26 | * 27 | * @param pubKey 公钥 28 | * @return ipeMD160Hash(sha256 ( pubkey)) 29 | */ 30 | public static byte[] ripeMD160Hash(byte[] pubKey) { 31 | //1. 先对公钥做 sha256 处理 32 | byte[] shaHashedKey = DigestUtils.sha256(pubKey); 33 | RIPEMD160Digest ripemd160 = new RIPEMD160Digest(); 34 | ripemd160.update(shaHashedKey, 0, shaHashedKey.length); 35 | byte[] output = new byte[ripemd160.getDigestSize()]; 36 | ripemd160.doFinal(output, 0); 37 | return output; 38 | } 39 | /** 40 | * 生成公钥的校验码 41 | * 42 | * @param payload 43 | * @return 44 | */ 45 | public static byte[] checksum(byte[] payload) { 46 | return Arrays.copyOfRange(doubleHash(payload), 0, 4); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.codec.binary.Hex; 8 | import org.apache.commons.lang3.ArrayUtils; 9 | 10 | /** 11 | * 字节数组工具类 12 | * @author hanru 13 | * 14 | */ 15 | public class ByteUtils { 16 | 17 | public static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 18 | 19 | 20 | /** 21 | * 将多个字节数组合并成一个字节数组 22 | * @param bytes 23 | * @return 24 | */ 25 | public static byte[] merge(byte[]... bytes) { 26 | Stream stream = Stream.of(); 27 | for (byte[] b : bytes) { 28 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 29 | } 30 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 31 | } 32 | 33 | /** 34 | * long 转化为 byte[] 35 | * @param val 36 | * @return 37 | */ 38 | public static byte[] toBytes(long val) { 39 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import com.esotericsoftware.kryo.Kryo; 4 | import com.esotericsoftware.kryo.io.Input; 5 | import com.esotericsoftware.kryo.io.Output; 6 | 7 | /** 8 | * 序列化工具类 9 | * @author hanru 10 | */ 11 | public class SerializeUtils { 12 | 13 | /** 14 | * 序列化 15 | * @param object 需要序列化的对象 16 | * @return 17 | */ 18 | public static byte[] serialize(Object object) { 19 | Output output = new Output(4096, -1); 20 | new Kryo().writeClassAndObject(output, object); 21 | byte[] bytes = output.toBytes(); 22 | output.close(); 23 | return bytes; 24 | } 25 | 26 | /** 27 | * 反序列化 28 | * @param bytes 对象对应的字节数组 29 | * @return 30 | */ 31 | public static Object deserialize(byte[] bytes) { 32 | Input input = new Input(bytes); 33 | Object obj = new Kryo().readClassAndObject(input); 34 | input.close(); 35 | return obj; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /part8_Transaction2/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part8_Transaction2/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/Main.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/block/Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/block/Block.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/block/Blockchain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/block/Blockchain.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/cli/CLI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/cli/CLI.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/pow/PowResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/pow/PowResult.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/TXInput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/TXInput.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/Transaction.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/Transaction.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/UTXOSet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/transaction/UTXOSet.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/util/AddressUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/util/AddressUtils.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/util/Base58Check.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/util/Base58Check.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/util/ByteUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/util/ByteUtils.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/wallet/Wallet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/wallet/Wallet.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class -------------------------------------------------------------------------------- /part8_Transaction2/target/classes/cldy/hanru/blockchain/wallet/WalletUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/classes/cldy/hanru/blockchain/wallet/WalletUtils.class -------------------------------------------------------------------------------- /part8_Transaction2/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Wed Oct 10 18:12:03 CST 2018 3 | version=1.0-SNAPSHOT 4 | groupId=cldy.hanru.blockchain 5 | artifactId=part8_Transaction2 6 | -------------------------------------------------------------------------------- /part8_Transaction2/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | cldy/hanru/blockchain/cli/CLI.class 2 | cldy/hanru/blockchain/block/Blockchain.class 3 | cldy/hanru/blockchain/util/SerializeUtils.class 4 | cldy/hanru/blockchain/wallet/WalletUtils.class 5 | cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class 6 | cldy/hanru/blockchain/Main.class 7 | cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class 8 | cldy/hanru/blockchain/util/AddressUtils.class 9 | cldy/hanru/blockchain/store/RocksDBUtils.class 10 | cldy/hanru/blockchain/transaction/Transaction.class 11 | cldy/hanru/blockchain/transaction/TXInput.class 12 | cldy/hanru/blockchain/util/Base58Check.class 13 | cldy/hanru/blockchain/pow/PowResult.class 14 | cldy/hanru/blockchain/util/ByteUtils.class 15 | cldy/hanru/blockchain/wallet/Wallet.class 16 | cldy/hanru/blockchain/transaction/UTXOSet.class 17 | cldy/hanru/blockchain/transaction/SpendableOutputResult.class 18 | cldy/hanru/blockchain/transaction/TXOutput.class 19 | cldy/hanru/blockchain/block/Block.class 20 | cldy/hanru/blockchain/pow/ProofOfWork.class 21 | -------------------------------------------------------------------------------- /part8_Transaction2/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java 2 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/wallet/Wallet.java 3 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java 4 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/UTXOSet.java 5 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/AddressUtils.java 6 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/wallet/WalletUtils.java 7 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java 8 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java 9 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/block/Block.java 10 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/store/RocksDBUtils.java 11 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/pow/PowResult.java 12 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/Base58Check.java 13 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/block/Blockchain.java 14 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/pow/ProofOfWork.java 15 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/Main.java 16 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/Transaction.java 17 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java 18 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/cli/CLI.java 19 | -------------------------------------------------------------------------------- /part8_Transaction2/target/part8_Transaction2-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/part8_Transaction2-jar-with-dependencies.jar -------------------------------------------------------------------------------- /part8_Transaction2/target/part8_Transaction2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/target/part8_Transaction2.jar -------------------------------------------------------------------------------- /part8_Transaction2/wallet.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part8_Transaction2/wallet.dat -------------------------------------------------------------------------------- /part9_Merkle/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /part9_Merkle/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /part9_Merkle/.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | -------------------------------------------------------------------------------- /part9_Merkle/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /part9_Merkle/blockchain.db/000004.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/blockchain.db/000004.sst -------------------------------------------------------------------------------- /part9_Merkle/blockchain.db/000010.sst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/blockchain.db/000010.sst -------------------------------------------------------------------------------- /part9_Merkle/blockchain.db/000015.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/blockchain.db/000015.log -------------------------------------------------------------------------------- /part9_Merkle/blockchain.db/CURRENT: -------------------------------------------------------------------------------- 1 | MANIFEST-000014 2 | -------------------------------------------------------------------------------- /part9_Merkle/blockchain.db/IDENTITY: -------------------------------------------------------------------------------- 1 | 155c6a2f6036e12d-b0dd4711eccfeb7c -------------------------------------------------------------------------------- /part9_Merkle/blockchain.db/LOCK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/blockchain.db/LOCK -------------------------------------------------------------------------------- /part9_Merkle/blockchain.db/MANIFEST-000014: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/blockchain.db/MANIFEST-000014 -------------------------------------------------------------------------------- /part9_Merkle/blockchain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # Check if the jar has been built. 6 | if [ ! -e target/part9_Merkle-jar-with-dependencies.jar ]; then 7 | echo "Compiling blockchain project to a JAR" 8 | mvn package -DskipTests 9 | fi 10 | 11 | java -jar target/part9_Merkle-jar-with-dependencies.jar "$@" 12 | 13 | -------------------------------------------------------------------------------- /part9_Merkle/readme.txt: -------------------------------------------------------------------------------- 1 | step1:在transaction包下,创建MerkleTree类 2 | 3 | step2:在MerkleTree了构建内部类:Node 4 | 5 | step3:构建叶子节点 6 | constructLeafNode() 7 | 8 | 9 | step4:构建底部节点 10 | bottomLevel() 11 | 12 | 13 | step5:构建内部节点 14 | internalHash() 15 | constructInternalNode() 16 | 17 | 18 | step6:构建层级节点 19 | internalLevel() 20 | 21 | 22 | step7:构建树 23 | constructTree() -------------------------------------------------------------------------------- /part9_Merkle/src/main/java/cldy/hanru/blockchain/Main.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain; 2 | 3 | 4 | import cldy.hanru.blockchain.cli.CLI; 5 | 6 | 7 | 8 | /** 9 | * 测试 10 | * 11 | * @author hanru 12 | */ 13 | public class Main { 14 | 15 | public static void main(String[] args) { 16 | 17 | CLI cli = new CLI(args); 18 | cli.run(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /part9_Merkle/src/main/java/cldy/hanru/blockchain/pow/PowResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.pow; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * 工作量计算结果 8 | * @author ruby 9 | * 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | public class PowResult { 14 | 15 | /** 16 | * 计数器 17 | */ 18 | private long nonce; 19 | /** 20 | * hash值 21 | */ 22 | private String hash; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /part9_Merkle/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Map; 8 | 9 | /** 10 | * @author hanru 11 | */ 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | @Data 15 | public class SpendableOutputResult { 16 | 17 | /** 18 | * 交易时的支付金额 19 | */ 20 | private int accumulated; 21 | /** 22 | * 未花费的交易 23 | */ 24 | private Map unspentOuts; 25 | } 26 | -------------------------------------------------------------------------------- /part9_Merkle/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | import cldy.hanru.blockchain.util.AddressUtils; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.util.Arrays; 9 | 10 | /** 11 | * @author hanru 12 | */ 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | @Data 16 | public class TXInput { 17 | 18 | 19 | /** 20 | * 交易Id的hash值 21 | */ 22 | private byte[] txId; 23 | /** 24 | * 交易输出索引 25 | */ 26 | private int txOutputIndex; 27 | /** 28 | * 解锁脚本 29 | */ 30 | // private String scriptSig; 31 | 32 | 33 | /** 34 | * 签名 35 | */ 36 | private byte[] signature; 37 | /** 38 | * 公钥 39 | */ 40 | private byte[] pubKey; 41 | 42 | 43 | /** 44 | * 判断解锁数据是否能够解锁交易输出 45 | * 46 | * @param unlockingData 47 | * @return 48 | */ 49 | // public boolean canUnlockOutputWith(String unlockingData) { 50 | // return this.getScriptSig().endsWith(unlockingData); 51 | // } 52 | 53 | 54 | /** 55 | * 检查公钥hash是否用于交易输入 56 | * 57 | * @param pubKeyHash 58 | * @return 59 | */ 60 | public boolean usesKey(byte[] pubKeyHash) { 61 | byte[] lockingHash = AddressUtils.ripeMD160Hash(this.getPubKey()); 62 | return Arrays.equals(lockingHash, pubKeyHash); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /part9_Merkle/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.transaction; 2 | 3 | 4 | import cldy.hanru.blockchain.util.Base58Check; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | import java.util.Arrays; 10 | 11 | /** 12 | * @author hanru 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | public class TXOutput { 18 | 19 | /** 20 | * 数值金额 21 | */ 22 | private int value; 23 | /** 24 | * 锁定脚本 25 | */ 26 | // private String scriptPubKey; 27 | 28 | 29 | /** 30 | * 公钥Hash 31 | */ 32 | private byte[] pubKeyHash; 33 | /** 34 | * 判断解锁数据是否能够解锁交易输出 35 | * 36 | * @param unlockingData 37 | * @return 38 | */ 39 | // public boolean canBeUnlockedWith(String unlockingData) { 40 | // 41 | // return this.getScriptPubKey().endsWith(unlockingData); 42 | // } 43 | 44 | 45 | /** 46 | * 检查交易输出是否能够使用指定的公钥 47 | * 48 | * @param pubKeyHash 49 | * @return 50 | */ 51 | public boolean isLockedWithKey(byte[] pubKeyHash) { 52 | return Arrays.equals(this.getPubKeyHash(), pubKeyHash); 53 | } 54 | 55 | /** 56 | * 创建交易输出 57 | * 58 | * @param value 59 | * @param address 60 | * @return 61 | */ 62 | public static TXOutput newTXOutput(int value, String address) { 63 | // 反向转化为 byte 数组 64 | byte[] versionedPayload = Base58Check.base58ToBytes(address); 65 | byte[] pubKeyHash = Arrays.copyOfRange(versionedPayload, 1, versionedPayload.length); 66 | return new TXOutput(value, pubKeyHash); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /part9_Merkle/src/main/java/cldy/hanru/blockchain/util/AddressUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import org.apache.commons.codec.digest.DigestUtils; 4 | import org.bouncycastle.crypto.digests.RIPEMD160Digest; 5 | import org.bouncycastle.util.Arrays; 6 | 7 | /** 8 | * 地址工具类 9 | * 10 | * @author hanru 11 | * 12 | */ 13 | public class AddressUtils { 14 | /** 15 | * 双重Hash 16 | * 17 | * @param data 18 | * @return 19 | */ 20 | public static byte[] doubleHash(byte[] data) { 21 | return DigestUtils.sha256(DigestUtils.sha256(data)); 22 | } 23 | 24 | /** 25 | * 计算公钥的 RIPEMD160 Hash值 26 | * 27 | * @param pubKey 公钥 28 | * @return ipeMD160Hash(sha256 ( pubkey)) 29 | */ 30 | public static byte[] ripeMD160Hash(byte[] pubKey) { 31 | //1. 先对公钥做 sha256 处理 32 | byte[] shaHashedKey = DigestUtils.sha256(pubKey); 33 | RIPEMD160Digest ripemd160 = new RIPEMD160Digest(); 34 | ripemd160.update(shaHashedKey, 0, shaHashedKey.length); 35 | byte[] output = new byte[ripemd160.getDigestSize()]; 36 | ripemd160.doFinal(output, 0); 37 | return output; 38 | } 39 | /** 40 | * 生成公钥的校验码 41 | * 42 | * @param payload 43 | * @return 44 | */ 45 | public static byte[] checksum(byte[] payload) { 46 | return Arrays.copyOfRange(doubleHash(payload), 0, 4); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /part9_Merkle/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Arrays; 5 | import java.util.stream.Stream; 6 | 7 | import org.apache.commons.codec.binary.Hex; 8 | import org.apache.commons.lang3.ArrayUtils; 9 | 10 | /** 11 | * 字节数组工具类 12 | * @author hanru 13 | * 14 | */ 15 | public class ByteUtils { 16 | 17 | public static final String ZERO_HASH = Hex.encodeHexString(new byte[32]); 18 | 19 | 20 | /** 21 | * 将多个字节数组合并成一个字节数组 22 | * @param bytes 23 | * @return 24 | */ 25 | public static byte[] merge(byte[]... bytes) { 26 | Stream stream = Stream.of(); 27 | for (byte[] b : bytes) { 28 | stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b))); 29 | } 30 | return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new)); 31 | } 32 | 33 | /** 34 | * long 转化为 byte[] 35 | * @param val 36 | * @return 37 | */ 38 | public static byte[] toBytes(long val) { 39 | return ByteBuffer.allocate(Long.BYTES).putLong(val).array(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /part9_Merkle/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | package cldy.hanru.blockchain.util; 2 | 3 | import com.esotericsoftware.kryo.Kryo; 4 | import com.esotericsoftware.kryo.io.Input; 5 | import com.esotericsoftware.kryo.io.Output; 6 | 7 | /** 8 | * 序列化工具类 9 | * @author hanru 10 | */ 11 | public class SerializeUtils { 12 | 13 | /** 14 | * 序列化 15 | * @param object 需要序列化的对象 16 | * @return 17 | */ 18 | public static byte[] serialize(Object object) { 19 | Output output = new Output(4096, -1); 20 | new Kryo().writeClassAndObject(output, object); 21 | byte[] bytes = output.toBytes(); 22 | output.close(); 23 | return bytes; 24 | } 25 | 26 | /** 27 | * 反序列化 28 | * @param bytes 对象对应的字节数组 29 | * @return 30 | */ 31 | public static Object deserialize(byte[] bytes) { 32 | Input input = new Input(bytes); 33 | Object obj = new Kryo().readClassAndObject(input); 34 | input.close(); 35 | return obj; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /part9_Merkle/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /part9_Merkle/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/Main.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/block/Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/block/Block.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/block/Blockchain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/block/Blockchain.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/cli/CLI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/cli/CLI.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/pow/PowResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/pow/PowResult.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/pow/ProofOfWork.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/store/RocksDBUtils.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/MerkleTree$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/MerkleTree$Node.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/MerkleTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/MerkleTree.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/SpendableOutputResult.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/TXInput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/TXInput.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/TXOutput.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/Transaction.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/Transaction.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/UTXOSet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/transaction/UTXOSet.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/util/AddressUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/util/AddressUtils.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/util/Base58Check.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/util/Base58Check.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/util/ByteUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/util/ByteUtils.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/util/SerializeUtils.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/wallet/Wallet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/wallet/Wallet.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class -------------------------------------------------------------------------------- /part9_Merkle/target/classes/cldy/hanru/blockchain/wallet/WalletUtils.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/classes/cldy/hanru/blockchain/wallet/WalletUtils.class -------------------------------------------------------------------------------- /part9_Merkle/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Sun Sep 30 14:13:11 CST 2018 3 | version=1.0-SNAPSHOT 4 | groupId=cldy.hanru.blockchain 5 | artifactId=part9_Merkle 6 | -------------------------------------------------------------------------------- /part9_Merkle/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | cldy/hanru/blockchain/cli/CLI.class 2 | cldy/hanru/blockchain/block/Blockchain.class 3 | cldy/hanru/blockchain/util/SerializeUtils.class 4 | cldy/hanru/blockchain/wallet/WalletUtils.class 5 | cldy/hanru/blockchain/wallet/WalletUtils$Wallets.class 6 | cldy/hanru/blockchain/Main.class 7 | cldy/hanru/blockchain/block/Blockchain$BlockchainIterator.class 8 | cldy/hanru/blockchain/util/AddressUtils.class 9 | cldy/hanru/blockchain/store/RocksDBUtils.class 10 | cldy/hanru/blockchain/transaction/Transaction.class 11 | cldy/hanru/blockchain/transaction/TXInput.class 12 | cldy/hanru/blockchain/util/Base58Check.class 13 | cldy/hanru/blockchain/pow/PowResult.class 14 | cldy/hanru/blockchain/util/ByteUtils.class 15 | cldy/hanru/blockchain/transaction/MerkleTree.class 16 | cldy/hanru/blockchain/wallet/Wallet.class 17 | cldy/hanru/blockchain/transaction/UTXOSet.class 18 | cldy/hanru/blockchain/transaction/SpendableOutputResult.class 19 | cldy/hanru/blockchain/transaction/TXOutput.class 20 | cldy/hanru/blockchain/block/Block.class 21 | cldy/hanru/blockchain/transaction/MerkleTree$Node.class 22 | cldy/hanru/blockchain/pow/ProofOfWork.class 23 | -------------------------------------------------------------------------------- /part9_Merkle/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/TXOutput.java 2 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/wallet/Wallet.java 3 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/SpendableOutputResult.java 4 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/UTXOSet.java 5 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/AddressUtils.java 6 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/wallet/WalletUtils.java 7 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/SerializeUtils.java 8 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/TXInput.java 9 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/block/Block.java 10 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/store/RocksDBUtils.java 11 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/pow/PowResult.java 12 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/Base58Check.java 13 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/block/Blockchain.java 14 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/pow/ProofOfWork.java 15 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/Main.java 16 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/transaction/Transaction.java 17 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/util/ByteUtils.java 18 | /Users/ruby/JavaProjects/part8_Transaction2/src/main/java/cldy/hanru/blockchain/cli/CLI.java 19 | -------------------------------------------------------------------------------- /part9_Merkle/target/part8_Transaction2-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/part8_Transaction2-jar-with-dependencies.jar -------------------------------------------------------------------------------- /part9_Merkle/target/part8_Transaction2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/part8_Transaction2.jar -------------------------------------------------------------------------------- /part9_Merkle/target/part9_Merkle-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/part9_Merkle-jar-with-dependencies.jar -------------------------------------------------------------------------------- /part9_Merkle/target/part9_Merkle.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/target/part9_Merkle.jar -------------------------------------------------------------------------------- /part9_Merkle/wallet.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rubyhan1314/BitcoinForJava/674ceb3d2a68be2809f8d869bce51f77baa6d100/part9_Merkle/wallet.dat --------------------------------------------------------------------------------