コード例 #1
0
ファイル: Mnemonic.cs プロジェクト: woutersmit/NBitcoin
		/// <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);
		}
コード例 #2
0
		// 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);
			}
		}
コード例 #3
0
ファイル: BitWriter.cs プロジェクト: woutersmit/NBitcoin
		public BitWriter ToWriter()
		{
			var writer = new BitWriter();
			writer.Write(array);
			return writer;
		}
コード例 #4
0
ファイル: BitWriter.cs プロジェクト: woutersmit/NBitcoin
		public BitReader(byte[] data, int bitCount)
		{
			BitWriter writer = new BitWriter();
			writer.Write(data, bitCount);
			array = writer.ToBitArray();
		}
コード例 #5
0
		public PartialMerkleTree(uint256[] vTxid, bool[] vMatch)
		{
			if(vMatch.Length != vTxid.Length)
				throw new ArgumentException("The size of the array of txid and matches is different");
			TransactionCount = (uint)vTxid.Length;

			MerkleNode root = MerkleNode.GetRoot(vTxid);
			BitWriter flags = new BitWriter();

			MarkNodes(root, vMatch);
			BuildCore(root, flags);

			Flags = flags.ToBitArray();
		}
コード例 #6
0
		/// <summary>
		/// Remove superflous branches
		/// </summary>
		/// <param name="transaction"></param>
		/// <returns></returns>
		public PartialMerkleTree Trim(params uint256[] matchedTransactions)
		{
			PartialMerkleTree trimmed = new PartialMerkleTree();
			trimmed.TransactionCount = TransactionCount;
			var root = GetMerkleRoot();
			foreach(var leaf in root.GetLeafs())
			{
				MarkToTop(leaf, false);
			}
			BitWriter flags = new BitWriter();
			foreach(var leaf in root.GetLeafs().Where(l => matchedTransactions.Contains(l.Hash)))
			{
				MarkToTop(leaf, true);
			}
			trimmed.BuildCore(root, flags);
			trimmed.Flags = flags.ToBitArray();
			return trimmed;
		}
コード例 #7
0
		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);
			}
		}