コード例 #1
0
        public void KeyPairTest()
        {
            //sr25519, schnorrkel, without psw or ///Substrate

            var mnemonic = "donor rocket find fan language damp yellow crouch attend meat hybrid pulse";
            var keyPair1 = Mnemonic.GetKeyPairFromMnemonic(mnemonic, "", BIP39Wordlist.English, ExpandMode.Ed25519);

            Assert.AreEqual("5CSFNKvSFchQd7TjuuvPca1RheLAqZfFKiqAM6Fv6us9QhvR", Utils.GetAddressFrom(keyPair1.Public.Key));

            var keyPair2 = Mnemonic.GetKeyPairFromMnemonic(mnemonic, "Substrate", BIP39Wordlist.English, ExpandMode.Ed25519);

            Assert.AreEqual("5FRbTVsuNAXFDq19gSnwihXUDMeEQKfhDnUWgYuUq6jFknVq", Utils.GetAddressFrom(keyPair2.Public.Key));

            Assert.AreEqual(32, keyPair2.Secret.key.GetBytes().Length);
            Assert.AreEqual("0x7CFF5CEAEEAF93EF4675DFCA17FF1383B66DF5141F491309D70A5C1087D3910D", Utils.Bytes2HexString(keyPair2.Secret.key.GetBytes()));
            Assert.AreEqual("0x767B646A4BEAD69074C77B69D142A21978D867357A2227D16370F8A675991011", Utils.Bytes2HexString(keyPair2.Secret.nonce));
            Assert.AreEqual("0x7CFF5CEAEEAF93EF4675DFCA17FF1383B66DF5141F491309D70A5C1087D3910D" +
                            "767B646A4BEAD69074C77B69D142A21978D867357A2227D16370F8A675991011", Utils.Bytes2HexString(keyPair2.Secret.ToBytes()));
            Assert.AreEqual("0x94A34FAF4D464F0404C41EFEAE6C476C21755492F77B5715CE5291CE601A8318", Utils.Bytes2HexString(keyPair2.Public.Key));

            var secret = Mnemonic.GetSecretKeyFromMnemonic(mnemonic, "Substrate", BIP39Wordlist.English);

            Assert.AreEqual("0x9AD12F46F904DA56073948B789F7FD8A5CD1A2E79480A5986DFB7C814E228586", Utils.Bytes2HexString(secret));

            var miniSecret = new MiniSecret(secret, ExpandMode.Ed25519);
            var keyPair3   = miniSecret.GetPair();

            Assert.AreEqual(keyPair2.Public.Key, keyPair3.Public.Key);
            Assert.AreEqual(keyPair2.Secret.ToBytes(), keyPair3.Secret.ToBytes());
        }
コード例 #2
0
        /// <summary>
        /// Creates the asynchronous.
        /// </summary>
        /// <param name="password">The password.</param>
        /// <param name="mnemonic">The mnemonic.</param>
        /// <param name="walletName">Name of the wallet.</param>
        /// <returns></returns>
        public async Task <bool> CreateAsync(string password, string mnemonic, string walletName = DefaultWalletName)
        {
            if (IsCreated)
            {
                Logger.Warn("Wallet already created.");
                return(true);
            }

            if (!IsValidPassword(password))
            {
                Logger.Warn(
                    "Password isn't is invalid, please provide a proper password. Minmimu eight size and must have upper, lower and digits.");
                return(false);
            }

            Logger.Info("Creating new wallet from mnemonic.");

            var seed = Mnemonic.GetSecretKeyFromMnemonic(mnemonic, "Substrate", BIP39Wordlist.English);

            var randomBytes = new byte[48];

            _random.NextBytes(randomBytes);

            var memoryBytes = randomBytes.AsMemory();

            var pswBytes = Encoding.UTF8.GetBytes(password);

            var salt = memoryBytes.Slice(0, 16).ToArray();

            pswBytes = SHA256.Create().ComputeHash(pswBytes);

            var encryptedSeed =
                ManagedAes.EncryptStringToBytes_Aes(Utils.Bytes2HexString(seed, Utils.HexStringFormat.Pure), pswBytes, salt);

            var miniSecret = new MiniSecret(seed, ExpandMode.Ed25519);
            var getPair    = miniSecret.GetPair();

            var keyType = KeyType.Sr25519;

            _walletFile = new WalletFile(keyType, getPair.Public.Key, encryptedSeed, salt);

            Caching.Persist(AddWalletFileType(walletName), _walletFile);

            Account = Account.Build(keyType, getPair.Secret.ToBytes(), getPair.Public.Key);

            if (IsOnline)
            {
                _subscriptionAccountInfo = await SubscribeAccountInfoAsync();
            }

            return(true);
        }
コード例 #3
0
        public void MnemonicSeedAndMiniSecretTest()
        {
            foreach (var vector in Vectors)
            {
                var mnemonic         = vector[0];
                var expected_entropy = vector[1];
                var expected_seed    = vector[2];

                var entropy = Mnemonic.GetEntropy(mnemonic, BIP39Wordlist.English);
                Assert.AreEqual(expected_entropy, entropy);

                var seed            = Mnemonic.SeedFromEntropy(Utils.HexToByteArray(entropy), "Substrate");
                var seedAsHexString = Utils.Bytes2HexString(seed, Utils.HexStringFormat.Pure);
                Assert.AreEqual(expected_seed, seedAsHexString.ToLower());

                var miniSecret = Mnemonic.GetSecretKeyFromMnemonic(mnemonic, "Substrate", BIP39Wordlist.English);
                Assert.AreEqual(expected_seed.Substring(0, 64), Utils.Bytes2HexString(miniSecret, Utils.HexStringFormat.Pure).ToLower());
            }
        }