Skip to content

Commit

Permalink
Update duck32 exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Oct 31, 2020
1 parent b17f71b commit eaa4361
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
33 changes: 20 additions & 13 deletions lib/src/Duck32.java
Expand Up @@ -169,24 +169,31 @@ private static String convertBytesToBase32(ByteString input)
private static ByteString convertBase32ToBytes(String encoding)
throws ValidationException
{
BigInteger big = new BigInteger(convertBech32ToBase32(encoding), 32);
byte[] data = big.toByteArray();
try
{
BigInteger big = new BigInteger(convertBech32ToBase32(encoding), 32);
byte[] data = big.toByteArray();

int data_size=HASH_BYTES + Globals.ADDRESS_SPEC_HASH_LEN;
int data_size=HASH_BYTES + Globals.ADDRESS_SPEC_HASH_LEN;


// Helpful biginteger might throw an extra zero byte on the front to show positive sign
// or it might start with a lot of zeros and be short so add them back in
int start = data.length - data_size;
if (start >= 0)
{
return ByteString.copyFrom(data, start, Globals.ADDRESS_SPEC_HASH_LEN + HASH_BYTES);
// Helpful biginteger might throw an extra zero byte on the front to show positive sign
// or it might start with a lot of zeros and be short so add them back in
int start = data.length - data_size;
if (start >= 0)
{
return ByteString.copyFrom(data, start, Globals.ADDRESS_SPEC_HASH_LEN + HASH_BYTES);
}
else
{
byte[] zeros = new byte[data_size];
int needed_zeros = data_size - data.length;
return ByteString.copyFrom(zeros, 0, needed_zeros).concat(ByteString.copyFrom(data));
}
}
else
catch(NumberFormatException e)
{
byte[] zeros = new byte[data_size];
int needed_zeros = data_size - data.length;
return ByteString.copyFrom(zeros, 0, needed_zeros).concat(ByteString.copyFrom(data));
throw new ValidationException(e);
}
}

Expand Down
9 changes: 7 additions & 2 deletions lib/src/SignatureUtil.java
Expand Up @@ -14,7 +14,7 @@
public class SignatureUtil
{
/**
* The public key sent on the network shall be just "03" plus the 32 bytes of X
* The public key sent on the network shall be just "03" or "02" plus the 32 bytes of X
* The curve is assumed to be secp256k1. Just like bitcoin.
*/
public static final int SIG_TYPE_ECDSA_COMPRESSED=1; //secp256k1 only
Expand Down Expand Up @@ -84,6 +84,11 @@ public static boolean checkSignature(SigSpec sig_spec, ByteString signed_data, B
PublicKey pub_key = decodePublicKey(sig_spec);
String algo=getAlgo(sig_type);

/*if (algo.equals("ECDSA"))
{
algo="SHA1WithECDSA";
}*/

try
{

Expand Down Expand Up @@ -140,7 +145,7 @@ public static int estimateSignatureBytes(int sig_type)
{
if (sig_type == SIG_TYPE_ECDSA_COMPRESSED)
{
return 70;
return 71;
}
if (sig_type == SIG_TYPE_ECDSA)
{
Expand Down
38 changes: 24 additions & 14 deletions lib/test/KeyUtilTest.java
Expand Up @@ -14,6 +14,7 @@
import snowblossom.lib.Globals;
import snowblossom.lib.KeyUtil;
import snowblossom.lib.SignatureUtil;
import snowblossom.lib.HexUtil;
import snowblossom.proto.SigSpec;
import snowblossom.proto.WalletKeyPair;

Expand All @@ -31,15 +32,19 @@ public static void loadProvider()
public void testCompressKeyEncoding()
throws Exception
{
KeyPair pair = KeyUtil.generateECCompressedKey();
for(int i=0; i<100; i++)
{
KeyPair pair = KeyUtil.generateECCompressedKey();

ByteString encoded = KeyUtil.getCompressedPublicKeyEncoding(pair.getPublic());
ByteString encoded = KeyUtil.getCompressedPublicKeyEncoding(pair.getPublic());

Assert.assertEquals(33, encoded.size());
Assert.assertEquals(33, encoded.size());
//System.out.println("Comp Byte: " + encoded.byteAt(0));

PublicKey k = KeyUtil.convertCompressedECDSA(encoded);
PublicKey k = KeyUtil.convertCompressedECDSA(encoded);

Assert.assertEquals(k, pair.getPublic());
Assert.assertEquals(k, pair.getPublic());
}
}

@Test
Expand Down Expand Up @@ -145,19 +150,24 @@ private void testKeyPair(WalletKeyPair wkp, String name)
{
Random rnd = new Random();
byte[] b = new byte[Globals.BLOCKCHAIN_HASH_LEN];
rnd.nextBytes(b);
for(int i=0; i<8; i++)
{
rnd.nextBytes(b);

ChainHash hash = new ChainHash(b);
ChainHash hash = new ChainHash(b);

ByteString sig = SignatureUtil.sign(wkp, hash);
SigSpec sig_spec = SigSpec.newBuilder()
.setSignatureType(wkp.getSignatureType())
.setPublicKey(wkp.getPublicKey())
.build();
ByteString sig = SignatureUtil.sign(wkp, hash);
SigSpec sig_spec = SigSpec.newBuilder()
.setSignatureType(wkp.getSignatureType())
.setPublicKey(wkp.getPublicKey())
.build();

logger.info(String.format("Key report %s Pub size: %d, sig %d", name, wkp.getPublicKey().size(), sig.size()));
logger.info(String.format("Key report %s Pub size: %d, sig %d", name, wkp.getPublicKey().size(), sig.size()));
logger.info("Key report: " + HexUtil.getHexString( sig));
//logger.info("Key report: " + KeyUtil.decomposeASN1Encoded( sig ));

Assert.assertTrue(SignatureUtil.checkSignature(sig_spec, hash.getBytes(), sig));
Assert.assertTrue(SignatureUtil.checkSignature(sig_spec, hash.getBytes(), sig));
}


}
Expand Down

0 comments on commit eaa4361

Please sign in to comment.