public override KeyValuePair <bool, byte[]> Execute(byte[] data) { byte[] h = new byte[32]; byte[] v = new byte[32]; byte[] r = new byte[32]; byte[] s = new byte[32]; DataWord out_val = null; try { Array.Copy(data, 0, h, 0, 32); Array.Copy(data, 32, v, 0, 32); Array.Copy(data, 64, r, 0, 32); int length = data.Length < 128 ? data.Length - 96 : 32; Array.Copy(data, 96, s, 0, length); ECDSASignature signature = ECDSASignature.FromComponents(r, s, v[31]); if (ValidateV(v) && signature.IsValidComponents) { out_val = new DataWord(ECKey.SignatureToAddress(h, signature)); } } catch { } if (out_val == null) { return(new KeyValuePair <bool, byte[]>(true, new byte[0])); } else { return(new KeyValuePair <bool, byte[]>(true, out_val.Data)); } }
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); }