示例#1
0
        public static EcKey CreateEcKeyFromHexString(string privateKey)
        {
            var pkey = new BigInteger(privateKey, 16);
            var key  = new EcKey(pkey);

            return(key);
        }
        public static EcKey createEcKeyFromHexString(String privateKey)
        {
            BigInteger pkey = new BigInteger(privateKey, 16);
            EcKey      key  = new EcKey(pkey);

            return(key);
        }
        public static String deriveSIN(EcKey ecKey)
        {
            // Get sha256 hash and then the RIPEMD-160 hash of the public key (this call gets the result in one step).
            byte[] pubKeyHash = ecKey.PubKeyHash;

            // Convert binary pubKeyHash, SINtype and version to Hex
            String version       = "0F";
            String SINtype       = "02";
            String pubKeyHashHex = bytesToHex(pubKeyHash);

            // Concatenate all three elements
            String preSIN = version + SINtype + pubKeyHashHex;

            // Convert the hex string back to binary and double sha256 hash it leaving in binary both times
            byte[] preSINbyte = hexToBytes(preSIN);
            byte[] hash2Bytes = Utils.DoubleDigest(preSINbyte);

            // Convert back to hex and take first four bytes
            String hashString  = bytesToHex(hash2Bytes);
            String first4Bytes = hashString.Substring(0, 8);

            // Append first four bytes to fully appended SIN string
            String unencoded = preSIN + first4Bytes;

            byte[] unencodedBytes = new BigInteger(unencoded, 16).ToByteArray();
            String encoded        = Base58.Encode(unencodedBytes);

            return(encoded);
        }
        public static void saveEcKey(EcKey ecKey)
        {
            byte[]     bytes = ecKey.ToAsn1();
            FileStream fs    = new FileStream(GetPrivateKeyFullName(), FileMode.Create, FileAccess.Write);

            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
        }
示例#5
0
        public static void saveEcKey(EcKey ecKey, string path)
        {
            byte[]     bytes = ecKey.ToAsn1();
            FileStream fs    = new FileStream(path, FileMode.Create, FileAccess.Write);

            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
        }
        public static void saveEcKey(EcKey ecKey)
        {
            byte[]     bytes = ecKey.ToAsn1();
            FileStream fs    = new FileStream(PRIV_KEY_FILENAME, FileMode.Create, FileAccess.Write);

            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
        }
示例#7
0
        public static async Task <EcKey> LoadEcKey()
        {
            using (var fs = File.OpenRead(PrivateKeyFilename)) {
                var b = new byte[1024];
                await fs.ReadAsync(b, 0, b.Length);

                var key = EcKey.FromAsn1(b);
                return(key);
            }
        }
示例#8
0
        /// <summary>
        ///     Signs the input string with the provided key
        /// </summary>
        /// <param name="ecKey">The key object to sign with</param>
        /// <param name="input">The string to be signed</param>
        /// <returns>The signature</returns>
        public static string Sign(EcKey ecKey, string input)
        {
            // return ecKey.Sign(input);
            var hash      = Sha256Hash(input);
            var hashBytes = HexToBytes(hash);
            var signature = ecKey.Sign(hashBytes);
            var bytesHex  = BytesToHex(signature);

            return(bytesHex);
        }
 public static EcKey loadEcKey()
 {
     using (FileStream fs = File.OpenRead(GetPrivateKeyFullName()))
     {
         byte[] b = new byte[1024];
         fs.Read(b, 0, b.Length);
         EcKey key = EcKey.FromAsn1(b);
         return(key);
     }
 }
示例#10
0
 public static EcKey loadEcKey(string path)
 {
     using (FileStream fs = File.OpenRead(path))
     {
         byte[] b = new byte[1024];
         fs.Read(b, 0, b.Length);
         EcKey key = EcKey.FromAsn1(b);
         return(key);
     }
 }
 public static EcKey loadEcKey()
 {
     using (FileStream fs = File.OpenRead(PRIV_KEY_FILENAME))
     {
         byte[] b = new byte[1024];
         fs.Read(b, 0, b.Length);
         EcKey key = EcKey.FromAsn1(b);
         return(key);
     }
 }
示例#12
0
        /// <summary>
        /// Constructor for use if the keys and SIN were derived external to this library.
        /// </summary>
        /// <param name="ecKey">An elliptical curve key.</param>
        /// <param name="clientName">The label for this client.</param>
        /// <param name="envUrl">The target server URL.</param>
        public BitPay(EcKey ecKey, String clientName = BITPAY_PLUGIN_INFO, String envUrl = BITPAY_URL)
        {
            // IgnoreBadCertificates();

            _ecKey = ecKey;
            this.deriveIdentity();
            _baseUrl                = envUrl;
            _httpClient             = new HttpClient();
            _httpClient.BaseAddress = new Uri(_baseUrl);
            this.tryGetAccessTokens();
        }
示例#13
0
        public static async Task SaveEcKey(EcKey ecKey)
        {
            var bytes = ecKey.ToAsn1();

            if (!Directory.Exists(BitPay.TokensFolder))
            {
                Directory.CreateDirectory(BitPay.TokensFolder);
            }
            using (var fs = new FileStream(PrivateKeyFilename, FileMode.Create, FileAccess.Write)) {
                await fs.WriteAsync(bytes, 0, bytes.Length);
            }
        }
