Skip to content

Commit 23f0e1f

Browse files
committed
MimbleWimble(Tests): parse MWEB tx as part of LTC tx
Made MWEB transaction a part of LitecoinTransaction, as it should be. Modified deserialization code to properly handle reading and writing MimbleWimble transaction. Got rid of hardcoded offsets.
1 parent 6fabc2e commit 23f0e1f

File tree

4 files changed

+46
-29
lines changed

4 files changed

+46
-29
lines changed

src/NLitecoin/Litecoin.fs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ module private TxListExtensions =
5050
type LitecoinTransaction() =
5151
inherit Transaction()
5252

53+
static let mwebExtensionTxFlag = 8uy
54+
55+
member val MimbleWimbleTransaction: Option<MimbleWimble.Transaction> = None with get, set
56+
5357
override self.GetConsensusFactory() = LitecoinConsensusFactory.Instance
5458

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

6569
if self.vin.Count = 0 && witSupported && not hasNoDummy then
6670
// We read a dummy or an empty vin.
67-
let flags = stream.ReadWrite flags
71+
flags <- stream.ReadWrite flags
6872
if flags <> 0uy then
6973
// Assume we read a dummy and a flag.
7074
stream.ReadWrite(&self.vin)
@@ -79,22 +83,17 @@ type LitecoinTransaction() =
7983
stream.ReadWrite(&self.vout)
8084
self.vout <- self.vout.WithTransaction self
8185

82-
let flags =
83-
if ((flags &&& 1uy) <> 0uy) && witSupported then
84-
// The witness flag is present, and we support witnesses.
85-
let wit = Witness self.Inputs
86-
wit.ReadWrite stream
87-
flags ^^^ 1uy
88-
else
89-
flags
90-
let flags =
91-
if (flags &&& 8uy) <> 0uy then //MWEB extension tx flag
92-
(* The MWEB flag is present, but currently no MWEB data is supported.
93-
* This fix just prevent from throwing exception bellow so cannonical litecoin transaction can be read
94-
*)
95-
flags ^^^ 8uy
96-
else
97-
flags
86+
if ((flags &&& 1uy) <> 0uy) && witSupported then
87+
// The witness flag is present, and we support witnesses.
88+
let wit = Witness self.Inputs
89+
wit.ReadWrite stream
90+
flags <- flags ^^^ 1uy
91+
92+
if (flags &&& mwebExtensionTxFlag) <> 0uy then
93+
let version = ref 0uy
94+
stream.ReadWrite version
95+
self.MimbleWimbleTransaction <- Some(MimbleWimble.Transaction.Read stream)
96+
flags <- flags ^^^ 8uy
9897

9998
if flags <> 0uy then
10099
// Unknown flag in the serialization
@@ -118,6 +117,12 @@ type LitecoinTransaction() =
118117
else
119118
0uy
120119

120+
let flags =
121+
if self.MimbleWimbleTransaction.IsSome then
122+
flags ||| mwebExtensionTxFlag
123+
else
124+
flags
125+
121126
let flags =
122127
if flags <> 0uy then
123128
// Use extended format in case witnesses are to be serialized.
@@ -135,6 +140,12 @@ type LitecoinTransaction() =
135140
let wit = Witness self.Inputs
136141
wit.ReadWrite stream
137142

143+
match self.MimbleWimbleTransaction with
144+
| Some mwebTransaction ->
145+
stream.ReadWrite MimbleWimble.Transaction.Version |> ignore
146+
(mwebTransaction :> MimbleWimble.ISerializeable).Write stream
147+
| None -> ()
148+
138149
override self.ReadWrite(stream: BitcoinStream) =
139150
let witSupported =
140151
(((uint32 stream.TransactionOptions) &&& (uint32 TransactionOptions.Witness)) <> 0u) &&

src/NLitecoin/MimbleWimble/Types.fs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,7 @@ type Transaction =
705705
// The transaction body.
706706
Body: TxBody
707707
}
708-
/// see https://github.com/litecoin-project/lips/blob/master/lip-0003.mediawiki#user-content-PegIn_Transactions
709-
static member RegularLTCPeginTranasctionSize = 200
710-
711-
static member RegularTransactionOffset = 9
708+
static member Version = 1uy
712709

713710
/// Parse hex-encoded MimbleWimble transaction
714711
static member ParseString(txString: string) : Transaction =

src/NLitecoin/NLitecoin.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<Compile Include="Litecoin.fs" />
1615
<Compile Include="MimbleWimble\Types.fs" />
1716
<Compile Include="MimbleWimble\EC.fs" />
1817
<Compile Include="MimbleWimble\Pedersen.fs" />
1918
<Compile Include="MimbleWimble\Bulletproof.fs" />
2019
<Compile Include="MimbleWimble\TransactionBuilder.fs" />
2120
<Compile Include="MimbleWimble\Wallet.fs" />
21+
<Compile Include="Litecoin.fs" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

tests/NLitecoin.MimbleWimble.Tests/TransactionTests.fs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ let Setup () =
1414
let ParsePegInTransaction () =
1515
let rawTransaction = IO.File.ReadAllText "transaction1.txt"
1616

17-
let mimbleRawWimbleTransaction = rawTransaction[Transaction.RegularLTCPeginTranasctionSize * 2 ..]
18-
let transaction = Transaction.ParseString mimbleRawWimbleTransaction
17+
let litecoinTransaction =
18+
NBitcoin.Transaction.Parse(rawTransaction, NLitecoin.Litecoin.Instance.Mainnet)
19+
20+
let transaction =
21+
(litecoinTransaction :?> NLitecoin.LitecoinTransaction).MimbleWimbleTransaction.Value
1922

2023
Assert.AreEqual(0, transaction.Body.Inputs.Length)
2124
Assert.AreEqual(1, transaction.Body.Kernels.Length)
@@ -31,8 +34,11 @@ let ParsePegInTransaction () =
3134
let ParseHogExTransaction () =
3235
let rawTransaction = IO.File.ReadAllText "transaction2.txt"
3336

34-
let mimbleRawWimbleTransaction = rawTransaction[Transaction.RegularTransactionOffset * 2 ..]
35-
let transaction = Transaction.ParseString mimbleRawWimbleTransaction
37+
let litecoinTransaction =
38+
NBitcoin.Transaction.Parse(rawTransaction, NLitecoin.Litecoin.Instance.Mainnet)
39+
40+
let transaction =
41+
(litecoinTransaction :?> NLitecoin.LitecoinTransaction).MimbleWimbleTransaction.Value
3642

3743
Assert.GreaterOrEqual(transaction.Body.Inputs.Length, 1)
3844
Assert.AreEqual(1, transaction.Body.Kernels.Length)
@@ -48,8 +54,11 @@ let ParseHogExTransaction () =
4854
let ParsePegOutTransaction () =
4955
let rawTransaction = IO.File.ReadAllText "transaction3.txt"
5056

51-
let mimbleRawWimbleTransaction = rawTransaction[Transaction.RegularTransactionOffset * 2 ..]
52-
let transaction = Transaction.ParseString mimbleRawWimbleTransaction
57+
let litecoinTransaction =
58+
NBitcoin.Transaction.Parse(rawTransaction, NLitecoin.Litecoin.Instance.Mainnet)
59+
60+
let transaction =
61+
(litecoinTransaction :?> NLitecoin.LitecoinTransaction).MimbleWimbleTransaction.Value
5362

5463
Assert.GreaterOrEqual(transaction.Body.Inputs.Length, 1)
5564
Assert.AreEqual(1, transaction.Body.Kernels.Length)

0 commit comments

Comments
 (0)