Example #1
0
        public async Task WaitFor(ExtPubKey pubKey, CancellationToken cancellation = default(CancellationToken))
        {
            TaskCompletionSource <bool> completion = new TaskCompletionSource <bool>();

            var key = Hashes.Hash160(pubKey.ToBytes());

            lock (_WaitFor)
            {
                _WaitFor.Add(key, completion);
            }

            cancellation.Register(() =>
            {
                completion.TrySetCanceled();
            });

            try
            {
                await completion.Task;
            }
            finally
            {
                lock (_WaitFor)
                {
                    _WaitFor.Remove(key, completion);
                }
            }
        }
Example #2
0
        private void RunTest(TestVector test)
        {
            var       seed   = TestUtils.ParseHex(test.strHexMaster);
            ExtKey    key    = new ExtKey(seed);
            ExtPubKey pubkey = key.Neuter();

            foreach (TestDerivation derive in test.vDerive)
            {
                byte[] data = key.ToBytes();
                Assert.Equal(74, data.Length);
                data = pubkey.ToBytes();
                Assert.Equal(74, data.Length);
                // Test private key
                BitcoinExtKey b58key   = Network.Main.CreateBitcoinExtKey(key);
                var           a        = Encoders.Hex.EncodeData(Encoders.Base58Check.DecodeData(b58key.ToString()));
                var           expected = Encoders.Hex.EncodeData(Encoders.Base58Check.DecodeData(derive.prv));
                Assert.True(b58key.ToString() == derive.prv);
                // Test public key
                BitcoinExtPubKey b58pubkey = Network.Main.CreateBitcoinExtPubKey(pubkey);
                Assert.True(b58pubkey.ToString() == derive.pub);
                // Derive new keys
                ExtKey    keyNew    = key.Derive(derive.nChild);
                ExtPubKey pubkeyNew = keyNew.Neuter();
                if (!((derive.nChild & 0x80000000) != 0))
                {
                    // Compare with public derivation
                    ExtPubKey pubkeyNew2 = pubkey.Derive(derive.nChild);
                    Assert.True(pubkeyNew == pubkeyNew2);
                }
                key    = keyNew;
                pubkey = pubkeyNew;
            }
        }
Example #3
0
        public KeyInformation GetKeyInformation(ExtPubKey pubKey, Script script)
        {
            var info = GetKeyInformation(script);

            if (info == null || !pubKey.ToBytes().SequenceEqual(info.RootKey))
            {
                return(null);
            }
            return(info);
        }
Example #4
0
        public void CanCheckChildKey()
        {
            var    parent   = new ExtKey();
            ExtKey child    = parent.Derive(1);
            var    notchild = new ExtKey();

            Assert.True(child.IsChildOf(parent));
            Assert.True(parent.IsParentOf(child));
            Assert.False(notchild.IsChildOf(parent));
            Assert.False(parent.IsParentOf(notchild));

            Assert.True(child.Neuter().IsChildOf(parent.Neuter()));
            Assert.True(parent.Neuter().IsParentOf(child.Neuter()));
            Assert.False(notchild.Neuter().IsChildOf(parent.Neuter()));
            Assert.False(parent.Neuter().IsParentOf(notchild.Neuter()));

            ExtPubKey keyA = parent.Neuter();
            var       keyB = new ExtPubKey(keyA.ToBytes());

            AssertEx.CollectionEquals(keyA.ToBytes(), keyB.ToBytes());
        }
Example #5
0
        public void CleanTransactions(ExtPubKey pubkey, List <TrackedTransaction> cleanList)
        {
            if (cleanList == null || cleanList.Count == 0)
            {
                return;
            }
            var tableName = $"T-{Hashes.Hash160(pubkey.ToBytes()).ToString()}";

            using (var tx = _Engine.GetTransaction())
            {
                foreach (var tracked in cleanList)
                {
                    tx.RemoveKey(tableName, tracked.GetRowKey());
                }
                tx.Commit();
            }
        }
Example #6
0
        public void CreatePlatformKey(out byte[] key1, out byte[] key2)
        {
            var       userExtKey = new ExtKey();
            ExtPubKey pubkey     = userExtKey.Neuter();

            key1 = userExtKey.PrivateKey.ToBytes();
            key2 = pubkey.ToBytes();

            var userDerivationScheme = this.Network.DerivationStrategyFactory.CreateDirectDerivationStrategy(pubkey, new DerivationStrategyOptions()
            {
                // Use non-segwit
                Legacy = true
            });

            //set the key to NBXplorer server
            this.NbxClient.Track(userDerivationScheme);
        }
Example #7
0
        private void Notify(ExtPubKey pubkey, bool log)
        {
            if (log)
            {
                Logs.Explorer.LogInformation($"A wallet received money");
            }
            var key = Hashes.Hash160(pubkey.ToBytes());

            lock (_WaitFor)
            {
                IReadOnlyCollection <Completion> completions;
                if (_WaitFor.TryGetValue(key, out completions))
                {
                    foreach (var completion in completions.ToList())
                    {
                        completion.TrySetResult(true);
                    }
                }
            }
        }
Example #8
0
		public void CanCheckChildKey()
		{
			var parent = new ExtKey();
			var child = parent.Derive(1);
			var notchild = new ExtKey();

			Assert.True(child.IsChildOf(parent));
			Assert.True(parent.IsParentOf(child));
			Assert.False(notchild.IsChildOf(parent));
			Assert.False(parent.IsParentOf(notchild));

			Assert.True(child.Neuter().IsChildOf(parent.Neuter()));
			Assert.True(parent.Neuter().IsParentOf(child.Neuter()));
			Assert.False(notchild.Neuter().IsChildOf(parent.Neuter()));
			Assert.False(parent.Neuter().IsParentOf(notchild.Neuter()));

			var keyA = parent.Neuter();
			var keyB = new ExtPubKey(keyA.ToBytes());
			AssertEx.CollectionEquals(keyA.ToBytes(), keyB.ToBytes());
		}
Example #9
0
 public KeyInformation(ExtPubKey pubKey, KeyPath keyPath)
 {
     KeyPath = keyPath;
     RootKey = pubKey.ToBytes();
 }