diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteService.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteService.java index 0e5ab18a7569748dcce3f1229db76b93685d905f..d82f4b5697b804a069a30d93ddb92b34f88b2551 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteService.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteService.java @@ -30,6 +30,8 @@ import org.duniter.core.client.model.local.Peer; import org.duniter.core.client.model.local.Wallet; import org.duniter.core.client.service.exception.InsufficientCreditException; +import java.util.Map; + public interface TransactionRemoteService extends Service { @@ -46,6 +48,18 @@ public interface TransactionRemoteService extends Service { String transfer(Peer peer, Wallet wallet, String destPubKey, long amount, String comment) throws InsufficientCreditException; + /** + * Transfer TX to the given peer, or if null to currency's default peer + * @param peer + * @param wallet + * @param mapPubkeyAmount + * @param comment + * @return + * @throws InsufficientCreditException + */ + String transfer(Peer peer, Wallet wallet, Map<String,Long> mapPubkeyAmount, + String comment) throws InsufficientCreditException; + /** * Same, using the default currency's peer * @param wallet diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteServiceImpl.java index 47c91a51dab46017ec0d4aa9f6e00bc25e35437e..d839e42cf9094c24b634cbc9ba66419dbf5acd40 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/TransactionRemoteServiceImpl.java @@ -49,7 +49,9 @@ import org.slf4j.LoggerFactory; import java.io.UnsupportedEncodingException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implements TransactionRemoteService { @@ -86,8 +88,17 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen return transfer(null, wallet, destPubKey, amount, comment); } + public String transfer(Peer peer, Wallet wallet, String destPubKey, long amount, String comment) throws InsufficientCreditException { + Map<String,Long> mapPubkeyAmount = new HashMap<String,Long>(); + mapPubkeyAmount.put(destPubKey,amount); + return transfer(peer,wallet,mapPubkeyAmount,comment); + } + + public String transfer(Peer peer, Wallet wallet, Map<String,Long> mapPubkeyAmount, + String comment) throws InsufficientCreditException{ + Preconditions.checkNotNull(wallet); Preconditions.checkArgument(peer != null || wallet.getCurrencyId() != null); @@ -102,7 +113,7 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen new HttpPost(getPath(wallet.getCurrencyId(), URL_TX_PROCESS)); // compute transaction - String transaction = getSignedTransaction(peer, wallet, currentBlock, destPubKey, 0, amount, + String transaction = getSignedTransaction(peer, wallet, currentBlock,mapPubkeyAmount , 0, comment); if (log.isDebugEnabled()) { @@ -237,9 +248,8 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen protected String getSignedTransaction(Peer peer, Wallet wallet, BlockchainBlock block, - String destPubKey, + Map<String,Long> mapPubkeyAmount, int locktime, - long amount, String comment) throws InsufficientCreditException { Preconditions.checkNotNull(wallet); Preconditions.checkArgument(StringUtils.isNotBlank(wallet.getCurrency())); @@ -262,8 +272,8 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen List<TxSource.Source> txInputs = new ArrayList<>(); List<TxOutput> txOutputs = new ArrayList<>(); computeTransactionInputsAndOuputs(block.getUnitbase(), - wallet.getPubKeyHash(), destPubKey, - sources, amount, txInputs, txOutputs); + wallet.getPubKeyHash(),mapPubkeyAmount, + sources, txInputs, txOutputs); String transaction = getTransaction(wallet.getCurrency(), block.getNumber(), block.getHash(), @@ -380,9 +390,14 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen public void computeTransactionInputsAndOuputs(int currentUnitBase, String srcPubKey, - String destPubKey, TxSource.Source[] sources, long amount, + Map<String,Long> mapPubkeyAmount, + TxSource.Source[] sources, List<TxSource.Source> resultInputs, List<TxOutput> resultOutputs) throws InsufficientCreditException{ + long amount =0; + for(String pubKey : mapPubkeyAmount.keySet()){ + amount += mapPubkeyAmount.get(pubKey); + } TxInputs inputs = new TxInputs(); inputs.amount = 0; inputs.minBase = currentUnitBase; @@ -419,18 +434,23 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen long rest = amount; int outputBase = inputs.maxBase; long outputAmount; - while(rest > 0) { - outputAmount = truncBase(rest, outputBase); - rest -= outputAmount; - if (outputAmount > 0) { - outputAmount = inversePowBase(outputAmount, outputBase); - TxOutput output = new TxOutput(); - output.setAmount(outputAmount); - output.setBase(outputBase); - output.setPubKey(destPubKey); - resultOutputs.add(output); + for(String destPubKey : mapPubkeyAmount.keySet()) { + rest = mapPubkeyAmount.get(destPubKey); + outputBase = inputs.maxBase; + + while (rest > 0) { + outputAmount = truncBase(rest, outputBase); + rest -= outputAmount; + if (outputAmount > 0) { + outputAmount = inversePowBase(outputAmount, outputBase); + TxOutput output = new TxOutput(); + output.setAmount(outputAmount); + output.setBase(outputBase); + output.setPubKey(destPubKey); + resultOutputs.add(output); + } + outputBase--; } - outputBase--; } rest = inputs.amount - amount; outputBase = inputs.maxBase;