///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private void initKeys() { if (ResourcesDataAccess.FileExists())//load from storage { Debug.Log("initKeys():privateKeyExists loading eckey"); // _ecKey = KeyUtils.loadEcKey(); _ecKey = KeyUtils.loadNBEcKey(); // Alternatively, load your private key from a location you specify. //_ecKey = KeyUtils.createEcKeyFromHexStringFile("C:\\Users\\Andy\\Documents\\private-key.txt"); } else//create a new private key and store { Debug.Log("initKeys(): Not privateKeyExists creating eckey"); //string privHexStr2 = "f356e8785923053e46422ad2c22fcb4f03f2cd0e6e33265bac16e314a7c26684"; //0.hard coded privkey //byte[] bytes2 = KeyUtils.hexToBytes(privHexStr2); //_ecKey = new Key(bytes2, -1, false) // _ecKey = KeyUtils.createEcKey(); //1.old ECkey _ecKey = KeyUtils.createNBEcKey(); //2.Nbitcoin eckey byte[] priv = _ecKey.ToBytes(); //Debug.Log("initKeys(): ecKey:" + KeyUtils.bytesToHex(priv)); KeyUtils.saveEcKey(_ecKey); //3.save geenrated one } Debug.Log("initKeys(): End"); }
//public static EcKey loadEcKey() //{ // using (FileStream fs = File.OpenRead(PRIV_KEY_FILENAME)) // { // byte[] b = new byte[1024]; // fs.Read(b, 0, b.Length); // string base58 = new Base58Encoding().Encode(b); // Debug.Log("loadEcKey():Base58 of prvkey:" + base58); // EcKey key = EcKey.FromAsn1(b); // return key; // } //} public static Key loadNBEcKey() { string base58 = ResourcesDataAccess.Load(); byte[] b = new Base58Encoding().Decode(base58); EcKey key = EcKey.FromAsn1(b); return(new Key(key.GetPrivKeyBytes())); }
//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(); //} public static void saveEcKey(Key nbEcKey) { Debug.Log("saveEcKey():start"); byte[] bytes = EcKey.ToAsn1(nbEcKey.ToBytes(), nbEcKey.PubKey.ToBytes()); string base58 = new Base58Encoding().Encode(bytes); ResourcesDataAccess.Save(base58); Debug.Log("saveEcKey():end"); }