Example #1
0
        public void AddSignature(byte[] privatekey, AccountStore account_store)
        {
            Transaction.Types.Contract contract = this.transaction.RawData.Contract[0];

            byte[] owner         = GetOwner(contract);
            int    permission_id = contract.PermissionId;

            AccountCapsule account = account_store.Get(owner);

            if (account == null)
            {
                throw new PermissionException("Account is not exist.");
            }

            Permission permission = account.GetPermissionById(permission_id);

            if (permission == null)
            {
                throw new PermissionException("Permission is not exist");
            }

            if (permission_id != 0)
            {
                if (permission.Type != Permission.Types.PermissionType.Active)
                {
                    throw new PermissionException("Permission type is error");
                }
                if (Wallet.CheckPermissionOperations(permission, contract))
                {
                    throw new PermissionException("Invalid permission");
                }
            }

            List <ByteString> approves = new List <ByteString>();
            ECKey             ec_key   = ECKey.FromPrivateKey(privatekey);

            byte[] address = Wallet.PublickKeyToAddress(ec_key.PublicKey);

            if (this.transaction.Signature.Count > 0)
            {
                CheckWeight(permission, new List <ByteString>(this.transaction.Signature), this.GetRawHash().Hash, approves);
                if (approves.Contains(ByteString.CopyFrom(address)))
                {
                    throw new PermissionException(Wallet.AddressToBase58(address) + "had signed!");
                }
            }

            long weight = GetWeight(permission, address);

            if (weight == 0)
            {
                throw new PermissionException(
                          privatekey.ToHexString() + " address is " +
                          Wallet.AddressToBase58(address) + "but it is not contained of permission.");
            }

            ECDSASignature signature = ec_key.Sign(this.GetRawHash().Hash);

            this.transaction.Signature.Add(ByteString.CopyFrom(signature.ToByteArray()));
        }
Example #2
0
        public static bool ValidateSignature(Transaction tx, byte[] hash, DatabaseManager db_manager)
        {
            Permission   permission    = null;
            AccountStore account_store = db_manager.Account;

            Transaction.Types.Contract contract = tx.RawData.Contract?[0];

            int permission_id = contract.PermissionId;

            byte[]         owner   = GetOwner(contract);
            AccountCapsule account = account_store.Get(owner);

            if (account == null)
            {
                if (permission_id == 0)
                {
                    permission = AccountCapsule.GetDefaultPermission(ByteString.CopyFrom(owner));
                }
                else if (permission_id == 2)
                {
                    permission = AccountCapsule.CreateDefaultActivePermission(ByteString.CopyFrom(owner), db_manager);
                }
            }
            else
            {
                permission = account.GetPermissionById(permission_id);
            }

            if (permission == null)
            {
                throw new PermissionException("Permission is not exist");
            }

            if (permission_id != 0)
            {
                if (permission.Type != Permission.Types.PermissionType.Active)
                {
                    throw new PermissionException("Permission type is error");
                }

                if (!Wallet.CheckPermissionOperations(permission, contract))
                {
                    throw new PermissionException("Invalid Permission");
                }
            }

            return(CheckWeight(permission, new List <ByteString>(tx.Signature), hash, null) >= permission.Threshold);
        }