Ejemplo n.º 1
0
        public static Wallet RestoreWalletFromMnemonic(string mnemonicStr, Network env)
        {
            Mnemonic mnemonic = new Mnemonic(mnemonicStr);

            var seed = mnemonic.DeriveSeed();
            var pk   = new ExtKey(seed);
            var kp   = KeyPath.Parse(keyPath);
            var key  = pk.Derive(kp);

            var privKey = key.PrivateKey;
            var bytes   = privKey.ToBytes();

            Wallet w = new Wallet();

            w._env            = BinanceEnvironment.GetEnvironment(env);
            w._privateKey     = privKey;
            w._privateKeyStr  = BitConverter.ToString(bytes).Replace("-", string.Empty);
            w._publicKey      = w._privateKey.PubKey.Compress();
            w._publicKeyBytes = w._publicKey.ToBytes();
            w._AddressBytes   = w._publicKey.Hash.ToBytes();
            w._addressStr     = Bech32Engine.Encode(w.Env.Hrp, w._AddressBytes);


            w.Init();

            return(w);
        }
Ejemplo n.º 2
0
        public Wallet(string privateKey, Network env)
        {
            _env            = BinanceEnvironment.GetEnvironment(env);
            _privateKey     = new Key(Helpers.StringToByteArrayFastest(privateKey));
            _privateKeyStr  = privateKey;
            _publicKey      = _privateKey.PubKey.Compress();
            _publicKeyBytes = _publicKey.ToBytes();
            _AddressBytes   = _publicKey.Hash.ToBytes();
            _addressStr     = Bech32Engine.Encode(_env.Hrp, _AddressBytes);

            Init();
        }
Ejemplo n.º 3
0
        public static CreateRandomAccountResponse CreateRandomAccount(Network network)
        {
            var env      = BinanceEnvironment.GetEnvironment(network);
            var response = new CreateRandomAccountResponse();

            response.Network = network;
            Mnemonic mnemonic = new Mnemonic(Wordlist.English, WordCount.TwentyFour);

            response.Mnemonic = mnemonic.ToString();
            var seed    = mnemonic.DeriveSeed();
            var pk      = new ExtKey(seed);
            var kp      = KeyPath.Parse(keyPath);
            var key     = pk.Derive(kp);
            var privKey = key.PrivateKey;

            response.PrivateKey = BitConverter.ToString(privKey.ToBytes()).Replace("-", string.Empty);
            response.Address    = Bech32Engine.Encode(env.Hrp, privKey.PubKey.Hash.ToBytes());

            return(response);
        }
Ejemplo n.º 4
0
        public void TestValid()
        {
            string[] valid_checksum =
            {
                "A12UEL5L",
                "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs",
                "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
                "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j",
                "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
            };

            foreach (var encoded in valid_checksum)
            {
                string hrp;
                byte[] data;
                Bech32Engine.Decode(encoded, out hrp, out data);
                Assert.IsNotNull(data, "bech32_decode fails: {0}", encoded);

                var rebuild = Bech32Engine.Encode(hrp, data);
                Assert.IsNotNull(rebuild, "bech32_encode fails: {0}", encoded);
                Assert.AreEqual(encoded.ToLower(), rebuild, "bech32_encode produces incorrect result : {0}", encoded);
            }
        }
Ejemplo n.º 5
0
        private string GetBech32Address(byte[] pubKeyBytes, string hrp)
        {
            var addr = addressFromPublicKey(pubKeyBytes);

            return(Bech32Engine.Encode(hrp, addr));
        }