Ejemplo n.º 1
0
 private byte[] schnorrComputeSigPoint(byte[] data, byte[] nonce, byte[] pubKey, bool compressed)
 {
     Assert.True(ECXOnlyPubKey.TryCreate(pubKey, Context.Instance, out var pk));
     Assert.True(SchnorrNonce.TryCreate(nonce, out var n));
     Assert.True(new OracleInfo(pk, n).TryComputeSigpoint(new DiscreteOutcome(data), out var sigpoint));
     return(sigpoint.ToBytes(compressed));
 }
 public static bool TryCreate(ReadOnlySpan <byte> pubkey, [MaybeNullWhen(false)] out TaprootInternalPubKey result)
 {
     if (ECXOnlyPubKey.TryCreate(pubkey, out var k))
     {
         result = new TaprootInternalPubKey(k);
         return(true);
     }
     result = null;
     return(false);
 }
 public TaprootInternalPubKey(ReadOnlySpan <byte> pubkey)
 {
     if (pubkey.Length != 32)
     {
         throw new FormatException("The pubkey size should be 32 bytes");
     }
     if (!ECXOnlyPubKey.TryCreate(pubkey, out var k))
     {
         throw new FormatException("Invalid taproot pubkey");
     }
     this.pubkey = k;
 }
Ejemplo n.º 4
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
        }
Ejemplo n.º 5
0
        public async Task <Oracle?> TryGetOracle(string oracleName)
        {
            var id = await NameRepository.GetId(Scopes.Oracles, oracleName);

            if (id is null)
            {
                return(null);
            }
            ECXOnlyPubKey.TryCreate(Encoders.Hex.DecodeData(id), Context.Instance, out var pubkey);
            if (pubkey is null)
            {
                return(null);
            }
            return(await Repository.GetOracle(pubkey));
        }
        public TaprootInternalPubKey(byte[] pubkey)
        {
            if (pubkey.Length != 32)
            {
                throw new FormatException("The pubkey size should be 32 bytes");
            }
#if HAS_SPAN
            if (!ECXOnlyPubKey.TryCreate(pubkey, out var k))
            {
                throw new FormatException("Invalid taproot pubkey");
            }
            this.pubkey = k;
#else
            pubkey.CopyTo(this.pubkey, 0);
#endif
        }
Ejemplo n.º 7
0
        public static bool TryParse(string str, [MaybeNullWhen(false)] out OracleId id)
        {
            id = null;
            if (!HexEncoder.IsWellFormed(str))
            {
                return(false);
            }
            var bytes = Encoders.Hex.DecodeData(str);

            if (!ECXOnlyPubKey.TryCreate(bytes, Context.Instance, out var k) || k is null)
            {
                return(false);
            }
            id = new OracleId(k);
            return(true);
        }
Ejemplo n.º 8
0
 private static bool TryCreate(ReadOnlySpan <byte> input64, [MaybeNullWhen(false)] out OracleInfo oracleInfo)
 {
     oracleInfo = null;
     if (input64.Length != 64)
     {
         return(false);
     }
     if (!ECXOnlyPubKey.TryCreate(input64.Slice(0, 32), Context.Instance, out var pubkey) || pubkey is null)
     {
         return(false);
     }
     if (!SchnorrNonce.TryCreate(input64.Slice(32), out var rValue) || rValue is null)
     {
         return(false);
     }
     oracleInfo = new OracleInfo(pubkey, rValue);
     return(true);
 }
