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