示例#1
0
        public static bool TryRecover(Context ctx, Secp256k1.SecpRecoverableECDSASignature recoverableSig, ReadOnlySpan <byte> msg32, [MaybeNullWhen(false)] out ECPubKey pubkey)
        {
            if (recoverableSig == null)
            {
                throw new ArgumentNullException(nameof(recoverableSig));
            }
            ctx ??= Context.Instance;
            GE     q;
            Scalar r, s;
            Scalar m;
            int    recid;

            if (msg32.Length != 32)
            {
                throw new ArgumentException(paramName: nameof(msg32), message: "msg32 should be 32 bytes");
            }

            (r, s, recid) = recoverableSig;
            VERIFY_CHECK(recid >= 0 && recid < 4);              /* should have been caught in parse_compact */
            m = new Scalar(msg32, out _);
            if (secp256k1_ecdsa_sig_recover(ctx.EcMultContext, r, s, out q, m, recid))
            {
                pubkey = new ECPubKey(q, ctx);
                return(true);
            }
            else
            {
                pubkey = null;
                return(false);
            }
        }
示例#2
0
        public void ReadWrite(BitcoinStream stream)
        {
#if HAS_SPAN
            if (stream.Serializing)
            {
                Span <byte> tmp = stackalloc byte[65];
                _ECKey.WriteToSpan(compressed, tmp, out var l);
                tmp = tmp.Slice(0, l);
                stream.ReadWrite(ref tmp);
            }
            else
            {
                Span <byte> tmp = stackalloc byte[compressed ? 33 : 65];
                stream.ReadWrite(ref tmp);
                if (NBitcoinContext.Instance.TryCreatePubKey(tmp, out var p) && p is Secp256k1.ECPubKey)
                {
                    _ECKey = p;
                }
                else
                {
                    throw new FormatException("Deserializing invalid pubkey");
                }
            }
#else
            stream.ReadWrite(ref vch);
            if (!stream.Serializing)
            {
                _ECKey = new ECKey(vch, false);
            }
#endif
        }
示例#3
0
文件: PubKey.cs 项目: awatin/NBitcoin
 internal PubKey(Secp256k1.ECPubKey pubkey, bool compressed)
 {
     if (pubkey == null)
     {
         throw new ArgumentNullException(nameof(pubkey));
     }
     this._ECKey     = pubkey;
     this.compressed = compressed;
 }