public void TxDeclarationTest() { var account1 = CreateAccount(); var account2 = CreateAccount(); var secret = Secret.GenerateSecret(); var declarations = new List <TxDeclaration>() { new MultiSignature(new [] { account1.Address, account2.Address }, 2), new HashLock(secret.ComputeSecretHash(SecretHashType.SHA3)), new TimeLock(123), new SecretRevelation(secret) }; 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)), }; var transaction = new Transaction(declarations, inputs, outputs, TransactionMessage.Empty, DateTime.UtcNow.Ticks); var signed = new SignedTransaction(transaction); var readed = WriteRead <SignedTransaction, ByteStream>(stream => stream.Write(signed), stream => stream.ReadSignedTransaction()); Assert.IsTrue(signed.CompareSigned(readed)); }
protected override void ExecuteCommand(string[] args) { var type = SecretHashType.SHA3; var secret = Secret.GenerateSecret(); var hash = secret.ComputeSecretHash(type); Console.WriteLine($"Secret {Convert.ToBase64String(secret.Bytes)}"); Console.WriteLine($"Hash {hash.Hash.ToBase64()}"); Console.WriteLine($"Type {type}"); }
public void Transaction_DuplicateDeclarations() { using (var context = CreateContext()) { var issuer = BTC_ISSUER; var signers = new List <Address>() { CreateAccount().Address, CreateAccount().Address }; var multi = new MultiSignature(signers, 2); // Check multisig { var signed = Transfer(issuer, multi.Address, Currency.BTC, 1, null, null, new List <TxDeclaration> { multi, multi }); context.SendTransaction(signed, ResultCode.TransactionValidationFailed); } // create timelock var timelock = new TimeLock(DateTime.Now.AddDays(-1).ToUnixTimestamp()); // Check timelocks { var signed = Transfer(issuer, timelock.Address, Currency.BTC, 1, null, null, new List <TxDeclaration> { timelock, timelock }); context.SendTransaction(signed, ResultCode.TransactionValidationFailed); } var secret = Secret.GenerateSecret(); var hashlock = new HashLock(secret.ComputeSecretHash(SecretHashType.SHA256)); // Check hashLock { var signed = Transfer(issuer, hashlock.Address, Currency.BTC, 1, null, null, new List <TxDeclaration> { hashlock, hashlock }); context.SendTransaction(signed, ResultCode.TransactionValidationFailed); } var secretRevelation = new SecretRevelation(secret); // Check Secret { var signed = Transfer(issuer, hashlock.Address, Currency.BTC, 1, null, null, new List <TxDeclaration> { secretRevelation, secretRevelation }); context.SendTransaction(signed, ResultCode.TransactionValidationFailed); } } }
public void TestHashTimeLock() { // TODO CANCEL sender + receiver // we can emulate hashtime lock behaviour using (var context = CreateContext()) { var issuer = BTC_ISSUER; var sender = CreateAccount(); var receiver = CreateAccount(); var timeunlocked = new TimeLock(DateTime.Now.AddDays(-1).ToUnixTimestamp()); var timelocked = new TimeLock(DateTime.Now.AddDays(1).ToUnixTimestamp()); // TODO we are missing Hash160 : SHA256 + ripemd160 // create secret hash var type = SecretHashType.SHA3; var secret = Secret.GenerateSecret(); var hash = secret.ComputeSecretHash(type); var hashlock = new HashLock(hash); var revelation = new SecretRevelation(secret); // CLAIM receiver + hashlock var claim = new MultiSignature(new List <Address>() { receiver.Address, hashlock.Address }, 2); // TIMEOUT sender + timelock var timeoutlocked = new MultiSignature(new List <Address>() { sender.Address, timelocked.Address }, 2); var timeoutunlocked = new MultiSignature(new List <Address>() { sender.Address, timeunlocked.Address }, 2); var hashtimelocked = new MultiSignature(new List <Address>() { claim.Address, timeoutlocked.Address }, 1); var hashtimeunlocked = new MultiSignature(new List <Address>() { claim.Address, timeoutunlocked.Address }, 1); // send money to hashtimelocked account { var signed = Transfer(issuer, hashtimelocked.Address, Currency.BTC, 10, null, null, new List <TxDeclaration>() { hashtimelocked }); context.SendTransaction(signed); Assert.IsTrue(context.TryCreateNextLedger()); Assert.IsTrue(context.TryGetAccount(issuer.Address.Encoded, out var issuerAccount)); Assert.IsTrue(context.TryGetAccount(hashtimelocked.Address.Encoded, out var hashtimelockedAccount)); // check that the money has been sent Assert.IsTrue(issuerAccount.GetBalance(Currency.BTC) == -10); Assert.IsTrue(hashtimelockedAccount.GetBalance(Currency.BTC) == 10); } // try timeout from hashtimelocked { var signed = Transfer(hashtimelocked.Address, new List <PrivateKeyNotWallet>() { sender }, sender, Currency.BTC, 10, new List <TxDeclaration>() { timelocked, timeoutlocked }); context.SendTransaction(signed, ResultCode.TransactionValidationFailed); } // claim from hashtimelocked { var signed = Transfer(hashtimelocked.Address, new List <PrivateKeyNotWallet>() { receiver }, issuer.Address, Currency.BTC, 10, new List <TxDeclaration>() { hashlock, claim, revelation }); context.SendTransaction(signed); Assert.IsTrue(context.TryCreateNextLedger()); // update data Assert.IsTrue(context.TryGetAccount(issuer.Address.Encoded, out var issuerAccount)); Assert.IsTrue(context.TryGetAccount(hashtimelocked.Address.Encoded, out var hashtimelockedAccount)); // check that the money has been sent Assert.IsTrue(issuerAccount.GetBalance(Currency.BTC) == 0); Assert.IsTrue(hashtimelockedAccount.GetBalance(Currency.BTC) == 0); } // send money to hashtimeunlocked account { var signed = Transfer(issuer, hashtimeunlocked.Address, Currency.BTC, 10, null, null, new List <TxDeclaration>() { hashtimeunlocked }); context.SendTransaction(signed); Assert.IsTrue(context.TryCreateNextLedger()); Assert.IsTrue(context.TryGetAccount(issuer.Address.Encoded, out var issuerAccount)); Assert.IsTrue(context.TryGetAccount(hashtimeunlocked.Address.Encoded, out var hashtimeunlockedAccount)); // check that the money has been sent Assert.IsTrue(issuerAccount.GetBalance(Currency.BTC) == -10); Assert.IsTrue(hashtimeunlockedAccount.GetBalance(Currency.BTC) == 10); } // timeout from hashtimeunlocked { var signed = Transfer(hashtimeunlocked.Address, new List <PrivateKeyNotWallet>() { sender }, issuer.Address, Currency.BTC, 10, new List <TxDeclaration>() { timeunlocked, timeoutunlocked }); context.SendTransaction(signed); Assert.IsTrue(context.TryCreateNextLedger()); // update data Assert.IsTrue(context.TryGetAccount(issuer.Address.Encoded, out var issuerAccount)); Assert.IsTrue(context.TryGetAccount(hashtimeunlocked.Address.Encoded, out var hashtimeunlockedAccount)); // check that the money has been sent Assert.IsTrue(issuerAccount.GetBalance(Currency.BTC) == 0); Assert.IsTrue(hashtimeunlockedAccount.GetBalance(Currency.BTC) == 0); } } }
public void TestHashLock() { using (var context = CreateContext()) { var sender = BTC_ISSUER; // create secret hash var type = SecretHashType.SHA3; var secret = Secret.GenerateSecret(); var hash = secret.ComputeSecretHash(type); var hashlock = new HashLock(hash); var revelation = new SecretRevelation(secret); // send money to hashlock account { var signed = Transfer(sender, hashlock.Address, Currency.BTC, 10); context.SendTransaction(signed); Assert.IsTrue(context.TryCreateNextLedger()); Assert.IsTrue(context.TryGetAccount(sender.Address.Encoded, out var senderAccount)); Assert.IsTrue(context.TryGetAccount(hashlock.Address.Encoded, out var hashlockAccount)); // check that the money has been sent Assert.IsTrue(senderAccount.GetBalance(Currency.BTC) == -10); Assert.IsTrue(hashlockAccount.GetBalance(Currency.BTC) == 10); } // try spend money without declaration and revelation { var signed = Transfer(hashlock.Address, new List <PrivateKeyNotWallet>(), sender, Currency.BTC, 10); context.SendTransaction(signed, ResultCode.TransactionValidationFailed); } // try spend money with declaration but without secret revelation { var signed = Transfer(hashlock.Address, new List <PrivateKeyNotWallet>(), sender, Currency.BTC, 10, new List <TxDeclaration>() { hashlock }); context.SendTransaction(signed, ResultCode.TransactionValidationFailed); } // try spend money with secret revelation but without declaration { var signed = Transfer(hashlock.Address, new List <PrivateKeyNotWallet>(), sender, Currency.BTC, 10, new List <TxDeclaration>() { revelation }); context.SendTransaction(signed, ResultCode.TransactionValidationFailed); } // spend money with hash { var signed = Transfer(hashlock.Address, new List <PrivateKeyNotWallet>(), sender, Currency.BTC, 10, new List <TxDeclaration>() { hashlock, revelation }); context.SendTransaction(signed); Assert.IsTrue(context.TryCreateNextLedger()); Assert.IsTrue(context.TryGetAccount(sender.Address.Encoded, out var senderAccount)); Assert.IsTrue(context.TryGetAccount(hashlock.Address.Encoded, out var hashlockAccount)); // check that the money has been sent Assert.IsTrue(senderAccount.GetBalance(Currency.BTC) == 0); Assert.IsTrue(hashlockAccount.GetBalance(Currency.BTC) == 0); } } }
public void LedgerTransformationTestMerkleHash() { using (var context = CreateContext(true)) { var sender = BTC_ISSUER; var signer1 = CreateAccount(); var signer2 = CreateAccount(); var signers = new List <Address>() { signer1, signer2 }; var multi = new MultiSignature(signers, 2); var secret = Secret.GenerateSecret(); var hashlock = new HashLock(secret.ComputeSecretHash(SecretHashType.SHA3)); var timeLock = new TimeLock(DateTime.Now.AddDays(-1).ToUnixTimestamp()); var signed = Transfer(sender, multi.Address, Currency.BTC, 10, null, null, new List <TxDeclaration>() { multi, hashlock, timeLock }); context.SendTransaction(signed); var signed1 = Transfer(sender, hashlock.Address, Currency.BTC, 10, null, null, new List <TxDeclaration>() { multi, hashlock, timeLock }); context.SendTransaction(signed1); var signed2 = Transfer(sender, timeLock.Address, Currency.BTC, 10, null, null, new List <TxDeclaration>() { multi, hashlock, timeLock }); context.SendTransaction(signed2); // Send address declaration but don't use the address var signed3 = Transfer(sender, timeLock.Address, Currency.BTC, 10, null, null, new List <TxDeclaration>() { new MultiSignature(new List <Address>() { CreateAccount(), CreateAccount(), CreateAccount() }, 3) }); context.SendTransaction(signed3); // Send address declaration but don't use the address var signed4 = Transfer(sender, timeLock.Address, Currency.BTC, 10, null, null, new List <TxDeclaration>() { new HashLock(Secret.GenerateSecret().ComputeSecretHash(SecretHashType.SHA256)) }); context.SendTransaction(signed4); // Send address declaration but don't use the address var signed5 = Transfer(sender, timeLock.Address, Currency.BTC, 10, null, null, new List <TxDeclaration>() { new TimeLock(777) }); context.SendTransaction(signed5); Assert.IsTrue(context.TryCreateNextLedger()); context.DataTransformationService.WaitTransformationCompleted(); var last = context.LedgerService.LedgerManager.GetMerkleRootHash(); Assert.Fail(); /* * var fromDb = context.DatabaseService.ReadDatabaseManager.GetLastLedger(); * * if(fromDb.GetHeight() != context.LedgerService.LedgerManager.LastLedger.GetHeight()) * Assert.Inconclusive(); * * Assert.IsTrue(last.Equals(fromDb.Ledger.MerkleHash)); */ } }