示例#14
0
 /// <summary>
 ///     Initialize the public/private key pair by either loading the existing one or by creating a new one
 /// </summary>
 /// <returns></returns>
 private async Task InitKeys()
 {
     if (KeyUtils.PrivateKeyExists(_configuration.GetSection("BitPayConfiguration:EnvConfig:" + _env + ":PrivateKeyPath").Value))
     {
         _ecKey = await KeyUtils.LoadEcKey();
     }
     else
     {
         _ecKey = KeyUtils.CreateEcKey();
         await KeyUtils.SaveEcKey(_ecKey);
     }
 }
示例#15
0
        public static async Task SaveEcKey(EcKey ecKey)
        {
            var bytes = ecKey.ToAsn1();

            if (!string.IsNullOrEmpty(Path.GetDirectoryName(PrivateKeyFile)) && !Directory.Exists(Path.GetDirectoryName(PrivateKeyFile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(PrivateKeyFile));
            }
            using (var fs = new FileStream(PrivateKeyFile, FileMode.Create, FileAccess.Write))
            {
                await fs.WriteAsync(bytes, 0, bytes.Length);
            }
        }
示例#16
0
        private void initKeys()
        {
            if (KeyUtils.privateKeyExists())
            {
                _ecKey = KeyUtils.loadEcKey();

                // Alternatively, load your private key from a location you specify.
                //_ecKey = KeyUtils.createEcKeyFromHexStringFile("C:\\Users\\Andy\\Documents\\private-key.txt");
            }
            else
            {
                _ecKey = KeyUtils.createEcKey();
                KeyUtils.saveEcKey(_ecKey);
            }
        }
示例#17
0
        /// <summary>
        /// Initialize the public/private key pair by either loading the existing one or by creating a new one
        /// </summary>
        /// <returns></returns>
        private async Task InitKeys()
        {
            if (KeyUtils.PrivateKeyExists())
            {
                _ecKey = await KeyUtils.LoadEcKey();

                // TODO: Alternatively, load your private key from a location you specify.
                //_ecKey = KeyUtils.createEcKeyFromHexStringFile("C:\\Users\\Andy\\Documents\\private-key.txt");
            }
            else
            {
                _ecKey = KeyUtils.CreateEcKey();
                await KeyUtils.SaveEcKey(_ecKey);
            }
        }
示例#18
0
        private void initKeys()
        {
            string path = HttpContext.Current.Server.MapPath("~/App_Start/bitpay_private.key");

            if (KeyUtils.privateKeyExists(path))
            {
                _ecKey = KeyUtils.loadEcKey(path);

                //  Alternatively, load your private key from a location you specify.
                //_ecKey = KeyUtils.createEcKeyFromHexStringFile(path);
            }
            else
            {
                _ecKey = KeyUtils.createEcKey();
                KeyUtils.saveEcKey(_ecKey, path);
            }
        }
示例#19
0
        public static string DeriveSin(EcKey ecKey)
        {
            if (_derivedSin != null)
            {
                return(_derivedSin);
            }
            // Get sha256 hash and then the RIPEMD-160 hash of the public key (this call gets the result in one step).
            var pubKey          = ecKey.PublicKey;
            var hash            = new SHA256Managed().ComputeHash(pubKey);
            var ripeMd160Digest = new RipeMD160Digest();

            ripeMd160Digest.BlockUpdate(hash, 0, hash.Length);
            var output = new byte[20];

            ripeMd160Digest.DoFinal(output, 0);

            var pubKeyHash = output;

            // Convert binary pubKeyHash, SINtype and version to Hex
            var version       = "0F";
            var siNtype       = "02";
            var pubKeyHashHex = BytesToHex(pubKeyHash);

            // Concatenate all three elements
            var preSin = version + siNtype + pubKeyHashHex;

            // Convert the hex string back to binary and double sha256 hash it leaving in binary both times
            var preSiNbyte = HexToBytes(preSin);
            var hash2Bytes = DoubleDigest(preSiNbyte);

            // Convert back to hex and take first four bytes
            var hashString  = BytesToHex(hash2Bytes);
            var first4Bytes = hashString.Substring(0, 8);

            // Append first four bytes to fully appended SIN string
            var unencoded      = preSin + first4Bytes;
            var unencodedBytes = new BigInteger(unencoded, 16).ToByteArray();
            var encoded        = Encode(unencodedBytes);

            _derivedSin = encoded;

            return(encoded);
        }
示例#20
0
 public static void saveEcKey(EcKey ecKey)
 {
     saveEcKey(ecKey, PRIV_KEY_FILENAME);
 }
        public static String sign(EcKey ecKey, String input)
        {
            String hash = sha256Hash(input);

            return(bytesToHex(ecKey.Sign(hexToBytes(hash))));
        }
示例#22
0
 /// <summary>
 /// Constructor for use if the keys and SIN were derived external to this library.
 /// </summary>
 /// <param name="ecKey">An elliptical curve key.</param>
 /// <param name="clientName">The label for this client.</param>
 /// <param name="envUrl">The target server URL.</param>
 public BitPay(EcKey ecKey, string clientName = BitpayPluginInfo, string envUrl = BitpayUrl)
 {
     _ecKey = ecKey;
     Init(clientName, envUrl).Wait();
 }