Beispiel #1
0
        public static object VerifyAndExecuteTxTest(byte[] proof, byte[] root)
        {
            //验证toMerkleValue
            byte[] CrossChainParams = MerkleProve(proof, root);
            if (CrossChainParams.Equals(new byte[] { 0x00 }))
            {
                Runtime.Notify("Proof verify error");
                return(false);
            }
            ToMerkleValue merkleValue = deserializMerkleValue(CrossChainParams);

            //通过参数中的txid查看交易是否已经被执行过
            if (Storage.Get(transactionPrefix.Concat(merkleValue.txHash)).AsBigInteger() == 1)
            {
                Runtime.Notify("Transaction has been executed");
                return(false);
            }
            //TODO: 确定Neo的跨链ID 并写死(暂定neo id 为4, 后面根据实际情况修改)
            if (merkleValue.TxParam.toChainID != 4)
            {
                Runtime.Notify("Not Neo crosschain tx");
                return(false);
            }
            Runtime.Notify("proof and root are correct");

            return(true);
        }
Beispiel #2
0
        public static bool VerifyAndExecuteTx(byte[] proof, BigInteger blockHeight)
        {
            BigInteger latestHeight = Storage.Get(latestHeightPrefix).AsBigInteger();

            if (blockHeight > latestHeight)
            {
                Runtime.Notify("blockHeight > LatestHeight!");
                return(false);
            }
            //get root by height
            byte[] rawHeader = getHeader(blockHeight);
            Header header;

            if (!rawHeader.Equals(new byte[] { 0x00 }))
            {
                header = deserializHeader(rawHeader);
            }
            else
            {
                Runtime.Notify("Header does not exist.");
                return(false);
            }
            // verify toMerkleValue
            byte[] CrossChainParams = MerkleProve(proof, header.crossStatesRoot);
            if (CrossChainParams.Equals(new byte[] { 0x00 }))
            {
                Runtime.Notify("Proof verify error");
                return(false);
            }
            ToMerkleValue merkleValue = deserializMerkleValue(CrossChainParams);

            //check by txid
            if (Storage.Get(transactionPrefix.Concat(merkleValue.txHash)).AsBigInteger() == 1)
            {
                Runtime.Notify("Transaction has been executed");
                return(false);
            }
            //check to chainID
            if (merkleValue.TxParam.toChainID != 4)
            {
                Runtime.Notify("Not Neo crosschain tx");
                return(false);
            }
            //run croos chain tx
            if (ExecuteCrossChainTx(merkleValue))
            {
                Runtime.Notify("Tx execute success");
            }
            else
            {
                Runtime.Notify("Tx execute fail");
            }

            //event
            CrossChainUnlockEvent(merkleValue.fromChainID, merkleValue.TxParam.toContract, merkleValue.txHash);

            return(true);
        }
Beispiel #3
0
        private static ToMerkleValue deserializMerkleValue(byte[] Source)
        {
            ToMerkleValue result = new ToMerkleValue();
            int           offset = 0;

            //get txHash
            var temp = ReadVarBytes(Source, offset);

            result.txHash = (byte[])temp[0];
            offset        = (int)temp[1];

            //get fromChainID, Uint64
            result.fromChainID = Source.Range(offset, 8).ToBigInteger();
            offset             = offset + 8;

            //get CrossChainTxParameter
            result.TxParam = deserializCrossChainTxParameter(Source, offset);
            return(result);
        }
Beispiel #4
0
 private static bool ExecuteCrossChainTx(ToMerkleValue value)
 {
     if (value.TxParam.toContract.Length == 20)
     {
         DyncCall TargetContract = (DyncCall)value.TxParam.toContract.ToDelegate();
         object[] parameter      = new object[] { value.TxParam.args, value.TxParam.fromContract, value.fromChainID };
         if (TargetContract(value.TxParam.method.AsString(), parameter) is null)
         {
             return(false);
         }
         else
         {
             Storage.Put(transactionPrefix.Concat(value.txHash), 1);
             return(true);
         }
     }
     else
     {
         Runtime.Notify("Contract length is not correct");
         return(false);
     }
 }
