public static string EncodeHex(this SignatureChain signatureChain) { var hex = signatureChain.Nonce.EncodeHex(); hex += signatureChain.DataSize.EncodeHex(); hex += signatureChain.BlockHash.EncodeHex(); hex += signatureChain.SourceId.EncodeHex(); hex += signatureChain.SourcePublicKey.EncodeHex(); hex += signatureChain.DestinationId.EncodeHex(); hex += signatureChain.DestinationPublicKey.EncodeHex(); return(hex); }
public static ClientMessage MakeOutboundMessage( Client client, IList <string> destinations, IList <byte[]> payloads, uint maxHoldingSeconds) { if (destinations == null || destinations.Count() == 0) { throw new InvalidArgumentException("No destination"); } if (payloads == null || payloads.Count() == 0) { throw new InvalidArgumentException("No payloads"); } if (payloads.Count() > 1 && payloads.Count() != destinations.Count()) { throw new InvalidArgumentException("Invalid payloads count"); } var signatureChainElement = new SignatureChainElement(); signatureChainElement.NextPublicKey = client.RemoteNode.Publickey.FromHexString(); var signatureChainElementHexEncoded = signatureChainElement.EncodeHex(); var signatureChain = new SignatureChain { Nonce = (uint)PseudoRandom.RandomInt(), SourceId = Address.AddressToId(client.Address).FromHexString(), SourcePublicKey = client.PublicKey.FromHexString() }; if (!string.IsNullOrWhiteSpace(client.SignatureChainBlockHash)) { signatureChain.BlockHash = client.SignatureChainBlockHash.FromHexString(); } var signatures = new List <byte[]>(); for (int i = 0; i < destinations.Count; i++) { signatureChain.DestinationId = Address.AddressToId(destinations[i]).FromHexString(); signatureChain.DestinationPublicKey = Address.AddressToPublicKey(destinations[i]).FromHexString(); if (payloads.Count > 1) { signatureChain.DataSize = (uint)payloads[i].Length; } else { signatureChain.DataSize = (uint)payloads[0].Length; } var hex = signatureChain.EncodeHex(); var digest = Hash.Sha256Hex(hex); digest = Hash.Sha256Hex(digest + signatureChainElementHexEncoded); var signature = client.Key.Sign(digest.FromHexString()); signatures.Add(signature); } var message = new OutboundMessage { Destinations = destinations.ToArray(), Payloads = payloads.ToArray(), MaxHoldingSeconds = maxHoldingSeconds, Nonce = signatureChain.Nonce, BlockHash = signatureChain.BlockHash, Signatures = signatures.ToArray() }; var compressionType = payloads.Count > 1 ? CompressionType.Zlib : CompressionType.None; var messageBytes = message.ToBytes(); return(MessageFactory.MakeClientMessage(ClientMessageType.OutboundMessage, messageBytes, compressionType)); }