This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| byte[] ip = subnetLocalhost.getAddress(); | |
| for (int i=1; i<255; i++) { | |
| ip[3] = (byte) i; | |
| InetAddress address = InetAddress.getByAddress(ip); | |
| // Comprobar que se añade esta máquina | |
| if (address.equals(localhost)) continue; | |
| // Intentar abrir socket TCP | |
| connectToServer(address); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| new Thread(() -> { | |
| server = new ServerSocket(Cutrecoin.PORT); | |
| while (isRunning) acceptClient(server.accept()); | |
| }).start(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void mine() { | |
| while (!isValidHash()) { | |
| nonce++; | |
| recalculateHash(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private final long index; | |
| private final ArrayList<Transaction> transactions; | |
| private long nonce = 0; | |
| private String hash; | |
| private final String previous_hash; | |
| private final PublicKey miner; | |
| private final float fee; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public float getBalance(PublicKey addr) { | |
| float balance = 0; | |
| for (Block b : chain) { | |
| // Añadir saldo de transacciones | |
| for (Transaction t : b.getTransactions()) { | |
| if (addr.equals(t.getFrom())) { | |
| // En caso de ser el pagador | |
| balance -= t.getAmount(); | |
| } else if (addr.equals(t.getTo())) { | |
| // En caso de ser el receptor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public boolean sign(PrivateKey pk) { | |
| // Comprobar que no está firmada la transacción | |
| if (signature != null) return false; | |
| // Comprobar que la clave privada se corresponde con esta transacción | |
| PublicKey expected = getPublicFromPrivate(pk); | |
| if (!expected.equals(from)) return false; | |
| // Intentar firmar la transacción | |
| try { | |
| Signature sig = Signature.getInstance("SHA1WithRSA"); | |
| // Especificar clave privada |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private final PublicKey from; | |
| private final PublicKey to; | |
| private final float amount; | |
| private final Date timestamp; | |
| private byte[] signature = null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static String keyToBase64(Key key) { | |
| return Base64.getEncoder().encodeToString(key.getEncoded()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static KeyPair generateKeyPair() { | |
| KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); | |
| kpg.initialize(1024); | |
| return kpg.genKeyPair(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Detectar Developer Tools abierto | |
| var tolerance = 300; | |
| var dev_open = (diff > tolerance); | |
| // Mostrar mensaje de error | |
| if (dev_open) { | |
| document.body.innerHTML = 'Cierra las Developer Tools y recarga la web'; | |
| } |