Some work on addresses and private keys that still doesn't work. As a side effect, sending objects now works basically.

This commit is contained in:
2015-05-01 17:14:43 +02:00
parent 8d1466f6f4
commit a65907f13b
29 changed files with 414 additions and 138 deletions

View File

@@ -16,14 +16,14 @@
package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.entity.payload.V3Pubkey;
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
import ch.dissem.bitmessage.utils.Base58;
import ch.dissem.bitmessage.utils.Bytes;
import ch.dissem.bitmessage.utils.Security;
import ch.dissem.bitmessage.utils.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import static org.junit.Assert.*;
public class BitmessageAddressTest {
@Test
@@ -49,24 +49,59 @@ public class BitmessageAddressTest {
assertNotNull(address.getPubkey());
}
@Test
public void testV3() {
// ripe 007402be6e76c3cb87caa946d0c003a3d4d8e1d5
// publicSigningKey in hex: 0435e3f10f4884ec42f11f1a815ace8c7c4575cad455ca98db19a245c4c57baebdce990919b647f2657596b75aa939b858bd70c55a03492dd95119bef009cf9eea
// publicEncryptionKey in hex: 04bf30a7ee7854f9381332a6285659215a6a4b2ab3479fa87fe996f7cd11710367748371d8d2545f8466964dd3140ab80508b2b18e45616ef6cc4d8e54db923761
BitmessageAddress address = new BitmessageAddress("BM-2D9Vc5rFxxR5vTi53T9gkLfemViHRMVLQZ");
V3Pubkey pubkey = new V3Pubkey.Builder()
.stream(1)
.publicSigningKey(Bytes.fromHex("0435e3f10f4884ec42f11f1a815ace8c7c4575cad455ca98db19a245c4c57baebdce990919b647f2657596b75aa939b858bd70c55a03492dd95119bef009cf9eea"))
.publicEncryptionKey(Bytes.fromHex("04bf30a7ee7854f9381332a6285659215a6a4b2ab3479fa87fe996f7cd11710367748371d8d2545f8466964dd3140ab80508b2b18e45616ef6cc4d8e54db923761"))
.build();
address.setPubkey(pubkey);
assertArrayEquals(Bytes.fromHex("007402be6e76c3cb87caa946d0c003a3d4d8e1d5"), address.getRipe());
}
@Test
public void testV3PubkeyImport() throws IOException {
ObjectMessage object = TestUtils.loadObjectMessage(3, "V3Pubkey.payload");
V3Pubkey pubkey = (V3Pubkey) object.getPayload();
BitmessageAddress address = new BitmessageAddress("BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn");
address.setPubkey(pubkey);
}
@Test
public void testV3Import() {
assertEquals(3, new BitmessageAddress("BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn").getVersion());
assertEquals(1, new BitmessageAddress("BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn").getStream());
String address_string = "BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn";
assertEquals(3, new BitmessageAddress(address_string).getVersion());
assertEquals(1, new BitmessageAddress(address_string).getStream());
byte[] privsigningkey = Base58.decode("5KU2gbe9u4rKJ8PHYb1rvwMnZnAJj4gtV5GLwoYckeYzygWUzB9");
byte[] privencryptionkey = Base58.decode("5KHd4c6cavd8xv4kzo3PwnVaYuBgEfg7voPQ5V97aZKgpYBXGck");
assertEquals((byte) 0x80, privsigningkey[0]);
assertEquals((byte) 0x80, privencryptionkey[0]);
privsigningkey = Bytes.subArray(privsigningkey, 1, privsigningkey.length - 5);
privencryptionkey = Bytes.subArray(privencryptionkey, 1, privencryptionkey.length - 5);
byte[] privsigningkey = getSecret("5KU2gbe9u4rKJ8PHYb1rvwMnZnAJj4gtV5GLwoYckeYzygWUzB9");
byte[] privencryptionkey = getSecret("5KHd4c6cavd8xv4kzo3PwnVaYuBgEfg7voPQ5V97aZKgpYBXGck");
privsigningkey = Bytes.expand(privsigningkey, 32);
privencryptionkey = Bytes.expand(privencryptionkey, 32);
System.out.println("\n\n" + Strings.hex(privsigningkey) + "\n\n");
// privsigningkey = Bytes.expand(privsigningkey, 32);
// privencryptionkey = Bytes.expand(privencryptionkey, 32);
BitmessageAddress address = new BitmessageAddress(new PrivateKey(privsigningkey, privencryptionkey,
Security.createPubkey(3, 1, privsigningkey, privencryptionkey, 320, 14000)));
assertEquals("BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn", address.getAddress());
assertEquals(address_string, address.getAddress());
}
private byte[] getSecret(String walletImportFormat) {
byte[] bytes = Base58.decode("5KU2gbe9u4rKJ8PHYb1rvwMnZnAJj4gtV5GLwoYckeYzygWUzB9");
assertEquals(37, bytes.length);
assertEquals((byte) 0x80, bytes[0]);
byte[] checksum = Bytes.subArray(bytes, bytes.length - 4, 4);
byte[] secret = Bytes.subArray(bytes, 1, 32);
// assertArrayEquals("Checksum failed", checksum, Bytes.subArray(Security.doubleSha512(new byte[]{(byte) 0x80}, secret, new byte[]{0x01}), 0, 4));
byte[] result = new byte[33];
result[0] = 0x04;
System.arraycopy(secret, 0, result, 1, secret.length);
return result;
}
@Test

View File

@@ -18,6 +18,7 @@ package ch.dissem.bitmessage.entity;
import ch.dissem.bitmessage.entity.payload.*;
import ch.dissem.bitmessage.factory.Factory;
import ch.dissem.bitmessage.utils.TestUtils;
import org.junit.Test;
import java.io.ByteArrayInputStream;
@@ -69,7 +70,7 @@ public class SerializationTest {
}
private void doTest(String resourceName, int version, Class<?> expectedPayloadType) throws IOException {
byte[] data = getBytes(resourceName);
byte[] data = TestUtils.getBytes(resourceName);
InputStream in = new ByteArrayInputStream(data);
ObjectMessage object = Factory.getObjectMessage(version, in, data.length);
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -77,16 +78,4 @@ public class SerializationTest {
assertArrayEquals(data, out.toByteArray());
assertEquals(expectedPayloadType.getCanonicalName(), object.getPayload().getClass().getCanonicalName());
}
private byte[] getBytes(String resourceName) throws IOException {
InputStream in = getClass().getClassLoader().getResourceAsStream(resourceName);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
return out.toByteArray();
}
}

View File

@@ -32,7 +32,14 @@ public class BytesTest {
public static final Random rnd = new Random();
@Test
public void testIncrement() throws IOException {
public void ensureExpandsCorrectly() {
byte[] source = {1};
byte[] expected = {0,1};
assertArrayEquals(expected, Bytes.expand(source, 2));
}
@Test
public void ensureIncrementCarryWorks() throws IOException {
byte[] bytes = {0, -1};
Bytes.inc(bytes);
assertArrayEquals(TestUtils.int16(256), bytes);

View File

@@ -29,6 +29,7 @@ public class StringsTest {
@Test
public void testHexString() {
assertEquals("0x48656c6c6f21", Strings.hex("Hello!".getBytes()));
assertEquals("48656c6c6f21", Strings.hex("Hello!".getBytes()).toString());
assertEquals("0001", Strings.hex(new byte[]{0, 1}).toString());
}
}

View File

@@ -16,8 +16,13 @@
package ch.dissem.bitmessage.utils;
import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.factory.Factory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* If there's ever a need for this in production code, it should be rewritten to be more efficient.
@@ -28,4 +33,22 @@ public class TestUtils {
Encode.int16(number, out);
return out.toByteArray();
}
public static ObjectMessage loadObjectMessage(int version, String resourceName) throws IOException {
byte[] data = getBytes(resourceName);
InputStream in = new ByteArrayInputStream(data);
return Factory.getObjectMessage(version, in, data.length);
}
public static byte[] getBytes(String resourceName) throws IOException {
InputStream in = TestUtils.class.getClassLoader().getResourceAsStream(resourceName);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
return out.toByteArray();
}
}