@Test public void testWalletConstructor() throws IOException, InvalidCipherTextException { DefaultConfig config = new DefaultConfig(); Wallet wallet = new Wallet(config); log.debug(wallet.toString()); assertNotNull(wallet); byte[] data = "sign data".getBytes(); byte[] signature = wallet.sign(data); boolean verifyResult = wallet.verify(data, signature); assertTrue(verifyResult); }
public TransactionSignature(Wallet wallet, byte[] headerHash) { this(wallet.signHashedData(headerHash)); }
@Override public String toString() { return "Wallet{" + "keyPath=" + keyPath + ", keyName=" + keyName + ", address=" + getHexAddress() + ", publicKey=" + Hex.toHexString(publicKey) + '}'; }
@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()); }
decryptKeyFileInit(keyPath, keyName, keyPassword); } catch (Exception e) { logger.debug("Key file is not exist. Create New key file."); encryptKeyFileInit(key, keyPath, keyName, keyPassword); } catch (IOException ioe) { logger.error("Cannot generate the Key file at " + keyPath + keyName);
@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); }
@Bean Wallet wallet(DefaultConfig defaultConfig) throws IOException, InvalidCipherTextException { return new Wallet(defaultConfig); } }
@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()); }
/** * Verify the signature as static * * @param data signed data * @param signature signature * @param hashed whether hashed data or not * @return verification result */ public static boolean verify(byte[] data, byte[] signature, boolean hashed) { return verify(data, signature, hashed, null); }
@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); }
public static JsonObject signBranch(Wallet wallet, JsonObject raw) { if (!raw.has("signature")) { raw.addProperty("owner", wallet.getHexAddress()); Sha3Hash hashForSign = new Sha3Hash(raw.toString().getBytes(StandardCharsets.UTF_8)); byte[] signature = wallet.signHashedData(hashForSign.getBytes()); raw.addProperty("signature", Hex.toHexString(signature)); } return raw; } }
@Test public void testWalletAndConfig() throws IOException, InvalidCipherTextException { DefaultConfig config = new DefaultConfig(); Wallet wallet = new Wallet(config); Path path = Paths.get(config.getKeyPath()); String keyPath = path.getParent().toString(); String keyName = path.getFileName().toString(); log.debug("walletKeyPath: " + wallet.getKeyPath()); log.debug("walletKeyName: " + wallet.getKeyName()); log.debug("configKeyPath: " + keyPath); log.debug("configKeyName: " + keyName); assertEquals(wallet.getKeyPath(), keyPath); assertEquals(wallet.getKeyName(), keyName); } }
@Test public void testGetAddressWithWalletAccount() throws IOException, InvalidCipherTextException { Account account = new Account(); log.debug("Account: " + account.toString()); log.debug("Account.address: " + Hex.toHexString(account.getAddress())); Wallet wallet = new Wallet(account.getKey(), "tmp/path", "nodePri.key", "Aa1234567890!"); log.debug("Wallet: " + wallet.toString()); log.debug("Wallet.address: " + Hex.toHexString(wallet.getAddress())); TransactionHusk tx1 = createTx(wallet); TransactionHusk tx2 = createTx(wallet); log.debug("Test Transaction1: " + tx1.toString()); log.debug("Test Transaction1 Address: " + tx1.getAddress()); log.debug("Test Transaction2: " + tx2.toString()); log.debug("Test Transaction2 Address: " + tx2.getAddress()); assertThat(wallet.getAddress()).isEqualTo(account.getAddress()); assertThat(tx1.getAddress()).isEqualTo(tx2.getAddress()); assertThat(account.getAddress()).isEqualTo(tx1.getAddress().getBytes()); }
@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 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()); }
@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 constructorTest() { Block block = new TestUtils(wallet).sampleBlock(); List<String> consensusList = new ArrayList<>(); consensusList.add(Hex.toHexString(wallet.sign(block.getHash()))); BlockCon blockCon1 = new BlockCon(block.getHeader().getIndex(), block.getHeader().getPrevBlockHash(), block, consensusList); assertTrue(BlockCon.verify(blockCon1)); BlockCon blockCon2 = new BlockCon(blockCon1.toBinary()); assertTrue(BlockCon.verify(blockCon2)); assertTrue(blockCon1.equals(blockCon2)); } }
/** * Wallet Constructor as loading the key file. * * @param keyPath keyPath(directory) * @param keyName keyName * @param password password * @throws IOException IOException * @throws InvalidCipherTextException InvalidCipherTextException */ public Wallet(String keyPath, String keyName, String password) throws IOException, InvalidCipherTextException { decryptKeyFileInit(keyPath, keyName, password); }
/** * Wallet Constructor(generate key file). * * @param key ECKey * @param keyPath keyPath(directory) * @param keyName keyName * @param password password * @throws IOException IOException * @throws InvalidCipherTextException InvalidCipherTextException */ public Wallet(ECKey key, String keyPath, String keyName, String password) throws IOException, InvalidCipherTextException { if (!Password.passwordValid(password)) { logger.error("Invalid Password"); throw new IOException("Invalid Password"); } encryptKeyFileInit(key, keyPath, keyName, password); }