use of snowblossom.proto.WalletKeyPair in project snowblossom by snowblossomcoin.
the class SeedTest method testSiteKey.
public static void testSiteKey(String seed) throws Exception {
HashSet<ByteString> seen_keys = new HashSet<>();
for (int i = 0; i < 16; i++) {
String site = NonsenseWordList.getNonsense(5);
WalletKeyPair wkp = SeedUtil.getSiteKey(new NetworkParamsTestnet(), seed, "", site);
WalletKeyPair wkp2 = SeedUtil.getSiteKey(new NetworkParamsTestnet(), seed, "", site);
logger.info("Key path: " + wkp.getHdPath());
testKeyPair(wkp, site);
Assert.assertEquals(wkp, wkp2);
ByteString pub_key = wkp.getPublicKey();
Assert.assertFalse(seen_keys.contains(pub_key));
seen_keys.add(pub_key);
}
}
use of snowblossom.proto.WalletKeyPair in project snowblossom by snowblossomcoin.
the class SeedTest method testGen.
private void testGen(int words) throws Exception {
String seed = SeedUtil.generateSeed(words);
List<String> lst = SeedUtil.getWordsFromSeed(seed);
Assert.assertEquals(words, lst.size());
ByteString seed_data = SeedUtil.decodeSeed(seed, "");
System.out.println("Seed: " + seed + " " + HexUtil.getHexString(seed_data));
DeterministicKey dk = HDKeyDerivation.createMasterPrivateKey(seed_data.toByteArray());
DeterministicHierarchy dh = new DeterministicHierarchy(dk);
System.out.println("Seed dk: " + dk.toString());
System.out.println("Seed ser: " + dk.serializePrivB58(org.bitcoinj.params.MainNetParams.get()));
DeterministicKey dk_acct = dh.get(ImmutableList.of(new ChildNumber(44, true), new ChildNumber(0, true), new ChildNumber(0, true)), true, true);
System.out.println("Seed ser: " + dk_acct.serializePubB58(org.bitcoinj.params.MainNetParams.get()));
System.out.println("Seed acct: " + dk_acct.toString());
System.out.println("Seed acct xprv: " + dk_acct.serializePrivB58(org.bitcoinj.params.MainNetParams.get()));
String xpub = SeedUtil.getSeedXpub(new NetworkParamsTestnet(), seed, "", 0);
for (int c = 0; c < 2; c++) for (int i = 0; i < 20; i++) {
WalletKeyPair wkp = SeedUtil.getKey(new NetworkParamsTestnet(), seed, "", 0, c, i);
System.out.println("Seed wkp: " + HexUtil.getHexString(wkp.getSeedId()) + " " + wkp.getHdPath());
Assert.assertTrue(wkp.getHdPath().startsWith("M/44H/2339H/0H/"));
Assert.assertTrue(wkp.getSeedId().size() == 20);
AddressSpecHash addr = AddressUtil.getHashForSpec(SeedUtil.getAddressSpec(new NetworkParamsTestnet(), xpub, c, i));
AddressSpecHash wkp_addr = AddressUtil.getHashForSpec(AddressUtil.getSimpleSpecForKey(wkp));
Assert.assertEquals(addr, wkp_addr);
testKeyPair(wkp, "hd");
}
ByteString seed_id_seed = SeedUtil.getSeedId(new NetworkParamsTestnet(), seed, "", 0);
ByteString seed_id_xpub = SeedUtil.getSeedIdFromXpub(xpub);
Assert.assertEquals(seed_id_seed, seed_id_xpub);
testSiteKey(seed);
}
use of snowblossom.proto.WalletKeyPair in project snowblossom by snowblossomcoin.
the class KeyUtil method generateWalletECKey.
public static WalletKeyPair generateWalletECKey(String curve_name) {
try {
ECGenParameterSpec spec = new ECGenParameterSpec(curve_name);
KeyPairGenerator key_gen = KeyPairGenerator.getInstance("ECDSA", Globals.getCryptoProviderName());
key_gen.initialize(spec);
KeyPair pair = key_gen.genKeyPair();
if (pair.getPublic() instanceof org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) {
org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey pub = (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) pair.getPublic();
pub.setPointFormat("COMPRESSED");
} else {
throw new Exception("Unable to set public point format to compressed. Not from SC or BC provider");
}
WalletKeyPair wkp = WalletKeyPair.newBuilder().setPublicKey(ByteString.copyFrom(pair.getPublic().getEncoded())).setPrivateKey(ByteString.copyFrom(pair.getPrivate().getEncoded())).setSignatureType(SignatureUtil.SIG_TYPE_ECDSA).build();
return wkp;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of snowblossom.proto.WalletKeyPair in project snowblossom by snowblossomcoin.
the class CipherUtil method decrypt.
public static ByteString decrypt(WalletKeyPair wkp, ByteString cipher_data) throws ValidationException {
try {
String algo = SignatureUtil.getAlgo(wkp.getSignatureType());
KeyPair kp = KeyUtil.decodeKeypair(wkp);
Cipher c = null;
if (algo.equals("ECDSA")) {
c = Cipher.getInstance("ECIES", "BC");
c.init(Cipher.DECRYPT_MODE, kp.getPrivate());
} else {
throw new ValidationException("Encryption not supported with " + algo);
}
return ByteString.copyFrom(c.doFinal(cipher_data.toByteArray()));
} catch (java.security.GeneralSecurityException e) {
throw new ValidationException(e);
}
}
Aggregations