Ejemplo n.º 9
0
        protected override async Task InvokeAsyncBase(InvocationContext context)
        {
            var oracleName = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("name")?.Trim();

            if (oracleName is null)
            {
                throw new CommandOptionRequiredException("name");
            }
            var pubkey = context.ParseResult.CommandResult.GetArgumentValueOrDefault <string>("pubkey")?.ToLowerInvariant()?.Trim();

            if (pubkey is null)
            {
                throw new CommandOptionRequiredException("pubkey");
            }
            var exists = await Repository.OracleExists(oracleName);

            if (exists && !Set)
            {
                throw new CommandException("name", "This oracle already exists");
            }
            if (!exists && Set)
            {
                throw new CommandException("name", "This oracle does not exists");
            }

            ECXOnlyPubKey?pubkeyObj;

            try
            {
                if (!ECXOnlyPubKey.TryCreate(Encoders.Hex.DecodeData(pubkey), Context.Instance, out pubkeyObj) || pubkeyObj is null)
                {
                    throw new CommandException("pubkey", "Invalid pubkey");
                }
            }
            catch (FormatException)
            {
                throw new CommandException("pubkey", "Invalid pubkey");
            }
            await Repository.SetOracle(oracleName, pubkeyObj);
        }
Ejemplo n.º 10
0
        public static new TaprootAddress Create(string bech32, Network expectedNetwork)
        {
            if (bech32 is null)
            {
                throw new ArgumentNullException(nameof(bech32));
            }
            if (expectedNetwork is null)
            {
                throw new ArgumentNullException(nameof(expectedNetwork));
            }
            bech32 = bech32.ToLowerInvariant();
            if (expectedNetwork.GetBech32Encoder(Bech32Type.TAPROOT_ADDRESS, false) is Bech32Encoder encoder)
            {
                var decoded = encoder.Decode(bech32, out var v);
#if HAS_SPAN
                if (v == 1 && ECXOnlyPubKey.TryCreate(decoded, out var k))
                {
                    return(new TaprootAddress(bech32, new TaprootPubKey(k), expectedNetwork));
                }
                else
                {
                    throw new FormatException("Invalid TaprootAddress");
                }
#else
                if (v == 1 && decoded.Length == 32)
                {
                    return(new TaprootAddress(bech32, new TaprootPubKey(decoded), expectedNetwork));
                }
                else
                {
                    throw new FormatException("Invalid TaprootAddress");
                }
#endif
            }
            else
            {
                throw expectedNetwork.Bech32NotSupported(Bech32Type.TAPROOT_ADDRESS);
            }
        }
Ejemplo n.º 11
0
        private ECXOnlyPubKey GetOraclePubKey(string?pubkey)
        {
            var optionName = "oraclepubkey";

            if (pubkey is null)
            {
                throw new CommandOptionRequiredException(optionName);
            }
            try
            {
                var hex = Encoders.Hex.DecodeData(pubkey);
                if (hex.Length != 32 ||
                    !ECXOnlyPubKey.TryCreate(hex, Context.Instance, out var r) || r is null)
                {
                    throw new CommandException(optionName, "Invalid pubkey");
                }
                return(r);
            }
            catch
            {
                throw new CommandException(optionName, "Invalid pubkey");
            }
        }
Ejemplo n.º 12
0
        public static bool TryParse(string str, out OracleInfo?oracleInfo)
        {
            oracleInfo = null;
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }
            var bytes = Encoders.Hex.DecodeData(str);

            if (bytes.Length != 64)
            {
                return(false);
            }
            if (!ECXOnlyPubKey.TryCreate(bytes.AsSpan().Slice(0, 32), Context.Instance, out var pubkey) || pubkey is null)
            {
                return(false);
            }
            if (!SchnorrNonce.TryCreate(bytes.AsSpan().Slice(32), out var rValue) || rValue is null)
            {
                return(false);
            }
            oracleInfo = new OracleInfo(pubkey, rValue);
            return(true);
        }
Ejemplo n.º 13
0
 private bool schnorrVerify(byte[] sig, byte[] data, byte[] pubx)
 {
     Assert.True(NBitcoin.Secp256k1.SecpSchnorrSignature.TryCreate(sig, out var o));
     Assert.True(ECXOnlyPubKey.TryCreate(pubx, Context.Instance, out var pub));
     return(pub.SigVerifyBIP340(o, data));
 }