Wallet: constructor use Network rather than NetworkParameters

With deprecation.
This commit is contained in:
Sean Gilligan 2023-04-09 09:40:10 -07:00 committed by Andreas Schildbach
parent f41336657d
commit ce07733bbf
7 changed files with 43 additions and 28 deletions

View File

@ -516,7 +516,7 @@ public class WalletAppKit extends AbstractIdleService implements Closeable {
else
kcg.fromRandom(preferredOutputScriptType);
return walletFactory.create(params, kcg.build());
return walletFactory.create(network, kcg.build());
}
private void maybeMoveOldWalletOutOfTheWay() {

View File

@ -246,6 +246,7 @@ public class Wallet extends BaseTaggableObject
// A list of scripts watched by this wallet.
@GuardedBy("keyChainGroupLock") private final Set<Script> watchedScripts;
protected final Network network;
protected final NetworkParameters params;
@Nullable private Sha256Hash lastBlockSeenHash;
@ -334,7 +335,7 @@ public class Wallet extends BaseTaggableObject
* @return A new empty wallet
*/
public static Wallet createDeterministic(NetworkParameters params, ScriptType outputScriptType, KeyChainGroupStructure keyChainGroupStructure) {
return new Wallet(params, KeyChainGroup.builder(params, keyChainGroupStructure).fromRandom(outputScriptType).build());
return new Wallet(params.network(), KeyChainGroup.builder(params, keyChainGroupStructure).fromRandom(outputScriptType).build());
}
/**
@ -343,7 +344,7 @@ public class Wallet extends BaseTaggableObject
* @param params network parameters
*/
public static Wallet createBasic(NetworkParameters params) {
return new Wallet(params, KeyChainGroup.createBasic(params));
return new Wallet(params.network(), KeyChainGroup.createBasic(params));
}
/**
@ -366,7 +367,7 @@ public class Wallet extends BaseTaggableObject
*/
public static Wallet fromSeed(NetworkParameters params, DeterministicSeed seed, ScriptType outputScriptType,
KeyChainGroupStructure structure) {
return new Wallet(params, KeyChainGroup.builder(params, structure).fromSeed(seed, outputScriptType).build());
return new Wallet(params.network(), KeyChainGroup.builder(params, structure).fromSeed(seed, outputScriptType).build());
}
/**
@ -380,7 +381,7 @@ public class Wallet extends BaseTaggableObject
List<ChildNumber> accountPath) {
DeterministicKeyChain chain = DeterministicKeyChain.builder().seed(seed).outputScriptType(outputScriptType)
.accountPath(accountPath).build();
return new Wallet(params, KeyChainGroup.builder(params).addChain(chain).build());
return new Wallet(params.network(), KeyChainGroup.builder(params).addChain(chain).build());
}
/**
@ -391,7 +392,7 @@ public class Wallet extends BaseTaggableObject
ScriptType outputScriptType) {
DeterministicKeyChain chain = DeterministicKeyChain.builder().watch(watchKey).outputScriptType(outputScriptType)
.build();
return new Wallet(params, KeyChainGroup.builder(params).addChain(chain).build());
return new Wallet(params.network(), KeyChainGroup.builder(params).addChain(chain).build());
}
/**
@ -437,7 +438,7 @@ public class Wallet extends BaseTaggableObject
ScriptType outputScriptType) {
DeterministicKeyChain chain = DeterministicKeyChain.builder().spend(spendKey).outputScriptType(outputScriptType)
.build();
return new Wallet(params, KeyChainGroup.builder(params).addChain(chain).build());
return new Wallet(params.network(), KeyChainGroup.builder(params).addChain(chain).build());
}
/**
@ -489,7 +490,7 @@ public class Wallet extends BaseTaggableObject
accountKey.clearCreationTime();
DeterministicKeyChain chain = DeterministicKeyChain.builder().spend(accountKey)
.outputScriptType(outputScriptType).build();
return new Wallet(params, KeyChainGroup.builder(params).addChain(chain).build());
return new Wallet(params.network(), KeyChainGroup.builder(params).addChain(chain).build());
}
private static ScriptType outputScriptTypeFromB58(NetworkParameters params, String base58) {
@ -506,12 +507,13 @@ public class Wallet extends BaseTaggableObject
* Creates a new, empty wallet with a randomly chosen seed and no transactions. Make sure to provide for sufficient
* backup! Any keys will be derived from the seed. If you want to restore a wallet from disk instead, see
* {@link #loadFromFile}.
* @param params network parameters
* @param network network to operate on
* @param keyChainGroup keychain group to manage keychains
*/
public Wallet(NetworkParameters params, KeyChainGroup keyChainGroup) {
this.params = Objects.requireNonNull(params);
this.coinSelector = DefaultCoinSelector.get(params.network());
public Wallet(Network network, KeyChainGroup keyChainGroup) {
this.network = Objects.requireNonNull(network);
this.params = NetworkParameters.of(network);
this.coinSelector = DefaultCoinSelector.get(network);
this.keyChainGroup = Objects.requireNonNull(keyChainGroup);
watchedScripts = new HashSet<>();
unspent = new HashMap<>();
@ -527,6 +529,14 @@ public class Wallet extends BaseTaggableObject
createTransientState();
}
/**
* @deprecated use {@link Wallet(NetworkParameters, KeyChainGroup)}
*/
@Deprecated
public Wallet(NetworkParameters params, KeyChainGroup keyChainGroup) {
this(params.network(), keyChainGroup);
}
private void createTransientState() {
ignoreNextNewBlock = new HashSet<>();
txConfidenceListener = (confidence, reason) -> {
@ -552,7 +562,7 @@ public class Wallet extends BaseTaggableObject
}
public Network network() {
return params.network();
return network;
}
public NetworkParameters getNetworkParameters() {
@ -567,7 +577,7 @@ public class Wallet extends BaseTaggableObject
*/
@Override
public Address parseAddress(String addressString) throws AddressFormatException {
return addressParser.parseAddress(addressString, (BitcoinNetwork) params.network());
return addressParser.parseAddress(addressString, network);
}
/**
@ -1195,7 +1205,7 @@ public class Wallet extends BaseTaggableObject
List<Address> addresses = new LinkedList<>();
for (Script script : watchedScripts)
if (ScriptPattern.isP2PKH(script))
addresses.add(script.getToAddress(params.network()));
addresses.add(script.getToAddress(network));
return addresses;
} finally {
keyChainGroupLock.unlock();
@ -2001,7 +2011,7 @@ public class Wallet extends BaseTaggableObject
// spend against one of our other pending transactions.
lock.lock();
try {
Transaction.verify(params.network(), tx);
Transaction.verify(network, tx);
// Ignore it if we already know about this transaction. Receiving a pending transaction never moves it
// between pools.
EnumSet<Pool> containingPools = getContainingPools(tx);
@ -2780,7 +2790,7 @@ public class Wallet extends BaseTaggableObject
* @throws VerificationException If transaction fails to verify
*/
public boolean maybeCommitTx(Transaction tx) throws VerificationException {
Transaction.verify(params.network(), tx);
Transaction.verify(network, tx);
lock.lock();
try {
if (pending.containsKey(tx.getTxId()))
@ -3871,7 +3881,7 @@ public class Wallet extends BaseTaggableObject
try {
Objects.requireNonNull(selector);
List<TransactionOutput> candidates = calculateAllSpendCandidates(true, false);
CoinSelection selection = selector.select((Coin) params.network().maxMoney(), candidates);
CoinSelection selection = selector.select((Coin) network.maxMoney(), candidates);
return selection.totalValue();
} finally {
lock.unlock();
@ -4468,7 +4478,7 @@ public class Wallet extends BaseTaggableObject
checkState(req.tx.getOutputs().size() == 1, () ->
"empty wallet TX must have a single output only");
CoinSelector selector = req.coinSelector == null ? coinSelector : req.coinSelector;
bestCoinSelection = selector.select((Coin) params.network().maxMoney(), candidates);
bestCoinSelection = selector.select((Coin) network.maxMoney(), candidates);
candidates = null; // Selector took ownership and might have changed candidates. Don't access again.
req.tx.getOutput(0).setValue(bestCoinSelection.totalValue());
log.info(" emptying {}", bestCoinSelection.totalValue().toFriendlyString());
@ -4756,7 +4766,7 @@ public class Wallet extends BaseTaggableObject
public void setUTXOProvider(@Nullable UTXOProvider provider) {
lock.lock();
try {
checkArgument(provider == null || provider.network() == params.network());
checkArgument(provider == null || provider.network() == network);
this.vUTXOProvider = provider;
} finally {
lock.unlock();

View File

@ -22,6 +22,7 @@ import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import com.google.protobuf.WireFormat;
import org.bitcoinj.base.Coin;
import org.bitcoinj.base.Network;
import org.bitcoinj.core.LockTime;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.PeerAddress;
@ -100,7 +101,11 @@ public class WalletProtobufSerializer {
@FunctionalInterface
public interface WalletFactory {
Wallet create(NetworkParameters params, KeyChainGroup keyChainGroup);
Wallet create(Network network, KeyChainGroup keyChainGroup);
@Deprecated
default Wallet create(NetworkParameters params, KeyChainGroup keyChainGroup) {
return create(params.network(), keyChainGroup);
}
WalletFactory DEFAULT = Wallet::new;
}
@ -498,7 +503,7 @@ public class WalletProtobufSerializer {
} else {
keyChainGroup = KeyChainGroup.fromProtobufUnencrypted(params, walletProto.getKeyList(), keyChainFactory);
}
Wallet wallet = factory.create(params, keyChainGroup);
Wallet wallet = factory.create(params.network(), keyChainGroup);
List<Script> scripts = new ArrayList<>();
for (Protos.Script protoScript : walletProto.getWatchedScriptList()) {

View File

@ -88,7 +88,7 @@ public class BloomFilterTest {
KeyChainGroup group = KeyChainGroup.builder(MAINNET).build();
// Add a random key which happens to have been used in a recent generation
group.importKeys(ECKey.fromPublicOnly(privKey.getKey()), ECKey.fromPublicOnly(ByteUtils.parseHex("03cb219f69f1b49468bd563239a86667e74a06fcba69ac50a08a5cbc42a5808e99")));
Wallet wallet = new Wallet(MAINNET, group);
Wallet wallet = new Wallet(BitcoinNetwork.MAINNET, group);
wallet.commitTx(new Transaction(ByteBuffer.wrap(ByteUtils.parseHex("01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0d038754030114062f503253482fffffffff01c05e559500000000232103cb219f69f1b49468bd563239a86667e74a06fcba69ac50a08a5cbc42a5808e99ac00000000"))));
// We should have 2 per pubkey, and one for the P2PK output we have

View File

@ -109,7 +109,7 @@ public class WalletProtobufSerializerTest {
myKey = new ECKey();
myKey.setCreationTime(Instant.ofEpochSecond(123456789L));
myAddress = myKey.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
myWallet = new Wallet(TESTNET, KeyChainGroup.builder(TESTNET).fromRandom(ScriptType.P2PKH).build());
myWallet = new Wallet(BitcoinNetwork.TESTNET, KeyChainGroup.builder(TESTNET).fromRandom(ScriptType.P2PKH).build());
myWallet.importKey(myKey);
mScriptCreationTime = TimeUtils.currentTime().minusSeconds(1234);
myWallet.addWatchedAddress(myWatchedKey.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET), mScriptCreationTime);

View File

@ -224,7 +224,7 @@ public class WalletTest extends TestWithWallet {
.outputScriptType(ScriptType.P2WPKH)
.accountPath(DeterministicKeyChain.BIP44_ACCOUNT_ZERO_PATH).build())
.build();
Wallet encryptedWallet = new Wallet(TESTNET, keyChainGroup);
Wallet encryptedWallet = new Wallet(BitcoinNetwork.TESTNET, keyChainGroup);
encryptedWallet = roundTrip(encryptedWallet);
encryptedWallet.encrypt(PASSWORD1);
encryptedWallet = roundTrip(encryptedWallet);
@ -3219,7 +3219,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void keyEvents() {
// Check that we can register an event listener, generate some keys and the callbacks are invoked properly.
wallet = new Wallet(TESTNET, KeyChainGroup.builder(TESTNET).fromRandom(ScriptType.P2PKH).build());
wallet = new Wallet(BitcoinNetwork.TESTNET, KeyChainGroup.builder(TESTNET).fromRandom(ScriptType.P2PKH).build());
final List<ECKey> keys = new LinkedList<>();
wallet.addKeyChainEventListener(Threading.SAME_THREAD, keys::addAll);
wallet.freshReceiveKey();
@ -3504,7 +3504,7 @@ public class WalletTest extends TestWithWallet {
DeterministicKeyChain p2wpkhChain = DeterministicKeyChain.builder().random(new SecureRandom())
.outputScriptType(ScriptType.P2WPKH).build();
KeyChainGroup kcg = KeyChainGroup.builder(TESTNET).addChain(p2pkhChain).addChain(p2wpkhChain).build();
Wallet wallet = new Wallet(TESTNET, kcg);
Wallet wallet = new Wallet(BitcoinNetwork.TESTNET, kcg);
// Set up one key from each chain.
ECKey importedKey = new ECKey();

View File

@ -113,7 +113,7 @@ public class TestWithNetworkConnections {
// Reduce the number of keys we need to work with to speed up these tests.
KeyChainGroup kcg = KeyChainGroup.builder(UNITTEST).lookaheadSize(4).lookaheadThreshold(2)
.fromRandom(ScriptType.P2PKH).build();
wallet = new Wallet(UNITTEST, kcg);
wallet = new Wallet(UNITTEST.network(), kcg);
address = wallet.freshReceiveAddress(ScriptType.P2PKH);
}
blockChain = new BlockChain(UNITTEST, wallet, blockStore);