Wallet: use Network in static factory methods

* Adds new methods taking `Network` rather than `NetworkParameters`
* Deprecates all converted methods
* Updates tests, examples, and tools that use these calls
This commit is contained in:
Sean Gilligan 2023-04-17 13:51:23 -07:00 committed by Andreas Schildbach
parent 3f3f9bb636
commit 9291841f8d
21 changed files with 278 additions and 119 deletions

View file

@ -317,23 +317,39 @@ 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 wallet will operate on
* @param outputScriptType type of addresses (aka output scripts) to generate for receiving
* @return A new empty wallet
*/
public static Wallet createDeterministic(Network network, ScriptType outputScriptType) {
return createDeterministic(network, outputScriptType, KeyChainGroupStructure.BIP32);
}
/**
* @deprecated use {@link #createDeterministic(Network, ScriptType)}
*/
@Deprecated
public static Wallet createDeterministic(NetworkParameters params, ScriptType outputScriptType) {
return createDeterministic(params, outputScriptType, KeyChainGroupStructure.BIP32);
return createDeterministic(params.network(), outputScriptType);
}
/**
* 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 wallet will operate on
* @param outputScriptType type of addresses (aka output scripts) to generate for receiving
* @param keyChainGroupStructure structure (e.g. BIP32 or BIP43)
* @return A new empty wallet
*/
public static Wallet createDeterministic(Network network, ScriptType outputScriptType, KeyChainGroupStructure keyChainGroupStructure) {
return new Wallet(network, KeyChainGroup.builder(NetworkParameters.of(network), keyChainGroupStructure).fromRandom(outputScriptType).build());
}
/**
* @deprecated use {@link Wallet#createDeterministic(Network, ScriptType, KeyChainGroupStructure)}
*/
@Deprecated
public static Wallet createDeterministic(NetworkParameters params, ScriptType outputScriptType, KeyChainGroupStructure keyChainGroupStructure) {
return new Wallet(params.network(), KeyChainGroup.builder(params, keyChainGroupStructure).fromRandom(outputScriptType).build());
}
@ -341,145 +357,234 @@ public class Wallet extends BaseTaggableObject
/**
* Creates a new, empty wallet with just a basic keychain and no transactions. No deterministic chains will be created
* automatically. This is meant for when you just want to import a few keys and operate on them.
* @param params network parameters
* @param network network wallet will operate on
*/
public static Wallet createBasic(NetworkParameters params) {
return new Wallet(params.network(), KeyChainGroup.createBasic(params));
public static Wallet createBasic(Network network) {
return new Wallet(network, KeyChainGroup.createBasic(NetworkParameters.of(network)));
}
/**
* @param params network parameters
* @deprecated use {@link #createBasic(Network)}
*/
@Deprecated
public static Wallet createBasic(NetworkParameters params) {
return createBasic(params.network());
}
/**
* @param network network wallet will operate on
* @param seed deterministic seed
* @param outputScriptType type of addresses (aka output scripts) to generate for receiving
* @return a wallet from a deterministic seed with a default account path
*/
public static Wallet fromSeed(NetworkParameters params, DeterministicSeed seed,
public static Wallet fromSeed(Network network, DeterministicSeed seed,
ScriptType outputScriptType) {
return fromSeed(params, seed, outputScriptType, KeyChainGroupStructure.BIP32);
return fromSeed(network, seed, outputScriptType, KeyChainGroupStructure.BIP32);
}
/**
* @param params network parameters
* @deprecated use {@link #fromSeed(Network, DeterministicSeed, ScriptType)}
*/
@Deprecated
public static Wallet fromSeed(NetworkParameters params, DeterministicSeed seed,
ScriptType outputScriptType) {
return fromSeed(params.network(), seed, outputScriptType);
}
/**
* @param network network wallet will operate on
* @param seed deterministic seed
* @param outputScriptType type of addresses (aka output scripts) to generate for receiving
* @param structure structure for your wallet
* @return a wallet from a deterministic seed with a default account path
*/
public static Wallet fromSeed(NetworkParameters params, DeterministicSeed seed, ScriptType outputScriptType,
public static Wallet fromSeed(Network network, DeterministicSeed seed, ScriptType outputScriptType,
KeyChainGroupStructure structure) {
return new Wallet(params.network(), KeyChainGroup.builder(params, structure).fromSeed(seed, outputScriptType).build());
return new Wallet(network, KeyChainGroup.builder(NetworkParameters.of(network), structure).fromSeed(seed, outputScriptType).build());
}
/**
* @param params network parameters
* @deprecated use {@link #fromSeed(Network, DeterministicSeed, ScriptType, KeyChainGroupStructure)}
*/
@Deprecated
public static Wallet fromSeed(NetworkParameters params, DeterministicSeed seed, ScriptType outputScriptType,
KeyChainGroupStructure structure) {
return fromSeed(params.network(), seed, outputScriptType, structure);
}
/**
* @param network network wallet will operate on
* @param seed deterministic seed
* @param outputScriptType type of addresses (aka output scripts) to generate for receiving
* @param accountPath account path to generate receiving addresses on
* @return an instance of a wallet from a deterministic seed.
*/
public static Wallet fromSeed(NetworkParameters params, DeterministicSeed seed, ScriptType outputScriptType,
public static Wallet fromSeed(Network network, DeterministicSeed seed, ScriptType outputScriptType,
List<ChildNumber> accountPath) {
DeterministicKeyChain chain = DeterministicKeyChain.builder().seed(seed).outputScriptType(outputScriptType)
.accountPath(accountPath).build();
return new Wallet(params.network(), KeyChainGroup.builder(params).addChain(chain).build());
return new Wallet(network, KeyChainGroup.builder(NetworkParameters.of(network)).addChain(chain).build());
}
/**
* @deprecated use {@link #fromSeed(Network, DeterministicSeed, ScriptType, List)}
*/
@Deprecated
public static Wallet fromSeed(NetworkParameters params, DeterministicSeed seed, ScriptType outputScriptType,
List<ChildNumber> accountPath) {
return fromSeed(params.network(), seed, outputScriptType, accountPath);
}
/**
* Creates a wallet that tracks payments to and from the HD key hierarchy rooted by the given watching key. This HAS
* to be an account key as returned by {@link DeterministicKeyChain#getWatchingKey()}.
*/
public static Wallet fromWatchingKey(NetworkParameters params, DeterministicKey watchKey,
public static Wallet fromWatchingKey(Network network, DeterministicKey watchKey,
ScriptType outputScriptType) {
DeterministicKeyChain chain = DeterministicKeyChain.builder().watch(watchKey).outputScriptType(outputScriptType)
.build();
return new Wallet(params.network(), KeyChainGroup.builder(params).addChain(chain).build());
return new Wallet(network, KeyChainGroup.builder(NetworkParameters.of(network)).addChain(chain).build());
}
/**
/**
* @deprecated use {@link #fromWatchingKey(Network, DeterministicKey, ScriptType)}
*/
@Deprecated
public static Wallet fromWatchingKey(NetworkParameters params, DeterministicKey watchKey,
ScriptType outputScriptType) {
return fromWatchingKey(params.network(), watchKey, outputScriptType);
}
/**
* Creates a wallet that tracks payments to and from the HD key hierarchy rooted by the given watching key. The
* account path is specified.
* @param params The network
* @param network network wallet will operate on
* @param watchKeyB58 The key in base58 notation
* @param creationTime creation time of the key (if unknown use {@link #fromWatchingKeyB58(NetworkParameters, String)}
* @param creationTime creation time of the key (if unknown use {@link #fromWatchingKeyB58(Network, String)}
* @return a new wallet
*/
public static Wallet fromWatchingKeyB58(NetworkParameters params, String watchKeyB58, Instant creationTime) {
final DeterministicKey watchKey = DeterministicKey.deserializeB58(null, watchKeyB58, params.network());
public static Wallet fromWatchingKeyB58(Network network, String watchKeyB58, Instant creationTime) {
final DeterministicKey watchKey = DeterministicKey.deserializeB58(null, watchKeyB58, network);
watchKey.setCreationTime(creationTime);
return fromWatchingKey(params, watchKey, outputScriptTypeFromB58(params, watchKeyB58));
return fromWatchingKey(network, watchKey, outputScriptTypeFromB58(NetworkParameters.of(network), watchKeyB58));
}
/**
* @deprecated use {@link #fromWatchingKeyB58(Network, String, Instant)}
*/
@Deprecated
public static Wallet fromWatchingKeyB58(NetworkParameters params, String watchKeyB58, Instant creationTime) {
return fromWatchingKeyB58(params.network(), watchKeyB58, creationTime);
}
/**
* Creates a wallet that tracks payments to and from the HD key hierarchy rooted by the given watching key. The
* account path is specified. The key's creation time will be set to {@link DeterministicHierarchy#BIP32_STANDARDISATION_TIME}
* @param params The network
* @param network network wallet will operate on
* @param watchKeyB58 The key in base58 notation
* @return a new wallet
*/
public static Wallet fromWatchingKeyB58(NetworkParameters params, String watchKeyB58) {
return fromWatchingKeyB58(params, watchKeyB58, DeterministicHierarchy.BIP32_STANDARDISATION_TIME);
public static Wallet fromWatchingKeyB58(Network network, String watchKeyB58) {
return fromWatchingKeyB58(network, watchKeyB58, DeterministicHierarchy.BIP32_STANDARDISATION_TIME);
}
/**
* @deprecated Use {@link #fromWatchingKeyB58(NetworkParameters, String, Instant)} or {@link #fromWatchingKeyB58(NetworkParameters, String)}
* @deprecated use {@link #fromWatchingKeyB58(Network, String)}
*/
@Deprecated
public static Wallet fromWatchingKeyB58(NetworkParameters params, String watchKeyB58) {
return fromWatchingKeyB58(params.network(), watchKeyB58);
}
/**
* @deprecated Use {@link #fromWatchingKeyB58(Network, String, Instant)} or {@link #fromWatchingKeyB58(Network, String)}
*/
@Deprecated
public static Wallet fromWatchingKeyB58(NetworkParameters params, String watchKeyB58, long creationTimeSeconds) {
return (creationTimeSeconds == 0)
? fromWatchingKeyB58(params, watchKeyB58)
: fromWatchingKeyB58(params, watchKeyB58, Instant.ofEpochSecond(creationTimeSeconds));
? fromWatchingKeyB58(params.network(), watchKeyB58)
: fromWatchingKeyB58(params.network(), watchKeyB58, Instant.ofEpochSecond(creationTimeSeconds));
}
/**
* Creates a wallet that tracks payments to and from the HD key hierarchy rooted by the given spending key. This HAS
* to be an account key as returned by {@link DeterministicKeyChain#getWatchingKey()}. This wallet can also spend.
* @param network network wallet will operate on
*/
public static Wallet fromSpendingKey(NetworkParameters params, DeterministicKey spendKey,
public static Wallet fromSpendingKey(Network network, DeterministicKey spendKey,
ScriptType outputScriptType) {
DeterministicKeyChain chain = DeterministicKeyChain.builder().spend(spendKey).outputScriptType(outputScriptType)
.build();
return new Wallet(network, KeyChainGroup.builder(NetworkParameters.of(network)).addChain(chain).build());
}
/**
* @deprecated use {@link #fromSpendingKey(Network, DeterministicKey, ScriptType)}
*/
@Deprecated
public static Wallet fromSpendingKey(NetworkParameters params, DeterministicKey spendKey,
ScriptType outputScriptType) {
DeterministicKeyChain chain = DeterministicKeyChain.builder().spend(spendKey).outputScriptType(outputScriptType)
.build();
return new Wallet(params.network(), KeyChainGroup.builder(params).addChain(chain).build());
}
/**
* Creates a wallet that tracks payments to and from the HD key hierarchy rooted by the given spending key.
* @param params The network
* @param network network wallet will operate on
* @param spendingKeyB58 The key in base58 notation
* @param creationTime creation time of the key (if unknown use {@link #fromWatchingKeyB58(NetworkParameters, String)}
* @param creationTime creation time of the key (if unknown use {@link #fromWatchingKeyB58(Network, String)}
* @return a new wallet
*/
public static Wallet fromSpendingKeyB58(NetworkParameters params, String spendingKeyB58, Instant creationTime) {
final DeterministicKey spendKey = DeterministicKey.deserializeB58(null, spendingKeyB58, params.network());
public static Wallet fromSpendingKeyB58(Network network, String spendingKeyB58, Instant creationTime) {
final DeterministicKey spendKey = DeterministicKey.deserializeB58(null, spendingKeyB58, network);
spendKey.setCreationTime(creationTime);
return fromSpendingKey(params, spendKey, outputScriptTypeFromB58(params, spendingKeyB58));
return fromSpendingKey(network, spendKey, outputScriptTypeFromB58(NetworkParameters.of(network), spendingKeyB58));
}
/**
* @deprecated use {@link #fromWatchingKeyB58(Network, String, Instant)}
*/
@Deprecated
public static Wallet fromSpendingKeyB58(NetworkParameters params, String spendingKeyB58, Instant creationTime) {
return fromWatchingKeyB58(params.network(), spendingKeyB58, creationTime);
}
/**
* Creates a wallet that tracks payments to and from the HD key hierarchy rooted by the given spending key.
* The key's creation time will be set to {@link DeterministicHierarchy#BIP32_STANDARDISATION_TIME}.
* @param params The network
* @param network network wallet will operate on
* @param spendingKeyB58 The key in base58 notation
* @return a new wallet
*/
public static Wallet fromSpendingKeyB58(NetworkParameters params, String spendingKeyB58) {
return fromSpendingKeyB58(params, spendingKeyB58, DeterministicHierarchy.BIP32_STANDARDISATION_TIME);
public static Wallet fromSpendingKeyB58(Network network, String spendingKeyB58) {
return fromSpendingKeyB58(network, spendingKeyB58, DeterministicHierarchy.BIP32_STANDARDISATION_TIME);
}
/**
* @deprecated Use {@link #fromSpendingKeyB58(NetworkParameters, String, Instant)} or {@link #fromSpendingKeyB58(NetworkParameters, String)}
* @deprecated use {@link #fromSpendingKeyB58(Network, String)}
*/
@Deprecated
public static Wallet fromSpendingKeyB58(NetworkParameters params, String spendingKeyB58) {
return fromSpendingKeyB58(params.network(), spendingKeyB58);
}
/**
* @deprecated Use {@link #fromSpendingKeyB58(Network, String, Instant)} or {@link #fromSpendingKeyB58(Network, String)}
*/
@Deprecated
public static Wallet fromSpendingKeyB58(NetworkParameters params, String spendingKeyB58, long creationTimeSeconds) {
return (creationTimeSeconds == 0)
? fromSpendingKeyB58(params, spendingKeyB58)
: fromSpendingKeyB58(params, spendingKeyB58, Instant.ofEpochSecond(creationTimeSeconds));
? fromSpendingKeyB58(params.network(), spendingKeyB58)
: fromSpendingKeyB58(params.network(), spendingKeyB58, Instant.ofEpochSecond(creationTimeSeconds));
}
/**
* Creates a wallet that tracks payments to and from the HD key hierarchy rooted by the given spending key. This HAS
* to be an account key as returned by {@link DeterministicKeyChain#getWatchingKey()}.
* @param network network wallet will operate on
*/
public static Wallet fromMasterKey(NetworkParameters params, DeterministicKey masterKey,
public static Wallet fromMasterKey(Network network, DeterministicKey masterKey,
ScriptType outputScriptType, ChildNumber accountNumber) {
DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(masterKey, accountNumber);
accountKey = accountKey.dropParent();
@ -490,7 +595,16 @@ public class Wallet extends BaseTaggableObject
accountKey.clearCreationTime();
DeterministicKeyChain chain = DeterministicKeyChain.builder().spend(accountKey)
.outputScriptType(outputScriptType).build();
return new Wallet(params.network(), KeyChainGroup.builder(params).addChain(chain).build());
return new Wallet(network, KeyChainGroup.builder(NetworkParameters.of(network)).addChain(chain).build());
}
/**
* @deprecated use {@link #fromMasterKey(Network, DeterministicKey, ScriptType, ChildNumber)}
*/
@Deprecated
public static Wallet fromMasterKey(NetworkParameters params, DeterministicKey masterKey,
ScriptType outputScriptType, ChildNumber accountNumber) {
return fromMasterKey(params.network(), masterKey, outputScriptType, accountNumber);
}
private static ScriptType outputScriptTypeFromB58(NetworkParameters params, String base58) {
@ -507,7 +621,7 @@ 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 network network to operate on
* @param network network wallet will operate on
* @param keyChainGroup keychain group to manage keychains
*/
public Wallet(Network network, KeyChainGroup keyChainGroup) {

View file

@ -328,7 +328,7 @@ public abstract class AbstractFullPrunedBlockChainTest {
rollingBlock = rollingBlock.createNextBlock(null);
// Create 1 BTC spend to a key in this wallet (to ourselves).
Wallet wallet = Wallet.createDeterministic(PARAMS, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(PARAMS.network(), ScriptType.P2PKH);
assertEquals("Available balance is incorrect", Coin.ZERO, wallet.getBalance(Wallet.BalanceType.AVAILABLE));
assertEquals("Estimated balance is incorrect", Coin.ZERO, wallet.getBalance(Wallet.BalanceType.ESTIMATED));

View file

@ -77,7 +77,7 @@ public class BlockChainTest {
BriefLogFormatter.initVerbose();
TimeUtils.setMockClock(); // Use mock clock
Context.propagate(new Context(100, Coin.ZERO, false, false));
testNetWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
testNetWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
testNetStore = new MemoryBlockStore(TESTNET.getGenesisBlock());
testNetChain = new BlockChain(TESTNET, testNetWallet, testNetStore);
coinbaseTo = testNetWallet.currentReceiveKey().toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
@ -332,7 +332,7 @@ public class BlockChainTest {
// Check that a coinbase transaction is only available to spend after NetworkParameters.getSpendableCoinbaseDepth() blocks.
// Create a second wallet to receive the coinbase spend.
Wallet wallet2 = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet wallet2 = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
ECKey receiveKey = wallet2.freshReceiveKey();
int height = 1;
testNetChain.addWallet(wallet2);

View file

@ -208,7 +208,7 @@ public class BlockTest {
// Create a wallet contain the miner's key that receives a spend from a coinbase.
ECKey miningKey = DumpedPrivateKey.fromBase58(BitcoinNetwork.MAINNET, MINING_PRIVATE_KEY).getKey();
assertNotNull(miningKey);
Wallet wallet = Wallet.createDeterministic(MAINNET, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.MAINNET, ScriptType.P2PKH);
wallet.importKey(miningKey);
// Initial balance should be zero by construction.

View file

@ -72,7 +72,7 @@ public class ChainSplitTest {
TimeUtils.setMockClock(); // Use mock clock
Context.propagate(new Context(100, Coin.ZERO, false, true));
MemoryBlockStore blockStore = new MemoryBlockStore(TESTNET.getGenesisBlock());
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
ECKey key1 = wallet.freshReceiveKey();
ECKey key2 = wallet.freshReceiveKey();
chain = new BlockChain(TESTNET, wallet, blockStore);

View file

@ -72,7 +72,7 @@ public class ParseByteCacheTest {
public void setUp() throws Exception {
TimeUtils.setMockClock(); // Use mock clock
Context.propagate(new Context(100, Transaction.DEFAULT_TX_FEE, false, true));
Wallet wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
wallet.freshReceiveKey();
resetBlockStore();

View file

@ -58,7 +58,7 @@ public class TransactionInputTest {
@Test
public void testStandardWalletDisconnect() throws Exception {
Wallet w = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet w = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
Address a = w.currentReceiveAddress();
Transaction tx1 = FakeTxBuilder.createFakeTxWithoutChangeAddress(Coin.COIN, a);
w.receivePending(tx1, null);
@ -81,7 +81,7 @@ public class TransactionInputTest {
@Test
public void testUTXOWalletDisconnect() throws Exception {
Wallet w = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet w = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
Address a = w.currentReceiveAddress();
final UTXO utxo = new UTXO(Sha256Hash.of(new byte[] { 1, 2, 3 }), 1, Coin.COIN, 0, false,
ScriptBuilder.createOutputScript(a));

View file

@ -192,7 +192,7 @@ public class TransactionTest {
@Test
public void testIsMatureReturnsFalseIfTransactionIsCoinbaseAndConfidenceTypeIsNotEqualToBuilding() {
Wallet wallet = Wallet.createBasic(TESTNET);
Wallet wallet = Wallet.createBasic(BitcoinNetwork.TESTNET);
Transaction tx = FakeTxBuilder.createFakeCoinbaseTx();
tx.getConfidence().setConfidenceType(ConfidenceType.UNKNOWN);

View file

@ -205,7 +205,7 @@ public class WalletProtobufSerializerTest {
for (int i = 0 ; i < 20 ; i++) {
myKey = new ECKey();
myAddress = myKey.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
myWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
myWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
myWallet.importKey(myKey);
Wallet wallet1 = roundTrip(myWallet);
ECKey foundKey = wallet1.findKeyFromPubKeyHash(myKey.getPubKeyHash(), null);
@ -219,7 +219,7 @@ public class WalletProtobufSerializerTest {
// Test the lastBlockSeenHash field works.
// LastBlockSeenHash should be empty if never set.
Wallet wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(wallet);
ByteString lastSeenBlockHash = walletProto.getLastSeenBlockHash();
assertTrue(lastSeenBlockHash.isEmpty());
@ -245,7 +245,7 @@ public class WalletProtobufSerializerTest {
@Test
public void testSequenceNumber() throws Exception {
Wallet wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
Transaction tx1 = createFakeTx(TESTNET.network(), Coin.COIN, wallet.currentReceiveAddress());
tx1.getInput(0).setSequenceNumber(TransactionInput.NO_SEQUENCE);
wallet.receivePending(tx1, null);
@ -350,7 +350,7 @@ public class WalletProtobufSerializerTest {
public void testRoundTripWatchingWallet() throws Exception {
final String xpub = "tpubD9LrDvFDrB6wYNhbR2XcRRaT4yCa37TjBR3YthBQvrtEwEq6CKeEXUs3TppQd38rfxmxD1qLkC99iP3vKcKwLESSSYdFAftbrpuhSnsw6XM";
final Instant creationTime = Instant.ofEpochSecond(1457019819);
Wallet wallet = Wallet.fromWatchingKeyB58(TESTNET, xpub, creationTime);
Wallet wallet = Wallet.fromWatchingKeyB58(BitcoinNetwork.TESTNET, xpub, creationTime);
Wallet wallet2 = roundTrip(wallet);
Wallet wallet3 = roundTrip(wallet2);
assertEquals(xpub, wallet.getWatchingKey().serializePubB58(TESTNET.network()));
@ -365,7 +365,7 @@ public class WalletProtobufSerializerTest {
@Test
public void testRoundTripMarriedWallet() throws Exception {
// create 2-of-2 married wallet
myWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
myWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
final DeterministicKeyChain partnerChain = DeterministicKeyChain.builder().random(new SecureRandom()).build();
DeterministicKey partnerKey = DeterministicKey.deserializeB58(null, partnerChain.getWatchingKey().serializePubB58(TESTNET.network()), TESTNET.network());
MarriedKeyChain chain = MarriedKeyChain.builder()
@ -437,7 +437,7 @@ public class WalletProtobufSerializerTest {
assertTrue(wallet.getExtensions().containsKey("com.whatever.required"));
// Non-mandatory extensions are ignored if the wallet doesn't know how to read them.
Wallet wallet2 = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet wallet2 = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
wallet2.addExtension(new FooWalletExtension("com.whatever.optional", false));
Protos.Wallet proto2 = new WalletProtobufSerializer().walletToProto(wallet2);
Wallet wallet5 = new WalletProtobufSerializer().readWallet(TESTNET, null, proto2);

View file

@ -16,6 +16,7 @@
package org.bitcoinj.testing;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.ScriptType;
import org.bitcoinj.base.internal.TimeUtils;
import org.bitcoinj.core.AbstractBlockChain;
@ -68,7 +69,7 @@ public class TestWithWallet {
public void setUp() throws Exception {
BriefLogFormatter.init();
Context.propagate(new Context(100, Coin.ZERO, false, true));
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH, KeyChainGroupStructure.BIP32);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH, KeyChainGroupStructure.BIP32);
myKey = wallet.freshReceiveKey();
myAddress = wallet.freshReceiveAddress(ScriptType.P2PKH);
blockStore = new MemoryBlockStore(TESTNET.getGenesisBlock());

View file

@ -17,6 +17,7 @@
package org.bitcoinj.wallet;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.Coin;
import org.bitcoinj.base.ScriptType;
import org.bitcoinj.base.internal.ByteUtils;
@ -62,7 +63,7 @@ public class DefaultRiskAnalysisTest {
@Before
public void setup() {
Context.propagate(new Context());
wallet = Wallet.createDeterministic(MAINNET, ScriptType.P2PKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.MAINNET, ScriptType.P2PKH);
wallet.setLastBlockSeenHeight(1000);
wallet.setLastBlockSeenTime(Instant.ofEpochSecond(TIMESTAMP));
}

View file

@ -48,6 +48,7 @@ import org.bitcoinj.crypto.HDPath;
import org.bitcoinj.crypto.KeyCrypter;
import org.bitcoinj.crypto.KeyCrypterException;
import org.bitcoinj.crypto.KeyCrypterScrypt;
import org.bitcoinj.crypto.MnemonicCode;
import org.bitcoinj.crypto.MnemonicException;
import org.bitcoinj.crypto.TransactionSignature;
import org.bitcoinj.crypto.internal.CryptoUtils;
@ -152,7 +153,7 @@ public class WalletTest extends TestWithWallet {
}
private void createMarriedWallet(int threshold, int numKeys, boolean addSigners) throws BlockStoreException {
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
blockStore = new MemoryBlockStore(TESTNET.getGenesisBlock());
chain = new BlockChain(TESTNET, wallet, blockStore);
@ -174,7 +175,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void createBasic() {
Wallet wallet = Wallet.createBasic(TESTNET);
Wallet wallet = Wallet.createBasic(BitcoinNetwork.TESTNET);
assertEquals(0, wallet.getKeyChainGroupSize());
wallet.importKey(new ECKey());
assertEquals(1, wallet.getKeyChainGroupSize());
@ -182,7 +183,7 @@ public class WalletTest extends TestWithWallet {
@Test(expected = IllegalStateException.class)
public void createBasic_noDerivation() {
Wallet wallet = Wallet.createBasic(TESTNET);
Wallet wallet = Wallet.createBasic(BitcoinNetwork.TESTNET);
wallet.currentReceiveAddress();
}
@ -210,7 +211,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void basicSpendingWithEncryptedWallet() throws Exception {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
Address myEncryptedAddress = encryptedWallet.freshReceiveKey().toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET);
basicSpendingCommon(encryptedWallet, myEncryptedAddress, OTHER_ADDRESS, encryptedWallet);
@ -747,7 +748,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void isTxConsistentReturnsFalseAsExpected() {
Wallet wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
TransactionOutput to = createMock(TransactionOutput.class);
EasyMock.expect(to.isAvailableForSpending()).andReturn(true);
EasyMock.expect(to.isMineOrWatched(wallet)).andReturn(true);
@ -764,7 +765,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void isTxConsistentReturnsFalseAsExpected_WhenAvailableForSpendingEqualsFalse() {
Wallet wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
TransactionOutput to = createMock(TransactionOutput.class);
EasyMock.expect(to.isAvailableForSpending()).andReturn(false);
EasyMock.expect(to.getSpentBy()).andReturn(null);
@ -1482,7 +1483,7 @@ public class WalletTest extends TestWithWallet {
public void keyCreationTime() {
Instant now = TimeUtils.currentTime().truncatedTo(ChronoUnit.SECONDS);
TimeUtils.setMockClock(now);
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
assertEquals(now, wallet.earliestKeyCreationTime());
TimeUtils.rollMockClock(Duration.ofMinutes(1));
wallet.freshReceiveKey();
@ -1493,7 +1494,7 @@ public class WalletTest extends TestWithWallet {
public void scriptCreationTime() {
Instant now = TimeUtils.currentTime().truncatedTo(ChronoUnit.SECONDS);
TimeUtils.setMockClock(now);
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
assertEquals(now, wallet.earliestKeyCreationTime());
TimeUtils.rollMockClock(Duration.ofMinutes(-2));
wallet.addWatchedAddress(OTHER_ADDRESS);
@ -1578,7 +1579,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void isWatching() {
assertFalse(wallet.isWatching());
Wallet watchingWallet = Wallet.fromWatchingKey(TESTNET,
Wallet watchingWallet = Wallet.fromWatchingKey(TESTNET.network(),
wallet.getWatchingKey().dropPrivateBytes().dropParent(), ScriptType.P2PKH);
assertTrue(watchingWallet.isWatching());
wallet.encrypt(PASSWORD1);
@ -1591,7 +1592,7 @@ public class WalletTest extends TestWithWallet {
String serialized = watchKey.serializePubB58(TESTNET.network());
// Construct watching wallet.
Wallet watchingWallet = Wallet.fromWatchingKey(TESTNET,
Wallet watchingWallet = Wallet.fromWatchingKey(TESTNET.network(),
DeterministicKey.deserializeB58(null, serialized, TESTNET.network()), ScriptType.P2PKH);
DeterministicKey key2 = watchingWallet.freshReceiveKey();
assertEquals(myKey, key2);
@ -1617,7 +1618,7 @@ public class WalletTest extends TestWithWallet {
public void watchingWalletWithCreationTime() {
DeterministicKey watchKey = wallet.getWatchingKey();
String serialized = watchKey.serializePubB58(TESTNET.network());
Wallet watchingWallet = Wallet.fromWatchingKeyB58(TESTNET, serialized, Instant.ofEpochSecond(1415282801));
Wallet watchingWallet = Wallet.fromWatchingKeyB58(TESTNET.network(), serialized, Instant.ofEpochSecond(1415282801));
DeterministicKey key2 = watchingWallet.freshReceiveKey();
assertEquals(myKey, key2);
@ -1922,7 +1923,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void encryptionDecryptionAESBasic() {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
AesKey aesKey = keyCrypter.deriveKey(PASSWORD1);
@ -1945,7 +1946,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void encryptionDecryptionPasswordBasic() {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
assertTrue(encryptedWallet.isEncrypted());
@ -1963,7 +1964,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void encryptionDecryptionBadPassword() {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
AesKey wrongAesKey = keyCrypter.deriveKey(WRONG_PASSWORD);
@ -1983,7 +1984,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void changePasswordTest() {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
CharSequence newPassword = "My name is Tom";
encryptedWallet.changeEncryptionPassword(PASSWORD1, newPassword);
@ -1993,7 +1994,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void changeAesKeyTest() {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
@ -2010,7 +2011,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void encryptionDecryptionCheckExceptions() {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
AesKey aesKey = keyCrypter.deriveKey(PASSWORD1);
@ -2049,7 +2050,7 @@ public class WalletTest extends TestWithWallet {
@Test(expected = KeyCrypterException.class)
public void addUnencryptedKeyToEncryptedWallet() {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
ECKey key1 = new ECKey();
@ -2058,7 +2059,7 @@ public class WalletTest extends TestWithWallet {
@Test(expected = KeyCrypterException.class)
public void addEncryptedKeyToUnencryptedWallet() {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
@ -2069,7 +2070,7 @@ public class WalletTest extends TestWithWallet {
@Test(expected = KeyCrypterException.class)
public void mismatchedCrypter() {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
AesKey aesKey = keyCrypter.deriveKey(PASSWORD1);
@ -2084,7 +2085,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void importAndEncrypt() throws InsufficientMoneyException {
Wallet encryptedWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet encryptedWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
encryptedWallet.encrypt(PASSWORD1);
final ECKey key = new ECKey();
@ -2674,7 +2675,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void witnessTransactionGetFeeTest() throws Exception {
Wallet mySegwitWallet = Wallet.createDeterministic(TESTNET, ScriptType.P2WPKH);
Wallet mySegwitWallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2WPKH);
Address mySegwitAddress = mySegwitWallet.freshReceiveAddress(ScriptType.P2WPKH);
// Prepare wallet to spend
@ -2902,7 +2903,7 @@ public class WalletTest extends TestWithWallet {
public void keyRotationRandom() throws Exception {
TimeUtils.setMockClock();
// Start with an empty wallet (no HD chain).
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
// Watch out for wallet-initiated broadcasts.
MockTransactionBroadcaster broadcaster = new MockTransactionBroadcaster(wallet);
// Send three cents to two different random keys, then add a key and mark the initial keys as compromised.
@ -3006,7 +3007,7 @@ public class WalletTest extends TestWithWallet {
public void keyRotationHD() throws Exception {
// Test that if we rotate an HD chain, a new one is created and all arrivals on the old keys are moved.
TimeUtils.setMockClock();
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
ECKey key1 = wallet.freshReceiveKey();
ECKey key2 = wallet.freshReceiveKey();
sendMoneyToWallet(wallet, AbstractBlockChain.NewBlockType.BEST_CHAIN, CENT, key1.toAddress(ScriptType.P2PKH, BitcoinNetwork.TESTNET));
@ -3080,7 +3081,7 @@ public class WalletTest extends TestWithWallet {
// Delete the sigs
for (TransactionInput input : req.tx.getInputs())
input.clearScriptBytes();
Wallet watching = Wallet.fromWatchingKey(TESTNET, wallet.getWatchingKey().dropParent().dropPrivateBytes(),
Wallet watching = Wallet.fromWatchingKey(TESTNET.network(), wallet.getWatchingKey().dropParent().dropPrivateBytes(),
ScriptType.P2PKH);
watching.freshReceiveKey();
watching.completeTx(SendRequest.forTx(req.tx));
@ -3228,7 +3229,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void upgradeToDeterministic_P2PKH_to_P2WPKH_unencrypted() {
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
assertFalse(wallet.isEncrypted());
assertFalse(wallet.isDeterministicUpgradeRequired(ScriptType.P2PKH));
assertTrue(wallet.isDeterministicUpgradeRequired(ScriptType.P2WPKH));
@ -3245,7 +3246,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void upgradeToDeterministic_P2PKH_to_P2WPKH_encrypted() {
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
assertFalse(wallet.isEncrypted());
assertFalse(wallet.isDeterministicUpgradeRequired(ScriptType.P2PKH));
assertTrue(wallet.isDeterministicUpgradeRequired(ScriptType.P2WPKH));
@ -3272,7 +3273,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void upgradeToDeterministic_noDowngrade_unencrypted() {
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2WPKH);
wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2WPKH);
assertFalse(wallet.isEncrypted());
assertFalse(wallet.isDeterministicUpgradeRequired(ScriptType.P2PKH));
assertFalse(wallet.isDeterministicUpgradeRequired(ScriptType.P2WPKH));
@ -3296,7 +3297,7 @@ public class WalletTest extends TestWithWallet {
public void watchingMarriedWallet() throws Exception {
DeterministicKey watchKey = wallet.getWatchingKey();
String serialized = watchKey.serializePubB58(TESTNET.network());
Wallet wallet = Wallet.fromWatchingKeyB58(TESTNET, serialized);
Wallet wallet = Wallet.fromWatchingKeyB58(TESTNET.network(), serialized);
blockStore = new MemoryBlockStore(TESTNET.getGenesisBlock());
chain = new BlockChain(TESTNET, wallet, blockStore);
@ -3376,7 +3377,7 @@ public class WalletTest extends TestWithWallet {
@Test
public void createBasicWithKeys() {
ECKey key = ECKey.fromPrivate(ByteUtils.parseHex("00905b93f990267f4104f316261fc10f9f983551f9ef160854f40102eb71cffdcc"));
Wallet wallet = Wallet.createBasic(TESTNET);
Wallet wallet = Wallet.createBasic(BitcoinNetwork.TESTNET);
wallet.importKey(key);
assertEquals(1, wallet.getImportedKeys().size());
assertEquals(key, wallet.getImportedKeys().get(0));
@ -3533,11 +3534,11 @@ public class WalletTest extends TestWithWallet {
@Test
public void roundtripViaMnemonicCode() {
Wallet wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2WPKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2WPKH);
List<String> mnemonicCode = wallet.getKeyChainSeed().getMnemonicCode();
final DeterministicSeed clonedSeed = DeterministicSeed.ofMnemonic(mnemonicCode, "",
wallet.earliestKeyCreationTime());
Wallet clone = Wallet.fromSeed(TESTNET, clonedSeed, ScriptType.P2WPKH);
Wallet clone = Wallet.fromSeed(BitcoinNetwork.TESTNET, clonedSeed, ScriptType.P2WPKH);
assertEquals(wallet.currentReceiveKey(), clone.currentReceiveKey());
assertEquals(wallet.freshReceiveAddress(ScriptType.P2PKH),
clone.freshReceiveAddress(ScriptType.P2PKH));
@ -3545,8 +3546,8 @@ public class WalletTest extends TestWithWallet {
@Test
public void oneTxTwoWallets() {
Wallet wallet1 = Wallet.createDeterministic(TESTNET, ScriptType.P2WPKH);
Wallet wallet2 = Wallet.createDeterministic(TESTNET, ScriptType.P2WPKH);
Wallet wallet1 = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2WPKH);
Wallet wallet2 = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2WPKH);
Address address1 = wallet1.freshReceiveAddress(ScriptType.P2PKH);
Address address2 = wallet2.freshReceiveAddress(ScriptType.P2PKH);
@ -3609,4 +3610,46 @@ public class WalletTest extends TestWithWallet {
assertEquals(txW1, txW2);
assertNotSame(txW1, txW2);
}
@Test
public void deprecatedMembers() {
Wallet wallet1 = Wallet.createBasic(TESTNET);
assertEquals(BitcoinNetwork.TESTNET, wallet1.network());
Wallet wallet2 = Wallet.createDeterministic(TESTNET, ScriptType.P2WPKH, KeyChainGroupStructure.BIP43);
assertEquals(BitcoinNetwork.TESTNET, wallet2.network());
Wallet wallet3 = Wallet.createDeterministic(TESTNET, ScriptType.P2WPKH);
assertEquals(BitcoinNetwork.TESTNET, wallet3.network());
Wallet wallet4 = Wallet.fromSeed(TESTNET, DeterministicSeed.ofEntropy(new byte[20], ""), ScriptType.P2WPKH);
assertEquals(BitcoinNetwork.TESTNET, wallet4.network());
Wallet wallet5 = Wallet.fromSeed(TESTNET, DeterministicSeed.ofEntropy(new byte[20], ""), ScriptType.P2WPKH, KeyChainGroupStructure.BIP43);
assertEquals(BitcoinNetwork.TESTNET, wallet5.network());
Wallet wallet6 = Wallet.fromSeed(TESTNET, DeterministicSeed.ofEntropy(new byte[20], ""), ScriptType.P2WPKH, HDPath.BIP44_PARENT);
assertEquals(BitcoinNetwork.TESTNET, wallet6.network());
HDPath accountPath = KeyChainGroupStructure.BIP43.accountPathFor(ScriptType.P2WPKH, BitcoinNetwork.TESTNET);
DeterministicKeyChain keyChain = DeterministicKeyChain.builder()
.random(new SecureRandom())
.accountPath(accountPath)
.build();
DeterministicKey watchingKey = keyChain.getWatchingKey().dropPrivateBytes().dropParent();
Wallet wallet7 = Wallet.fromWatchingKey(TESTNET, watchingKey, ScriptType.P2WPKH);
assertEquals(BitcoinNetwork.TESTNET, wallet7.network());
String watchingKeyb58 = watchingKey.serializePubB58(TESTNET.network());
Wallet wallet8 = Wallet.fromWatchingKeyB58(TESTNET, watchingKeyb58, Instant.ofEpochSecond(1415282801));
assertEquals(BitcoinNetwork.TESTNET, wallet8.network());
Wallet wallet9 = Wallet.fromWatchingKeyB58(TESTNET, watchingKeyb58);
assertEquals(BitcoinNetwork.TESTNET, wallet9.network());
Wallet wallet10 = Wallet.fromWatchingKeyB58(TESTNET, watchingKeyb58, 1415282801);
assertEquals(BitcoinNetwork.TESTNET, wallet10.network());
}
}

View file

@ -17,9 +17,7 @@
package org.bitcoinj.examples;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.Network;
import org.bitcoinj.base.ScriptType;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.base.internal.InternalUtils;
import org.bitcoinj.wallet.DeterministicSeed;
import org.bitcoinj.wallet.Wallet;
@ -36,8 +34,7 @@ public class BackupToMnemonicSeed {
public static void main(String[] args) {
Network network = BitcoinNetwork.TESTNET;
Wallet wallet = Wallet.createDeterministic(NetworkParameters.of(network), ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
DeterministicSeed seed = wallet.getKeyChainSeed();
System.out.println("seed: " + seed.toString());

View file

@ -63,7 +63,7 @@ public class PrivateKeys {
System.out.println("Address from private key is: " + key.toAddress(ScriptType.P2WPKH, network).toString());
// Import the private key to a fresh wallet.
Wallet wallet = Wallet.createDeterministic(params, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(network, ScriptType.P2PKH);
wallet.importKey(key);
// And the address ...

View file

@ -51,7 +51,7 @@ public class RestoreFromSeed {
DeterministicSeed seed = DeterministicSeed.ofMnemonic(seedCode, passphrase, creationtime);
// The wallet class provides a easy fromSeed() function that loads a new wallet from a given seed.
Wallet wallet = Wallet.fromSeed(params, seed, ScriptType.P2PKH);
Wallet wallet = Wallet.fromSeed(network, seed, ScriptType.P2PKH);
// Because we are importing an existing wallet which might already have transactions we must re-download the blockchain to make the wallet picks up these transactions
// You can find some information about this in the guides: https://bitcoinj.github.io/working-with-the-wallet#setup

View file

@ -77,7 +77,7 @@ public class FilteredBlockAndPartialMerkleTreeTest extends TestWithPeerGroup {
BigInteger.ONE, 100_000));
store.setChainHead(store.get(Sha256Hash.wrap("000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506")));
wallet = Wallet.createBasic(TESTNET);
wallet = Wallet.createBasic(BitcoinNetwork.TESTNET);
wallet.importKeys(Arrays.asList(ECKey.fromPublicOnly(ByteUtils.parseHex("04b27f7e9475ccf5d9a431cb86d665b8302c140144ec2397fce792f4a4e7765fecf8128534eaa71df04f93c74676ae8279195128a1506ebf7379d23dab8fca0f63")),
ECKey.fromPublicOnly(ByteUtils.parseHex("04732012cb962afa90d31b25d8fb0e32c94e513ab7a17805c14ca4c3423e18b4fb5d0e676841733cb83abaf975845c9f6f2a8097b7d04f4908b18368d6fc2d68ec")),
ECKey.fromPublicOnly(ByteUtils.parseHex("04cfb4113b3387637131ebec76871fd2760fc430dd16de0110f0eb07bb31ffac85e2607c189cb8582ea1ccaeb64ffd655409106589778f3000fdfe3263440b0350")),

View file

@ -275,7 +275,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
// Create a peer.
InboundMessageQueuer p1 = connectPeer(1);
Wallet wallet2 = Wallet.createDeterministic(UNITTEST, ScriptType.P2PKH);
Wallet wallet2 = Wallet.createDeterministic(UNITTEST.network(), ScriptType.P2PKH);
ECKey key2 = wallet2.freshReceiveKey();
Address address2 = key2.toAddress(ScriptType.P2PKH, UNITTEST.network());
@ -436,7 +436,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
final Instant now = TimeUtils.currentTime().truncatedTo(ChronoUnit.SECONDS);
peerGroup.start();
assertTrue(peerGroup.fastCatchupTime().isAfter(now.minusSeconds(WEEK).minusSeconds(10000)));
Wallet w2 = Wallet.createDeterministic(UNITTEST, ScriptType.P2PKH);
Wallet w2 = Wallet.createDeterministic(UNITTEST.network(), ScriptType.P2PKH);
ECKey key1 = new ECKey();
key1.setCreationTime(now.minus(1, ChronoUnit.DAYS)); // One day ago.
w2.importKey(key1);
@ -798,7 +798,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
final int NUM_KEYS = 9;
// First, grab a load of keys from the wallet, and then recreate it so it forgets that those keys were issued.
Wallet shadow = Wallet.fromSeed(wallet.getParams(), wallet.getKeyChainSeed(), ScriptType.P2PKH);
Wallet shadow = Wallet.fromSeed(wallet.network(), wallet.getKeyChainSeed(), ScriptType.P2PKH);
List<ECKey> keys = new ArrayList<>(NUM_KEYS);
for (int i = 0; i < NUM_KEYS; i++) {
keys.add(shadow.freshReceiveKey());

View file

@ -694,7 +694,7 @@ public class PeerTest extends TestWithNetworkConnections {
connectWithVersion(70001, Services.NODE_NETWORK);
// Test that if we receive a relevant transaction that has a lock time, it doesn't result in a notification
// until we explicitly opt in to seeing those.
Wallet wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
ECKey key = wallet.freshReceiveKey();
peer.addWallet(wallet);
final Transaction[] vtx = new Transaction[1];
@ -740,7 +740,7 @@ public class PeerTest extends TestWithNetworkConnections {
private void checkTimeLockedDependency(boolean shouldAccept) throws Exception {
// Initial setup.
connectWithVersion(70001, Services.NODE_NETWORK);
Wallet wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
Wallet wallet = Wallet.createDeterministic(BitcoinNetwork.TESTNET, ScriptType.P2PKH);
ECKey key = wallet.freshReceiveKey();
wallet.setAcceptRiskyTransactions(shouldAccept);
peer.addWallet(wallet);

View file

@ -16,6 +16,7 @@
package org.bitcoinj.wallet;
import org.bitcoinj.base.Network;
import org.bitcoinj.base.ScriptType;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.NetworkParameters;
@ -59,7 +60,7 @@ public class WalletAccountPathTest {
void walletStructurePathTest2(KeyChainGroupStructure structure, HDPath expectedPath, ScriptType scriptType,
BitcoinNetwork network) throws IOException, UnreadableWalletException {
// When we create a wallet with parameterized structure, network, and scriptType
Wallet wallet = createWallet(walletFile, NetworkParameters.of(network), structure, scriptType);
Wallet wallet = createWallet(walletFile, network, structure, scriptType);
// Then the account path is as expected
assertEquals(expectedPath, wallet.getActiveKeyChain().getAccountPath());
@ -80,10 +81,10 @@ public class WalletAccountPathTest {
}
// Create a wallet, save it to a file, then reload from a file
private static Wallet createWallet(File walletFile, NetworkParameters params, KeyChainGroupStructure structure, ScriptType outputScriptType) throws IOException, UnreadableWalletException {
private static Wallet createWallet(File walletFile, Network network, KeyChainGroupStructure structure, ScriptType outputScriptType) throws IOException, UnreadableWalletException {
Context.propagate(new Context());
DeterministicSeed seed = new DeterministicSeed(testWalletMnemonic, null, "", Instant.now());
Wallet wallet = Wallet.fromSeed(params, seed, outputScriptType, structure);
Wallet wallet = Wallet.fromSeed(network, seed, outputScriptType, structure);
wallet.saveToFile(walletFile);
return Wallet.loadFromFile(walletFile);
}

View file

@ -24,6 +24,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.Network;
import org.bitcoinj.base.internal.TimeUtils;
import org.bitcoinj.core.listeners.*;
import org.bitcoinj.core.NetworkParameters;
@ -31,7 +33,6 @@ import org.bitcoinj.core.Peer;
import org.bitcoinj.core.PeerGroup;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.net.discovery.DnsDiscovery;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.utils.BriefLogFormatter;
import org.bitcoinj.wallet.DefaultRiskAnalysis;
import org.bitcoinj.wallet.RiskAnalysis.Result;
@ -40,7 +41,7 @@ import org.slf4j.LoggerFactory;
public class WatchMempool {
private static final Logger log = LoggerFactory.getLogger(WatchMempool.class);
private static final NetworkParameters PARAMS = MainNetParams.get();
private static final Network NETWORK = BitcoinNetwork.MAINNET;
private static final List<Transaction> NO_DEPS = Collections.emptyList();
private static final Map<String, Integer> counters = new HashMap<>();
private static final String TOTAL_KEY = "TOTAL";
@ -49,9 +50,9 @@ public class WatchMempool {
public static void main(String[] args) throws InterruptedException {
BriefLogFormatter.init();
PeerGroup peerGroup = new PeerGroup(PARAMS.network());
PeerGroup peerGroup = new PeerGroup(NETWORK);
peerGroup.setMaxConnections(32);
peerGroup.addPeerDiscovery(new DnsDiscovery(PARAMS));
peerGroup.addPeerDiscovery(new DnsDiscovery(NetworkParameters.of(NETWORK)));
peerGroup.addOnTransactionBroadcastListener((peer, tx) -> {
Result result = DefaultRiskAnalysis.FACTORY.create(null, tx, NO_DEPS).analyze();
incrementCounter(TOTAL_KEY);

View file

@ -18,6 +18,7 @@
package org.bitcoinj.wallettool;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.Network;
import org.bitcoinj.base.Sha256Hash;
import org.bitcoinj.base.internal.TimeUtils;
import org.bitcoinj.crypto.AesKey;
@ -378,7 +379,7 @@ public class WalletTool implements Callable<Integer> {
}
if (action == ActionEnum.CREATE) {
createWallet(params, walletFile);
createWallet(net, walletFile);
return 0; // We're done.
}
if (!walletFile.exists()) {
@ -1069,7 +1070,7 @@ public class WalletTool implements Callable<Integer> {
}
}
private void createWallet(NetworkParameters params, File walletFile) throws IOException {
private void createWallet(Network network, File walletFile) throws IOException {
KeyChainGroupStructure keyChainGroupStructure = KeyChainGroupStructure.BIP32;
if (walletFile.exists() && !force) {
@ -1098,11 +1099,11 @@ public class WalletTool implements Callable<Integer> {
// not reached - all subclasses handled above
throw new RuntimeException(e);
}
wallet = Wallet.fromSeed(params, seed, outputScriptType, keyChainGroupStructure);
wallet = Wallet.fromSeed(network, seed, outputScriptType, keyChainGroupStructure);
} else if (watchKeyStr != null) {
wallet = Wallet.fromWatchingKeyB58(params, watchKeyStr, creationTime);
wallet = Wallet.fromWatchingKeyB58(network, watchKeyStr, creationTime);
} else {
wallet = Wallet.createDeterministic(params, outputScriptType, keyChainGroupStructure);
wallet = Wallet.createDeterministic(network, outputScriptType, keyChainGroupStructure);
}
if (password != null)
wallet.encrypt(password);