Beispiel #5
0
        public static bool VerifyAndExecuteTx(byte[] proof, byte[] RawHeader, byte[] headerProof, byte[] currentRawHeader, byte[] signList)
        {
            Header txheader = deserializHeader(RawHeader);

            byte[][]   keepers            = (byte[][])Storage.Get(mCKeeperPubKeysPrefix).Deserialize();
            int        n                  = keepers.Length;
            int        m                  = n - (n - 1) / 3;
            BigInteger currentEpochHeight = Storage.Get(currentEpochHeightPrefix).Concat(new byte[] { 0x00 }).AsBigInteger();

            byte[] StateRootValue = new byte[] { 0x00 };
            if (txheader.height >= currentEpochHeight)
            {
                if (!verifySig(RawHeader, signList, keepers, m))
                {
                    Runtime.Notify("Verify RawHeader signature failed!");
                    return(false);
                }
            }
            else
            {
                if (!verifySig(currentRawHeader, signList, keepers, m))
                {
                    Runtime.Notify("Verify currentRawHeader signature failed!");
                    return(false);
                }
                Header currentHeader = deserializHeader(currentRawHeader);
                StateRootValue = MerkleProve(headerProof, currentHeader.blockRoot);
                byte[] RawHeaderHash = Hash256(RawHeader);
                if (!StateRootValue.Equals(RawHeaderHash))
                {
                    Runtime.Notify("Verify block proof signature failed!");
                    return(false);
                }
            }
            // Through rawHeader.CrossStateRoot, the toMerkleValue or cross chain msg can be verified and parsed from proof
            StateRootValue = MerkleProve(proof, txheader.crossStatesRoot);
            if (StateRootValue.Equals(new byte[] { 0x00 }))
            {
                Runtime.Notify("cross chain Proof verify error");
                return(false);
            }
            ToMerkleValue merkleValue = deserializMerkleValue(StateRootValue);

            //check by txid
            if (Storage.Get(transactionPrefix.Concat(merkleValue.fromChainID.AsByteArray()).Concat(merkleValue.txHash)).AsBigInteger() == 1)
            {
                Runtime.Notify("Transaction has been executed");
                return(false);
            }
            //check to chainID
            if (merkleValue.TxParam.toChainID != 4)
            {
                Runtime.Notify("Not Neo crosschain tx");
                return(false);
            }
            //run croos chain tx
            if (ExecuteCrossChainTx(merkleValue))
            {
                Runtime.Notify("Tx execute success");
            }
            else
            {
                Runtime.Notify("Tx execute fail");
                return(false);
            }

            //event
            CrossChainUnlockEvent(merkleValue.fromChainID, merkleValue.TxParam.toContract, merkleValue.txHash);
            return(true);
        }
Beispiel #6
0
        public static bool VerifyAndExecuteTx(byte[] proof, BigInteger blockHeight)
        {
            BigInteger latestHeight = Storage.Get(latestHeightPrefix).AsBigInteger();

            if (blockHeight > latestHeight)
            {
                Runtime.Notify("blockHeight > LatestHeight!");
                return(false);
            }
            //根据高度取出root
            Map <BigInteger, Header> MCBlockHeaders = ((Map <BigInteger, Header>)Storage.Get(mCBlockHeadersPrefix).Deserialize());
            Header header;

            if (MCBlockHeaders.HasKey(blockHeight))
            {
                header = MCBlockHeaders[blockHeight];
            }
            else
            {
                Runtime.Notify("Header does not exist.");
                return(false);
            }
            //验证toMerkleValue
            byte[] CrossChainParams = MerkleProve(proof, header.crossStatesRoot);
            if (CrossChainParams.Equals(new byte[] { 0x00 }))
            {
                Runtime.Notify("Proof verify error");
                return(false);
            }
            ToMerkleValue merkleValue = deserializMerkleValue(CrossChainParams);

            //通过参数中的txid查看交易是否已经被执行过
            if (Storage.Get(transactionPrefix.Concat(merkleValue.txHash)).AsBigInteger() == 1)
            {
                Runtime.Notify("Transaction has been executed");
                return(false);
            }
            //TODO: 确定Neo的跨链ID 并写死(暂定neo id 为4, 后面根据实际情况修改)
            if (merkleValue.TxParam.toChainID != 4)
            {
                Runtime.Notify("Not Neo crosschain tx");
                return(false);
            }
            //执行跨链交易
            if (ExecuteCrossChainTx(merkleValue))
            {
                Runtime.Notify("Tx execute success");
            }
            else
            {
                Runtime.Notify("Tx execute fail");
            }

            //发出事件
            VerifyAndExecuteTxEvent
                (merkleValue.fromChainID,
                merkleValue.TxParam.toContract,
                merkleValue.txHash,
                merkleValue.TxParam.txHash);

            return(true);
        }