public static byte[] ToAddress(Transaction.Types.Contract contract) { ByteString result = null; try { Any contract_parameter = contract.Parameter; switch (contract.Type) { case ContractType.TransferContract: result = contract_parameter.Unpack <TransferContract>().ToAddress; break; case ContractType.TransferAssetContract: result = contract_parameter.Unpack <TransferAssetContract>().ToAddress; break; case ContractType.ParticipateAssetIssueContract: result = contract_parameter.Unpack <ParticipateAssetIssueContract>().ToAddress; break; } } catch (System.Exception e) { Logger.Error(e.Message); } return(result?.ToByteArray()); }
public static long GetCallTokenValue(Transaction.Types.Contract contract) { long result = 0; try { Any contract_parameter = contract.Parameter; switch (contract.Type) { case ContractType.TriggerSmartContract: result = contract_parameter.Unpack <TriggerSmartContract>().CallTokenValue; break; case ContractType.CreateSmartContract: result = contract_parameter.Unpack <CreateSmartContract>().CallTokenValue; break; } } catch (System.Exception e) { Logger.Error(e.Message); } return(result); }
private Transaction CreateTransaction(Wallet.WalletClient wallet, byte[] fromBytes, byte[] toBytes, long amount) { Transaction transactionBuilder = new Transaction(); Block newestBlock = wallet.GetNowBlockAsync(GetEmptyMessage).GetAwaiter().GetResult(); Transaction.Types.Contract contractBuilder = new Transaction.Types.Contract(); TransferContract transferContractBuilder = new TransferContract(); transferContractBuilder.Amount = amount; transferContractBuilder.ToAddress = ByteString.CopyFrom(toBytes); transferContractBuilder.OwnerAddress = ByteString.CopyFrom(fromBytes); try { Any any = Any.Pack(transferContractBuilder.Clone()); contractBuilder.Parameter = any; } catch (Exception) { return(null); } contractBuilder.Type = Transaction.Types.Contract.Types.ContractType.TransferContract; transactionBuilder.RawData = new Transaction.Types.raw(); transactionBuilder.RawData.Contract.Add(contractBuilder); transactionBuilder.RawData.Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); transactionBuilder.RawData.Expiration = newestBlock.BlockHeader.RawData.Timestamp + 10 * 60 * 60 * 1000; Transaction transaction = transactionBuilder.Clone(); var refTransaction = SetReference(transaction, newestBlock); return(refTransaction); }
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())); }
public void Main4Test() { Wallet.WalletClient wallet = GetMainNetWalletClient; var keyTriple = new KeyTriple("B7E85CA5910922C49DCCB92EE48DFEC13A60674845CB7152C86B88F31DA6DC67"); byte[] hashTransaction1 = StringHelper.HexStringToByteArray("a7e974c6e69fb7741bf5e08de8a2d8f6617826c59422b440de22e0612b03c393"); BytesMessage bytesMessage1 = new BytesMessage(); bytesMessage1.Value = ByteString.CopyFrom(hashTransaction1); Transaction transactionLoad1 = wallet.GetTransactionByIdAsync(bytesMessage1).GetAwaiter().GetResult(); Transaction.Types.Contract contract = transactionLoad1.RawData.Contract[0]; TransferContract transferContract = TransferContract.Parser.ParseFrom(contract.Parameter.Value.ToByteArray()); byte[] publicKeyOwner = transferContract.OwnerAddress.ToByteArray(); string encode = Base58.Encode(publicKeyOwner); byte[] transactionRaw = transactionLoad1.RawData.ToByteArray(); byte[] hash = Sha256.Hash(transactionRaw); string signBase64 = transactionLoad1.Signature[0].ToBase64(); byte[] byteArray = transactionLoad1.Signature[0].ToByteArray(); bool isSignatureValidFromBytes = new ECSigner().IsSignatureValidFromBytes(hash, byteArray, publicKeyOwner); }
private async Task <TransactionExtention> CreateTransactionAsync(string from, string to, long amount) { var fromAddress = Base58Encoder.DecodeFromBase58Check(from); var toAddress = Base58Encoder.DecodeFromBase58Check(to); var transferContract = new TransferContract { OwnerAddress = ByteString.CopyFrom(fromAddress), ToAddress = ByteString.CopyFrom(toAddress), Amount = amount }; var transaction = new Transaction(); var contract = new Transaction.Types.Contract(); try { contract.Parameter = Google.Protobuf.WellKnownTypes.Any.Pack(transferContract); } catch (Exception) { return(new TransactionExtention { Result = new Return { Result = false, Code = Return.Types.response_code.OtherError }, }); } var newestBlock = await _wallet.GetNowBlock2Async(new EmptyMessage()); contract.Type = Transaction.Types.Contract.Types.ContractType.TransferContract; transaction.RawData = new Transaction.Types.raw(); transaction.RawData.Contract.Add(contract); transaction.RawData.Timestamp = DateTime.Now.Ticks; transaction.RawData.Expiration = newestBlock.BlockHeader.RawData.Timestamp + 10 * 60 * 60 * 1000; var blockHeight = newestBlock.BlockHeader.RawData.Number; var blockHash = Sha256Sm3Hash.Of(newestBlock.BlockHeader.RawData.ToByteArray()).GetBytes(); var bb = ByteBuffer.Allocate(8); bb.PutLong(blockHeight); var refBlockNum = bb.ToArray(); transaction.RawData.RefBlockHash = ByteString.CopyFrom(blockHash.SubArray(8, 8)); transaction.RawData.RefBlockBytes = ByteString.CopyFrom(refBlockNum.SubArray(6, 2)); var transactionExtension = new TransactionExtention { Transaction = transaction, Txid = ByteString.CopyFromUtf8(transaction.GetTxid()), Result = new Return { Result = true, Code = Return.Types.response_code.Success }, }; return(transactionExtension); }
public static bool CheckPermissionOperations(Permission permission, Transaction.Types.Contract contract) { ByteString operations = permission.Operations; if (operations.Length != 32) { throw new PermissionException("Operations size must 32 bytes"); } int contract_type = (int)contract.Type; return((operations[(int)(contract_type / 8)] & (1 << (contract_type % 8))) != 0); }
public void ValidateTransactionTest() { Wallet.WalletClient wallet = GetMainNetWalletClient; byte[] hashTransaction1 = StringHelper.HexStringToByteArray("a7e974c6e69fb7741bf5e08de8a2d8f6617826c59422b440de22e0612b03c393"); BytesMessage bytesMessage1 = new BytesMessage(); bytesMessage1.Value = ByteString.CopyFrom(hashTransaction1); Transaction signedTransaction = wallet.GetTransactionByIdAsync(bytesMessage1).GetAwaiter().GetResult(); Assert.IsTrue(signedTransaction.Signature.Count == signedTransaction.RawData.Contract.Count); RepeatedField <Transaction.Types.Contract> listContract = signedTransaction.RawData.Contract; byte[] hash = Sha256.Hash(signedTransaction.RawData.ToByteArray()); int count = signedTransaction.Signature.Count; if (count == 0) { Assert.Fail(); } for (int i = 0; i < count; ++i) { try { Transaction.Types.Contract contract = listContract[i]; TransferContract transferContract = TransferContract.Parser.ParseFrom(signedTransaction.RawData.Contract[i].Parameter.Value.ToByteArray()); byte[] owner = transferContract.OwnerAddress.ToByteArray(); signedTransaction.Signature[i].ToByteArray(); string signatureBase64 = Base64.ToBase64String(signedTransaction.Signature[i].ToByteArray()); byte[] address = signatureToAddress(hash, signatureBase64); if (!Arrays.AreEqual(owner, address)) { Assert.Fail(); } } catch (Exception e) { e.ToString(); Assert.Fail(); } } Assert.IsTrue(true); }
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); }
public static Transaction createTransaction(Wallet.WalletClient wallet, byte[] from, byte[] to, long amount) { Transaction transactionBuilder = new Transaction(); transactionBuilder.RawData = new Transaction.Types.raw(); Block newestBlock = wallet.GetNowBlockAsync(new EmptyMessage()).GetAwaiter().GetResult(); TransferContract transferContract = new TransferContract { Amount = amount, OwnerAddress = ByteString.CopyFrom(@from), ToAddress = ByteString.CopyFrom(to) }; // Transaction transactionNet = wallet.CreateTransactionAsync(transferContract).GetAwaiter().GetResult(); Transaction.Types.Contract contract = new Transaction.Types.Contract(); try { Any any = Any.Pack(transferContract); contract.Parameter = any; } catch (Exception) { return(null); } contract.Type = Transaction.Types.Contract.Types.ContractType.TransferContract; transactionBuilder.RawData.Contract.Add(contract); transactionBuilder.RawData.Timestamp = DateTime.Now.Ticks; transactionBuilder.RawData.Expiration = newestBlock.BlockHeader.RawData.Timestamp + 10 * 60 * 60 * 1000; Transaction refTransaction = setReference(transactionBuilder, newestBlock); return(refTransaction); }
public static void ValidContractProto(Transaction.Types.Contract contract) { System.Type type = null; Any contract_parameter = contract.Parameter; Google.Protobuf.IMessage src = null; Google.Protobuf.IMessage contract_message = null; switch (contract.Type) { case ContractType.AccountCreateContract: src = contract_parameter.Unpack <AccountCreateContract>(); contract_message = Parse <AccountCreateContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.TransferContract: src = contract_parameter.Unpack <TransferContract>(); contract_message = Parse <TransferContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.TransferAssetContract: src = contract_parameter.Unpack <TransferAssetContract>(); contract_message = Parse <TransferAssetContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.VoteAssetContract: src = contract_parameter.Unpack <VoteAssetContract>(); contract_message = Parse <VoteAssetContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.VoteWitnessContract: src = contract_parameter.Unpack <VoteWitnessContract>(); contract_message = Parse <VoteWitnessContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.WitnessCreateContract: src = contract_parameter.Unpack <WitnessCreateContract>(); contract_message = Parse <WitnessCreateContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.AssetIssueContract: src = contract_parameter.Unpack <AssetIssueContract>(); contract_message = Parse <AssetIssueContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.WitnessUpdateContract: src = contract_parameter.Unpack <WitnessUpdateContract>(); contract_message = Parse <WitnessUpdateContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.ParticipateAssetIssueContract: src = contract_parameter.Unpack <ParticipateAssetIssueContract>(); contract_message = Parse <ParticipateAssetIssueContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.AccountUpdateContract: src = contract_parameter.Unpack <AccountUpdateContract>(); contract_message = Parse <AccountUpdateContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.FreezeBalanceContract: src = contract_parameter.Unpack <FreezeBalanceContract>(); contract_message = Parse <FreezeBalanceContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.UnfreezeBalanceContract: src = contract_parameter.Unpack <UnfreezeBalanceContract>(); contract_message = Parse <UnfreezeBalanceContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.UnfreezeAssetContract: src = contract_parameter.Unpack <UnfreezeAssetContract>(); contract_message = Parse <UnfreezeAssetContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.WithdrawBalanceContract: src = contract_parameter.Unpack <WithdrawBalanceContract>(); contract_message = Parse <WithdrawBalanceContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.CreateSmartContract: src = contract_parameter.Unpack <CreateSmartContract>(); contract_message = Parse <CreateSmartContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.TriggerSmartContract: src = contract_parameter.Unpack <TriggerSmartContract>(); contract_message = Parse <TriggerSmartContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.UpdateAssetContract: src = contract_parameter.Unpack <UpdateAssetContract>(); contract_message = Parse <UpdateAssetContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.ProposalCreateContract: src = contract_parameter.Unpack <ProposalCreateContract>(); contract_message = Parse <ProposalCreateContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.ProposalApproveContract: src = contract_parameter.Unpack <ProposalApproveContract>(); contract_message = Parse <ProposalApproveContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.ProposalDeleteContract: src = contract_parameter.Unpack <ProposalDeleteContract>(); contract_message = Parse <ProposalDeleteContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.SetAccountIdContract: src = contract_parameter.Unpack <SetAccountIdContract>(); contract_message = Parse <SetAccountIdContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.UpdateSettingContract: src = contract_parameter.Unpack <UpdateSettingContract>(); contract_message = Parse <UpdateSettingContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.UpdateEnergyLimitContract: src = contract_parameter.Unpack <UpdateEnergyLimitContract>(); contract_message = Parse <UpdateEnergyLimitContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.ClearAbicontract: src = contract_parameter.Unpack <ClearABIContract>(); contract_message = Parse <ClearABIContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.ExchangeCreateContract: src = contract_parameter.Unpack <ExchangeCreateContract>(); contract_message = Parse <ExchangeCreateContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.ExchangeInjectContract: src = contract_parameter.Unpack <ExchangeInjectContract>(); contract_message = Parse <ExchangeInjectContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.ExchangeWithdrawContract: src = contract_parameter.Unpack <ExchangeWithdrawContract>(); contract_message = Parse <ExchangeWithdrawContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.ExchangeTransactionContract: src = contract_parameter.Unpack <ExchangeTransactionContract>(); contract_message = Parse <ExchangeTransactionContract>(Message.GetCodedInputStream(src.ToByteArray())); break; case ContractType.AccountPermissionUpdateContract: src = contract_parameter.Unpack <AccountPermissionUpdateContract>(); contract_message = Parse <AccountPermissionUpdateContract>(Message.GetCodedInputStream(src.ToByteArray())); break; default: break; } if (type == null) { throw new P2pException(P2pException.ErrorType.PROTOBUF_ERROR, P2pException.GetDescription(P2pException.ErrorType.PROTOBUF_ERROR)); } Message.CompareBytes(src.ToByteArray(), contract_message.ToByteArray()); }
public static byte[] GetOwner(Transaction.Types.Contract contract) { ByteString owner; try { Any contractParameter = contract.Parameter; switch (contract.Type) { case Transaction.Types.Contract.Types.ContractType.AccountCreateContract: owner = contractParameter.Unpack <AccountCreateContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.TransferContract: owner = contractParameter.Unpack <TransferContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.TransferAssetContract: owner = contractParameter.Unpack <TransferAssetContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.VoteAssetContract: owner = contractParameter.Unpack <VoteAssetContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.VoteWitnessContract: owner = contractParameter.Unpack <VoteWitnessContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.WitnessCreateContract: owner = contractParameter.Unpack <WitnessCreateContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.AssetIssueContract: owner = contractParameter.Unpack <AssetIssueContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.WitnessUpdateContract: owner = contractParameter.Unpack <WitnessUpdateContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.ParticipateAssetIssueContract: owner = contractParameter.Unpack <ParticipateAssetIssueContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.AccountUpdateContract: owner = contractParameter.Unpack <AccountUpdateContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.FreezeBalanceContract: owner = contractParameter.Unpack <FreezeBalanceContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.UnfreezeBalanceContract: owner = contractParameter.Unpack <UnfreezeBalanceContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.UnfreezeAssetContract: owner = contractParameter.Unpack <UnfreezeAssetContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.WithdrawBalanceContract: owner = contractParameter.Unpack <WithdrawBalanceContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.CreateSmartContract: owner = contractParameter.Unpack <CreateSmartContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.TriggerSmartContract: owner = contractParameter.Unpack <TriggerSmartContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.UpdateAssetContract: owner = contractParameter.Unpack <UpdateAssetContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.ProposalCreateContract: owner = contractParameter.Unpack <ProposalCreateContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.ProposalApproveContract: owner = contractParameter.Unpack <ProposalApproveContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.ProposalDeleteContract: owner = contractParameter.Unpack <ProposalDeleteContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.SetAccountIdContract: owner = contractParameter.Unpack <SetAccountIdContract>().OwnerAddress; break; //case Transaction.Types.Contract.Types.ContractType.BuyStorageContract: // owner = contractParameter.Unpack<BuyStorageContract>().OwnerAddress; // break; //case Transaction.Types.Contract.Types.ContractType.BuyStorageBytesContract: // owner = contractParameter.Unpack<BuyStorageBytesContract>().OwnerAddress; // break; //case Transaction.Types.Contract.Types.ContractType.SellStorageContract: // owner = contractParameter.Unpack<SellStorageContract>().OwnerAddress; // break; case Transaction.Types.Contract.Types.ContractType.UpdateSettingContract: owner = contractParameter.Unpack <UpdateSettingContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.UpdateEnergyLimitContract: owner = contractParameter.Unpack <UpdateEnergyLimitContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.ClearAbicontract: owner = contractParameter.Unpack <ClearABIContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.ExchangeCreateContract: owner = contractParameter.Unpack <ExchangeCreateContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.ExchangeInjectContract: owner = contractParameter.Unpack <ExchangeInjectContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.ExchangeWithdrawContract: owner = contractParameter.Unpack <ExchangeWithdrawContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.ExchangeTransactionContract: owner = contractParameter.Unpack <ExchangeTransactionContract>().OwnerAddress; break; case Transaction.Types.Contract.Types.ContractType.AccountPermissionUpdateContract: owner = contractParameter.Unpack <AccountPermissionUpdateContract>().OwnerAddress; break; default: return(null); } return(owner.ToByteArray()); } catch (System.Exception e) { Logger.Error(e.Message); return(null); } }