Esempio n. 1
0
        public static Byte[] ToByteArray(String s, out Byte version)
        {
            SHA256 sha256 = SHA256.Create();

            Byte[] b    = Base58String.ToByteArray(s);
            Byte[] hash = sha256.ComputeHash(sha256.ComputeHash(b.Take(b.Length - 4).ToArray()));
            if (!hash.Take(4).SequenceEqual(b.Skip(b.Length - 4).Take(4)))
            {
                throw new ArgumentException("Invalid Base58Check String");
            }
            version = b.First();
            return(b.Skip(1).Take(b.Length - 5).ToArray());
        }
Esempio n. 2
0
        private byte[] parseKey(string key)
        {
            SHA256 sha = SHA256.Create();

            byte[] decoded = Base58String.ToByteArray(key);

            if (decoded.Length != 82) throw new Exception("Not enough data");

            byte[] checksum = decoded.Skip(78).Take(4).ToArray();

            byte[] bytes = decoded.Take(78).ToArray();

            byte[] hash = sha.ComputeHash(sha.ComputeHash(bytes));

            if (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3])
            {
                throw new Exception("Invalid checksum");
            }

            return bytes;
        }
Esempio n. 3
0
        public string ExtendedPrivateKeyString(string format)
        {

            SHA256 sha = SHA256.Create();

            if (format == null || format == "base58")
            {
                byte[] hash = sha.ComputeHash(sha.ComputeHash(this.extended_private_key));
                byte[] checksum = hash.Take(4).ToArray();
                byte[] data = this.extended_private_key.Concat(checksum).ToArray();

                return Base58String.FromByteArray(data);

            }
            else if (format == "hex")
            {
                return HexString.FromByteArray(this.extended_private_key);
            }
            else
            {
                throw new Exception("bad format");
            }
        }
Esempio n. 4
0
        public BIP32(string sKey, secp256k1Wrap psecp256k1 )
        {
            secp256k1 = psecp256k1;

            SHA256 sha = SHA256.Create();

            byte[] decoded = Base58String.ToByteArray(sKey);

            if (decoded.Length != 82) throw new Exception("Not enough data");

            byte[] checksum = decoded.Skip(78).Take(4).ToArray();

            byte[] bytes = decoded.Take(78).ToArray();

            byte[] hash = sha.ComputeHash(sha.ComputeHash(bytes));

            if (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3])
            {
                throw new Exception("Invalid checksum");
            }

            Initialise(bytes);
        }
Esempio n. 5
0
        public static string MultiSig2Of3TransactionForTesting(string publickey1, string publickey2, string publickey3, string privKey1, string privKey2, string transactionid, uint prevoutIndex, string toAddress, ulong amount)
        {
            TxIn[]  txins  = new TxIn[1];
            TxOut[] txouts = new TxOut[1];

            byte version = 0;

            //PayToAddress
            HashType hashType = HashType.SIGHASH_ALL;

            PublicKey pk = new PublicKey(HexString.ToByteArray(toAddress));
            //Address add = new Address(pk.);

            //address to send coins to
            // string dec = HexString.FromByteArray(Base58CheckString.ToByteArray(add.ToString(), out version));


            //create address

            //create script to pay to address
            Script sc = ScriptTemplate.PayToAddress(pk.address);

            //create transaction output
            TxOut tout = new TxOut(amount - 5, sc.ToBytes());

            txouts[0] = tout;

            object[] addresses = new object[3];

            addresses[0] = publickey1;
            addresses[1] = publickey2;
            addresses[2] = publickey3;

            string mscript = MultSigScript(addresses);


            //create input based on previous output to spend
            //and the redeem script for the multi-sig address
            TxIn tin = new TxIn(HexString.ToByteArrayReversed(transactionid), prevoutIndex, HexString.ToByteArray(mscript));

            txins[0] = tin;

            //basically the redeem script
            Script subScript = new Script(tin.scriptSig);

            //create the new transaction to spend the coins at the multisig address
            Transaction tx = new Transaction(1, txins, txouts, 0);

            //clone the transaction
            Transaction txCopy = new Transaction(tx.ToBytes());

            SHA256 sha256 = SHA256.Create();

            //hash the transaction with some leading bytes
            Byte[] txHash = txCopy.ToBytes().Concat(new Byte[] { (Byte)hashType, 0x00, 0x00, 0x00 }).ToArray();
            txHash = sha256.ComputeHash(sha256.ComputeHash(txHash));

            //get the private keys bytes 1-33 and base58
            byte[]     test58 = Base58String.ToByteArray(privKey1).Skip(1).Take(32).ToArray();
            ECKeyPair  key1   = new ECKeyPair(test58, null, false);
            PrivateKey pk1    = new PrivateKey(key1);

            byte[]     test582 = Base58String.ToByteArray(privKey2).Skip(1).Take(32).ToArray();
            ECKeyPair  key2    = new ECKeyPair(test582, null, false);
            PrivateKey pk2     = new PrivateKey(key2);



            //Script scriptSig = new Script(tin.scriptSig);

            //sign the transaction hash
            Byte[] sig  = pk1.Sign(txHash).Concat(new Byte[] { (Byte)hashType }).ToArray();
            Byte[] sig2 = pk2.Sign(txHash).Concat(new Byte[] { (Byte)hashType }).ToArray();

            //create a new script to sign
            Script s = new Script(new Byte[] { 0x00 });

            //add the signatures to the script
            s.elements.Add(new ScriptElement(sig));
            s.elements.Add(new ScriptElement(sig2));

            //txtResult.Text = HexString.FromByteArray(tx.ToBytes());

            //add the original multisig script
            s.elements.Add(new ScriptElement(tin.scriptSig));

            //overwrite the script on the transaction with the new signatures + script
            tin.scriptSig = s.ToBytes();

            return(HexString.FromByteArray(tx.ToBytes()));
        }