Search in sources :

Example 16 with WalletKeyPair

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);
    }
}
Also used : WalletKeyPair(snowblossom.proto.WalletKeyPair) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) HashSet(java.util.HashSet)

Example 17 with WalletKeyPair

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);
}
Also used : WalletKeyPair(snowblossom.proto.WalletKeyPair) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ChildNumber(org.bitcoinj.crypto.ChildNumber) DeterministicKey(org.bitcoinj.crypto.DeterministicKey) DeterministicHierarchy(org.bitcoinj.crypto.DeterministicHierarchy)

Example 18 with WalletKeyPair

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);
    }
}
Also used : WalletKeyPair(snowblossom.proto.WalletKeyPair) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) WalletKeyPair(snowblossom.proto.WalletKeyPair) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) ECParameterSpec(java.security.spec.ECParameterSpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec)

Example 19 with WalletKeyPair

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);
    }
}
Also used : KeyPair(java.security.KeyPair) WalletKeyPair(snowblossom.proto.WalletKeyPair) ByteString(com.google.protobuf.ByteString) Cipher(javax.crypto.Cipher)

Aggregations

WalletKeyPair (snowblossom.proto.WalletKeyPair)19 ByteString (com.google.protobuf.ByteString)9 Test (org.junit.Test)8 KeyPair (java.security.KeyPair)2 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)2 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)2 AddressSpec (snowblossom.proto.AddressSpec)2 X509Certificate (java.security.cert.X509Certificate)1 ECParameterSpec (java.security.spec.ECParameterSpec)1 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 Cipher (javax.crypto.Cipher)1 ChildNumber (org.bitcoinj.crypto.ChildNumber)1 DeterministicHierarchy (org.bitcoinj.crypto.DeterministicHierarchy)1 DeterministicKey (org.bitcoinj.crypto.DeterministicKey)1