예제 #1
0
파일: Key.cs 프로젝트: dmirgaleev/netezos
 public Signature Sign(string message)
 {
     using (Store.Unlock())
     {
         return(Curve.Sign(Utf8.Parse(message), Store.Data));
     }
 }
예제 #2
0
파일: Bip39.cs 프로젝트: dmirgaleev/netezos
        public static byte[] GetSeed(string words, string passphrase = "")
        {
            if (string.IsNullOrWhiteSpace(words))
            {
                throw new ArgumentNullException(nameof(words));
            }

            var pass = Utf8.Parse(words);
            var salt = Utf8.Parse("mnemonic" + passphrase);

            return(Pbkdf2.ComputeDerivedKey(new HMACSHA512(pass), salt, 2048, 64));
        }
예제 #3
0
        public override IMicheline Optimize(IMicheline value)
        {
            if (value is MichelineString micheStr)
            {
                var address         = micheStr.Value.Length > 36 ? micheStr.Value.Substring(0, 36) : micheStr.Value;
                var addressBytes    = Base58.Parse(address, 3);
                var entrypointBytes = micheStr.Value.Length > 37
                    ? Utf8.Parse(micheStr.Value.Substring(37))
                    : null;

                var res = new byte[22 + (entrypointBytes?.Length ?? 0)];

                switch (address.Substring(0, 3))
                {
                case "tz1":
                    addressBytes.CopyTo(res, 2);
                    res[0] = 0;
                    res[1] = 0;
                    break;

                case "tz2":
                    addressBytes.CopyTo(res, 2);
                    res[0] = 0;
                    res[1] = 1;
                    break;

                case "tz3":
                    addressBytes.CopyTo(res, 2);
                    res[0] = 0;
                    res[1] = 2;
                    break;

                case "KT1":
                    addressBytes.CopyTo(res, 1);
                    res[0]  = 1;
                    res[21] = 0;
                    break;

                default:
                    throw FormatException(value);
                }

                if (entrypointBytes != null)
                {
                    entrypointBytes.CopyTo(res, 22);
                }

                return(new MichelineBytes(res));
            }

            return(value);
        }
예제 #4
0
 public static byte[] ForgeString(string value, int len = 4)
 {
     return(ForgeInt32(value.Length, len).Concat(Utf8.Parse(value)));
 }
예제 #5
0
        public static void WriteFlat(BinaryWriter writer, IMicheline value)
        {
            var stack = new Stack <object>();

            stack.Push(value);

            int  i, len, tag;
            bool argsCount;

            byte[] bytes;
            while (stack.Count > 0)
            {
                switch (stack.Pop())
                {
                case MichelineArray array:
                    if (array.Count >= 0x1F)
                    {
                        writer.Write((byte)((int)array.Type | 0x1F));
                        writer.Write7BitInt(array.Count);
                    }
                    else
                    {
                        writer.Write((byte)((int)array.Type | array.Count));
                    }
                    for (i = array.Count - 1; i >= 0; i--)
                    {
                        stack.Push(array[i]);
                    }
                    break;

                case MichelinePrim prim:
                    argsCount = false;
                    tag       = (int)prim.Type;
                    if (prim.Args?.Count > 0)
                    {
                        if (prim.Args.Count >= 0x07)
                        {
                            tag      |= 0x70;
                            argsCount = true;
                        }
                        else
                        {
                            tag |= prim.Args.Count << 4;
                        }
                    }
                    if (prim.Annots?.Count > 0)
                    {
                        stack.Push(prim.Annots);
                        if (prim.Annots.Count >= 0x0F)
                        {
                            tag |= 0x0F;
                            stack.Push(prim.Annots.Count);
                        }
                        else
                        {
                            tag |= prim.Annots.Count;
                        }
                    }
                    writer.Write((byte)tag);
                    writer.Write((byte)prim.Prim);
                    if (prim.Args != null)
                    {
                        if (argsCount)
                        {
                            writer.Write7BitInt(prim.Args.Count);
                        }

                        for (i = prim.Args.Count - 1; i >= 0; i--)
                        {
                            stack.Push(prim.Args[i]);
                        }
                    }
                    break;

                case MichelineBytes micheBytes:
                    bytes = micheBytes.Value;
                    len   = micheBytes.Value.Length;
                    if (len >= 0x1F)
                    {
                        writer.Write((byte)((int)micheBytes.Type | 0x1F));
                        writer.Write7BitInt(len);
                    }
                    else
                    {
                        writer.Write((byte)((int)micheBytes.Type | len));
                    }
                    writer.Write(bytes);
                    break;

                case MichelineString micheString:
                    bytes = Utf8.Parse(micheString.Value);
                    len   = bytes.Length;
                    if (len >= 0x1F)
                    {
                        writer.Write((byte)((int)micheString.Type | 0x1F));
                        writer.Write7BitInt(len);
                    }
                    else
                    {
                        writer.Write((byte)((int)micheString.Type | len));
                    }
                    writer.Write(bytes);
                    break;

                case MichelineInt micheInt:
                    bytes = micheInt.Value.ToByteArray();
                    len   = bytes.Length;
                    if (len >= 0x1F)
                    {
                        writer.Write((byte)((int)micheInt.Type | 0x1F));
                        writer.Write7BitInt(len);
                    }
                    else
                    {
                        writer.Write((byte)((int)micheInt.Type | len));
                    }
                    writer.Write(bytes);
                    break;

                case int intValue:
                    writer.Write7BitInt(intValue);
                    break;

                case IEnumerable <IAnnotation> annots:
                    foreach (var annot in annots)
                    {
                        bytes = Utf8.Parse(annot.Value);
                        len   = bytes.Length;
                        if (len >= 0x3F)
                        {
                            writer.Write((byte)((int)annot.Type | 0x3F));
                            writer.Write7BitInt(len);
                        }
                        else
                        {
                            writer.Write((byte)((int)annot.Type | len));
                        }
                        writer.Write(bytes);
                    }
                    break;

                default:
                    throw new Exception();
                }
            }
        }
