@Test public void testKeySave() throws IOException, InvalidCipherTextException { // generate key & save a file Wallet wt = new Wallet(null, "/tmp/", "nodePri.key", "Password1234!"); byte[] encData = FileUtil.readFile(wt.getKeyPath(), wt.getKeyName()); log.debug("path:" + wt.getKeyPath() + wt.getKeyName()); log.debug("encData:" + Hex.toHexString(encData)); log.debug("pubKey:" + Hex.toHexString(wt.getPubicKey())); // load key Wallet wt2 = new Wallet("/tmp/", wt.getKeyName(), "Password1234!"); log.debug("pubKey2:" + Hex.toHexString(wt2.getPubicKey())); assertArrayEquals(wt.getPubicKey(), wt2.getPubicKey()); }
@Test public void testWalletSignAndVerify() throws IOException, InvalidCipherTextException { Wallet wallet = new Wallet(null, "tmp/temp", "temp.key", "Aa1234567890!"); byte[] plain = "test data 1111".getBytes(); log.debug("Plain data: " + new String(plain)); byte[] signature = wallet.sign(plain); log.debug("Signature: " + Hex.toHexString(signature)); boolean verifyResult = wallet.verify(plain, signature); log.debug("Verify Result: " + verifyResult); assertTrue(verifyResult); verifyResult = wallet.verifyHashedData(HashUtil.sha3(plain), signature); log.debug("Verify Result: " + verifyResult); assertTrue(verifyResult); verifyResult = Wallet.verify(HashUtil.sha3(plain), signature, true); log.debug("Verify Result: " + verifyResult); assertTrue(verifyResult); verifyResult = Wallet.verify(plain, signature, false); log.debug("Verify Result: " + verifyResult); assertTrue(verifyResult); verifyResult = Wallet.verify(plain, signature, false, wallet.getPubicKey()); log.debug("Verify Result: " + verifyResult); assertTrue(verifyResult); }
@Before public void setUp() throws Exception { JsonObject jsonParam1 = new JsonObject(); jsonParam1.addProperty("address", "5db10750e8caff27f906b41c71b3471057dd2000"); jsonParam1.addProperty("amount", "10000000"); JsonObject jsonObject1 = new JsonObject(); jsonObject1.addProperty("method", "transfer"); jsonObject1.add("params", jsonParam1); JsonObject jsonParam2 = new JsonObject(); jsonParam2.addProperty("address", "5db10750e8caff27f906b41c71b3471057dd2001"); jsonParam2.addProperty("amount", "5000000"); JsonObject jsonObject2 = new JsonObject(); jsonObject2.addProperty("method", "transfer"); jsonObject2.add("params", jsonParam2); JsonArray jsonArray = new JsonArray(); jsonArray.add(jsonObject1); jsonArray.add(jsonObject2); txBody = new TransactionBody(jsonArray); byte[] chain = new byte[20]; byte[] version = new byte[8]; byte[] type = new byte[8]; long timestamp = TimeUtils.time(); txHeader = new TransactionHeader(chain, version, type, timestamp, txBody); wallet = new Wallet(); log.debug("wallet.pubKey=" + Hex.toHexString(wallet.getPubicKey())); txSig = new TransactionSignature(wallet, txHeader.getHashForSigning()); tx1 = new Transaction(txHeader, txSig.getSignature(), txBody); assertTrue(tx1.verify()); }
@Test public void testBlockKey() throws Exception { Block block2 = block1.clone(); log.debug("block2 pubKey=" + block2.getPubKeyHexString()); assertThat(block1.getPubKeyHexString()).isEqualTo(block2.getPubKeyHexString()); assertThat(block1.getPubKey()).isEqualTo(block2.getPubKey()); assertThat(block1.getPubKey()).isEqualTo(wallet.getPubicKey()); log.debug("block1 author address=" + block1.getAddressHexString()); log.debug("block2 author address=" + block2.getAddressHexString()); log.debug("wallet address=" + wallet.getHexAddress()); assertThat(block1.getAddressHexString()).isEqualTo(block2.getAddressHexString()); assertThat(block1.getAddressHexString()).isEqualTo(wallet.getHexAddress()); assertThat(block1.verify()).isTrue(); assertThat(block2.verify()).isTrue(); } }
@Test public void testTransactionKey() throws Exception { Transaction tx2 = tx1.clone(); log.debug("tx2 pubKey=" + tx2.getPubKeyHexString()); log.debug("tx2 headerHash=" + Hex.toHexString(tx2.getHeader().getHashForSigning())); log.debug("tx2 pubKey=" + Hex.toHexString(tx2.getPubKey())); assertEquals(tx1.getPubKeyHexString(), tx2.getPubKeyHexString()); assertArrayEquals(tx1.getPubKey(), tx2.getPubKey()); assertArrayEquals(tx1.getPubKey(), wallet.getPubicKey()); log.debug("tx1 address=" + tx1.getAddressToString()); log.debug("tx2 address=" + tx2.getAddressToString()); log.debug("wallet address=" + wallet.getHexAddress()); log.debug("wallet signature=" + Hex.toHexString( wallet.signHashedData(tx1.getHeader().getHashForSigning()))); log.debug("wallet pubKey=" + Hex.toHexString( wallet.getPubicKey())); assertArrayEquals(tx1.getAddress(), tx2.getAddress()); assertArrayEquals(tx1.getAddress(), wallet.getAddress()); }
@Test public void testGetAddressWithSig() throws IOException, InvalidCipherTextException, SignatureException { Account account = new Account(); log.debug("Account: " + account.toString()); log.debug("Account.address: " + Hex.toHexString(account.getAddress())); log.debug("Account.pubKey: " + Hex.toHexString(account.getKey().getPubKey())); Wallet wallet = new Wallet(account.getKey(), "tmp/path", "nodePri.key", "Aa1234567890!"); log.debug("Wallet: " + wallet.toString()); log.debug("Wallet.address: " + Hex.toHexString(wallet.getAddress())); log.debug("Wallet.pubKey: " + Hex.toHexString(wallet.getPubicKey())); TransactionHusk txHusk1 = createTx(wallet); log.debug("Test Transaction1: " + txHusk1.toString()); log.debug("Test Transaction1 Address: " + txHusk1.getAddress()); assertThat(txHusk1.verify()).isTrue(); assertThat(wallet.getAddress()).isEqualTo(account.getAddress()); assertThat(wallet.getAddress()).isEqualTo(txHusk1.getAddress().getBytes()); byte[] hashedRawData = txHusk1.getHashForSigning().getBytes(); log.debug("hashedRawData: " + Hex.toHexString(hashedRawData)); byte[] signatureBin = txHusk1.getInstance().getSignature().toByteArray(); log.debug("signatureBin: " + Hex.toHexString(signatureBin)); ECKey.ECDSASignature ecdsaSignature = new ECKey.ECDSASignature(signatureBin); ECKey key = ECKey.signatureToKey(hashedRawData, ecdsaSignature); byte[] address = key.getAddress(); byte[] pubKey = key.getPubKey(); log.debug("address: " + Hex.toHexString(address)); log.debug("pubKey: " + Hex.toHexString(pubKey)); assertThat(account.getAddress()).isEqualTo(address); assertThat(account.getKey().getPubKey()).isEqualTo(pubKey); }
@Test public void testTransactionToProto() throws Exception { Transaction tx2 = tx1.clone(); log.debug("tx2 pubKey=" + tx2.getPubKeyHexString()); assertEquals(tx1.getPubKeyHexString(), tx2.getPubKeyHexString()); assertArrayEquals(tx1.getPubKey(), tx2.getPubKey()); assertArrayEquals(tx1.getPubKey(), wallet.getPubicKey()); log.debug("tx1 address=" + tx1.getAddressToString()); log.debug("tx2 address=" + tx2.getAddressToString()); log.debug("wallet address=" + wallet.getHexAddress()); assertArrayEquals(tx1.getAddress(), tx2.getAddress()); assertArrayEquals(tx1.getAddress(), wallet.getAddress()); Proto.Transaction protoTx1 = Transaction.toProtoTransaction(tx1); Proto.Transaction protoTx2 = Transaction.toProtoTransaction(tx2); log.debug("tx1 proto=" + Hex.toHexString(protoTx1.toByteArray())); log.debug("tx2 proto=" + Hex.toHexString(protoTx2.toByteArray())); assertArrayEquals(protoTx1.toByteArray(), protoTx2.toByteArray()); Transaction tx3 = Transaction.toTransaction(protoTx1); log.debug("tx1=" + tx1.toString()); log.debug("tx3=" + tx3.toString()); assertEquals(tx1.toString(), tx3.toString()); Proto.Transaction protoTx3 = Transaction.toProtoTransaction(tx1); assertArrayEquals(protoTx1.toByteArray(), protoTx3.toByteArray()); }