示例#1
0
        public static string GetMnemonic(PrivateKeyNotWallet key, Wordlist wordlist)
        {
            var mnemonic = new Mnemonic(wordlist, key.PrivateKey.GetBytes());

            Debug.Assert(mnemonic.Words.Length == 24);
            return(mnemonic.ToString());
        }
示例#2
0
        public static PrivateKeyNotWallet Decrypt(string encoded, string password, Network network)
        {
            var decoded = Encoders.Base58Check.DecodeData(encoded);

            var version     = decoded.SafeSubarray(0, 1);
            var flag        = decoded.SafeSubarray(1, 1);
            var addresshash = decoded.SafeSubarray(2, 4);
            var encrypted   = decoded.SafeSubarray(6, 32);

            var expected = network.GetVersionBytes(Network.VersionByte.BIP38_ENCRYPTED_PRIVATE_KEY);

            if (!Utils.ArrayEqual(version, expected))
            {
                throw new ArgumentException("Invalid version");
            }
            if (!Utils.ArrayEqual(flag, new[] { FLAG }))
            {
                throw new ArgumentException("Invalid flag");
            }

            var derived  = SCrypt.BitcoinComputeDerivedKey(Encoding.UTF8.GetBytes(password), addresshash);
            var keyBytes = Bip38Engine.DecryptKey(encrypted, derived);

            var key = PrivateKeyNotWallet.FromBytes(keyBytes);

            var addressBytes = Encoders.ASCII.DecodeData(key.PublicKey.GetAddress().Encoded);
            var salt         = Hashes.Hash256(addressBytes).ToBytes().SafeSubarray(0, 4);

            if (!Utils.ArrayEqual(salt, addresshash))
            {
                throw new ArgumentException("Invalid password (or invalid Network)");
            }

            return(key);
        }
        public static Signature CreateSignature(this PrivateKeyNotWallet account, Hash256 hash, Network network)
        {
            var signature = account.PrivateKey.SignMessage(hash.Bytes, network);

            Debug.Assert(account.PublicKey.Verify(hash, signature, network));
            return(new Signature(account.PublicKey, signature));
        }
        public Transaction CreateMultiSignatureTransaction(out PrivateKeyNotWallet account1, out PrivateKeyNotWallet account2, out PrivateKeyNotWallet account3, out PrivateKeyNotWallet account4)
        {
            account1 = CreateAccount();
            account2 = CreateAccount();
            account3 = CreateAccount();
            account4 = CreateAccount();

            var multi = new MultiSignature(new List <Address> {
                account1, account2, account3
            }, 2);

            var declarations = new List <TxDeclaration>()
            {
                multi
            };

            var inputs = new List <TxInput>()
            {
                new TxInput(multi.Address, Currency.BTC, Amount.FromWholeValue(10)),
            };

            var outputs = new List <TxOutput>()
            {
                new TxOutput(account4, Currency.BTC, Amount.FromWholeValue(10)),
            };

            return(new Transaction(declarations, inputs, outputs, TransactionMessage.Empty, DateTime.UtcNow.Ticks));
        }
        private void EncryptDecrypt(PrivateKeyNotWallet wallet, string password)
        {
            var network   = TestNetwork.Instance;
            var encrypted = EncryptedPrivateKeyFormat.Encrypt(wallet, password, network);
            var decrypted = EncryptedPrivateKeyFormat.Decrypt(encrypted, password, network);

            Assert.IsTrue(Utils.ArrayEqual(decrypted.PrivateKey.GetBytes(), wallet.PrivateKey.GetBytes()));
        }
示例#6
0
        public static string GetMnemonicFromEncrypted(PrivateKeyNotWallet key, Wordlist wordlist, string password, Network network)
        {
            var encryptedString = EncryptedPrivateKeyFormat.Encrypt(key, password, network);
            var encrypted       = Encoders.Base58Check.DecodeData(encryptedString);
            var mnemonic        = new Mnemonic(wordlist, encrypted);

            Debug.Assert(mnemonic.Words.Length == 28);
            return(mnemonic.ToString());
        }
示例#7
0
        public static void Dump(this PrivateKeyNotWallet wallet)
        {
            var privkey = wallet.PrivateKey;
            var pubkey  = wallet.PublicKey;
            var address = pubkey.GetAddress();

            Console.WriteLine("Private Key : {0}", privkey.ToBase64());
            Console.WriteLine("Public Key : {0}", pubkey.ToBase64());
            Console.WriteLine("Address : {0}", address.Encoded);
        }
示例#8
0
        protected override void ExecuteCommand(string[] args)
        {
            var wallet = PrivateKeyNotWallet.FromBase64(privatekeyArgument.Value);
            var alias  = aliasArgument.Value;

            if (WalletService.ImportPrivateKey(alias, wallet))
            {
                Console.WriteLine($"Successfully imported key : {alias}");
            }
        }
示例#9
0
        protected override void ExecuteCommand(string[] args)
        {
            var privatekey = privatekeyArgument.Value;
            var hash       = hashArgument.Value;

            var wallet = PrivateKeyNotWallet.FromBase64(privatekey);

            var signature = wallet.CreateSignature(new Hash256(Convert.FromBase64String(hash)), LedgerService.LedgerManager.Network);

            Console.WriteLine("Signature : {0}", Convert.ToBase64String(signature.SignatureByte.Bytes));
        }
示例#10
0
        public void TestMnemonicRestore_Language(Wordlist wordlist)
        {
            foreach (var key in keys)
            {
                var account = PrivateKeyNotWallet.FromBase64(key);

                var words    = MnemonicPrivateKeyFormat.GetMnemonic(account, wordlist);
                var restored = MnemonicPrivateKeyFormat.FromMnemonic(words);
                Assert.IsTrue(account.PrivateKey.Equals(restored));
            }
        }
示例#11
0
        public static PrivateKeyNotWallet GeneratePrivateKey()
        {
            var gen         = new ECKeyPairGenerator("EC");
            var keyGenParam = new KeyGenerationParameters(new SecureRandom(), 256);

            gen.Init(keyGenParam);
            var keyPair      = gen.GenerateKeyPair();
            var privateBytes = ((ECPrivateKeyParameters)keyPair.Private).D.ToByteArray();

            return(PrivateKeyNotWallet.FromBytes(privateBytes));
        }
 public void TestEncryptDecrypt()
 {
     foreach (var key in keys)
     {
         var wallet = PrivateKeyNotWallet.FromBase64(key);
         EncryptDecrypt(wallet, "I am the real S4t0sh1 !");
         EncryptDecrypt(wallet, "O RLY ?");
         EncryptDecrypt(wallet, "Just kidding :D");
         EncryptDecrypt(wallet, "What a @#$@$#!#$!#$@!#@#!^$^@ !");
         EncryptDecrypt(wallet, "乱七八糟");
     }
 }
示例#13
0
        public bool ImportPrivateKey(string label, PrivateKeyNotWallet key)
        {
            var aliased = AliasManager.SetAlias(label, key);
            var address = key.Address.Encoded;

            if (wallets.ContainsKey(label))
            {
                return(false);
            }
            wallets.Add(address, aliased);
            AddressListener.Listen(key.Address);
            return(true);
        }
示例#14
0
        protected override void ExecuteCommand(string[] args)
        {
            var privatekey = privateKeyArgument.Value;
            var wallet     = PrivateKeyNotWallet.FromBase64(privatekey);
            var password   = passwordArgument.Value;

            var encrypted = EncryptedPrivateKeyFormat.Encrypt(wallet, password, LedgerService.LedgerManager.Network);
            var decrypted = EncryptedPrivateKeyFormat.Decrypt(encrypted, password, LedgerService.LedgerManager.Network);

            Debug.Assert(decrypted.PrivateKey.ToBase64() == privatekey);

            Console.WriteLine("Success ! Encrypted : {0}", encrypted);
        }
示例#15
0
        public void TestMnemonic_CreateFromEncrypted_RestoreFromEncrypted()
        {
            foreach (var key in keys)
            {
                var account = PrivateKeyNotWallet.FromBase64(key);

                var password = "******";

                var words    = MnemonicPrivateKeyFormat.GetMnemonicFromEncrypted(account, Wordlist.English, password, Network);
                var restored = MnemonicPrivateKeyFormat.FromMnemonicEncrypted(words, password, Network);
                Assert.IsTrue(account.PrivateKey.Equals(restored));
            }
        }
        private SignedLedger CreateSignedLedger()
        {
            var account1 = PrivateKeyNotWallet.FromBase64("AKiWI3xivi2tsMz1Sh/v+0WrJaM60t/3h/qcEfu6r1pH");
            var block    = Block.CreateBlock(1, new List <SignedTransaction> {
                CreateSignedTransaction()
            });
            var merkle = new LedgerMerkleRoot(new List <Account>(), new List <TxDeclaration>(), new FakeLogger(), new Hasher()).Hash;
            var ledger = new Ledger(new LedgerLight(1, DateTime.UtcNow.ToUnixTimestamp(), new LedgerHash(Hash256.Zero.Bytes), new ProtocolVersion(0x1)), block, merkle);
            var signed = new SignedLedger(ledger);
            var hash   = signed.Hash;

            signed.AddSignature(account1.CreateSignature(hash, Network));
            return(signed);
        }
        public void TestKeyHashCode()
        {
            var dictionary = new Dictionary <PublicKey, PrivateKeyNotWallet>();

            foreach (var key in keys)
            {
                var account = PrivateKeyNotWallet.FromBase64(key);
                Assert.IsFalse(dictionary.ContainsKey(account));
                dictionary.Add(account, account);
                Assert.IsTrue(dictionary.ContainsKey(account));

                var copy = PrivateKeyNotWallet.FromBase64(key);
                Assert.IsTrue(dictionary.ContainsKey(copy));
            }
        }
        public void TestSignatureVerify()
        {
            // get the private key
            foreach (var key in keys)
            {
                var account = PrivateKeyNotWallet.FromBase64(key);

                var text = "Guillaume is the best !";
                var hash = Hashes.Hash256(Encoding.UTF8.GetBytes(text));


                var signature = account.CreateSignature(hash, Network);
                Assert.IsTrue(signature.PublicKey.Verify(hash, signature.SignatureByte, Network));
            }
        }
        private Transaction CreatePaymentTransaction(out PrivateKeyNotWallet account1, out PrivateKeyNotWallet account2)
        {
            account1 = CreateAccount();
            account2 = CreateAccount();

            var declarations = new List <TxDeclaration>();

            var inputs = new List <TxInput>()
            {
                new TxInput(account1, Currency.BTC, Amount.FromWholeValue(10)),
            };

            var outputs = new List <TxOutput>()
            {
                new TxOutput(account2, Currency.BTC, Amount.FromWholeValue(10)),
            };

            return(new Transaction(declarations, inputs, outputs, TransactionMessage.Empty, DateTime.UtcNow.Ticks));
        }
示例#20
0
        public static Transaction CreateExchangeTransaction(this TestBase test, out PrivateKeyNotWallet account1, out PrivateKeyNotWallet account2)
        {
            account1 = test.CreateAccount();
            account2 = test.CreateAccount();

            var declarations = new List <TxDeclaration>();

            var inputs = new List <TxInput>()
            {
                new TxInput(account1, Currency.BTC, Amount.FromWholeValue(10)),
                new TxInput(account2, Currency.LTC, Amount.FromWholeValue(30)),
            };

            var outputs = new List <TxOutput>()
            {
                new TxOutput(account2, Currency.BTC, Amount.FromWholeValue(10)),
                new TxOutput(account1, Currency.LTC, Amount.FromWholeValue(30)),
            };

            return(new Transaction(declarations, inputs, outputs, TransactionMessage.Empty, DateTime.UtcNow.Ticks));
        }
示例#21
0
        public static string Encrypt(PrivateKeyNotWallet key, string password, Network network)
        {
            var version = network.GetVersionBytes(Network.VersionByte.BIP38_ENCRYPTED_PRIVATE_KEY);
            var vch     = key.PrivateKey.GetBytes();
            //Compute the Bitcoin address (ASCII),
            var addressBytes = Encoders.ASCII.DecodeData(key.PublicKey.GetAddress().Encoded);
            // and take the first four bytes of SHA256(SHA256()) of it. Let's call this "addresshash".
            var addresshash = Hashes.Hash256(addressBytes).ToBytes().SafeSubarray(0, 4);

            var derived = SCrypt.BitcoinComputeDerivedKey(Encoding.UTF8.GetBytes(password), addresshash);

            var encrypted = Bip38Engine.EncryptKey(vch, derived);

            //flagByte |= (key.IsCompressed ? (byte)0x20 : (byte)0x00); // WTF OR 0x00 is IDENTITY

            var bytes = version
                        .Concat(new[] { FLAG })
                        .Concat(addresshash)
                        .Concat(encrypted).ToArray();

            return(Encoders.Base58Check.EncodeData(bytes));
        }
示例#22
0
 protected SignedTransaction Transfer(PrivateKeyNotWallet sender, Address receiver, Currency currency, Amount amount, PrivateKeyNotWallet feePayer = null, long?fees = null, List <TxDeclaration> declarations = null, TransactionMessage message = null)
 {
     return(Transfer(sender, new List <PrivateKeyNotWallet> {
         sender
     }, receiver, currency, amount, declarations, feePayer, fees, message));
 }
示例#23
0
 // account 1 is vending machine
 protected SignedTransaction Exchange(Address account1, Currency currency1, Amount amount1, PrivateKeyNotWallet account2, Currency currency2, Amount amount2)
 {
     return(Exchange(account1, currency1, amount1, account2, currency2, amount2, account2));
 }
 public static bool SignLedger(this PrivateKeyNotWallet account, SignedLedger signed, Network network)
 {
     return(signed.AddSignature(account.CreateSignature(signed.Hash, network)));
 }
 public static bool SignTransaction(this PrivateKeyNotWallet account, SignedTransaction signed, Network network)
 {
     return(signed.AddSignature(account.CreateSignature(signed.Hash, network)));
 }
示例#26
0
        protected SignedTransaction Exchange(Address account1, Currency currency1, Amount amount1, Address account2, Currency currency2, Amount amount2, PrivateKeyNotWallet signer)
        {
            var inputs = new List <TxInput>()
            {
                new TxInput(account1, currency1, amount1),
                new TxInput(account2, currency2, amount2),
            };

            var outputs = new List <TxOutput>()
            {
                new TxOutput(account2, currency1, amount1),
                new TxOutput(account1, currency2, amount2),
            };

            var transaction = new Transaction(new List <TxDeclaration>(), inputs, outputs, null, DateTime.UtcNow.AddMinutes(1).ToUnixTimestamp());

            var signed = new SignedTransaction(transaction);

            Assert.IsTrue(signer.SignTransaction(signed, Network));

            return(signed);
        }
示例#27
0
 public PrivateKeyNotWallet CreateAccount()
 {
     return(PrivateKeyNotWallet.FromBase64(keys[count++]));
 }
示例#28
0
        protected SignedTransaction Transfer(Address sender, List <PrivateKeyNotWallet> signers, Address receiver, Currency currency, Amount amount, List <TxDeclaration> declarations = null, PrivateKeyNotWallet feePayer = null, long?fees = null, TransactionMessage message = null)
        {
            if (message == null)
            {
                message = TransactionMessage.Empty;
            }
            var inputs = new List <TxInput>()
            {
                new TxInput(sender, currency, amount),
            };

            var outputs = new List <TxOutput>()
            {
                new TxOutput(receiver, currency, amount),
            };

            var feeTxInput = fees == null ? null : new TxInput(feePayer, Currency.BTC, fees);

            var transaction = new Transaction(declarations ?? new List <TxDeclaration>(), inputs, outputs, message, DateTime.UtcNow.AddMinutes(1).ToUnixTimestamp(), feeTxInput);

            var signed = new SignedTransaction(transaction);

            foreach (var signer in signers)
            {
                Assert.IsTrue(signer.SignTransaction(signed, Network));
            }

            if (feePayer != null && feePayer.Address != sender)
            {
                Assert.IsTrue(feePayer.SignTransaction(signed, Network));
            }

            return(signed);
        }