diff --git a/Libplanet/Tx/Transaction.cs b/Libplanet/Tx/Transaction.cs index e68769b8406..391fe1d35a5 100644 --- a/Libplanet/Tx/Transaction.cs +++ b/Libplanet/Tx/Transaction.cs @@ -1,4 +1,3 @@ -#nullable disable using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -38,7 +37,7 @@ public sealed class Transaction : IEquatable>, ITxExcerpt private TxId? _id; private TxMetadata _metadata; private byte[] _signature; - private byte[] _bytes; + private byte[]? _bytes; /// /// Creates a new instance by copying data from a specified @@ -59,8 +58,9 @@ public Transaction(ITxMetadata metadata, IEnumerable actions, byte[] signatur _metadata = new TxMetadata(metadata); Actions = actions?.ToImmutableList() ?? throw new ArgumentNullException(nameof(actions)); - Signature = signature - ?? throw new ArgumentNullException(nameof(signature)); + _signature = + new byte[(signature ?? throw new ArgumentNullException(nameof(signature))).Length]; + signature.CopyTo(_signature, 0); } /// @@ -126,8 +126,9 @@ public Transaction( Timestamp = timestamp, }; - Signature = signature ?? - throw new ArgumentNullException(nameof(signature)); + _signature = + new byte[(signature ?? throw new ArgumentNullException(nameof(signature))).Length]; + signature.CopyTo(_signature, 0); Actions = actions?.ToImmutableList() ?? throw new ArgumentNullException(nameof(actions)); } @@ -209,6 +210,8 @@ public TxId Id /// A new array of this transaction's /// signature. Changing a returned array does not affect the internal /// state of this object. + /// Although this cannot be , it can be an empty + /// array if the transaction is not signed yet. public byte[] Signature { get @@ -372,7 +375,7 @@ public static Transaction Create( PrivateKey privateKey, BlockHash? genesisHash, IEnumerable actions, - IImmutableSet
updatedAddresses = null, + IImmutableSet
? updatedAddresses = null, DateTimeOffset? timestamp = null ) { @@ -491,17 +494,13 @@ public static Transaction CreateUnsigned( PublicKey publicKey, BlockHash? genesisHash, IEnumerable actions, - IImmutableSet
updatedAddresses = null, + IImmutableSet
? updatedAddresses = null, DateTimeOffset? timestamp = null ) { var signer = new Address(publicKey); - if (ReferenceEquals(updatedAddresses, null)) - { - updatedAddresses = ImmutableHashSet
.Empty; - } - + updatedAddresses ??= ImmutableHashSet
.Empty; DateTimeOffset ts = timestamp ?? DateTimeOffset.UtcNow; ImmutableArray actionsArray = actions.ToImmutableArray(); @@ -555,7 +554,7 @@ public byte[] Serialize(bool sign) // Poor man's way to optimize serialization without signature... // FIXME: We need to rather reorganize the serialization layout // & optimize Bencodex.Codec in general. - if (_signature is { } && _signature.Length > 0) + if (_signature.Length > 0) { byte[] sigDict = Codec.Encode(Dictionary.Empty.Add(TxMetadata.SignatureKey, _signature)); @@ -617,10 +616,10 @@ public void Validate() } /// - public bool Equals(Transaction other) => Id.Equals(other?.Id); + public bool Equals(Transaction? other) => Id.Equals(other?.Id); /// - public override bool Equals(object obj) => obj is Transaction other && Equals(other); + public override bool Equals(object? obj) => obj is Transaction other && Equals(other); /// public override int GetHashCode() => Id.GetHashCode();