Ejemplo n.º 1
0
        Types.Transaction Sign(Types.Output output, byte[] asset, ulong amount)
        {
            ulong change;
            var   assets = new List <Asset>();

            var outputs = new List <Types.Output>();

            using (TransactionContext dbTx = _DBContext.GetTransactionContext())
            {
                if (!GetAssets(dbTx, asset, amount, out change, assets))
                {
                    return(null);
                }
                else if (change > 0)
                {
                    Key key;

                    if (_KeyStore.GetUnusedKey(dbTx, out key, true))
                    {
                        _Keys.Add(key);
                        dbTx.Commit();
                    }

                    outputs.Add(new Types.Output(key.Address.GetLock(), new Types.Spend(asset, change)));
                }
            }

            outputs.Add(output);

            return(TransactionValidation.signTx(new Types.Transaction(
                                                    0,
                                                    ListModule.OfSeq(assets.Select(t => t.Outpoint)),
                                                    ListModule.OfSeq(new List <byte[]>()),
                                                    ListModule.OfSeq(outputs),
                                                    null), ListModule.OfSeq(assets.Select(i => i.Key.Private))));
        }
Ejemplo n.º 2
0
        public bool GetContractActivationTx(byte[] code, ulong zenAmount, out Types.Transaction signedTx, byte[] secureTokenHash = null)
        {
            ulong change;
            var   assets = new List <Asset>();

            var outputs = new List <Types.Output>();

            using (TransactionContext dbTx = _DBContext.GetTransactionContext())
            {
                if (!GetAssets(dbTx, Tests.zhash, zenAmount, out change, assets))
                {
                    signedTx = null;
                    return(false);
                }
                else if (change > 0)
                {
                    Key key;

                    if (_KeyStore.GetUnusedKey(dbTx, out key, true))
                    {
                        _Keys.Add(key);
                        dbTx.Commit();
                    }

                    outputs.Add(new Types.Output(key.Address.GetLock(), new Types.Spend(Tests.zhash, change)));
                }

                if (secureTokenHash != null)
                {
                    ulong secureTokenChange;
                    // var secureTokenAssets = new Assets();

                    if (!GetAssets(dbTx, secureTokenHash, 1, out secureTokenChange, assets))
                    {
                        signedTx = null;
                        return(false);
                    }
                    else if (secureTokenChange > 0)
                    {
                        Key key;

                        if (_KeyStore.GetUnusedKey(dbTx, out key, true))
                        {
                            _Keys.Add(key);
                            dbTx.Commit();
                        }

                        outputs.Add(new Types.Output(key.Address.GetLock(), new Types.Spend(secureTokenHash, secureTokenChange)));
                    }
                }
            }

            var output = new Types.Output(
                Types.OutputLock.NewContractSacrificeLock(
                    new Types.LockCore(0, ListModule.OfSeq(new byte[][] { }))
                    ),
                new Types.Spend(Tests.zhash, zenAmount)
                );

            outputs.Add(output);

            if (secureTokenHash != null)
            {
                outputs.Add(new Types.Output(Types.OutputLock.NewContractLock(Merkle.innerHash(code), new byte[] { }), new Types.Spend(secureTokenHash, 1)));
            }

            signedTx = TransactionValidation.signTx(new Types.Transaction(
                                                        0,
                                                        ListModule.OfSeq(assets.Select(t => t.Outpoint)),
                                                        ListModule.OfSeq(new List <byte[]>()),
                                                        ListModule.OfSeq(outputs),
                                                        new Microsoft.FSharp.Core.FSharpOption <Types.ExtendedContract>(
                                                            Types.ExtendedContract.NewContract(new Types.Contract(code, new byte[] { }, new byte[] { }))
                                                            )
                                                        ), ListModule.OfSeq(assets.Select(i => i.Key.Private)));

            return(true);
        }
 public static Types.Transaction Sign(this Types.Transaction tx, params byte[][] keys)
 {
     return(TransactionValidation.signTx(tx, ListModule.OfSeq(keys)));
 }