diff --git a/drongo b/drongo index d0a75fd2..f88e3d44 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit d0a75fd26809d47fddf0c432bd8f6e6a9a522c64 +Subproject commit f88e3d442357e514421b993155062f7ea8177d06 diff --git a/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java b/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java index 5c3fc1e4..ab05c008 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java @@ -2,16 +2,12 @@ package com.sparrowwallet.sparrow.io; import com.github.arteam.simplejsonrpc.client.*; import com.github.arteam.simplejsonrpc.client.builder.BatchRequestBuilder; -import com.github.arteam.simplejsonrpc.client.generator.CurrentTimeIdGenerator; -import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcMethod; -import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcParam; -import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcService; import com.google.common.net.HostAndPort; import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.protocol.Transaction; -import com.sparrowwallet.drongo.wallet.TransactionReference; +import com.sparrowwallet.drongo.wallet.BlockchainTransactionHash; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletNode; import javafx.concurrent.Service; @@ -101,13 +97,13 @@ public class ElectrumServer { Optional optionalNode = nodes.stream().filter(n -> n.getDerivationPath().equals(path)).findFirst(); if(optionalNode.isPresent()) { WalletNode node = optionalNode.get(); - Set references = Arrays.stream(txes).map(ScriptHashTx::getTransactionReference).collect(Collectors.toSet()); + Set references = Arrays.stream(txes).map(ScriptHashTx::getBlockchainTransactionHash).collect(Collectors.toSet()); - for(TransactionReference reference : references) { + for(BlockchainTransactionHash reference : references) { if(!node.getHistory().add(reference)) { - Optional optionalReference = node.getHistory().stream().filter(tr -> tr.getTransactionId().equals(reference.getTransactionId())).findFirst(); + Optional optionalReference = node.getHistory().stream().filter(tr -> tr.getHash().equals(reference.getHash())).findFirst(); if(optionalReference.isPresent()) { - TransactionReference existingReference = optionalReference.get(); + BlockchainTransactionHash existingReference = optionalReference.get(); if(existingReference.getHeight() < reference.getHeight()) { node.getHistory().remove(existingReference); node.getHistory().add(reference); @@ -131,29 +127,30 @@ public class ElectrumServer { public void getReferencedTransactions(Wallet wallet, KeyPurpose keyPurpose) throws ServerException { WalletNode purposeNode = wallet.getNode(keyPurpose); - Set references = new HashSet<>(); + Set references = new HashSet<>(); for(WalletNode addressNode : purposeNode.getChildren()) { references.addAll(addressNode.getHistory()); } - Map transactionMap = getTransactions(references); + Map transactionMap = getTransactions(references); wallet.getTransactions().putAll(transactionMap); } - public Map getTransactions(Set references) throws ServerException { + public Map getTransactions(Set references) throws ServerException { try { JsonRpcClient client = new JsonRpcClient(getTransport()); BatchRequestBuilder batchRequest = client.createBatchRequest().keysType(String.class).returnType(String.class); - for(TransactionReference reference : references) { - batchRequest.add(reference.getTransactionId(), "blockchain.transaction.get", reference.getTransactionId()); + for(BlockchainTransactionHash reference : references) { + batchRequest.add(reference.getHashAsString(), "blockchain.transaction.get", reference.getHashAsString()); } Map result = batchRequest.execute(); - Map transactionMap = new HashMap<>(); + Map transactionMap = new HashMap<>(); for(String txid : result.keySet()) { + Sha256Hash hash = Sha256Hash.wrap(txid); byte[] rawtx = Utils.hexToBytes(result.get(txid)); Transaction transaction = new Transaction(rawtx); - transactionMap.put(txid, transaction); + transactionMap.put(hash, transaction); } return transactionMap; @@ -175,8 +172,9 @@ public class ElectrumServer { public String tx_hash; public long fee; - public TransactionReference getTransactionReference() { - return new TransactionReference(tx_hash, height, fee); + public BlockchainTransactionHash getBlockchainTransactionHash() { + Sha256Hash hash = Sha256Hash.wrap(tx_hash); + return new BlockchainTransactionHash(hash, height, fee); } @Override @@ -189,16 +187,6 @@ public class ElectrumServer { } } - @JsonRpcService - @JsonRpcId(CurrentTimeIdGenerator.class) - @JsonRpcParams(ParamsType.MAP) - private interface ELectrumXService { - - @JsonRpcMethod("blockchain.scripthash.get_history") - List getHistory(@JsonRpcParam("scripthash") String scriptHash); - - } - private static class TcpTransport implements Transport { private static final int DEFAULT_PORT = 50001; diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index 525c80bd..880b5f25 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -5,6 +5,7 @@ import com.sparrowwallet.drongo.ExtendedKey; import com.sparrowwallet.drongo.SecureString; import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.crypto.*; +import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.wallet.Keystore; import com.sparrowwallet.drongo.wallet.MnemonicException; @@ -59,6 +60,8 @@ public class Storage { gsonBuilder.registerTypeAdapter(ExtendedKey.class, new ExtendedPublicKeyDeserializer()); gsonBuilder.registerTypeAdapter(byte[].class, new ByteArraySerializer()); gsonBuilder.registerTypeAdapter(byte[].class, new ByteArrayDeserializer()); + gsonBuilder.registerTypeAdapter(Sha256Hash.class, new Sha256HashSerializer()); + gsonBuilder.registerTypeAdapter(Sha256Hash.class, new Sha256HashDeserializer()); gsonBuilder.registerTypeAdapter(Transaction.class, new TransactionSerializer()); gsonBuilder.registerTypeAdapter(Transaction.class, new TransactionDeserializer()); if(includeWalletSerializers) { @@ -271,6 +274,20 @@ public class Storage { } } + private static class Sha256HashSerializer implements JsonSerializer { + @Override + public JsonElement serialize(Sha256Hash src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.toString()); + } + } + + private static class Sha256HashDeserializer implements JsonDeserializer { + @Override + public Sha256Hash deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return Sha256Hash.wrap(json.getAsJsonPrimitive().getAsString()); + } + } + private static class TransactionSerializer implements JsonSerializer { @Override public JsonElement serialize(Transaction src, Type typeOfSrc, JsonSerializationContext context) {