public override bool Execute(TransactionResultCapsule result)
        {
            long fee = CalcFee();
            AccountPermissionUpdateContract apu_contract = null;

            try
            {
                apu_contract = contract.Unpack <AccountPermissionUpdateContract>();
                byte[] owner_address = apu_contract.OwnerAddress.ToByteArray();

                AccountCapsule account = this.db_manager.Account.Get(owner_address);
                account.UpdatePermissions(apu_contract.Owner, apu_contract.Witness, new List <Permission>(apu_contract.Actives));
                this.db_manager.Account.Put(owner_address, account);

                this.db_manager.AdjustBalance(owner_address, -fee);
                this.db_manager.AdjustBalance(this.db_manager.Account.GetBlackHole().CreateDatabaseKey(), fee);

                result.SetStatus(fee, code.Sucess);
            }
            catch (BalanceInsufficientException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }
            catch (InvalidProtocolBufferException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }

            return(true);
        }
Example #2
0
        public static RpcApiResult CreateAccountPermissionUpdateContract(byte[] owner_address,
                                                                         string permission_json,
                                                                         out AccountPermissionUpdateContract contract)
        {
            try
            {
                contract = new AccountPermissionUpdateContract();
                JObject json = JObject.Parse(permission_json);

                if (json.TryGetValue("owner_permission", out JToken output))
                {
                    contract.Owner = JsonToPermission(output as JObject);
                }

                if (json.TryGetValue("witness_permission", out output))
                {
                    contract.Witness = JsonToPermission(output as JObject);
                }

                if (json.TryGetValue("active_permission", out output))
                {
                    List <Permission> permissions = new List <Permission>();
                    foreach (JToken permission in output as JArray)
                    {
                        contract.Actives.Add(JsonToPermission(permission as JObject));
                    }
                }

                contract.OwnerAddress = ByteString.CopyFrom(owner_address);
            }
            catch (System.Exception e)
            {
                throw e;
            }

            return(RpcApiResult.Success);
        }
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }

            if (this.db_manager == null)
            {
                throw new ContractValidateException("No this.db_manager!");
            }

            if (this.db_manager.DynamicProperties.GetAllowMultiSign() != 1)
            {
                throw new ContractValidateException(
                          "multi sign is not allowed, need to be opened by the committee");
            }
            if (!this.contract.Is(AccountPermissionUpdateContract.Descriptor))
            {
                AccountPermissionUpdateContract apu_contract = null;

                try
                {
                    apu_contract = contract.Unpack <AccountPermissionUpdateContract>();
                }
                catch (InvalidProtocolBufferException e)
                {
                    Logger.Debug(e.Message);
                    throw new ContractValidateException(e.Message);
                }

                byte[] owner_address = apu_contract.OwnerAddress.ToByteArray();
                if (!Wallet.IsValidAddress(owner_address))
                {
                    throw new ContractValidateException("invalidate owner_address");
                }
                AccountCapsule accountCapsule = this.db_manager.Account.Get(owner_address);
                if (accountCapsule == null)
                {
                    throw new ContractValidateException("owner_address account does not exist");
                }

                if (apu_contract.Owner == null)
                {
                    throw new ContractValidateException("owner permission is missed");
                }

                if (accountCapsule.IsWitness)
                {
                    if (apu_contract.Witness == null)
                    {
                        throw new ContractValidateException("witness permission is missed");
                    }
                }
                else
                {
                    if (apu_contract.Witness != null)
                    {
                        throw new ContractValidateException("account isn't witness can't set witness permission");
                    }
                }

                if (apu_contract.Actives.Count == 0)
                {
                    throw new ContractValidateException("active permission is missed");
                }

                if (apu_contract.Actives.Count > 8)
                {
                    throw new ContractValidateException("active permission is too many");
                }

                Permission owner   = apu_contract.Owner;
                Permission witness = apu_contract.Witness;

                if (owner.Type != Permission.Types.PermissionType.Owner)
                {
                    throw new ContractValidateException("owner permission type is error");
                }

                if (!CheckPermission(owner))
                {
                    return(false);
                }
                if (accountCapsule.IsWitness)
                {
                    if (witness.Type != Permission.Types.PermissionType.Witness)
                    {
                        throw new ContractValidateException("witness permission type is error");
                    }
                    if (!CheckPermission(witness))
                    {
                        return(false);
                    }
                }
                foreach (Permission permission in apu_contract.Actives)
                {
                    if (permission.Type != Permission.Types.PermissionType.Active)
                    {
                        throw new ContractValidateException("active permission type is error");
                    }
                    if (!CheckPermission(permission))
                    {
                        return(false);
                    }
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [AccountPermissionUpdateContract],real type[" + contract.GetType().Name + "]");
            }

            return(true);
        }