public static long CheckWeight(Permission permission, List <ByteString> signature, byte[] hash, List <ByteString> approve_list) { long result = 0; if (signature.Count > permission.Keys.Count) { throw new PermissionException( "Signature count is" + signature.Count + "more than key counts of permission" + permission.Keys.Count); } Dictionary <ByteString, long> signature_weight = new Dictionary <ByteString, long>(); foreach (ByteString sign in signature) { if (sign.Length < 65) { throw new SignatureFormatException("Signature size is" + sign.Length); } ECKey ec_key = ECKey.RecoverFromSignature(ECDSASignature.ExtractECDSASignature(sign.ToByteArray()), hash, false); byte[] publickey = ec_key.PublicKey; byte[] address = Wallet.PublickKeyToAddress(ec_key.PublicKey); long weight = GetWeight(permission, address); if (weight == 0) { throw new PermissionException( sign.ToByteArray().ToHexString() + "is signed by" + Wallet.AddressToBase58(address) + "but it is not contained of permission."); } if (signature_weight.ContainsKey(sign)) { throw new PermissionException(Wallet.AddressToBase58(address) + " has signed twice"); } signature_weight.Add(sign, weight); if (approve_list != null) { approve_list.Add(ByteString.CopyFrom(publickey)); } result += weight; } return(result); }
public bool ValidateSignature(DatabaseManager db_manager) { try { ECDSASignature signature = ECDSASignature.ExtractECDSASignature(this.block.BlockHeader.WitnessSignature.ToByteArray()); byte[] signature_address = ECKey.SignatureToAddress(GetRawHash().Hash, signature); byte[] witness_address = this.block.BlockHeader.RawData.WitnessAddress.ToByteArray(); if (db_manager.DynamicProperties.GetAllowMultiSign() != 1) { return(signature_address.SequenceEqual(witness_address)); } else { byte[] witness_permission_address = db_manager.Account.Get(witness_address)?.GetWitnessPermissionAddress(); return(signature_address.SequenceEqual(witness_permission_address)); } } catch (System.Exception e) { throw new ValidateSignatureException(e.Message); } }
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); }