//creates a new wallet entry public string Wallet_CreateAddress() { //Use the Crypto provider to generate a new public + private key pair _cryptoProvider.GenerateKeyPair(); //export encoded string representations (usually base 64) of the public and private keys string privateKey = _cryptoProvider.ExportPrivateKey(); string publicKey = _cryptoProvider.ExportPublicKey(); //use the Address Encode mentods of the Cryto provider to create a BitCoin like address from the public key string address = CryptoProvider.AddressEncoder.CreateAddress(publicKey); //create the wallet entry object and add it to the wallet WalletLib.WalletEntry we = new WalletLib.WalletEntry(address, publicKey, privateKey, 0); _wallet.WalletEntries.Add(we); //return the new entry return(we.address); }
//selects the specified address from the wallet //Initializes the crypto provider with the public and private keys from the wallet public bool Wallet_SelectAddress(int index) { bool result = false; if (_wallet.WalletEntries.Count() > index) { //if the wallet entry was found at the specified index, //initialize the crypto provider instance with the public and private keys from the wallet WalletLib.WalletEntry we = _wallet.WalletEntries[index]; _cryptoProvider.ImportPublicKey(we.publicKey); _cryptoProvider.ImportPrivateKey(we.privateKey); } else { result = false; } return(result); }
//sends a Balance request to the BlockChain server //A list of adresses fromt he client wallet is sent public void Wallet_Balance() { var json_serializer = new JavaScriptSerializer(); //serialize the list of addresses from the client wallet List <string> addressList = _wallet.WalletEntries.Select(x => x.address).ToList(); string addresses = json_serializer.Serialize(addressList); RestClientLib.RestClient client = new RestClientLib.RestClient(); //send request to the BlockChain Server string url = rootUrl + "/balance"; string jsonResult = client.Post(url, addresses); //deserialize the list of balances returned List <Output> balances = json_serializer.Deserialize <List <Output> >(jsonResult); //update the wallet entry balance for each address returned foreach (Output o in balances) { WalletLib.WalletEntry we = _wallet.WalletEntries.Where(x => x.address == o.address).FirstOrDefault(); if (we != null) { if (o.amount < 0) { we.amount = 0; } else { we.amount = o.amount; } } } //save the wallet Wallet_Save(); }