Ejemplo n.º 1
0
        /// <summary>
        /// Derive keyPaths as fast as possible using caching and parallelism
        /// </summary>
        /// <param name="hdkey">The hdKey to derive</param>
        /// <param name="keyPaths">keyPaths to derive</param>
        /// <returns>An array of keyPaths.Length size with the derived keys</returns>
        public static IHDKey[] Derive(this IHDKey hdkey, KeyPath[] keyPaths)
        {
            if (hdkey == null)
            {
                throw new ArgumentNullException(nameof(hdkey));
            }
            if (keyPaths == null)
            {
                throw new ArgumentNullException(nameof(keyPaths));
            }
            var result = new IHDKey[keyPaths.Length];
            var cache  = (HDKeyCache)hdkey.AsHDKeyCache();

#if !NOPARALLEL
            Parallel.For(0, keyPaths.Length, i =>
            {
                result[i] = hdkey.Derive(keyPaths[i]);
            });
#else
            for (int i = 0; i < keyPaths.Length; i++)
            {
                result[i] = hdkey.Derive(keyPaths[i]);
            }
#endif
            return(result);
        }
Ejemplo n.º 2
0
        public IHDKey Derive(uint index)
        {
            var childPath = _PathFromRoot.Derive(index);
            var key       = derivationCache.GetOrAdd(childPath, _ => hdKey.Derive(index));

            return(new HDKeyCache(key, childPath, derivationCache));
        }
        public void Setup()
        {
            var seed = new ExtKey();

            accPath = new KeyPath("87'/0'/0'").ToRootedKeyPath(seed.GetPublicKey().GetHDFingerPrint());
            acc     = seed.Derive(accPath.KeyPath).AsHDKeyCache();
            var coins = Enumerable
                        .Range(0, 1300)
                        .Select(i => new Coin(RandomOutpoint(), new TxOut(Money.Coins(1.0m), acc.Derive(0).Derive((uint)i).GetPublicKey().GetScriptPubKey(ScriptPubKeyType.Segwit))))
                        .ToArray();
            var tx = Transaction.Create(Network.Main);

            foreach (var c in coins)
            {
                tx.Inputs.Add(c.Outpoint);
            }
            tx.Outputs.Add(Money.Coins(1299.0m), new Key());
            var psbt = PSBT.FromTransaction(tx, Network.Main);

            psbt.AddCoins(coins);
            for (int i = 0; i < coins.Length; i++)
            {
                psbt.Inputs[i].AddKeyPath(acc.Derive(0).Derive((uint)i).GetPublicKey(), accPath.Derive(0).Derive((uint)i));
            }
            psbtStr = psbt.ToBase64();
            psbt.SignAll(acc.AsHDScriptPubKey(ScriptPubKeyType.Segwit), acc, accPath);
            psbtSignedStr = psbt.ToBase64();
        }
Ejemplo n.º 4
0
 public static IHDKey Derive(this IHDKey hdkey, uint index)
 {
     if (hdkey == null)
     {
         throw new ArgumentNullException(nameof(hdkey));
     }
     return(hdkey.Derive(new KeyPath(index)));
 }
        /// <summary>
        /// Add keypath information to this PSBT for each input or output involving it
        /// </summary>
        /// <param name="masterKey">The master key of the keypaths</param>
        /// <param name="paths">The path of the public keys with their expected scriptPubKey</param>
        /// <returns>This PSBT</returns>
        public PSBT AddKeyPath(IHDKey masterKey, params Tuple <KeyPath, Script>[] paths)
        {
            if (masterKey == null)
            {
                throw new ArgumentNullException(nameof(masterKey));
            }
            if (paths == null)
            {
                throw new ArgumentNullException(nameof(paths));
            }

            masterKey = masterKey.AsHDKeyCache();
            foreach (var path in paths)
            {
                var key = masterKey.Derive(path.Item1);
                AddKeyPath(masterKey.GetPublicKey().GetHDFingerPrint(), key.GetPublicKey(), path.Item1, path.Item2);
            }
            return(this);
        }