private DeterministicKey deriveChild(DeterministicKey parent, ChildNumber createChildNumber) { DeterministicKey childKey = HDKeyDerivation.deriveChildKey(parent, createChildNumber); putKey(childKey); return childKey; }
/** * Constructs a new hierarchy rooted at the given key. Note that this does not have to be the top of the tree. * You can construct a DeterministicHierarchy for a subtree of a larger tree that you may not own. */ public DeterministicHierarchy(DeterministicKey rootKey) { putKey(rootKey); rootPath = rootKey.getPath(); }
/** * Constructs a new hierarchy rooted at the given key. Note that this does not have to be the top of the tree. * You can construct a DeterministicHierarchy for a subtree of a larger tree that you may not own. */ public DeterministicHierarchy(DeterministicKey rootKey) { putKey(rootKey); rootPath = rootKey.getPath(); }
private DeterministicKey deriveChild(DeterministicKey parent, ChildNumber createChildNumber) { DeterministicKey childKey = HDKeyDerivation.deriveChildKey(parent, createChildNumber); putKey(childKey); return childKey; }
/** * Constructs a new hierarchy rooted at the given key. Note that this does not have to be the top of the tree. * You can construct a DeterministicHierarchy for a subtree of a larger tree that you may not own. */ public DeterministicHierarchy(DeterministicKey rootKey) { putKey(rootKey); rootPath = rootKey.getPath(); }
/** * Constructs a new hierarchy rooted at the given key. Note that this does not have to be the top of the tree. * You can construct a DeterministicHierarchy for a subtree of a larger tree that you may not own. */ public DeterministicHierarchy(DeterministicKey rootKey) { putKey(rootKey); rootPath = rootKey.getPath(); }
private DeterministicKey deriveChild(DeterministicKey parent, ChildNumber createChildNumber) { DeterministicKey childKey = HDKeyDerivation.deriveChildKey(parent, createChildNumber); putKey(childKey); return childKey; }
private DeterministicKey deriveChild(DeterministicKey parent, ChildNumber createChildNumber) { DeterministicKey childKey = HDKeyDerivation.deriveChildKey(parent, createChildNumber); putKey(childKey); return childKey; }
/** * Returns a key for the given path, optionally creating it. * * @param path the path to the key * @param relativePath whether the path is relative to the root path * @param create whether the key corresponding to path should be created (with any necessary ancestors) if it doesn't exist already * @return next newly created key using the child derivation function * @throws IllegalArgumentException if create is false and the path was not found. */ public DeterministicKey get(List<ChildNumber> path, boolean relativePath, boolean create) { ImmutableList<ChildNumber> absolutePath = relativePath ? ImmutableList.<ChildNumber>builder().addAll(rootPath).addAll(path).build() : ImmutableList.copyOf(path); if (!keys.containsKey(absolutePath)) { if (!create) throw new IllegalArgumentException(String.format(Locale.US, "No key found for %s path %s.", relativePath ? "relative" : "absolute", HDUtils.formatPath(path))); checkArgument(absolutePath.size() > 0, "Can't derive the master key: nothing to derive from."); DeterministicKey parent = get(absolutePath.subList(0, absolutePath.size() - 1), false, true); putKey(HDKeyDerivation.deriveChildKey(parent, absolutePath.get(absolutePath.size() - 1))); } return keys.get(absolutePath); }
/** * Returns a key for the given path, optionally creating it. * * @param path the path to the key * @param relativePath whether the path is relative to the root path * @param create whether the key corresponding to path should be created (with any necessary ancestors) if it doesn't exist already * @return next newly created key using the child derivation function * @throws IllegalArgumentException if create is false and the path was not found. */ public DeterministicKey get(List<ChildNumber> path, boolean relativePath, boolean create) { ImmutableList<ChildNumber> absolutePath = relativePath ? ImmutableList.<ChildNumber>builder().addAll(rootPath).addAll(path).build() : ImmutableList.copyOf(path); if (!keys.containsKey(absolutePath)) { if (!create) throw new IllegalArgumentException(String.format(Locale.US, "No key found for %s path %s.", relativePath ? "relative" : "absolute", HDUtils.formatPath(path))); checkArgument(absolutePath.size() > 0, "Can't derive the master key: nothing to derive from."); DeterministicKey parent = get(absolutePath.subList(0, absolutePath.size() - 1), false, true); putKey(HDKeyDerivation.deriveChildKey(parent, absolutePath.get(absolutePath.size() - 1))); } return keys.get(absolutePath); }
/** * Returns a key for the given path, optionally creating it. * * @param path the path to the key * @param relativePath whether the path is relative to the root path * @param create whether the key corresponding to path should be created (with any necessary ancestors) if it doesn't exist already * @return next newly created key using the child derivation function * @throws IllegalArgumentException if create is false and the path was not found. */ public DeterministicKey get(List<ChildNumber> path, boolean relativePath, boolean create) { ImmutableList<ChildNumber> absolutePath = relativePath ? ImmutableList.<ChildNumber>builder().addAll(rootPath).addAll(path).build() : ImmutableList.copyOf(path); if (!keys.containsKey(absolutePath)) { if (!create) throw new IllegalArgumentException(String.format(Locale.US, "No key found for %s path %s.", relativePath ? "relative" : "absolute", HDUtils.formatPath(path))); checkArgument(absolutePath.size() > 0, "Can't derive the master key: nothing to derive from."); DeterministicKey parent = get(absolutePath.subList(0, absolutePath.size() - 1), false, true); putKey(HDKeyDerivation.deriveChildKey(parent, absolutePath.get(absolutePath.size() - 1))); } return keys.get(absolutePath); }
/** * Returns a key for the given path, optionally creating it. * * @param path the path to the key * @param relativePath whether the path is relative to the root path * @param create whether the key corresponding to path should be created (with any necessary ancestors) if it doesn't exist already * @return next newly created key using the child derivation function * @throws IllegalArgumentException if create is false and the path was not found. */ public DeterministicKey get(List<ChildNumber> path, boolean relativePath, boolean create) { ImmutableList<ChildNumber> absolutePath = relativePath ? ImmutableList.<ChildNumber>builder().addAll(rootPath).addAll(path).build() : ImmutableList.copyOf(path); if (!keys.containsKey(absolutePath)) { if (!create) throw new IllegalArgumentException(String.format(Locale.US, "No key found for %s path %s.", relativePath ? "relative" : "absolute", HDUtils.formatPath(path))); checkArgument(absolutePath.size() > 0, "Can't derive the master key: nothing to derive from."); DeterministicKey parent = get(absolutePath.subList(0, absolutePath.size() - 1), false, true); putKey(HDKeyDerivation.deriveChildKey(parent, absolutePath.get(absolutePath.size() - 1))); } return keys.get(absolutePath); }
private DeterministicKey encryptNonLeaf(KeyParameter aesKey, SimpleHDKeyChain chain, DeterministicKey parent, ImmutableList<ChildNumber> path) { DeterministicKey key = chain.hierarchy.get(path, true, false); key = key.encrypt(checkNotNull(simpleKeyChain.getKeyCrypter(), "Chain has null KeyCrypter"), aesKey, parent); hierarchy.putKey(key); simpleKeyChain.importKey(key); return key; }
private DeterministicKey encryptNonLeaf(KeyParameter aesKey, DeterministicKeyChain chain, DeterministicKey parent, ImmutableList<ChildNumber> path) { DeterministicKey key = chain.hierarchy.get(path, false, false); key = key.encrypt(checkNotNull(basicKeyChain.getKeyCrypter()), aesKey, parent); hierarchy.putKey(key); basicKeyChain.importKey(key); return key; }
private DeterministicKey encryptNonLeaf(KeyParameter aesKey, DeterministicKeyChain chain, DeterministicKey parent, ImmutableList<ChildNumber> path) { DeterministicKey key = chain.hierarchy.get(path, false, false); key = key.encrypt(checkNotNull(basicKeyChain.getKeyCrypter()), aesKey, parent); hierarchy.putKey(key); basicKeyChain.importKey(key); return key; }
private DeterministicKey encryptNonLeaf(KeyParameter aesKey, DeterministicKeyChain chain, DeterministicKey parent, ImmutableList<ChildNumber> path) { DeterministicKey key = chain.hierarchy.get(path, false, false); key = key.encrypt(checkNotNull(basicKeyChain.getKeyCrypter()), aesKey, parent); hierarchy.putKey(key); basicKeyChain.importKey(key); return key; }
private DeterministicKey encryptNonLeaf(KeyParameter aesKey, DeterministicKeyChain chain, DeterministicKey parent, ImmutableList<ChildNumber> path) { DeterministicKey key = chain.hierarchy.get(path, false, false); key = key.encrypt(checkNotNull(basicKeyChain.getKeyCrypter()), aesKey, parent); hierarchy.putKey(key); basicKeyChain.importKey(key); return key; }
private DeterministicKey encryptNonLeaf(KeyParameter aesKey, SimpleHDKeyChain chain, DeterministicKey parent, ImmutableList<ChildNumber> path) { DeterministicKey key = chain.hierarchy.get(path, true, false); key = key.encrypt(checkNotNull(simpleKeyChain.getKeyCrypter(), "Chain has null KeyCrypter"), aesKey, parent); hierarchy.putKey(key); simpleKeyChain.importKey(key); return key; }
@Override public SimpleHDKeyChain toDecrypted(KeyParameter aesKey) { checkState(getKeyCrypter() != null, "Key chain not encrypted"); checkState(rootKey.isEncrypted(), "Root key not encrypted"); DeterministicKey decKey = rootKey.decrypt(getKeyCrypter(), aesKey); SimpleHDKeyChain chain = new SimpleHDKeyChain(decKey); // Now double check that the keys match to catch the case where the key is wrong but padding didn't catch it. if (!chain.getWatchingKey().getPubKeyPoint().equals(getWatchingKey().getPubKeyPoint())) throw new KeyCrypterException("Provided AES key is wrong"); chain.lookaheadSize = lookaheadSize; // Now copy the (pubkey only) leaf keys across to avoid rederiving them. The private key bytes are missing // anyway so there's nothing to decrypt. for (ECKey eckey : simpleKeyChain.getKeys()) { DeterministicKey key = (DeterministicKey) eckey; if (!isLeaf(key)) continue; // Not a leaf key. checkState(key.isEncrypted(), "Key is not encrypted"); DeterministicKey parent = chain.hierarchy.get(checkNotNull(key.getParent(), "Key has null parent").getPath(), false, false); // Clone the key to the new decrypted hierarchy. key = new DeterministicKey(key.getPubOnly(), parent); chain.hierarchy.putKey(key); chain.simpleKeyChain.importKeys(key); } chain.issuedExternalKeys = issuedExternalKeys; chain.issuedInternalKeys = issuedInternalKeys; return chain; }
private SimpleHDKeyChain(KeyCrypter crypter, KeyParameter aesKey, SimpleHDKeyChain chain) { checkArgument(!chain.rootKey.isEncrypted(), "Chain already encrypted"); this.issuedExternalKeys = chain.issuedExternalKeys; this.issuedInternalKeys = chain.issuedInternalKeys; this.lookaheadSize = chain.lookaheadSize; this.lookaheadThreshold = chain.lookaheadThreshold; simpleKeyChain = new SimpleKeyChain(crypter); // The first number is the "account number" but we don't use that feature. rootKey = chain.rootKey.encrypt(crypter, aesKey, null); hierarchy = new DeterministicHierarchy(rootKey); simpleKeyChain.importKey(rootKey); externalKey = encryptNonLeaf(aesKey, chain, rootKey, EXTERNAL_PATH); internalKey = encryptNonLeaf(aesKey, chain, rootKey, INTERNAL_PATH); // Now copy the (pubkey only) leaf keys across to avoid rederiving them. The private key bytes are missing // anyway so there's nothing to encrypt. for (ECKey eckey : chain.simpleKeyChain.getKeys()) { DeterministicKey key = (DeterministicKey) eckey; if (!isLeaf(key)) continue; // Not a leaf key. DeterministicKey parent = hierarchy.get(checkNotNull(key.getParent(), "Key has no parent").getPath(), false, false); // Clone the key to the new encrypted hierarchy. key = new DeterministicKey(key.getPubOnly(), parent); hierarchy.putKey(key); simpleKeyChain.importKey(key); } }