|
9 | 9 | using System.Diagnostics.CodeAnalysis; |
10 | 10 | using NBitcoin.Crypto; |
11 | 11 | using System.Text; |
| 12 | +using Newtonsoft.Json.Linq; |
12 | 13 |
|
13 | 14 | namespace NBitcoin |
14 | 15 | { |
15 | | - public class PSBTInput : PSBTCoin |
| 16 | + public partial class PSBTInput : PSBTCoin |
16 | 17 | { |
17 | 18 | // Those fields are not saved, but can be used as hint to solve more info for the PSBT |
18 | 19 | internal Script originalScriptSig = Script.Empty; |
@@ -159,6 +160,32 @@ internal PSBTInput(BitcoinStream stream, PSBT parent, uint index, TxIn input) : |
159 | 160 | throw new FormatException("Invalid PSBTInput. Unexpected value length for PSBT_IN_TAP_MERKLE_ROOT"); |
160 | 161 | TaprootMerkleRoot = new uint256(v); |
161 | 162 | break; |
| 163 | +#if HAS_SPAN |
| 164 | + case PSBTConstants.PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS: |
| 165 | + if (k.Length != 34) |
| 166 | + throw new FormatException("Invalid PSBTInput. Unexpected key length for PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS"); |
| 167 | + if (v.Length % 33 != 0 || v.Length == 0) |
| 168 | + throw new FormatException("Invalid PSBTInput. Unexpected value length for PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS"); |
| 169 | + var pk = NBitcoin.MusigParticipantPubKeys.Parse(k, v); |
| 170 | + this.MusigParticipantPubKeys.Add(pk.Aggregated, pk.PubKeys); |
| 171 | + break; |
| 172 | + case PSBTConstants.PSBT_IN_MUSIG2_PUB_NONCE: |
| 173 | + if (k.Length is not (1 + 33 + 33 or 1 + 33 + 33 + 32)) |
| 174 | + throw new FormatException("Invalid PSBTInput. Unexpected key length for PSBT_IN_MUSIG2_PUB_NONCE"); |
| 175 | + if (k.Length is not 66) |
| 176 | + throw new FormatException("Invalid PSBTInput. Unexpected value length for PSBT_IN_MUSIG2_PUB_NONCE"); |
| 177 | + var musigNonceKey = MusigTarget.Parse(k); |
| 178 | + this.MusigPubNonces.Add(musigNonceKey, v); |
| 179 | + break; |
| 180 | + case PSBTConstants.PSBT_IN_MUSIG2_PARTIAL_SIG: |
| 181 | + if (k.Length is not (1 + 33 + 33 or 1 + 33 + 33 + 32)) |
| 182 | + throw new FormatException("Invalid PSBTInput. Unexpected key length for PSBT_IN_MUSIG2_PARTIAL_SIG"); |
| 183 | + if (k.Length is not 32) |
| 184 | + throw new FormatException("Invalid PSBTInput. Unexpected value length for PSBT_IN_MUSIG2_PARTIAL_SIG"); |
| 185 | + var musigSigKey = MusigTarget.Parse(k); |
| 186 | + this.MusigPartialSigs.Add(musigSigKey, v); |
| 187 | + break; |
| 188 | +#endif |
162 | 189 | case PSBTConstants.PSBT_IN_SCRIPTSIG: |
163 | 190 | if (k.Length != 1) |
164 | 191 | throw new FormatException("Invalid PSBTInput. Contains illegal value in key for final scriptsig"); |
@@ -416,7 +443,14 @@ public void UpdateFrom(PSBTInput other) |
416 | 443 |
|
417 | 444 | foreach (var keyPath in other.HDTaprootKeyPaths) |
418 | 445 | HDTaprootKeyPaths.TryAdd(keyPath.Key, keyPath.Value); |
419 | | - |
| 446 | +#if HAS_SPAN |
| 447 | + foreach (var o in other.MusigParticipantPubKeys) |
| 448 | + MusigParticipantPubKeys.TryAdd(o.Key, o.Value); |
| 449 | + foreach (var o in other.MusigPartialSigs) |
| 450 | + MusigPartialSigs.TryAdd(o.Key, o.Value); |
| 451 | + foreach (var o in other.MusigPubNonces) |
| 452 | + MusigPubNonces.TryAdd(o.Key, o.Value); |
| 453 | +#endif |
420 | 454 | TaprootInternalKey ??= other.TaprootInternalKey; |
421 | 455 | TaprootKeySignature ??= other.TaprootKeySignature; |
422 | 456 | TaprootMerkleRoot ??= other.TaprootMerkleRoot; |
@@ -728,7 +762,32 @@ public void Serialize(BitcoinStream stream) |
728 | 762 | var value = merkleRoot.ToBytes(); |
729 | 763 | stream.ReadWriteAsVarString(ref value); |
730 | 764 | } |
731 | | - |
| 765 | +#if HAS_SPAN |
| 766 | + foreach (var mpk in MusigParticipantPubKeys) |
| 767 | + { |
| 768 | + var key = new byte[] { PSBTConstants.PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS }.Concat(mpk.Key.ToBytes()); |
| 769 | + stream.ReadWriteAsVarString(ref key); |
| 770 | + foreach (var pk in mpk.Value) |
| 771 | + { |
| 772 | + var b = pk.ToBytes(); |
| 773 | + stream.ReadWriteAsVarString(ref b); |
| 774 | + } |
| 775 | + } |
| 776 | + foreach (var mpn in MusigPubNonces) |
| 777 | + { |
| 778 | + var key = mpn.Key.ToBytes(PSBTConstants.PSBT_IN_MUSIG2_PUB_NONCE); |
| 779 | + stream.ReadWriteAsVarString(ref key); |
| 780 | + var v = mpn.Value; |
| 781 | + stream.ReadWriteAsVarString(ref v); |
| 782 | + } |
| 783 | + foreach (var mpn in MusigPartialSigs) |
| 784 | + { |
| 785 | + var key = mpn.Key.ToBytes(PSBTConstants.PSBT_IN_MUSIG2_PARTIAL_SIG); |
| 786 | + stream.ReadWriteAsVarString(ref key); |
| 787 | + var v = mpn.Value; |
| 788 | + stream.ReadWriteAsVarString(ref v); |
| 789 | + } |
| 790 | +#endif |
732 | 791 | if (this.TaprootInternalKey is TaprootInternalPubKey tp) |
733 | 792 | { |
734 | 793 | stream.ReadWriteAsVarInt(ref defaultKeyLen); |
@@ -856,13 +915,53 @@ internal void Write(JsonTextWriter jsonWriter) |
856 | 915 | { |
857 | 916 | jsonWriter.WritePropertyValue("taproot_key_signature", tsig.ToString()); |
858 | 917 | } |
| 918 | + |
859 | 919 | jsonWriter.WritePropertyName("partial_signatures"); |
860 | 920 | jsonWriter.WriteStartObject(); |
861 | 921 | foreach (var sig in partial_sigs) |
862 | 922 | { |
863 | 923 | jsonWriter.WritePropertyValue(sig.Key.ToString(), Encoders.Hex.EncodeData(sig.Value.ToBytes())); |
864 | 924 | } |
865 | 925 | jsonWriter.WriteEndObject(); |
| 926 | +#if HAS_SPAN |
| 927 | + if (MusigParticipantPubKeys.Count != 0) |
| 928 | + { |
| 929 | + jsonWriter.WritePropertyName("musig_participant_pubkeys"); |
| 930 | + jsonWriter.WriteStartObject(); |
| 931 | + foreach (var o in MusigParticipantPubKeys) |
| 932 | + { |
| 933 | + jsonWriter.WritePropertyName(o.Key.ToHex()); |
| 934 | + jsonWriter.WriteStartArray(); |
| 935 | + foreach (var k in o.Value) |
| 936 | + { |
| 937 | + jsonWriter.WriteValue(k.ToHex()); |
| 938 | + } |
| 939 | + jsonWriter.WriteEndArray(); |
| 940 | + } |
| 941 | + jsonWriter.WriteEndObject(); |
| 942 | + } |
| 943 | + |
| 944 | + if (MusigPartialSigs.Count != 0) |
| 945 | + { |
| 946 | + jsonWriter.WritePropertyName("musig_partial_signatures"); |
| 947 | + jsonWriter.WriteStartObject(); |
| 948 | + foreach (var sig in MusigPartialSigs) |
| 949 | + { |
| 950 | + jsonWriter.WritePropertyValue(Encoders.Hex.EncodeData(sig.Key.ToBytes(0)[1..]), Encoders.Hex.EncodeData(sig.Value)); |
| 951 | + } |
| 952 | + jsonWriter.WriteEndObject(); |
| 953 | + } |
| 954 | + if (MusigPartialSigs.Count != 0) |
| 955 | + { |
| 956 | + jsonWriter.WritePropertyName("musig_pub_nonces"); |
| 957 | + jsonWriter.WriteStartObject(); |
| 958 | + foreach (var sig in MusigPubNonces) |
| 959 | + { |
| 960 | + jsonWriter.WritePropertyValue(Encoders.Hex.EncodeData(sig.Key.ToBytes(0)[1..]), Encoders.Hex.EncodeData(sig.Value)); |
| 961 | + } |
| 962 | + jsonWriter.WriteEndObject(); |
| 963 | + } |
| 964 | +#endif |
866 | 965 | if (sighash_type is uint s) |
867 | 966 | jsonWriter.WritePropertyValue("sighash", GetName(s)); |
868 | 967 | if (this.FinalScriptSig != null) |
|
0 commit comments