3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 23:02:15 +00:00

Add Wallet.fromKeys(). Creates a wallet containing a given set of keys. All further keys will be derived from the oldest key.

This commit is contained in:
Andreas Schildbach 2014-12-17 23:09:27 +01:00
parent ae585608e6
commit 89c53a8f8e
2 changed files with 23 additions and 0 deletions

View File

@ -240,6 +240,18 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
return new Wallet(params, new KeyChainGroup(params, watchKey));
}
/**
* Creates a wallet containing a given set of keys. All further keys will be derived from the oldest key.
*/
public static Wallet fromKeys(NetworkParameters params, List<ECKey> keys) {
for (ECKey key : keys)
checkArgument(!(key instanceof DeterministicKey));
KeyChainGroup group = new KeyChainGroup(params);
group.importKeys(keys);
return new Wallet(params, group);
}
// TODO: When this class moves to the Wallet package, along with the protobuf serializer, then hide this.
/** For internal use only. */
public Wallet(NetworkParameters params, KeyChainGroup keyChainGroup) {

View File

@ -3108,4 +3108,15 @@ public class WalletTest extends TestWithWallet {
SendRequest req = SendRequest.to(notMyAddr.getParameters(), key, Coin.CENT);
wallet.sendCoins(req);
}
@Test
public void fromKeys() {
ECKey key = ECKey.fromPrivate(Utils.HEX.decode("00905b93f990267f4104f316261fc10f9f983551f9ef160854f40102eb71cffdcc"));
Wallet wallet = Wallet.fromKeys(params, Arrays.asList(key));
assertEquals(1, wallet.getImportedKeys().size());
assertEquals(key, wallet.getImportedKeys().get(0));
wallet.upgradeToDeterministic(null);
String seed = wallet.getKeyChainSeed().toHexString();
assertEquals("5ca8cd6c01aa004d3c5396c628b78a4a89462f412f460a845b594ac42eceaa264b0e14dcd4fe73d4ed08ce06f4c28facfa85042d26d784ab2798a870bb7af556", seed);
}
}