Exemple #1
0
        public static Key FromMnemonic(Mnemonic mnemonic, string passphrase, ECKind kind = ECKind.Ed25519)
        {
            var seed = mnemonic.GetSeed(passphrase);
            var key  = new Key(seed.GetBytes(0, 32), kind, true);

            seed.Flush();
            return(key);
        }
Exemple #2
0
 internal static byte[] GetPubKeyPrefix(ECKind kind)
 {
     return(kind == ECKind.Ed25519
         ? _EdPublicKeyPrefix
         : kind == ECKind.NistP256
             ? _NistPublicKeyPrefix
             : _SecpPublicKeyPrefix);
 }
Exemple #3
0
 internal static byte[] GetSignaturePrefix(ECKind kind)
 {
     return(kind == ECKind.Ed25519
         ? _EdSignaturePrefix
         : kind == ECKind.NistP256
             ? _NistSignaturePrefix
             : _SecpSignaturePrefix);
 }
Exemple #4
0
 public static Curve FromKind(ECKind kind)
 {
     return(kind == ECKind.Ed25519
         ? new Ed25519()
         : kind == ECKind.NistP256
             ? (Curve) new NistP256()
             : new Secp256k1());
 }
Exemple #5
0
 public static ICurve GetCurve(ECKind kind)
 {
     return(kind == ECKind.Ed25519
         ? new Ed25519()
         : kind == ECKind.NistP256
             ? (ICurve) new NistP256()
             : new Secp256k1());
 }
Exemple #6
0
        public Key(ECKind kind = ECKind.Ed25519)
        {
            Curve = Curve.FromKind(kind);
            var bytes = Curve.GeneratePrivateKey();

            Store = new PlainSecretStore(bytes);
            bytes.Flush();
        }
Exemple #7
0
        public async Task<PubKey> GetPublicKeyAsync(ECKind curve = ECKind.Ed25519, bool display = false, CancellationToken cancellation = default)
        {
            var response = await ExchangeSingleAPDUAsync(
                    TezosWalletCLA,
                    (byte) Instruction.InsGetPublicKey,
                    (byte) (display ? 1 : 0),
                    (byte) (curve - 1) , KeyPath, OK, cancellation)
                .ConfigureAwait(false);

            return PubKey.FromBytes(Utils.GetBytes(response, 2, response.Length - 2));
        }
Exemple #8
0
        internal Key(byte[] bytes, ECKind kind, bool flush = false)
        {
            if (bytes?.Length != 32)
            {
                throw new ArgumentException("Invalid private key length", nameof(bytes));
            }

            Curve = Curve.FromKind(kind);
            Store = new PlainSecretStore(bytes);
            if (flush)
            {
                bytes.Flush();
            }
        }
Exemple #9
0
        internal PubKey(byte[] bytes, ECKind kind, bool flush = false)
        {
            if (kind == ECKind.Ed25519 && bytes.Length != 32 || kind != ECKind.Ed25519 && bytes.Length != 33)
            {
                throw new ArgumentException("Invalid public key length", nameof(bytes));
            }

            Curve = Curve.FromKind(kind);
            Store = new PlainSecretStore(bytes);
            if (flush)
            {
                bytes.Flush();
            }
        }
Exemple #10
0
        internal PubKey(byte[] bytes, ECKind kind, bool flush = false)
        {
            if (bytes.Length < 32)
            {
                throw new ArgumentException("Invalid public key length", nameof(bytes));
            }

            Curve = Curves.GetCurve(kind);
            Store = new PlainSecretStore(bytes);

            if (flush)
            {
                bytes.Flush();
            }
        }
Exemple #11
0
        }                                                                       //TODO: check key strength

        internal Key(byte[] bytes, ECKind kind, bool flush = false)
        {
            if (bytes.Length < 32)
            {
                throw new ArgumentException("Invalid private key length", nameof(bytes));
            }

            Curve = Curves.GetCurve(kind);

            var privateKey = Curve.GetPrivateKey(bytes);

            Store = new PlainSecretStore(privateKey);

            privateKey.Flush();
            if (flush)
            {
                bytes.Flush();
            }
        }
Exemple #12
0
        static void TestPublicKeyDerivation(string path, string seed, ECKind kind = ECKind.Secp256k1)
        {
            var masterKey    = HDKey.FromSeed(Hex.Parse(seed), kind);
            var masterPubKey = masterKey.HDPubKey;

            foreach (var uind in HDPath.Parse(path))
            {
                var childKey    = masterKey.Derive((int)(uind & 0x7FFFFFFF), (uind & 0x80000000) != 0);
                var childPubKey = childKey.HDPubKey;

                if ((uind & 0x80000000) == 0)
                {
                    var derivedPubKey = masterPubKey.Derive((int)uind);
                    Assert.Equal(derivedPubKey.PubKey.GetBase58(), childPubKey.PubKey.GetBase58());
                    Assert.Equal(Hex.Convert(derivedPubKey.ChainCode), Hex.Convert(childPubKey.ChainCode));
                }

                masterKey    = childKey;
                masterPubKey = childPubKey;
            }
        }
Exemple #13
0
        public async Task<Signature> Sign(byte[] data, ECKind curve = ECKind.Ed25519, CancellationToken cancellation = default)
        {
            var list = new List<byte[]> { KeyPath };
            var response = Array.Empty<byte>();
            
            list.AddRange(Utils.SplitArray(data));
            
            foreach (var (value, index) in list.Select((value, i) => (value, i)))
            {
                var code = (index == 0)
                    ? 0x00
                    : (index == list.Count - 1)
                        ? 0x81
                        : 0x01;

                response = await ExchangeSingleAPDUAsync(
                    TezosWalletCLA,
                    (byte) Instruction.InsSign,
                    (byte) code,
                    (byte) (curve - 1), value, OK, cancellation).ConfigureAwait(false);
            }
	
            return new Signature(response, Utils.GetSignaturePrefix(curve));
        }
Exemple #14
0
 public static Key FromMnemonic(Mnemonic mnemonic, string passphrase, ECKind kind = ECKind.Ed25519)
 => new Key(mnemonic.GetSeed(passphrase), kind, true);
Exemple #15
0
 /// <summary>
 /// Generates a new extended (hierarchical deterministic) private key
 /// </summary>
 /// <param name="kind">Elliptic curve kind</param>
 public HDKey(ECKind kind = ECKind.Ed25519)
 {
     Key        = new(RNG.GetBytes(32), kind, true);
     _ChainCode = RNG.GetBytes(32);
 }
Exemple #16
0
 public static PubKey FromBase64(string base64, ECKind kind = ECKind.Ed25519)
 => new PubKey(Base64.Decode(base64), kind, true);
Exemple #17
0
 public static PubKey FromHex(string hex, ECKind kind = ECKind.Ed25519)
 => new PubKey(Hex.Parse(hex), kind, true);
Exemple #18
0
 public static PubKey FromBytes(byte[] bytes, ECKind kind = ECKind.Ed25519)
 => new PubKey(bytes, kind);
Exemple #19
0
        }                                                                      //TODO: check key strength

        public Key(ECKind kind) : this(RNG.GetNonZeroBytes(32), kind, true)
        {
        }                                                                       //TODO: check key strength
Exemple #20
0
 public static Key FromBase64(string base64, ECKind kind = ECKind.Ed25519)
 => new Key(Base64.Parse(base64), kind, true);