Example #1
0
		public void AddressEquality() {
			Address address1 = new Address("1JArS6jzE3AJ9sZ3aFij1BmTcpFGgN86hA");
			Address address2 = new Address("1JArS6jzE3AJ9sZ3aFij1BmTcpFGgN86hA");
			Address address3 = new Address("1VayNert3x1KzbpzMGt2qdqrAThiRovi8");
			Address address4 = new Address("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i");

			Assert.IsTrue(address1.Equals(address2));
			Assert.IsTrue(address1 == address2);
			Assert.IsFalse(address1 != address2);

			Assert.IsFalse(address1.Equals(address3));
			Assert.IsFalse(address1 == address3);
			Assert.IsTrue(address1 != address3);

			Assert.IsFalse(address1.Equals(address4));
			Assert.IsFalse(address1 == address4);
			Assert.IsTrue(address1 != address4);

			Assert.IsFalse(address2.Equals(address3));
			Assert.IsFalse(address2 == address3);
			Assert.IsTrue(address2 != address3);

			Assert.IsFalse(address2.Equals(address4));
			Assert.IsFalse(address2 == address4);
			Assert.IsTrue(address2 != address4);

			Assert.IsFalse(address3.Equals(address4));
			Assert.IsFalse(address3 == address4);
			Assert.IsTrue(address3 != address4);
		}
		static string GetHashedAddress(Address address) {
			byte[] currentHash = Base58.DecodeWithChecksum(address.ID);
			for(int i = 0; i < 1024; i++) {
				currentHash = SHA256.Hash(currentHash);
			}

			return Base58.Encode(currentHash);
		}
		public override async Task<Money> GetAddressBalanceAsync(Address address, ulong startHeight = 0) {
			PathInfo balanceCache = DefaultCachePath.SubPath("balances");
			FileInfo cache = FileInfo.Create(balanceCache, String.Format("{0}.{1}", GetHashedAddress(address), "bal"));

			Money balance = await base.GetAddressBalanceAsync(address, startHeight);
			using(var writer = new StreamWriter(FileStream.Create(cache, FileMode.Create))) {
				await writer.WriteLineAsync(balance.Cents.ToString());
			}
			return balance;
		}
		public override async Task<Money> GetCachedBalanceAsync(Address address, ulong startHeight = 0) {
			PathInfo balanceCache = DefaultCachePath.SubPath("balances");
			FileInfo cache = FileInfo.Create(balanceCache, String.Format("{0}.{1}", GetHashedAddress(address), "bal"));


			if(cache.Exists) {
				using(var stream = FileStream.Create(cache, FileMode.Open)) {
					using(var reader = new StreamReader(stream)) {
						return new Money(Int64.Parse(await reader.ReadLineAsync()), "BTC");
					}
				}
			}

			return await base.GetCachedBalanceAsync(address, startHeight);
		}
Example #5
0
		public async Task RemoveWatchAddressAsync(Address address) {
			if(IsLocked) throw new LockedException();

			if(WatchAddresses.Contains(address))
				WatchAddresses.Remove(address);
			else
				throw new OperationException(String.Format("You aren't watching the address '{0}'", address));

			await SaveAsync();
		}
Example #6
0
		public async Task RemoveAddressAsync(Address address) {
			if(IsLocked) throw new LockedException();

			if(Addresses.Contains(address)) {
				if(PrivateKeys.Contains(address)) {
					await privateKeyLock.WaitAsync();
					try {
						PrivateKeys.Remove(address);
					}
					finally {
						privateKeyLock.Release();
					}
				}
				if(PublicAddresses.Contains(address)) {
					PublicAddresses.Remove(address);
				}

				await SaveAsync();
			}
			else {
				throw new OperationException(String.Format("Your wallet doesn't contain the address {0}", address));
			}
		}
Example #7
0
		public async Task<Transaction> CreateTransactionAsync(Dictionary<Address, Money> destinations, Address changeAddress = null) {
			Money change;
			List<Transaction.Output> inpoints = CoinPicker.SelectInpoints(await GetSpendableOutputsAsync(), destinations, out change);
			var destinationsWithChange = new Dictionary<Address, Money>(destinations);

			if(change > new Money(0, "BTC")) {
				changeAddress = changeAddress ?? (await GenerateAddressAsync("Change")).Address;
				destinationsWithChange.Add(changeAddress, change);
			}

			Transaction tx = Transaction.Create(inpoints, destinationsWithChange, PrivateKeys.ToDictionary(k => k.Address, k => k.PrivateKey));
			if(!tx.IncludesStandardFee)
				throw new OperationException("Transaction generated without proper fee!");

			return tx;
		}
Example #8
0
		public async Task SetLabelAsync(Address address, string label, bool isPublic = true) {
			if(IsLocked)
				throw new LockedException();

			await privateKeyLock.WaitAsync();
			try {
				bool wasUpdated = false;
				if(PrivateAddresses.Contains(address)) {
					PrivateKeys[address].Label = label;
					wasUpdated = true;
				}

				if(isPublic && PublicAddresses.Contains(address)) {
					PublicAddresses[address].Label = label;
					wasUpdated = true;
				}

				if(!wasUpdated)
					throw new OperationException(String.Format("Could not find address '{0}' in your wallet", address));
			}
			finally {
				privateKeyLock.Release();
			}
			SaveAsync();
		}
Example #9
0
		public async Task HideAddressAsync(Address address) {
			if(IsLocked)
				throw new LockedException();
			if(PrivateKeys.Contains(address) && PublicAddresses.Contains(address)) {
				PublicAddresses.Remove(address);
				await SaveAsync();
			}
			else {
				// TODO: Tailor this message for both the key not being present and the address already being private
				throw new OperationException(String.Format("Address '{0}' isnt public", address));
			}
		}
Example #10
0
		public async Task ImportWatchAddressAsync(Address address, string label = null) {
			await ImportWatchAddressAsync(new AddressDetails(address, label));
		}