/* * * Next Private Key = hash( current public key + seed + currentIndex ) */ private Wallet next(byte[] seed) { byte[] currentIndexByte = BitConverter.GetBytes(currentIndex); byte[] beforehash = new byte[keyPair.PublicKey.Length + seed.Length + currentIndexByte.Length]; System.Buffer.BlockCopy(keyPair.PublicKey, 0, beforehash, 0, keyPair.PublicKey.Length); System.Buffer.BlockCopy(seed, 0, beforehash, keyPair.PublicKey.Length, seed.Length); System.Buffer.BlockCopy(currentIndexByte, 0, beforehash, keyPair.PublicKey.Length + seed.Length, currentIndexByte.Length); byte[] nextPrivKey = Hash.ComputeDoubleSHA256(beforehash); byte[] nextPubKeyByte; EccService.GenerateKeyFromPrivateKey(nextPrivKey, out nextPubKeyByte); KeyPair nextKeyPair = new KeyPair { PrivateKey = nextPrivKey, PublicKey = nextPubKeyByte, Address = BlockchainUtil.ToAddress(nextPubKeyByte), }; return(new Wallet(nextKeyPair, currentIndex + 1)); }
static String Encode(byte[] publickKey, int shiftNum){ //should use hash var seed = System.Text.Encoding.ASCII.GetString(publickKey); string seedWithShiftnum = seed + shiftNum; var it = System.Text.Encoding.ASCII.GetBytes(seedWithShiftnum); var it2 = BlockchainUtil.ToAddress(it); var it3 = System.Text.Encoding.ASCII.GetString(it2); var it4 = Base64Encode(it3); Console.WriteLine(it4); return it4; }
public static void Exec(string[] args) { byte[] publicKey; byte[] privateKey; EccService.GenerateKey(out privateKey, out publicKey); var json = JsonConvert.SerializeObject( new KeyPair { PrivateKey = privateKey, PublicKey = publicKey, Address = BlockchainUtil.ToAddress(publicKey), }, Formatting.Indented); Console.WriteLine(json); }
public void Run(Transaction tx, DateTime blockTime, ulong coinbase = 0, List <TransactionOutput> spentTxo = null) { logger.LogDebug($@"Attempt to run TX:{ tx.Id.ToString().Substring(0, 7)}"); // Transaction header validity check. if (tx.Timestamp > blockTime || !(coinbase == 0 ^ tx.InEntries.Count == 0)) { throw new ArgumentException(); } // In-Entry validity check. ulong inSum = coinbase; var redeemed = new List <TransactionOutput>(); var signHash = BlockchainUtil.GetTransactionSignHash(tx.Original); foreach (var inEntry in tx.InEntries) { // Signature check. var verified = EccService.Verify( signHash, inEntry.Signature, inEntry.PublicKey); // UTXO check. The transaction output must not be spent by // previous transactions. var txo = new TransactionOutput { TransactionId = inEntry.TransactionId, OutIndex = inEntry.OutEntryIndex, }; var unspent = !(spentTxo?.Contains(txo) ?? false) && Utxos.TryGetValue(txo, out txo); // Recipient address check. var addr = BlockchainUtil.ToAddress(inEntry.PublicKey); var redeemable = txo.Recipient.Equals( ByteString.CopyFrom(addr)); // Sum all the reedemable. inSum = checked (inSum + txo.Amount); if (!verified || !unspent || !redeemable) { throw new ArgumentException(); } redeemed.Add(txo); } // Out-entry validity check. ulong outSum = 0; ushort outIndex = 0; var generated = new List <TransactionOutput>(); foreach (var outEntry in tx.OutEntries) { if (outEntry.RecipientHash.IsNull() || outEntry.Amount <= 0) { throw new ArgumentException(); } // Sum all the transferred. outSum = checked (outSum + outEntry.Amount); // Create new UTXO entry. generated.Add(new TransactionOutput { TransactionId = tx.Id, OutIndex = outIndex++, Recipient = outEntry.RecipientHash, Amount = outEntry.Amount, }); } // Output exceeds input or coinbase. if (outSum > inSum) { throw new ArgumentException(); } tx.ExecInfo = new TransactionExecInformation { Coinbase = coinbase != 0, RedeemedOutputs = redeemed, GeneratedOutputs = generated, TransactionFee = inSum - outSum, }; }
public static String SeedToAddress(byte[] publicKey, Func<String, String> encodeFn){ var pk = System.Text.Encoding.ASCII.GetString(publicKey); var enk = System.Text.Encoding.ASCII.GetBytes( encodeFn(pk) ); var addr = System.Text.Encoding.ASCII.GetString( BlockchainUtil.ToAddress(enk) ); return addr;// hash }