public PostchainMessenger(GTXClient gtxClient, byte[] gtxPrivateKey, byte[] gtxPublicKey) { _gtxClient = gtxClient; _gtxPrivateKey = gtxPrivateKey; _gtxPublicKey = gtxPublicKey; _dhKeys = new Dictionary <string, AsymmetricCipherKeyPair>(); _dhParameters = new Dictionary <string, DHParameters>(); _dhSecretHashes = new Dictionary <string, byte[]>(); }
public static void Main() { var keyPair = Util.MakeKeyPair(); gtxPrivateKey = keyPair["privKey"]; gtxPublicKey = keyPair["pubKey"]; restClient = new RESTClient("http://localhost:7740", blockchainRIDEclipse); gtxClient = new GTXClient(restClient, blockchainRIDEclipse); Console.WriteLine("Usage:"); Console.WriteLine("/c:\tCreate a user"); Console.WriteLine("/sf:\tSend friend request to user"); Console.WriteLine("/af:\tAccept friend request from user"); Console.WriteLine("/s:\tSend message to user"); messenger = new PostchainMessenger(gtxClient, keyPair["privKey"], keyPair["pubKey"]); Task.Run(async() => { while (running) { if (userCreated) { List <string> newFriendRequests = await messenger.GetFriendRequests(); foreach (string friendRequestName in newFriendRequests) { Console.WriteLine("New friend request from " + friendRequestName); } List <Message> newMessages = await messenger.GetNewMessages(); foreach (Message message in newMessages) { Console.WriteLine("[" + message.timestamp + "] " + message.sender + ": " + message.content); } } await Task.Delay(100); } } ); while (running) { string input = Console.ReadLine(); string[] parameters = input.Split(' '); if (parameters.Length < 2) { continue; } switch (parameters[0]) { case "/c": { HandleCreateUser(parameters[1]); break; } case "/sf": { HandleSendFriendRequest(parameters[1]); break; } case "/af": { HandleAcceptFriendRequest(parameters[1]); break; } case "/s": { HandleSendMessage(parameters[1], parameters.Skip(2).Aggregate((acc, str) => acc += " " + str)); break; } } } }
public async void DemoTest() { var keyPair = PostchainUtil.MakeKeyPair(); var privKey = keyPair["privKey"]; var pubKey = keyPair["pubKey"]; // The lower-level client that can be used for any // postchain client messages. It only handles binary data. var rest = new RESTClient("http://localhost:7740/"); // Instead of updateing the BRID each time the Rell code is compiled, // the blockchain can now be accessed throug its chain_id from the // run.xml file (<chain name="MyCahin" iid="0">) var initResult = await rest.InitializeBRIDFromChainID(0); if (initResult.Error) { Console.WriteLine("DemoTest: Cannot connect to blockchain!"); return; } // Create an instance of the higher-level gtx client. It will // use the rest client instance var gtx = new GTXClient(rest); // Start a new request. A request instance is created. // The public keys are the keys that must sign the request // before sending it to postchain. Can be empty. var req = gtx.NewTransaction(new byte[][] { pubKey }); req.AddOperation("insert_city", "Hamburg", 22222); req.AddOperation("create_user", pubKey, "Peter"); // Since transactions with the same operations will result in the same txid, // transactions can contain "nop" operations. This is needed to satisfy // the unique txid constraint of the postchain. req.AddOperation("nop", new Random().Next()); req.Sign(privKey, pubKey); var result = await req.PostAndWaitConfirmation(); if (result.Error) { Console.WriteLine("DemoTest Operation failed: " + result.ErrorMessage); } // The expected return type has to be passed to the query function. This // also works with complex types (i.e. your own struct as well as lists). // The returned tuple will consist of (content, control). The content is of // the type you pass the function. The control struct contains an error flag // as well as the error message. var queryResult = await gtx.Query <int>("get_city", ("name", "Hamburg")); if (queryResult.control.Error) { Console.WriteLine("DemoTest city query error: " + queryResult.control.ErrorMessage); } else { int plz = queryResult.content; Console.WriteLine("DemoTest ZIP Query: " + plz); } // Same as above with the exception that byte arrays will be returned as strings. // To convert it to a byte array, use the util function Util.HexStringToBuffer() // in the Chromia.Postchain.Client.GTX namespace. var queryResult2 = await gtx.Query <string>("get_user_pubkey", ("name", "Peter")); if (queryResult2.control.Error) { Console.WriteLine("DemoTest userquery error: " + queryResult2.control.ErrorMessage); } else { string queryPubkeyString = queryResult2.content; byte[] queryPubkey = PostchainUtil.HexStringToBuffer(queryPubkeyString); Console.WriteLine("DemoTest User Query: " + PostchainUtil.ByteArrayToString(queryPubkey)); } }