Beispiel #1
0
        public static byte[] Base58ToAddress(string base58)
        {
            if (base58.IsNullOrEmpty())
            {
                Logger.Warning("Base58 string value is null.");
            }


            byte[] decode = Base58.Decode(base58);
            if (decode.Length <= 4)
            {
                return(null);
            }

            byte[] address = new byte[decode.Length - 4];
            Array.Copy(decode, 0, address, 0, address.Length);

            byte[] hash0 = SHA256Hash.ToHash(address);
            byte[] hash1 = SHA256Hash.ToHash(hash0);

            if (hash1[0] == decode[address.Length] &&
                hash1[1] == decode[address.Length + 1] &&
                hash1[2] == decode[address.Length + 2] &&
                hash1[3] == decode[address.Length + 3])
            {
                return(IsValidAddress(address) ? address : null);
            }

            return(null);
        }
            public override KeyValuePair <bool, byte[]> Execute(byte[] data)
            {
                if (data == null)
                {
                    return(new KeyValuePair <bool, byte[]>(true, SHA256Hash.ToHash(new byte[0])));
                }

                return(new KeyValuePair <bool, byte[]>(true, SHA256Hash.ToHash(data)));
            }
Beispiel #3
0
        public static string Encode58Check(byte[] input)
        {
            byte[] hash0 = SHA256Hash.ToHash(input);
            byte[] hash1 = SHA256Hash.ToHash(hash0);
            byte[] check = new byte[input.Length + 4];

            Array.Copy(input, 0, check, 0, input.Length);
            Array.Copy(hash1, 0, check, input.Length, 4);

            return(Base58.Encode(check));
        }
 public override KeyValuePair <bool, byte[]> Execute(byte[] data)
 {
     byte[] target = new byte[20];
     if (data == null)
     {
         data = new byte[0];
     }
     byte[] orig = SHA256Hash.ToHash(data);
     Array.Copy(orig, 0, target, 0, 20);
     return(new KeyValuePair <bool, byte[]>(true, SHA256Hash.ToHash(target)));
 }
Beispiel #5
0
        public static TransactionSignWeight GetTransactionSignWeight(Transaction transaction)
        {
            TransactionSignWeight weight    = new TransactionSignWeight();
            TransactionExtention  extention = new TransactionExtention();

            weight.Result    = new TransactionSignWeight.Types.Result();
            extention.Result = new Return();

            extention.Transaction   = transaction;
            extention.Txid          = ByteString.CopyFrom(SHA256Hash.ToHash(transaction.RawData.ToByteArray()));
            extention.Result.Result = true;
            extention.Result.Code   = Return.Types.response_code.Success;

            weight.Transaction = extention;

            try
            {
                Contract contract = transaction.RawData.Contract[0];
                byte[]   owner    = TransactionCapsule.GetOwner(contract);

                AccountCapsule account = Manager.Instance.DBManager.Account.Get(owner);
                if (account == null)
                {
                    throw new PermissionException("Account is not exist!");
                }

                int        permission_id = contract.PermissionId;
                Permission permission    = account.GetPermissionById(permission_id);
                if (permission == null)
                {
                    throw new PermissionException("permission isn't exit");
                }

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

                    if (!CheckPermissionOprations(permission, contract))
                    {
                        throw new PermissionException("Permission denied");
                    }
                }

                weight.Permission = permission;
                if (transaction.Signature.Count > 0)
                {
                    List <ByteString> approves = new List <ByteString>();

                    weight.ApprovedList.AddRange(approves);
                    weight.CurrentWeight = TransactionCapsule.CheckWeight(permission,
                                                                          new List <ByteString>(transaction.Signature),
                                                                          SHA256Hash.ToHash(transaction.RawData.ToByteArray()),
                                                                          approves);
                }

                if (weight.CurrentWeight >= permission.Threshold)
                {
                    weight.Result.Code = TransactionSignWeight.Types.Result.Types.response_code.EnoughPermission;
                }
                else
                {
                    weight.Result.Code = TransactionSignWeight.Types.Result.Types.response_code.NotEnoughPermission;
                }
            }
            catch (SignatureFormatException e)
            {
                weight.Result.Code    = TransactionSignWeight.Types.Result.Types.response_code.SignatureFormatError;
                weight.Result.Message = e.Message;
            }
            catch (SignatureException e)
            {
                weight.Result.Code    = TransactionSignWeight.Types.Result.Types.response_code.ComputeAddressError;
                weight.Result.Message = e.Message;
            }
            catch (PermissionException e)
            {
                weight.Result.Code    = TransactionSignWeight.Types.Result.Types.response_code.PermissionError;
                weight.Result.Message = e.Message;
            }
            catch (System.Exception e)
            {
                weight.Result.Code    = TransactionSignWeight.Types.Result.Types.response_code.OtherError;
                weight.Result.Message = e.Message;
            }

            return(weight);
        }
Beispiel #6
0
        public static TransactionApprovedList GetTransactionApprovedList(Transaction transaction)
        {
            TransactionExtention transaction_extention = new TransactionExtention()
            {
                Transaction = transaction,
                Txid        = ByteString.CopyFrom(SHA256Hash.ToHash(transaction.RawData.ToByteArray())),
                Result      = new Return()
                {
                    Result = true,
                    Code   = Return.Types.response_code.Success
                }
            };

            TransactionApprovedList approved = new TransactionApprovedList()
            {
                Transaction = transaction_extention
            };

            try
            {
                Contract       contract      = transaction.RawData.Contract[0];
                byte[]         owner_address = TransactionCapsule.GetOwner(contract);
                AccountCapsule account       = Manager.Instance.DBManager.Account.Get(owner_address);
                if (account == null)
                {
                    throw new PermissionException("Account is not exist.");
                }

                if (transaction.Signature.Count > 0)
                {
                    byte[] hash = SHA256Hash.ToHash(transaction.RawData.ToByteArray());
                    foreach (var signature in transaction.Signature)
                    {
                        if (signature.Count() < 65)
                        {
                            throw new SignatureFormatException("Signature size is " + signature.Count());
                        }

                        byte[] signature_address = ECKey.SignatureToAddress(hash, ECDSASignature.ExtractECDSASignature(signature.ToByteArray()));
                        approved.ApprovedList.Add(ByteString.CopyFrom(signature_address));
                    }
                }
                approved.Result = new TransactionApprovedList.Types.Result()
                {
                    Code = TransactionApprovedList.Types.Result.Types.response_code.Success
                };
            }
            catch (SignatureFormatException e)
            {
                approved.Result = new TransactionApprovedList.Types.Result()
                {
                    Code    = TransactionApprovedList.Types.Result.Types.response_code.SignatureFormatError,
                    Message = e.Message
                };
            }
            catch (SignatureException e)
            {
                approved.Result = new TransactionApprovedList.Types.Result()
                {
                    Code    = TransactionApprovedList.Types.Result.Types.response_code.ComputeAddressError,
                    Message = e.Message
                };
            }
            catch (System.Exception e)
            {
                approved.Result = new TransactionApprovedList.Types.Result()
                {
                    Code    = TransactionApprovedList.Types.Result.Types.response_code.OtherError,
                    Message = e.Message
                };
            }

            return(approved);
        }