Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for SubzeroUtils.derivePublicKey function #712

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.VerificationException;
import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.crypto.HDKeyDerivation;
import org.bitcoinj.params.TestNet3Params;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static com.squareup.subzero.shared.SubzeroUtils.ERROR_ENCRYPTED_MASTER_SEED_SIZE;
Expand Down Expand Up @@ -109,7 +111,7 @@ public void setUp() {
assertEquals("2MuAdStu2xZtRSyA5B6wRtj7SmaLjDyfm1H", address.toString());
}

@Test public void derivePublicKey() {
@Test public void testDerivePublicKeyRandom() {
// Using https://iancoleman.io/bip39/, I created a random key. The derivation we use for cold
// wallet is m/coin_type'/change/address_index. The coin_type' is taken care of for us,
// so we only care about change/address_index.
Expand Down Expand Up @@ -139,6 +141,38 @@ public void setUp() {
assertArrayEquals(expected, childKey.getPubKey());
}

@Test public void testDerivePublicKeyNegativeIndex() {
DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]);
Path invalidPath = Path.newBuilder().setIsChange(false).setIndex(-1).build();

Exception exception = assertThrows(IllegalStateException.class, () -> {
SubzeroUtils.derivePublicKey(rootKey, invalidPath);
});

assertEquals("index should be between 0 and 2^31-1", exception.getMessage());
}

@Test public void testDerivePublicKeyMaxIndex() {
DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]);
// Integer.MAX_VALUE is 2^31-1.
Path path = Path.newBuilder().setIsChange(false).setIndex(Integer.MAX_VALUE).build();

DeterministicKey derivedKey = SubzeroUtils.derivePublicKey(rootKey, path);
assertEquals(33, derivedKey.getPubKey().length);
}

@Test public void testDerivePublicKeyReproducible() {
DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]);
Path path = Path.newBuilder().setIsChange(false).setIndex(37).build();

DeterministicKey key1 = SubzeroUtils.derivePublicKey(rootKey, path);
DeterministicKey key2 = SubzeroUtils.derivePublicKey(rootKey, path);

assertNotNull(key1);
assertNotNull(key2);
assertEquals(key1, key2);
}

@Test
public void testValidateFees() {
String feeExceedsLimitString = "Transaction fee is greater than 1BTC and 10% of the total "
Expand Down
Loading