/// <summary> /// Initializes a new instance of <see cref="ReturnOp"/> using the given <see cref="IScript"/>. /// </summary> /// <exception cref="ArgumentNullException"/> /// <param name="scr">Script to use</param> /// <param name="usePushOp"> /// [Default value = true] /// If true, the data will be included after <see cref="OP.RETURN"/> using <see cref="PushDataOp"/> scheme. /// </param> public ReturnOp(IScript scr, bool usePushOp = true) { if (scr == null) { throw new ArgumentNullException(nameof(scr), "Script can not be null."); } byte[] temp = scr.Data; if (usePushOp) { StackInt size = new StackInt(temp.Length); FastStream stream = new FastStream(temp.Length + 2); stream.Write((byte)OP.RETURN); size.WriteToStream(stream); stream.Write(temp); data = stream.ToByteArray(); } else { data = new byte[temp.Length + 1]; data[0] = (byte)OP.RETURN; Buffer.BlockCopy(temp, 0, data, 1, temp.Length); } }
public void ToByteArrayTest(byte[] data, int finalOffset, uint val) { StackInt ci = new StackInt(val); FastStream stream = new FastStream(10); ci.WriteToStream(stream); byte[] actual = stream.ToByteArray(); byte[] expected = new byte[finalOffset]; Buffer.BlockCopy(data, 0, expected, 0, finalOffset); Assert.Equal(expected, actual); }
public void AddWithStackIntLengthTest(int init, int add) { var counter = new SizeCounter(init); counter.AddWithStackIntLength(add); var si = new StackInt(add); var stream = new FastStream(5); si.WriteToStream(stream); int expected = stream.GetSize() + add + init; Assert.Equal(expected, counter.Size); }
/// <summary> /// Initializes a new instance of <see cref="ReturnOp"/> using the given data. /// </summary> /// <param name="ba">Data to use (can be null)</param> /// <param name="usePushOp"> /// [Default value = true] /// If true, the data will be included after <see cref="OP.RETURN"/> using <see cref="PushDataOp"/> scheme. /// </param> public ReturnOp(byte[] ba, bool usePushOp = true) { if (ba == null || ba.Length == 0) { data = new byte[1] { (byte)OP.RETURN }; } else if (usePushOp) { StackInt size = new StackInt(ba.Length); FastStream stream = new FastStream(ba.Length + 2); stream.Write((byte)OP.RETURN); size.WriteToStream(stream); stream.Write(ba); data = stream.ToByteArray(); } else { data = new byte[ba.Length + 1]; data[0] = (byte)OP.RETURN; Buffer.BlockCopy(ba, 0, data, 1, ba.Length); } }