Commit 660243c8 authored by Sergei Zubov's avatar Sergei Zubov

Add zero inputs for transfers to own addresses

parent 1425e56c
Pipeline #638 failed with stages
in 45 seconds
......@@ -34,8 +34,8 @@ namespace NBitcoin
int changeIndex = ctx.Transaction.Outputs.FindIndex(p =>
p.ScriptPubKey == group.ChangeScript[(int) ctx.ChangeType]);
if (changeIndex == -1) return result;
if (changeIndex != -1)
{
var outPoint = new OutPoint
{
Hash = uint256.Zero,
......@@ -48,7 +48,31 @@ namespace NBitcoin
});
group.Coins.Add(outPoint, new Coin(uint256.Zero, outPoint.N,
Money.Zero, @group.ChangeScript[(int) ctx.ChangeType]));
Money.Zero, group.ChangeScript[(int) ctx.ChangeType]));
}
foreach (var ownAddress in group.ownAddresses)
{
var ownAddressOutputIndex = ctx.Transaction.Outputs.FindIndex(p =>
string.Equals(p.ScriptPubKey.GetDestinationAddress(this.Network).ToString(), ownAddress));
if(ownAddressOutputIndex == -1 || ownAddressOutputIndex == changeIndex)
continue;
var outPoint = new OutPoint
{
Hash = uint256.Zero,
N = (uint) ownAddressOutputIndex
};
ctx.Transaction.AddInput(new TxIn
{
PrevOut = outPoint
});
group.Coins.Add(outPoint, new Coin(uint256.Zero, outPoint.N,
Money.Zero, ctx.Transaction.Outputs[ownAddressOutputIndex].ScriptPubKey));
}
return result;
}
......
......@@ -495,6 +495,7 @@ namespace NBitcoin
internal List<Builder> IssuanceBuilders = new List<Builder>();
internal Dictionary<AssetId, List<Builder>> BuildersByAsset = new Dictionary<AssetId, List<Builder>>();
internal Script[] ChangeScript = new Script[3];
internal IEnumerable<string> ownAddresses { get; set; }
internal void Shuffle()
{
Shuffle(this.Builders);
......@@ -1074,6 +1075,12 @@ namespace NBitcoin
return this;
}
public TransactionBuilder SetOwnAddresses(IEnumerable<string> ownAddresses)
{
this.CurrentGroup.ownAddresses = ownAddresses;
return this;
}
public TransactionBuilder SetCoinSelector(ICoinSelector selector)
{
if(selector == null)
......
......@@ -30,6 +30,18 @@ namespace Stratis.Bitcoin.Features.Wallet
}
}
protected override void InitializeTransactionBuilder(TransactionBuildContext context)
{
base.InitializeTransactionBuilder(context);
this.AddOwnAddresses(context);
}
private void AddOwnAddresses(TransactionBuildContext context)
{
context.TransactionBuilder.SetOwnAddresses(
(this.walletManager as IDeStreamWalletManager).GetOwnAddresses(context.AccountReference));
}
/// <inheritdoc />
protected override void AddFee(TransactionBuildContext context)
{
......@@ -140,6 +152,29 @@ namespace Stratis.Bitcoin.Features.Wallet
seedExtKey.Derive(new KeyPath(context.ChangeAddress.HdPath)).GetWif(wallet.Network);
signingKeys.Add(changeAddressPrivateKey);
var transfersToSelf = context.Recipients.Where(p =>
(this.walletManager as IDeStreamWalletManager).GetOwnAddresses(context.AccountReference)
.Contains(p.ScriptPubKey.GetDestinationAddress(this.network).ToString()) && context.UnspentOutputs.All(q => p.ScriptPubKey != q.Address.ScriptPubKey));
foreach (var ownAddress in transfersToSelf)
{
var ownHdAddress = this.walletManager.GetAccounts(context.AccountReference.WalletName)
.Single(q => q.Name == context.AccountReference.AccountName).GetCombinedAddresses()
.Single(p => string.Equals(ownAddress.ScriptPubKey.GetDestinationAddress(this.network).ToString(), p.Address));
var ownAddressPrivateKey = seedExtKey.Derive(new KeyPath(ownHdAddress.HdPath)).GetWif(wallet.Network);
signingKeys.Add(ownAddressPrivateKey);
}
// signingKeys.UnionWith(context.Recipients.Where(p =>
// (this.walletManager as IDeStreamWalletManager).GetOwnAddresses(context.AccountReference)
// .Contains(p.ScriptPubKey.GetDestinationAddress(this.network).ToString()) &&
// context.UnspentOutputs.All(q => p.ScriptPubKey != q.Address.ScriptPubKey)).Select(r =>
// seedExtKey.Derive(new KeyPath(this.walletManager.GetAccounts(context.AccountReference.WalletName)
// .Single(q => q.Name == context.AccountReference.AccountName).GetCombinedAddresses()
// .Single(p =>
// string.Equals(r.ScriptPubKey.GetDestinationAddress(this.network).ToString(), p.Address))
// .HdPath)).GetWif(wallet.Network)));
context.TransactionBuilder.AddKeys(signingKeys.ToArray());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment