Example #1
0
        protected NativeContract()
        {
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitPush(Name);
                sb.EmitSysCall(ApplicationEngine.Neo_Native_Call);
                this.Script = sb.ToArray();
            }
            this.Hash = Script.ToScriptHash();
            List <ContractMethodDescriptor> descriptors = new List <ContractMethodDescriptor>();
            List <string> safeMethods = new List <string>();

            foreach (MethodInfo method in GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                ContractMethodAttribute attribute = method.GetCustomAttribute <ContractMethodAttribute>();
                if (attribute is null)
                {
                    continue;
                }
                string name = attribute.Name ?? (method.Name.ToLower()[0] + method.Name.Substring(1));
                descriptors.Add(new ContractMethodDescriptor
                {
                    Name       = name,
                    ReturnType = attribute.ReturnType,
                    Parameters = attribute.ParameterTypes.Zip(attribute.ParameterNames, (t, n) => new ContractParameterDefinition {
                        Type = t, Name = n
                    }).ToArray()
                });
                if (!attribute.RequiredCallFlags.HasFlag(CallFlags.AllowModifyStates))
                {
                    safeMethods.Add(name);
                }
                methods.Add(name, new ContractMethodMetadata
                {
                    Delegate          = (Func <ApplicationEngine, Array, StackItem>)method.CreateDelegate(typeof(Func <ApplicationEngine, Array, StackItem>), this),
                    Price             = attribute.Price,
                    RequiredCallFlags = attribute.RequiredCallFlags
                });
            }
            this.Manifest = new ContractManifest
            {
                Permissions = new[] { ContractPermission.DefaultPermission },
                Abi         = new ContractAbi()
                {
                    Hash    = Hash,
                    Events  = new ContractEventDescriptor[0],
                    Methods = descriptors.ToArray()
                },
                Features    = ContractFeatures.NoProperty,
                Groups      = new ContractGroup[0],
                SafeMethods = WildcardContainer <string> .Create(safeMethods.ToArray()),
                Trusts      = WildcardContainer <UInt160> .Create(),
                Extra       = null,
            };
            contractsList.Add(this);
            contractsNameDictionary.Add(Name, this);
            contractsHashDictionary.Add(Hash, this);
        }
Example #2
0
        protected NativeContract()
        {
            List <ContractMethodMetadata> descriptors = new List <ContractMethodMetadata>();

            foreach (MemberInfo member in GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                ContractMethodAttribute attribute = member.GetCustomAttribute <ContractMethodAttribute>();
                if (attribute is null)
                {
                    continue;
                }
                descriptors.Add(new ContractMethodMetadata(member, attribute));
            }
            descriptors = descriptors.OrderBy(p => p.Name).ThenBy(p => p.Parameters.Length).ToList();
            byte[] script;
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                foreach (ContractMethodMetadata method in descriptors)
                {
                    method.Descriptor.Offset = sb.Offset;
                    sb.EmitPush(0); //version
                    methods.Add(sb.Offset, method);
                    sb.EmitSysCall(ApplicationEngine.System_Contract_CallNative);
                    sb.Emit(OpCode.RET);
                }
                script = sb.ToArray();
            }
            this.Nef = new NefFile
            {
                Compiler = "neo-core-v3.0",
                Tokens   = Array.Empty <MethodToken>(),
                Script   = script
            };
            this.Nef.CheckSum = NefFile.ComputeChecksum(Nef);
            this.Hash         = Helper.GetContractHash(UInt160.Zero, 0, Name);
            this.Manifest     = new ContractManifest
            {
                Name               = Name,
                Groups             = Array.Empty <ContractGroup>(),
                SupportedStandards = Array.Empty <string>(),
                Abi = new ContractAbi()
                {
                    Events  = Array.Empty <ContractEventDescriptor>(),
                    Methods = descriptors.Select(p => p.Descriptor).ToArray()
                },
                Permissions = new[] { ContractPermission.DefaultPermission },
                Trusts      = WildcardContainer <UInt160> .Create(),
                Extra       = null
            };
            if (ProtocolSettings.Default.NativeActivations.TryGetValue(Name, out uint activationIndex))
            {
                this.ActiveBlockIndex = activationIndex;
            }
            contractsList.Add(this);
            contractsDictionary.Add(Hash, this);
        }
Example #3
0
        protected NativeContract()
        {
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitPush(Name);
                sb.EmitSysCall(ApplicationEngine.Neo_Native_Call);
                this.Script = sb.ToArray();
            }
            this.Hash = Script.ToScriptHash();
            List <ContractMethodDescriptor> descriptors = new List <ContractMethodDescriptor>();
            List <string> safeMethods = new List <string>();

            foreach (MemberInfo member in GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                ContractMethodAttribute attribute = member.GetCustomAttribute <ContractMethodAttribute>();
                if (attribute is null)
                {
                    continue;
                }
                ContractMethodMetadata metadata = new ContractMethodMetadata(member, attribute);
                descriptors.Add(new ContractMethodDescriptor
                {
                    Name       = metadata.Name,
                    ReturnType = ToParameterType(metadata.Handler.ReturnType),
                    Parameters = metadata.Parameters.Select(p => new ContractParameterDefinition {
                        Type = ToParameterType(p.Type), Name = p.Name
                    }).ToArray()
                });
                if (!attribute.RequiredCallFlags.HasFlag(CallFlags.AllowModifyStates))
                {
                    safeMethods.Add(metadata.Name);
                }
                methods.Add(metadata.Name, metadata);
            }
            this.Manifest = new ContractManifest
            {
                Groups             = System.Array.Empty <ContractGroup>(),
                Features           = ContractFeatures.NoProperty,
                SupportedStandards = new string[0],
                Abi = new ContractAbi()
                {
                    Hash    = Hash,
                    Events  = System.Array.Empty <ContractEventDescriptor>(),
                    Methods = descriptors.ToArray()
                },
                Permissions = new[] { ContractPermission.DefaultPermission },
                Trusts      = WildcardContainer <UInt160> .Create(),
                SafeMethods = WildcardContainer <string> .Create(safeMethods.ToArray()),
                Extra       = null
            };
            contractsList.Add(this);
            contractsNameDictionary.Add(Name, this);
            contractsHashDictionary.Add(Hash, this);
        }
Example #4
0
        protected NativeContract()
        {
            this.ServiceHash = ServiceName.ToInteropMethodHash();
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall(ServiceHash);
                this.Script = sb.ToArray();
            }
            this.Hash     = Script.ToScriptHash();
            this.Manifest = ContractManifest.CreateDefault(this.Hash);
            List <ContractMethodDescriptor> descriptors = new List <ContractMethodDescriptor>();
            List <string> safeMethods = new List <string>();

            foreach (MethodInfo method in GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                ContractMethodAttribute attribute = method.GetCustomAttribute <ContractMethodAttribute>();
                if (attribute is null)
                {
                    continue;
                }
                string name = attribute.Name ?? (method.Name.ToLower()[0] + method.Name.Substring(1));
                descriptors.Add(new ContractMethodDescriptor
                {
                    Name       = name,
                    ReturnType = attribute.ReturnType,
                    Parameters = attribute.ParameterTypes.Zip(attribute.ParameterNames, (t, n) => new ContractParameterDefinition {
                        Type = t, Name = n
                    }).ToArray()
                });
                if (attribute.SafeMethod)
                {
                    safeMethods.Add(name);
                }
                methods.Add(name, new ContractMethodMetadata
                {
                    Delegate        = (Func <ApplicationEngine, VMArray, StackItem>)method.CreateDelegate(typeof(Func <ApplicationEngine, VMArray, StackItem>), this),
                    Price           = attribute.Price,
                    AllowedTriggers = attribute.AllowedTriggers
                });
            }
            this.Manifest.Abi.Methods = descriptors.ToArray();
            this.Manifest.SafeMethods = WildCardContainer <string> .Create(safeMethods.ToArray());

            contracts.Add(this);
        }
Example #5
0
 public ContractMethodMetadata(MemberInfo member, ContractMethodAttribute attribute)
 {
     this.Name    = attribute.Name ?? member.Name.ToLower()[0] + member.Name[1..];
Example #6
0
        protected NativeContract()
        {
            this.Id = --id_counter;
            byte[] script;
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitPush(Id);
                sb.EmitSysCall(ApplicationEngine.System_Contract_CallNative);
                script = sb.ToArray();
            }
            this.Nef = new NefFile
            {
                Compiler = nameof(ScriptBuilder),
                Version  = "3.0",
                Tokens   = System.Array.Empty <MethodToken>(),
                Script   = script
            };
            this.Nef.CheckSum = NefFile.ComputeChecksum(Nef);
            this.Hash         = Helper.GetContractHash(UInt160.Zero, script);
            List <ContractMethodDescriptor> descriptors = new List <ContractMethodDescriptor>();

            foreach (MemberInfo member in GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                ContractMethodAttribute attribute = member.GetCustomAttribute <ContractMethodAttribute>();
                if (attribute is null)
                {
                    continue;
                }
                ContractMethodMetadata metadata = new ContractMethodMetadata(member, attribute);
                descriptors.Add(new ContractMethodDescriptor
                {
                    Name       = metadata.Name,
                    ReturnType = ToParameterType(metadata.Handler.ReturnType),
                    Parameters = metadata.Parameters.Select(p => new ContractParameterDefinition {
                        Type = ToParameterType(p.Type), Name = p.Name
                    }).ToArray(),
                    Safe = (attribute.RequiredCallFlags & ~CallFlags.ReadOnly) == 0
                });
                methods.Add(metadata.Name, metadata);
            }
            this.Manifest = new ContractManifest
            {
                Name               = Name,
                Groups             = System.Array.Empty <ContractGroup>(),
                SupportedStandards = new string[0],
                Abi = new ContractAbi()
                {
                    Events  = System.Array.Empty <ContractEventDescriptor>(),
                    Methods = descriptors.ToArray()
                },
                Permissions = new[] { ContractPermission.DefaultPermission },
                Trusts      = WildcardContainer <UInt160> .Create(),
                Extra       = null
            };
            if (ProtocolSettings.Default.NativeActivations.TryGetValue(Name, out uint activationIndex))
            {
                this.ActiveBlockIndex = activationIndex;
            }
            contractsList.Add(this);
            contractsIdDictionary.Add(Id, this);
            contractsHashDictionary.Add(Hash, this);
        }