Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MimbleWimble support #2

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
/src/NLitecoin/bin
/tests/NLitecoin.Tests/obj
/tests/NLitecoin.Tests/bin
/tests/NLitecoin.MimbleWimble.Tests/obj
/tests/NLitecoin.MimbleWimble.Tests/bin
10 changes: 8 additions & 2 deletions NLitecoin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "NLitecoin", "src\NLitecoin\NLitecoin.fsproj", "{1128287F-0435-4FA9-B4DD-420A9E1FDB23}"
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "NLitecoin", "src\NLitecoin\NLitecoin.fsproj", "{1128287F-0435-4FA9-B4DD-420A9E1FDB23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLitecoin.Tests", "tests\NLitecoin.Tests\NLitecoin.Tests.csproj", "{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLitecoin.Tests", "tests\NLitecoin.Tests\NLitecoin.Tests.csproj", "{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "NLitecoin.MimbleWimble.Tests", "tests\NLitecoin.MimbleWimble.Tests\NLitecoin.MimbleWimble.Tests.fsproj", "{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +23,10 @@ Global
{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F954D3AC-36BC-483E-8DB1-4CC65DF1694D}.Release|Any CPU.Build.0 = Release|Any CPU
{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A67D54D-BE2C-449F-88D2-D6739A5F3F89}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
51 changes: 33 additions & 18 deletions src/NLitecoin/Litecoin.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type internal Witness(inputs: TxInList) =
| null -> WitScript.Empty
| witScript -> witScript
script.ToBytes()
stream.ReadWrite(ref bytes)
stream.ReadWrite bytes
else
input.WitScript <- WitScript.Load stream

Expand Down Expand Up @@ -50,10 +50,14 @@ module private TxListExtensions =
type LitecoinTransaction() =
inherit Transaction()

static let mwebExtensionTxFlag = 8uy

member val MimbleWimbleTransaction: Option<MimbleWimble.Transaction> = None with get, set

override self.GetConsensusFactory() = LitecoinConsensusFactory.Instance

member private self.Read (stream: BitcoinStream) (witSupported: bool) =
let flags = 0uy
let mutable flags = 0uy
self.nVersion <- stream.ReadWrite self.nVersion
// Try to read the vin. In case the dummy is there, this will be read as an empty vector.
stream.ReadWrite(&self.vin)
Expand All @@ -64,7 +68,7 @@ type LitecoinTransaction() =

if self.vin.Count = 0 && witSupported && not hasNoDummy then
// We read a dummy or an empty vin.
let flags = stream.ReadWrite flags
flags <- stream.ReadWrite flags
if flags <> 0uy then
// Assume we read a dummy and a flag.
stream.ReadWrite(&self.vin)
Expand All @@ -79,22 +83,20 @@ type LitecoinTransaction() =
stream.ReadWrite(&self.vout)
self.vout <- self.vout.WithTransaction self

let flags =
if ((flags &&& 1uy) <> 0uy) && witSupported then
// The witness flag is present, and we support witnesses.
let wit = Witness self.Inputs
wit.ReadWrite stream
flags ^^^ 1uy
else
flags
let flags =
if (flags &&& 8uy) <> 0uy then //MWEB extension tx flag
(* The MWEB flag is present, but currently no MWEB data is supported.
* This fix just prevent from throwing exception bellow so cannonical litecoin transaction can be read
*)
flags ^^^ 8uy
if ((flags &&& 1uy) <> 0uy) && witSupported then
// The witness flag is present, and we support witnesses.
let wit = Witness self.Inputs
wit.ReadWrite stream
flags <- flags ^^^ 1uy

if (flags &&& mwebExtensionTxFlag) <> 0uy then
let isMWTransactionPresent = stream.ReadWrite Unchecked.defaultof<byte>
if isMWTransactionPresent <> 0uy then
self.MimbleWimbleTransaction <- Some(MimbleWimble.Transaction.Read stream)
else
flags
// HogEx transaction
self.MimbleWimbleTransaction <- None
flags <- flags ^^^ 8uy

if flags <> 0uy then
// Unknown flag in the serialization
Expand All @@ -118,6 +120,12 @@ type LitecoinTransaction() =
else
0uy

let flags =
if self.MimbleWimbleTransaction.IsSome then
flags ||| mwebExtensionTxFlag
else
flags

let flags =
if flags <> 0uy then
// Use extended format in case witnesses are to be serialized.
Expand All @@ -135,6 +143,13 @@ type LitecoinTransaction() =
let wit = Witness self.Inputs
wit.ReadWrite stream

match self.MimbleWimbleTransaction with
| Some mwebTransaction ->
let valueIsPresentMarker = 1uy
stream.ReadWrite valueIsPresentMarker |> ignore
(mwebTransaction :> MimbleWimble.ISerializeable).Write stream
| None -> ()

override self.ReadWrite(stream: BitcoinStream) =
let witSupported =
(((uint32 stream.TransactionOptions) &&& (uint32 TransactionOptions.Witness)) <> 0u) &&
Expand Down
Loading
Loading