Example #1
0
            public override void FromStackItem(StackItem stackItem)
            {
                base.FromStackItem(stackItem);
                Array array = (Array)((Struct)stackItem)[1];

                Tokens.AddRange(array.Select(p => p.GetSpan().ToArray()));
            }
Example #2
0
        void IInteroperable.FromStackItem(StackItem stackItem)
        {
            Struct @struct = (Struct)stackItem;

            Contract = @struct[0] switch
            {
                Null => ContractPermissionDescriptor.CreateWildcard(),
                StackItem item => new ContractPermissionDescriptor(item.GetSpan())
            };
            Methods = @struct[1] switch
            {
                Null => WildcardContainer <string> .CreateWildcard(),
                Array array => WildcardContainer <string> .Create(array.Select(p => p.GetString()).ToArray()),
                _ => throw new ArgumentException(null, nameof(stackItem))
            };
        }
Example #3
0
        void IInteroperable.FromStackItem(StackItem stackItem)
        {
            Struct @struct = (Struct)stackItem;

            Name               = @struct[0].GetString();
            Groups             = ((Array)@struct[1]).Select(p => p.ToInteroperable <ContractGroup>()).ToArray();
            SupportedStandards = ((Array)@struct[2]).Select(p => p.GetString()).ToArray();
            Abi         = @struct[3].ToInteroperable <ContractAbi>();
            Permissions = ((Array)@struct[4]).Select(p => p.ToInteroperable <ContractPermission>()).ToArray();
            Trusts      = @struct[5] switch
            {
                Null => WildcardContainer <UInt160> .CreateWildcard(),
                Array array => WildcardContainer <UInt160> .Create(array.Select(p => new UInt160(p.GetSpan())).ToArray()),
                _ => throw new ArgumentException(null, nameof(stackItem))
            };
            Extra = JObject.Parse(@struct[6].GetSpan());
        }
Example #4
0
        public override void Deserialize(BinaryReader reader)
        {
            base.Deserialize(reader);
            int count = (int)reader.ReadVarInt(MaxContentsPerBlock);

            if (count == 0)
            {
                throw new FormatException();
            }
            ConsensusData = reader.ReadSerializable <ConsensusData>();
            Transactions  = new Transaction[count - 1];
            for (int i = 0; i < Transactions.Length; i++)
            {
                Transactions[i] = reader.ReadSerializable <Transaction>();
            }
            if (Transactions.Distinct().Count() != Transactions.Length)
            {
                throw new FormatException();
            }
            if (CalculateMerkleRoot(ConsensusData.Hash, Transactions.Select(p => p.Hash)) != MerkleRoot)
            {
                throw new FormatException();
            }
        }
Example #5
0
        void IInteroperable.FromStackItem(StackItem stackItem)
        {
            Struct @struct = (Struct)stackItem;

            Name   = @struct[0].GetString();
            Groups = ((Array)@struct[1]).Select(p => p.ToInteroperable <ContractGroup>()).ToArray();
            if (((Map)@struct[2]).Count != 0)
            {
                throw new ArgumentException(null, nameof(stackItem));
            }
            SupportedStandards = ((Array)@struct[3]).Select(p => p.GetString()).ToArray();
            Abi         = @struct[4].ToInteroperable <ContractAbi>();
            Permissions = ((Array)@struct[5]).Select(p => p.ToInteroperable <ContractPermission>()).ToArray();
            Trusts      = @struct[6] switch
            {
                Null => WildcardContainer <ContractPermissionDescriptor> .CreateWildcard(),
                Array array => WildcardContainer <ContractPermissionDescriptor> .Create(array.Select(p => new ContractPermissionDescriptor(p.GetSpan())).ToArray()),
                _ => throw new ArgumentException(null, nameof(stackItem))
            };
            Extra = (JObject)JToken.Parse(@struct[7].GetSpan());
        }
Example #6
0
        public async Task <object> UpdateContract(UInt160 contractHash, string nefPath, string manifestPath = null, bool sendTx = false, UInt160[] cosigners = null)
        {
            if (CurrentWallet == null)
            {
                return(Error(ErrorCode.WalletNotOpen));
            }
            if (nefPath.IsNull())
            {
                return(Error(ErrorCode.ParameterIsNull, "nefPath is empty."));
            }
            if (manifestPath.IsNull())
            {
                manifestPath = Path.ChangeExtension(nefPath, ".manifest.json");
            }
            // Read nef
            NefFile nefFile = ReadNefFile(nefPath);
            // Read manifest
            ContractManifest manifest = ReadManifestFile(manifestPath);

            // Basic script checks
            await CheckBadOpcode(nefFile.Script);

            // Build script
            using ScriptBuilder sb = new ScriptBuilder();
            sb.EmitDynamicCall(contractHash, "update", nefFile.ToArray(), manifest.ToJson().ToString(), null);
            var script = sb.ToArray();

            var singers = new List <Signer> {
            };

            if (cosigners != null)
            {
                singers.AddRange(cosigners.Select(s => new Signer()
                {
                    Account = s, Scopes = WitnessScope.Global
                }));
            }
            Transaction tx;

            try
            {
                tx = CurrentWallet.MakeTransaction(Helpers.GetDefaultSnapshot(), script, null, singers.ToArray());
            }
            catch (InvalidOperationException ex)
            {
                return(Error(ErrorCode.EngineFault, ex.GetExMessage()));
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Insufficient GAS"))
                {
                    return(Error(ErrorCode.GasNotEnough));
                }
                throw;
            }

            var result = new DeployResultModel
            {
                ContractHash = contractHash,
                GasConsumed  = new BigDecimal((BigInteger)tx.SystemFee, NativeContract.GAS.Decimals)
            };

            if (sendTx)
            {
                var(signSuccess, context) = CurrentWallet.TrySignTx(tx);
                if (!signSuccess)
                {
                    return(Error(ErrorCode.SignFail, context.SafeSerialize()));
                }
                await tx.Broadcast();

                result.TxId = tx.Hash;
            }
            return(result);
        }
Example #7
0
 public void RebuildMerkleRoot()
 {
     MerkleRoot = CalculateMerkleRoot(ConsensusData.Hash, Transactions.Select(p => p.Hash));
 }