Beispiel #1
0
		/// <summary>
		/// Generate a mnemonic
		/// </summary>
		/// <param name="wordList"></param>
		/// <param name="entropy"></param>
		public Mnemonic(Wordlist wordList, byte[] entropy = null)
		{
			wordList = wordList ?? Wordlist.English;
			_WordList = wordList;
			if(entropy == null)
				entropy = RandomUtils.GetBytes(32);

			var i = Array.IndexOf(entArray, entropy.Length * 8);
			if(i == -1)
				throw new ArgumentException("The length for entropy should be : " + String.Join(",", entArray), "entropy");

			int cs = csArray[i];
			byte[] checksum = Hashes.SHA256(entropy);
			BitWriter entcsResult = new BitWriter();

			entcsResult.Write(entropy);
			entcsResult.Write(checksum, cs);
			_Indices = entcsResult.ToIntegers();
			_Words = _WordList.GetWords(_Indices);
			_Mnemonic = _WordList.GetSentence(_Indices);
		}
		// serialization implementation
		#region IBitcoinSerializable Members

		public void ReadWrite(BitcoinStream stream)
		{
			stream.ReadWrite(ref _TransactionCount);
			stream.ReadWrite(ref _Hashes);
			byte[] vBytes = null;
			if(!stream.Serializing)
			{
				stream.ReadWriteAsVarString(ref vBytes);
				BitWriter writer = new BitWriter();
				for(int p = 0 ; p < vBytes.Length * 8 ; p++)
					writer.Write((vBytes[p / 8] & (1 << (p % 8))) != 0);
				_Flags = writer.ToBitArray();
			}
			else
			{
				vBytes = new byte[(_Flags.Length + 7) / 8];
				for(int p = 0 ; p < _Flags.Length ; p++)
					vBytes[p / 8] |= (byte)(ToByte(_Flags.Get(p)) << (p % 8));
				stream.ReadWriteAsVarString(ref vBytes);
			}
		}
Beispiel #3
0
		public BitWriter ToWriter()
		{
			var writer = new BitWriter();
			writer.Write(array);
			return writer;
		}
Beispiel #4
0
		public BitReader(byte[] data, int bitCount)
		{
			BitWriter writer = new BitWriter();
			writer.Write(data, bitCount);
			array = writer.ToBitArray();
		}
		private void BuildCore(MerkleNode node, BitWriter flags)
		{
			if(node == null)
				return;
			flags.Write(node.IsMarked);
			if(node.IsLeaf || !node.IsMarked)
				Hashes.Add(node.Hash);

			if(node.IsMarked)
			{
				BuildCore(node.Left, flags);
				BuildCore(node.Right, flags);
			}
		}