示例#1
0
 private TaprootFullPubKey(ECXOnlyPubKey outputKey, bool outputParity, TaprootInternalPubKey internalKey, uint256?merkleRoot) : base(outputKey)
 {
     OutputKey       = new TaprootPubKey(outputKey);
     OutputKeyParity = outputParity;
     InternalKey     = internalKey;
     MerkleRoot      = merkleRoot;
 }
示例#2
0
        public TaprootPubKey?ExtractScriptPubKeyParameters(Script scriptPubKey)
        {
            var ok = FastCheckScriptPubKey(scriptPubKey, out _);

            if (!ok)
            {
                return(null);
            }
            var arr = scriptPubKey.ToBytes(true);

#if !HAS_SPAN
            var pk = new byte[32];
            Array.Copy(arr, 2, pk, 0, 32);
            if (TaprootPubKey.TryCreate(pk, out var r))
            {
                return(r);
            }
            return(null);
#else
            if (TaprootPubKey.TryCreate(arr.AsSpan().Slice(2), out var r))
            {
                return(r);
            }
            return(null);
#endif
        }
示例#3
0
        internal TaprootAddress(string str, byte[] key, Network network) : base(str, network)
        {
#if HAS_SPAN
            if (ECXOnlyPubKey.TryCreate(key, out var k))
            {
                _PubKey = new TaprootPubKey(k);
            }
            else
            {
                throw new FormatException("Invalid TaprootAddress");
            }
#else
            _PubKey = new TaprootPubKey(key);
#endif
        }
示例#4
0
 public TaprootAddress(TaprootPubKey pubKey, Network network) :
     base(NotNull(pubKey) ?? Network.CreateBech32(Bech32Type.TAPROOT_ADDRESS, pubKey.ToBytes(), 1, network), network)
 {
     _PubKey = pubKey;
 }
示例#5
0
 internal TaprootAddress(string str, TaprootPubKey key, Network network) : base(str, network)
 {
     _PubKey = key;
 }
示例#6
0
        internal PSBTOutput(BitcoinStream stream, PSBT parent, uint index, TxOut txOut) : base(parent)
        {
            if (txOut == null)
            {
                throw new ArgumentNullException(nameof(txOut));
            }
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }

            TxOut = txOut;
            Index = index;

            byte[] k = new byte[0];
            byte[] v = new byte[0];
            try
            {
                stream.ReadWriteAsVarString(ref k);
            }
            catch (EndOfStreamException e)
            {
                throw new FormatException("Invalid PSBTOutput. Could not read key", e);
            }
            while (k.Length != 0)
            {
                try
                {
                    stream.ReadWriteAsVarString(ref v);
                }
                catch (EndOfStreamException e)
                {
                    throw new FormatException("Invalid PSBTOutput. Could not read value", e);
                }
                switch (k.First())
                {
                case PSBTConstants.PSBT_OUT_REDEEMSCRIPT:
                    if (k.Length != 1)
                    {
                        throw new FormatException("Invalid PSBTOutput. Contains illegal value in key for redeem script");
                    }
                    if (redeem_script != null)
                    {
                        throw new FormatException("Invalid PSBTOutput, duplicate key for redeem_script");
                    }
                    redeem_script = Script.FromBytesUnsafe(v);
                    break;

                case PSBTConstants.PSBT_OUT_WITNESSSCRIPT:
                    if (k.Length != 1)
                    {
                        throw new FormatException("Invalid PSBTOutput. Unexpected key length for PSBT_OUT_BIP32_DERIVATION");
                    }
                    if (witness_script != null)
                    {
                        throw new FormatException("Invalid PSBTOutput, duplicate key for redeem_script");
                    }
                    witness_script = Script.FromBytesUnsafe(v);
                    break;

                case PSBTConstants.PSBT_OUT_BIP32_DERIVATION:
                    var pubkey2 = new PubKey(k.Skip(1).ToArray());
                    if (hd_keypaths.ContainsKey(pubkey2))
                    {
                        throw new FormatException("Invalid PSBTOutput, duplicate key for hd_keypaths");
                    }
                    KeyPath path = KeyPath.FromBytes(v.Skip(4).ToArray());
                    hd_keypaths.Add(pubkey2, new RootedKeyPath(new HDFingerprint(v.Take(4).ToArray()), path));
                    break;

                case PSBTConstants.PSBT_OUT_TAP_BIP32_DERIVATION:
                    var pubkey3 = new TaprootPubKey(k.Skip(1).ToArray());
                    if (hd_taprootkeypaths.ContainsKey(pubkey3))
                    {
                        throw new FormatException("Invalid PSBTOutput, duplicate key for hd_taproot_keypaths");
                    }
                    var            bs     = new BitcoinStream(v);
                    List <uint256> hashes = null !;
                    bs.ReadWrite(ref hashes);
                    var     pos   = (int)bs.Inner.Position;
                    KeyPath path2 = KeyPath.FromBytes(v.Skip(pos + 4).ToArray());
                    hd_taprootkeypaths.Add(pubkey3,
                                           new TaprootKeyPath(
                                               new RootedKeyPath(new HDFingerprint(v.Skip(pos).Take(4).ToArray()), path2),
                                               hashes.ToArray()));
                    break;

                case PSBTConstants.PSBT_OUT_TAP_INTERNAL_KEY:
                    if (k.Length != 1)
                    {
                        throw new FormatException("Invalid PSBTOutput. Contains illegal value in key for internal taproot pubkey");
                    }
                    if (!TaprootInternalPubKey.TryCreate(v, out var tpk))
                    {
                        throw new FormatException("Invalid PSBTOutput. Contains invalid internal taproot pubkey");
                    }
                    TaprootInternalKey = tpk;
                    break;

                default:
                    if (unknown.ContainsKey(k))
                    {
                        throw new FormatException("Invalid PSBTInput, duplicate key for unknown value");
                    }
                    unknown.Add(k, v);
                    break;
                }
                stream.ReadWriteAsVarString(ref k);
            }
        }
示例#7
0
 public Script GenerateScriptPubKey(TaprootPubKey pubKey)
 {
     return(pubKey.ScriptPubKey);
 }