public static bool Transfer(this NativeContract contract, Persistence.Snapshot snapshot, byte[] from, byte[] to, BigInteger amount, bool signFrom) { var engine = new ApplicationEngine(TriggerType.Application, new ManualWitness(signFrom ? new[] { new UInt160(from) } : null), snapshot, 0, true); engine.LoadScript(contract.Script); var script = new ScriptBuilder(); script.EmitPush(amount); script.EmitPush(to); script.EmitPush(from); script.EmitPush(3); script.Emit(OpCode.PACK); script.EmitPush("transfer"); engine.LoadScript(script.ToArray()); if (engine.Execute() == VMState.FAULT) { return(false); } var result = engine.ResultStack.Pop(); result.Should().BeOfType(typeof(VM.Types.Boolean)); return((result as VM.Types.Boolean).GetBoolean()); }
public static StackItem Call(this NativeContract contract, Persistence.Snapshot snapshot, IVerifiable container, string method, params ContractParameter[] args) { var engine = new ApplicationEngine(TriggerType.Application, container, snapshot, 0, true); engine.LoadScript(contract.Script); var script = new ScriptBuilder(); for (var i = args.Length - 1; i >= 0; i--) { script.EmitPush(args[i]); } script.EmitPush(args.Length); script.Emit(OpCode.PACK); script.EmitPush(method); engine.LoadScript(script.ToArray()); if (engine.Execute() != VMState.HALT) { throw new InvalidOperationException(); } return(engine.ResultStack.Pop()); }
public static Coin[] checkUtxo(Coin[] unspentsAsset) { List <Coin> unspents = unspentsAsset.ToList(); foreach (var item in unspentsAsset) { Transaction preTx = Blockchain.Singleton.GetTransaction(item.Reference.PrevHash); TransactionAttribute[] attribute = preTx.Attributes; using (Persistence.Snapshot snapshot = Blockchain.Singleton.GetSnapshot()) { foreach (TransactionAttribute att in attribute) { if (att.Usage == TransactionAttributeUsage.SmartContractScript) { BinaryReader OpReader = new BinaryReader(new MemoryStream(att.Data, false)); List <int> changeNum = ParseIndexList(OpReader); if (!changeNum.Contains(item.Reference.PrevIndex)) { using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, null, snapshot, Fixed8.Zero)) { engine.LoadScript(OpReader.ReadBytes(OpReader.ReadByte())); if (!engine.Execute() || engine.ResultStack.Count != 1 || !engine.ResultStack.Pop().GetBoolean()) { unspents.Remove(item); } } } } } } } return(unspents.ToArray()); }
public static BigInteger TotalSupply(this NativeContract contract, Persistence.Snapshot snapshot) { var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true); engine.LoadScript(contract.Script); var script = new ScriptBuilder(); script.EmitPush(0); script.Emit(OpCode.PACK); script.EmitPush("totalSupply"); engine.LoadScript(script.ToArray()); engine.Execute().Should().Be(VMState.HALT); var result = engine.ResultStack.Pop(); result.Should().BeOfType(typeof(VM.Types.Integer)); return((result as VM.Types.Integer).GetBigInteger()); }
public UInt160[] GetScriptHashesForVerifying(Persistence.Snapshot snapshot) => _hashForVerify;
public UInt160[] GetScriptHashesForVerifying(Persistence.Snapshot snapshot) { return(_hashForVerify); }
public UInt160 GetScriptHashForVerification(Persistence.Snapshot snapshot) { return(_hashForVerify); }
public static StackItem Call(this NativeContract contract, Persistence.Snapshot snapshot, string method, params ContractParameter[] args) { return(Call(contract, snapshot, null, method, args)); }
public static Coin[] checkUtxo(Coin[] unspentsAsset) { List <Coin> unspents = unspentsAsset.ToList(); foreach (var item in unspentsAsset) { Transaction preTx = Blockchain.Singleton.GetTransaction(item.Reference.PrevHash); TransactionAttribute[] attribute = preTx.Attributes; using (Persistence.Snapshot snapshot = Blockchain.Singleton.GetSnapshot()) { foreach (TransactionAttribute att in attribute) { if (att.Usage == TransactionAttributeUsage.SmartContractScript) { int n = -1; System.IO.BinaryReader OpReader = new System.IO.BinaryReader(new System.IO.MemoryStream(att.Data, false)); OpCode opcode = (OpCode)OpReader.ReadByte(); switch (opcode) { case OpCode.PUSH0: break; case OpCode.PUSHDATA1: n = BitConverter.ToInt16(OpReader.ReadBytes(OpReader.ReadByte()), 0); break; case OpCode.PUSHDATA2: n = BitConverter.ToInt16(OpReader.ReadBytes(OpReader.ReadUInt16()), 0); break; case OpCode.PUSHDATA4: n = BitConverter.ToInt32(OpReader.ReadBytes((int)OpReader.ReadUInt32()), 0); break; case OpCode.PUSHM1: case OpCode.PUSH1: case OpCode.PUSH2: case OpCode.PUSH3: case OpCode.PUSH4: case OpCode.PUSH5: case OpCode.PUSH6: case OpCode.PUSH7: case OpCode.PUSH8: case OpCode.PUSH9: case OpCode.PUSH10: case OpCode.PUSH11: case OpCode.PUSH12: case OpCode.PUSH13: case OpCode.PUSH14: case OpCode.PUSH15: case OpCode.PUSH16: n = (int)opcode - (int)OpCode.PUSH1 + 1; break; } if (item.Reference.PrevIndex != n) { using (ApplicationEngine engine = new ApplicationEngine(TriggerType.Verification, null, snapshot, Fixed8.Zero)) { engine.LoadScript(OpReader.ReadBytes(OpReader.ReadByte())); if (!engine.Execute() || engine.ResultStack.Count != 1 || !engine.ResultStack.Pop().GetBoolean()) { unspents.Remove(item); } } } } } } } return(unspents.ToArray()); }