async Task test_startAuction_xxx_sell() { subPrintLine("startAuction [xxx]." + this.root + ". input xxx:"); var subname = Console.ReadLine(); var roothash = nns_tools.nameHash(this.root); //得到注册器 var info = await nns_tools.api_InvokeScript(Config.sc_nns, "getOwnerInfo", "(hex256)" + roothash.ToString()); var reg_sc = new Hash160(info.value.subItem[0].subItem[1].data); subPrintLine("reg=" + reg_sc.ToString()); var who = this.scriptHash; var result = await nns_tools.api_SendTransaction(prikey, reg_sc, "startAuction", "(hex160)" + who.ToString(), "(hex256)" + roothash.ToString(), "(string)" + subname ); subPrintLine("result=" + result); }
public static async Task <Result> api_GetBalance(Hash160 scripthash, string addr) { byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "getbalance", out postdata, new MyJson.JsonNode_ValueString(addr)); var text = await Helper.HttpPost(url, postdata); Result rest = new Result(); rest.textInfo = text; //if (json.ContainsKey("result")) //{ // var result = json["result"].AsList()[0].AsDict()["stack"].AsList(); // rest.value = ResultItem.FromJson("Array", result); //} //return rest;// subPrintLine("得到的结果是:" + result); var json = MyJson.Parse(text).AsDict()["result"].AsList(); foreach (var item in json) { if (item.AsDict()["asset"].AsString() == Config.id_GAS) { Console.WriteLine("gas=" + item.AsDict()["balance"].ToString()); } if (item.AsDict()["asset"].AsString() == Config.id_NEO) { Console.WriteLine("neo=" + item.AsDict()["balance"].ToString()); rest.textInfo = item.AsDict()["balance"].ToString(); } } return(rest); }
private void QueryAssetInfo(object sender, RoutedEventArgs e) { Hash160 assetid = new Hash160(this.tb_assetsetting_hash.Text); byte[] script = dex_ScriptPackage.GetScript_GetAssetSetting(assetid); Invoke(script); }
public static async Task <Result> api_InvokeScript(Hash160 scripthash, string methodname, params string[] subparam) { byte[] data = null; using (ScriptBuilder sb = new ScriptBuilder()) { MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); for (var i = 0; i < subparam.Length; i++) { array.AddArrayValue(subparam[i]); } sb.EmitParamJson(array); sb.EmitPushString(methodname); sb.EmitAppCall(scripthash); data = sb.ToArray(); } string script = ThinNeo.Helper.Bytes2HexString(data); byte[] postdata; var url = Helper.MakeRpcUrlPost(api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(script)); var text = await Helper.HttpPost(url, postdata); MyJson.JsonNode_Object json = MyJson.Parse(text) as MyJson.JsonNode_Object; Result rest = new Result(); rest.textInfo = text; if (json.ContainsKey("result")) { var result = json["result"].AsList()[0].AsDict()["stack"].AsList(); rest.value = ResultItem.FromJson("Array", result); } return(rest);// subPrintLine("得到的结果是:" + result); }
public static async Task <string> api_SendbatchTransfer(byte[] prikey, Hash160 schash, string methodname, params string[] subparam) { byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); byte[] data = null; using (ScriptBuilder sb = new ScriptBuilder()) { MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); byte[] randombytes = new byte[32]; using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) { rng.GetBytes(randombytes); } BigInteger randomNum = new BigInteger(randombytes); sb.EmitPushNumber(randomNum); sb.Emit(ThinNeo.VM.OpCode.DROP); if (subparam != null && subparam.Length > 0) { for (var i = 0; i < subparam.Length; i++) { array.AddArrayValue(subparam[i]); } } sb.EmitParamJson(array); sb.EmitPushString(methodname); sb.EmitAppCall(schash); data = sb.ToArray(); } //MakeTran ThinNeo.Transaction tran = new ThinNeo.Transaction(); tran.version = 0;//0 or 1 tran.inputs = new ThinNeo.TransactionInput[0]; tran.outputs = new ThinNeo.TransactionOutput[0]; tran.type = ThinNeo.TransactionType.InvocationTransaction; tran.extdata = new ThinNeo.InvokeTransData(); var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = data; idata.gas = 0; tran.attributes = new ThinNeo.Attribute[1]; tran.attributes[0] = new ThinNeo.Attribute(); tran.attributes[0].usage = ThinNeo.TransactionAttributeUsage.Script; tran.attributes[0].data = ThinNeo.Helper.GetPublicKeyHashFromAddress(address); //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); var result = await Helper.HttpPost(url, postdata); return(result); }
async Task test_set_resolver() { subPrintLine("get domain info:input domain like a.b.c~"); var domain = Console.ReadLine(); string[] strs = domain.Split('.'); var mh = nns_tools.nameHash(strs[strs.Length - 1]); for (var i = strs.Length - 2; i >= 0; i--) { mh = nns_tools.nameHashSub(mh, strs[i]); } subPrintLine("mh=" + mh.ToString()); byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(Config.test_wif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); Hash160 hash = ThinNeo.Helper.GetScriptHashFromPublicKey(pubkey); var resolver = new Hash160("0x6e2aea28af9c5febea0774759b1b76398e3167f1"); var result = await nns_tools.api_SendTransaction(prikey, Config.sc_nns, "owner_SetResolver", "(hex160)" + hash.ToString(), //参数1 所有者 "(hex256)" + mh.ToString(), //参数2 域名fullhash "(hex160)" + resolver.ToString() //参数3 解析器地址 ); subPrintLine("result=" + result); }
async Task test_collectDomain() { subPrintLine("get [xxx]." + this.root + " domain. input xxx:"); var subname = Console.ReadLine(); var roothash = nns_tools.nameHash(this.root); var fullhash = nns_tools.nameHashSub(roothash, subname); //得到注册器 var info = await nns_tools.api_InvokeScript(Config.sc_nns, "getOwnerInfo", "(hex256)" + roothash.ToString()); var reg_sc = new Hash160(info.value.subItem[0].subItem[1].data); subPrintLine("reg=" + reg_sc.ToString()); //得到拍卖ID var info3 = await nns_tools.api_InvokeScript(reg_sc, "getAuctionStateByFullhash", "(hex256)" + fullhash.ToString()); var id = info3.value.subItem[0].subItem[0].AsHash256(); var who = this.scriptHash; var result = await nns_tools.api_SendTransaction(prikey, reg_sc, "collectDomain", "(hex160)" + who.ToString(), //参数1 who "(hex256)" + id.ToString() //参数2 交易id ); subPrintLine("result=" + result); }
async Task test_raise_xxx_sell() { subPrintLine("input value"); var value = Console.ReadLine(); subPrintLine("raise 1 for [xxx]." + this.root + ". input xxx:"); var subname = Console.ReadLine(); var roothash = nns_tools.nameHash(this.root); var fullhash = nns_tools.nameHashSub(roothash, subname); //得到注册器 var info = await nns_tools.api_InvokeScript(Config.sc_nns, "getOwnerInfo", "(hex256)" + roothash.ToString()); var reg_sc = new Hash160(info.value.subItem[0].subItem[1].data); subPrintLine("reg=" + reg_sc.ToString()); //得到拍卖ID var info3 = await nns_tools.api_InvokeScript(reg_sc, "getAuctionStateByFullhash", "(hex256)" + fullhash.ToString()); var id = info3.value.subItem[0].subItem[0].AsHash256(); var who = this.scriptHash; var result = await nns_tools.api_SendTransaction(this.prikey, reg_sc, "raise", "(hex160)" + who.ToString(), //参数1 who "(hex256)" + id.ToString(), //参数2 交易id "(int)" + value //参数3,加价多少 //"(int)1"//参数3,加价多少 ); subPrintLine("result=" + result); }
private void SetMortgageAssetHash(object sender, RoutedEventArgs e) { string key = this.tb_mortgageAssetHash_key.Text; Hash160 value = new Hash160(this.tb_mortgageAssetHash_value.Text); byte[] script = dex_ScriptPackage.GetScript_SetSysSetting_MortgageAssetHash(key, value); MakeTran(script); }
public unsafe void Compress65Test() { byte[] data = GetRandomBytes(65); byte[] actual = Hash160.Compress65(data); byte[] expected = ComputeHash160(data); Assert.Equal(expected, actual); }
public unsafe void Compress65Test() { using Hash160 hash = new Hash160(); byte[] data = Helper.HexToBytes(UncompPub); byte[] actual = hash.Compress65(data); byte[] expected = Helper.HexToBytes(UncompPubHex); Assert.Equal(65, data.Length); Assert.Equal(expected, actual); }
public unsafe void Compress22Test() { using Hash160 hash = new Hash160(); byte[] data = Helper.HexToBytes("01d349abfc6687a7ca8c23e207f88356b1d712322bae"); byte[] actual = hash.Compress22(data); byte[] expected = Helper.HexToBytes("1a5b597a300d83e993c8a3e518c21c9b309596e9"); Assert.Equal(22, data.Length); Assert.Equal(expected, actual); }
public unsafe void Compress33_P2shTest() { byte[] data = GetRandomBytes(33); byte[] actual = Hash160.Compress33_P2sh(data); // Hash160(0x0014-Hash160(pub)) byte[] expected = ComputeHash160(new byte[] { 0, 20 }.ConcatFast(ComputeHash160(data))); Assert.Equal(expected, actual); }
private void Bet(object sender, RoutedEventArgs e) { string address = wallet.Address; Hash256 auctionid = new Hash256(this.tb_bet_auctionid.Text); Hash160 assetid = new Hash160(this.tb_bet_assetid.Text); BigInteger price = BigInteger.Parse(this.tb_bet_price.Text); byte[] script = dex_ScriptPackage.GetScript_Bet(address, auctionid, assetid, price); MakeTran(script); }
public static async Task <string> api_SendTransaction(byte[] prikey, Hash160 schash, string methodname, params string[] subparam) { byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, address); if (dir.ContainsKey(Config.id_GAS) == false) { Console.WriteLine("no gas"); return(null); } //MakeTran ThinNeo.Transaction tran = null; { byte[] data = null; using (ScriptBuilder sb = new ScriptBuilder()) { MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); if (subparam != null && subparam.Length > 0) { for (var i = 0; i < subparam.Length; i++) { array.AddArrayValue(subparam[i]); } } sb.EmitParamJson(array); sb.EmitPushString(methodname); sb.EmitAppCall(schash); data = sb.ToArray(); } tran = Helper.makeTran(dir[Config.id_GAS], null, new ThinNeo.Hash256(Nep55_1.id_GAS), 0); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = data; idata.gas = 0; } //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); var result = await Helper.HttpPost(url, postdata); return(result); }
public unsafe void Compress65Test() { using Hash160 hash = new Hash160(); byte[] data = new byte[65]; Helper.FillRandomByte(data); byte[] actual = hash.Compress65(data); byte[] expected = ComputeHash160(data); Assert.Equal(expected, actual); }
public bool TryReadHash160(out Hash160 value) { if (data.Length < ConsumedBytes + 20) { value = default; return(false); } value = new Hash160(data.Slice(ConsumedBytes, 20)); ConsumedBytes += 20; return(true); }
public byte[] GetScript_GetAssetSetting(Hash160 assetid) { JArray inputJA = JArray.Parse(string.Format(@" [ '(str)getAssetSetting', [ '(hex160){0}' ] ]", assetid)); return(GetScript(contractHash, inputJA)); }
private void OfferToBuyer(object sender, RoutedEventArgs e) { string addres = wallet.Address; string[] domains = this.tb_offerToBuyer_domain.Text.Split('.'); Hash160 assetid = new Hash160(this.tb_offerToBuyer_assetid.Text); BigInteger price = BigInteger.Parse(this.tb_offerToBuyer_price.Text); BigInteger mortgage = BigInteger.Parse(this.tb_offerToBuyer_mortgage.Text); byte[] script = dex_ScriptPackage.GetScript_OfferToBuy(addres, domains, assetid, price, mortgage); MakeTran(script); }
public unsafe void Compress33_P2shTest() { using Hash160 hash = new Hash160(); byte[] data = new byte[33]; Helper.FillRandomByte(data); byte[] actual = hash.Compress33_P2sh(data); // Hash160(0x0014-Hash160(pub)) byte[] expected = ComputeHash160(new byte[] { 0, 20 }.ConcatFast(ComputeHash160(data))); Assert.Equal(expected, actual); }
private void Auction(object sender, RoutedEventArgs e) { string[] domains = this.tb_auction_domain.Text.Split('.'); Hash160 assetid = new Hash160(this.tb_auction_assetid.Text); BigInteger startPrice = BigInteger.Parse(this.tb_auction_startPrice.Text); BigInteger endPrice = BigInteger.Parse(this.tb_auction_endPrice.Text); BigInteger salePrice = BigInteger.Parse(this.tb_auction_salePrice.Text); BigInteger mortgage = BigInteger.Parse(this.tb_auction_mortgage.Text); byte[] script = dex_ScriptPackage.GetScript_Auction(domains, assetid, startPrice, endPrice, salePrice, mortgage); MakeTran(script); }
private void SetAssetInfo(object sender, RoutedEventArgs e) { Hash160 assetid = new Hash160(this.tb_assetsetting_hash.Text); BigInteger enable = this.tb_assetsetting_enable.IsChecked == true?1:0; BigInteger min = BigInteger.Parse(this.tb_assetsetting_valuemin.Text); BigInteger unit = BigInteger.Parse(this.tb_assetsetting_valueUnit.Text); BigInteger feeratio = BigInteger.Parse(this.tb_assetsetting_feeRate.Text); string transferMethod = this.tb_assetsetting_transferMethod.Text; byte[] script = dex_ScriptPackage.GetScript_SetAssetSetting(assetid, enable, min, unit, feeratio, transferMethod); MakeTran(script); }
private void initAccount() { this.prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(Config.test_wif); this.pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); this.scriptHash = ThinNeo.Helper.GetScriptHashFromPublicKey(pubkey); this.address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); Console.WriteLine("\n************************************* -- Current Account -- **************************************\n"); subPrintLine("Address : " + this.address); subPrintLine("Prikey : " + ThinNeo.Helper.Bytes2HexString(prikey)); subPrintLine("Pubkey : " + ThinNeo.Helper.Bytes2HexString(pubkey)); subPrintLine("ScriptHash : " + this.scriptHash.ToString()); Console.WriteLine("\n**************************************************************************************************\n"); }
public byte[] GetScript_SetSysSetting_MortgageAssetHash(string key, Hash160 hash) { JArray inputJA = JArray.Parse(string.Format(@" [ '(str)setSysSetting', [ '(str){0}', '(hex160){1}' ] ]", key, hash)); return(GetScript(contractHash, inputJA)); }
public static string GetAddressFromScriptHash(Hash160 scripthash) { byte[] data = new byte[20 + 1]; data[0] = 0x17; Array.Copy(scripthash, 0, data, 1, 20); var hash = sha256.ComputeHash(data); hash = sha256.ComputeHash(hash); var alldata = data.Concat(hash.Take(4)).ToArray(); return(Base58.Encode(alldata)); }
public void ComputeHashTest() { byte[] data1 = Helper.HexToBytes("03306eeb63417d2e50c49bd5cb6256296116d6474c14853d64e008d281e392109a"); byte[] actual1 = Hash160.ComputeHash_static(data1); byte[] expected1 = Helper.HexToBytes("3edd2f8b85027645ddb5aec9ad59b3b60c396c7e"); byte[] data2 = Helper.HexToBytes("04306EEB63417D2E50C49BD5CB6256296116D6474C14853D64E008D281E392109AF3C0F0E015C966BE3DBB4BD09E4BE95EC109CCDFBEC4C4FD910E77091DC00A67"); byte[] actual2 = Hash160.ComputeHash_static(data2); byte[] expected2 = Helper.HexToBytes("543e87f1cde0a028ad4c33afc8052ed78846c216"); Assert.Equal(expected1, actual1); Assert.Equal(expected2, actual2); }
public byte[] GetScript_SetMoneyIn(Hash256 txid, Hash160 assetid) { JArray inputJA = JArray.Parse(string.Format(@" [ '(str)setMoneyIn', [ '(hex256){0}', '(hex160){1}' ] ]", txid, assetid)); return(GetScript(contractHash, inputJA)); }
public byte[] GetScript_GetMoneyBackAll(string address, Hash160 assetid) { JArray inputJA = JArray.Parse(string.Format(@" [ '(str)getMoneyBackAll', [ '(addr){0}', '(hex160){1}' ] ]", address, assetid)); return(GetScript(contractHash, inputJA)); }
public byte[] GetScript_GetMoneyBack(string address, Hash160 assetid, BigInteger value) { JArray inputJA = JArray.Parse(string.Format(@" [ '(str)getMoneyBack', [ '(addr){0}', '(hex160){1}', '(int){2}' ] ]", address, assetid, value)); return(GetScript(contractHash, inputJA)); }
public void ComputeHashTest() { using Hash160 hash = new Hash160(); byte[] data1 = Helper.HexToBytes(CompPub); byte[] actual1 = hash.ComputeHash(data1); byte[] expected1 = Helper.HexToBytes(CompPubHex); byte[] data2 = Helper.HexToBytes(UncompPub); byte[] actual2 = hash.ComputeHash(data2); byte[] expected2 = Helper.HexToBytes(UncompPubHex); Assert.Equal(expected1, actual1); Assert.Equal(expected2, actual2); }