// bad naming
 public virtual bool DeclareAccount(HashLock account)
 {
     if (TrySetDeclaration(account))
     {
         hashLocksToInclude.Add(account);
         return(true);
     }
     return(false);
 }
Esempio n. 2
0
        protected override void ExecuteCommand(string[] args)
        {
            // TODO register hash lock account somewhere
            var bytes    = Convert.FromBase64String(hashArgument.Value);
            var hashlock = new HashLock(new SecretHash(SecretHashType.SHA3, new Hash256(bytes)));

            WalletService.ImportDeclaration(aliasArgument.Value, hashlock);

            Console.WriteLine($"HashLock Address {hashlock.Address.Encoded}");
        }
Esempio n. 3
0
        public LocalRepository(IDataStore dataStore, ITextConverter textComparer)
        {
            this.dataStore    = dataStore;
            this.textComparer = textComparer;

            var futureTimeStamp = System.DateTime.Now.AddMilliseconds(100).Ticks;

            keyProvider = new DocumentKeySource(futureTimeStamp);

            hashLock = new HashLock(avaliableLocks: 2000);
        }
Esempio n. 4
0
        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 HashLockRequiredSignature(HashLock locker)
 {
     this.locker = locker;
 }
Esempio n. 6
0
 protected void Write(HashLock locker)
 {
     Write(locker.SecretHash);
 }
Esempio n. 7
0
        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);
                }
            }
        }
Esempio n. 8
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);
                }
            }
        }
        private static Internals.HashLock CreateHashLock(HashLock declaration)
        {
            var secretHash = new Internals.SecretHash(declaration.SecretHash.Type, Convert.ToBase64String(declaration.SecretHash.Hash.Bytes));

            return(new Internals.HashLock(secretHash, declaration.Address.Encoded));
        }
Esempio n. 10
0
 private bool TryGetHashLockFromDeclarations(List <TxDeclaration> declarations, Address address, out HashLock hashLock)
 {
     foreach (var txDeclaration in declarations)
     {
         if (txDeclaration.Type == DeclarationType.HashLock)
         {
             var declaration = (HashLock)txDeclaration;
             if (declaration.Address == address)
             {
                 hashLock = declaration;
                 return(true);
             }
         }
     }
     hashLock = null;
     return(false);
 }
Esempio n. 11
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));
                 */
            }
        }