Skip to content
Snippets Groups Projects
Commit e67cac73 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

Merge branch 'master' into 'master'

# Transaction multi-destinataire

See merge request !1
parents 0f2a0780 3d7c33e4
Branches
Tags
1 merge request!1# Transaction multi-destinataire
Pipeline #15528 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
......
......@@ -48,7 +48,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 {
......@@ -85,8 +87,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.getCurrency() != null);
......@@ -98,7 +109,7 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
HttpPost httpPost = new HttpPost(httpService.getPath(peer, 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()) {
......@@ -220,9 +231,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()));
......@@ -245,8 +255,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(),
......@@ -363,9 +373,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;
......@@ -402,6 +417,10 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
long rest = amount;
int outputBase = inputs.maxBase;
long outputAmount;
for(String destPubKey : mapPubkeyAmount.keySet()) {
rest = mapPubkeyAmount.get(destPubKey);
outputBase = inputs.maxBase;
while (rest > 0) {
outputAmount = truncBase(rest, outputBase);
rest -= outputAmount;
......@@ -415,6 +434,7 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
}
outputBase--;
}
}
rest = inputs.amount - amount;
outputBase = inputs.maxBase;
while(rest > 0) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment