forked from btcsuite/Paymetheus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remaining issues should be fixed on feature branches and submitted with PRs.
- Loading branch information
Showing
129 changed files
with
5,873 additions
and
2,553 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) 2016 The btcsuite developers | ||
// Copyright (c) 2016 The Decred developers | ||
// Licensed under the ISC license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
|
||
namespace Paymetheus.Decred | ||
{ | ||
internal static class Errors | ||
{ | ||
public static ArgumentOutOfRangeException RequireNonNegative(string paramName) => | ||
new ArgumentOutOfRangeException(paramName, "Non-negative number required."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2016 The btcsuite developers | ||
// Copyright (c) 2016 The Decred developers | ||
// Licensed under the ISC license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Paymetheus.Decred.Util | ||
{ | ||
public struct TupleValue<T1, T2> | ||
{ | ||
public TupleValue(T1 item1, T2 item2) | ||
{ | ||
Item1 = item1; | ||
Item2 = item2; | ||
} | ||
|
||
public T1 Item1 { get; } | ||
public T2 Item2 { get; } | ||
} | ||
|
||
public struct TupleValue<T1, T2, T3> | ||
{ | ||
public TupleValue(T1 item1, T2 item2, T3 item3) | ||
{ | ||
Item1 = item1; | ||
Item2 = item2; | ||
Item3 = item3; | ||
} | ||
|
||
public T1 Item1 { get; } | ||
public T2 Item2 { get; } | ||
public T3 Item3 { get; } | ||
} | ||
|
||
/// <summary> | ||
/// TupleValue is an efficient alternative for the System.Tuple types. TupleValue is a | ||
/// struct (value type) while Tuple is a class (reference type). A similar TupleValue struct | ||
/// and tuple syntax sugar is planned for C# 7, which would eliminate the need for these | ||
/// types if added. | ||
/// </summary> | ||
public static class TupleValue | ||
{ | ||
public static TupleValue<T1, T2> Create<T1, T2>(T1 item1, T2 item2) => | ||
new TupleValue<T1, T2>(item1, item2); | ||
|
||
public static TupleValue<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3) => | ||
new TupleValue<T1, T2, T3>(item1, item2, item3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2016 The Decred developers | ||
// Licensed under the ISC license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Paymetheus.Decred.Wallet | ||
{ | ||
public struct Balances | ||
{ | ||
public Balances(Amount total, Amount spendable, Amount locked) | ||
{ | ||
TotalBalance = total; | ||
SpendableBalance = spendable; | ||
LockedBalance = locked; | ||
} | ||
|
||
public Amount TotalBalance { get; internal set; } | ||
public Amount SpendableBalance { get; internal set; } | ||
public Amount LockedBalance { get; internal set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) 2016 The btcsuite developers | ||
// Copyright (c) 2016 The Decred developers | ||
// Licensed under the ISC license. See LICENSE file in the project root for full license information. | ||
|
||
using Paymetheus.Decred.Script; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Paymetheus.Decred.Wallet | ||
{ | ||
public static class TransactionAuthor | ||
{ | ||
/// <summary> | ||
/// Provides transaction inputs referencing spendable outputs to construct a | ||
/// transaction with some target output amount. | ||
/// </summary> | ||
/// <param name="target">Target amount the previous output value must meet or exceed.</param> | ||
/// <returns>Total previous output amount and array of transaction inputs if the target was met.</returns> | ||
public delegate Task<Tuple<Amount, Transaction.Input[]>> InputSource(Amount target); | ||
|
||
/// <summary> | ||
/// Constructs an unsigned transaction by referencing previous unspent outputs. | ||
/// A change output is added when necessary to return extra value back to the wallet. | ||
/// </summary> | ||
/// <param name="outputs">Transaction output array without change.</param> | ||
/// <param name="changeScript">Output script to pay change to.</param> | ||
/// <param name="fetchInputsAsync">Input selection source.</param> | ||
/// <returns>Unsigned transaction and total input amount.</returns> | ||
/// <exception cref="InsufficientFundsException">Input source was unable to provide enough input value.</exception> | ||
public static async Task<Tuple<Transaction, Amount>> BuildUnsignedTransaction(Transaction.Output[] outputs, | ||
OutputScript changeScript, | ||
Amount feePerKb, | ||
InputSource fetchInputsAsync) | ||
{ | ||
if (outputs == null) | ||
throw new ArgumentNullException(nameof(outputs)); | ||
if (changeScript == null) | ||
throw new ArgumentNullException(nameof(changeScript)); | ||
if (fetchInputsAsync == null) | ||
throw new ArgumentNullException(nameof(fetchInputsAsync)); | ||
|
||
var targetAmount = outputs.Sum(o => o.Amount); | ||
var estimatedSize = Transaction.EstimateSerializeSize(1, outputs, true); | ||
var targetFee = TransactionFees.FeeForSerializeSize(feePerKb, estimatedSize); | ||
|
||
while (true) | ||
{ | ||
var funding = await fetchInputsAsync(targetAmount + targetFee); | ||
var inputAmount = funding.Item1; | ||
var inputs = funding.Item2; | ||
if (inputAmount < targetAmount + targetFee) | ||
{ | ||
throw new InsufficientFundsException(); | ||
} | ||
|
||
var unsignedTransaction = new Transaction(Transaction.SupportedVersion, inputs, outputs, 0, 0); | ||
if (inputAmount > targetAmount + targetFee) | ||
{ | ||
unsignedTransaction = TransactionFees.AddChange(unsignedTransaction, inputAmount, | ||
changeScript, feePerKb); | ||
} | ||
|
||
if (TransactionFees.EstimatedFeePerKb(unsignedTransaction, inputAmount) < feePerKb) | ||
{ | ||
estimatedSize = Transaction.EstimateSerializeSize(inputs.Length, outputs, true); | ||
targetFee = TransactionFees.FeeForSerializeSize(feePerKb, estimatedSize); | ||
} | ||
else | ||
{ | ||
return Tuple.Create(unsignedTransaction, inputAmount); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public class InsufficientFundsException : Exception | ||
{ | ||
public InsufficientFundsException() : base("Insufficient funds available to construct transaction.") { } | ||
} | ||
} | ||
|
Oops, something went wrong.