A KeyChainGroup is used by the Wallet and manages: a BasicKeyChain object (which will normally be empty), and zero or more DeterministicKeyChains. The last added deterministic keychain is always the default active keychain, that's the one we normally derive keys and addres
| 90 | * class docs for {@link DeterministicKeyChain} for more information on this topic.</p> |
| 91 | */ |
| 92 | public class KeyChainGroup implements KeyBag { |
| 93 | |
| 94 | /** |
| 95 | * Builder for {@link KeyChainGroup}. Use {@link KeyChainGroup#builder(Network)} to acquire an instance. |
| 96 | */ |
| 97 | public static class Builder { |
| 98 | private final Network network; |
| 99 | private final KeyChainGroupStructure structure; |
| 100 | private final List<DeterministicKeyChain> chains = new LinkedList<>(); |
| 101 | private int lookaheadSize = -1, lookaheadThreshold = -1; |
| 102 | |
| 103 | private Builder(Network network, KeyChainGroupStructure structure) { |
| 104 | this.network = network; |
| 105 | this.structure = structure; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * <p>Add chain from a random source.</p> |
| 110 | * <p>In the case of P2PKH, just a P2PKH chain is created and activated which is then the default chain for fresh |
| 111 | * addresses. It can be upgraded to P2WPKH later.</p> |
| 112 | * <p>In the case of P2WPKH, both a P2PKH and a P2WPKH chain are created and activated, the latter being the default |
| 113 | * chain. This behaviour will likely be changed in future such that only a P2WPKH chain is created and |
| 114 | * activated.</p> |
| 115 | * @param outputScriptType type of addresses (aka output scripts) to generate for receiving |
| 116 | */ |
| 117 | public Builder fromRandom(ScriptType outputScriptType) { |
| 118 | DeterministicSeed seed = DeterministicSeed.ofRandom(new SecureRandom(), |
| 119 | DeterministicSeed.DEFAULT_SEED_ENTROPY_BITS, ""); |
| 120 | fromSeed(seed, outputScriptType); |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * <p>Add chain from a given seed.</p> |
| 126 | * <p>In the case of P2PKH, just a P2PKH chain is created and activated which is then the default chain for fresh |
| 127 | * addresses. It can be upgraded to P2WPKH later.</p> |
| 128 | * <p>In the case of P2WPKH, both a P2PKH and a P2WPKH chain are created and activated, the latter being the default |
| 129 | * chain. This behaviour will likely be changed in future such that only a P2WPKH chain is created and |
| 130 | * activated.</p> |
| 131 | * @param seed deterministic seed to derive all keys from |
| 132 | * @param outputScriptType type of addresses (aka output scripts) to generate for receiving |
| 133 | */ |
| 134 | public Builder fromSeed(DeterministicSeed seed, ScriptType outputScriptType) { |
| 135 | if (outputScriptType == ScriptType.P2PKH) { |
| 136 | DeterministicKeyChain chain = DeterministicKeyChain.builder().seed(seed) |
| 137 | .outputScriptType(ScriptType.P2PKH) |
| 138 | .accountPath(structure.accountPathFor(ScriptType.P2PKH, network)).build(); |
| 139 | this.chains.clear(); |
| 140 | this.chains.add(chain); |
| 141 | } else if (outputScriptType == ScriptType.P2WPKH) { |
| 142 | DeterministicKeyChain fallbackChain = DeterministicKeyChain.builder().seed(seed) |
| 143 | .outputScriptType(ScriptType.P2PKH) |
| 144 | .accountPath(structure.accountPathFor(ScriptType.P2PKH, network)).build(); |
| 145 | DeterministicKeyChain defaultChain = DeterministicKeyChain.builder().seed(seed) |
| 146 | .outputScriptType(ScriptType.P2WPKH) |
| 147 | .accountPath(structure.accountPathFor(ScriptType.P2WPKH, network)).build(); |
| 148 | this.chains.clear(); |
| 149 | this.chains.add(fallbackChain); |
nothing calls this directly
no outgoing calls
no test coverage detected