Ejemplo n.º 1
0
        public static Credentials Create(string copayerName, Network network)
        {
            var newCopayerKey   = new NBitcoin.ExtKey();
            var walletKey       = new NBitcoin.Key();
            var copayerXPrivKey = newCopayerKey.ToString(network);

            return(Credentials.FromExtendedPrivateKey(copayerXPrivKey, walletKey, copayerName, network));
        }
Ejemplo n.º 2
0
 public ExtKey Derive(uint nChild)
 {
     var result = new ExtKey();
     result.nDepth = (byte)(nDepth + 1);
     result.vchFingerprint = key.PubKey.ID.ToBytes().Take(result.vchFingerprint.Length).ToArray();
     result.nChild = nChild;
     result.key = key.Derivate(this.vchChainCode, nChild, out result.vchChainCode);
     return result;
 }
Ejemplo n.º 3
0
        static void Generate()
        {
            var k = new NBitcoin.ExtKey();

            Console.Out.WriteLine("Ext priv");
            Console.Out.WriteLine(k.ToString(NBitcoin.Network.TestNet));

            Console.Out.WriteLine("Ext pub");
            Console.Out.WriteLine(k.Neuter());

            var pk = k.PrivateKey;

            Console.Out.WriteLine("PrivK");
            Console.Out.WriteLine(pk.ToString(NBitcoin.Network.TestNet));

            Console.Out.WriteLine("Public");
            Console.Out.WriteLine(pk.PubKey.ToString());
        }
        public PSBT SignAll(ExtKey extkey, KeyPath keyPath, SigHash sigHash)
        {
            if (extkey == null)
            {
                throw new ArgumentNullException(nameof(extkey));
            }
            if (keyPath == null)
            {
                throw new ArgumentNullException(nameof(keyPath));
            }
            var privKey = extkey.Derive(keyPath).PrivateKey;

            foreach (var input in this.Inputs)
            {
                if (input.HDKeyPaths.TryGetValue(privKey.PubKey, out var v) &&
                    v.Item1 != default &&
                    v.Item1 == extkey.PrivateKey.PubKey.GetHDFingerPrint())
                {
                    input.Sign(privKey);
                }
            }
            return(this);
        }
Ejemplo n.º 5
0
        public ExtKey GetParentExtKey(ExtPubKey parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }
            if (Depth == 0)
            {
                throw new InvalidOperationException("This ExtKey is the root key of the HD tree");
            }
            if (IsHardened)
            {
                throw new InvalidOperationException("This private key is hardened, so you can't get its parent");
            }
            var expectedFingerPrint = parent.CalculateChildFingerprint();

            if (parent.Depth != this.Depth - 1 || !expectedFingerPrint.SequenceEqual(vchFingerprint))
            {
                throw new ArgumentException("The parent ExtPubKey is not the immediate parent of this ExtKey", "parent");
            }

            byte[] l  = null;
            byte[] ll = new byte[32];
            byte[] lr = new byte[32];

            var pubKey = parent.PubKey.ToBytes();

            l = Hashes.BIP32Hash(parent.vchChainCode, nChild, pubKey[0], pubKey.SafeSubarray(1));
            Array.Copy(l, ll, 32);
            Array.Copy(l, 32, lr, 0, 32);
            var ccChild = lr;

            BigInteger parse256LL = new BigInteger(1, ll);
            BigInteger N          = ECKey.CURVE.N;

            if (!ccChild.SequenceEqual(vchChainCode))
            {
                throw new InvalidOperationException("The derived chain code of the parent is not equal to this child chain code");
            }

            var keyBytes = PrivateKey.ToBytes();
            var key      = new BigInteger(1, keyBytes);

            BigInteger kPar           = key.Add(parse256LL.Negate()).Mod(N);
            var        keyParentBytes = kPar.ToByteArrayUnsigned();

            if (keyParentBytes.Length < 32)
            {
                keyParentBytes = new byte[32 - keyParentBytes.Length].Concat(keyParentBytes).ToArray();
            }

            var parentExtKey = new ExtKey
            {
                vchChainCode   = parent.vchChainCode,
                nDepth         = parent.Depth,
                vchFingerprint = parent.Fingerprint,
                nChild         = parent.nChild,
                key            = new Key(keyParentBytes)
            };

            return(parentExtKey);
        }