예제 #6
0
        public override IMicheline Optimize(IMicheline value)
        {
            if (value is not MichelineString micheStr)
            {
                return(value);
            }

            string address;

            byte[] addressBytes;
            byte[] entrypointBytes;

            if (micheStr.Value.StartsWith("txr1"))
            {
                address         = micheStr.Value.Length > 37 ? micheStr.Value.Substring(0, 37) : micheStr.Value;
                addressBytes    = Base58.Parse(address, 4);
                entrypointBytes = micheStr.Value.Length > 38
                    ? Utf8.Parse(micheStr.Value.Substring(38))
                    : null;
            }
            else
            {
                address         = micheStr.Value.Length > 36 ? micheStr.Value.Substring(0, 36) : micheStr.Value;
                addressBytes    = Base58.Parse(address, 3);
                entrypointBytes = micheStr.Value.Length > 37
                    ? Utf8.Parse(micheStr.Value.Substring(37))
                    : null;
            }

            var res = new byte[22 + (entrypointBytes?.Length ?? 0)];

            switch (address.Substring(0, 3))
            {
            case "tz1":
                addressBytes.CopyTo(res, 2);
                res[0] = 0;
                res[1] = 0;
                break;

            case "tz2":
                addressBytes.CopyTo(res, 2);
                res[0] = 0;
                res[1] = 1;
                break;

            case "tz3":
                addressBytes.CopyTo(res, 2);
                res[0] = 0;
                res[1] = 2;
                break;

            case "KT1":
                addressBytes.CopyTo(res, 1);
                res[0]  = 1;
                res[21] = 0;
                break;

            case "txr" when address.StartsWith("txr1"):
                addressBytes.CopyTo(res, 1);

                res[0]  = 2;
                res[21] = 0;
                break;

            default:
                throw FormatException(value);
            }

            if (entrypointBytes != null)
            {
                entrypointBytes.CopyTo(res, 22);
            }

            return(new MichelineBytes(res));
        }
예제 #7
0
        public bool TryAuthenticate(AuthHeaders headers, string json, out string error)
        {
            error = null;

            if (string.IsNullOrEmpty(headers.User))
            {
                error = $"The X-TZKT-USER header is required";
                return(false);
            }

            if (headers.Nonce == null)
            {
                error = $"The X-TZKT-NONCE header is required";
                return(false);
            }

            if (string.IsNullOrEmpty(headers.Signature))
            {
                error = $"The X-TZKT-SIGNATURE header is required";
                return(false);
            }

            if (string.IsNullOrEmpty(json))
            {
                error = $"The body is empty";
                return(false);
            }

            if (!Config.Credentials.TryGetValue(headers.User, out var pubKey))
            {
                error = $"User {headers.User} doesn't exist";
                return(false);
            }

            var nonce     = (long)headers.Nonce;
            var nonceTime = DateTime.UnixEpoch.AddMilliseconds(nonce);

            if (nonceTime < DateTime.UtcNow.AddSeconds(-Config.NonceLifetime))
            {
                error = $"Nonce too old. Server time: {DateTime.UtcNow}, nonce: {nonceTime}";
                return(false);
            }

            if (nonce <= Nonces[headers.User])
            {
                error = $"Nonce {nonce} has already used";
                return(false);
            }

            var hash = Hex.Convert(Blake2b.GetDigest(Utf8.Parse(json)));

            var key = PubKey.FromBase58(pubKey);

            if (!key.Verify($"{headers.Nonce}{hash}", headers.Signature))
            {
                error = $"Invalid signature";
                return(false);
            }

            Nonces[headers.User] = nonce;
            return(true);
        }
예제 #8
0
        public static byte[] ForgeString(string value, int len = 4)
        {
            var bytes = Utf8.Parse(value);

            return(ForgeInt32(bytes.Length, len).Concat(bytes));
        }