public void TestIterator_Create() { var engine = GetEngine(); var arr = new VMArray { new byte[] { 0x01 }, new byte[] { 0x02 } }; var ret = engine.CreateIterator(arr); ret.Next(); ret.Value().GetSpan().ToHexString().Should().Be(new byte[] { 0x01 }.ToHexString()); var interop = new InteropInterface(1); Assert.ThrowsException <ArgumentException>(() => engine.CreateIterator(interop)); var map = new Map { [1] = 2, [3] = 4 }; ret = engine.CreateIterator(map); ret.Next(); ret.Key().GetInteger().Should().Be(1); ret.Value().GetInteger().Should().Be(2); }
public void TestToParameter() { StackItem byteItem = "00e057eb481b".HexToBytes(); Assert.AreEqual(30000000000000L, (long)new BigInteger(byteItem.ToParameter().Value as byte[])); StackItem boolItem = false; Assert.AreEqual(false, (bool)boolItem.ToParameter().Value); StackItem intItem = new BigInteger(1000); Assert.AreEqual(1000, (BigInteger)intItem.ToParameter().Value); StackItem interopItem = new InteropInterface("test"); Assert.AreEqual(ContractParameterType.InteropInterface, interopItem.ToParameter().Type); StackItem arrayItem = new VM.Types.Array(new[] { byteItem, boolItem, intItem, interopItem }); Assert.AreEqual(1000, (BigInteger)(arrayItem.ToParameter().Value as List <ContractParameter>)[2].Value); StackItem mapItem = new Map { [(PrimitiveType)byteItem] = intItem }; Assert.AreEqual(1000, (BigInteger)(mapItem.ToParameter().Value as List <KeyValuePair <ContractParameter, ContractParameter> >)[0].Value.Value); }
private void TestToParameter2InteropInterface() { StackItem item = new InteropInterface(new object()); ContractParameter parameter = VM.Helper.ToParameter(item); Assert.AreEqual(ContractParameterType.InteropInterface, parameter.Type); }
public void HashCodeTest() { StackItem itemA = "NEO"; StackItem itemB = "NEO"; StackItem itemC = "SmartEconomy"; Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode()); Assert.IsTrue(itemA.GetHashCode() != itemC.GetHashCode()); itemA = new VM.Types.Buffer(1); itemB = new VM.Types.Buffer(1); Assert.IsTrue(itemA.GetHashCode() != itemB.GetHashCode()); itemA = true; itemB = true; itemC = false; Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode()); Assert.IsTrue(itemA.GetHashCode() != itemC.GetHashCode()); itemA = 1; itemB = 1; itemC = 123; Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode()); Assert.IsTrue(itemA.GetHashCode() != itemC.GetHashCode()); itemA = new Null(); itemB = new Null(); Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode()); itemA = new VM.Types.Array(); Assert.ThrowsException <System.NotSupportedException>(() => itemA.GetHashCode()); itemA = new Struct(); Assert.ThrowsException <System.NotSupportedException>(() => itemA.GetHashCode()); itemA = new Map(); Assert.ThrowsException <System.NotSupportedException>(() => itemA.GetHashCode()); itemA = new InteropInterface(123); itemB = new InteropInterface(123); Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode()); var script = new Script(new byte[0]); itemA = new Pointer(script, 123); itemB = new Pointer(script, 123); itemC = new Pointer(script, 1234); Assert.IsTrue(itemA.GetHashCode() == itemB.GetHashCode()); Assert.IsTrue(itemA.GetHashCode() != itemC.GetHashCode()); }
public void TestSerialize() { byte[] result1 = BinarySerializer.Serialize(new byte[5], MaxItemSize); byte[] expectedArray1 = new byte[] { 0x28, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray1), Encoding.Default.GetString(result1)); byte[] result2 = BinarySerializer.Serialize(true, MaxItemSize); byte[] expectedArray2 = new byte[] { 0x20, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray2), Encoding.Default.GetString(result2)); byte[] result3 = BinarySerializer.Serialize(1, MaxItemSize); byte[] expectedArray3 = new byte[] { 0x21, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray3), Encoding.Default.GetString(result3)); StackItem stackItem4 = new InteropInterface(new object()); Action action4 = () => BinarySerializer.Serialize(stackItem4, MaxItemSize); action4.Should().Throw<NotSupportedException>(); List<StackItem> list6 = new List<StackItem> { 1 }; StackItem stackItem62 = new VM.Types.Array(list6); byte[] result6 = BinarySerializer.Serialize(stackItem62, MaxItemSize); byte[] expectedArray6 = new byte[] { 0x40,0x01,0x21,0x01,0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray6), Encoding.Default.GetString(result6)); List<StackItem> list7 = new List<StackItem> { 1 }; StackItem stackItem72 = new Struct(list7); byte[] result7 = BinarySerializer.Serialize(stackItem72, MaxItemSize); byte[] expectedArray7 = new byte[] { 0x41,0x01,0x21,0x01,0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray7), Encoding.Default.GetString(result7)); StackItem stackItem82 = new Map { [2] = 1 }; byte[] result8 = BinarySerializer.Serialize(stackItem82, MaxItemSize); byte[] expectedArray8 = new byte[] { 0x48,0x01,0x21,0x01,0x02,0x21,0x01,0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray8), Encoding.Default.GetString(result8)); Map stackItem91 = new Map(); stackItem91[1] = stackItem91; Action action9 = () => BinarySerializer.Serialize(stackItem91, MaxItemSize); action9.Should().Throw<NotSupportedException>(); VM.Types.Array stackItem10 = new VM.Types.Array(); stackItem10.Add(stackItem10); Action action10 = () => BinarySerializer.Serialize(stackItem10, MaxItemSize); action10.Should().Throw<NotSupportedException>(); }
protected internal byte[] Sha256(StackItem item) { ReadOnlySpan <byte> value = item switch { InteropInterface _interface => _interface.GetInterface <IVerifiable>().GetHashData(), Null _ => ScriptContainer.GetHashData(), _ => item.GetSpan() }; return(value.Sha256()); }
private bool VerifyWithECDsa(StackItem item, byte[] pubkey, byte[] signature, ECCurve curve) { ReadOnlySpan <byte> message = item switch { InteropInterface _interface => _interface.GetInterface <IVerifiable>().GetHashData(), Null _ => ScriptContainer.GetHashData(), _ => item.GetSpan() }; try { return(Crypto.VerifySignature(message, signature, pubkey, curve)); } catch (ArgumentException) { return(false); } }
public override bool Equals(StackItem other) { if (ReferenceEquals(this, other)) { return(true); } if (ReferenceEquals(null, other)) { return(false); } InteropInterface i = other as InteropInterface; if (i == null) { return(false); } return(_object.Equals(i._object)); }
private static bool Crypto_ECDsaVerify(ApplicationEngine engine) { StackItem item0 = engine.CurrentContext.EvaluationStack.Pop(); ReadOnlySpan <byte> message = item0 switch { InteropInterface _interface => _interface.GetInterface <IVerifiable>().GetHashData(), Null _ => engine.ScriptContainer.GetHashData(), _ => item0.GetSpan() }; ReadOnlySpan <byte> pubkey = engine.CurrentContext.EvaluationStack.Pop().GetSpan(); ReadOnlySpan <byte> signature = engine.CurrentContext.EvaluationStack.Pop().GetSpan(); try { engine.CurrentContext.EvaluationStack.Push(Cryptography.Crypto.VerifySignature(message, signature, pubkey)); } catch (ArgumentException) { engine.CurrentContext.EvaluationStack.Push(false); } return(true); }
public void TestIterator_Create() { var engine = GetEngine(); var arr = new VMArray { new byte[] { 0x01 }, new byte[] { 0x02 } }; engine.CurrentContext.EvaluationStack.Push(arr); InteropService.Invoke(engine, InteropService.Neo_Iterator_Create).Should().BeTrue(); var ret = (InteropInterface <IIterator>)engine.CurrentContext.EvaluationStack.Pop(); ret.GetInterface <IIterator>().Next(); ret.GetInterface <IIterator>().Value().GetSpan().ToHexString() .Should().Be(new byte[] { 0x01 }.ToHexString()); var interop = new InteropInterface <object>(1); engine.CurrentContext.EvaluationStack.Push(interop); InteropService.Invoke(engine, InteropService.Neo_Iterator_Create).Should().BeFalse(); var map = new Map { { new Integer(1), new Integer(2) }, { new Integer(3), new Integer(4) } }; engine.CurrentContext.EvaluationStack.Push(map); InteropService.Invoke(engine, InteropService.Neo_Iterator_Create).Should().BeTrue(); ret = (InteropInterface <IIterator>)engine.CurrentContext.EvaluationStack.Pop(); ret.GetInterface <IIterator>().Next(); ret.GetInterface <IIterator>().Key().GetBigInteger().Should().Be(1); ret.GetInterface <IIterator>().Value().GetBigInteger().Should().Be(2); engine.CurrentContext.EvaluationStack.Push(1); InteropService.Invoke(engine, InteropService.Neo_Iterator_Create).Should().BeTrue(); }
private bool CheckMultiSigWithECDsa(StackItem item0, byte[][] pubkeys, byte[][] signatures, ECCurve curve) { int m = signatures.Length, n = pubkeys.Length; ReadOnlySpan <byte> message = item0 switch { InteropInterface _interface => _interface.GetInterface <IVerifiable>().GetHashData(), Null _ => ScriptContainer.GetHashData(), _ => item0.GetSpan() }; if (n == 0 || m == 0 || m > n) { throw new ArgumentException(); } AddGas(ECDsaVerifyPrice * n * exec_fee_factor); try { for (int i = 0, j = 0; i < m && j < n;) { if (Crypto.VerifySignature(message, signatures[i], pubkeys[j], curve)) { i++; } j++; if (m - i > n - j) { return(false); } } } catch (ArgumentException) { return(false); } return(true); } }
public InteropInterfaceReturn(InteropInterface item) { this.value = item.GetInterface <dynamic>(); this.IsNull = item.IsNull; }
public void CastTest() { // Signed integer StackItem item = int.MaxValue; Assert.IsInstanceOfType(item, typeof(Integer)); Assert.AreEqual(new BigInteger(int.MaxValue), item.GetBigInteger()); // Unsigned integer item = uint.MaxValue; Assert.IsInstanceOfType(item, typeof(Integer)); Assert.AreEqual(new BigInteger(uint.MaxValue), item.GetBigInteger()); // Signed long item = long.MaxValue; Assert.IsInstanceOfType(item, typeof(Integer)); Assert.AreEqual(new BigInteger(long.MaxValue), item.GetBigInteger()); // Unsigned long item = ulong.MaxValue; Assert.IsInstanceOfType(item, typeof(Integer)); Assert.AreEqual(new BigInteger(ulong.MaxValue), item.GetBigInteger()); // BigInteger item = BigInteger.MinusOne; Assert.IsInstanceOfType(item, typeof(Integer)); Assert.AreEqual(new BigInteger(-1), item.GetBigInteger()); // Boolean item = true; Assert.IsInstanceOfType(item, typeof(Boolean)); Assert.IsTrue(item.GetBoolean()); // ByteArray item = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; Assert.IsInstanceOfType(item, typeof(ByteArray)); CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }, item.GetByteArray()); // String item = "NEO - 种智能经济分布式网络"; Assert.IsInstanceOfType(item, typeof(ByteArray)); CollectionAssert.AreEqual(Encoding.UTF8.GetBytes("NEO - 种智能经济分布式网络"), item.GetByteArray()); Assert.AreEqual("NEO - 种智能经济分布式网络", item.GetString()); // Array item = new StackItem[] { true, false }; Assert.IsInstanceOfType(item, typeof(Array)); Assert.IsTrue(((Array)item)[0].GetBoolean()); Assert.IsFalse(((Array)item)[1].GetBoolean()); // List item = new List <StackItem>(new StackItem[] { true, false }); Assert.IsInstanceOfType(item, typeof(Array)); Assert.IsTrue(((Array)item)[0].GetBoolean()); Assert.IsFalse(((Array)item)[1].GetBoolean()); // Interop var interop = new InteropInterface <Dictionary <int, int> >(new Dictionary <int, int>() { { 1, 1 } }); Dictionary <int, int> value = interop; Assert.AreEqual(1, value.Count); }
private static bool Crypto_ECDsaCheckMultiSig(ApplicationEngine engine) { StackItem item0 = engine.CurrentContext.EvaluationStack.Pop(); ReadOnlySpan <byte> message = item0 switch { InteropInterface _interface => _interface.GetInterface <IVerifiable>().GetHashData(), Null _ => engine.ScriptContainer.GetHashData(), _ => item0.GetSpan() }; int n; byte[][] pubkeys; StackItem item = engine.CurrentContext.EvaluationStack.Pop(); if (item is Array array1) { pubkeys = array1.Select(p => p.GetSpan().ToArray()).ToArray(); n = pubkeys.Length; if (n == 0) { return(false); } } else { n = (int)item.GetBigInteger(); if (n < 1 || n > engine.CurrentContext.EvaluationStack.Count) { return(false); } pubkeys = new byte[n][]; for (int i = 0; i < n; i++) { pubkeys[i] = engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray(); } } int m; byte[][] signatures; item = engine.CurrentContext.EvaluationStack.Pop(); if (item is Array array2) { signatures = array2.Select(p => p.GetSpan().ToArray()).ToArray(); m = signatures.Length; if (m == 0 || m > n) { return(false); } } else { m = (int)item.GetBigInteger(); if (m < 1 || m > n || m > engine.CurrentContext.EvaluationStack.Count) { return(false); } signatures = new byte[m][]; for (int i = 0; i < m; i++) { signatures[i] = engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToArray(); } } bool fSuccess = true; try { for (int i = 0, j = 0; fSuccess && i < m && j < n;) { if (Cryptography.Crypto.VerifySignature(message, signatures[i], pubkeys[j])) { i++; } j++; if (m - i > n - j) { fSuccess = false; } } } catch (ArgumentException) { fSuccess = false; } engine.CurrentContext.EvaluationStack.Push(fSuccess); return(true); } }
public void TestSerialize() { byte[] result1 = StackItemSerializer.Serialize(new byte[5]); byte[] expectedArray1 = new byte[] { 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray1), Encoding.Default.GetString(result1)); byte[] result2 = StackItemSerializer.Serialize(true); byte[] expectedArray2 = new byte[] { 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray2), Encoding.Default.GetString(result2)); byte[] result3 = StackItemSerializer.Serialize(1); byte[] expectedArray3 = new byte[] { 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray3), Encoding.Default.GetString(result3)); StackItem stackItem4 = new InteropInterface <object>(new object()); Action action4 = () => StackItemSerializer.Serialize(stackItem4); action4.Should().Throw <NotSupportedException>(); byte[] result5 = StackItemSerializer.Serialize(1); byte[] expectedArray5 = new byte[] { 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray5), Encoding.Default.GetString(result5)); List <StackItem> list6 = new List <StackItem> { 1 }; StackItem stackItem62 = new VM.Types.Array(list6); byte[] result6 = StackItemSerializer.Serialize(stackItem62); byte[] expectedArray6 = new byte[] { 0x80, 0x01, 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray6), Encoding.Default.GetString(result6)); List <StackItem> list7 = new List <StackItem> { 1 }; StackItem stackItem72 = new Struct(list7); byte[] result7 = StackItemSerializer.Serialize(stackItem72); byte[] expectedArray7 = new byte[] { 0x81, 0x01, 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray7), Encoding.Default.GetString(result7)); Dictionary <PrimitiveType, StackItem> list8 = new Dictionary <PrimitiveType, StackItem> { [2] = 1 }; StackItem stackItem82 = new Map(list8); byte[] result8 = StackItemSerializer.Serialize(stackItem82); byte[] expectedArray8 = new byte[] { 0x82, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray8), Encoding.Default.GetString(result8)); Map stackItem91 = new Map(); stackItem91.Add(1, stackItem91); Action action9 = () => StackItemSerializer.Serialize(stackItem91); action9.Should().Throw <NotSupportedException>(); VM.Types.Array stackItem10 = new VM.Types.Array(); stackItem10.Add(stackItem10); Action action10 = () => StackItemSerializer.Serialize(stackItem10); action10.Should().Throw <NotSupportedException>(); }
public void TestSerialize() { StackItem stackItem1 = new ByteArray(new byte[5]); byte[] result1 = Neo.SmartContract.Helper.Serialize(stackItem1); byte[] expectedArray1 = new byte[] { 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray1), Encoding.Default.GetString(result1)); StackItem stackItem2 = new VM.Types.Boolean(true); byte[] result2 = Neo.SmartContract.Helper.Serialize(stackItem2); byte[] expectedArray2 = new byte[] { 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray2), Encoding.Default.GetString(result2)); StackItem stackItem3 = new VM.Types.Integer(1); byte[] result3 = Neo.SmartContract.Helper.Serialize(stackItem3); byte[] expectedArray3 = new byte[] { 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray3), Encoding.Default.GetString(result3)); StackItem stackItem4 = new InteropInterface <object>(new object()); Action action4 = () => Neo.SmartContract.Helper.Serialize(stackItem4); action4.Should().Throw <NotSupportedException>(); StackItem stackItem5 = new VM.Types.Integer(1); byte[] result5 = Neo.SmartContract.Helper.Serialize(stackItem5); byte[] expectedArray5 = new byte[] { 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray5), Encoding.Default.GetString(result5)); StackItem stackItem61 = new VM.Types.Integer(1); List <StackItem> list6 = new List <StackItem> { stackItem61 }; StackItem stackItem62 = new VM.Types.Array(list6); byte[] result6 = Neo.SmartContract.Helper.Serialize(stackItem62); byte[] expectedArray6 = new byte[] { 0x80, 0x01, 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray6), Encoding.Default.GetString(result6)); StackItem stackItem71 = new VM.Types.Integer(1); List <StackItem> list7 = new List <StackItem> { stackItem71 }; StackItem stackItem72 = new VM.Types.Struct(list7); byte[] result7 = Neo.SmartContract.Helper.Serialize(stackItem72); byte[] expectedArray7 = new byte[] { 0x81, 0x01, 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray7), Encoding.Default.GetString(result7)); StackItem stackItem81 = new VM.Types.Integer(1); Dictionary <PrimitiveType, StackItem> list8 = new Dictionary <PrimitiveType, StackItem> { { new VM.Types.Integer(2), stackItem81 } }; StackItem stackItem82 = new VM.Types.Map(list8); byte[] result8 = Neo.SmartContract.Helper.Serialize(stackItem82); byte[] expectedArray8 = new byte[] { 0x82, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray8), Encoding.Default.GetString(result8)); Integer stackItem9 = new VM.Types.Integer(1); Map stackItem91 = new VM.Types.Map(); stackItem91.Add(stackItem9, stackItem91); Action action9 = () => Neo.SmartContract.Helper.Serialize(stackItem91); action9.Should().Throw <NotSupportedException>(); VM.Types.Array stackItem10 = new VM.Types.Array(); stackItem10.Add(stackItem10); Action action10 = () => Neo.SmartContract.Helper.Serialize(stackItem10); action10.Should().Throw <NotSupportedException>(); }