Ejemplo n.º 6
0
        public ExtKey Derive(KeyPath derivation)
        {
            ExtKey result = this;

            return(derivation.Indexes.Aggregate(result, (current, index) => current.Derive(index)));
        }
Ejemplo n.º 7
0
 public bool IsParentOf(ExtKey childKey)
 {
     return(childKey.IsChildOf(this));
 }
Ejemplo n.º 8
0
		public ExtKey GetParentExtKey(ExtPubKey parent)
		{
			if(parent == null)
				throw new ArgumentNullException("parent");
			if(Depth == 0)
				throw new InvalidOperationException("This ExtKey is the root key of the HD tree");
			if(IsHardened)
				throw new InvalidOperationException("This private key is hardened, so you can't get its parent");
			var expectedFingerPrint = parent.CalculateChildFingerprint();
			if(parent.Depth != this.Depth - 1 || !expectedFingerPrint.SequenceEqual(vchFingerprint))
				throw new ArgumentException("The parent ExtPubKey is not the immediate parent of this ExtKey", "parent");

			byte[] l = null;
			byte[] ll = new byte[32];
			byte[] lr = new byte[32];

			var pubKey = parent.PubKey.ToBytes();
			l = Hashes.BIP32Hash(parent.vchChainCode, nChild, pubKey[0], pubKey.SafeSubarray(1));
			Array.Copy(l, ll, 32);
			Array.Copy(l, 32, lr, 0, 32);
			var ccChild = lr;

			BigInteger parse256LL = new BigInteger(1, ll);
			BigInteger N = ECKey.CURVE.N;

			if(!ccChild.SequenceEqual(vchChainCode))
				throw new InvalidOperationException("The derived chain code of the parent is not equal to this child chain code");

			var keyBytes = PrivateKey.ToBytes();
			var key = new BigInteger(1, keyBytes);

			BigInteger kPar = key.Add(parse256LL.Negate()).Mod(N);
			var keyParentBytes = kPar.ToByteArrayUnsigned();
			if(keyParentBytes.Length < 32)
				keyParentBytes = new byte[32 - keyParentBytes.Length].Concat(keyParentBytes).ToArray();

			var parentExtKey = new ExtKey
			{
				vchChainCode = parent.vchChainCode,
				nDepth = parent.Depth,
				vchFingerprint = parent.Fingerprint,
				nChild = parent.nChild,
				key = new Key(keyParentBytes)
			};
			return parentExtKey;
		}
Ejemplo n.º 9
0
		public ExtKey Derive(uint index)
		{
			var result = new ExtKey
			{
				nDepth = (byte)(nDepth + 1),
				vchFingerprint = CalculateChildFingerprint(),
				nChild = index
			};
			result.key = key.Derivate(this.vchChainCode, index, out result.vchChainCode);
			return result;
		}
Ejemplo n.º 10
0
		public bool IsParentOf(ExtKey childKey)
		{
			return childKey.IsChildOf(this);
		}
Ejemplo n.º 11
0
 public BitcoinExtKey Derive(uint index)
 {
     return(new BitcoinExtKey(ExtKey.Derive(index), Network));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Gets the Base58 representation, in the same network, of the neutered extended key.
 /// </summary>
 public BitcoinExtPubKey Neuter()
 {
     return(ExtKey.Neuter().GetWif(Network));
 }
Ejemplo n.º 13
0
 public BitcoinExtKey(ExtKey key, Network network)
     : base(key, network)
 {
 }
 public PSBT SignAll(ExtKey extkey, KeyPath keyPath)
 {
     return(SignAll(extkey, keyPath, SigHash.All));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Constructor. Creates a representation of an extended key, within the specified network.
 /// </summary>
 public BitcoinExtKey(ExtKey key, Network network)
     : base(key.ToBytes(), network)
 {
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Constructor. Creates a representation of an extended key, within the specified network.
 /// </summary>
 public BitcoinExtKey(ExtKey key, Network network)
     : base(key, network)
 {
 }
Ejemplo n.º 17
0
 public BitcoinExtKey CreateBitcoinExtKey(ExtKey key)
 {
     return(new BitcoinExtKey(key, this));
 }
Ejemplo n.º 18
0
		public bool IsChildOf(ExtKey parentKey)
		{
			if(Depth != parentKey.Depth + 1)
				return false;
			return parentKey.CalculateChildFingerprint().SequenceEqual(Fingerprint);
		}