Skip to content

Commit e7017bf

Browse files
committed
KeyChainGroup: migrate to Network from NetworkParameters
1 parent 3e7761c commit e7017bf

File tree

10 files changed

+98
-81
lines changed

10 files changed

+98
-81
lines changed

core/src/main/java/org/bitcoinj/kits/WalletAppKit.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ private Wallet loadWallet(boolean shouldReplayWallet) throws Exception {
508508
}
509509

510510
protected Wallet createWallet() {
511-
KeyChainGroup.Builder kcg = KeyChainGroup.builder(params, structure);
511+
KeyChainGroup.Builder kcg = KeyChainGroup.builder(network, structure);
512512
if (restoreFromSeed != null)
513513
kcg.fromSeed(restoreFromSeed, preferredOutputScriptType);
514514
else if (restoreFromKey != null)

core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java

+51-32
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.protobuf.ByteString;
2121
import org.bitcoinj.base.BitcoinNetwork;
2222
import org.bitcoinj.base.Address;
23+
import org.bitcoinj.base.Network;
2324
import org.bitcoinj.base.internal.TimeUtils;
2425
import org.bitcoinj.crypto.AesKey;
2526
import org.bitcoinj.core.BloomFilter;
@@ -90,16 +91,16 @@
9091
public class KeyChainGroup implements KeyBag {
9192

9293
/**
93-
* Builder for {@link KeyChainGroup}. Use {@link KeyChainGroup#builder(NetworkParameters)} to acquire an instance.
94+
* Builder for {@link KeyChainGroup}. Use {@link KeyChainGroup#builder(Network)} to acquire an instance.
9495
*/
9596
public static class Builder {
96-
private final NetworkParameters params;
97+
private final Network network;
9798
private final KeyChainGroupStructure structure;
9899
private final List<DeterministicKeyChain> chains = new LinkedList<>();
99100
private int lookaheadSize = -1, lookaheadThreshold = -1;
100101

101-
private Builder(NetworkParameters params, KeyChainGroupStructure structure) {
102-
this.params = params;
102+
private Builder(Network network, KeyChainGroupStructure structure) {
103+
this.network = network;
103104
this.structure = structure;
104105
}
105106

@@ -133,16 +134,16 @@ public Builder fromSeed(DeterministicSeed seed, ScriptType outputScriptType) {
133134
if (outputScriptType == ScriptType.P2PKH) {
134135
DeterministicKeyChain chain = DeterministicKeyChain.builder().seed(seed)
135136
.outputScriptType(ScriptType.P2PKH)
136-
.accountPath(structure.accountPathFor(ScriptType.P2PKH, params)).build();
137+
.accountPath(structure.accountPathFor(ScriptType.P2PKH, network)).build();
137138
this.chains.clear();
138139
this.chains.add(chain);
139140
} else if (outputScriptType == ScriptType.P2WPKH) {
140141
DeterministicKeyChain fallbackChain = DeterministicKeyChain.builder().seed(seed)
141142
.outputScriptType(ScriptType.P2PKH)
142-
.accountPath(structure.accountPathFor(ScriptType.P2PKH, params)).build();
143+
.accountPath(structure.accountPathFor(ScriptType.P2PKH, network)).build();
143144
DeterministicKeyChain defaultChain = DeterministicKeyChain.builder().seed(seed)
144145
.outputScriptType(ScriptType.P2WPKH)
145-
.accountPath(structure.accountPathFor(ScriptType.P2WPKH, params)).build();
146+
.accountPath(structure.accountPathFor(ScriptType.P2WPKH, network)).build();
146147
this.chains.clear();
147148
this.chains.add(fallbackChain);
148149
this.chains.add(defaultChain);
@@ -166,16 +167,16 @@ public Builder fromKey(DeterministicKey accountKey, ScriptType outputScriptType)
166167
if (outputScriptType == ScriptType.P2PKH) {
167168
DeterministicKeyChain chain = DeterministicKeyChain.builder().spend(accountKey)
168169
.outputScriptType(ScriptType.P2PKH)
169-
.accountPath(structure.accountPathFor(ScriptType.P2PKH, params)).build();
170+
.accountPath(structure.accountPathFor(ScriptType.P2PKH, network)).build();
170171
this.chains.clear();
171172
this.chains.add(chain);
172173
} else if (outputScriptType == ScriptType.P2WPKH) {
173174
DeterministicKeyChain fallbackChain = DeterministicKeyChain.builder().spend(accountKey)
174175
.outputScriptType(ScriptType.P2PKH)
175-
.accountPath(structure.accountPathFor(ScriptType.P2PKH, params)).build();
176+
.accountPath(structure.accountPathFor(ScriptType.P2PKH, network)).build();
176177
DeterministicKeyChain defaultChain = DeterministicKeyChain.builder().spend(accountKey)
177178
.outputScriptType(ScriptType.P2WPKH)
178-
.accountPath(structure.accountPathFor(ScriptType.P2WPKH, params)).build();
179+
.accountPath(structure.accountPathFor(ScriptType.P2WPKH, network)).build();
179180
this.chains.clear();
180181
this.chains.add(fallbackChain);
181182
this.chains.add(defaultChain);
@@ -223,14 +224,14 @@ public Builder lookaheadThreshold(int lookaheadThreshold) {
223224
}
224225

225226
public KeyChainGroup build() {
226-
return new KeyChainGroup(params, null, chains, lookaheadSize, lookaheadThreshold, null, null);
227+
return new KeyChainGroup(network, null, chains, lookaheadSize, lookaheadThreshold, null, null);
227228
}
228229
}
229230

230231
private static final Logger log = LoggerFactory.getLogger(KeyChainGroup.class);
231232

232233
private BasicKeyChain basic;
233-
private final NetworkParameters params;
234+
private final Network network;
234235
// Keychains for deterministically derived keys.
235236
protected final @Nullable LinkedList<DeterministicKeyChain> chains;
236237
// currentKeys is used for normal, non-multisig/married wallets. currentAddresses is used when we're handing out
@@ -244,22 +245,40 @@ public KeyChainGroup build() {
244245
private final CopyOnWriteArrayList<ListenerRegistration<CurrentKeyChangeEventListener>> currentKeyChangeListeners = new CopyOnWriteArrayList<>();
245246

246247
/** Creates a keychain group with just a basic chain. No deterministic chains will be created automatically. */
248+
public static KeyChainGroup createBasic(Network network) {
249+
return new KeyChainGroup(network, new BasicKeyChain(), null, -1, -1, null, null);
250+
}
251+
252+
/** @deprecated use {@link #createBasic(Network)} */
253+
@Deprecated
247254
public static KeyChainGroup createBasic(NetworkParameters params) {
248-
return new KeyChainGroup(params, new BasicKeyChain(), null, -1, -1, null, null);
255+
return createBasic(params.network());
256+
}
257+
258+
public static KeyChainGroup.Builder builder(Network network) {
259+
return new Builder(network, KeyChainGroupStructure.BIP32);
249260
}
250261

262+
/** @deprecated use {@link #builder(Network)} */
263+
@Deprecated
251264
public static KeyChainGroup.Builder builder(NetworkParameters params) {
252-
return new Builder(params, KeyChainGroupStructure.BIP32);
265+
return builder(params.network());
266+
}
267+
268+
public static KeyChainGroup.Builder builder(Network network, KeyChainGroupStructure structure) {
269+
return new Builder(network, structure);
253270
}
254271

272+
/** @deprecated use {@link #builder(Network, KeyChainGroupStructure)} */
273+
@Deprecated
255274
public static KeyChainGroup.Builder builder(NetworkParameters params, KeyChainGroupStructure structure) {
256-
return new Builder(params, structure);
275+
return builder(params.network(), structure);
257276
}
258277

259-
private KeyChainGroup(NetworkParameters params, @Nullable BasicKeyChain basicKeyChain,
278+
private KeyChainGroup(Network network, @Nullable BasicKeyChain basicKeyChain,
260279
@Nullable List<DeterministicKeyChain> chains, int lookaheadSize, int lookaheadThreshold,
261280
@Nullable EnumMap<KeyChain.KeyPurpose, DeterministicKey> currentKeys, @Nullable KeyCrypter crypter) {
262-
this.params = params;
281+
this.network = network;
263282
this.basic = basicKeyChain == null ? new BasicKeyChain() : basicKeyChain;
264283
if (chains != null) {
265284
if (lookaheadSize > -1)
@@ -287,7 +306,7 @@ private KeyChainGroup(NetworkParameters params, @Nullable BasicKeyChain basicKey
287306
for (Map.Entry<KeyChain.KeyPurpose, DeterministicKey> entry : this.currentKeys.entrySet()) {
288307
Address address = ScriptBuilder
289308
.createP2SHOutputScript(getActiveKeyChain().getRedeemData(entry.getValue()).redeemScript)
290-
.getToAddress(params.network());
309+
.getToAddress(network);
291310
currentAddresses.put(entry.getKey(), address);
292311
}
293312
}
@@ -376,7 +395,7 @@ public Address currentAddress(KeyChain.KeyPurpose purpose) {
376395
}
377396
return current;
378397
} else if (outputScriptType == ScriptType.P2PKH || outputScriptType == ScriptType.P2WPKH) {
379-
return currentKey(purpose).toAddress(outputScriptType, params.network());
398+
return currentKey(purpose).toAddress(outputScriptType, network);
380399
} else {
381400
throw new IllegalStateException(chain.getOutputScriptType().toString());
382401
}
@@ -428,7 +447,7 @@ public List<DeterministicKey> freshKeys(KeyChain.KeyPurpose purpose, int numberO
428447
*/
429448
public Address freshAddress(KeyChain.KeyPurpose purpose, ScriptType outputScriptType, @Nullable Instant keyRotationTime) {
430449
DeterministicKeyChain chain = getActiveKeyChain(outputScriptType, keyRotationTime);
431-
return chain.getKey(purpose).toAddress(outputScriptType, params.network());
450+
return chain.getKey(purpose).toAddress(outputScriptType, network);
432451
}
433452

434453
/** @deprecated use {@link #freshAddress(KeyChain.KeyPurpose, ScriptType, Instant)} */
@@ -447,13 +466,13 @@ public Address freshAddress(KeyChain.KeyPurpose purpose) {
447466
if (chain.isMarried()) {
448467
Script outputScript = chain.freshOutputScript(purpose);
449468
checkState(ScriptPattern.isP2SH(outputScript)); // Only handle P2SH for now
450-
Address freshAddress = LegacyAddress.fromScriptHash(params.network(),
469+
Address freshAddress = LegacyAddress.fromScriptHash(network,
451470
ScriptPattern.extractHashFromP2SH(outputScript));
452471
maybeLookaheadScripts();
453472
currentAddresses.put(purpose, freshAddress);
454473
return freshAddress;
455474
} else if (outputScriptType == ScriptType.P2PKH || outputScriptType == ScriptType.P2WPKH) {
456-
return freshKey(purpose).toAddress(outputScriptType, params.network());
475+
return freshKey(purpose).toAddress(outputScriptType, network);
457476
} else {
458477
throw new IllegalStateException(chain.getOutputScriptType().toString());
459478
}
@@ -968,11 +987,11 @@ public List<Protos.Key> serializeToProtobuf() {
968987
.collect(Collectors.toList());
969988
}
970989

971-
static KeyChainGroup fromProtobufUnencrypted(NetworkParameters params, List<Protos.Key> keys) throws UnreadableWalletException {
972-
return fromProtobufUnencrypted(params, keys, new DefaultKeyChainFactory());
990+
static KeyChainGroup fromProtobufUnencrypted(Network network, List<Protos.Key> keys) throws UnreadableWalletException {
991+
return fromProtobufUnencrypted(network, keys, new DefaultKeyChainFactory());
973992
}
974993

975-
public static KeyChainGroup fromProtobufUnencrypted(NetworkParameters params, List<Protos.Key> keys, KeyChainFactory factory) throws UnreadableWalletException {
994+
public static KeyChainGroup fromProtobufUnencrypted(Network network, List<Protos.Key> keys, KeyChainFactory factory) throws UnreadableWalletException {
976995
BasicKeyChain basicKeyChain = BasicKeyChain.fromProtobufUnencrypted(keys);
977996
List<DeterministicKeyChain> chains = DeterministicKeyChain.fromProtobuf(keys, null, factory);
978997
int lookaheadSize = -1, lookaheadThreshold = -1;
@@ -984,14 +1003,14 @@ public static KeyChainGroup fromProtobufUnencrypted(NetworkParameters params, Li
9841003
currentKeys = createCurrentKeysMap(chains);
9851004
}
9861005
extractFollowingKeychains(chains);
987-
return new KeyChainGroup(params, basicKeyChain, chains, lookaheadSize, lookaheadThreshold, currentKeys, null);
1006+
return new KeyChainGroup(network, basicKeyChain, chains, lookaheadSize, lookaheadThreshold, currentKeys, null);
9881007
}
9891008

990-
static KeyChainGroup fromProtobufEncrypted(NetworkParameters params, List<Protos.Key> keys, KeyCrypter crypter) throws UnreadableWalletException {
991-
return fromProtobufEncrypted(params, keys, crypter, new DefaultKeyChainFactory());
1009+
static KeyChainGroup fromProtobufEncrypted(Network network, List<Protos.Key> keys, KeyCrypter crypter) throws UnreadableWalletException {
1010+
return fromProtobufEncrypted(network, keys, crypter, new DefaultKeyChainFactory());
9921011
}
9931012

994-
public static KeyChainGroup fromProtobufEncrypted(NetworkParameters params, List<Protos.Key> keys, KeyCrypter crypter, KeyChainFactory factory) throws UnreadableWalletException {
1013+
public static KeyChainGroup fromProtobufEncrypted(Network network, List<Protos.Key> keys, KeyCrypter crypter, KeyChainFactory factory) throws UnreadableWalletException {
9951014
Objects.requireNonNull(crypter);
9961015
BasicKeyChain basicKeyChain = BasicKeyChain.fromProtobufEncrypted(keys, crypter);
9971016
List<DeterministicKeyChain> chains = DeterministicKeyChain.fromProtobuf(keys, crypter, factory);
@@ -1004,7 +1023,7 @@ public static KeyChainGroup fromProtobufEncrypted(NetworkParameters params, List
10041023
currentKeys = createCurrentKeysMap(chains);
10051024
}
10061025
extractFollowingKeychains(chains);
1007-
return new KeyChainGroup(params, basicKeyChain, chains, lookaheadSize, lookaheadThreshold, currentKeys, crypter);
1026+
return new KeyChainGroup(network, basicKeyChain, chains, lookaheadSize, lookaheadThreshold, currentKeys, crypter);
10081027
}
10091028

10101029
/**
@@ -1134,10 +1153,10 @@ private static void extractFollowingKeychains(List<DeterministicKeyChain> chains
11341153
public String toString(boolean includeLookahead, boolean includePrivateKeys, @Nullable AesKey aesKey) {
11351154
final StringBuilder builder = new StringBuilder();
11361155
if (basic != null)
1137-
builder.append(basic.toString(includePrivateKeys, aesKey, params.network()));
1156+
builder.append(basic.toString(includePrivateKeys, aesKey, network));
11381157
if (chains != null)
11391158
for (DeterministicKeyChain chain : chains)
1140-
builder.append(chain.toString(includeLookahead, includePrivateKeys, aesKey, params.network())).append('\n');
1159+
builder.append(chain.toString(includeLookahead, includePrivateKeys, aesKey, network)).append('\n');
11411160
return builder.toString();
11421161
}
11431162

0 commit comments

Comments
 (0)