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
1 merge request!1# Transaction multi-destinataire
...@@ -30,6 +30,8 @@ import org.duniter.core.client.model.local.Peer; ...@@ -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.model.local.Wallet;
import org.duniter.core.client.service.exception.InsufficientCreditException; import org.duniter.core.client.service.exception.InsufficientCreditException;
import java.util.Map;
public interface TransactionRemoteService extends Service { public interface TransactionRemoteService extends Service {
...@@ -46,6 +48,18 @@ 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 transfer(Peer peer, Wallet wallet, String destPubKey, long amount,
String comment) throws InsufficientCreditException; 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 * Same, using the default currency's peer
* @param wallet * @param wallet
......
...@@ -49,7 +49,9 @@ import org.slf4j.LoggerFactory; ...@@ -49,7 +49,9 @@ import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implements TransactionRemoteService { public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implements TransactionRemoteService {
...@@ -86,8 +88,17 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen ...@@ -86,8 +88,17 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
return transfer(null, wallet, destPubKey, amount, comment); return transfer(null, wallet, destPubKey, amount, comment);
} }
public String transfer(Peer peer, Wallet wallet, String destPubKey, long amount, public String transfer(Peer peer, Wallet wallet, String destPubKey, long amount,
String comment) throws InsufficientCreditException { 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.checkNotNull(wallet);
Preconditions.checkArgument(peer != null || wallet.getCurrencyId() != null); Preconditions.checkArgument(peer != null || wallet.getCurrencyId() != null);
...@@ -102,7 +113,7 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen ...@@ -102,7 +113,7 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
new HttpPost(getPath(wallet.getCurrencyId(), URL_TX_PROCESS)); new HttpPost(getPath(wallet.getCurrencyId(), URL_TX_PROCESS));
// compute transaction // compute transaction
String transaction = getSignedTransaction(peer, wallet, currentBlock, destPubKey, 0, amount, String transaction = getSignedTransaction(peer, wallet, currentBlock,mapPubkeyAmount , 0,
comment); comment);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
...@@ -237,9 +248,8 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen ...@@ -237,9 +248,8 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
protected String getSignedTransaction(Peer peer, protected String getSignedTransaction(Peer peer,
Wallet wallet, Wallet wallet,
BlockchainBlock block, BlockchainBlock block,
String destPubKey, Map<String,Long> mapPubkeyAmount,
int locktime, int locktime,
long amount,
String comment) throws InsufficientCreditException { String comment) throws InsufficientCreditException {
Preconditions.checkNotNull(wallet); Preconditions.checkNotNull(wallet);
Preconditions.checkArgument(StringUtils.isNotBlank(wallet.getCurrency())); Preconditions.checkArgument(StringUtils.isNotBlank(wallet.getCurrency()));
...@@ -262,8 +272,8 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen ...@@ -262,8 +272,8 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
List<TxSource.Source> txInputs = new ArrayList<>(); List<TxSource.Source> txInputs = new ArrayList<>();
List<TxOutput> txOutputs = new ArrayList<>(); List<TxOutput> txOutputs = new ArrayList<>();
computeTransactionInputsAndOuputs(block.getUnitbase(), computeTransactionInputsAndOuputs(block.getUnitbase(),
wallet.getPubKeyHash(), destPubKey, wallet.getPubKeyHash(),mapPubkeyAmount,
sources, amount, txInputs, txOutputs); sources, txInputs, txOutputs);
String transaction = getTransaction(wallet.getCurrency(), String transaction = getTransaction(wallet.getCurrency(),
block.getNumber(), block.getHash(), block.getNumber(), block.getHash(),
...@@ -380,9 +390,14 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen ...@@ -380,9 +390,14 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
public void computeTransactionInputsAndOuputs(int currentUnitBase, public void computeTransactionInputsAndOuputs(int currentUnitBase,
String srcPubKey, 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{ 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(); TxInputs inputs = new TxInputs();
inputs.amount = 0; inputs.amount = 0;
inputs.minBase = currentUnitBase; inputs.minBase = currentUnitBase;
...@@ -419,6 +434,10 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen ...@@ -419,6 +434,10 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
long rest = amount; long rest = amount;
int outputBase = inputs.maxBase; int outputBase = inputs.maxBase;
long outputAmount; long outputAmount;
for(String destPubKey : mapPubkeyAmount.keySet()) {
rest = mapPubkeyAmount.get(destPubKey);
outputBase = inputs.maxBase;
while (rest > 0) { while (rest > 0) {
outputAmount = truncBase(rest, outputBase); outputAmount = truncBase(rest, outputBase);
rest -= outputAmount; rest -= outputAmount;
...@@ -432,6 +451,7 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen ...@@ -432,6 +451,7 @@ public class TransactionRemoteServiceImpl extends BaseRemoteServiceImpl implemen
} }
outputBase--; outputBase--;
} }
}
rest = inputs.amount - amount; rest = inputs.amount - amount;
outputBase = inputs.maxBase; outputBase = inputs.maxBase;
while(rest > 0) { while(rest > 0) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment