├── settings.gradle ├── TODO ├── bitcoin-core ├── src │ ├── test │ │ ├── resources │ │ │ ├── networkconfiguration2-config.json │ │ │ ├── networkconfiguration1-config.json │ │ │ └── networkconfiguration3-config.json │ │ └── java │ │ │ └── test │ │ │ └── com │ │ │ └── wealdtech │ │ │ └── bitcoin │ │ │ ├── transaction │ │ │ ├── TransactionTest.java │ │ │ └── HashTypeTest.java │ │ │ ├── crypto │ │ │ └── ECKeyTest.java │ │ │ ├── BitcoinKeyTest.java │ │ │ ├── NetworkTest.java │ │ │ ├── script │ │ │ ├── OpcodeTest.java │ │ │ ├── OpTest.java │ │ │ └── ScriptTest.java │ │ │ ├── generator │ │ │ └── raw │ │ │ │ ├── ScriptGeneratorRawImplTest.java │ │ │ │ ├── UtilsTest.java │ │ │ │ └── TransactionGeneratorRawImplTest.java │ │ │ ├── AddressTest.java │ │ │ ├── NetworkConfigurationTest.java │ │ │ ├── scenarios │ │ │ └── AnyoneCanPayTest.java │ │ │ ├── ValueTest.java │ │ │ └── BTCUnitTest.java │ └── main │ │ └── java │ │ └── com │ │ └── wealdtech │ │ └── bitcoin │ │ ├── crypto │ │ ├── Hash.java │ │ ├── ECSignature.java │ │ ├── Ripemd160Hash.java │ │ ├── Sha256Hash.java │ │ ├── Crypto.java │ │ └── ECKey.java │ │ ├── generator │ │ ├── Generator.java │ │ └── raw │ │ │ ├── BaseGeneratorRawImpl.java │ │ │ ├── TransactionGeneratorRawImpl.java │ │ │ ├── ScriptGeneratorRawImpl.java │ │ │ └── Utils.java │ │ ├── script │ │ ├── StandardOutputScript.java │ │ ├── StandardInputScript.java │ │ ├── Script.java │ │ ├── Op.java │ │ └── Opcode.java │ │ ├── Address.java │ │ ├── transaction │ │ ├── SimpleSpendTransactionBuilder.java │ │ ├── HashType.java │ │ ├── TransactionOutput.java │ │ ├── TransactionInput.java │ │ └── Transaction.java │ │ ├── config │ │ └── NetworkConfiguration.java │ │ ├── BitcoinKey.java │ │ ├── Network.java │ │ ├── Value.java │ │ ├── BTCUnit.java │ │ └── utils │ │ └── Base58.java ├── .project └── build.gradle ├── .gitignore └── README.md /settings.gradle: -------------------------------------------------------------------------------- 1 | include 'bitcoin-core' 2 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | Core: 2 | - Create basic system to generate transactions 3 | -------------------------------------------------------------------------------- /bitcoin-core/src/test/resources/networkconfiguration2-config.json: -------------------------------------------------------------------------------- 1 | // Empty configuration 2 | { 3 | } -------------------------------------------------------------------------------- /bitcoin-core/src/test/resources/networkconfiguration1-config.json: -------------------------------------------------------------------------------- 1 | // Test configuration 2 | { 3 | "network": "Test" 4 | } -------------------------------------------------------------------------------- /bitcoin-core/src/test/resources/networkconfiguration3-config.json: -------------------------------------------------------------------------------- 1 | // Invalid configuration 2 | { 3 | "network": "Bad" 4 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | 8 | # Eclipse 9 | bin 10 | .classpath 11 | .metadata 12 | .settings 13 | 14 | # Gradle 15 | build 16 | .gradle 17 | 18 | # TestNG 19 | test-output 20 | -------------------------------------------------------------------------------- /bitcoin-core/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | bitcoin-core 4 | 5 | 6 | 7 | org.eclipse.jdt.core.javanature 8 | 9 | 10 | 11 | org.eclipse.jdt.core.javabuilder 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /bitcoin-core/src/main/java/com/wealdtech/bitcoin/crypto/Hash.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Weald Technology Trading Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.wealdtech.bitcoin.crypto; 17 | 18 | import java.io.Serializable; 19 | 20 | import com.google.common.collect.ImmutableList; 21 | 22 | public interface Hash extends Serializable, Comparable 23 | { 24 | public ImmutableList getBytes(); 25 | 26 | @Override 27 | int compareTo(Hash that); 28 | } 29 | -------------------------------------------------------------------------------- /bitcoin-core/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies 2 | { 3 | compile 'com.google.guava:guava:14.0' 4 | compile 'com.google.code.findbugs:jsr305:1.3.9' 5 | compile 'com.google.inject:guice:3.0' 6 | compile 'ch.qos.logback:logback-classic:1.0.9' 7 | compile 'com.fasterxml.jackson.core:jackson-core:2.1.4' 8 | compile 'com.fasterxml.jackson.core:jackson-databind:2.1.4' 9 | compile 'com.fasterxml.jackson.core:jackson-annotations:2.1.4' 10 | compile 'com.fasterxml.jackson.datatype:jackson-datatype-guava:2.1.2' 11 | compile 'joda-time:joda-time:2.1' 12 | compile 'com.yammer.metrics:metrics-core:2.2.0' 13 | compile 'org.mindrot:jbcrypt:0.3m' 14 | compile 'com.wealdtech:wealdtech-core:1.5.0' 15 | compile 'org.bouncycastle:bcprov-jdk16:1.46' 16 | testCompile 'org.testng:testng:6.8' 17 | } 18 | 19 | uploadArchives { 20 | repositories { 21 | mavenDeployer { 22 | pom.artifactId = 'bitcoin-core' 23 | pom.project { 24 | name 'Bitcoin Core' 25 | description 'Weald Technology core libraries for Bitcoin' 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /bitcoin-core/src/main/java/com/wealdtech/bitcoin/generator/Generator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Weald Technology Trading Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.wealdtech.bitcoin.generator; 17 | 18 | import java.io.ByteArrayOutputStream; 19 | 20 | import com.google.common.collect.ImmutableList; 21 | 22 | public interface Generator 23 | { 24 | void startGen(); 25 | 26 | void startGen(final ByteArrayOutputStream inBuf); 27 | 28 | void generate(final T input); 29 | 30 | void generate(final T input, final boolean includeLength); 31 | 32 | void addRawBytes(final byte[] bytes); 33 | 34 | ImmutableList finishGen(); 35 | } 36 | -------------------------------------------------------------------------------- /bitcoin-core/src/test/java/test/com/wealdtech/bitcoin/transaction/TransactionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Weald Technology Trading Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test.com.wealdtech.bitcoin.transaction; 17 | 18 | import static org.testng.Assert.fail; 19 | 20 | import org.testng.annotations.Test; 21 | 22 | import com.wealdtech.DataError; 23 | 24 | public class TransactionTest 25 | { 26 | @Test 27 | public void testParseValid1() throws Exception 28 | { 29 | } 30 | 31 | @Test 32 | public void testParseInvalid1() throws Exception 33 | { 34 | try 35 | { 36 | fail(""); 37 | } 38 | catch (DataError.Bad de) 39 | { 40 | // Good 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /bitcoin-core/src/test/java/test/com/wealdtech/bitcoin/crypto/ECKeyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Weald Technology Trading Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test.com.wealdtech.bitcoin.crypto; 17 | 18 | import static org.testng.Assert.assertTrue; 19 | 20 | import java.math.BigInteger; 21 | 22 | import org.testng.annotations.Test; 23 | 24 | import com.wealdtech.bitcoin.crypto.ECKey; 25 | import com.wealdtech.bitcoin.utils.Base58; 26 | 27 | public class ECKeyTest 28 | { 29 | @Test 30 | public void testECKey1() throws Exception 31 | { 32 | final ECKey key = ECKey.fromBase58String("cNasU8BJHPcApCVfe4jU6bRSLazB1gnA46JTdYg4CuNUZnajKWVx"); 33 | System.err.println("Pubkey is " + key.getPubKey()); 34 | BigInteger pubkey = key.getPubKey(); 35 | 36 | assertTrue(key.getPubKey().equals(new BigInteger(1, Base58.decodeChecked("n1H4uDa8J3NooFU5sNqb4zLUcvE611SqgZ")))); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /bitcoin-core/src/test/java/test/com/wealdtech/bitcoin/BitcoinKeyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Weald Technology Trading Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test.com.wealdtech.bitcoin; 17 | 18 | import org.testng.annotations.BeforeClass; 19 | import org.testng.annotations.Test; 20 | 21 | import com.wealdtech.bitcoin.Network; 22 | import com.wealdtech.bitcoin.config.NetworkConfiguration; 23 | import com.wealdtech.configuration.ConfigurationSource; 24 | 25 | public class BitcoinKeyTest 26 | { 27 | private Network network; 28 | 29 | @BeforeClass 30 | public void setup() throws Exception 31 | { 32 | this.network = new ConfigurationSource().getConfiguration("bitcoinkey-config.json", NetworkConfiguration.class).getNetwork(); 33 | } 34 | 35 | @Test 36 | public void testValid1() throws Exception 37 | { 38 | // final BitcoinKey key = new BitcoinKey("fred"); 39 | } 40 | 41 | // NULL 42 | 43 | // Invalid address 44 | 45 | // Invalid checksum 46 | 47 | } 48 | -------------------------------------------------------------------------------- /bitcoin-core/src/test/java/test/com/wealdtech/bitcoin/NetworkTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Weald Technology Trading Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test.com.wealdtech.bitcoin; 17 | 18 | import static org.testng.Assert.assertEquals; 19 | 20 | import org.testng.annotations.Test; 21 | 22 | import com.wealdtech.bitcoin.Network; 23 | 24 | public class NetworkTest 25 | { 26 | @Test 27 | public void testProdPubkeyValid() throws Exception 28 | { 29 | assertEquals(Network.PRODUCTION, Network.fromVersion(0)); 30 | } 31 | 32 | @Test 33 | public void testProdScriptValid() throws Exception 34 | { 35 | assertEquals(Network.PRODUCTION, Network.fromVersion(5)); 36 | } 37 | 38 | @Test 39 | public void testTestPubkeyValid() throws Exception 40 | { 41 | assertEquals(Network.TEST, Network.fromVersion(111)); 42 | } 43 | 44 | @Test 45 | public void testTestScriptValid() throws Exception 46 | { 47 | assertEquals(Network.TEST, Network.fromVersion(196)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /bitcoin-core/src/test/java/test/com/wealdtech/bitcoin/script/OpcodeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Weald Technology Trading Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test.com.wealdtech.bitcoin.script; 17 | 18 | import static org.testng.Assert.*; 19 | 20 | import org.testng.annotations.Test; 21 | 22 | import com.wealdtech.DataError; 23 | import com.wealdtech.bitcoin.script.Opcode; 24 | 25 | public class OpcodeTest 26 | { 27 | @Test 28 | public void testParseValid1() throws Exception 29 | { 30 | final Opcode opcode = Opcode.parse("op_checksig"); 31 | assertEquals(opcode, Opcode.OP_CHECKSIG); 32 | } 33 | 34 | @Test 35 | public void testToStringValid1() throws Exception 36 | { 37 | final Opcode opcode = Opcode.OP_CHECKSIG; 38 | assertEquals(opcode.toString(), "OP_CHECKSIG"); 39 | } 40 | 41 | @Test 42 | public void testParseInvalid1() throws Exception 43 | { 44 | try 45 | { 46 | Opcode.parse("op_notappearinginthiscode"); 47 | fail("Managed to parse invalid opcode"); 48 | } 49 | catch (DataError.Bad de) 50 | { 51 | // Good 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /bitcoin-core/src/main/java/com/wealdtech/bitcoin/script/StandardOutputScript.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Weald Technology Trading Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.wealdtech.bitcoin.script; 17 | 18 | import static com.wealdtech.bitcoin.script.Opcode.*; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | import com.wealdtech.bitcoin.Address; 24 | import com.wealdtech.bitcoin.transaction.TransactionOutput; 25 | 26 | /** 27 | * Standard output script which sends the funds to a single recipient. 28 | */ 29 | public class StandardOutputScript 30 | { 31 | /** 32 | * Create a standard output script which sends the funds to a single recipient 33 | * @param recipient the {@link Address} of the recipient 34 | * @return a script suitable for insertion in to a {@link TransactionOutput} 35 | */ 36 | public static Script create(final Address recipient) 37 | { 38 | List ops = new ArrayList<>(); 39 | ops.add(new Op(OP_DUP)); 40 | ops.add(new Op(OP_HASH160)); 41 | ops.add(new Op(recipient.getHash().getBytes())); 42 | ops.add(new Op(OP_EQUALVERIFY)); 43 | ops.add(new Op(OP_CHECKSIG)); 44 | 45 | return new Script(ops); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /bitcoin-core/src/test/java/test/com/wealdtech/bitcoin/generator/raw/ScriptGeneratorRawImplTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Weald Technology Trading Limited 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test.com.wealdtech.bitcoin.generator.raw; 17 | 18 | import static org.testng.Assert.assertEquals; 19 | 20 | import org.testng.annotations.Test; 21 | 22 | import com.wealdtech.bitcoin.Address; 23 | import com.wealdtech.bitcoin.generator.Generator; 24 | import com.wealdtech.bitcoin.generator.raw.ScriptGeneratorRawImpl; 25 | import com.wealdtech.bitcoin.generator.raw.Utils; 26 | import com.wealdtech.bitcoin.script.StandardOutputScript; 27 | import com.wealdtech.bitcoin.script.Script; 28 | 29 | public class ScriptGeneratorRawImplTest 30 | { 31 | @Test 32 | public void testOutputScript1() throws Exception 33 | { 34 | Script script = StandardOutputScript.create(Address.fromAddressString("mnR2ntXMDv2PaHCgCbby8iQg6TAv8Ecp5D")); 35 | 36 | Generator