/** * Returns true if this pubkey is canonical, i.e. the correct length taking into wallet compression. * * @return - */ public boolean isPubKeyCanonical() { return isPubKeyCanonical(pub.getEncoded(/* uncompressed */ false)); }
@Test public void testIsPubKeyCanonicalWrongPrefix() { // Test wrong prefix 4, right length 65 byte[] nonCanonicalPubkey4 = new byte[65]; assertFalse(ECKey.isPubKeyCanonical(nonCanonicalPubkey4)); // Test wrong prefix 2, right length 33 byte[] nonCanonicalPubkey5 = new byte[33]; assertFalse(ECKey.isPubKeyCanonical(nonCanonicalPubkey5)); // Test wrong prefix 3, right length 33 byte[] nonCanonicalPubkey6 = new byte[33]; assertFalse(ECKey.isPubKeyCanonical(nonCanonicalPubkey6)); }
@Test public void testIsPubKeyCanonicalCorect() { // Test correct prefix 4, right length 65 byte[] canonicalPubkey1 = new byte[65]; canonicalPubkey1[0] = 0x04; assertTrue(ECKey.isPubKeyCanonical(canonicalPubkey1)); // Test correct prefix 2, right length 33 byte[] canonicalPubkey2 = new byte[33]; canonicalPubkey2[0] = 0x02; assertTrue(ECKey.isPubKeyCanonical(canonicalPubkey2)); // Test correct prefix 3, right length 33 byte[] canonicalPubkey3 = new byte[33]; canonicalPubkey3[0] = 0x03; assertTrue(ECKey.isPubKeyCanonical(canonicalPubkey3)); }
@Test public void testIsPubKeyCanonicalWrongLength() { // Test correct prefix 4, but wrong length !65 byte[] nonCanonicalPubkey1 = new byte[64]; nonCanonicalPubkey1[0] = 0x04; assertFalse(ECKey.isPubKeyCanonical(nonCanonicalPubkey1)); // Test correct prefix 2, but wrong length !33 byte[] nonCanonicalPubkey2 = new byte[32]; nonCanonicalPubkey2[0] = 0x02; assertFalse(ECKey.isPubKeyCanonical(nonCanonicalPubkey2)); // Test correct prefix 3, but wrong length !33 byte[] nonCanonicalPubkey3 = new byte[32]; nonCanonicalPubkey3[0] = 0x03; assertFalse(ECKey.isPubKeyCanonical(nonCanonicalPubkey3)); }
@Test public void testECKey() { ECKey key = new ECKey(); assertTrue(key.isPubKeyCanonical()); assertNotNull(key.getPubKey()); assertNotNull(key.getPrivKeyBytes()); log.debug(Hex.toHexString(key.getPrivKeyBytes()) + " :Generated privkey"); log.debug(Hex.toHexString(key.getPubKey()) + " :Generated pubkey"); }
@Test public void testFromPrivateKey() { ECKey key = ECKey.fromPrivate(privateKey); assertTrue(key.isPubKeyCanonical()); assertTrue(key.hasPrivKey()); assertArrayEquals(pubKey, key.getPubKey()); }
@Test public void testIsPubKeyOnly() { ECKey key = ECKey.fromPublicOnly(pubKey); assertTrue(key.isPubKeyCanonical()); assertTrue(key.isPubKeyOnly()); assertArrayEquals(key.getPubKey(), pubKey); }