-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a list of RedeemScripts property to replace the single entry RedeemScript * Fix issue link * Create script array json converter * Remove all functionality frmo the old RedeemScript propert and assign its value on startup * fix failing test
- Loading branch information
1 parent
1a1c2ee
commit cfeda4a
Showing
6 changed files
with
155 additions
and
27 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/Blockcore/Utilities/JsonConverters/ScriptCollectionJsonConverter.cs
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,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Blockcore.Consensus.ScriptInfo; | ||
using Blockcore.Consensus.TransactionInfo; | ||
using NBitcoin; | ||
using NBitcoin.DataEncoders; | ||
using Newtonsoft.Json; | ||
|
||
namespace Blockcore.Utilities.JsonConverters | ||
{ | ||
/// <summary> | ||
/// Converter used to convert a <see cref="Script"/> or a <see cref="WitScript"/> to and from JSON. | ||
/// </summary> | ||
/// <seealso cref="Newtonsoft.Json.JsonConverter" /> | ||
public class ScriptCollectionJsonConverter : JsonConverter | ||
{ | ||
/// <inheritdoc /> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(ICollection<Script>) || objectType == typeof(ICollection<WitScript>); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == JsonToken.Null) | ||
return null; | ||
|
||
try | ||
{ | ||
var array = serializer.Deserialize<string[]>(reader); | ||
|
||
if (objectType == typeof(ICollection<Script>)) | ||
{ | ||
return array.Select(s => Script.FromBytesUnsafe(Encoders.Hex.DecodeData(s))).ToList(); | ||
} | ||
|
||
if (objectType == typeof(ICollection<WitScript>)) | ||
{ | ||
return array.Select(s => new WitScript(s)).ToList(); | ||
} | ||
} | ||
catch (FormatException) | ||
{ | ||
} | ||
|
||
throw new JsonObjectException("A script should be a byte string", reader); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
if (value != null) | ||
{ | ||
if (value is ICollection<Script> array) | ||
{ | ||
var serialize = array.Select(s => Encoders.Hex.EncodeData(s.ToBytes(false))).ToArray(); | ||
serializer.Serialize(writer, serialize); | ||
} | ||
|
||
if (value is ICollection<WitScript> arrayw) | ||
{ | ||
var serialize = arrayw.Select(s => s.ToString()).ToArray(); | ||
serializer.Serialize(writer, serialize); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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