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); }
internal static byte[] GetPubKeyPrefix(ECKind kind) { return(kind == ECKind.Ed25519 ? _EdPublicKeyPrefix : kind == ECKind.NistP256 ? _NistPublicKeyPrefix : _SecpPublicKeyPrefix); }
internal static byte[] GetSignaturePrefix(ECKind kind) { return(kind == ECKind.Ed25519 ? _EdSignaturePrefix : kind == ECKind.NistP256 ? _NistSignaturePrefix : _SecpSignaturePrefix); }
public static Curve FromKind(ECKind kind) { return(kind == ECKind.Ed25519 ? new Ed25519() : kind == ECKind.NistP256 ? (Curve) new NistP256() : new Secp256k1()); }
public static ICurve GetCurve(ECKind kind) { return(kind == ECKind.Ed25519 ? new Ed25519() : kind == ECKind.NistP256 ? (ICurve) new NistP256() : new Secp256k1()); }
public Key(ECKind kind = ECKind.Ed25519) { Curve = Curve.FromKind(kind); var bytes = Curve.GeneratePrivateKey(); Store = new PlainSecretStore(bytes); bytes.Flush(); }
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)); }
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(); } }
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(); } }
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(); } }
} //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(); } }
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; } }
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)); }
public static Key FromMnemonic(Mnemonic mnemonic, string passphrase, ECKind kind = ECKind.Ed25519) => new Key(mnemonic.GetSeed(passphrase), kind, true);
/// <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); }
public static PubKey FromBase64(string base64, ECKind kind = ECKind.Ed25519) => new PubKey(Base64.Decode(base64), kind, true);
public static PubKey FromHex(string hex, ECKind kind = ECKind.Ed25519) => new PubKey(Hex.Parse(hex), kind, true);
public static PubKey FromBytes(byte[] bytes, ECKind kind = ECKind.Ed25519) => new PubKey(bytes, kind);
} //TODO: check key strength public Key(ECKind kind) : this(RNG.GetNonZeroBytes(32), kind, true) { } //TODO: check key strength
public static Key FromBase64(string base64, ECKind kind = ECKind.Ed25519) => new Key(Base64.Parse(base64), kind, true);