20
20
import com .google .protobuf .ByteString ;
21
21
import org .bitcoinj .base .BitcoinNetwork ;
22
22
import org .bitcoinj .base .Address ;
23
+ import org .bitcoinj .base .Network ;
23
24
import org .bitcoinj .base .internal .TimeUtils ;
24
25
import org .bitcoinj .crypto .AesKey ;
25
26
import org .bitcoinj .core .BloomFilter ;
90
91
public class KeyChainGroup implements KeyBag {
91
92
92
93
/**
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.
94
95
*/
95
96
public static class Builder {
96
- private final NetworkParameters params ;
97
+ private final Network network ;
97
98
private final KeyChainGroupStructure structure ;
98
99
private final List <DeterministicKeyChain > chains = new LinkedList <>();
99
100
private int lookaheadSize = -1 , lookaheadThreshold = -1 ;
100
101
101
- private Builder (NetworkParameters params , KeyChainGroupStructure structure ) {
102
- this .params = params ;
102
+ private Builder (Network network , KeyChainGroupStructure structure ) {
103
+ this .network = network ;
103
104
this .structure = structure ;
104
105
}
105
106
@@ -133,16 +134,16 @@ public Builder fromSeed(DeterministicSeed seed, ScriptType outputScriptType) {
133
134
if (outputScriptType == ScriptType .P2PKH ) {
134
135
DeterministicKeyChain chain = DeterministicKeyChain .builder ().seed (seed )
135
136
.outputScriptType (ScriptType .P2PKH )
136
- .accountPath (structure .accountPathFor (ScriptType .P2PKH , params )).build ();
137
+ .accountPath (structure .accountPathFor (ScriptType .P2PKH , network )).build ();
137
138
this .chains .clear ();
138
139
this .chains .add (chain );
139
140
} else if (outputScriptType == ScriptType .P2WPKH ) {
140
141
DeterministicKeyChain fallbackChain = DeterministicKeyChain .builder ().seed (seed )
141
142
.outputScriptType (ScriptType .P2PKH )
142
- .accountPath (structure .accountPathFor (ScriptType .P2PKH , params )).build ();
143
+ .accountPath (structure .accountPathFor (ScriptType .P2PKH , network )).build ();
143
144
DeterministicKeyChain defaultChain = DeterministicKeyChain .builder ().seed (seed )
144
145
.outputScriptType (ScriptType .P2WPKH )
145
- .accountPath (structure .accountPathFor (ScriptType .P2WPKH , params )).build ();
146
+ .accountPath (structure .accountPathFor (ScriptType .P2WPKH , network )).build ();
146
147
this .chains .clear ();
147
148
this .chains .add (fallbackChain );
148
149
this .chains .add (defaultChain );
@@ -166,16 +167,16 @@ public Builder fromKey(DeterministicKey accountKey, ScriptType outputScriptType)
166
167
if (outputScriptType == ScriptType .P2PKH ) {
167
168
DeterministicKeyChain chain = DeterministicKeyChain .builder ().spend (accountKey )
168
169
.outputScriptType (ScriptType .P2PKH )
169
- .accountPath (structure .accountPathFor (ScriptType .P2PKH , params )).build ();
170
+ .accountPath (structure .accountPathFor (ScriptType .P2PKH , network )).build ();
170
171
this .chains .clear ();
171
172
this .chains .add (chain );
172
173
} else if (outputScriptType == ScriptType .P2WPKH ) {
173
174
DeterministicKeyChain fallbackChain = DeterministicKeyChain .builder ().spend (accountKey )
174
175
.outputScriptType (ScriptType .P2PKH )
175
- .accountPath (structure .accountPathFor (ScriptType .P2PKH , params )).build ();
176
+ .accountPath (structure .accountPathFor (ScriptType .P2PKH , network )).build ();
176
177
DeterministicKeyChain defaultChain = DeterministicKeyChain .builder ().spend (accountKey )
177
178
.outputScriptType (ScriptType .P2WPKH )
178
- .accountPath (structure .accountPathFor (ScriptType .P2WPKH , params )).build ();
179
+ .accountPath (structure .accountPathFor (ScriptType .P2WPKH , network )).build ();
179
180
this .chains .clear ();
180
181
this .chains .add (fallbackChain );
181
182
this .chains .add (defaultChain );
@@ -223,14 +224,14 @@ public Builder lookaheadThreshold(int lookaheadThreshold) {
223
224
}
224
225
225
226
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 );
227
228
}
228
229
}
229
230
230
231
private static final Logger log = LoggerFactory .getLogger (KeyChainGroup .class );
231
232
232
233
private BasicKeyChain basic ;
233
- private final NetworkParameters params ;
234
+ private final Network network ;
234
235
// Keychains for deterministically derived keys.
235
236
protected final @ Nullable LinkedList <DeterministicKeyChain > chains ;
236
237
// currentKeys is used for normal, non-multisig/married wallets. currentAddresses is used when we're handing out
@@ -244,22 +245,40 @@ public KeyChainGroup build() {
244
245
private final CopyOnWriteArrayList <ListenerRegistration <CurrentKeyChangeEventListener >> currentKeyChangeListeners = new CopyOnWriteArrayList <>();
245
246
246
247
/** 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
247
254
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 );
249
260
}
250
261
262
+ /** @deprecated use {@link #builder(Network)} */
263
+ @ Deprecated
251
264
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 );
253
270
}
254
271
272
+ /** @deprecated use {@link #builder(Network, KeyChainGroupStructure)} */
273
+ @ Deprecated
255
274
public static KeyChainGroup .Builder builder (NetworkParameters params , KeyChainGroupStructure structure ) {
256
- return new Builder (params , structure );
275
+ return builder (params . network () , structure );
257
276
}
258
277
259
- private KeyChainGroup (NetworkParameters params , @ Nullable BasicKeyChain basicKeyChain ,
278
+ private KeyChainGroup (Network network , @ Nullable BasicKeyChain basicKeyChain ,
260
279
@ Nullable List <DeterministicKeyChain > chains , int lookaheadSize , int lookaheadThreshold ,
261
280
@ Nullable EnumMap <KeyChain .KeyPurpose , DeterministicKey > currentKeys , @ Nullable KeyCrypter crypter ) {
262
- this .params = params ;
281
+ this .network = network ;
263
282
this .basic = basicKeyChain == null ? new BasicKeyChain () : basicKeyChain ;
264
283
if (chains != null ) {
265
284
if (lookaheadSize > -1 )
@@ -287,7 +306,7 @@ private KeyChainGroup(NetworkParameters params, @Nullable BasicKeyChain basicKey
287
306
for (Map .Entry <KeyChain .KeyPurpose , DeterministicKey > entry : this .currentKeys .entrySet ()) {
288
307
Address address = ScriptBuilder
289
308
.createP2SHOutputScript (getActiveKeyChain ().getRedeemData (entry .getValue ()).redeemScript )
290
- .getToAddress (params . network () );
309
+ .getToAddress (network );
291
310
currentAddresses .put (entry .getKey (), address );
292
311
}
293
312
}
@@ -376,7 +395,7 @@ public Address currentAddress(KeyChain.KeyPurpose purpose) {
376
395
}
377
396
return current ;
378
397
} else if (outputScriptType == ScriptType .P2PKH || outputScriptType == ScriptType .P2WPKH ) {
379
- return currentKey (purpose ).toAddress (outputScriptType , params . network () );
398
+ return currentKey (purpose ).toAddress (outputScriptType , network );
380
399
} else {
381
400
throw new IllegalStateException (chain .getOutputScriptType ().toString ());
382
401
}
@@ -428,7 +447,7 @@ public List<DeterministicKey> freshKeys(KeyChain.KeyPurpose purpose, int numberO
428
447
*/
429
448
public Address freshAddress (KeyChain .KeyPurpose purpose , ScriptType outputScriptType , @ Nullable Instant keyRotationTime ) {
430
449
DeterministicKeyChain chain = getActiveKeyChain (outputScriptType , keyRotationTime );
431
- return chain .getKey (purpose ).toAddress (outputScriptType , params . network () );
450
+ return chain .getKey (purpose ).toAddress (outputScriptType , network );
432
451
}
433
452
434
453
/** @deprecated use {@link #freshAddress(KeyChain.KeyPurpose, ScriptType, Instant)} */
@@ -447,13 +466,13 @@ public Address freshAddress(KeyChain.KeyPurpose purpose) {
447
466
if (chain .isMarried ()) {
448
467
Script outputScript = chain .freshOutputScript (purpose );
449
468
checkState (ScriptPattern .isP2SH (outputScript )); // Only handle P2SH for now
450
- Address freshAddress = LegacyAddress .fromScriptHash (params . network () ,
469
+ Address freshAddress = LegacyAddress .fromScriptHash (network ,
451
470
ScriptPattern .extractHashFromP2SH (outputScript ));
452
471
maybeLookaheadScripts ();
453
472
currentAddresses .put (purpose , freshAddress );
454
473
return freshAddress ;
455
474
} else if (outputScriptType == ScriptType .P2PKH || outputScriptType == ScriptType .P2WPKH ) {
456
- return freshKey (purpose ).toAddress (outputScriptType , params . network () );
475
+ return freshKey (purpose ).toAddress (outputScriptType , network );
457
476
} else {
458
477
throw new IllegalStateException (chain .getOutputScriptType ().toString ());
459
478
}
@@ -968,11 +987,11 @@ public List<Protos.Key> serializeToProtobuf() {
968
987
.collect (Collectors .toList ());
969
988
}
970
989
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 ());
973
992
}
974
993
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 {
976
995
BasicKeyChain basicKeyChain = BasicKeyChain .fromProtobufUnencrypted (keys );
977
996
List <DeterministicKeyChain > chains = DeterministicKeyChain .fromProtobuf (keys , null , factory );
978
997
int lookaheadSize = -1 , lookaheadThreshold = -1 ;
@@ -984,14 +1003,14 @@ public static KeyChainGroup fromProtobufUnencrypted(NetworkParameters params, Li
984
1003
currentKeys = createCurrentKeysMap (chains );
985
1004
}
986
1005
extractFollowingKeychains (chains );
987
- return new KeyChainGroup (params , basicKeyChain , chains , lookaheadSize , lookaheadThreshold , currentKeys , null );
1006
+ return new KeyChainGroup (network , basicKeyChain , chains , lookaheadSize , lookaheadThreshold , currentKeys , null );
988
1007
}
989
1008
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 ());
992
1011
}
993
1012
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 {
995
1014
Objects .requireNonNull (crypter );
996
1015
BasicKeyChain basicKeyChain = BasicKeyChain .fromProtobufEncrypted (keys , crypter );
997
1016
List <DeterministicKeyChain > chains = DeterministicKeyChain .fromProtobuf (keys , crypter , factory );
@@ -1004,7 +1023,7 @@ public static KeyChainGroup fromProtobufEncrypted(NetworkParameters params, List
1004
1023
currentKeys = createCurrentKeysMap (chains );
1005
1024
}
1006
1025
extractFollowingKeychains (chains );
1007
- return new KeyChainGroup (params , basicKeyChain , chains , lookaheadSize , lookaheadThreshold , currentKeys , crypter );
1026
+ return new KeyChainGroup (network , basicKeyChain , chains , lookaheadSize , lookaheadThreshold , currentKeys , crypter );
1008
1027
}
1009
1028
1010
1029
/**
@@ -1134,10 +1153,10 @@ private static void extractFollowingKeychains(List<DeterministicKeyChain> chains
1134
1153
public String toString (boolean includeLookahead , boolean includePrivateKeys , @ Nullable AesKey aesKey ) {
1135
1154
final StringBuilder builder = new StringBuilder ();
1136
1155
if (basic != null )
1137
- builder .append (basic .toString (includePrivateKeys , aesKey , params . network () ));
1156
+ builder .append (basic .toString (includePrivateKeys , aesKey , network ));
1138
1157
if (chains != null )
1139
1158
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' );
1141
1160
return builder .toString ();
1142
1161
}
1143
1162
0 commit comments