Exemplo n.º 1
0
        protected static ByteString NewTokenId(ByteString salt)
        {
            StorageContext context = Storage.CurrentContext;

            byte[]     key = new byte[] { Prefix_TokenId };
            ByteString id  = Storage.Get(context, key);

            Storage.Put(context, key, (BigInteger)id + 1);
            if (id is not null)
            {
                salt += id;
            }
            return(CryptoLib.Sha256(salt));
        }
Exemplo n.º 2
0
        public static void CreateSale(UInt160 seller, BigInteger amount, BigInteger price, string description, ByteString?saleId)
        {
            if (amount != price * 2)
            {
                throw new Exception("seller deposit must be 2x price");
            }
            var token = Runtime.CallingScriptHash;

            if (saleId != null)
            {
                if (saleId.Length != 16)
                {
                    throw new Exception("sale ID must be 16 bytes long");
                }
            }
            else
            {
                var saleHash = new List <object>();
                saleHash.Add(seller);
                saleHash.Add(price);
                saleHash.Add(description);
                saleHash.Add(Runtime.GetRandom());
                saleId = CryptoLib.Sha256(StdLib.Serialize(saleHash));
            }

            StorageMap salesMap       = new(Storage.CurrentContext, Prefix_Sales);
            var        serializedSale = salesMap.Get(saleId);

            if (serializedSale != null)
            {
                throw new Exception("specified saleId already exists");
            }

            var saleInfo = new SaleInfo();

            saleInfo.Seller      = seller;
            saleInfo.Description = description;
            saleInfo.Price       = price;
            saleInfo.Token       = token;
            saleInfo.State       = SaleState.New;

            salesMap.Put(saleId, StdLib.Serialize(saleInfo));

            StorageMap accountMap = new(Storage.CurrentContext, Prefix_AccountSales);

            accountMap.Put(seller + saleId, 0);

            OnNewSale(saleId, seller, description, token, price);
        }
Exemplo n.º 3
0
        protected static ByteString NewTokenId()
        {
            StorageContext context = Storage.CurrentContext;

            byte[]     key = new byte[] { Prefix_TokenId };
            ByteString id  = Storage.Get(context, key);

            Storage.Put(context, key, (BigInteger)id + 1);
            ByteString data = Runtime.ExecutingScriptHash;

            if (id is not null)
            {
                data += id;
            }
            return(CryptoLib.Sha256(data));
        }
Exemplo n.º 4
0
        public static bool crossChain(BigInteger toChainID, byte[] toChainAddress, byte[] functionName, byte[] args, byte[] caller)
        {
            var tx       = (Transaction)Runtime.ScriptContainer;
            var IdAsByte = toChainID.ToByteArray();
            CrossChainTxParameter para = new CrossChainTxParameter
            {
                toChainID  = IdAsByte,
                toContract = toChainAddress,
                method     = functionName,
                args       = args,

                txHash       = (byte[])tx.Hash,
                crossChainID = (byte[])CryptoLib.Sha256((ByteString)((byte[])Runtime.ExecutingScriptHash).Concat((byte[])tx.Hash)),
                fromContract = caller
            };
            BigInteger requestId   = getRequestID(IdAsByte);
            var        resquestKey = putRequest(IdAsByte, requestId, para);

            //event
            CrossChainLockEvent(caller, para.fromContract, toChainID, resquestKey, para.args);
            return(true);
        }
Exemplo n.º 5
0
 public static ByteString Hash256(ByteString value)
 {
     return(CryptoLib.Sha256(CryptoLib.Sha256(value)));
 }
Exemplo n.º 6
0
 public static ByteString Hash160(ByteString value)
 {
     return(CryptoLib.ripemd160(CryptoLib.Sha256(value)));
 }
Exemplo n.º 7
0
 public static byte[] SHA256(byte[] value)
 {
     return((byte[])CryptoLib.Sha256((ByteString)value));
 }
Exemplo n.º 8
0
 public static byte[] Hash160(byte[] message)
 {
     return((byte[])CryptoLib.ripemd160(CryptoLib.Sha256((ByteString)message)));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Tested, 对叶节点进行Hash计算
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static byte[] hashLeaf(byte[] value)
 {
     byte[] prefix = new byte[] { 0x00 };
     return((byte[])CryptoLib.Sha256((ByteString)prefix.Concat(value)));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Tested, 对子节点进行Hash计算
 /// </summary>
 /// <param name="v"></param>
 /// <param name="hash"></param>
 /// <returns></returns>
 public static byte[] hashChildren(byte[] v, byte[] hash)
 {
     byte[] prefix = new byte[] { 1 };
     return((byte[])CryptoLib.Sha256((ByteString)prefix.Concat(v).Concat(hash)));
 }