Skip to content

Commit

Permalink
Migrate SSLSocketFactory -> SSLContext, NetworkParameters -> Network
Browse files Browse the repository at this point in the history
* Remove deprecated methods, etc.
* Breaking change to replace SSLSocketFactory with SSLContext
* Other, related changes
  • Loading branch information
msgilligan committed Aug 31, 2023
1 parent 28d6435 commit dd5f5d8
Show file tree
Hide file tree
Showing 19 changed files with 47 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public OmniCoreCLICall(BitcoinCLITool tool, PrintStream out, PrintStream err, St

@Override
public OmniClient createClient(SSLContext sslContext, RpcConfig config) {
return new OmniClient(sslContext.getSocketFactory(), config.network(), config.getURI(), config.getUsername(), config.getPassword(), false, false);
return new OmniClient(sslContext, config.network(), config.getURI(), config.getUsername(), config.getPassword(), false, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private LegacyAddress parseBase58AnyNetwork(String base58)
.orElseThrow(() -> new AddressFormatException.InvalidPrefix("No network found for " + base58));
}

// Create an unmodifiable set of NetworkParameters from an array/varargs
// Create an unmodifiable list of Network from an array/varargs
private static List<Network> unmodifiableList(Network... ts) {
return Collections.unmodifiableList(new ArrayList<>(Arrays.asList(ts)));
}
Expand Down
11 changes: 8 additions & 3 deletions omnij-core/src/main/java/foundation/omni/tx/ClassCEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.bitcoinj.base.Address;
import org.bitcoinj.base.Coin;
import org.bitcoinj.base.Network;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.script.Script;
Expand All @@ -21,14 +20,20 @@ public class ClassCEncoder {
private static final int OMNI_MARKER_LENGTH = OMNI_MARKER.length();
/** Maximum Class C Payload length (not counting the 4-byte {@code 'omni'} marker) */
public static final int MAX_CLASS_C_PAYLOAD = 80 - OMNI_MARKER_LENGTH;
private final NetworkParameters netParams;

/**
* Construct an encoder
*/
public ClassCEncoder() {
}

/**
* Construct an encoder for a network
* @param network The network this instance will encode transactions for
* @deprecated Use {@link ClassCEncoder#ClassCEncoder()}
*/
@Deprecated
public ClassCEncoder(Network network) {
this.netParams = NetworkParameters.of(network);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.bitcoinj.base.Coin;
import org.bitcoinj.base.Network;
import org.bitcoinj.crypto.ECKey;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.script.Script;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.bitcoinj.base.Address;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.ScriptType;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.crypto.ECKey;
import org.bitcoinj.core.InsufficientMoneyException;
import org.bitcoinj.core.Transaction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import foundation.omni.CurrencyID;
import foundation.omni.OmniValue;
import foundation.omni.net.OmniNetwork;
import foundation.omni.net.OmniNetworkParameters;
import org.bitcoinj.base.Address;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.Coin;
Expand Down Expand Up @@ -54,7 +53,7 @@ public OmniTxBuilder(OmniNetwork omniNetwork, FeeCalculator feeCalculator) {
this.network = omniNetwork.bitcoinNetwork();
this.omniNetwork = omniNetwork;
this.transactionEncoder = new EncodeMultisig(network);
this.classCEncoder = new ClassCEncoder(network);
this.classCEncoder = new ClassCEncoder();
this.feeCalculator = feeCalculator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import foundation.omni.OmniDivisibleValue
import org.bitcoinj.base.BitcoinNetwork
import org.bitcoinj.base.Coin
import org.bitcoinj.base.Sha256Hash
import org.bitcoinj.core.NetworkParameters
import org.bitcoinj.core.Transaction
import org.bitcoinj.core.TransactionInput
import org.bitcoinj.core.TransactionOutPoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package foundation.omni.tx

import org.bitcoinj.core.NetworkParameters
import org.bitcoinj.core.Transaction
import spock.lang.Ignore

Expand Down
34 changes: 7 additions & 27 deletions omnij-jsonrpc/src/main/java/foundation/omni/rpc/OmniClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.bitcoinj.base.Address;
import org.bitcoinj.base.Coin;
import org.bitcoinj.base.Network;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.base.Sha256Hash;
import org.consensusj.bitcoin.jsonrpc.RpcConfig;
import org.consensusj.bitcoin.jsonrpc.bitcoind.BitcoinConfFile;
Expand All @@ -24,7 +23,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
Expand Down Expand Up @@ -61,44 +60,25 @@ public OmniClient(RpcConfig config) {
}

public OmniClient(Network network, URI server, String rpcuser, String rpcpassword) {
this((SSLSocketFactory)SSLSocketFactory.getDefault(), network, server, rpcuser, rpcpassword, false, false);
this(getDefaultSSLContext(), network, server, rpcuser, rpcpassword, false, false);
}

public OmniClient(SSLSocketFactory sslSocketFactory, Network network, URI server, String rpcuser, String rpcpassword, boolean useZmq) {
this(sslSocketFactory, network, server, rpcuser, rpcpassword, useZmq, false);
public OmniClient(SSLContext sslContext, Network network, URI server, String rpcuser, String rpcpassword, boolean useZmq) {
this(sslContext, network, server, rpcuser, rpcpassword, useZmq, false);
}

public OmniClient(Network network, URI server, String rpcuser, String rpcpassword, boolean useZmq, boolean isOmniProxy) {
this((SSLSocketFactory)SSLSocketFactory.getDefault(), network, server, rpcuser, rpcpassword, useZmq, isOmniProxy);
this(getDefaultSSLContext(), network, server, rpcuser, rpcpassword, useZmq, isOmniProxy);
}

public OmniClient(SSLSocketFactory sslSocketFactory, Network network, URI server, String rpcuser, String rpcpassword, boolean useZmq, boolean isOmniProxy) {
super(sslSocketFactory, network, server, rpcuser, rpcpassword, useZmq);
public OmniClient(SSLContext sslContext, Network network, URI server, String rpcuser, String rpcpassword, boolean useZmq, boolean isOmniProxy) {
super(sslContext, network, server, rpcuser, rpcpassword, useZmq);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new OmniClientModule());
mapper.registerModule(new ParameterNamesModule());
this.isOmniProxy = isOmniProxy;
}

/**
* @deprecated Use {@link OmniClient#OmniClient(SSLSocketFactory, Network, URI, String, String, boolean, boolean)}
*/
@Deprecated
public OmniClient(SSLSocketFactory sslSocketFactory, NetworkParameters netParams, URI server, String rpcuser, String rpcpassword, boolean useZmq, boolean isOmniProxy) {
this(sslSocketFactory, netParams.network(), server, rpcuser, rpcpassword, useZmq, isOmniProxy);
}


@Deprecated
public OmniClient(SSLSocketFactory sslSocketFactory, NetworkParameters netParams, URI server, String rpcuser, String rpcpassword) {
this(sslSocketFactory, netParams.network(), server, rpcuser, rpcpassword, false);
}

@Deprecated
public OmniClient(NetworkParameters netParams, URI server, String rpcuser, String rpcpassword) {
this((SSLSocketFactory)SSLSocketFactory.getDefault(), netParams.network(), server, rpcuser, rpcpassword, false);
}

public OmniNetworkParameters getOmniNetParams() {
return OmniNetworkParameters.fromBitcoinNetwork(getNetwork());
}
Expand Down
7 changes: 0 additions & 7 deletions omnij-net-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,3 @@ jar {
'Implementation-Version': archiveVersion.get()
}
}

idea {
module {
jdkName = '1.8'
// languageLevel = '1.8'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import foundation.omni.json.pojo.WalletAddressBalance;
import foundation.omni.BalanceEntry;
import org.bitcoinj.base.Address;
import org.bitcoinj.core.NetworkParameters;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
Expand All @@ -56,31 +56,28 @@ public OmniCoreClient(OmniClient client)
this.client = client;
}

public OmniCoreClient(SSLSocketFactory sslSocketFactory, Network network, URI coreURI, String user, String pass, boolean useZmq, boolean isOmniProxy) {
client = new OmniClient(sslSocketFactory, network, coreURI, user, pass, useZmq, isOmniProxy);
public OmniCoreClient(SSLContext sslContext, Network network, URI coreURI, String user, String pass, boolean useZmq, boolean isOmniProxy) {
client = new OmniClient(sslContext, network, coreURI, user, pass, useZmq, isOmniProxy);
}

@Deprecated
public OmniCoreClient(SSLSocketFactory sslSocketFactory, NetworkParameters netParams, URI coreURI, String user, String pass, boolean useZmq, boolean isOmniProxy) {
this(sslSocketFactory, netParams.network(), coreURI, user, pass, useZmq, isOmniProxy);
}

@Deprecated
public OmniCoreClient(SSLSocketFactory sslSocketFactory, NetworkParameters netParams, URI coreURI, String user, String pass) {
this(sslSocketFactory, netParams.network(), coreURI, user, pass, false, false);
}

@Deprecated
public OmniCoreClient(NetworkParameters netParams, URI coreURI, String user, String pass) {
this((SSLSocketFactory)SSLSocketFactory.getDefault(), netParams.network(), coreURI, user, pass);
public OmniCoreClient(SSLContext sslContext, Network network, URI coreURI, String user, String pass) {
this(sslContext, network, coreURI, user, pass, false, false);
}

public OmniCoreClient(SSLSocketFactory sslSocketFactory, Network network, URI coreURI, String user, String pass) {
this(sslSocketFactory, network, coreURI, user, pass, false, false);
public OmniCoreClient(Network network, URI coreURI, String user, String pass) {
this(getDefaultSSLContext(), network, coreURI, user, pass);
}

public OmniCoreClient(Network network, URI coreURI, String user, String pass) {
this((SSLSocketFactory)SSLSocketFactory.getDefault(), network, coreURI, user, pass);
/**
* Return the default {@link SSLContext} without declaring a checked exception
* @return The default {@code SSLContext}
*/
protected static SSLContext getDefaultSSLContext() {
try {
return SSLContext.getDefault();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.reactivex.rxjava3.processors.FlowableProcessor;
import org.bitcoinj.base.Address;
import org.bitcoinj.base.Network;
import org.bitcoinj.core.NetworkParameters;
import org.consensusj.bitcoin.json.pojo.ChainTip;
import org.consensusj.bitcoin.rx.jsonrpc.PollingChainTipService;
import org.reactivestreams.Publisher;
Expand Down Expand Up @@ -92,11 +91,6 @@ public OmniwalletAbstractClient(URI baseURI, boolean debug, boolean strictMode,
chainTipSource = pollForDistinctChainTip();
}

@Deprecated
public OmniwalletAbstractClient(URI baseURI, boolean debug, boolean strictMode, NetworkParameters netParams) {
this(baseURI, debug, strictMode, netParams.network());
}

public synchronized void start() {
if (chainTipSubscription == null) {
chainTipSubscription = chainTipSource.subscribe(chainTipProcessor::onNext, chainTipProcessor::onError, chainTipProcessor::onComplete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import foundation.omni.netapi.omniwallet.json.RevisionInfo;
import org.bitcoinj.base.Address;
import org.bitcoinj.base.Network;
import org.bitcoinj.core.NetworkParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -61,18 +60,6 @@ public OmniwalletModernJDKClient(URI baseURI, boolean debug, boolean strictMode,
objectMapper.registerModule(new OmniwalletClientModule(network));
}

/**
*
* @param baseURI Base URL of server
* @param debug Enable debugging, logging, etc.
* @param strictMode Only accept valid amounts from server
* @param netParams Specify active Bitcoin network (used for Address validation)
*/
@Deprecated
public OmniwalletModernJDKClient(URI baseURI, boolean debug, boolean strictMode, NetworkParameters netParams) {
this(baseURI, debug, strictMode, netParams.network());
}

@Override
protected CompletableFuture<OmniwalletPropertiesListResponse> propertiesList() {
HttpRequest request = buildGetRequest("/v1/properties/list");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.bitcoinj.base.Network;
import org.bitcoinj.core.NetworkParameters;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Field;
Expand Down Expand Up @@ -127,34 +126,6 @@ public OmniwalletClient(URI baseURI, boolean debug, boolean strictMode, Network
service = restAdapter.create(OmniwalletService.class);
}

/**
* @param baseURI Base URL of server
* @param debug Enable debugging, logging, etc.
* @param strictMode Only accept valid amounts from server
* @param netParams Specify active Bitcoin network (used for Address validation)
* @deprecated Use {@link OmniwalletClient#OmniwalletClient(URI, boolean, boolean, Network)}
*/
@Deprecated
public OmniwalletClient(URI baseURI, boolean debug, boolean strictMode, NetworkParameters netParams) {
this(baseURI, debug, strictMode, netParams.network());
}

/**
* OmniwalletClient constructor with all parameters
*
* @param baseURI Base URL of server
* @param debug Enable debugging, logging, etc.
* @param strictMode Only accept valid amounts from server
* @param netParams Specify active Bitcoin network (used for Address validation)
* @param client Custom OkHttp client
* @param executor Executor
* @deprecated Use {@link OmniwalletClient#OmniwalletClient(URI, boolean, boolean, Network, OkHttpClient, Executor)}
*/
@Deprecated
public OmniwalletClient(URI baseURI, boolean debug, boolean strictMode, NetworkParameters netParams, OkHttpClient client, Executor executor) {
this(baseURI, debug, strictMode, netParams.network(), client, executor);
}

public static OkHttpClient defaultHttpClient(boolean debug) {

OkHttpClient.Builder builder = defaultHttpClientBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class OmniCoreConsensusTool extends OmniCoreClient implements ConsensusTo
/**
* URI Constructor
*
* @param network *bitcoinj* NetworkParameters for server to connect to
* @param network *bitcoinj* Network for server to connect to
* @param coreURI URI to connect to - user/pass if required, must be encoded in URL
*/
public OmniCoreConsensusTool(Network network, URI coreURI) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package foundation.omni.txsigner;

import org.bitcoinj.base.Network;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Transaction;
import org.consensusj.bitcoin.json.pojo.SignedRawTransaction;
import org.consensusj.bitcoin.jsonrpc.BitcoinClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.bitcoinj.base.Address
import org.bitcoinj.base.BitcoinNetwork
import org.bitcoinj.base.Network
import org.bitcoinj.base.ScriptType
import org.bitcoinj.core.NetworkParameters
import org.bitcoinj.core.Transaction
import org.bitcoinj.core.TransactionInput
import org.bitcoinj.script.Script
Expand Down Expand Up @@ -43,7 +42,6 @@ class OmniKeychainSigningServiceSpec extends Specification {
def "basic test"() {
given: "Setup similar to that in SendTool"
Network network = BitcoinNetwork.TESTNET
NetworkParameters netParams = NetworkParameters.of(network)
ScriptType outputScriptType = ScriptType.P2PKH
DeterministicSeed seed = setupTestSeed()

Expand All @@ -54,7 +52,7 @@ class OmniKeychainSigningServiceSpec extends Specification {

URI omniProxyTestNetURI = omniProxyUri
RpcConfig config = new RpcConfig(network, omniProxyTestNetURI, "bitcoinrpc", "pass");
var omniProxyClient = new OmniClient(config.getNetParams(),
var omniProxyClient = new OmniClient(config.network(),
config.getURI(),
config.getUsername(),
config.getPassword(),
Expand All @@ -72,15 +70,15 @@ class OmniKeychainSigningServiceSpec extends Specification {
Transaction tx = signService.omniSignTx(sendTx).join()

and: "We do some verification"
tx.verify()
Transaction.verify(network, tx)
Script scriptPubKey = ScriptBuilder.createOutputScript(fromAddress);
TransactionInput input = tx.getInputs().get(0)
input.getScriptSig()
.correctlySpends(tx, 0, null, input.getValue(), scriptPubKey, Script.ALL_VERIFY_FLAGS);

var serializedTx = ByteBuffer.wrap(tx.bitcoinSerialize())
var serializedTx = ByteBuffer.wrap(tx.serialize()t)

Transaction deserializedTx = new Transaction(netParams, serializedTx)
Transaction deserializedTx = Transaction.read(serializedTx)

then: "No exception was thrown and deserializedTx is not null"
deserializedTx != null
Expand Down
Loading

0 comments on commit dd5f5d8

Please sign in to comment.