From 21f74197c830080c0999f96e75f8ffb1b897486e Mon Sep 17 00:00:00 2001 From: Jimmy Date: Tue, 20 Feb 2024 01:39:56 +0800 Subject: [PATCH 01/19] add examples and ease contract implementation --- examples/Directory.Build.props | 28 ++ .../ContractCall.cs | 41 ++ .../Example.SmartContract.ContractCall.csproj | 9 + examples/Example.SmartContract.Event/Event.cs | 43 ++ .../Example.SmartContract.Event.csproj | 9 + .../Example.SmartContract.Exception.csproj | 9 + .../Exception.cs | 428 ++++++++++++++++++ .../Example.SmartContract.HelloWorld.csproj | 9 + .../HelloWorld.cs | 58 +++ .../Example.SmartContract.Inscription.csproj | 9 + .../Inscription.cs | 76 ++++ .../Example.SmartContract.Modifier.csproj | 9 + .../Modifier.cs | 51 +++ .../Example.SmartContract.NEP17.csproj | 9 + .../Example.SmartContract.NEP17/Nep17Token.cs | 164 +++++++ .../Example.SmartContract.NFT.csproj | 9 + .../Example.SmartContract.NFT/Loot.Admin.cs | 99 ++++ examples/Example.SmartContract.NFT/Loot.cs | 228 ++++++++++ examples/Example.SmartContract.NFT/README.md | 118 +++++ .../Example.SmartContract.NFT/Token.Item.cs | 192 ++++++++ examples/Example.SmartContract.NFT/Token.cs | 39 ++ examples/Example.SmartContract.NFT/Tools.cs | 29 ++ .../Example.SmartContract.Oracle.csproj | 9 + .../Example.SmartContract.Oracle/Oracle.cs | 97 ++++ .../Example.SmartContract.Storage.csproj | 9 + .../Example.SmartContract.Storage/Storage.cs | 241 ++++++++++ .../Example.SmartContract.Transfer.csproj | 9 + .../TransferContract.cs | 61 +++ .../Example.SmartContract.ZKP.csproj | 8 + examples/Example.SmartContract.ZKP/ZKP.cs | 106 +++++ examples/examples.sln | 103 +++++ neo-devpack-dotnet.sln | 87 ++++ src/Neo.Compiler.CSharp/CompilationContext.cs | 25 +- .../ContractManifestExtension.cs | 35 +- .../MethodConvert/MethodConvert.cs | 20 +- .../Attributes/AuthorAttribute.cs | 12 - .../Attributes/ByteArrayAttribute.cs | 22 + .../Attributes/ContractAuthorAttribute.cs | 16 + .../ContractDescriptionAttribute.cs | 12 + .../Attributes/ContractEmailAttribute.cs | 12 + .../Attributes/ContractVersionAttribute.cs | 12 + .../Attributes/DescriptionAttribute.cs | 12 - .../Attributes/EmailAttribute.cs | 12 - .../Attributes/Hash160Attribute.cs | 22 + .../Attributes/InitialValueAttribute.cs | 9 +- .../Attributes/ManifestExtraAttribute.cs | 14 +- .../Attributes/NEPStandard.cs | 24 - .../Attributes/PublicKeyAttribute.cs | 22 + .../Attributes/StringAttribute.cs | 22 + .../Attributes/SupportedStandardsAttribute.cs | 2 +- .../Attributes/VersionAttribute.cs | 12 - src/Neo.SmartContract.Framework/ByteString.cs | 10 +- src/Neo.SmartContract.Framework/Method.cs | 9 + .../NEPStandard.cs | 24 + .../Neo.SmartContract.Framework.csproj | 4 + src/Neo.SmartContract.Framework/Nep11Token.cs | 14 +- src/Neo.SmartContract.Framework/Nep17Token.cs | 16 +- src/Neo.SmartContract.Framework/Permission.cs | 6 + .../templates/neocontract/Contract1.cs | 6 +- .../neocontractnep17/Nep17Contract.cs | 4 +- .../templates/neocontractoracle/Contract1.cs | 6 +- .../templates/neocontractowner/Contract1.cs | 6 +- .../Contract_ABIAttributes.cs | 3 +- .../Contract_Foreach.cs | 2 +- .../Contract_StaticVar.cs | 7 +- .../Contract_TryCatch.cs | 8 +- .../Contract_Types_ECPoint.cs | 2 +- .../Contract_UIntTypes.cs | 2 +- .../Contract_Helper.cs | 4 +- .../Contract_ManifestAttribute.cs | 8 +- .../Contract_SupportedStandard11Enum.cs | 2 +- .../Contract_SupportedStandard17Enum.cs | 6 +- 72 files changed, 2695 insertions(+), 167 deletions(-) create mode 100644 examples/Directory.Build.props create mode 100644 examples/Example.SmartContract.ContractCall/ContractCall.cs create mode 100644 examples/Example.SmartContract.ContractCall/Example.SmartContract.ContractCall.csproj create mode 100644 examples/Example.SmartContract.Event/Event.cs create mode 100644 examples/Example.SmartContract.Event/Example.SmartContract.Event.csproj create mode 100644 examples/Example.SmartContract.Exception/Example.SmartContract.Exception.csproj create mode 100644 examples/Example.SmartContract.Exception/Exception.cs create mode 100644 examples/Example.SmartContract.HelloWorld/Example.SmartContract.HelloWorld.csproj create mode 100644 examples/Example.SmartContract.HelloWorld/HelloWorld.cs create mode 100644 examples/Example.SmartContract.Inscription/Example.SmartContract.Inscription.csproj create mode 100644 examples/Example.SmartContract.Inscription/Inscription.cs create mode 100644 examples/Example.SmartContract.Modifier/Example.SmartContract.Modifier.csproj create mode 100644 examples/Example.SmartContract.Modifier/Modifier.cs create mode 100644 examples/Example.SmartContract.NEP17/Example.SmartContract.NEP17.csproj create mode 100644 examples/Example.SmartContract.NEP17/Nep17Token.cs create mode 100644 examples/Example.SmartContract.NFT/Example.SmartContract.NFT.csproj create mode 100644 examples/Example.SmartContract.NFT/Loot.Admin.cs create mode 100644 examples/Example.SmartContract.NFT/Loot.cs create mode 100644 examples/Example.SmartContract.NFT/README.md create mode 100644 examples/Example.SmartContract.NFT/Token.Item.cs create mode 100644 examples/Example.SmartContract.NFT/Token.cs create mode 100644 examples/Example.SmartContract.NFT/Tools.cs create mode 100644 examples/Example.SmartContract.Oracle/Example.SmartContract.Oracle.csproj create mode 100644 examples/Example.SmartContract.Oracle/Oracle.cs create mode 100644 examples/Example.SmartContract.Storage/Example.SmartContract.Storage.csproj create mode 100644 examples/Example.SmartContract.Storage/Storage.cs create mode 100644 examples/Example.SmartContract.Transfer/Example.SmartContract.Transfer.csproj create mode 100644 examples/Example.SmartContract.Transfer/TransferContract.cs create mode 100644 examples/Example.SmartContract.ZKP/Example.SmartContract.ZKP.csproj create mode 100644 examples/Example.SmartContract.ZKP/ZKP.cs create mode 100644 examples/examples.sln delete mode 100644 src/Neo.SmartContract.Framework/Attributes/AuthorAttribute.cs create mode 100644 src/Neo.SmartContract.Framework/Attributes/ByteArrayAttribute.cs create mode 100644 src/Neo.SmartContract.Framework/Attributes/ContractAuthorAttribute.cs create mode 100644 src/Neo.SmartContract.Framework/Attributes/ContractDescriptionAttribute.cs create mode 100644 src/Neo.SmartContract.Framework/Attributes/ContractEmailAttribute.cs create mode 100644 src/Neo.SmartContract.Framework/Attributes/ContractVersionAttribute.cs delete mode 100644 src/Neo.SmartContract.Framework/Attributes/DescriptionAttribute.cs delete mode 100644 src/Neo.SmartContract.Framework/Attributes/EmailAttribute.cs create mode 100644 src/Neo.SmartContract.Framework/Attributes/Hash160Attribute.cs delete mode 100644 src/Neo.SmartContract.Framework/Attributes/NEPStandard.cs create mode 100644 src/Neo.SmartContract.Framework/Attributes/PublicKeyAttribute.cs create mode 100644 src/Neo.SmartContract.Framework/Attributes/StringAttribute.cs delete mode 100644 src/Neo.SmartContract.Framework/Attributes/VersionAttribute.cs create mode 100644 src/Neo.SmartContract.Framework/Method.cs create mode 100644 src/Neo.SmartContract.Framework/NEPStandard.cs create mode 100644 src/Neo.SmartContract.Framework/Permission.cs diff --git a/examples/Directory.Build.props b/examples/Directory.Build.props new file mode 100644 index 000000000..80064c08a --- /dev/null +++ b/examples/Directory.Build.props @@ -0,0 +1,28 @@ + + + + + 2024 The SharpNeo Examples Project + 3.6.2 + net7.0 + The SharpNeo Project + neo.png + https://github.com/neo-project/neo-devpack-dotnet/examples + MIT + README.md + git + https://github.com/R3E-Network/SharpNeo.git + true + snupkg + The SharpNeo Examples Project + true + + + + + + + + + + diff --git a/examples/Example.SmartContract.ContractCall/ContractCall.cs b/examples/Example.SmartContract.ContractCall/ContractCall.cs new file mode 100644 index 000000000..37f927e44 --- /dev/null +++ b/examples/Example.SmartContract.ContractCall/ContractCall.cs @@ -0,0 +1,41 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// ContractCall.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using System.ComponentModel; +using Neo; +using Neo.SmartContract; +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Services; +using System.Numerics; + +namespace ContractCall; + +[DisplayName("SampleContractCall")] +[ContractAuthor("core-dev")] +[ContractEmail("core@neo.org")] +[ContractVersion("0.0.1")] +[ContractDescription("A sample contract to demonstrate how to call a contract")] +[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.ContractCall")] +public class SampleContractCall : SmartContract +{ + [Hash160("0x13a83e059c2eedd5157b766d3357bc826810905e")] + private static readonly UInt160 DummyTarget; + + public static void onNEP17Payment(UInt160 from, BigInteger amount, BigInteger data) + { + UInt160 tokenHash = Runtime.CallingScriptHash; + if (!data.Equals(123)) return; + UInt160 @this = Runtime.ExecutingScriptHash; + BigInteger balanceOf = (BigInteger)Contract.Call(tokenHash, "balanceOf", CallFlags.All, @this); + Contract.Call(DummyTarget, "dummyMethod", CallFlags.All, @this, tokenHash, balanceOf); + } +} diff --git a/examples/Example.SmartContract.ContractCall/Example.SmartContract.ContractCall.csproj b/examples/Example.SmartContract.ContractCall/Example.SmartContract.ContractCall.csproj new file mode 100644 index 000000000..eccc3b6a5 --- /dev/null +++ b/examples/Example.SmartContract.ContractCall/Example.SmartContract.ContractCall.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + ContractCall + + + diff --git a/examples/Example.SmartContract.Event/Event.cs b/examples/Example.SmartContract.Event/Event.cs new file mode 100644 index 000000000..61ae0ea42 --- /dev/null +++ b/examples/Example.SmartContract.Event/Event.cs @@ -0,0 +1,43 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Event.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; +using System; +using System.ComponentModel; +using System.Numerics; + +namespace Event; + +[DisplayName("SampleEvent")] +[ContractAuthor("code-dev", "core@neo.org")] +[ContractDescription("A sample contract that demonstrates how to use Events")] +[ContractVersion("0.0.1")] +[ContractSourceCode("https://github.com/neo-project/samples")] +[ContractPermission(Permission.WildCard, Method.WildCard)] +public class SampleEvent : SmartContract +{ + [DisplayName("new_event_name")] + public static event Action event_name; + + public static event Action event2; + + public static bool Main() + { + byte[] ba = new byte[] { 0x01, 0x02, 0x03 }; + event_name(ba, "oi", 10); // will Example.SmartContract.Runtime.Notify: 'new_event_name', '\x01\x02\x03', 'oi', 10 + + event2(ba, 50); // will Example.SmartContract.Runtime.Notify: 'event2', '\x01\x02\x03', '\x32' + + return false; + } +} diff --git a/examples/Example.SmartContract.Event/Example.SmartContract.Event.csproj b/examples/Example.SmartContract.Event/Example.SmartContract.Event.csproj new file mode 100644 index 000000000..d641e6477 --- /dev/null +++ b/examples/Example.SmartContract.Event/Example.SmartContract.Event.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + Event + + + diff --git a/examples/Example.SmartContract.Exception/Example.SmartContract.Exception.csproj b/examples/Example.SmartContract.Exception/Example.SmartContract.Exception.csproj new file mode 100644 index 000000000..f0d365483 --- /dev/null +++ b/examples/Example.SmartContract.Exception/Example.SmartContract.Exception.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + Exception + + + diff --git a/examples/Example.SmartContract.Exception/Exception.cs b/examples/Example.SmartContract.Exception/Exception.cs new file mode 100644 index 000000000..04b441d4a --- /dev/null +++ b/examples/Example.SmartContract.Exception/Exception.cs @@ -0,0 +1,428 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Exception.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo; +using Neo.Cryptography.ECC; +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using System.ComponentModel; + +namespace Exception +{ + [DisplayName("SampleException")] + [ContractAuthor("core-dev", "core@neo.org")] + [ContractDescription("A sample contract to demonstrate how to handle exception")] + [ContractVersion("0.0.1")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Exception")] + [ContractPermission(Permission.WildCard, Method.WildCard)] + public class SampleException : SmartContract + { + [ByteArray("0a0b0c0d0E0F")] + private static readonly ByteString invalidECpoint = default; + [ByteArray("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9")] + private static readonly ByteString byteString2Ecpoint = default; + [Hash160("NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq")] + private static readonly ByteString validUInt160 = default; + [ByteArray("edcf8679104ec2911a4fe29ad7db232a493e5b990fb1da7af0c7b989948c8925")] + private static readonly byte[] validUInt256 = default; + public static object try01() + { + int v = 0; + try + { + v = 2; + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + public static object try02() + { + int v = 0; + try + { + v = 2; + throw new System.Exception(); + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + public static object try03() + { + int v = 0; + try + { + v = 2; + throwcall(); + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + public static object tryNest() + { + int v = 0; + try + { + try + { + v = 2; + throwcall(); + } + catch + { + v = 3; + throwcall(); + } + finally + { + throwcall(); + v++; + } + } + catch + { + v++; + } + return v; + } + + public static object tryFinally() + { + int v = 0; + try + { + v = 2; + } + finally + { + v++; + } + return v; + } + + public static object tryFinallyAndRethrow() + { + int v = 0; + try + { + v = 2; + throwcall(); + } + finally + { + v++; + } + return v; + } + + public static object tryCatch() + { + int v = 0; + try + { + v = 2; + throwcall(); + } + catch + { + v++; + } + return v; + } + + public static object tryWithTwoFinally() + { + int v = 0; + try + { + try + { + v++; + } + catch + { + v += 2; + } + finally + { + v += 3; + } + } + catch + { + v += 4; + } + finally + { + v += 5; + } + return v; + } + + public static object tryecpointCast() + { + int v = 0; + try + { + v = 2; + ECPoint pubkey = (ECPoint)invalidECpoint; + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + public static object tryvalidByteString2Ecpoint() + { + int v = 0; + try + { + v = 2; + ECPoint pubkey = (ECPoint)byteString2Ecpoint; + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + public static object tryinvalidByteArray2UInt160() + { + int v = 0; + try + { + v = 2; + UInt160 data = (UInt160)invalidECpoint; + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + public static object tryvalidByteArray2UInt160() + { + int v = 0; + try + { + v = 2; + UInt160 data = (UInt160)validUInt160; + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + public static object tryinvalidByteArray2UInt256() + { + int v = 0; + try + { + v = 2; + UInt256 data = (UInt256)invalidECpoint; + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + public static object tryvalidByteArray2UInt256() + { + int v = 0; + try + { + v = 2; + UInt256 data = (UInt256)validUInt256; + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + public static (int, object) tryNULL2Ecpoint_1() + { + int v = 0; + ECPoint data = (ECPoint)(new byte[33]); + try + { + v = 2; + data = (ECPoint)null; + } + catch + { + v = 3; + } + finally + { + v++; + if (data is null) + { + v++; + } + } + return (v, data); + } + + public static (int, object) tryNULL2Uint160_1() + { + int v = 0; + UInt160 data = (UInt160)(new byte[20]); + try + { + v = 2; + data = (UInt160)null; + } + catch + { + v = 3; + } + finally + { + v++; + if (data is null) + { + v++; + } + } + return (v, data); + } + + public static (int, object) tryNULL2Uint256_1() + { + int v = 0; + UInt256 data = (UInt256)(new byte[32]); + try + { + v = 2; + data = (UInt256)null; + } + catch + { + v = 3; + } + finally + { + v++; + if (data is null) + { + v++; + } + } + return (v, data); + } + + public static (int, object) tryNULL2Bytestring_1() + { + int v = 0; + ByteString data = "123"; + try + { + v = 2; + data = (ByteString)null; + } + catch + { + v = 3; + } + finally + { + v++; + if (data is null) + { + v++; + } + } + return (v, data); + } + + public static object throwcall() + { + throw new System.Exception(); + } + + public static object tryUncatchableException() + { + int v = 0; + try + { + v = 2; + throw new UncatchableException(); + } + catch + { + v = 3; + } + finally + { + v++; + } + return v; + } + + } +} diff --git a/examples/Example.SmartContract.HelloWorld/Example.SmartContract.HelloWorld.csproj b/examples/Example.SmartContract.HelloWorld/Example.SmartContract.HelloWorld.csproj new file mode 100644 index 000000000..3ef48ddcd --- /dev/null +++ b/examples/Example.SmartContract.HelloWorld/Example.SmartContract.HelloWorld.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + HelloWorld + + + diff --git a/examples/Example.SmartContract.HelloWorld/HelloWorld.cs b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs new file mode 100644 index 000000000..0dd0ccc5b --- /dev/null +++ b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs @@ -0,0 +1,58 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// HelloWorld.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; + +using System.ComponentModel; + +namespace HelloWorld; + +[DisplayName("Example.SmartContract.HelloWorld")] +[ContractDescription("A simple `hello world` contract")] +[ContractEmail("core@neo.org")] +[ContractVersion("0.0.1")] +[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/HelloWorld")] +[ContractPermission(Permission.WildCard, Method.WildCard)] +public class HelloWorldorld : SmartContract +{ + [Safe] + public static string SayHello() + { + return "Hello, World!"; + } + + [DisplayName("_deploy")] + public static void OnDeployment(object data, bool update) + { + if (update) + { + // Add logic for fixing contract on update + return; + } + // Add logic here for 1st time deployed + } + + // TODO: Allow ONLY contract owner to call update + public static bool Update(ByteString nefFile, string manifest) + { + ContractManagement.Update(nefFile, manifest); + return true; + } + + // TODO: Allow ONLY contract owner to call destroy + public static bool Destroy() + { + ContractManagement.Destroy(); + return true; + } +} diff --git a/examples/Example.SmartContract.Inscription/Example.SmartContract.Inscription.csproj b/examples/Example.SmartContract.Inscription/Example.SmartContract.Inscription.csproj new file mode 100644 index 000000000..370a12122 --- /dev/null +++ b/examples/Example.SmartContract.Inscription/Example.SmartContract.Inscription.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + Inscription + + + diff --git a/examples/Example.SmartContract.Inscription/Inscription.cs b/examples/Example.SmartContract.Inscription/Inscription.cs new file mode 100644 index 000000000..4e4c8b19f --- /dev/null +++ b/examples/Example.SmartContract.Inscription/Inscription.cs @@ -0,0 +1,76 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Inscription.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo; +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; +using Neo.SmartContract.Framework.Services; +using System; +using System.ComponentModel; + +namespace Inscription +{ + [DisplayName("SampleInscription")] + [ContractAuthor("core-dev", "core@neo.org")] + [ContractDescription("A sample inscription contract.")] + [ContractVersion("0.0.1")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Inscription")] + [ContractPermission(Permission.WildCard, Method.WildCard)] + public class SampleInscription : SmartContract + { + // Neo.SmartContract.Examples.Event for logging inscriptions + [DisplayName("InscriptionAdded")] + public static event Action InscriptionAdded; + + // Method to store an inscription + public static void AddInscription(UInt160 address, string inscription) + { + if (!Runtime.CheckWitness(address)) + throw new Exception("Unauthorized: Caller is not the address owner"); + + Storage.Put(Storage.CurrentContext, address, inscription); + InscriptionAdded(address, inscription); + } + + // Method to read an inscription + [Safe] + public static string GetInscription(UInt160 address) + { + return Storage.Get(Storage.CurrentContext, address); + } + + [DisplayName("_deploy")] + public static void OnDeployment(object data, bool update) + { + if (update) + { + // Add logic for fixing contract on update + return; + } + // Add logic here for 1st time deployed + } + + // TODO: Allow ONLY contract owner to call update + public static bool Update(ByteString nefFile, string manifest) + { + ContractManagement.Update(nefFile, manifest); + return true; + } + + // TODO: Allow ONLY contract owner to call destroy + public static bool Destroy() + { + ContractManagement.Destroy(); + return true; + } + } +} diff --git a/examples/Example.SmartContract.Modifier/Example.SmartContract.Modifier.csproj b/examples/Example.SmartContract.Modifier/Example.SmartContract.Modifier.csproj new file mode 100644 index 000000000..da6967e95 --- /dev/null +++ b/examples/Example.SmartContract.Modifier/Example.SmartContract.Modifier.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + Modifier + + + diff --git a/examples/Example.SmartContract.Modifier/Modifier.cs b/examples/Example.SmartContract.Modifier/Modifier.cs new file mode 100644 index 000000000..62ad7c340 --- /dev/null +++ b/examples/Example.SmartContract.Modifier/Modifier.cs @@ -0,0 +1,51 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Modifier.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo; +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; +using Neo.SmartContract.Framework.Services; +using System.ComponentModel; + +namespace Modifier +{ + public class OwnerOnlyAttribute : ModifierAttribute + { + readonly UInt160 _owner; + + public OwnerOnlyAttribute(string hex) + { + _owner = (UInt160)(byte[])StdLib.Base64Decode(hex); + } + + public override void Enter() + { + if (!Runtime.CheckWitness(_owner)) throw new System.Exception(); + } + + public override void Exit() { } + } + + [DisplayName("SampleModifier")] + [ContractAuthor("core-dev", "core@neo.org")] + [ContractDescription("A sample contract to demonstrate how to use modifiers")] + [ContractVersion("0.0.1")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Exception")] + public class SampleModifier : SmartContract + { + [OwnerOnly("AAAAAAAAAAAAAAAAAAAAAAAAAAA=")] + public static bool Test() + { + return true; + } + } +} diff --git a/examples/Example.SmartContract.NEP17/Example.SmartContract.NEP17.csproj b/examples/Example.SmartContract.NEP17/Example.SmartContract.NEP17.csproj new file mode 100644 index 000000000..496917f66 --- /dev/null +++ b/examples/Example.SmartContract.NEP17/Example.SmartContract.NEP17.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + NEP17 + + + diff --git a/examples/Example.SmartContract.NEP17/Nep17Token.cs b/examples/Example.SmartContract.NEP17/Nep17Token.cs new file mode 100644 index 000000000..a7a4ece5c --- /dev/null +++ b/examples/Example.SmartContract.NEP17/Nep17Token.cs @@ -0,0 +1,164 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Nep17Token.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo; +using Neo.SmartContract; +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; +using Neo.SmartContract.Framework.Services; +using System; +using System.ComponentModel; +using System.Numerics; + +namespace NEP17 +{ + [DisplayName("SampleNep17Token")] + [ContractAuthor("core-dev", "core@neo.org")] + [ContractVersion("0.0.1")] + [ContractDescription("A sample NEP-17 token")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.NEP17")] + [ContractPermission(Permission.WildCard, Method.WildCard)] + [SupportedStandards(NepStandard.Nep17)] + public class SampleNep17Token : Nep17Token + { + #region Owner + + private const byte PrefixOwner = 0xff; + + [Hash160("NUuJw4C4XJFzxAvSZnFTfsNoWZytmQKXQP")] + private static readonly UInt160 InitialOwner = default; + + [Safe] + public static UInt160 GetOwner() + { + var currentOwner = Storage.Get(new[] { PrefixOwner }); + + if (currentOwner == null) + return InitialOwner; + + return (UInt160)currentOwner; + } + + private static bool IsOwner() => Runtime.CheckWitness(GetOwner()); + + public delegate void OnSetOwnerDelegate(UInt160 newOwner); + + [DisplayName("SetOwner")] + public static event OnSetOwnerDelegate OnSetOwner; + + public static void SetOwner(UInt160 newOwner) + { + if (IsOwner() == false) + throw new InvalidOperationException("No Authorization!"); + if (newOwner != null && newOwner.IsValid) + { + Storage.Put(new[] { PrefixOwner }, newOwner); + OnSetOwner(newOwner); + } + } + + #endregion + + #region Minter + + private const byte PrefixMinter = 0xfd; + + [Hash160("NUuJw4C4XJFzxAvSZnFTfsNoWZytmQKXQP")] + private static readonly UInt160 InitialMinter = default; + + [Safe] + public static UInt160 GetMinter() + { + var currentMinter = Storage.Get(new[] { PrefixMinter }); + + if (currentMinter == null) + return InitialMinter; + + return (UInt160)currentMinter; + } + + private static bool IsMinter() => Runtime.CheckWitness(GetMinter()); + + public delegate void OnSetMinterDelegate(UInt160 newMinter); + + [DisplayName("SetMinter")] + public static event OnSetMinterDelegate OnSetMinter; + + public static void SetMinter(UInt160 newMinter) + { + if (IsOwner() == false) + throw new InvalidOperationException("No Authorization!"); + if (newMinter is not { IsValid: true }) return; + Storage.Put(new[] { PrefixMinter }, newMinter); + OnSetMinter(newMinter); + } + + public new static void Mint(UInt160 to, BigInteger amount) + { + if (IsOwner() == false && IsMinter() == false) + throw new InvalidOperationException("No Authorization!"); + Nep17Token.Mint(to, amount); + } + + #endregion + + #region Example.SmartContract.NEP17 + + public override string Symbol { [Safe] get => "SampleNep17Token"; } + public override byte Decimals { [Safe] get => 8; } + + public new static void Burn(UInt160 account, BigInteger amount) + { + if (IsOwner() == false && IsMinter() == false) + throw new InvalidOperationException("No Authorization!"); + Nep17Token.Burn(account, amount); + } + + #endregion + + #region Payment + + public static bool Withdraw(UInt160 to, BigInteger amount) + { + if (IsOwner() == false) + throw new InvalidOperationException("No Authorization!"); + if (amount <= 0) + return false; + // TODO: Add logic + return true; + } + + // NOTE: Allow NEP-17 tokens to be received for this contract + public static void OnNEP17Payment(UInt160 from, BigInteger amount, object data) + { + // TODO: Add logic + } + + #endregion + + #region Basic + + [Safe] + public static bool Verify() => IsOwner(); + + public static bool Update(ByteString nefFile, string manifest) + { + if (IsOwner() == false) + throw new InvalidOperationException("No Authorization!"); + ContractManagement.Update(nefFile, manifest); + return true; + } + + #endregion + + } +} diff --git a/examples/Example.SmartContract.NFT/Example.SmartContract.NFT.csproj b/examples/Example.SmartContract.NFT/Example.SmartContract.NFT.csproj new file mode 100644 index 000000000..4a761d58a --- /dev/null +++ b/examples/Example.SmartContract.NFT/Example.SmartContract.NFT.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + NFT + + + diff --git a/examples/Example.SmartContract.NFT/Loot.Admin.cs b/examples/Example.SmartContract.NFT/Loot.Admin.cs new file mode 100644 index 000000000..941aeb53b --- /dev/null +++ b/examples/Example.SmartContract.NFT/Loot.Admin.cs @@ -0,0 +1,99 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Loot.Admin.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo; +using Neo.SmartContract; +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; +using Neo.SmartContract.Framework.Services; +using System.Runtime.CompilerServices; + +namespace NFT +{ + /// + /// Security Requirements: + /// All public functions in this partial class + /// that has write permission must be owner only + /// + /// [SetOwner] -- confirmed by jinghui + /// [_deploy] -- except this one, confirmed by jinghui + /// [Update] -- confirmed by jinghui + /// [Destroy] -- confirmed by jinghui + /// [Pause] -- confirmed by jinghui + /// [Resume] -- confirmed by jinghui + /// + /// + public partial class Loot + { + + [Hash160("NaA5nQieb5YGg5nSFjhJMVEXQCQ5HdukwP")] + static readonly UInt160 Owner = default; + + /// + /// Security requirement: + /// The prefix should be unique in the contract: checked globally. + /// + private static readonly StorageMap OwnerMap = new(Storage.CurrentContext, (byte)StoragePrefix.Owner); + + public static bool Verify() => Runtime.CheckWitness(GetOwner()); + + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void OwnerOnly() => Tools.Require(Verify(), "Authorization failed."); + + + /// + /// Security Requirements: + /// <0> Only the owner of the contract + /// are allowed to call this function: constrained internally + /// + /// <1> the new address should be + /// a valid address: constrained internally + /// + /// + /// + /// + public static UInt160 SetOwner(UInt160 newOwner) + { + // <0> -- confirmed by jinghui + OwnerOnly(); + // <1> -- confirmed by jinghui + Tools.Require(newOwner.IsValid, "Loot::UInt160 is invalid."); + OwnerMap.Put("owner", newOwner); + return GetOwner(); + } + + [Safe] + public static UInt160 GetOwner() + { + var owner = OwnerMap.Get("owner"); + return owner != null ? (UInt160)owner : Owner; + } + + public static void _deploy(object _, bool update) + { + if (update) return; + } + + public static void Update(ByteString nefFile, string manifest) + { + OwnerOnly(); + ContractManagement.Update(nefFile, manifest, null); + } + + public static void Destroy() + { + OwnerOnly(); + ContractManagement.Destroy(); + } + } +} diff --git a/examples/Example.SmartContract.NFT/Loot.cs b/examples/Example.SmartContract.NFT/Loot.cs new file mode 100644 index 000000000..5090bd9b2 --- /dev/null +++ b/examples/Example.SmartContract.NFT/Loot.cs @@ -0,0 +1,228 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Loot.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo; +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; +using Neo.SmartContract.Framework.Services; +using System; +using System.ComponentModel; +using System.Numerics; +using System.Runtime.CompilerServices; + +namespace NFT +{ + [DisplayName("Secure-Loot")] + [ContractAuthor("core-dev", "core@neo.org")] + [ContractDescription("This is a text Example.SmartContract.NFT")] + [SupportedStandards(NepStandard.Nep11)] + [ContractPermission(Permission.WildCard, Method.OnNEP11Payment)] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Loot")] + public partial class Loot : Nep11Token + { + public override string Symbol { [Safe] get => "sLoot"; } + + private static readonly StorageMap TokenIndexMap = new(Storage.CurrentContext, (byte)StoragePrefix.Token); + private static readonly StorageMap TokenMap = new(Storage.CurrentContext, Prefix_Token); + public static event Action EventMsg; + + [Safe] + public override Map Properties(ByteString tokenId) + { + Tools.Require(Runtime.EntryScriptHash == Runtime.CallingScriptHash); + StorageMap tokenMap = new(Storage.CurrentContext, Prefix_Token); + TokenState token = (TokenState)StdLib.Deserialize(tokenMap[tokenId]); + Map map = new() + { + ["name"] = token.Name, + ["owner"] = token.Owner, + ["tokenID"] = token.TokenId, + ["credential"] = token.Credential + }; + return map; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private TokenState GetToken(BigInteger tokenId) + { + TokenState token = (TokenState)StdLib.Deserialize(TokenMap[tokenId.ToString()]); + Tools.Require(token is not null, "Token not exists"); + return token; + } + + [Safe] + public BigInteger GetCredential(BigInteger tokenId) => GetToken(tokenId).Credential; + + [Safe] + public string GetWeapon(BigInteger credential) => Pluck(credential, 0xd7a595, _weapons); + + [Safe] + public string GetChest(BigInteger credential) => Pluck(credential, 0x5a7e36, _chestArmor); + + [Safe] + public string GetHead(BigInteger credential) => Pluck(credential, 0x0cdfbb, _headArmor); + + [Safe] + public string GetWaist(BigInteger credential) => Pluck(credential, 0x7dcd1b, _waistArmor); + + [Safe] + public string GetFoot(BigInteger credential) => Pluck(credential, 0x92877a, _footArmor); + + [Safe] + public string GetHand(BigInteger credential) => Pluck(credential, 0x323282, _handArmor); + + [Safe] + public string GetNeck(BigInteger credential) => Pluck(credential, 0x0d59c2, _necklaces); + + [Safe] + public string GetRing(BigInteger credential) => Pluck(credential, 0xfae431, _rings); + + [Safe] + private string Pluck(BigInteger credential, BigInteger keyPrefix, string[] sourceArray) + { + var rand = credential ^ keyPrefix; + var value = rand % sourceArray.Length; + string output = sourceArray[(int)rand % sourceArray.Length]; + var greatness = rand % 21; + + value = rand % _suffixes.Length; + if (greatness > 14) output = $"{output} {_suffixes[(int)value]}"; + if (greatness < 19) return output; + value = rand % _namePrefixes.Length; + var name0 = _namePrefixes[(int)value]; + value = rand % _nameSuffixes.Length; + var name1 = _nameSuffixes[(int)value]; + output = greatness == 19 ? $"\"{name0} {name1}\" {output}" : $"\"{name0} {name1}\" {output} +1"; + return output; + } + + + [Safe] + public string tokenURI(BigInteger tokenId) + { + var token = GetToken(tokenId); + var parts = new string[19]; + parts[0] = "" + + "" + + "" + + ""; + parts[1] = $"{token.Name}"; + parts[2] = ""; + parts[3] = GetWeapon(token.Credential); + parts[4] = ""; + parts[5] = GetChest(token.Credential); + parts[6] = ""; + parts[7] = GetHead(token.Credential); + parts[8] = ""; + parts[9] = GetWaist(token.Credential); + parts[10] = ""; + parts[11] = GetFoot(token.Credential); + parts[12] = ""; + parts[13] = GetHand(token.Credential); + parts[14] = ""; + parts[15] = GetNeck(token.Credential); + parts[16] = ""; + parts[17] = GetRing(token.Credential); + parts[18] = ""; + + string output = $"{parts[0]} {parts[1]} {parts[2]} {parts[3]} {parts[4]} {parts[5]} {parts[6]} {parts[7]} {parts[8]}"; + output = $"{output} {parts[9]} {parts[10]} {parts[11]} {parts[12]} {parts[13]} {parts[14]} {parts[15]} {parts[16]} {parts[17]} {parts[18]}"; + //string json = StdLib.Base64Encode($"{{\"name\": \"Bag # {tokenId.ToString()}\", \"description\": \"Loot is randomized adventurer gear generated and stored on chain.Stats, images, and other functionality are intentionally omitted for others to interpret.Feel free to use Loot in any way you want.\", \"image\": \"data:image / svg + xml; base64, { StdLib.Base64Encode(output)} \"}}"); + //output = $"data:application/json;base64, {json}"; + return output; + } + + /// + /// Security Requirements: + /// + /// <0> Has to check the validity of the token Id + /// both the upper and lower bound + /// + /// <1> shall not be called from a contract + /// + /// <3> tx shall fault if token already taken + /// + /// <4> update the token map. + /// + /// + public void Claim(BigInteger tokenId) + { + // 222 reserved to the developer + Tools.Require(!tokenId.IsZero && tokenId < 7778, "Token ID invalid"); + Tools.Require(Runtime.EntryScriptHash == Runtime.CallingScriptHash, "Contract calls are not allowed"); + Transaction tx = (Transaction)Runtime.ScriptContainer; + MintToken(tokenId, tx.Sender); + EventMsg("Player mints success"); + } + + /// + /// Security Requirements: + /// + /// <0> only the owner can call this function + /// + /// <1> the range of the tokenid is to be in (7777, 8001) + /// + /// + public void OwnerClaim(BigInteger tokenId) + { + OwnerOnly(); + Tools.Require(tokenId > 7777 && tokenId < 8001, "Token ID invalid"); + var sender = GetOwner(); + MintToken(tokenId, sender); + EventMsg("Owner mints success"); + } + + /// + /// Security Requirements: + /// + /// <0> the transaction should `FAULT` if the token already taken + /// + /// <1> has to update the taken map if a new token is mint. + /// + /// + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void MintToken(BigInteger tokenId, UInt160 sender) + { + var credential = CheckClaim(tokenId); + TokenState token = TokenState.MintLoot(sender, tokenId, credential); + Mint(tokenId.ToString(), token); + TokenIndexMap.Put(tokenId.ToString(), "taken"); + } + + /// + /// Security requirements: + /// + /// <0> throw exception if token already taken + /// + /// <1> should get a random number as credential that + /// is not predictable and not linked to the tokenId + /// + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private BigInteger CheckClaim(BigInteger tokenId) + { + // <0> -- confirmed + Tools.Require(TokenIndexMap.Get(tokenId.ToString()) != "taken", "Token already claimed."); + // <1> -- confirmed + return Runtime.GetRandom(); + } + } + + internal enum StoragePrefix + { + Owner = 0x15, + Token = 0x16, + } +} diff --git a/examples/Example.SmartContract.NFT/README.md b/examples/Example.SmartContract.NFT/README.md new file mode 100644 index 000000000..c034a9e69 --- /dev/null +++ b/examples/Example.SmartContract.NFT/README.md @@ -0,0 +1,118 @@ +# Loot +This is a migration of loot nft contract. +This also solves the random issue in loot. + +![7772](https://user-images.githubusercontent.com/10189511/132791851-8ef79f6a-cff3-448b-81f8-3db12d86b705.png) + +## Claim a new token + +```json +{ + "jsonrpc": "2.0", + "id": 1, + "method": "invokefunction", + "params": [ + "0xb78af146bd7aa6870bc5e005bb1134d5d1bfd2dc", + "claim", + [ + { + "type": "Integer", + "value": "7772" + } + ], + [ + { + "account": "0x8af673c2769fed72659b17217365b5077c173d9c", + "scopes": "CalledByEntry" + } + ] + ] +} +``` +`response` +```json +{ + "jsonrpc": "2.0", + "id": 1, + "result": { + "script": "AVseEcAfDAVjbGFpbQwU3NK/0dU0EbsF4MULh6Z6vUbxirdBYn1bUg==", + "state": "HALT", + "gasconsumed": "19857460", + "exception": null, + "stack": [ + { + "type": "Any" + } + ], + "tx": "AHhuDV00AC8BAAAAAKgDCQAAAAAAh3sEAAGcPRd8B7VlcyEXm2Vy7Z92wnP2igEAKAFbHhHAHwwFY2xhaW0MFNzSv9HVNBG7BeDFC4emer1G8Yq3QWJ9W1IBQgxAHJrQavDMCW7hWxb3OdnzDSB0/wUXwQeeb3sPf82uW3Y1kB5ODpjfqa671OVJpL/NtLes82NjYJ4LkAZ3Gzk3oygMIQKUFixhqSbk60uFf+WBp2lM7TvCcatKcpiNfG/QJQs5XUFW57Mn" + } +} +``` +copy the `tx` filed and send the transaction with `sendrawtranaction` API + +## Get the loot token by calling tokenURI + +```json +{ + "jsonrpc": "2.0", + "id": 1, + "method": "invokefunction", + "params": [ + "0xb78af146bd7aa6870bc5e005bb1134d5d1bfd2dc", + "tokenURI", + [ + { + "type": "Integer", + "value": "7772" + } + ] + ] +} +``` +`response` +```json +{ + "jsonrpc": "2.0", + "id": 1, + "result": { + "script": "AVweEcAfDAh0b2tlblVSSQwU3NK/0dU0EbsF4MULh6Z6vUbxirdBYn1bUg==", + "state": "HALT", + "gasconsumed": "6995805", + "exception": null, + "stack": [ + { + "type": "ByteString", + "value": "PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaW5ZTWluIG1lZXQiIHZpZXdCb3g9IjAgMCAzNTAgMzUwIj48c3R5bGU+LmJhc2UgeyBmaWxsOiB3aGl0ZTsgZm9udC1mYW1pbHk6IHNlcmlmOyBmb250LXNpemU6IDE0cHg7IH08L3N0eWxlPjxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9ImJsYWNrIiAvPjx0ZXh0IHg9IjEwIiB5PSIyMCIgY2xhc3M9ImJhc2UiPiBOMyBTZWN1cmUgTG9vdCAjNzc3MiA8L3RleHQ+PHRleHQgeD0iMTAiIHk9IjQwIiBjbGFzcz0iYmFzZSI+IE1hdWwgPC90ZXh0Pjx0ZXh0IHg9IjEwIiB5PSI2MCIgY2xhc3M9ImJhc2UiPiBTdHVkZGVkIExlYXRoZXIgQXJtb3Igb2YgUmFnZSA8L3RleHQ+PHRleHQgeD0iMTAiIHk9IjgwIiBjbGFzcz0iYmFzZSI+IEdyZWF0IEhlbG0gPC90ZXh0Pjx0ZXh0IHg9IjEwIiB5PSIxMDAiIGNsYXNzPSJiYXNlIj4gTGluZW4gU2FzaCA8L3RleHQ+PHRleHQgeD0iMTAiIHk9IjEyMCIgY2xhc3M9ImJhc2UiPiBEZW1vbmhpZGUgQm9vdHMgPC90ZXh0Pjx0ZXh0IHg9IjEwIiB5PSIxNDAiIGNsYXNzPSJiYXNlIj4gSGFyZCBMZWF0aGVyIEdsb3ZlcyA8L3RleHQ+PHRleHQgeD0iMTAiIHk9IjE2MCIgY2xhc3M9ImJhc2UiPiBOZWNrbGFjZSA8L3RleHQ+PHRleHQgeD0iMTAiIHk9IjE4MCIgY2xhc3M9ImJhc2UiPiAiUmFnZSBQZWFrIiBCcm9uemUgUmluZyBvZiBSZWZsZWN0aW9uICsxPC90ZXh0Pjwvc3ZnPg==" + } + ], + "tx": "AB8q7X1dv2oAAAAAAIQJCQAAAAAAi3sEAAGcPRd8B7VlcyEXm2Vy7Z92wnP2igEAKwFcHhHAHwwIdG9rZW5VUkkMFNzSv9HVNBG7BeDFC4emer1G8Yq3QWJ9W1IBQgxAsdoIJKmcQDp8gmLOiN96ElbIBjeEQ0AebCC5tkK6s7B36V6EhZXF1cTGpPB9OOGedvc6iZWlfEKXhKntCEN2gCgMIQKUFixhqSbk60uFf+WBp2lM7TvCcatKcpiNfG/QJQs5XUFW57Mn" + } +} +``` + +Decode the `value` with base64: +```html + + + + N3 Secure Loot #7772 + Maul + Studded Leather Armor of Rage + Great Helm + Linen Sash + Demonhide Boots + Hard Leather Gloves + Necklace + "Rage Peak" Bronze Ring of Reflection +1 + +``` + + + + diff --git a/examples/Example.SmartContract.NFT/Token.Item.cs b/examples/Example.SmartContract.NFT/Token.Item.cs new file mode 100644 index 000000000..15ebaa301 --- /dev/null +++ b/examples/Example.SmartContract.NFT/Token.Item.cs @@ -0,0 +1,192 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Token.Item.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +namespace NFT +{ + public partial class Loot + { + private readonly string[] _weapons = { + "Warhammer", + "Quarterstaff", + "Maul", + "Mace", + "Club", + "Katana", + "Falchion", + "Scimitar", + "Long Sword", + "Short Sword", + "Ghost Wand", + "Grave Wand", + "Bone Wand", + "Wand", + "Grimoire", + "Chronicle", + "Tome", + "Book" + }; + + private readonly string[] _chestArmor = { + "Divine Robe", + "Silk Robe", + "Linen Robe", + "Robe", + "Shirt", + "Demon Husk", + "Dragonskin Armor", + "Studded Leather Armor", + "Hard Leather Armor", + "Leather Armor", + "Holy Chestplate", + "Ornate Chestplate", + "Plate Mail", + "Chain Mail", + "Ring Mail" + }; + + private readonly string[] _headArmor = { + "Ancient Helm", + "Ornate Helm", + "Great Helm", + "Full Helm", + "Helm", + "Demon Crown", + "Dragon's Crown", + "War Cap", + "Leather Cap", + "Cap", + "Crown", + "Divine Hood", + "Silk Hood", + "Linen Hood", + "Hood" + }; + + private readonly string[] _waistArmor = { + "Ornate Belt", + "War Belt", + "Plated Belt", + "Mesh Belt", + "Heavy Belt", + "Demonhide Belt", + "Dragonskin Belt", + "Studded Leather Belt", + "Hard Leather Belt", + "Leather Belt", + "Brightsilk Sash", + "Silk Sash", + "Wool Sash", + "Linen Sash", + "Sash" + }; + + private readonly string[] _footArmor = { + "Holy Greaves", + "Ornate Greaves", + "Greaves", + "Chain Boots", + "Heavy Boots", + "Demonhide Boots", + "Dragonskin Boots", + "Studded Leather Boots", + "Hard Leather Boots", + "Leather Boots", + "Divine Slippers", + "Silk Slippers", + "Wool Shoes", + "Linen Shoes", + "Shoes" + }; + + private readonly string[] _handArmor = { + "Holy Gauntlets", + "Ornate Gauntlets", + "Gauntlets", + "Chain Gloves", + "Heavy Gloves", + "Demon's Hands", + "Dragonskin Gloves", + "Studded Leather Gloves", + "Hard Leather Gloves", + "Leather Gloves", + "Divine Gloves", + "Silk Gloves", + "Wool Gloves", + "Linen Gloves", + "Gloves" + }; + + private readonly string[] _necklaces = { + "Necklace", + "Amulet", + "Pendant" + }; + + private readonly string[] _rings = { + "Gold Ring", + "Silver Ring", + "Bronze Ring", + "Platinum Ring", + "Titanium Ring" + }; + + private readonly string[] _suffixes = { + "of Power", + "of Giants", + "of Titans", + "of Skill", + "of Perfection", + "of Brilliance", + "of Enlightenment", + "of Protection", + "of Anger", + "of Rage", + "of Fury", + "of Vitriol", + "of the Fox", + "of Detection", + "of Reflection", + "of the Twins" + }; + + private readonly string[] _namePrefixes = { + "Agony", "Apocalypse", "Armageddon", "Beast", "Behemoth", "Blight", "Blood", "Bramble", + "Brimstone", "Brood", "Carrion", "Cataclysm", "Chimeric", "Corpse", "Corruption", "Damnation", + "Death", "Demon", "Dire", "Dragon", "Dread", "Doom", "Dusk", "Eagle", "Empyrean", "Fate", "Foe", + "Gale", "Ghoul", "Gloom", "Glyph", "Golem", "Grim", "Hate", "Havoc", "Honour", "Horror", "Hypnotic", + "Kraken", "Loath", "Maelstrom", "Mind", "Miracle", "Morbid", "Oblivion", "Onslaught", "Pain", + "Pandemonium", "Phoenix", "Plague", "Rage", "Rapture", "Rune", "Skull", "Sol", "Soul", "Sorrow", + "Spirit", "Storm", "Tempest", "Torment", "Vengeance", "Victory", "Viper", "Vortex", "Woe", "Wrath", + "Light's", "Shimmering" + }; + + private readonly string[] _nameSuffixes = { + "Bane", + "Root", + "Bite", + "Song", + "Roar", + "Grasp", + "Instrument", + "Glow", + "Bender", + "Shadow", + "Whisper", + "Shout", + "Growl", + "Tear", + "Peak", + "Form", + "Sun", + "Moon" + }; + } +} diff --git a/examples/Example.SmartContract.NFT/Token.cs b/examples/Example.SmartContract.NFT/Token.cs new file mode 100644 index 000000000..5afb94727 --- /dev/null +++ b/examples/Example.SmartContract.NFT/Token.cs @@ -0,0 +1,39 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Token.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo; +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Services; +using System.Numerics; +namespace NFT +{ + public class TokenState : Nep11TokenState + { + public BigInteger TokenId; + + public BigInteger Credential; + + public static TokenState MintLoot(UInt160 owner, BigInteger tokenId, BigInteger credential) => new(owner, tokenId, credential); + + private TokenState(UInt160 owner, BigInteger tokenId, BigInteger credential) + { + Owner = owner; + TokenId = tokenId; + Credential = credential; + Name = "N3 Secure Loot #" + TokenId; + } + + public void OwnerOnly() + { + Tools.Require(Runtime.CheckWitness(Owner), "Authorization failed."); + } + } +} diff --git a/examples/Example.SmartContract.NFT/Tools.cs b/examples/Example.SmartContract.NFT/Tools.cs new file mode 100644 index 000000000..87acef4a2 --- /dev/null +++ b/examples/Example.SmartContract.NFT/Tools.cs @@ -0,0 +1,29 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Tools.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using System; +using System.Runtime.CompilerServices; + +namespace NFT +{ + static class Tools + { + /// + /// If the condition `istrue` does not hold, + /// then the transaction throw exception + /// making transaction `FAULT` + /// + /// true condition, has to be true to run + /// Transaction FAULT reason + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Require(bool isTrue, string msg = "Invalid") { if (!isTrue) throw new Exception($"Loot::{msg}"); } + } +} diff --git a/examples/Example.SmartContract.Oracle/Example.SmartContract.Oracle.csproj b/examples/Example.SmartContract.Oracle/Example.SmartContract.Oracle.csproj new file mode 100644 index 000000000..515e82b3b --- /dev/null +++ b/examples/Example.SmartContract.Oracle/Example.SmartContract.Oracle.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + Oracle + + + diff --git a/examples/Example.SmartContract.Oracle/Oracle.cs b/examples/Example.SmartContract.Oracle/Oracle.cs new file mode 100644 index 000000000..787af8aa3 --- /dev/null +++ b/examples/Example.SmartContract.Oracle/Oracle.cs @@ -0,0 +1,97 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Oracle.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; +using Neo.SmartContract.Framework.Services; +using System; +using System.ComponentModel; + +namespace Oracle +{ + [DisplayName("SampleOracle")] + [ContractAuthor("code-dev", "core@neo.org")] + [ContractDescription("A sample contract to demonstrate how to use Example.SmartContract.Oracle Service")] + [ContractVersion("0.0.1")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Oracle")] + [ContractPermission(Permission.WildCard, Method.WildCard)] + public class SampleOracle : SmartContract + { + + // (string requestedUrl, object jsonValue) + [DisplayName("RequestSuccessful")] + public static event Action OnRequestSuccessful; + public static void DoRequest() + { + /* + JSON DATA + { + "id": "6520ad3c12a5d3765988542a", + "record": { + "propertyName": "Hello World!" + }, + "metadata": { + "name": "Example.SmartContract.HelloWorld", + "readCountRemaining": 98, + "timeToExpire": 86379, + "createdAt": "2023-10-07T00:58:36.746Z" + } + } + See JSONPath format at https://github.com/atifaziz/JSONPath + JSONPath = "$.record.propertyName" + ReturnValue = ["Hello World!"] + ReturnValueType = string array + */ + var requestUrl = "https://api.jsonbin.io/v3/qs/6520ad3c12a5d3765988542a"; + Neo.SmartContract.Framework.Native.Oracle.Request(requestUrl, "$.record.propertyName", "onOracleResponse", null, Neo.SmartContract.Framework.Native.Oracle.MinimumResponseFee); + } + + // This method is called after the Example.SmartContract.Oracle receives response from requested URL + public static void OnOracleResponse(string requestedUrl, object userData, OracleResponseCode oracleResponse, string jsonString) + { + if (Runtime.CallingScriptHash != Neo.SmartContract.Framework.Native.Oracle.Hash) + throw new InvalidOperationException("No Authorization!"); + if (oracleResponse != OracleResponseCode.Success) + throw new Exception("Example.SmartContract.Oracle response failure with code " + (byte)oracleResponse); + + var jsonArrayValues = (object[])StdLib.JsonDeserialize(jsonString); + var jsonFirstValue = (string)jsonArrayValues[0]; + + OnRequestSuccessful(requestedUrl, jsonFirstValue); + } + + [DisplayName("_deploy")] + public static void OnDeployment(object data, bool update) + { + if (update) + { + // Add logic for fixing contract on update + return; + } + // Add logic here for 1st time deployed + } + + // TODO: Allow ONLY contract owner to call update + public static bool Update(ByteString nefFile, string manifest) + { + ContractManagement.Update(nefFile, manifest); + return true; + } + + // TODO: Allow ONLY contract owner to call destroy + public static bool Destroy() + { + ContractManagement.Destroy(); + return true; + } + } +} diff --git a/examples/Example.SmartContract.Storage/Example.SmartContract.Storage.csproj b/examples/Example.SmartContract.Storage/Example.SmartContract.Storage.csproj new file mode 100644 index 000000000..d1ccfe790 --- /dev/null +++ b/examples/Example.SmartContract.Storage/Example.SmartContract.Storage.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + Storage + + + diff --git a/examples/Example.SmartContract.Storage/Storage.cs b/examples/Example.SmartContract.Storage/Storage.cs new file mode 100644 index 000000000..04e0f01fc --- /dev/null +++ b/examples/Example.SmartContract.Storage/Storage.cs @@ -0,0 +1,241 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Storage.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo; +using Neo.Cryptography.ECC; +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Services; +using System.ComponentModel; + +namespace Storage +{ + [DisplayName("SampleStorage")] + [ContractAuthor("code-dev", "core@neo.org")] + [ContractDescription("A sample contract to demonstrate how to use storage")] + [ContractVersion("0.0.1")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Storage")] + [ContractPermission(Permission.WildCard, Method.WildCard)] + public class SampleStorage : SmartContract + { + #region Byte + + public static bool TestPutByte(byte[] key, byte[] value) + { + var storage = new StorageMap(Neo.SmartContract.Framework.Services.Storage.CurrentContext, 0x11); + storage.Put((ByteString)key, (ByteString)value); + return true; + } + + public static void TestDeleteByte(byte[] key) + { + var storage = new StorageMap(Neo.SmartContract.Framework.Services.Storage.CurrentContext, 0x11); + storage.Delete((ByteString)key); + } + + public static byte[] TestGetByte(byte[] key) + { + var context = Neo.SmartContract.Framework.Services.Storage.CurrentReadOnlyContext; + var storage = new StorageMap(context, 0x11); + var value = storage.Get((ByteString)key); + return (byte[])value; + } + + public static byte[] TestOver16Bytes() + { + var value = new byte[] { 0x3b, 0x00, 0x32, 0x03, 0x23, 0x23, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02 }; + StorageMap storageMap = new StorageMap(Neo.SmartContract.Framework.Services.Storage.CurrentContext, "test_map"); + storageMap.Put((ByteString)new byte[] { 0x01 }, (ByteString)value); + return (byte[])storageMap.Get((ByteString)new byte[] { 0x01 }); + } + + #endregion + + #region String + + public static bool TestPutString(byte[] key, byte[] value) + { + var prefix = "aa"; + var storage = new StorageMap(Neo.SmartContract.Framework.Services.Storage.CurrentContext, prefix); + storage.Put((ByteString)key, (ByteString)value); + return true; + } + + public static void TestDeleteString(byte[] key) + { + var prefix = "aa"; + var storage = new StorageMap(Neo.SmartContract.Framework.Services.Storage.CurrentContext, prefix); + storage.Delete((ByteString)key); + } + + public static byte[] TestGetString(byte[] key) + { + var prefix = "aa"; + var context = Neo.SmartContract.Framework.Services.Storage.CurrentReadOnlyContext; + var storage = new StorageMap(context, prefix); + var value = storage.Get((ByteString)key); + return (byte[])value; + } + + #endregion + + #region ByteArray + + public static bool TestPutByteArray(byte[] key, byte[] value) + { + var prefix = new byte[] { 0x00, 0xFF }; + var storage = new StorageMap(Neo.SmartContract.Framework.Services.Storage.CurrentContext, prefix); + storage.Put((ByteString)key, (ByteString)value); + return true; + } + + public static void TestDeleteByteArray(byte[] key) + { + var prefix = new byte[] { 0x00, 0xFF }; + var storage = new StorageMap(Neo.SmartContract.Framework.Services.Storage.CurrentContext, prefix); + storage.Delete((ByteString)key); + } + + public static byte[] TestGetByteArray(byte[] key) + { + var prefix = new byte[] { 0x00, 0xFF }; + var context = Neo.SmartContract.Framework.Services.Storage.CurrentContext.AsReadOnly; + var storage = new StorageMap(context, prefix); + var value = storage.Get((ByteString)key); + return (byte[])value; + } + + public static bool TestNewGetMethods() + { + var prefix = new byte[] { 0x00, 0xFF }; + var context = Neo.SmartContract.Framework.Services.Storage.CurrentContext; + var storage = new StorageMap(context, prefix); + + var boolValue = true; + var intValue = 123; + var stringValue = "hello world"; + var uint160Value = (UInt160)new byte[] { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 + }; + var uint256Value = (UInt256)new byte[] { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x00, 0x01 + }; + var ecPointValue = (ECPoint)new byte[] { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x00, 0x01, 0x02 + }; + + storage.Put("bool", boolValue); + storage.Put("int", intValue); + storage.Put("string", stringValue); + storage.Put("uint160", uint160Value); + storage.Put("uint256", uint256Value); + storage.Put("ecpoint", ecPointValue); + + var boolValue2 = storage.GetBoolean("bool"); + var intValue2 = storage.GetInteger("int"); + var stringValue2 = storage.GetString("string"); + var uint160Value2 = storage.GetUInt160("uint160"); + var uint256Value2 = storage.GetUInt256("uint256"); + var ecPointValue2 = storage.GetECPoint("ecpoint"); + + return boolValue == boolValue2 + && intValue == intValue2 + && stringValue == stringValue2 + && uint160Value == uint160Value2 + && uint256Value == uint256Value2 + && ecPointValue == ecPointValue2; + } + + public static byte[] TestNewGetByteArray() + { + var prefix = new byte[] { 0x00, 0xFF }; + var context = Neo.SmartContract.Framework.Services.Storage.CurrentContext; + var storage = new StorageMap(context, prefix); + var byteArray = new byte[] { 0x00, 0x01 }; + storage.Put("byteArray", byteArray); + var byteArray2 = storage.GetByteArray("byteArray"); + return byteArray2; + } + + #endregion + + public static bool TestPutReadOnly(byte[] key, byte[] value) + { + var prefix = new byte[] { 0x00, 0xFF }; + var context = Neo.SmartContract.Framework.Services.Storage.CurrentContext.AsReadOnly; + var storage = new StorageMap(context, prefix); + storage.Put((ByteString)key, (ByteString)value); + return true; + } + + #region Serialize + + class Value + { + public int Val; + } + + public static int SerializeTest(byte[] key, int value) + { + var prefix = new byte[] { 0x01, 0xAA }; + var context = Neo.SmartContract.Framework.Services.Storage.CurrentContext; + var storage = new StorageMap(context, prefix); + var val = new Value() { Val = value }; + storage.PutObject(key, val); + val = (Value)storage.GetObject(key); + return val.Val; + } + + #endregion + + #region Find + + public static byte[] TestFind() + { + var context = Neo.SmartContract.Framework.Services.Storage.CurrentContext; + Neo.SmartContract.Framework.Services.Storage.Put(context, (ByteString)"key1", (ByteString)new byte[] { 0x01 }); + Neo.SmartContract.Framework.Services.Storage.Put(context, (ByteString)"key2", (ByteString)new byte[] { 0x02 }); + Iterator iterator = (Iterator)Neo.SmartContract.Framework.Services.Storage.Find(context, "key", FindOptions.ValuesOnly); + iterator.Next(); + return iterator.Value; + } + + #endregion + + #region IndexProperty + + public static bool TestIndexPut(byte[] key, byte[] value) + { + var prefix = "ii"; + var storage = new StorageMap(Neo.SmartContract.Framework.Services.Storage.CurrentContext, prefix); + storage[(ByteString)key] = (ByteString)value; + return true; + } + + public static byte[] TestIndexGet(byte[] key) + { + var prefix = "ii"; + var context = Neo.SmartContract.Framework.Services.Storage.CurrentReadOnlyContext; + var storage = new StorageMap(context, prefix); + var value = storage[(ByteString)key]; + return (byte[])value; + } + + #endregion + } +} diff --git a/examples/Example.SmartContract.Transfer/Example.SmartContract.Transfer.csproj b/examples/Example.SmartContract.Transfer/Example.SmartContract.Transfer.csproj new file mode 100644 index 000000000..933ddad9a --- /dev/null +++ b/examples/Example.SmartContract.Transfer/Example.SmartContract.Transfer.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + disable + Transfer + + + diff --git a/examples/Example.SmartContract.Transfer/TransferContract.cs b/examples/Example.SmartContract.Transfer/TransferContract.cs new file mode 100644 index 000000000..af5c44a9e --- /dev/null +++ b/examples/Example.SmartContract.Transfer/TransferContract.cs @@ -0,0 +1,61 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// Contract1.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; + +using System.ComponentModel; + +namespace Transfer; + +/// +/// This is a sample contract that can be used as a template for creating new contracts. +/// +[DisplayName("Contract1")] +[ContractAuthor("", "")] +[ContractDescription("")] +[ContractVersion("1.0.0.0")] +[ContractSourceCode("https://github.com/cschuchardt88/neo-templates")] +[ContractPermission(Permission.WildCard, Method.WildCard)] +public class TransferContract : SmartContract +{ + [Safe] + public static string SayHello(string name) + { + return $"Hello, {name}"; + } + + [DisplayName("_deploy")] + public static void OnDeployment(object data, bool update) + { + if (update) + { + // Add logic for fixing contract on update + return; + } + // Add logic here for 1st time deployed + } + + // TODO: Allow ONLY contract owner to call update + public static bool Update(ByteString nefFile, string manifest) + { + ContractManagement.Update(nefFile, manifest); + return true; + } + + // TODO: Allow ONLY contract owner to call destroy + public static bool Destroy() + { + ContractManagement.Destroy(); + return true; + } +} diff --git a/examples/Example.SmartContract.ZKP/Example.SmartContract.ZKP.csproj b/examples/Example.SmartContract.ZKP/Example.SmartContract.ZKP.csproj new file mode 100644 index 000000000..6d339401e --- /dev/null +++ b/examples/Example.SmartContract.ZKP/Example.SmartContract.ZKP.csproj @@ -0,0 +1,8 @@ + + + + net7.0 + disable + ZKP + + diff --git a/examples/Example.SmartContract.ZKP/ZKP.cs b/examples/Example.SmartContract.ZKP/ZKP.cs new file mode 100644 index 000000000..ceb04c2f5 --- /dev/null +++ b/examples/Example.SmartContract.ZKP/ZKP.cs @@ -0,0 +1,106 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// ZKP.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Native; +using System; +using System.ComponentModel; + +namespace ZKP +{ + [DisplayName("SampleZKP")] + [ContractAuthor("code-dev")] + [ContractVersion("0.0.1")] + [ContractDescription("A sample contract to demonstrate how to use Example.SmartContract.ZKPil")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/ZKP")] + [ContractPermission(Permission.WildCard, Method.WildCard)] + public class SampleZKP : SmartContract + { + public static readonly byte[] alphaPoint = + { + 0, 106, 167, 155, 164, 170, 67, 158, 237, 78, 91, 7, 243, 191, 186, 221, 27, 97, 6, 190, 193, 204, 85, 206, 83, 56, 3, 209, 132, 249, 221, 94, 124, 20, 245, 113, 143, 70, 245, 159, 104, 213, 37, 151, 209, 125, 160, 143, 22, 78, 252, 97, 225, 215, 133, 240, 84, 149, 231, 142, 83, + 116, 156, 136, 120, 45, 242, 155, 199, 169, 244, 228, 221, 245, 160, 28, 247, 39, 83, 6, 172, 25, 126, 223, 231, 40, 94, 154, 69, 103, 123, 105, 242, 163, 156, 220 + }; + + public static readonly byte[] betaPoint = + { + 3, 91, 30, 20, 61, 202, 142, 33, 164, 33, 215, 106, 219, 39, 136, 96, 112, 254, 117, 55, 156, 44, 55, 125, 240, 63, 166, 206, 157, 17, 201, 11, 33, 172, 226, 58, 254, 202, 46, 128, 2, 179, 227, 37, 230, 127, 121, 118, 6, 59, 84, 145, 104, 196, 68, 37, 209, 54, 86, 148, 155, 251, + 36, 110, 127, 190, 205, 52, 100, 136, 226, 196, 249, 172, 122, 215, 230, 42, 92, 175, 190, 120, 19, 80, 56, 148, 236, 157, 108, 74, 45, 29, 157, 243, 96, 94, 17, 16, 242, 165, 56, 132, 223, 128, 0, 35, 238, 8, 138, 176, 102, 236, 242, 177, 252, 151, 152, 94, 230, 130, 111, 185, + 250, 195, 15, 125, 31, 128, 3, 102, 181, 56, 19, 195, 121, 224, 82, 228, 3, 41, 31, 122, 220, 133, 18, 212, 95, 194, 201, 185, 75, 111, 233, 98, 80, 47, 9, 191, 178, 119, 49, 238, 30, 235, 217, 40, 8, 199, 253, 123, 8, 85, 78, 100, 32, 111, 185, 57, 197, 240, 76, 6, 252, 16, 114, + 82, 90, 163, 240, 146, 4, 2 + }; + + public static readonly byte[] gamma_inversePoint = + { + 23, 69, 47, 108, 115, 173, 254, 203, 89, 67, 183, 224, 176, 26, 127, 132, 89, 162, 99, 241, 66, 228, 177, 17, 57, 85, 3, 13, 148, 88, 162, 54, 220, 189, 33, 172, 38, 192, 116, 236, 13, 115, 219, 201, 51, 166, 253, 240, 12, 32, 77, 82, 161, 189, 240, 198, 148, 184, 17, 92, 162, + 145, 166, 55, 252, 245, 194, 95, 71, 208, 215, 23, 19, 95, 138, 147, 149, 26, 35, 108, 141, 25, 139, 103, 59, 48, 189, 88, 204, 100, 255, 116, 194, 229, 157, 5, 19, 16, 31, 158, 222, 45, 151, 86, 218, 157, 17, 252, 29, 131, 121, 107, 168, 46, 145, 176, 122, 146, 93, 180, 181, 98, + 229, 4, 29, 34, 137, 222, 93, 124, 90, 211, 99, 51, 90, 96, 191, 203, 14, 34, 14, 5, 77, 209, 0, 62, 138, 150, 103, 94, 252, 200, 186, 64, 197, 27, 173, 229, 189, 193, 196, 75, 40, 95, 107, 36, 50, 90, 146, 59, 215, 202, 184, 77, 87, 20, 53, 241, 208, 72, 158, 45, 22, 81, 53, + 220, 40, 222, 26, 69, 230, 253 + }; + + public static readonly byte[] deltaPoint = + { + 1, 78, 83, 175, 159, 103, 127, 217, 80, 213, 0, 194, 108, 30, 210, 241, 138, 209, 0, 164, 117, 32, 68, 102, 121, 36, 40, 65, 89, 205, 198, 1, 14, 144, 196, 236, 176, 214, 119, 139, 225, 118, 215, 185, 36, 216, 183, 27, 22, 126, 193, 21, 173, 212, 250, 104, 25, 69, 107, 40, 199, + 160, 228, 239, 112, 102, 144, 85, 58, 109, 122, 73, 221, 170, 145, 188, 60, 9, 228, 178, 36, 227, 175, 140, 40, 181, 158, 175, 91, 189, 92, 169, 90, 90, 30, 153, 4, 225, 187, 53, 206, 114, 60, 109, 51, 184, 2, 100, 39, 95, 43, 33, 82, 141, 161, 200, 136, 146, 33, 18, 202, 141, + 43, 222, 64, 81, 151, 58, 141, 146, 8, 214, 159, 110, 167, 173, 253, 57, 190, 62, 94, 88, 245, 59, 20, 121, 233, 209, 122, 42, 13, 184, 114, 0, 19, 32, 120, 143, 108, 118, 107, 241, 218, 182, 69, 135, 117, 42, 231, 191, 199, 88, 88, 145, 134, 24, 133, 211, 53, 72, 23, 214, 105, + 97, 134, 254, 116, 89, 166, 119, 221, 223 + }; + + public static readonly byte[][] ic = new byte[][] + { + new byte[] + { + 14, 152, 253, 159, 101, 142, 227, 5, 166, 71, 152, 207, 32, 152, 56, 172, 191, 43, 184, 28, 148, 40, 224, 42, 135, 137, 181, 215, 96, 34, 200, 127, 77, 151, 165, 11, 130, 57, 91, 83, 71, 38, 253, 159, 103, 191, 139, 120, 20, 9, 91, 120, 106, 16, 209, 88, 87, 206, 209, 233, + 129, 87, 15, 139, 92, 164, 84, 150, 51, 92, 220, 188, 115, 217, 131, 193, 213, 23, 225, 128, 244, 135, 95, 128, 181, 127, 159, 195, 219, 176, 152, 16, 186, 80, 5, 143 + }, + new byte[] + { + 17, 158, 199, 19, 137, 211, 161, 248, 118, 149, 250, 145, 46, 221, 160, 86, 40, 165, 110, 198, 160, 203, 188, 84, 210, 83, 159, 176, 113, 111, 10, 235, 192, 243, 242, 110, 188, 210, 98, 199, 74, 66, 118, 251, 3, 188, 58, 84, 23, 55, 88, 168, 37, 240, 121, 248, 22, 139, 165, + 151, 47, 163, 72, 114, 80, 152, 28, 160, 76, 58, 252, 162, 110, 107, 202, 173, 57, 219, 79, 119, 196, 249, 84, 215, 233, 11, 59, 50, 204, 23, 149, 64, 88, 135, 163, 190 + } + }; + + /// + /// Verify '&' circuit.To verify x1&x2 = 0 or 1 + /// AB=alpha*beta+sum(pub_input[i]*(beta*u_i(x)+alpha*v_i(x)+w_i(x))/gamma)*gamma+C*delta + /// + /// Point A + /// Point B + /// Point C + /// Public paramters + /// result + public static bool Veify(byte[] a, byte[] b, byte[] c, byte[][] public_input) + { + //Equation left1: A*B + byte[] lt = (byte[])CryptoLib.Bls12381Pairing(a, b); + //Equation right1: alpha*beta + byte[] rt1 = (byte[])CryptoLib.Bls12381Pairing(alphaPoint, betaPoint); + //Equation right2: sum(pub_input[i]*(beta*u_i(x)+alpha*v_i(x)+w_i(x))/gamma)*gamma + int inputlen = public_input.Length; + int iclen = ic.Length; + if (iclen != inputlen + 1) throw new Exception("error: inputlen or iclen"); + byte[] acc = ic[0]; + for (int i = 0; i < inputlen; i++) + { + byte[] temp = (byte[])CryptoLib.Bls12381Mul(ic[i + 1], public_input[i]/*32-bytes LE field element.*/, false); + acc = (byte[])CryptoLib.Bls12381Add(acc, temp); + } + byte[] rt2 = (byte[])CryptoLib.Bls12381Pairing(acc, gamma_inversePoint); + //Equation right3: C*delta + byte[] rt3 = (byte[])CryptoLib.Bls12381Pairing(c, deltaPoint); + //Check equal + byte[] t1 = (byte[])CryptoLib.Bls12381Add(rt1, rt2); + byte[] t2 = (byte[])CryptoLib.Bls12381Add(t1, rt3); + return lt == t2; + } + } +} diff --git a/examples/examples.sln b/examples/examples.sln new file mode 100644 index 000000000..b32509950 --- /dev/null +++ b/examples/examples.sln @@ -0,0 +1,103 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HelloWorld", "HelloWorld\HelloWorld.csproj", "{945B07AF-282D-4C1C-9DF4-79B34383BE69}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Event", "Event\Event.csproj", "{835FC3FC-E99C-4AD1-A187-43C7C25CB990}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NEP17", "NEP17\NEP17.csproj", "{D7060EE6-EF53-42CA-922D-B6897BD65753}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFT", "NFT\NFT.csproj", "{16A18BAB-7D79-44ED-9D1F-8D6996D3C934}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFT-Divisible", "NFT-Divisible\NFT-Divisible.csproj", "{A40D7B53-D90D-4F0B-90DC-2842D3CBBE53}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Oracle", "Oracle\Oracle.csproj", "{098F4EF0-4F89-4D37-994F-1308DC656E33}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Runtime", "Runtime\Runtime.csproj", "{4562730E-0DAA-488E-8F29-260061EA4D07}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZKP", "ZKP\ZKP.csproj", "{EC4BC9EC-31F3-4130-B629-0298D7090E68}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Storage", "Storage\Storage.csproj", "{8CA5AA75-4886-488B-8738-1419B64B21EE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modifier", "Modifier\Modifier.csproj", "{BBB49141-0B82-4EB9-9C51-E110792CC759}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exception", "Exception\Exception.csproj", "{DD44BA11-7DF6-4CBD-B41D-8D33D273F774}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inscription", "Inscription\Inscription.csproj", "{B233DCE7-D97D-4BE3-8AE1-9A2416814C02}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exchange", "Exchange\Exchange.csproj", "{3199912E-35D4-4FA0-BA6B-497872A7828F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContractCall", "ContractCall\ContractCall.csproj", "{FF93B618-586B-429B-A85A-31E69BB6CB73}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {945B07AF-282D-4C1C-9DF4-79B34383BE69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {945B07AF-282D-4C1C-9DF4-79B34383BE69}.Debug|Any CPU.Build.0 = Debug|Any CPU + {945B07AF-282D-4C1C-9DF4-79B34383BE69}.Release|Any CPU.ActiveCfg = Release|Any CPU + {945B07AF-282D-4C1C-9DF4-79B34383BE69}.Release|Any CPU.Build.0 = Release|Any CPU + {835FC3FC-E99C-4AD1-A187-43C7C25CB990}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {835FC3FC-E99C-4AD1-A187-43C7C25CB990}.Debug|Any CPU.Build.0 = Debug|Any CPU + {835FC3FC-E99C-4AD1-A187-43C7C25CB990}.Release|Any CPU.ActiveCfg = Release|Any CPU + {835FC3FC-E99C-4AD1-A187-43C7C25CB990}.Release|Any CPU.Build.0 = Release|Any CPU + {D7060EE6-EF53-42CA-922D-B6897BD65753}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7060EE6-EF53-42CA-922D-B6897BD65753}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7060EE6-EF53-42CA-922D-B6897BD65753}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7060EE6-EF53-42CA-922D-B6897BD65753}.Release|Any CPU.Build.0 = Release|Any CPU + {16A18BAB-7D79-44ED-9D1F-8D6996D3C934}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {16A18BAB-7D79-44ED-9D1F-8D6996D3C934}.Debug|Any CPU.Build.0 = Debug|Any CPU + {16A18BAB-7D79-44ED-9D1F-8D6996D3C934}.Release|Any CPU.ActiveCfg = Release|Any CPU + {16A18BAB-7D79-44ED-9D1F-8D6996D3C934}.Release|Any CPU.Build.0 = Release|Any CPU + {A40D7B53-D90D-4F0B-90DC-2842D3CBBE53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A40D7B53-D90D-4F0B-90DC-2842D3CBBE53}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A40D7B53-D90D-4F0B-90DC-2842D3CBBE53}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A40D7B53-D90D-4F0B-90DC-2842D3CBBE53}.Release|Any CPU.Build.0 = Release|Any CPU + {098F4EF0-4F89-4D37-994F-1308DC656E33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {098F4EF0-4F89-4D37-994F-1308DC656E33}.Debug|Any CPU.Build.0 = Debug|Any CPU + {098F4EF0-4F89-4D37-994F-1308DC656E33}.Release|Any CPU.ActiveCfg = Release|Any CPU + {098F4EF0-4F89-4D37-994F-1308DC656E33}.Release|Any CPU.Build.0 = Release|Any CPU + {4562730E-0DAA-488E-8F29-260061EA4D07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4562730E-0DAA-488E-8F29-260061EA4D07}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4562730E-0DAA-488E-8F29-260061EA4D07}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4562730E-0DAA-488E-8F29-260061EA4D07}.Release|Any CPU.Build.0 = Release|Any CPU + {EC4BC9EC-31F3-4130-B629-0298D7090E68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EC4BC9EC-31F3-4130-B629-0298D7090E68}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC4BC9EC-31F3-4130-B629-0298D7090E68}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EC4BC9EC-31F3-4130-B629-0298D7090E68}.Release|Any CPU.Build.0 = Release|Any CPU + {8CA5AA75-4886-488B-8738-1419B64B21EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8CA5AA75-4886-488B-8738-1419B64B21EE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8CA5AA75-4886-488B-8738-1419B64B21EE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8CA5AA75-4886-488B-8738-1419B64B21EE}.Release|Any CPU.Build.0 = Release|Any CPU + {BBB49141-0B82-4EB9-9C51-E110792CC759}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BBB49141-0B82-4EB9-9C51-E110792CC759}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BBB49141-0B82-4EB9-9C51-E110792CC759}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BBB49141-0B82-4EB9-9C51-E110792CC759}.Release|Any CPU.Build.0 = Release|Any CPU + {DD44BA11-7DF6-4CBD-B41D-8D33D273F774}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DD44BA11-7DF6-4CBD-B41D-8D33D273F774}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DD44BA11-7DF6-4CBD-B41D-8D33D273F774}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DD44BA11-7DF6-4CBD-B41D-8D33D273F774}.Release|Any CPU.Build.0 = Release|Any CPU + {B233DCE7-D97D-4BE3-8AE1-9A2416814C02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B233DCE7-D97D-4BE3-8AE1-9A2416814C02}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B233DCE7-D97D-4BE3-8AE1-9A2416814C02}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B233DCE7-D97D-4BE3-8AE1-9A2416814C02}.Release|Any CPU.Build.0 = Release|Any CPU + {3199912E-35D4-4FA0-BA6B-497872A7828F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3199912E-35D4-4FA0-BA6B-497872A7828F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3199912E-35D4-4FA0-BA6B-497872A7828F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3199912E-35D4-4FA0-BA6B-497872A7828F}.Release|Any CPU.Build.0 = Release|Any CPU + {FF93B618-586B-429B-A85A-31E69BB6CB73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF93B618-586B-429B-A85A-31E69BB6CB73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF93B618-586B-429B-A85A-31E69BB6CB73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF93B618-586B-429B-A85A-31E69BB6CB73}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8E04F9CB-CF94-4AD1-8537-E7BDFFEB7D08} + EndGlobalSection +EndGlobal diff --git a/neo-devpack-dotnet.sln b/neo-devpack-dotnet.sln index 0ba3f1b26..78486592b 100644 --- a/neo-devpack-dotnet.sln +++ b/neo-devpack-dotnet.sln @@ -37,10 +37,37 @@ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Testing.UnitTests", "tests\Neo.SmartContract.Testing.UnitTests\Neo.SmartContract.Testing.UnitTests.csproj", "{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Template.UnitTests", "tests\Neo.SmartContract.Template.UnitTests\Neo.SmartContract.Template.UnitTests.csproj", "{17F45E0B-AB1C-4796-8C99-E5212A5592F8}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Extensions", "neo\src\Neo.Extensions\Neo.Extensions.csproj", "{E5EFB018-810D-4297-8921-940FA0B1ED97}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.IO", "neo\src\Neo.IO\Neo.IO.csproj", "{C2B7927F-AAA5-432A-8E76-B5080BD7EFB9}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{C10F9957-077F-42C8-B350-8607D4899B7E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.ContractCall", "examples\Example.SmartContract.ContractCall\Example.SmartContract.ContractCall.csproj", "{AE9BF707-FA85-41CB-BE04-3B0AD024329B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Event", "examples\Example.SmartContract.Event\Example.SmartContract.Event.csproj", "{826888EC-9EEF-42DA-A136-048C031D9CAB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Exception", "examples\Example.SmartContract.Exception\Example.SmartContract.Exception.csproj", "{07936699-8820-4D5B-9B3D-DF3FA944FE1E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.HelloWorld", "examples\Example.SmartContract.HelloWorld\Example.SmartContract.HelloWorld.csproj", "{43617A35-2E3A-43F5-80B3-685ADBF4F908}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Inscription", "examples\Example.SmartContract.Inscription\Example.SmartContract.Inscription.csproj", "{9AF351B8-E3E9-42A7-853A-C98FC159A9E5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Modifier", "examples\Example.SmartContract.Modifier\Example.SmartContract.Modifier.csproj", "{50E8D694-AC3C-456C-B901-3530E9AFF555}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.NEP17", "examples\Example.SmartContract.NEP17\Example.SmartContract.NEP17.csproj", "{FBDF111C-65C0-45E4-8D25-9BE78C35CDC3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.NFT", "examples\Example.SmartContract.NFT\Example.SmartContract.NFT.csproj", "{051A2A1E-2225-405F-82F6-79E4D5345DF6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Oracle", "examples\Example.SmartContract.Oracle\Example.SmartContract.Oracle.csproj", "{A39DA537-024A-430F-A3BA-034D6E947FA6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Storage", "examples\Example.SmartContract.Storage\Example.SmartContract.Storage.csproj", "{18A5C205-2102-4F4B-BB76-37660F74709C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Transfer", "examples\Example.SmartContract.Transfer\Example.SmartContract.Transfer.csproj", "{5319BE3E-A5E9-4AFF-94D6-A669DA214F24}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.ZKP", "examples\Example.SmartContract.ZKP\Example.SmartContract.ZKP.csproj", "{141AD5BA-1735-4583-93B6-145CF72721E5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -115,6 +142,54 @@ Global {C2B7927F-AAA5-432A-8E76-B5080BD7EFB9}.Debug|Any CPU.Build.0 = Debug|Any CPU {C2B7927F-AAA5-432A-8E76-B5080BD7EFB9}.Release|Any CPU.ActiveCfg = Release|Any CPU {C2B7927F-AAA5-432A-8E76-B5080BD7EFB9}.Release|Any CPU.Build.0 = Release|Any CPU + {AE9BF707-FA85-41CB-BE04-3B0AD024329B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AE9BF707-FA85-41CB-BE04-3B0AD024329B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AE9BF707-FA85-41CB-BE04-3B0AD024329B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AE9BF707-FA85-41CB-BE04-3B0AD024329B}.Release|Any CPU.Build.0 = Release|Any CPU + {826888EC-9EEF-42DA-A136-048C031D9CAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {826888EC-9EEF-42DA-A136-048C031D9CAB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {826888EC-9EEF-42DA-A136-048C031D9CAB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {826888EC-9EEF-42DA-A136-048C031D9CAB}.Release|Any CPU.Build.0 = Release|Any CPU + {07936699-8820-4D5B-9B3D-DF3FA944FE1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {07936699-8820-4D5B-9B3D-DF3FA944FE1E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {07936699-8820-4D5B-9B3D-DF3FA944FE1E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {07936699-8820-4D5B-9B3D-DF3FA944FE1E}.Release|Any CPU.Build.0 = Release|Any CPU + {43617A35-2E3A-43F5-80B3-685ADBF4F908}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {43617A35-2E3A-43F5-80B3-685ADBF4F908}.Debug|Any CPU.Build.0 = Debug|Any CPU + {43617A35-2E3A-43F5-80B3-685ADBF4F908}.Release|Any CPU.ActiveCfg = Release|Any CPU + {43617A35-2E3A-43F5-80B3-685ADBF4F908}.Release|Any CPU.Build.0 = Release|Any CPU + {9AF351B8-E3E9-42A7-853A-C98FC159A9E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AF351B8-E3E9-42A7-853A-C98FC159A9E5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AF351B8-E3E9-42A7-853A-C98FC159A9E5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AF351B8-E3E9-42A7-853A-C98FC159A9E5}.Release|Any CPU.Build.0 = Release|Any CPU + {50E8D694-AC3C-456C-B901-3530E9AFF555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {50E8D694-AC3C-456C-B901-3530E9AFF555}.Debug|Any CPU.Build.0 = Debug|Any CPU + {50E8D694-AC3C-456C-B901-3530E9AFF555}.Release|Any CPU.ActiveCfg = Release|Any CPU + {50E8D694-AC3C-456C-B901-3530E9AFF555}.Release|Any CPU.Build.0 = Release|Any CPU + {FBDF111C-65C0-45E4-8D25-9BE78C35CDC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FBDF111C-65C0-45E4-8D25-9BE78C35CDC3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FBDF111C-65C0-45E4-8D25-9BE78C35CDC3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FBDF111C-65C0-45E4-8D25-9BE78C35CDC3}.Release|Any CPU.Build.0 = Release|Any CPU + {051A2A1E-2225-405F-82F6-79E4D5345DF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {051A2A1E-2225-405F-82F6-79E4D5345DF6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {051A2A1E-2225-405F-82F6-79E4D5345DF6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {051A2A1E-2225-405F-82F6-79E4D5345DF6}.Release|Any CPU.Build.0 = Release|Any CPU + {A39DA537-024A-430F-A3BA-034D6E947FA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A39DA537-024A-430F-A3BA-034D6E947FA6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A39DA537-024A-430F-A3BA-034D6E947FA6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A39DA537-024A-430F-A3BA-034D6E947FA6}.Release|Any CPU.Build.0 = Release|Any CPU + {18A5C205-2102-4F4B-BB76-37660F74709C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {18A5C205-2102-4F4B-BB76-37660F74709C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {18A5C205-2102-4F4B-BB76-37660F74709C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {18A5C205-2102-4F4B-BB76-37660F74709C}.Release|Any CPU.Build.0 = Release|Any CPU + {5319BE3E-A5E9-4AFF-94D6-A669DA214F24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5319BE3E-A5E9-4AFF-94D6-A669DA214F24}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5319BE3E-A5E9-4AFF-94D6-A669DA214F24}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5319BE3E-A5E9-4AFF-94D6-A669DA214F24}.Release|Any CPU.Build.0 = Release|Any CPU + {141AD5BA-1735-4583-93B6-145CF72721E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {141AD5BA-1735-4583-93B6-145CF72721E5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {141AD5BA-1735-4583-93B6-145CF72721E5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {141AD5BA-1735-4583-93B6-145CF72721E5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -137,6 +212,18 @@ Global {17F45E0B-AB1C-4796-8C99-E5212A5592F8} = {D5266066-0AFD-44D5-A83E-2F73668A63C8} {E5EFB018-810D-4297-8921-940FA0B1ED97} = {49D5873D-7B38-48A5-B853-85146F032091} {C2B7927F-AAA5-432A-8E76-B5080BD7EFB9} = {49D5873D-7B38-48A5-B853-85146F032091} + {AE9BF707-FA85-41CB-BE04-3B0AD024329B} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {826888EC-9EEF-42DA-A136-048C031D9CAB} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {07936699-8820-4D5B-9B3D-DF3FA944FE1E} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {43617A35-2E3A-43F5-80B3-685ADBF4F908} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {9AF351B8-E3E9-42A7-853A-C98FC159A9E5} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {50E8D694-AC3C-456C-B901-3530E9AFF555} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {FBDF111C-65C0-45E4-8D25-9BE78C35CDC3} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {051A2A1E-2225-405F-82F6-79E4D5345DF6} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {A39DA537-024A-430F-A3BA-034D6E947FA6} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {18A5C205-2102-4F4B-BB76-37660F74709C} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {5319BE3E-A5E9-4AFF-94D6-A669DA214F24} = {C10F9957-077F-42C8-B350-8607D4899B7E} + {141AD5BA-1735-4583-93B6-145CF72721E5} = {C10F9957-077F-42C8-B350-8607D4899B7E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {6DA935E1-C674-4364-B087-F1B511B79215} diff --git a/src/Neo.Compiler.CSharp/CompilationContext.cs b/src/Neo.Compiler.CSharp/CompilationContext.cs index 57b3a7653..6171d44a3 100644 --- a/src/Neo.Compiler.CSharp/CompilationContext.cs +++ b/src/Neo.Compiler.CSharp/CompilationContext.cs @@ -30,6 +30,7 @@ using System.Runtime.CompilerServices; using System.Text; using System.Xml.Linq; +using scfx::Neo.SmartContract.Framework; using scfx::Neo.SmartContract.Framework.Attributes; using Diagnostic = Microsoft.CodeAnalysis.Diagnostic; @@ -42,18 +43,18 @@ public class CompilationContext private readonly Compilation compilation; private string? displayName, className; private bool scTypeFound; - private readonly List diagnostics = new(); + private readonly System.Collections.Generic.List diagnostics = new(); private readonly HashSet supportedStandards = new(); - private readonly List methodsExported = new(); - private readonly List eventsExported = new(); + private readonly System.Collections.Generic.List methodsExported = new(); + private readonly System.Collections.Generic.List eventsExported = new(); private readonly PermissionBuilder permissions = new(); private readonly HashSet trusts = new(); private readonly JObject manifestExtra = new(); private readonly MethodConvertCollection methodsConverted = new(); private readonly MethodConvertCollection methodsForward = new(); - private readonly List methodTokens = new(); + private readonly System.Collections.Generic.List methodTokens = new(); private readonly Dictionary staticFields = new(SymbolEqualityComparer.Default); - private readonly List anonymousStaticFields = new(); + private readonly System.Collections.Generic.List anonymousStaticFields = new(); private readonly Dictionary vtables = new(SymbolEqualityComparer.Default); private byte[]? script; @@ -163,7 +164,7 @@ public static CompilationContext Compile(IEnumerable sourceFiles, IEnume public static CompilationContext CompileSources(string[] sourceFiles, Options options) { - List references = new(commonReferences) + System.Collections.Generic.List references = new(commonReferences) { MetadataReference.CreateFromFile(typeof(scfx.Neo.SmartContract.Framework.SmartContract).Assembly.Location) }; @@ -177,7 +178,7 @@ public static Compilation GetCompilation(string csproj, Options options) HashSet sourceFiles = Directory.EnumerateFiles(folder, "*.cs", SearchOption.AllDirectories) .Where(p => !p.StartsWith(obj)) .ToHashSet(StringComparer.OrdinalIgnoreCase); - List references = new(commonReferences); + System.Collections.Generic.List references = new(commonReferences); CSharpCompilationOptions compilationOptions = new(OutputKind.DynamicallyLinkedLibrary, deterministic: true, nullableContextOptions: options.Nullable); XDocument document = XDocument.Load(csproj); sourceFiles.UnionWith(document.Root!.Elements("ItemGroup").Elements("Compile").Attributes("Include").Select(p => Path.GetFullPath(p.Value, folder))); @@ -338,11 +339,11 @@ public ContractManifest CreateManifest() public JObject CreateDebugInformation(string folder = "") { - List documents = new(); - List methods = new(); + System.Collections.Generic.List documents = new(); + System.Collections.Generic.List methods = new(); foreach (var m in methodsConverted.Where(p => p.SyntaxNode is not null)) { - List sequencePoints = new(); + System.Collections.Generic.List sequencePoints = new(); foreach (var ins in m.Instructions.Where(i => i.SourceLocation is not null)) { var doc = ins.SourceLocation!.SourceTree!.FilePath; @@ -461,8 +462,8 @@ private void ProcessClass(SemanticModel model, INamedTypeSymbol symbol) attribute.ConstructorArguments[0].Values .Select(p => p.Value) .Select(p => - p is int ip && Enum.IsDefined(typeof(NEPStandard), ip) - ? ((NEPStandard)ip).ToStandard() + p is int ip && Enum.IsDefined(typeof(NepStandard), ip) + ? ((NepStandard)ip).ToStandard() : p as string ) .Where(v => v != null)! // Ensure null values are not added diff --git a/src/Neo.Compiler.CSharp/ContractManifestExtension.cs b/src/Neo.Compiler.CSharp/ContractManifestExtension.cs index 439f76fb7..b87294bc5 100644 --- a/src/Neo.Compiler.CSharp/ContractManifestExtension.cs +++ b/src/Neo.Compiler.CSharp/ContractManifestExtension.cs @@ -16,6 +16,7 @@ using System.Linq; using Neo.SmartContract; using Neo.SmartContract.Manifest; +using scfx::Neo.SmartContract.Framework; using scfx::Neo.SmartContract.Framework.Attributes; namespace Neo.Compiler @@ -79,27 +80,27 @@ private static void CheckNep11Compliant(this ContractManifest manifest) a.Parameters[3].Type == ContractParameterType.ByteArray); if (!symbolValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP11.ToStandard()} implementation: symbol"); + $"Incomplete NEP standard {NepStandard.Nep11.ToStandard()} implementation: symbol"); if (!decimalsValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP11.ToStandard()} implementation: decimals"); + $"Incomplete NEP standard {NepStandard.Nep11.ToStandard()} implementation: decimals"); if (!totalSupplyValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP11.ToStandard()} implementation: totalSupply"); + $"Incomplete NEP standard {NepStandard.Nep11.ToStandard()} implementation: totalSupply"); if (!balanceOfValid1 && !balanceOfValid2) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP11.ToStandard()} implementation: balanceOf"); + $"Incomplete NEP standard {NepStandard.Nep11.ToStandard()} implementation: balanceOf"); if (!tokensOfValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP11.ToStandard()} implementation: tokensOf"); + $"Incomplete NEP standard {NepStandard.Nep11.ToStandard()} implementation: tokensOf"); if (!ownerOfValid1 && !ownerOfValid2) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP11.ToStandard()} implementation: ownerOf"); + $"Incomplete NEP standard {NepStandard.Nep11.ToStandard()} implementation: ownerOf"); if (!transferValid1 && !transferValid2) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP11.ToStandard()} implementation:transfer"); + $"Incomplete NEP standard {NepStandard.Nep11.ToStandard()} implementation:transfer"); if (!transferEvent) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP11.ToStandard()} implementation: {nameof(transferEvent)}"); + $"Incomplete NEP standard {NepStandard.Nep11.ToStandard()} implementation: {nameof(transferEvent)}"); } catch (Exception ex) when (ex is not CompilationException) { @@ -108,7 +109,7 @@ private static void CheckNep11Compliant(this ContractManifest manifest) catch { throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP11.ToStandard()} implementation: Unidentified issue."); + $"Incomplete NEP standard {NepStandard.Nep11.ToStandard()} implementation: Unidentified issue."); } } @@ -145,30 +146,30 @@ private static void CheckNep17Compliant(this ContractManifest manifest) s.Parameters[2].Type == ContractParameterType.Integer); if (!symbolValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP17.ToStandard()} implementation: symbol"); + $"Incomplete NEP standard {NepStandard.Nep17.ToStandard()} implementation: symbol"); if (!decimalsValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP17.ToStandard()} implementation: decimals"); + $"Incomplete NEP standard {NepStandard.Nep17.ToStandard()} implementation: decimals"); if (!totalSupplyValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP17.ToStandard()} implementation: totalSupply"); + $"Incomplete NEP standard {NepStandard.Nep17.ToStandard()} implementation: totalSupply"); if (!balanceOfValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP17.ToStandard()} implementation: balanceOf"); + $"Incomplete NEP standard {NepStandard.Nep17.ToStandard()} implementation: balanceOf"); if (!transferValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard, - $"Incomplete NEP standard {NEPStandard.NEP17.ToStandard()} implementation: transfer"); + $"Incomplete NEP standard {NepStandard.Nep17.ToStandard()} implementation: transfer"); } catch (Exception ex) when (ex is not CompilationException) { - throw new CompilationException(DiagnosticId.IncorrectNEPStandard, $"Incomplete NEP standard {NEPStandard.NEP17.ToStandard()} implementation: Unidentified issue."); + throw new CompilationException(DiagnosticId.IncorrectNEPStandard, $"Incomplete NEP standard {NepStandard.Nep17.ToStandard()} implementation: Unidentified issue."); } } internal static ContractManifest CheckStandards(this ContractManifest manifest) { - if (manifest.SupportedStandards.Contains(NEPStandard.NEP11.ToStandard())) + if (manifest.SupportedStandards.Contains(NepStandard.Nep11.ToStandard())) { manifest.CheckNep11Compliant(); } - if (manifest.SupportedStandards.Contains(NEPStandard.NEP17.ToStandard())) + if (manifest.SupportedStandards.Contains(NepStandard.Nep17.ToStandard())) { manifest.CheckNep17Compliant(); } diff --git a/src/Neo.Compiler.CSharp/MethodConvert/MethodConvert.cs b/src/Neo.Compiler.CSharp/MethodConvert/MethodConvert.cs index 5e0dd416d..ab24265d2 100644 --- a/src/Neo.Compiler.CSharp/MethodConvert/MethodConvert.cs +++ b/src/Neo.Compiler.CSharp/MethodConvert/MethodConvert.cs @@ -306,7 +306,7 @@ private void ProcessStaticFields(SemanticModel model) private void ProcessFieldInitializer(SemanticModel model, IFieldSymbol field, Action? preInitialize, Action? postInitialize) { - AttributeData? initialValue = field.GetAttributes().FirstOrDefault(p => p.AttributeClass!.Name == nameof(InitialValueAttribute)); + AttributeData? initialValue = field.GetAttributes().FirstOrDefault(p => p.AttributeClass!.Name == nameof(InitialValueAttribute) || p.AttributeClass!.IsSubclassOf(nameof(InitialValueAttribute))); if (initialValue is null) { EqualsValueClauseSyntax? initializer; @@ -337,10 +337,20 @@ private void ProcessFieldInitializer(SemanticModel model, IFieldSymbol field, Ac { preInitialize?.Invoke(); string value = (string)initialValue.ConstructorArguments[0].Value!; - ContractParameterType type = (ContractParameterType)initialValue.ConstructorArguments[1].Value!; + var attributeName = initialValue.AttributeClass!.Name; + ContractParameterType parameterType = attributeName switch + { + nameof(InitialValueAttribute) => (ContractParameterType)initialValue.ConstructorArguments[1].Value!, + nameof(Hash160Attribute) => ContractParameterType.Hash160, + nameof(PublicKeyAttribute) => ContractParameterType.PublicKey, + nameof(ByteArrayAttribute) => ContractParameterType.ByteArray, + nameof(StringAttribute) => ContractParameterType.String, + _ => throw new CompilationException(field, DiagnosticId.InvalidInitialValueType, $"Unsupported initial value type: {attributeName}"), + }; + try { - switch (type) + switch (parameterType) { case ContractParameterType.String: Push(value); @@ -355,12 +365,12 @@ private void ProcessFieldInitializer(SemanticModel model, IFieldSymbol field, Ac Push(ECPoint.Parse(value, ECCurve.Secp256r1).EncodePoint(true)); break; default: - throw new CompilationException(field, DiagnosticId.InvalidInitialValueType, $"Unsupported initial value type: {type}"); + throw new CompilationException(field, DiagnosticId.InvalidInitialValueType, $"Unsupported initial value type: {parameterType}"); } } catch (Exception ex) when (ex is not CompilationException) { - throw new CompilationException(field, DiagnosticId.InvalidInitialValue, $"Invalid initial value: {value} of type: {type}"); + throw new CompilationException(field, DiagnosticId.InvalidInitialValue, $"Invalid initial value: {value} of type: {parameterType}"); } postInitialize?.Invoke(); } diff --git a/src/Neo.SmartContract.Framework/Attributes/AuthorAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/AuthorAttribute.cs deleted file mode 100644 index bab7fb99d..000000000 --- a/src/Neo.SmartContract.Framework/Attributes/AuthorAttribute.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Neo.SmartContract.Framework.Attributes -{ - [AttributeUsage(AttributeTargets.Class)] - public class AuthorAttribute : ManifestExtraAttribute - { - public AuthorAttribute(string value) : base(AttributeType[nameof(AuthorAttribute)], value) - { - } - } -} diff --git a/src/Neo.SmartContract.Framework/Attributes/ByteArrayAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ByteArrayAttribute.cs new file mode 100644 index 000000000..0cb807c05 --- /dev/null +++ b/src/Neo.SmartContract.Framework/Attributes/ByteArrayAttribute.cs @@ -0,0 +1,22 @@ +// Copyright (C) 2015-2023 The Neo Project. +// +// The Neo.SmartContract.Framework is free software distributed under the MIT +// software license, see the accompanying file LICENSE in the main directory +// of the project or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using System; + +namespace Neo.SmartContract.Framework.Attributes +{ + [AttributeUsage(AttributeTargets.Field)] + public class ByteArrayAttribute : InitialValueAttribute + { + public ByteArrayAttribute(string value) : base(value) + { + } + } +} diff --git a/src/Neo.SmartContract.Framework/Attributes/ContractAuthorAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ContractAuthorAttribute.cs new file mode 100644 index 000000000..77b74a930 --- /dev/null +++ b/src/Neo.SmartContract.Framework/Attributes/ContractAuthorAttribute.cs @@ -0,0 +1,16 @@ +using System; + +namespace Neo.SmartContract.Framework.Attributes +{ + [AttributeUsage(AttributeTargets.Class)] + public class ContractAuthorAttribute : ManifestExtraAttribute + { + public ContractAuthorAttribute(string author) : base(AttributeType[nameof(ContractAuthorAttribute)], author) + { + } + + public ContractAuthorAttribute(string author, string email) : base(AttributeType[nameof(ContractAuthorAttribute)], author, email) + { + } + } +} diff --git a/src/Neo.SmartContract.Framework/Attributes/ContractDescriptionAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ContractDescriptionAttribute.cs new file mode 100644 index 000000000..f7f90deee --- /dev/null +++ b/src/Neo.SmartContract.Framework/Attributes/ContractDescriptionAttribute.cs @@ -0,0 +1,12 @@ +using System; + +namespace Neo.SmartContract.Framework.Attributes +{ + [AttributeUsage(AttributeTargets.Class)] + public class ContractDescriptionAttribute : ManifestExtraAttribute + { + public ContractDescriptionAttribute(string value) : base(AttributeType[nameof(ContractDescriptionAttribute)], value) + { + } + } +} diff --git a/src/Neo.SmartContract.Framework/Attributes/ContractEmailAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ContractEmailAttribute.cs new file mode 100644 index 000000000..12df70046 --- /dev/null +++ b/src/Neo.SmartContract.Framework/Attributes/ContractEmailAttribute.cs @@ -0,0 +1,12 @@ +using System; + +namespace Neo.SmartContract.Framework.Attributes +{ + [AttributeUsage(AttributeTargets.Class)] + public class ContractEmailAttribute : ManifestExtraAttribute + { + public ContractEmailAttribute(string value) : base(AttributeType[nameof(ContractEmailAttribute)], value) + { + } + } +} diff --git a/src/Neo.SmartContract.Framework/Attributes/ContractVersionAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ContractVersionAttribute.cs new file mode 100644 index 000000000..7a6d3c274 --- /dev/null +++ b/src/Neo.SmartContract.Framework/Attributes/ContractVersionAttribute.cs @@ -0,0 +1,12 @@ +using System; + +namespace Neo.SmartContract.Framework.Attributes +{ + [AttributeUsage(AttributeTargets.Class)] + public class ContractVersionAttribute : ManifestExtraAttribute + { + public ContractVersionAttribute(string value) : base(AttributeType[nameof(ContractVersionAttribute)], value) + { + } + } +} diff --git a/src/Neo.SmartContract.Framework/Attributes/DescriptionAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/DescriptionAttribute.cs deleted file mode 100644 index ab1812bee..000000000 --- a/src/Neo.SmartContract.Framework/Attributes/DescriptionAttribute.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Neo.SmartContract.Framework.Attributes -{ - [AttributeUsage(AttributeTargets.Class)] - public class DescriptionAttribute : ManifestExtraAttribute - { - public DescriptionAttribute(string value) : base(AttributeType[nameof(DescriptionAttribute)], value) - { - } - } -} diff --git a/src/Neo.SmartContract.Framework/Attributes/EmailAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/EmailAttribute.cs deleted file mode 100644 index 22c16a28d..000000000 --- a/src/Neo.SmartContract.Framework/Attributes/EmailAttribute.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Neo.SmartContract.Framework.Attributes -{ - [AttributeUsage(AttributeTargets.Class)] - public class EmailAttribute : ManifestExtraAttribute - { - public EmailAttribute(string value) : base(AttributeType[nameof(EmailAttribute)], value) - { - } - } -} diff --git a/src/Neo.SmartContract.Framework/Attributes/Hash160Attribute.cs b/src/Neo.SmartContract.Framework/Attributes/Hash160Attribute.cs new file mode 100644 index 000000000..54a4bb9dd --- /dev/null +++ b/src/Neo.SmartContract.Framework/Attributes/Hash160Attribute.cs @@ -0,0 +1,22 @@ +// Copyright (C) 2015-2023 The Neo Project. +// +// The Neo.SmartContract.Framework is free software distributed under the MIT +// software license, see the accompanying file LICENSE in the main directory +// of the project or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using System; + +namespace Neo.SmartContract.Framework.Attributes +{ + [AttributeUsage(AttributeTargets.Field)] + public class Hash160Attribute : InitialValueAttribute + { + public Hash160Attribute(string value) : base(value) + { + } + } +} diff --git a/src/Neo.SmartContract.Framework/Attributes/InitialValueAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/InitialValueAttribute.cs index f32dcbc16..fd642f86e 100644 --- a/src/Neo.SmartContract.Framework/Attributes/InitialValueAttribute.cs +++ b/src/Neo.SmartContract.Framework/Attributes/InitialValueAttribute.cs @@ -9,6 +9,7 @@ // modifications are permitted. using System; +using System.Collections.Generic; namespace Neo.SmartContract.Framework.Attributes { @@ -29,11 +30,11 @@ namespace Neo.SmartContract.Framework.Attributes /// Examples: /// /// // Example of initializing a UInt160 field with a Hash160 address - /// [InitialValue("NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq", ContractParameterType.Hash160)] + /// [InitialValue("NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq")] /// private static readonly UInt160 validUInt160 = default; /// /// // Example of initializing a byte array field with a hex string representing a UInt256 value - /// [InitialValue("edcf8679104ec2911a4fe29ad7db232a493e5b990fb1da7af0c7b989948c8925", ContractParameterType.ByteArray)] + /// [InitialValue("edcf8679104ec2911a4fe29ad7db232a493e5b990fb1da7af0c7b989948c8925")] /// private static readonly byte[] validUInt256 = default; /// /// @@ -59,5 +60,9 @@ public class InitialValueAttribute : Attribute public InitialValueAttribute(string value, ContractParameterType type) { } + + public InitialValueAttribute(string value) + { + } } } diff --git a/src/Neo.SmartContract.Framework/Attributes/ManifestExtraAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ManifestExtraAttribute.cs index cf4ac1a66..c11d8f94c 100644 --- a/src/Neo.SmartContract.Framework/Attributes/ManifestExtraAttribute.cs +++ b/src/Neo.SmartContract.Framework/Attributes/ManifestExtraAttribute.cs @@ -20,12 +20,16 @@ public ManifestExtraAttribute(string key, string value) { } - internal static readonly Dictionary AttributeType = new Dictionary + public ManifestExtraAttribute(string key, string value, string value2) { - { nameof(AuthorAttribute), "Author" }, - { nameof(EmailAttribute), "E-mail" }, - { nameof(DescriptionAttribute), "Description" }, - { nameof(VersionAttribute), "Version" }, + } + + internal static readonly Dictionary AttributeType = new() + { + { nameof(ContractAuthorAttribute), "Author" }, + { nameof(ContractEmailAttribute), "E-mail" }, + { nameof(ContractDescriptionAttribute), "Description" }, + { nameof(ContractVersionAttribute), "Version" }, }; } } diff --git a/src/Neo.SmartContract.Framework/Attributes/NEPStandard.cs b/src/Neo.SmartContract.Framework/Attributes/NEPStandard.cs deleted file mode 100644 index 403502763..000000000 --- a/src/Neo.SmartContract.Framework/Attributes/NEPStandard.cs +++ /dev/null @@ -1,24 +0,0 @@ -namespace Neo.SmartContract.Framework.Attributes -{ - public enum NEPStandard - { - NEP11, - NEP17, - } - - public static class NEPStandardExtensions - { - public static string ToStandard(this NEPStandard standard) - { - switch (standard) - { - case NEPStandard.NEP11: - return "NEP-11"; - case NEPStandard.NEP17: - return "NEP-17"; - default: - return standard.ToString(); - } - } - } -} diff --git a/src/Neo.SmartContract.Framework/Attributes/PublicKeyAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/PublicKeyAttribute.cs new file mode 100644 index 000000000..570e6370d --- /dev/null +++ b/src/Neo.SmartContract.Framework/Attributes/PublicKeyAttribute.cs @@ -0,0 +1,22 @@ +// Copyright (C) 2015-2023 The Neo Project. +// +// The Neo.SmartContract.Framework is free software distributed under the MIT +// software license, see the accompanying file LICENSE in the main directory +// of the project or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using System; + +namespace Neo.SmartContract.Framework.Attributes +{ + [AttributeUsage(AttributeTargets.Field)] + public class PublicKeyAttribute : InitialValueAttribute + { + public PublicKeyAttribute(string value) : base(value) + { + } + } +} diff --git a/src/Neo.SmartContract.Framework/Attributes/StringAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/StringAttribute.cs new file mode 100644 index 000000000..b4a35f8a2 --- /dev/null +++ b/src/Neo.SmartContract.Framework/Attributes/StringAttribute.cs @@ -0,0 +1,22 @@ +// Copyright (C) 2015-2023 The Neo Project. +// +// The Neo.SmartContract.Framework is free software distributed under the MIT +// software license, see the accompanying file LICENSE in the main directory +// of the project or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +using System; + +namespace Neo.SmartContract.Framework.Attributes +{ + [AttributeUsage(AttributeTargets.Field)] + public class StringAttribute : InitialValueAttribute + { + public StringAttribute(string value) : base(value) + { + } + } +} diff --git a/src/Neo.SmartContract.Framework/Attributes/SupportedStandardsAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/SupportedStandardsAttribute.cs index a250c0971..9e14290b3 100644 --- a/src/Neo.SmartContract.Framework/Attributes/SupportedStandardsAttribute.cs +++ b/src/Neo.SmartContract.Framework/Attributes/SupportedStandardsAttribute.cs @@ -19,7 +19,7 @@ public SupportedStandardsAttribute(params string[] supportedStandards) { } - public SupportedStandardsAttribute(params NEPStandard[] supportedStandards) + public SupportedStandardsAttribute(params NepStandard[] supportedStandards) { } } diff --git a/src/Neo.SmartContract.Framework/Attributes/VersionAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/VersionAttribute.cs deleted file mode 100644 index eb62fa3bf..000000000 --- a/src/Neo.SmartContract.Framework/Attributes/VersionAttribute.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Neo.SmartContract.Framework.Attributes -{ - [AttributeUsage(AttributeTargets.Class)] - public class VersionAttribute : ManifestExtraAttribute - { - public VersionAttribute(string value) : base(AttributeType[nameof(VersionAttribute)], value) - { - } - } -} diff --git a/src/Neo.SmartContract.Framework/ByteString.cs b/src/Neo.SmartContract.Framework/ByteString.cs index 7fd23ce9b..bba3ce4e3 100644 --- a/src/Neo.SmartContract.Framework/ByteString.cs +++ b/src/Neo.SmartContract.Framework/ByteString.cs @@ -1,10 +1,10 @@ // Copyright (C) 2015-2023 The Neo Project. -// -// The Neo.SmartContract.Framework is free software distributed under the MIT -// software license, see the accompanying file LICENSE in the main directory -// of the project or http://www.opensource.org/licenses/mit-license.php +// +// The Neo.SmartContract.Framework is free software distributed under the MIT +// software license, see the accompanying file LICENSE in the main directory +// of the project or http://www.opensource.org/licenses/mit-license.php // for more details. -// +// // Redistribution and use in source and binary forms with or without // modifications are permitted. diff --git a/src/Neo.SmartContract.Framework/Method.cs b/src/Neo.SmartContract.Framework/Method.cs new file mode 100644 index 000000000..9099e57c1 --- /dev/null +++ b/src/Neo.SmartContract.Framework/Method.cs @@ -0,0 +1,9 @@ +namespace Neo.SmartContract.Framework; + +public class Method +{ + public const string WildCard = "*"; + public const string OnNEP17Payment = "onNEP17Payment"; + public const string OnNEP11Payment = "onNEP11Payment"; + public const string Deploy = "_deploy"; +} diff --git a/src/Neo.SmartContract.Framework/NEPStandard.cs b/src/Neo.SmartContract.Framework/NEPStandard.cs new file mode 100644 index 000000000..de825cd61 --- /dev/null +++ b/src/Neo.SmartContract.Framework/NEPStandard.cs @@ -0,0 +1,24 @@ +namespace Neo.SmartContract.Framework +{ + public enum NepStandard + { + Nep11, + Nep17, + } + + public static class NepStandardExtensions + { + public static string ToStandard(this NepStandard standard) + { + switch (standard) + { + case NepStandard.Nep11: + return "NEP-11"; + case NepStandard.Nep17: + return "NEP-17"; + default: + return standard.ToString(); + } + } + } +} diff --git a/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj b/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj index b05baa37e..df483d556 100644 --- a/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj +++ b/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj @@ -14,6 +14,10 @@ true src + + true + src + diff --git a/src/Neo.SmartContract.Framework/Nep11Token.cs b/src/Neo.SmartContract.Framework/Nep11Token.cs index 1a86381cd..d69ef0fd4 100644 --- a/src/Neo.SmartContract.Framework/Nep11Token.cs +++ b/src/Neo.SmartContract.Framework/Nep11Token.cs @@ -1,10 +1,10 @@ // Copyright (C) 2015-2023 The Neo Project. -// -// The Neo.SmartContract.Framework is free software distributed under the MIT -// software license, see the accompanying file LICENSE in the main directory -// of the project or http://www.opensource.org/licenses/mit-license.php +// +// The Neo.SmartContract.Framework is free software distributed under the MIT +// software license, see the accompanying file LICENSE in the main directory +// of the project or http://www.opensource.org/licenses/mit-license.php // for more details. -// +// // Redistribution and use in source and binary forms with or without // modifications are permitted. @@ -17,8 +17,8 @@ namespace Neo.SmartContract.Framework { - [SupportedStandards("NEP-11")] - [ContractPermission("*", "onNEP11Payment")] + [SupportedStandards(NepStandard.Nep11)] + [ContractPermission(Permission.WildCard, Method.OnNEP11Payment)] public abstract class Nep11Token : TokenContract where TokenState : Nep11TokenState { diff --git a/src/Neo.SmartContract.Framework/Nep17Token.cs b/src/Neo.SmartContract.Framework/Nep17Token.cs index 079aeb8e5..9cb8256cc 100644 --- a/src/Neo.SmartContract.Framework/Nep17Token.cs +++ b/src/Neo.SmartContract.Framework/Nep17Token.cs @@ -1,10 +1,10 @@ // Copyright (C) 2015-2023 The Neo Project. -// -// The Neo.SmartContract.Framework is free software distributed under the MIT -// software license, see the accompanying file LICENSE in the main directory -// of the project or http://www.opensource.org/licenses/mit-license.php +// +// The Neo.SmartContract.Framework is free software distributed under the MIT +// software license, see the accompanying file LICENSE in the main directory +// of the project or http://www.opensource.org/licenses/mit-license.php // for more details. -// +// // Redistribution and use in source and binary forms with or without // modifications are permitted. @@ -17,8 +17,8 @@ namespace Neo.SmartContract.Framework { - [SupportedStandards("NEP-17")] - [ContractPermission("*", "onNEP17Payment")] + [SupportedStandards(NepStandard.Nep17)] + [ContractPermission(Permission.WildCard, Method.OnNEP17Payment)] public abstract class Nep17Token : TokenContract { public delegate void OnTransferDelegate(UInt160 from, UInt160 to, BigInteger amount); @@ -68,7 +68,7 @@ protected static void PostTransfer(UInt160 from, UInt160 to, BigInteger amount, { OnTransfer(from, to, amount); if (to is not null && ContractManagement.GetContract(to) is not null) - Contract.Call(to, "onNEP17Payment", CallFlags.All, from, amount, data); + Contract.Call(to, Method.OnNEP17Payment, CallFlags.All, from, amount, data); } } } diff --git a/src/Neo.SmartContract.Framework/Permission.cs b/src/Neo.SmartContract.Framework/Permission.cs new file mode 100644 index 000000000..a44c069f0 --- /dev/null +++ b/src/Neo.SmartContract.Framework/Permission.cs @@ -0,0 +1,6 @@ +namespace Neo.SmartContract.Framework; + +public class Permission +{ + public const string WildCard = "*"; +} diff --git a/src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs b/src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs index a76f89e64..758b6ae08 100644 --- a/src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs +++ b/src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs @@ -12,15 +12,15 @@ namespace ProjectName { [DisplayName(nameof(Contract1))] [ManifestExtra("Author", "")] - [ManifestExtra("Description", "")] + [ContractDescription( "")] [ManifestExtra("Email", "")] [ManifestExtra("Version", "")] [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template")] - [ContractPermission("*", "*")] + [ContractPermission(Permission.WildCard, Method.WildCard)] public class Contract1 : SmartContract { // TODO: Replace it with your own address. - [InitialValue("", ContractParameterType.Hash160)] + [Hash160("")] static readonly UInt160 Owner = default; private static bool IsOwner() => Runtime.CheckWitness(Owner); diff --git a/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs b/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs index cf4a3b79c..1a061800f 100644 --- a/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs +++ b/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs @@ -12,11 +12,11 @@ namespace ProjectName { [DisplayName(nameof(Nep17Contract))] [ManifestExtra("Author", "")] - [ManifestExtra("Description", "")] + [ContractDescription( "")] [ManifestExtra("Email", "")] [ManifestExtra("Version", "")] [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs")] - [ContractPermission("*", "*")] + [ContractPermission(Permission.WildCard, Method.WildCard)] [SupportedStandards("NEP-17")] public class Nep17Contract : Nep17Token { diff --git a/src/Neo.SmartContract.Template/templates/neocontractoracle/Contract1.cs b/src/Neo.SmartContract.Template/templates/neocontractoracle/Contract1.cs index 4d74615bc..064931b0b 100644 --- a/src/Neo.SmartContract.Template/templates/neocontractoracle/Contract1.cs +++ b/src/Neo.SmartContract.Template/templates/neocontractoracle/Contract1.cs @@ -12,11 +12,11 @@ namespace ProjectName { [DisplayName(nameof(Contract1))] [ManifestExtra("Author", "")] - [ManifestExtra("Description", "")] + [ContractDescription( "")] [ManifestExtra("Email", "")] [ManifestExtra("Version", "")] [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template")] - [ContractPermission("*", "*")] + [ContractPermission(Permission.WildCard, Method.WildCard)] public class Contract1 : SmartContract { public delegate void OnRequestSuccessfulDelegate(string requestedUrl, object jsonValue); @@ -25,7 +25,7 @@ public class Contract1 : SmartContract public static event OnRequestSuccessfulDelegate OnRequestSuccessful; // TODO: Replace it with your own address. - [InitialValue("", ContractParameterType.Hash160)] + [Hash160("")] static readonly UInt160 Owner = default; private static bool IsOwner() => Runtime.CheckWitness(Owner); diff --git a/src/Neo.SmartContract.Template/templates/neocontractowner/Contract1.cs b/src/Neo.SmartContract.Template/templates/neocontractowner/Contract1.cs index b6089817e..4530aeaed 100644 --- a/src/Neo.SmartContract.Template/templates/neocontractowner/Contract1.cs +++ b/src/Neo.SmartContract.Template/templates/neocontractowner/Contract1.cs @@ -12,11 +12,11 @@ namespace ProjectName { [DisplayName(nameof(Contract1))] [ManifestExtra("Author", "")] - [ManifestExtra("Description", "")] + [ContractDescription( "")] [ManifestExtra("Email", "")] [ManifestExtra("Version", "")] [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template")] - [ContractPermission("*", "*")] + [ContractPermission(Permission.WildCard, Method.WildCard)] public class Contract1 : SmartContract { private const byte Prefix_Owner = 0xff; @@ -27,7 +27,7 @@ public class Contract1 : SmartContract public static event OnSetOwnerDelegate OnSetOwner; // TODO: Replace it with your own address. - [InitialValue("", ContractParameterType.Hash160)] + [Hash160("")] private static readonly UInt160 InitialOwner = default; private static bool IsOwner() => Runtime.CheckWitness(GetOwner()); diff --git a/tests/Neo.Compiler.CSharp.TestContracts/Contract_ABIAttributes.cs b/tests/Neo.Compiler.CSharp.TestContracts/Contract_ABIAttributes.cs index 9d57bc4ef..b5d6f3a02 100644 --- a/tests/Neo.Compiler.CSharp.TestContracts/Contract_ABIAttributes.cs +++ b/tests/Neo.Compiler.CSharp.TestContracts/Contract_ABIAttributes.cs @@ -1,8 +1,9 @@ +using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Attributes; namespace Neo.Compiler.CSharp.UnitTests.TestClasses; -[ContractPermission("*", "c")] +[ContractPermission(Permission.WildCard, "c")] [ContractPermission("0x01ff00ff00ff00ff00ff00ff00ff00ff00ff00a4", "a", "b")] [ContractTrust("0x0a0b00ff00ff00ff00ff00ff00ff00ff00ff00a4")] public class Contract_ABIAttributes : SmartContract.Framework.SmartContract diff --git a/tests/Neo.Compiler.CSharp.TestContracts/Contract_Foreach.cs b/tests/Neo.Compiler.CSharp.TestContracts/Contract_Foreach.cs index c87bb731c..28b6f2307 100644 --- a/tests/Neo.Compiler.CSharp.TestContracts/Contract_Foreach.cs +++ b/tests/Neo.Compiler.CSharp.TestContracts/Contract_Foreach.cs @@ -8,7 +8,7 @@ namespace Neo.Compiler.CSharp.UnitTests.TestClasses { public class Contract_Foreach : SmartContract.Framework.SmartContract { - [InitialValue("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9", ContractParameterType.ByteArray)] + [ByteArray("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9")] private static readonly byte[] rawECpoint = default; struct Pending diff --git a/tests/Neo.Compiler.CSharp.TestContracts/Contract_StaticVar.cs b/tests/Neo.Compiler.CSharp.TestContracts/Contract_StaticVar.cs index 7db3f369a..cc9c3966d 100644 --- a/tests/Neo.Compiler.CSharp.TestContracts/Contract_StaticVar.cs +++ b/tests/Neo.Compiler.CSharp.TestContracts/Contract_StaticVar.cs @@ -1,6 +1,5 @@ using System.Numerics; using Neo.Cryptography.ECC; -using Neo.SmartContract; using Neo.SmartContract.Framework.Attributes; namespace Neo.Compiler.CSharp.UnitTests.TestClasses @@ -24,20 +23,20 @@ public class Contract_StaticVar : SmartContract.Framework.SmartContract /// A static field of type ECPoint initialized with the InitialValue attribute. This is used to demonstrate initializing /// complex types like ECPoint at compile time to avoid runtime overhead. /// - [InitialValue("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9", ContractParameterType.PublicKey)] + [PublicKey("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9")] private static readonly ECPoint eCPoint = default; /// /// A static field of type UInt160 initialized with the InitialValue attribute. This allows for compile-time /// initialization of blockchain-specific types like addresses, represented here as Hash160. /// - [InitialValue("NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq", ContractParameterType.Hash160)] + [Hash160("NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq")] private static readonly UInt160 uInt160 = default; /// /// A static string field initialized with the InitialValue attribute. This demonstrates initializing contract fields that cannot be directly assigned with their value at compile time. /// - [InitialValue("hello world", ContractParameterType.String)] + [String("hello world")] public static readonly string a4 = default; /// diff --git a/tests/Neo.Compiler.CSharp.TestContracts/Contract_TryCatch.cs b/tests/Neo.Compiler.CSharp.TestContracts/Contract_TryCatch.cs index 0a9be0412..4b3063c1d 100644 --- a/tests/Neo.Compiler.CSharp.TestContracts/Contract_TryCatch.cs +++ b/tests/Neo.Compiler.CSharp.TestContracts/Contract_TryCatch.cs @@ -7,13 +7,13 @@ namespace Neo.Compiler.CSharp.UnitTests.TestClasses { public class Contract_TryCatch : SmartContract.Framework.SmartContract { - [InitialValue("0a0b0c0d0E0F", ContractParameterType.ByteArray)] + [ByteArray("0a0b0c0d0E0F")] private static readonly ByteString invalidECpoint = default; - [InitialValue("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9", ContractParameterType.ByteArray)] + [ByteArray("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9")] private static readonly ByteString byteString2Ecpoint = default; - [InitialValue("NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq", ContractParameterType.Hash160)] + [Hash160("NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq")] private static readonly ByteString validUInt160 = default; - [InitialValue("edcf8679104ec2911a4fe29ad7db232a493e5b990fb1da7af0c7b989948c8925", ContractParameterType.ByteArray)] + [ByteArray("edcf8679104ec2911a4fe29ad7db232a493e5b990fb1da7af0c7b989948c8925")] private static readonly byte[] validUInt256 = default; public static object try01() { diff --git a/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_ECPoint.cs b/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_ECPoint.cs index 0e7bacdfe..0315b4205 100644 --- a/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_ECPoint.cs +++ b/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_ECPoint.cs @@ -7,7 +7,7 @@ namespace Neo.Compiler.CSharp.UnitTests.TestClasses { public class Contract_Types_ECPoint : SmartContract.Framework.SmartContract { - [InitialValue("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9", ContractParameterType.PublicKey)] + [PublicKey("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9")] private static readonly ECPoint publicKey2Ecpoint = default; public static bool isValid(ECPoint point) { return point.IsValid; } diff --git a/tests/Neo.Compiler.CSharp.TestContracts/Contract_UIntTypes.cs b/tests/Neo.Compiler.CSharp.TestContracts/Contract_UIntTypes.cs index d828445cf..ba04d59ec 100644 --- a/tests/Neo.Compiler.CSharp.TestContracts/Contract_UIntTypes.cs +++ b/tests/Neo.Compiler.CSharp.TestContracts/Contract_UIntTypes.cs @@ -6,7 +6,7 @@ namespace Neo.Compiler.CSharp.UnitTests.TestClasses { public class Contract_UIntTypes : SmartContract.Framework.SmartContract { - [InitialValue("NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB", ContractParameterType.Hash160)] + [Hash160("NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB")] static readonly UInt160 Owner = default; public static bool checkOwner(UInt160 owner) { return owner == Owner; } diff --git a/tests/Neo.SmartContract.Framework.TestContracts/Contract_Helper.cs b/tests/Neo.SmartContract.Framework.TestContracts/Contract_Helper.cs index 77bc6cc70..ef782cab4 100644 --- a/tests/Neo.SmartContract.Framework.TestContracts/Contract_Helper.cs +++ b/tests/Neo.SmartContract.Framework.TestContracts/Contract_Helper.cs @@ -5,10 +5,10 @@ namespace Neo.SmartContract.Framework.UnitTests.TestClasses { public class Contract_Helper : SmartContract { - [InitialValue("0a0b0c0d0E0F", ContractParameterType.ByteArray)] + [ByteArray("0a0b0c0d0E0F")] private static readonly byte[] data = default; - [InitialValue("NL1JGjDe22U44R57ZXVSeTYFBavEkVmkgF", ContractParameterType.Hash160)] + [Hash160("NL1JGjDe22U44R57ZXVSeTYFBavEkVmkgF")] private static readonly byte[] hashResult = default; public static byte[] TestHexToBytes() diff --git a/tests/Neo.SmartContract.Framework.TestContracts/Contract_ManifestAttribute.cs b/tests/Neo.SmartContract.Framework.TestContracts/Contract_ManifestAttribute.cs index 9e7aff6ea..7ec190dc3 100644 --- a/tests/Neo.SmartContract.Framework.TestContracts/Contract_ManifestAttribute.cs +++ b/tests/Neo.SmartContract.Framework.TestContracts/Contract_ManifestAttribute.cs @@ -2,10 +2,10 @@ namespace Neo.SmartContract.Framework.UnitTests.TestClasses { - [Author("core-dev")] - [Email("core@neo.org")] - [Version("v3.6.3")] - [Description("This is a test contract.")] + [ContractAuthor("core-dev")] + [ContractEmail("core@neo.org")] + [ContractVersion("v3.6.3")] + [ContractDescription("This is a test contract.")] [ManifestExtra("ExtraKey", "ExtraValue")] public class Contract_ManifestAttribute : SmartContract { diff --git a/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs b/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs index e0e2ae201..21f851859 100644 --- a/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs +++ b/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs @@ -2,7 +2,7 @@ namespace Neo.SmartContract.Framework.UnitTests.TestClasses { - [SupportedStandards(NEPStandard.NEP11)] + [SupportedStandards(NepStandard.Nep11)] public class Contract_SupportedStandard11Enum : Nep11Token { public static bool TestStandard() diff --git a/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard17Enum.cs b/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard17Enum.cs index f1978bba6..af5ea6e73 100644 --- a/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard17Enum.cs +++ b/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard17Enum.cs @@ -5,12 +5,12 @@ namespace Neo.SmartContract.Framework.UnitTests.TestClasses { [DisplayName(nameof(Contract_SupportedStandard17Enum))] [ManifestExtra("Author", "")] - [ManifestExtra("Description", "")] + [ContractDescription("")] [ManifestExtra("Email", "")] [ManifestExtra("Version", "")] [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template")] - [ContractPermission("*", "*")] - [SupportedStandards(NEPStandard.NEP17)] + [ContractPermission(Permission.WildCard, Method.WildCard)] + [SupportedStandards(NepStandard.Nep17)] public class Contract_SupportedStandard17Enum : Nep17Token { public override string Symbol { [Safe] get; } From 2c0da4ef1c0ae092d391617008359fe80c52f6d6 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Wed, 21 Feb 2024 17:52:19 +0800 Subject: [PATCH 02/19] optimize the code --- .../Exception.cs | 16 ++++++++-------- examples/Example.SmartContract.NFT/Loot.cs | 1 - neo | 2 +- .../Attributes/ContractAuthorAttribute.cs | 9 +++++++++ .../ContractDescriptionAttribute.cs | 4 ++++ .../Attributes/ContractPermissionAttribute.cs | 19 ++++++++++++++----- .../Attributes/ContractSourceCodeAttribute.cs | 14 +++++++++----- .../Attributes/ContractVersionAttribute.cs | 5 +++++ src/Neo.SmartContract.Framework/Method.cs | 14 ++++++++++++-- src/Neo.SmartContract.Framework/Permission.cs | 5 ++++- 10 files changed, 66 insertions(+), 23 deletions(-) diff --git a/examples/Example.SmartContract.Exception/Exception.cs b/examples/Example.SmartContract.Exception/Exception.cs index 04b441d4a..ade8df724 100644 --- a/examples/Example.SmartContract.Exception/Exception.cs +++ b/examples/Example.SmartContract.Exception/Exception.cs @@ -76,7 +76,7 @@ public static object try03() try { v = 2; - throwcall(); + Throwcall(); } catch { @@ -97,16 +97,16 @@ public static object tryNest() try { v = 2; - throwcall(); + Throwcall(); } catch { v = 3; - throwcall(); + Throwcall(); } finally { - throwcall(); + Throwcall(); v++; } } @@ -137,7 +137,7 @@ public static object tryFinallyAndRethrow() try { v = 2; - throwcall(); + Throwcall(); } finally { @@ -152,7 +152,7 @@ public static object tryCatch() try { v = 2; - throwcall(); + Throwcall(); } catch { @@ -400,12 +400,12 @@ public static (int, object) tryNULL2Bytestring_1() return (v, data); } - public static object throwcall() + private static object Throwcall() { throw new System.Exception(); } - public static object tryUncatchableException() + public static object TryUncatchableException() { int v = 0; try diff --git a/examples/Example.SmartContract.NFT/Loot.cs b/examples/Example.SmartContract.NFT/Loot.cs index 5090bd9b2..b68b06ea2 100644 --- a/examples/Example.SmartContract.NFT/Loot.cs +++ b/examples/Example.SmartContract.NFT/Loot.cs @@ -105,7 +105,6 @@ private string Pluck(BigInteger credential, BigInteger keyPrefix, string[] sourc return output; } - [Safe] public string tokenURI(BigInteger tokenId) { diff --git a/neo b/neo index 3f002a657..ffa90648c 160000 --- a/neo +++ b/neo @@ -1 +1 @@ -Subproject commit 3f002a657bd7272f551eb6d4eafa5552cf0ac88a +Subproject commit ffa90648c8f6269f2b157564fb2203f5278114c8 diff --git a/src/Neo.SmartContract.Framework/Attributes/ContractAuthorAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ContractAuthorAttribute.cs index 77b74a930..0862988bb 100644 --- a/src/Neo.SmartContract.Framework/Attributes/ContractAuthorAttribute.cs +++ b/src/Neo.SmartContract.Framework/Attributes/ContractAuthorAttribute.cs @@ -5,10 +5,19 @@ namespace Neo.SmartContract.Framework.Attributes [AttributeUsage(AttributeTargets.Class)] public class ContractAuthorAttribute : ManifestExtraAttribute { + /// + /// Specifies the author of the contract in the manifest. + /// + /// The name of the contract author public ContractAuthorAttribute(string author) : base(AttributeType[nameof(ContractAuthorAttribute)], author) { } + /// + /// Specifies the author and email of the contract in the manifest. + /// + /// The name of the contract author + /// The email of the contract author public ContractAuthorAttribute(string author, string email) : base(AttributeType[nameof(ContractAuthorAttribute)], author, email) { } diff --git a/src/Neo.SmartContract.Framework/Attributes/ContractDescriptionAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ContractDescriptionAttribute.cs index f7f90deee..acf525361 100644 --- a/src/Neo.SmartContract.Framework/Attributes/ContractDescriptionAttribute.cs +++ b/src/Neo.SmartContract.Framework/Attributes/ContractDescriptionAttribute.cs @@ -5,6 +5,10 @@ namespace Neo.SmartContract.Framework.Attributes [AttributeUsage(AttributeTargets.Class)] public class ContractDescriptionAttribute : ManifestExtraAttribute { + /// + /// Specifies the description of the contract in the manifest. + /// + /// Description of the contract. public ContractDescriptionAttribute(string value) : base(AttributeType[nameof(ContractDescriptionAttribute)], value) { } diff --git a/src/Neo.SmartContract.Framework/Attributes/ContractPermissionAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ContractPermissionAttribute.cs index 5d8da69f6..b7e282098 100644 --- a/src/Neo.SmartContract.Framework/Attributes/ContractPermissionAttribute.cs +++ b/src/Neo.SmartContract.Framework/Attributes/ContractPermissionAttribute.cs @@ -1,10 +1,10 @@ // Copyright (C) 2015-2023 The Neo Project. -// -// The Neo.SmartContract.Framework is free software distributed under the MIT -// software license, see the accompanying file LICENSE in the main directory -// of the project or http://www.opensource.org/licenses/mit-license.php +// +// The Neo.SmartContract.Framework is free software distributed under the MIT +// software license, see the accompanying file LICENSE in the main directory +// of the project or http://www.opensource.org/licenses/mit-license.php // for more details. -// +// // Redistribution and use in source and binary forms with or without // modifications are permitted. @@ -15,6 +15,15 @@ namespace Neo.SmartContract.Framework.Attributes [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class ContractPermissionAttribute : Attribute { + /// + /// Specify which contract and methods are allowed to call from this contract. + /// + /// Address of contract allowed to call + /// Name of method allowed to call. + /// + /// If the contract is specified as Permission.WildCard, then all contracts are allowed to call the specified methods. + /// If the method is specified as Method.WildCard, then all methods are allowed to call from the specified contract. + /// public ContractPermissionAttribute(string contract, params string[] methods) { } diff --git a/src/Neo.SmartContract.Framework/Attributes/ContractSourceCodeAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ContractSourceCodeAttribute.cs index e568b081d..0f0e525c6 100644 --- a/src/Neo.SmartContract.Framework/Attributes/ContractSourceCodeAttribute.cs +++ b/src/Neo.SmartContract.Framework/Attributes/ContractSourceCodeAttribute.cs @@ -1,10 +1,10 @@ // Copyright (C) 2015-2023 The Neo Project. -// -// The Neo.SmartContract.Framework is free software distributed under the MIT -// software license, see the accompanying file LICENSE in the main directory -// of the project or http://www.opensource.org/licenses/mit-license.php +// +// The Neo.SmartContract.Framework is free software distributed under the MIT +// software license, see the accompanying file LICENSE in the main directory +// of the project or http://www.opensource.org/licenses/mit-license.php // for more details. -// +// // Redistribution and use in source and binary forms with or without // modifications are permitted. @@ -15,6 +15,10 @@ namespace Neo.SmartContract.Framework.Attributes [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class ContractSourceCodeAttribute : Attribute { + /// + /// Specify the URL of the contract source code. + /// + /// public ContractSourceCodeAttribute(string url) { } diff --git a/src/Neo.SmartContract.Framework/Attributes/ContractVersionAttribute.cs b/src/Neo.SmartContract.Framework/Attributes/ContractVersionAttribute.cs index 7a6d3c274..126bcf701 100644 --- a/src/Neo.SmartContract.Framework/Attributes/ContractVersionAttribute.cs +++ b/src/Neo.SmartContract.Framework/Attributes/ContractVersionAttribute.cs @@ -5,6 +5,11 @@ namespace Neo.SmartContract.Framework.Attributes [AttributeUsage(AttributeTargets.Class)] public class ContractVersionAttribute : ManifestExtraAttribute { + /// + /// Specifies the version of the contract in the manifest. + /// + /// Version of the contract + /// The version is different from the Update Counter. public ContractVersionAttribute(string value) : base(AttributeType[nameof(ContractVersionAttribute)], value) { } diff --git a/src/Neo.SmartContract.Framework/Method.cs b/src/Neo.SmartContract.Framework/Method.cs index 9099e57c1..485c1f4f6 100644 --- a/src/Neo.SmartContract.Framework/Method.cs +++ b/src/Neo.SmartContract.Framework/Method.cs @@ -1,9 +1,19 @@ namespace Neo.SmartContract.Framework; -public class Method +public static class Method { + /// + /// Indicates that the contract is allowed to call any method of allowed contract. + /// public const string WildCard = "*"; + + /// + /// The name of the method that is called when a contract receives NEP-17 tokens. + /// public const string OnNEP17Payment = "onNEP17Payment"; + + /// + /// The name of the method that is called when a contract receives NEP-11 tokens. + /// public const string OnNEP11Payment = "onNEP11Payment"; - public const string Deploy = "_deploy"; } diff --git a/src/Neo.SmartContract.Framework/Permission.cs b/src/Neo.SmartContract.Framework/Permission.cs index a44c069f0..f7604175d 100644 --- a/src/Neo.SmartContract.Framework/Permission.cs +++ b/src/Neo.SmartContract.Framework/Permission.cs @@ -1,6 +1,9 @@ namespace Neo.SmartContract.Framework; -public class Permission +public static class Permission { + /// + /// Indicates that the contract is allowed to call of any contract. + /// public const string WildCard = "*"; } From 24555e24ef6ba380dc098d51d11dbc5fd8d0243d Mon Sep 17 00:00:00 2001 From: Shargon Date: Sun, 25 Feb 2024 11:24:25 -0800 Subject: [PATCH 03/19] Delete src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs this was removed from templates --- .../templates/neocontract/Contract1.cs | 64 ------------------- 1 file changed, 64 deletions(-) delete mode 100644 src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs diff --git a/src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs b/src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs deleted file mode 100644 index 758b6ae08..000000000 --- a/src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Neo; -using Neo.SmartContract; -using Neo.SmartContract.Framework; -using Neo.SmartContract.Framework.Attributes; -using Neo.SmartContract.Framework.Native; -using Neo.SmartContract.Framework.Services; - -using System; -using System.ComponentModel; - -namespace ProjectName -{ - [DisplayName(nameof(Contract1))] - [ManifestExtra("Author", "")] - [ContractDescription( "")] - [ManifestExtra("Email", "")] - [ManifestExtra("Version", "")] - [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template")] - [ContractPermission(Permission.WildCard, Method.WildCard)] - public class Contract1 : SmartContract - { - // TODO: Replace it with your own address. - [Hash160("")] - static readonly UInt160 Owner = default; - - private static bool IsOwner() => Runtime.CheckWitness(Owner); - - // When this contract address is included in the transaction signature, - // this method will be triggered as a VerificationTrigger to verify that the signature is correct. - // For example, this method needs to be called when withdrawing token from the contract. - [Safe] - public static bool Verify() => IsOwner(); - - // TODO: Replace it with your methods. - public static string MyMethod() - { - return Storage.Get(Storage.CurrentContext, "Hello"); - } - - public static void _deploy(object data, bool update) - { - if (update) - { - // This will be executed during update - return; - } - - // This will be executed during deploy - Storage.Put(Storage.CurrentContext, "Hello", "World"); - } - - public static void Update(ByteString nefFile, string manifest) - { - if (!IsOwner()) throw new Exception("No authorization."); - ContractManagement.Update(nefFile, manifest, null); - } - - public static void Destroy() - { - if (!IsOwner()) throw new Exception("No authorization."); - ContractManagement.Destroy(); - } - } -} From 09f278574a3d39b4181675d12f24b4a371b5de3d Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 26 Feb 2024 10:18:33 +0800 Subject: [PATCH 04/19] Merge branch 'master' into add-examples # Conflicts: # src/Neo.Compiler.CSharp/CompilationContext.cs # src/Neo.SmartContract.Template/templates/neocontract/Contract1.cs --- examples/Directory.Build.props | 6 +++-- .../Example.SmartContract.ContractCall.csproj | 2 -- .../Example.SmartContract.Event.csproj | 2 -- .../Example.SmartContract.Exception.csproj | 2 -- .../Example.SmartContract.HelloWorld.csproj | 2 -- .../Example.SmartContract.Inscription.csproj | 2 -- .../Example.SmartContract.Modifier.csproj | 2 -- .../Example.SmartContract.NEP17.csproj | 2 -- .../Example.SmartContract.NFT.csproj | 2 -- .../Example.SmartContract.Oracle.csproj | 2 -- .../Example.SmartContract.Storage.csproj | 2 -- .../TransferContract.cs | 23 +++++++++++++++---- .../Example.SmartContract.ZKP.csproj | 2 -- 13 files changed, 22 insertions(+), 29 deletions(-) diff --git a/examples/Directory.Build.props b/examples/Directory.Build.props index 80064c08a..d0f277a62 100644 --- a/examples/Directory.Build.props +++ b/examples/Directory.Build.props @@ -11,11 +11,13 @@ MIT README.md git - https://github.com/R3E-Network/SharpNeo.git + https://github.com/neo-project/neo-devpack-dotnet.git true snupkg - The SharpNeo Examples Project + The Neo N3 Contract Examples Project true + enable + false diff --git a/examples/Example.SmartContract.ContractCall/Example.SmartContract.ContractCall.csproj b/examples/Example.SmartContract.ContractCall/Example.SmartContract.ContractCall.csproj index eccc3b6a5..59cc4c33b 100644 --- a/examples/Example.SmartContract.ContractCall/Example.SmartContract.ContractCall.csproj +++ b/examples/Example.SmartContract.ContractCall/Example.SmartContract.ContractCall.csproj @@ -1,8 +1,6 @@ - net7.0 - disable ContractCall diff --git a/examples/Example.SmartContract.Event/Example.SmartContract.Event.csproj b/examples/Example.SmartContract.Event/Example.SmartContract.Event.csproj index d641e6477..80c008054 100644 --- a/examples/Example.SmartContract.Event/Example.SmartContract.Event.csproj +++ b/examples/Example.SmartContract.Event/Example.SmartContract.Event.csproj @@ -1,8 +1,6 @@ - net7.0 - disable Event diff --git a/examples/Example.SmartContract.Exception/Example.SmartContract.Exception.csproj b/examples/Example.SmartContract.Exception/Example.SmartContract.Exception.csproj index f0d365483..592d53f3f 100644 --- a/examples/Example.SmartContract.Exception/Example.SmartContract.Exception.csproj +++ b/examples/Example.SmartContract.Exception/Example.SmartContract.Exception.csproj @@ -1,8 +1,6 @@ - net7.0 - disable Exception diff --git a/examples/Example.SmartContract.HelloWorld/Example.SmartContract.HelloWorld.csproj b/examples/Example.SmartContract.HelloWorld/Example.SmartContract.HelloWorld.csproj index 3ef48ddcd..6d516fa09 100644 --- a/examples/Example.SmartContract.HelloWorld/Example.SmartContract.HelloWorld.csproj +++ b/examples/Example.SmartContract.HelloWorld/Example.SmartContract.HelloWorld.csproj @@ -1,8 +1,6 @@ - net7.0 - disable HelloWorld diff --git a/examples/Example.SmartContract.Inscription/Example.SmartContract.Inscription.csproj b/examples/Example.SmartContract.Inscription/Example.SmartContract.Inscription.csproj index 370a12122..afde06410 100644 --- a/examples/Example.SmartContract.Inscription/Example.SmartContract.Inscription.csproj +++ b/examples/Example.SmartContract.Inscription/Example.SmartContract.Inscription.csproj @@ -1,8 +1,6 @@ - net7.0 - disable Inscription diff --git a/examples/Example.SmartContract.Modifier/Example.SmartContract.Modifier.csproj b/examples/Example.SmartContract.Modifier/Example.SmartContract.Modifier.csproj index da6967e95..ab78d1534 100644 --- a/examples/Example.SmartContract.Modifier/Example.SmartContract.Modifier.csproj +++ b/examples/Example.SmartContract.Modifier/Example.SmartContract.Modifier.csproj @@ -1,8 +1,6 @@ - net7.0 - disable Modifier diff --git a/examples/Example.SmartContract.NEP17/Example.SmartContract.NEP17.csproj b/examples/Example.SmartContract.NEP17/Example.SmartContract.NEP17.csproj index 496917f66..e5f50d64b 100644 --- a/examples/Example.SmartContract.NEP17/Example.SmartContract.NEP17.csproj +++ b/examples/Example.SmartContract.NEP17/Example.SmartContract.NEP17.csproj @@ -1,8 +1,6 @@ - net7.0 - disable NEP17 diff --git a/examples/Example.SmartContract.NFT/Example.SmartContract.NFT.csproj b/examples/Example.SmartContract.NFT/Example.SmartContract.NFT.csproj index 4a761d58a..999246b58 100644 --- a/examples/Example.SmartContract.NFT/Example.SmartContract.NFT.csproj +++ b/examples/Example.SmartContract.NFT/Example.SmartContract.NFT.csproj @@ -1,8 +1,6 @@ - net7.0 - disable NFT diff --git a/examples/Example.SmartContract.Oracle/Example.SmartContract.Oracle.csproj b/examples/Example.SmartContract.Oracle/Example.SmartContract.Oracle.csproj index 515e82b3b..b4ef64523 100644 --- a/examples/Example.SmartContract.Oracle/Example.SmartContract.Oracle.csproj +++ b/examples/Example.SmartContract.Oracle/Example.SmartContract.Oracle.csproj @@ -1,8 +1,6 @@ - net7.0 - disable Oracle diff --git a/examples/Example.SmartContract.Storage/Example.SmartContract.Storage.csproj b/examples/Example.SmartContract.Storage/Example.SmartContract.Storage.csproj index d1ccfe790..f7c36aeab 100644 --- a/examples/Example.SmartContract.Storage/Example.SmartContract.Storage.csproj +++ b/examples/Example.SmartContract.Storage/Example.SmartContract.Storage.csproj @@ -1,8 +1,6 @@ - net7.0 - disable Storage diff --git a/examples/Example.SmartContract.Transfer/TransferContract.cs b/examples/Example.SmartContract.Transfer/TransferContract.cs index af5c44a9e..b97b25aaa 100644 --- a/examples/Example.SmartContract.Transfer/TransferContract.cs +++ b/examples/Example.SmartContract.Transfer/TransferContract.cs @@ -14,6 +14,9 @@ using Neo.SmartContract.Framework.Native; using System.ComponentModel; +using System.Numerics; +using Neo; +using Neo.SmartContract.Framework.Services; namespace Transfer; @@ -28,11 +31,9 @@ namespace Transfer; [ContractPermission(Permission.WildCard, Method.WildCard)] public class TransferContract : SmartContract { - [Safe] - public static string SayHello(string name) - { - return $"Hello, {name}"; - } + + [Hash160("NUuJw4C4XJFzxAvSZnFTfsNoWZytmQKXQP")] + private static readonly UInt160 Owner = default; [DisplayName("_deploy")] public static void OnDeployment(object data, bool update) @@ -58,4 +59,16 @@ public static bool Destroy() ContractManagement.Destroy(); return true; } + + /// + /// Transfer method that demonstrate how to transfer NEO and GAS + /// + /// Target address to send Neo and GAS + /// Amount of tokens to be sent + public static void Transfer(UInt160 to, BigInteger amount) + { + ExecutionEngine.Assert(Runtime.CheckWitness(Owner)); + ExecutionEngine.Assert(NEO.Transfer(Runtime.ExecutingScriptHash, to, amount)); + ExecutionEngine.Assert(GAS.Transfer(Runtime.ExecutingScriptHash, to, GAS.BalanceOf(Runtime.ExecutingScriptHash), true)); + } } diff --git a/examples/Example.SmartContract.ZKP/Example.SmartContract.ZKP.csproj b/examples/Example.SmartContract.ZKP/Example.SmartContract.ZKP.csproj index 6d339401e..dbf4d1cba 100644 --- a/examples/Example.SmartContract.ZKP/Example.SmartContract.ZKP.csproj +++ b/examples/Example.SmartContract.ZKP/Example.SmartContract.ZKP.csproj @@ -1,8 +1,6 @@ - net7.0 - disable ZKP From b6d6c1e8b617677b3e9d884e08a0a16600fadd4f Mon Sep 17 00:00:00 2001 From: Jim8y Date: Mon, 26 Feb 2024 16:11:39 +0800 Subject: [PATCH 05/19] fix coverage issue --- .../templates/CoverageContractTests.cs | 1 + .../templates/neocontractoracle/OracleRequestTests.cs | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Neo.SmartContract.Template.UnitTests/templates/CoverageContractTests.cs b/tests/Neo.SmartContract.Template.UnitTests/templates/CoverageContractTests.cs index a947c8507..b487a1d0a 100644 --- a/tests/Neo.SmartContract.Template.UnitTests/templates/CoverageContractTests.cs +++ b/tests/Neo.SmartContract.Template.UnitTests/templates/CoverageContractTests.cs @@ -1,5 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.SmartContract.Template.UnitTests.templates.neocontractnep17; +using Neo.SmartContract.Template.UnitTests.templates.neocontractoracle; using Neo.SmartContract.Template.UnitTests.templates.neocontractowner; using Neo.SmartContract.Testing; using Neo.SmartContract.Testing.Coverage; diff --git a/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs b/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs index 38b3fd537..4b202e664 100644 --- a/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs +++ b/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs @@ -6,10 +6,11 @@ using Neo.VM; using System.Text; -namespace Neo.SmartContract.Template.UnitTests.templates.neocontractowner +namespace Neo.SmartContract.Template.UnitTests.templates.neocontractoracle { + /// - /// You need to build the solution to resolve Ownable class. + /// You need to build the solution to resolve OracleRequest class. /// [TestClass] public class OracleRequestTests : TestBase From d61e4ffad9e2791ac368a43252d81661d387f9f0 Mon Sep 17 00:00:00 2001 From: Shargon Date: Mon, 26 Feb 2024 01:47:14 -0800 Subject: [PATCH 06/19] Update tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs --- .../Contract_SupportedStandard11Enum.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs b/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs index 201253253..fb38c56ae 100644 --- a/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs +++ b/tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs @@ -4,7 +4,6 @@ namespace Neo.SmartContract.Framework.UnitTests.TestClasses { - [SupportedStandards(NepStandard.Nep11)] public class Contract_SupportedStandard11Enum : Nep11Token, INep11Payment { From 28c829d62f262db003c99ccb1bdd0bdfc05b9ffa Mon Sep 17 00:00:00 2001 From: Jim8y Date: Mon, 26 Feb 2024 17:52:42 +0800 Subject: [PATCH 07/19] remove conflict --- .../templates/neocontractowner/Ownable.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs b/src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs index ff023333a..1513efa3f 100644 --- a/src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs +++ b/src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs @@ -37,16 +37,10 @@ private static bool IsOwner() => [DisplayName("SetOwner")] public static event OnSetOwnerDelegate OnSetOwner; -<<<<<<< HEAD:src/Neo.SmartContract.Template/templates/neocontractowner/Contract1.cs - // TODO: Replace it with your own address. - [Hash160("")] - private static readonly UInt160 InitialOwner = default; -======= public static void SetOwner(UInt160 newOwner) { if (IsOwner() == false) throw new InvalidOperationException("No Authorization!"); ->>>>>>> master:src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs ExecutionEngine.Assert(newOwner.IsValid && !newOwner.IsZero, "owner must be valid"); From f0c1d6e26dbc31c8341f59543bf6adaa69dd2a60 Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 27 Feb 2024 05:54:11 -0800 Subject: [PATCH 08/19] Update tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs --- .../templates/neocontractoracle/OracleRequestTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs b/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs index 617e7ce5a..b84209cad 100644 --- a/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs +++ b/tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs @@ -8,7 +8,6 @@ namespace Neo.SmartContract.Template.UnitTests.templates.neocontractoracle { - /// /// You need to build the solution to resolve OracleRequest class. /// From 79f01a36f347b6d4c81bdad36ee6636bb2aca26a Mon Sep 17 00:00:00 2001 From: Jimmy Date: Tue, 27 Feb 2024 21:55:25 +0800 Subject: [PATCH 09/19] Update src/Neo.SmartContract.Framework/Interfaces/INEP11Payment.cs --- src/Neo.SmartContract.Framework/Interfaces/INEP11Payment.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Neo.SmartContract.Framework/Interfaces/INEP11Payment.cs b/src/Neo.SmartContract.Framework/Interfaces/INEP11Payment.cs index 693666dfc..fa27c78a4 100644 --- a/src/Neo.SmartContract.Framework/Interfaces/INEP11Payment.cs +++ b/src/Neo.SmartContract.Framework/Interfaces/INEP11Payment.cs @@ -1,4 +1,3 @@ -#nullable enable using System.Numerics; namespace Neo.SmartContract.Framework.Interfaces; From 528d26387493bf9b12639c5c26945e337a1eed8a Mon Sep 17 00:00:00 2001 From: Jim8y Date: Tue, 27 Feb 2024 22:01:07 +0800 Subject: [PATCH 10/19] fix url --- examples/Example.SmartContract.ContractCall/ContractCall.cs | 2 +- examples/Example.SmartContract.Event/Event.cs | 2 +- examples/Example.SmartContract.Exception/Exception.cs | 2 +- examples/Example.SmartContract.HelloWorld/HelloWorld.cs | 2 +- examples/Example.SmartContract.Inscription/Inscription.cs | 2 +- examples/Example.SmartContract.Modifier/Modifier.cs | 2 +- examples/Example.SmartContract.NEP17/Nep17Token.cs | 2 +- examples/Example.SmartContract.NFT/Loot.cs | 2 +- examples/Example.SmartContract.Oracle/Oracle.cs | 2 +- examples/Example.SmartContract.Storage/Storage.cs | 2 +- examples/Example.SmartContract.Transfer/TransferContract.cs | 2 +- examples/Example.SmartContract.ZKP/ZKP.cs | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/Example.SmartContract.ContractCall/ContractCall.cs b/examples/Example.SmartContract.ContractCall/ContractCall.cs index 37f927e44..34d9fba05 100644 --- a/examples/Example.SmartContract.ContractCall/ContractCall.cs +++ b/examples/Example.SmartContract.ContractCall/ContractCall.cs @@ -24,7 +24,7 @@ namespace ContractCall; [ContractEmail("core@neo.org")] [ContractVersion("0.0.1")] [ContractDescription("A sample contract to demonstrate how to call a contract")] -[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.ContractCall")] +[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] public class SampleContractCall : SmartContract { [Hash160("0x13a83e059c2eedd5157b766d3357bc826810905e")] diff --git a/examples/Example.SmartContract.Event/Event.cs b/examples/Example.SmartContract.Event/Event.cs index 61ae0ea42..fd6fc2c12 100644 --- a/examples/Example.SmartContract.Event/Event.cs +++ b/examples/Example.SmartContract.Event/Event.cs @@ -22,7 +22,7 @@ namespace Event; [ContractAuthor("code-dev", "core@neo.org")] [ContractDescription("A sample contract that demonstrates how to use Events")] [ContractVersion("0.0.1")] -[ContractSourceCode("https://github.com/neo-project/samples")] +[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] public class SampleEvent : SmartContract { diff --git a/examples/Example.SmartContract.Exception/Exception.cs b/examples/Example.SmartContract.Exception/Exception.cs index 847ac2b98..3d050b49a 100644 --- a/examples/Example.SmartContract.Exception/Exception.cs +++ b/examples/Example.SmartContract.Exception/Exception.cs @@ -19,7 +19,7 @@ namespace Exception [ContractAuthor("core-dev", "core@neo.org")] [ContractDescription("A sample contract to demonstrate how to handle exception")] [ContractVersion("0.0.1")] - [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Exception")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] public class SampleException : SmartContract { diff --git a/examples/Example.SmartContract.HelloWorld/HelloWorld.cs b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs index 0dd0ccc5b..76bf09a3e 100644 --- a/examples/Example.SmartContract.HelloWorld/HelloWorld.cs +++ b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs @@ -21,7 +21,7 @@ namespace HelloWorld; [ContractDescription("A simple `hello world` contract")] [ContractEmail("core@neo.org")] [ContractVersion("0.0.1")] -[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/HelloWorld")] +[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] public class HelloWorldorld : SmartContract { diff --git a/examples/Example.SmartContract.Inscription/Inscription.cs b/examples/Example.SmartContract.Inscription/Inscription.cs index 4e4c8b19f..64dc23922 100644 --- a/examples/Example.SmartContract.Inscription/Inscription.cs +++ b/examples/Example.SmartContract.Inscription/Inscription.cs @@ -23,7 +23,7 @@ namespace Inscription [ContractAuthor("core-dev", "core@neo.org")] [ContractDescription("A sample inscription contract.")] [ContractVersion("0.0.1")] - [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Inscription")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] public class SampleInscription : SmartContract { diff --git a/examples/Example.SmartContract.Modifier/Modifier.cs b/examples/Example.SmartContract.Modifier/Modifier.cs index 62ad7c340..5aaaad8a2 100644 --- a/examples/Example.SmartContract.Modifier/Modifier.cs +++ b/examples/Example.SmartContract.Modifier/Modifier.cs @@ -39,7 +39,7 @@ public override void Exit() { } [ContractAuthor("core-dev", "core@neo.org")] [ContractDescription("A sample contract to demonstrate how to use modifiers")] [ContractVersion("0.0.1")] - [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Exception")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] public class SampleModifier : SmartContract { [OwnerOnly("AAAAAAAAAAAAAAAAAAAAAAAAAAA=")] diff --git a/examples/Example.SmartContract.NEP17/Nep17Token.cs b/examples/Example.SmartContract.NEP17/Nep17Token.cs index 577c4be6a..944af00f4 100644 --- a/examples/Example.SmartContract.NEP17/Nep17Token.cs +++ b/examples/Example.SmartContract.NEP17/Nep17Token.cs @@ -26,7 +26,7 @@ namespace NEP17 [ContractAuthor("core-dev", "core@neo.org")] [ContractVersion("0.0.1")] [ContractDescription("A sample NEP-17 token")] - [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.NEP17")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] [SupportedStandards(NepStandard.Nep17)] public class SampleNep17Token : Nep17Token, INep17Payment diff --git a/examples/Example.SmartContract.NFT/Loot.cs b/examples/Example.SmartContract.NFT/Loot.cs index b68b06ea2..9bd492a67 100644 --- a/examples/Example.SmartContract.NFT/Loot.cs +++ b/examples/Example.SmartContract.NFT/Loot.cs @@ -26,7 +26,7 @@ namespace NFT [ContractDescription("This is a text Example.SmartContract.NFT")] [SupportedStandards(NepStandard.Nep11)] [ContractPermission(Permission.WildCard, Method.OnNEP11Payment)] - [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Loot")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] public partial class Loot : Nep11Token { public override string Symbol { [Safe] get => "sLoot"; } diff --git a/examples/Example.SmartContract.Oracle/Oracle.cs b/examples/Example.SmartContract.Oracle/Oracle.cs index 787af8aa3..67a522dc5 100644 --- a/examples/Example.SmartContract.Oracle/Oracle.cs +++ b/examples/Example.SmartContract.Oracle/Oracle.cs @@ -22,7 +22,7 @@ namespace Oracle [ContractAuthor("code-dev", "core@neo.org")] [ContractDescription("A sample contract to demonstrate how to use Example.SmartContract.Oracle Service")] [ContractVersion("0.0.1")] - [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Oracle")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] public class SampleOracle : SmartContract { diff --git a/examples/Example.SmartContract.Storage/Storage.cs b/examples/Example.SmartContract.Storage/Storage.cs index 06696b37d..0644fd297 100644 --- a/examples/Example.SmartContract.Storage/Storage.cs +++ b/examples/Example.SmartContract.Storage/Storage.cs @@ -20,7 +20,7 @@ namespace Storage [ContractAuthor("code-dev", "core@neo.org")] [ContractDescription("A sample contract to demonstrate how to use storage")] [ContractVersion("0.0.1")] - [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/Example.SmartContract.Storage")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] public class SampleStorage : SmartContract { diff --git a/examples/Example.SmartContract.Transfer/TransferContract.cs b/examples/Example.SmartContract.Transfer/TransferContract.cs index b97b25aaa..2412a0358 100644 --- a/examples/Example.SmartContract.Transfer/TransferContract.cs +++ b/examples/Example.SmartContract.Transfer/TransferContract.cs @@ -27,7 +27,7 @@ namespace Transfer; [ContractAuthor("", "")] [ContractDescription("")] [ContractVersion("1.0.0.0")] -[ContractSourceCode("https://github.com/cschuchardt88/neo-templates")] +[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] public class TransferContract : SmartContract { diff --git a/examples/Example.SmartContract.ZKP/ZKP.cs b/examples/Example.SmartContract.ZKP/ZKP.cs index ceb04c2f5..50390ce68 100644 --- a/examples/Example.SmartContract.ZKP/ZKP.cs +++ b/examples/Example.SmartContract.ZKP/ZKP.cs @@ -21,7 +21,7 @@ namespace ZKP [ContractAuthor("code-dev")] [ContractVersion("0.0.1")] [ContractDescription("A sample contract to demonstrate how to use Example.SmartContract.ZKPil")] - [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/examples/ZKP")] + [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] public class SampleZKP : SmartContract { From 39d28f93310efe86361831d591c779ad8facb3a8 Mon Sep 17 00:00:00 2001 From: Jim8y Date: Tue, 27 Feb 2024 22:05:23 +0800 Subject: [PATCH 11/19] fix name --- examples/Example.SmartContract.HelloWorld/HelloWorld.cs | 2 +- examples/Example.SmartContract.NFT/Loot.cs | 2 +- examples/Example.SmartContract.Transfer/TransferContract.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/Example.SmartContract.HelloWorld/HelloWorld.cs b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs index 76bf09a3e..260c7cb6e 100644 --- a/examples/Example.SmartContract.HelloWorld/HelloWorld.cs +++ b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs @@ -17,7 +17,7 @@ namespace HelloWorld; -[DisplayName("Example.SmartContract.HelloWorld")] +[DisplayName("SampleHelloWorld")] [ContractDescription("A simple `hello world` contract")] [ContractEmail("core@neo.org")] [ContractVersion("0.0.1")] diff --git a/examples/Example.SmartContract.NFT/Loot.cs b/examples/Example.SmartContract.NFT/Loot.cs index 9bd492a67..1026933fd 100644 --- a/examples/Example.SmartContract.NFT/Loot.cs +++ b/examples/Example.SmartContract.NFT/Loot.cs @@ -21,7 +21,7 @@ namespace NFT { - [DisplayName("Secure-Loot")] + [DisplayName("SampleLootNFT")] [ContractAuthor("core-dev", "core@neo.org")] [ContractDescription("This is a text Example.SmartContract.NFT")] [SupportedStandards(NepStandard.Nep11)] diff --git a/examples/Example.SmartContract.Transfer/TransferContract.cs b/examples/Example.SmartContract.Transfer/TransferContract.cs index 2412a0358..a6711ccdf 100644 --- a/examples/Example.SmartContract.Transfer/TransferContract.cs +++ b/examples/Example.SmartContract.Transfer/TransferContract.cs @@ -23,7 +23,7 @@ namespace Transfer; /// /// This is a sample contract that can be used as a template for creating new contracts. /// -[DisplayName("Contract1")] +[DisplayName("SampleTransferContract")] [ContractAuthor("", "")] [ContractDescription("")] [ContractVersion("1.0.0.0")] From c3fd99bd61af3c4b1dd5db658efe9f5969c96234 Mon Sep 17 00:00:00 2001 From: Jim8y Date: Tue, 27 Feb 2024 22:11:39 +0800 Subject: [PATCH 12/19] remove extra file --- .../templates/CoverageContractTests.cs | 59 ------------------- 1 file changed, 59 deletions(-) delete mode 100644 tests/Neo.SmartContract.Template.UnitTests/templates/CoverageContractTests.cs diff --git a/tests/Neo.SmartContract.Template.UnitTests/templates/CoverageContractTests.cs b/tests/Neo.SmartContract.Template.UnitTests/templates/CoverageContractTests.cs deleted file mode 100644 index b487a1d0a..000000000 --- a/tests/Neo.SmartContract.Template.UnitTests/templates/CoverageContractTests.cs +++ /dev/null @@ -1,59 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Neo.SmartContract.Template.UnitTests.templates.neocontractnep17; -using Neo.SmartContract.Template.UnitTests.templates.neocontractoracle; -using Neo.SmartContract.Template.UnitTests.templates.neocontractowner; -using Neo.SmartContract.Testing; -using Neo.SmartContract.Testing.Coverage; -using Neo.SmartContract.Testing.Coverage.Formats; - -namespace Neo.SmartContract.Template.UnitTests.templates -{ - [TestClass] - public class CoverageContractTests - { - /// - /// Required coverage to be success - /// - public static decimal RequiredCoverage { get; set; } = 1M; - - [AssemblyCleanup] - public static void EnsureCoverage() - { - // Join here all of your coverage sources - - var coverageNep17 = Nep17ContractTests.Coverage; - coverageNep17?.Join(OwnerContractTests.Coverage); - var coverageOwnable = OwnableContractTests.Coverage; - var coverageOracle = OracleRequestTests.Coverage; - - // Dump coverage to console - - Assert.IsNotNull(coverageNep17, "NEP17 coverage can't be null"); - Assert.IsNotNull(coverageOwnable, "Ownable coverage can't be null"); - Assert.IsNotNull(coverageOracle, "Oracle coverage can't be null"); - - var coverage = new CoveredCollection(coverageNep17, coverageOwnable, coverageOracle); - - // Dump current coverage - - Console.WriteLine(coverage.Dump()); - File.WriteAllText("coverage.instruction.html", coverage.Dump(DumpFormat.Html)); - - // Write the cobertura format - - File.WriteAllText("coverage.cobertura.xml", new CoberturaFormat( - (coverageNep17, Nep17Contract.DebugInfo), - (coverageOwnable, Ownable.DebugInfo), - (coverageOracle, OracleRequest.DebugInfo) - ).Dump()); - - // Write the report to the specific path - - CoverageReporting.CreateReport("coverage.cobertura.xml", "./coverageReport/"); - - // Ensure that the coverage is more than X% at the end of the tests - - Assert.IsTrue(coverage.CoveredLinesPercentage >= RequiredCoverage, $"Coverage is less than {RequiredCoverage:P2}"); - } - } -} From 862a2a07276190e03f156347c2e6f4304a8087f1 Mon Sep 17 00:00:00 2001 From: Jim8y Date: Tue, 27 Feb 2024 23:16:53 +0800 Subject: [PATCH 13/19] revert --- .../templates/neocontractnep17/Nep17Contract.cs | 4 ++-- .../templates/neocontractoracle/OracleRequest.cs | 4 ++-- .../templates/neocontractowner/Ownable.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs b/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs index 12e601cf3..3778a5163 100644 --- a/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs +++ b/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs @@ -11,11 +11,11 @@ namespace Neo.SmartContract.Template { [DisplayName(nameof(Nep17Contract))] [ManifestExtra("Author", "")] - [ContractDescription( "")] + [ManifestExtra("Description", "")] [ManifestExtra("Email", "")] [ManifestExtra("Version", "")] [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template/templates/neocontractnep17/Nep17Contract.cs")] - [ContractPermission(Permission.WildCard, Method.WildCard)] + [ContractPermission("*", "*")] [SupportedStandards("NEP-17")] public class Nep17Contract : Neo.SmartContract.Framework.Nep17Token { diff --git a/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs b/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs index 891e61534..4f9cc2114 100644 --- a/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs +++ b/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs @@ -12,11 +12,11 @@ namespace Neo.SmartContract.Template { [DisplayName(nameof(OracleRequest))] [ManifestExtra("Author", "")] - [ContractDescription( "")] + [ManifestExtra("Description", "")] [ManifestExtra("Email", "")] [ManifestExtra("Version", "")] [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs")] - [ContractPermission(Permission.WildCard, Method.WildCard)] + [ContractPermission("*","*")] public class OracleRequest : Neo.SmartContract.Framework.SmartContract { [Safe] diff --git a/src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs b/src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs index 214e7b568..9f54ad119 100644 --- a/src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs +++ b/src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs @@ -11,11 +11,11 @@ namespace Neo.SmartContract.Template { [DisplayName(nameof(Ownable))] [ManifestExtra("Author", "")] - [ContractDescription( "")] + [ManifestExtra("Description", "")] [ManifestExtra("Email", "")] [ManifestExtra("Version", "")] [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template/templates/neocontractowner/Ownable.cs")] - [ContractPermission(Permission.WildCard, Method.WildCard)] + [ContractPermission("*", "*")] public class Ownable : Neo.SmartContract.Framework.SmartContract { #region Owner From ae15f04e209a5d667ccf602466dae020c72e084b Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 28 Feb 2024 02:56:33 -0800 Subject: [PATCH 14/19] Update TransferContract.cs --- .../TransferContract.cs | 26 ------------------- 1 file changed, 26 deletions(-) diff --git a/examples/Example.SmartContract.Transfer/TransferContract.cs b/examples/Example.SmartContract.Transfer/TransferContract.cs index a6711ccdf..237aafa93 100644 --- a/examples/Example.SmartContract.Transfer/TransferContract.cs +++ b/examples/Example.SmartContract.Transfer/TransferContract.cs @@ -31,35 +31,9 @@ namespace Transfer; [ContractPermission(Permission.WildCard, Method.WildCard)] public class TransferContract : SmartContract { - [Hash160("NUuJw4C4XJFzxAvSZnFTfsNoWZytmQKXQP")] private static readonly UInt160 Owner = default; - [DisplayName("_deploy")] - public static void OnDeployment(object data, bool update) - { - if (update) - { - // Add logic for fixing contract on update - return; - } - // Add logic here for 1st time deployed - } - - // TODO: Allow ONLY contract owner to call update - public static bool Update(ByteString nefFile, string manifest) - { - ContractManagement.Update(nefFile, manifest); - return true; - } - - // TODO: Allow ONLY contract owner to call destroy - public static bool Destroy() - { - ContractManagement.Destroy(); - return true; - } - /// /// Transfer method that demonstrate how to transfer NEO and GAS /// From a0fd5c1737364f064a4eb7dc6cc4ab5c6a42425b Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 28 Feb 2024 12:16:51 +0100 Subject: [PATCH 15/19] Some fixes & clean code --- .../ContractCall.cs | 6 +- examples/Example.SmartContract.Event/Event.cs | 1 - .../Exception.cs | 4 + .../HelloWorld.cs | 26 --- .../Inscription.cs | 26 --- .../Modifier.cs | 6 +- .../Example.SmartContract.NEP17/Nep17Token.cs | 2 +- .../Example.SmartContract.NFT/Loot.Admin.cs | 16 +- examples/Example.SmartContract.NFT/Loot.cs | 43 ++-- .../Example.SmartContract.NFT/Token.Item.cs | 200 +++++++++--------- examples/Example.SmartContract.NFT/Token.cs | 4 +- examples/Example.SmartContract.NFT/Tools.cs | 29 --- .../Example.SmartContract.Oracle/Oracle.cs | 45 +--- neo-devpack-dotnet.sln | 32 +-- 14 files changed, 157 insertions(+), 283 deletions(-) delete mode 100644 examples/Example.SmartContract.NFT/Tools.cs diff --git a/examples/Example.SmartContract.ContractCall/ContractCall.cs b/examples/Example.SmartContract.ContractCall/ContractCall.cs index 34d9fba05..e632cbc66 100644 --- a/examples/Example.SmartContract.ContractCall/ContractCall.cs +++ b/examples/Example.SmartContract.ContractCall/ContractCall.cs @@ -9,12 +9,10 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. -using System.ComponentModel; -using Neo; -using Neo.SmartContract; using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Attributes; using Neo.SmartContract.Framework.Services; +using System.ComponentModel; using System.Numerics; namespace ContractCall; @@ -32,9 +30,9 @@ public class SampleContractCall : SmartContract public static void onNEP17Payment(UInt160 from, BigInteger amount, BigInteger data) { - UInt160 tokenHash = Runtime.CallingScriptHash; if (!data.Equals(123)) return; UInt160 @this = Runtime.ExecutingScriptHash; + UInt160 tokenHash = Runtime.CallingScriptHash; BigInteger balanceOf = (BigInteger)Contract.Call(tokenHash, "balanceOf", CallFlags.All, @this); Contract.Call(DummyTarget, "dummyMethod", CallFlags.All, @this, tokenHash, balanceOf); } diff --git a/examples/Example.SmartContract.Event/Event.cs b/examples/Example.SmartContract.Event/Event.cs index fd6fc2c12..3db3c405f 100644 --- a/examples/Example.SmartContract.Event/Event.cs +++ b/examples/Example.SmartContract.Event/Event.cs @@ -11,7 +11,6 @@ using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Attributes; -using Neo.SmartContract.Framework.Native; using System; using System.ComponentModel; using System.Numerics; diff --git a/examples/Example.SmartContract.Exception/Exception.cs b/examples/Example.SmartContract.Exception/Exception.cs index 3d050b49a..a310d24c8 100644 --- a/examples/Example.SmartContract.Exception/Exception.cs +++ b/examples/Example.SmartContract.Exception/Exception.cs @@ -25,12 +25,16 @@ public class SampleException : SmartContract { [ByteArray("0a0b0c0d0E0F")] private static readonly ByteString invalidECpoint = default; + [ByteArray("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9")] private static readonly ByteString byteString2Ecpoint = default; + [Hash160("NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq")] private static readonly ByteString validUInt160 = default; + [ByteArray("edcf8679104ec2911a4fe29ad7db232a493e5b990fb1da7af0c7b989948c8925")] private static readonly byte[] validUInt256 = default; + public static object try01() { int v = 0; diff --git a/examples/Example.SmartContract.HelloWorld/HelloWorld.cs b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs index 260c7cb6e..004c59d6b 100644 --- a/examples/Example.SmartContract.HelloWorld/HelloWorld.cs +++ b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs @@ -11,7 +11,6 @@ using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Attributes; -using Neo.SmartContract.Framework.Native; using System.ComponentModel; @@ -30,29 +29,4 @@ public static string SayHello() { return "Hello, World!"; } - - [DisplayName("_deploy")] - public static void OnDeployment(object data, bool update) - { - if (update) - { - // Add logic for fixing contract on update - return; - } - // Add logic here for 1st time deployed - } - - // TODO: Allow ONLY contract owner to call update - public static bool Update(ByteString nefFile, string manifest) - { - ContractManagement.Update(nefFile, manifest); - return true; - } - - // TODO: Allow ONLY contract owner to call destroy - public static bool Destroy() - { - ContractManagement.Destroy(); - return true; - } } diff --git a/examples/Example.SmartContract.Inscription/Inscription.cs b/examples/Example.SmartContract.Inscription/Inscription.cs index 3b9da9b0b..f8867d714 100644 --- a/examples/Example.SmartContract.Inscription/Inscription.cs +++ b/examples/Example.SmartContract.Inscription/Inscription.cs @@ -11,7 +11,6 @@ using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Attributes; -using Neo.SmartContract.Framework.Native; using Neo.SmartContract.Framework.Services; using System; using System.ComponentModel; @@ -46,30 +45,5 @@ public static string GetInscription(UInt160 address) { return Storage.Get(Storage.CurrentContext, address); } - - [DisplayName("_deploy")] - public static void OnDeployment(object data, bool update) - { - if (update) - { - // Add logic for fixing contract on update - return; - } - // Add logic here for 1st time deployed - } - - // TODO: Allow ONLY contract owner to call update - public static bool Update(ByteString nefFile, string manifest) - { - ContractManagement.Update(nefFile, manifest); - return true; - } - - // TODO: Allow ONLY contract owner to call destroy - public static bool Destroy() - { - ContractManagement.Destroy(); - return true; - } } } diff --git a/examples/Example.SmartContract.Modifier/Modifier.cs b/examples/Example.SmartContract.Modifier/Modifier.cs index 592c2b45f..84db0cee3 100644 --- a/examples/Example.SmartContract.Modifier/Modifier.cs +++ b/examples/Example.SmartContract.Modifier/Modifier.cs @@ -17,11 +17,11 @@ namespace Modifier { - public class OwnerOnlyAttribute : ModifierAttribute + public class OnlyOwnerAttribute : ModifierAttribute { readonly UInt160 _owner; - public OwnerOnlyAttribute(string hex) + public OnlyOwnerAttribute(string hex) { _owner = (UInt160)(byte[])StdLib.Base64Decode(hex); } @@ -41,7 +41,7 @@ public override void Exit() { } [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] public class SampleModifier : SmartContract { - [OwnerOnly("AAAAAAAAAAAAAAAAAAAAAAAAAAA=")] + [OnlyOwner("AAAAAAAAAAAAAAAAAAAAAAAAAAA=")] public static bool Test() { return true; diff --git a/examples/Example.SmartContract.NEP17/Nep17Token.cs b/examples/Example.SmartContract.NEP17/Nep17Token.cs index 93bbb6021..78e2c2cca 100644 --- a/examples/Example.SmartContract.NEP17/Nep17Token.cs +++ b/examples/Example.SmartContract.NEP17/Nep17Token.cs @@ -11,12 +11,12 @@ using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Attributes; +using Neo.SmartContract.Framework.Interfaces; using Neo.SmartContract.Framework.Native; using Neo.SmartContract.Framework.Services; using System; using System.ComponentModel; using System.Numerics; -using Neo.SmartContract.Framework.Interfaces; namespace NEP17 { diff --git a/examples/Example.SmartContract.NFT/Loot.Admin.cs b/examples/Example.SmartContract.NFT/Loot.Admin.cs index 941aeb53b..e6f7146fd 100644 --- a/examples/Example.SmartContract.NFT/Loot.Admin.cs +++ b/examples/Example.SmartContract.NFT/Loot.Admin.cs @@ -9,8 +9,6 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. -using Neo; -using Neo.SmartContract; using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Attributes; using Neo.SmartContract.Framework.Native; @@ -30,11 +28,9 @@ namespace NFT /// [Destroy] -- confirmed by jinghui /// [Pause] -- confirmed by jinghui /// [Resume] -- confirmed by jinghui - /// /// public partial class Loot { - [Hash160("NaA5nQieb5YGg5nSFjhJMVEXQCQ5HdukwP")] static readonly UInt160 Owner = default; @@ -46,10 +42,8 @@ public partial class Loot public static bool Verify() => Runtime.CheckWitness(GetOwner()); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void OwnerOnly() => Tools.Require(Verify(), "Authorization failed."); - + private static void OwnerOnly() => ExecutionEngine.Assert(Verify(), "Authorization failed."); /// /// Security Requirements: @@ -58,7 +52,6 @@ public partial class Loot /// /// <1> the new address should be /// a valid address: constrained internally - /// /// /// /// @@ -67,7 +60,7 @@ public static UInt160 SetOwner(UInt160 newOwner) // <0> -- confirmed by jinghui OwnerOnly(); // <1> -- confirmed by jinghui - Tools.Require(newOwner.IsValid, "Loot::UInt160 is invalid."); + ExecutionEngine.Assert(newOwner.IsValid, "Loot::UInt160 is invalid."); OwnerMap.Put("owner", newOwner); return GetOwner(); } @@ -79,11 +72,6 @@ public static UInt160 GetOwner() return owner != null ? (UInt160)owner : Owner; } - public static void _deploy(object _, bool update) - { - if (update) return; - } - public static void Update(ByteString nefFile, string manifest) { OwnerOnly(); diff --git a/examples/Example.SmartContract.NFT/Loot.cs b/examples/Example.SmartContract.NFT/Loot.cs index 69c270b26..871310c3c 100644 --- a/examples/Example.SmartContract.NFT/Loot.cs +++ b/examples/Example.SmartContract.NFT/Loot.cs @@ -37,7 +37,7 @@ public partial class Loot : Nep11Token [Safe] public override Map Properties(ByteString tokenId) { - Tools.Require(Runtime.EntryScriptHash == Runtime.CallingScriptHash); + ExecutionEngine.Assert(Runtime.EntryScriptHash == Runtime.CallingScriptHash); StorageMap tokenMap = new(Storage.CurrentContext, Prefix_Token); TokenState token = (TokenState)StdLib.Deserialize(tokenMap[tokenId]); Map map = new() @@ -54,7 +54,7 @@ public override Map Properties(ByteString tokenId) private TokenState GetToken(BigInteger tokenId) { TokenState token = (TokenState)StdLib.Deserialize(TokenMap[tokenId.ToString()]); - Tools.Require(token is not null, "Token not exists"); + ExecutionEngine.Assert(token is not null, "Token not exists"); return token; } @@ -141,22 +141,18 @@ public string tokenURI(BigInteger tokenId) /// /// Security Requirements: - /// - /// <0> Has to check the validity of the token Id + /// [0] Has to check the validity of the token Id /// both the upper and lower bound - /// - /// <1> shall not be called from a contract - /// - /// <3> tx shall fault if token already taken - /// - /// <4> update the token map. + /// [1] shall not be called from a contract + /// [3] tx shall fault if token already taken + /// [4] update the token map. /// /// public void Claim(BigInteger tokenId) { // 222 reserved to the developer - Tools.Require(!tokenId.IsZero && tokenId < 7778, "Token ID invalid"); - Tools.Require(Runtime.EntryScriptHash == Runtime.CallingScriptHash, "Contract calls are not allowed"); + ExecutionEngine.Assert(!tokenId.IsZero && tokenId < 7778, "Token ID invalid"); + ExecutionEngine.Assert(Runtime.EntryScriptHash == Runtime.CallingScriptHash, "Contract calls are not allowed"); Transaction tx = (Transaction)Runtime.ScriptContainer; MintToken(tokenId, tx.Sender); EventMsg("Player mints success"); @@ -164,16 +160,14 @@ public void Claim(BigInteger tokenId) /// /// Security Requirements: - /// - /// <0> only the owner can call this function - /// - /// <1> the range of the tokenid is to be in (7777, 8001) + /// [0] only the owner can call this function + /// [1] the range of the tokenid is to be in (7777, 8001) /// /// public void OwnerClaim(BigInteger tokenId) { OwnerOnly(); - Tools.Require(tokenId > 7777 && tokenId < 8001, "Token ID invalid"); + ExecutionEngine.Assert(tokenId > 7777 && tokenId < 8001, "Token ID invalid"); var sender = GetOwner(); MintToken(tokenId, sender); EventMsg("Owner mints success"); @@ -181,11 +175,8 @@ public void OwnerClaim(BigInteger tokenId) /// /// Security Requirements: - /// - /// <0> the transaction should `FAULT` if the token already taken - /// - /// <1> has to update the taken map if a new token is mint. - /// + /// [0] the transaction should `FAULT` if the token already taken + /// [1] has to update the taken map if a new token is mint. /// /// /// @@ -200,10 +191,8 @@ private void MintToken(BigInteger tokenId, UInt160 sender) /// /// Security requirements: - /// - /// <0> throw exception if token already taken - /// - /// <1> should get a random number as credential that + /// [0] throw exception if token already taken + /// [1] should get a random number as credential that /// is not predictable and not linked to the tokenId /// /// @@ -212,7 +201,7 @@ private void MintToken(BigInteger tokenId, UInt160 sender) private BigInteger CheckClaim(BigInteger tokenId) { // <0> -- confirmed - Tools.Require(TokenIndexMap.Get(tokenId.ToString()) != "taken", "Token already claimed."); + ExecutionEngine.Assert(TokenIndexMap.Get(tokenId.ToString()) != "taken", "Token already claimed."); // <1> -- confirmed return Runtime.GetRandom(); } diff --git a/examples/Example.SmartContract.NFT/Token.Item.cs b/examples/Example.SmartContract.NFT/Token.Item.cs index 15ebaa301..d338c324b 100644 --- a/examples/Example.SmartContract.NFT/Token.Item.cs +++ b/examples/Example.SmartContract.NFT/Token.Item.cs @@ -14,25 +14,25 @@ namespace NFT public partial class Loot { private readonly string[] _weapons = { - "Warhammer", - "Quarterstaff", - "Maul", - "Mace", - "Club", - "Katana", - "Falchion", - "Scimitar", - "Long Sword", - "Short Sword", - "Ghost Wand", - "Grave Wand", - "Bone Wand", - "Wand", - "Grimoire", - "Chronicle", - "Tome", - "Book" - }; + "Warhammer", + "Quarterstaff", + "Maul", + "Mace", + "Club", + "Katana", + "Falchion", + "Scimitar", + "Long Sword", + "Short Sword", + "Ghost Wand", + "Grave Wand", + "Bone Wand", + "Wand", + "Grimoire", + "Chronicle", + "Tome", + "Book" + }; private readonly string[] _chestArmor = { "Divine Robe", @@ -89,39 +89,39 @@ public partial class Loot }; private readonly string[] _footArmor = { - "Holy Greaves", - "Ornate Greaves", - "Greaves", - "Chain Boots", - "Heavy Boots", - "Demonhide Boots", - "Dragonskin Boots", - "Studded Leather Boots", - "Hard Leather Boots", - "Leather Boots", - "Divine Slippers", - "Silk Slippers", - "Wool Shoes", - "Linen Shoes", - "Shoes" + "Holy Greaves", + "Ornate Greaves", + "Greaves", + "Chain Boots", + "Heavy Boots", + "Demonhide Boots", + "Dragonskin Boots", + "Studded Leather Boots", + "Hard Leather Boots", + "Leather Boots", + "Divine Slippers", + "Silk Slippers", + "Wool Shoes", + "Linen Shoes", + "Shoes" }; private readonly string[] _handArmor = { - "Holy Gauntlets", - "Ornate Gauntlets", - "Gauntlets", - "Chain Gloves", - "Heavy Gloves", - "Demon's Hands", - "Dragonskin Gloves", - "Studded Leather Gloves", - "Hard Leather Gloves", - "Leather Gloves", - "Divine Gloves", - "Silk Gloves", - "Wool Gloves", - "Linen Gloves", - "Gloves" + "Holy Gauntlets", + "Ornate Gauntlets", + "Gauntlets", + "Chain Gloves", + "Heavy Gloves", + "Demon's Hands", + "Dragonskin Gloves", + "Studded Leather Gloves", + "Hard Leather Gloves", + "Leather Gloves", + "Divine Gloves", + "Silk Gloves", + "Wool Gloves", + "Linen Gloves", + "Gloves" }; private readonly string[] _necklaces = { @@ -131,62 +131,62 @@ public partial class Loot }; private readonly string[] _rings = { - "Gold Ring", - "Silver Ring", - "Bronze Ring", - "Platinum Ring", - "Titanium Ring" - }; + "Gold Ring", + "Silver Ring", + "Bronze Ring", + "Platinum Ring", + "Titanium Ring" + }; private readonly string[] _suffixes = { - "of Power", - "of Giants", - "of Titans", - "of Skill", - "of Perfection", - "of Brilliance", - "of Enlightenment", - "of Protection", - "of Anger", - "of Rage", - "of Fury", - "of Vitriol", - "of the Fox", - "of Detection", - "of Reflection", - "of the Twins" - }; + "of Power", + "of Giants", + "of Titans", + "of Skill", + "of Perfection", + "of Brilliance", + "of Enlightenment", + "of Protection", + "of Anger", + "of Rage", + "of Fury", + "of Vitriol", + "of the Fox", + "of Detection", + "of Reflection", + "of the Twins" + }; private readonly string[] _namePrefixes = { - "Agony", "Apocalypse", "Armageddon", "Beast", "Behemoth", "Blight", "Blood", "Bramble", - "Brimstone", "Brood", "Carrion", "Cataclysm", "Chimeric", "Corpse", "Corruption", "Damnation", - "Death", "Demon", "Dire", "Dragon", "Dread", "Doom", "Dusk", "Eagle", "Empyrean", "Fate", "Foe", - "Gale", "Ghoul", "Gloom", "Glyph", "Golem", "Grim", "Hate", "Havoc", "Honour", "Horror", "Hypnotic", - "Kraken", "Loath", "Maelstrom", "Mind", "Miracle", "Morbid", "Oblivion", "Onslaught", "Pain", - "Pandemonium", "Phoenix", "Plague", "Rage", "Rapture", "Rune", "Skull", "Sol", "Soul", "Sorrow", - "Spirit", "Storm", "Tempest", "Torment", "Vengeance", "Victory", "Viper", "Vortex", "Woe", "Wrath", - "Light's", "Shimmering" - }; + "Agony", "Apocalypse", "Armageddon", "Beast", "Behemoth", "Blight", "Blood", "Bramble", + "Brimstone", "Brood", "Carrion", "Cataclysm", "Chimeric", "Corpse", "Corruption", "Damnation", + "Death", "Demon", "Dire", "Dragon", "Dread", "Doom", "Dusk", "Eagle", "Empyrean", "Fate", "Foe", + "Gale", "Ghoul", "Gloom", "Glyph", "Golem", "Grim", "Hate", "Havoc", "Honour", "Horror", "Hypnotic", + "Kraken", "Loath", "Maelstrom", "Mind", "Miracle", "Morbid", "Oblivion", "Onslaught", "Pain", + "Pandemonium", "Phoenix", "Plague", "Rage", "Rapture", "Rune", "Skull", "Sol", "Soul", "Sorrow", + "Spirit", "Storm", "Tempest", "Torment", "Vengeance", "Victory", "Viper", "Vortex", "Woe", "Wrath", + "Light's", "Shimmering" + }; private readonly string[] _nameSuffixes = { - "Bane", - "Root", - "Bite", - "Song", - "Roar", - "Grasp", - "Instrument", - "Glow", - "Bender", - "Shadow", - "Whisper", - "Shout", - "Growl", - "Tear", - "Peak", - "Form", - "Sun", - "Moon" - }; + "Bane", + "Root", + "Bite", + "Song", + "Roar", + "Grasp", + "Instrument", + "Glow", + "Bender", + "Shadow", + "Whisper", + "Shout", + "Growl", + "Tear", + "Peak", + "Form", + "Sun", + "Moon" + }; } } diff --git a/examples/Example.SmartContract.NFT/Token.cs b/examples/Example.SmartContract.NFT/Token.cs index 5afb94727..ec3be557a 100644 --- a/examples/Example.SmartContract.NFT/Token.cs +++ b/examples/Example.SmartContract.NFT/Token.cs @@ -9,10 +9,10 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. -using Neo; using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Services; using System.Numerics; + namespace NFT { public class TokenState : Nep11TokenState @@ -33,7 +33,7 @@ private TokenState(UInt160 owner, BigInteger tokenId, BigInteger credential) public void OwnerOnly() { - Tools.Require(Runtime.CheckWitness(Owner), "Authorization failed."); + ExecutionEngine.Assert(Runtime.CheckWitness(Owner), "Authorization failed."); } } } diff --git a/examples/Example.SmartContract.NFT/Tools.cs b/examples/Example.SmartContract.NFT/Tools.cs deleted file mode 100644 index 87acef4a2..000000000 --- a/examples/Example.SmartContract.NFT/Tools.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (C) 2015-2024 The Neo Project. -// -// Tools.cs file belongs to the neo project and is free -// software distributed under the MIT software license, see the -// accompanying file LICENSE in the main directory of the -// repository or http://www.opensource.org/licenses/mit-license.php -// for more details. -// -// Redistribution and use in source and binary forms with or without -// modifications are permitted. - -using System; -using System.Runtime.CompilerServices; - -namespace NFT -{ - static class Tools - { - /// - /// If the condition `istrue` does not hold, - /// then the transaction throw exception - /// making transaction `FAULT` - /// - /// true condition, has to be true to run - /// Transaction FAULT reason - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static void Require(bool isTrue, string msg = "Invalid") { if (!isTrue) throw new Exception($"Loot::{msg}"); } - } -} diff --git a/examples/Example.SmartContract.Oracle/Oracle.cs b/examples/Example.SmartContract.Oracle/Oracle.cs index 67a522dc5..5bf48fbf1 100644 --- a/examples/Example.SmartContract.Oracle/Oracle.cs +++ b/examples/Example.SmartContract.Oracle/Oracle.cs @@ -26,21 +26,23 @@ namespace Oracle [ContractPermission(Permission.WildCard, Method.WildCard)] public class SampleOracle : SmartContract { + [Safe] + public static string GetResponse() + { + return Storage.Get(Storage.CurrentContext, "Response"); + } - // (string requestedUrl, object jsonValue) - [DisplayName("RequestSuccessful")] - public static event Action OnRequestSuccessful; public static void DoRequest() { /* - JSON DATA + JSON DATA EXAMPLE { "id": "6520ad3c12a5d3765988542a", "record": { "propertyName": "Hello World!" }, "metadata": { - "name": "Example.SmartContract.HelloWorld", + "name": "HelloWorld", "readCountRemaining": 98, "timeToExpire": 86379, "createdAt": "2023-10-07T00:58:36.746Z" @@ -55,43 +57,18 @@ JSON DATA Neo.SmartContract.Framework.Native.Oracle.Request(requestUrl, "$.record.propertyName", "onOracleResponse", null, Neo.SmartContract.Framework.Native.Oracle.MinimumResponseFee); } - // This method is called after the Example.SmartContract.Oracle receives response from requested URL - public static void OnOracleResponse(string requestedUrl, object userData, OracleResponseCode oracleResponse, string jsonString) + // This method is called after the Oracle receives response from requested URL + public static void onOracleResponse(string requestedUrl, object userData, OracleResponseCode oracleResponse, string jsonString) { if (Runtime.CallingScriptHash != Neo.SmartContract.Framework.Native.Oracle.Hash) throw new InvalidOperationException("No Authorization!"); if (oracleResponse != OracleResponseCode.Success) - throw new Exception("Example.SmartContract.Oracle response failure with code " + (byte)oracleResponse); + throw new Exception("Oracle response failure with code " + (byte)oracleResponse); var jsonArrayValues = (object[])StdLib.JsonDeserialize(jsonString); var jsonFirstValue = (string)jsonArrayValues[0]; - OnRequestSuccessful(requestedUrl, jsonFirstValue); - } - - [DisplayName("_deploy")] - public static void OnDeployment(object data, bool update) - { - if (update) - { - // Add logic for fixing contract on update - return; - } - // Add logic here for 1st time deployed - } - - // TODO: Allow ONLY contract owner to call update - public static bool Update(ByteString nefFile, string manifest) - { - ContractManagement.Update(nefFile, manifest); - return true; - } - - // TODO: Allow ONLY contract owner to call destroy - public static bool Destroy() - { - ContractManagement.Destroy(); - return true; + Storage.Put(Storage.CurrentContext, "Response", jsonFirstValue); } } } diff --git a/neo-devpack-dotnet.sln b/neo-devpack-dotnet.sln index 78486592b..600260ddd 100644 --- a/neo-devpack-dotnet.sln +++ b/neo-devpack-dotnet.sln @@ -30,7 +30,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Json", "neo\src\Neo.Jso EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.VM", "neo\src\Neo.VM\Neo.VM.csproj", "{D6D53889-5A10-46A4-BA66-E78B56EC1881}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dependency", "Dependency", "{49D5873D-7B38-48A5-B853-85146F032091}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "dependency", "dependency", "{49D5873D-7B38-48A5-B853-85146F032091}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Testing", "src\Neo.SmartContract.Testing\Neo.SmartContract.Testing.csproj", "{648DCE6F-A0BA-4032-951B-20CF5BBFD998}" EndProject @@ -38,35 +38,35 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Testing.U EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Template.UnitTests", "tests\Neo.SmartContract.Template.UnitTests\Neo.SmartContract.Template.UnitTests.csproj", "{17F45E0B-AB1C-4796-8C99-E5212A5592F8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Extensions", "neo\src\Neo.Extensions\Neo.Extensions.csproj", "{E5EFB018-810D-4297-8921-940FA0B1ED97}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Extensions", "neo\src\Neo.Extensions\Neo.Extensions.csproj", "{E5EFB018-810D-4297-8921-940FA0B1ED97}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.IO", "neo\src\Neo.IO\Neo.IO.csproj", "{C2B7927F-AAA5-432A-8E76-B5080BD7EFB9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.IO", "neo\src\Neo.IO\Neo.IO.csproj", "{C2B7927F-AAA5-432A-8E76-B5080BD7EFB9}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{C10F9957-077F-42C8-B350-8607D4899B7E}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{C10F9957-077F-42C8-B350-8607D4899B7E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.ContractCall", "examples\Example.SmartContract.ContractCall\Example.SmartContract.ContractCall.csproj", "{AE9BF707-FA85-41CB-BE04-3B0AD024329B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.ContractCall", "examples\Example.SmartContract.ContractCall\Example.SmartContract.ContractCall.csproj", "{AE9BF707-FA85-41CB-BE04-3B0AD024329B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Event", "examples\Example.SmartContract.Event\Example.SmartContract.Event.csproj", "{826888EC-9EEF-42DA-A136-048C031D9CAB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.Event", "examples\Example.SmartContract.Event\Example.SmartContract.Event.csproj", "{826888EC-9EEF-42DA-A136-048C031D9CAB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Exception", "examples\Example.SmartContract.Exception\Example.SmartContract.Exception.csproj", "{07936699-8820-4D5B-9B3D-DF3FA944FE1E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.Exception", "examples\Example.SmartContract.Exception\Example.SmartContract.Exception.csproj", "{07936699-8820-4D5B-9B3D-DF3FA944FE1E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.HelloWorld", "examples\Example.SmartContract.HelloWorld\Example.SmartContract.HelloWorld.csproj", "{43617A35-2E3A-43F5-80B3-685ADBF4F908}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.HelloWorld", "examples\Example.SmartContract.HelloWorld\Example.SmartContract.HelloWorld.csproj", "{43617A35-2E3A-43F5-80B3-685ADBF4F908}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Inscription", "examples\Example.SmartContract.Inscription\Example.SmartContract.Inscription.csproj", "{9AF351B8-E3E9-42A7-853A-C98FC159A9E5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.Inscription", "examples\Example.SmartContract.Inscription\Example.SmartContract.Inscription.csproj", "{9AF351B8-E3E9-42A7-853A-C98FC159A9E5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Modifier", "examples\Example.SmartContract.Modifier\Example.SmartContract.Modifier.csproj", "{50E8D694-AC3C-456C-B901-3530E9AFF555}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.Modifier", "examples\Example.SmartContract.Modifier\Example.SmartContract.Modifier.csproj", "{50E8D694-AC3C-456C-B901-3530E9AFF555}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.NEP17", "examples\Example.SmartContract.NEP17\Example.SmartContract.NEP17.csproj", "{FBDF111C-65C0-45E4-8D25-9BE78C35CDC3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.NEP17", "examples\Example.SmartContract.NEP17\Example.SmartContract.NEP17.csproj", "{FBDF111C-65C0-45E4-8D25-9BE78C35CDC3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.NFT", "examples\Example.SmartContract.NFT\Example.SmartContract.NFT.csproj", "{051A2A1E-2225-405F-82F6-79E4D5345DF6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.NFT", "examples\Example.SmartContract.NFT\Example.SmartContract.NFT.csproj", "{051A2A1E-2225-405F-82F6-79E4D5345DF6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Oracle", "examples\Example.SmartContract.Oracle\Example.SmartContract.Oracle.csproj", "{A39DA537-024A-430F-A3BA-034D6E947FA6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.Oracle", "examples\Example.SmartContract.Oracle\Example.SmartContract.Oracle.csproj", "{A39DA537-024A-430F-A3BA-034D6E947FA6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Storage", "examples\Example.SmartContract.Storage\Example.SmartContract.Storage.csproj", "{18A5C205-2102-4F4B-BB76-37660F74709C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.Storage", "examples\Example.SmartContract.Storage\Example.SmartContract.Storage.csproj", "{18A5C205-2102-4F4B-BB76-37660F74709C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.Transfer", "examples\Example.SmartContract.Transfer\Example.SmartContract.Transfer.csproj", "{5319BE3E-A5E9-4AFF-94D6-A669DA214F24}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.Transfer", "examples\Example.SmartContract.Transfer\Example.SmartContract.Transfer.csproj", "{5319BE3E-A5E9-4AFF-94D6-A669DA214F24}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.SmartContract.ZKP", "examples\Example.SmartContract.ZKP\Example.SmartContract.ZKP.csproj", "{141AD5BA-1735-4583-93B6-145CF72721E5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.ZKP", "examples\Example.SmartContract.ZKP\Example.SmartContract.ZKP.csproj", "{141AD5BA-1735-4583-93B6-145CF72721E5}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution From 933d6fa461d0ab9871a2cdc960be3b3419be61bf Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 28 Feb 2024 12:20:29 +0100 Subject: [PATCH 16/19] clean --- examples/Example.SmartContract.NFT/Loot.Admin.cs | 5 ++--- .../templates/neocontractoracle/OracleRequest.cs | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/Example.SmartContract.NFT/Loot.Admin.cs b/examples/Example.SmartContract.NFT/Loot.Admin.cs index e6f7146fd..f47607204 100644 --- a/examples/Example.SmartContract.NFT/Loot.Admin.cs +++ b/examples/Example.SmartContract.NFT/Loot.Admin.cs @@ -47,10 +47,9 @@ public partial class Loot /// /// Security Requirements: - /// <0> Only the owner of the contract + /// [0] Only the owner of the contract /// are allowed to call this function: constrained internally - /// - /// <1> the new address should be + /// [1] the new address should be /// a valid address: constrained internally /// /// diff --git a/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs b/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs index 4f9cc2114..47c9ff267 100644 --- a/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs +++ b/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs @@ -16,7 +16,7 @@ namespace Neo.SmartContract.Template [ManifestExtra("Email", "")] [ManifestExtra("Version", "")] [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs")] - [ContractPermission("*","*")] + [ContractPermission("*", "*")] public class OracleRequest : Neo.SmartContract.Framework.SmartContract { [Safe] From 330ff6a10f70297c0a2e2480f1fdea0317de7005 Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 28 Feb 2024 12:25:33 +0100 Subject: [PATCH 17/19] Clean and comment --- examples/Example.SmartContract.Event/Event.cs | 2 -- .../HelloWorld.cs | 4 ++++ .../Inscription.cs | 17 ++++++++++++++--- examples/Example.SmartContract.Oracle/Oracle.cs | 10 +++++++++- .../neocontractoracle/OracleRequest.cs | 10 +++++++++- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/examples/Example.SmartContract.Event/Event.cs b/examples/Example.SmartContract.Event/Event.cs index 3db3c405f..fa60ed436 100644 --- a/examples/Example.SmartContract.Event/Event.cs +++ b/examples/Example.SmartContract.Event/Event.cs @@ -34,9 +34,7 @@ public static bool Main() { byte[] ba = new byte[] { 0x01, 0x02, 0x03 }; event_name(ba, "oi", 10); // will Example.SmartContract.Runtime.Notify: 'new_event_name', '\x01\x02\x03', 'oi', 10 - event2(ba, 50); // will Example.SmartContract.Runtime.Notify: 'event2', '\x01\x02\x03', '\x32' - return false; } } diff --git a/examples/Example.SmartContract.HelloWorld/HelloWorld.cs b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs index 004c59d6b..ea311f1fe 100644 --- a/examples/Example.SmartContract.HelloWorld/HelloWorld.cs +++ b/examples/Example.SmartContract.HelloWorld/HelloWorld.cs @@ -24,6 +24,10 @@ namespace HelloWorld; [ContractPermission(Permission.WildCard, Method.WildCard)] public class HelloWorldorld : SmartContract { + /// + /// Hello world from NEO! + /// + /// Hello world string [Safe] public static string SayHello() { diff --git a/examples/Example.SmartContract.Inscription/Inscription.cs b/examples/Example.SmartContract.Inscription/Inscription.cs index f8867d714..c64a135f8 100644 --- a/examples/Example.SmartContract.Inscription/Inscription.cs +++ b/examples/Example.SmartContract.Inscription/Inscription.cs @@ -25,11 +25,18 @@ namespace Inscription [ContractPermission(Permission.WildCard, Method.WildCard)] public class SampleInscription : SmartContract { - // Neo.SmartContract.Examples.Event for logging inscriptions + /// + /// Neo.SmartContract.Examples.Event for logging inscriptions + /// [DisplayName("InscriptionAdded")] public static event Action InscriptionAdded; - // Method to store an inscription + /// + /// Method to store an inscription + /// + /// Address + /// Inscription + /// Failure when is not signed by the address public static void AddInscription(UInt160 address, string inscription) { if (!Runtime.CheckWitness(address)) @@ -39,7 +46,11 @@ public static void AddInscription(UInt160 address, string inscription) InscriptionAdded(address, inscription); } - // Method to read an inscription + /// + /// Method to read an inscription + /// + /// Address + /// Inscription readed [Safe] public static string GetInscription(UInt160 address) { diff --git a/examples/Example.SmartContract.Oracle/Oracle.cs b/examples/Example.SmartContract.Oracle/Oracle.cs index 5bf48fbf1..15655f814 100644 --- a/examples/Example.SmartContract.Oracle/Oracle.cs +++ b/examples/Example.SmartContract.Oracle/Oracle.cs @@ -57,7 +57,15 @@ JSON DATA EXAMPLE Neo.SmartContract.Framework.Native.Oracle.Request(requestUrl, "$.record.propertyName", "onOracleResponse", null, Neo.SmartContract.Framework.Native.Oracle.MinimumResponseFee); } - // This method is called after the Oracle receives response from requested URL + /// + /// This method is called after the Oracle receives response from requested URL + /// + /// Requested url + /// User data provided during the request + /// Oracle response code + /// Oracle response data + /// It was not called by the oracle + /// It was not a success public static void onOracleResponse(string requestedUrl, object userData, OracleResponseCode oracleResponse, string jsonString) { if (Runtime.CallingScriptHash != Neo.SmartContract.Framework.Native.Oracle.Hash) diff --git a/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs b/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs index 47c9ff267..42dff9d96 100644 --- a/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs +++ b/src/Neo.SmartContract.Template/templates/neocontractoracle/OracleRequest.cs @@ -50,7 +50,15 @@ JSON DATA EXAMPLE Oracle.Request(requestUrl, "$.record.propertyName", "onOracleResponse", null, Oracle.MinimumResponseFee); } - // This method is called after the Oracle receives response from requested URL + /// + /// This method is called after the Oracle receives response from requested URL + /// + /// Requested url + /// User data provided during the request + /// Oracle response code + /// Oracle response data + /// It was not called by the oracle + /// It was not a success public static void onOracleResponse(string requestedUrl, object userData, OracleResponseCode oracleResponse, string jsonString) { if (Runtime.CallingScriptHash != Oracle.Hash) From 4a7ba49875bf2b69dd37224757e19fbe6da92437 Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 28 Feb 2024 12:27:17 +0100 Subject: [PATCH 18/19] clean --- examples/Example.SmartContract.NEP17/Nep17Token.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/Example.SmartContract.NEP17/Nep17Token.cs b/examples/Example.SmartContract.NEP17/Nep17Token.cs index 78e2c2cca..d1e1d96ef 100644 --- a/examples/Example.SmartContract.NEP17/Nep17Token.cs +++ b/examples/Example.SmartContract.NEP17/Nep17Token.cs @@ -159,6 +159,5 @@ public static bool Update(ByteString nefFile, string manifest) } #endregion - } } From af5f86655e1719b5f84fc66069a3508e90b15bfc Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 28 Feb 2024 12:31:23 +0100 Subject: [PATCH 19/19] Remove todo --- .../Example.SmartContract.NEP17/Nep17Token.cs | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/examples/Example.SmartContract.NEP17/Nep17Token.cs b/examples/Example.SmartContract.NEP17/Nep17Token.cs index d1e1d96ef..f56831903 100644 --- a/examples/Example.SmartContract.NEP17/Nep17Token.cs +++ b/examples/Example.SmartContract.NEP17/Nep17Token.cs @@ -11,7 +11,6 @@ using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Attributes; -using Neo.SmartContract.Framework.Interfaces; using Neo.SmartContract.Framework.Native; using Neo.SmartContract.Framework.Services; using System; @@ -27,7 +26,7 @@ namespace NEP17 [ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")] [ContractPermission(Permission.WildCard, Method.WildCard)] [SupportedStandards(NepStandard.Nep17)] - public class SampleNep17Token : Nep17Token, INep17Payable + public class SampleNep17Token : Nep17Token { #region Owner @@ -124,27 +123,6 @@ public static void SetMinter(UInt160 newMinter) #endregion - #region Payment - - public static bool Withdraw(UInt160 to, BigInteger amount) - { - if (IsOwner() == false) - throw new InvalidOperationException("No Authorization!"); - if (amount <= 0) - return false; - // TODO: Add logic - return true; - } - - // NOTE: Allow NEP-17 tokens to be received for this contract - /// - public void OnNEP17Payment(UInt160 from, BigInteger amount, object? data) - { - // TODO: Add logic - } - - #endregion - #region Basic [Safe]