cormorant: support transaction.get without txindex, use step function to add bip47 addresses

This commit is contained in:
Craig Raw 2023-01-19 13:52:47 +02:00
parent e7ed82699c
commit 276cb8aecb
3 changed files with 57 additions and 13 deletions

View file

@ -133,7 +133,12 @@ public class BitcoindClient {
try { try {
getBitcoindService().loadWallet(CORE_WALLET_NAME, false); getBitcoindService().loadWallet(CORE_WALLET_NAME, false);
} catch(JsonRpcException e) { } catch(JsonRpcException e) {
getBitcoindService().unloadWallet(CORE_WALLET_NAME, false); try {
getBitcoindService().unloadWallet(CORE_WALLET_NAME, false);
} catch(JsonRpcException ex) {
//ignore
}
getBitcoindService().loadWallet(CORE_WALLET_NAME, false); getBitcoindService().loadWallet(CORE_WALLET_NAME, false);
} }
} }
@ -188,10 +193,15 @@ public class BitcoindClient {
for(Wallet childWallet : wallet.getChildWallets()) { for(Wallet childWallet : wallet.getChildWallets()) {
if(childWallet.isNested()) { if(childWallet.isNested()) {
Wallet copyChildWallet = childWallet.copy();
for(KeyPurpose keyPurpose : KeyPurpose.DEFAULT_PURPOSES) { for(KeyPurpose keyPurpose : KeyPurpose.DEFAULT_PURPOSES) {
for(WalletNode addressNode : childWallet.getNode(keyPurpose).getChildren()) { WalletNode purposeNode = copyChildWallet.getNode(keyPurpose);
int addressCount = purposeNode.getChildren().size();
int gapLimit = ((int)Math.floor(addressCount / 10.0) * 10) + 10;
purposeNode.fillToIndex(gapLimit - 1);
for(WalletNode addressNode : purposeNode.getChildren()) {
String addressOutputDescriptor = OutputDescriptor.toDescriptorString(addressNode.getAddress()); String addressOutputDescriptor = OutputDescriptor.toDescriptorString(addressNode.getAddress());
addOutputDescriptor(outputDescriptors, addressOutputDescriptor, childWallet, null, earliestBirthDate); addOutputDescriptor(outputDescriptors, addressOutputDescriptor, copyChildWallet, null, earliestBirthDate);
} }
} }
} }

View file

@ -43,6 +43,9 @@ public interface BitcoindClientService {
@JsonRpcMethod("getrawtransaction") @JsonRpcMethod("getrawtransaction")
Object getRawTransaction(@JsonRpcParam("txid") String txid, @JsonRpcParam("verbose") boolean verbose); Object getRawTransaction(@JsonRpcParam("txid") String txid, @JsonRpcParam("verbose") boolean verbose);
@JsonRpcMethod("gettransaction")
Map<String, Object> getTransaction(@JsonRpcParam("txid") String txid, @JsonRpcParam("verbose") boolean verbose);
@JsonRpcMethod("getmempoolentry") @JsonRpcMethod("getmempoolentry")
MempoolEntry getMempoolEntry(@JsonRpcParam("txid") String txid); MempoolEntry getMempoolEntry(@JsonRpcParam("txid") String txid);

View file

@ -13,10 +13,7 @@ import com.sparrowwallet.sparrow.net.cormorant.index.TxEntry;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@JsonRpcService @JsonRpcService
public class ElectrumServerService { public class ElectrumServerService {
@ -146,13 +143,47 @@ public class ElectrumServerService {
} }
@JsonRpcMethod("blockchain.transaction.get") @JsonRpcMethod("blockchain.transaction.get")
@SuppressWarnings("unchecked")
public Object getTransaction(@JsonRpcParam("tx_hash") String tx_hash, @JsonRpcParam("verbose") @JsonRpcOptional boolean verbose) throws BitcoindIOException, TransactionNotFoundException { public Object getTransaction(@JsonRpcParam("tx_hash") String tx_hash, @JsonRpcParam("verbose") @JsonRpcOptional boolean verbose) throws BitcoindIOException, TransactionNotFoundException {
try { if(verbose) {
return bitcoindClient.getBitcoindService().getRawTransaction(tx_hash, verbose); try {
} catch(JsonRpcException e) { return bitcoindClient.getBitcoindService().getRawTransaction(tx_hash, true);
throw new TransactionNotFoundException(e.getErrorMessage()); } catch(JsonRpcException e) {
} catch(IllegalStateException e) { try {
throw new BitcoindIOException(e); Map<String, Object> txInfo = bitcoindClient.getBitcoindService().getTransaction(tx_hash, true);
Object decoded = txInfo.get("decoded");
if(decoded instanceof Map<?, ?>) {
Map<String, Object> decodedMap = (Map<String, Object>)decoded;
decodedMap.put("hex", txInfo.get("hex"));
decodedMap.put("confirmations", txInfo.get("confirmations"));
decodedMap.put("blockhash", txInfo.get("blockhash"));
decodedMap.put("time", txInfo.get("time"));
decodedMap.put("blocktime", txInfo.get("blocktime"));
return decoded;
}
throw new TransactionNotFoundException(e.getErrorMessage());
} catch(JsonRpcException ex) {
throw new TransactionNotFoundException(ex.getErrorMessage());
} catch(IllegalStateException ex) {
throw new BitcoindIOException(ex);
}
} catch(IllegalStateException e) {
throw new BitcoindIOException(e);
}
} else {
try {
return bitcoindClient.getBitcoindService().getTransaction(tx_hash, false).get("hex");
} catch(JsonRpcException e) {
try {
return bitcoindClient.getBitcoindService().getRawTransaction(tx_hash, false);
} catch(JsonRpcException ex) {
throw new TransactionNotFoundException(ex.getErrorMessage());
} catch(IllegalStateException ex) {
throw new BitcoindIOException(e);
}
} catch(IllegalStateException e) {
throw new BitcoindIOException(e);
}
} }
} }