Skip to content
Snippets Groups Projects
Commit 3d7c33e4 authored by jm's avatar jm
Browse files

# Transaction multi-destinataire

parent b8f7f85c
No related branches found
No related tags found
No related merge requests found
Pipeline #4177 failed
......@@ -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
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment