private static ECDsa loadECDSAFromPrivateKey(AccountPrivateKey privateKey) { var ecparams = new ECParameters(); ecparams.Curve = ECCurve.NamedCurves.nistP256; var ecPoint = new ECPoint(); ecPoint.X = XConvert.GetECBytesFromBigInteger(privateKey.X); ecPoint.Y = XConvert.GetECBytesFromBigInteger(privateKey.Y); ecparams.Q = ecPoint; ecparams.D = XConvert.GetECBytesFromBigInteger(privateKey.D); return(ECDsa.Create(ecparams)); }
private static ECDsa loadECDSAFromPublicKey(AccountPublicKey publicKey) { var param = new ECParameters { Curve = ECCurve.NamedCurves.nistP256, Q = new ECPoint { X = XConvert.GetECBytesFromBigInteger(publicKey.X), Y = XConvert.GetECBytesFromBigInteger(publicKey.Y), }, }; return(ECDsa.Create(param)); }
private Pb.CommonReply PostTx(string bcname, Transaction tx) { var pbtx = XConvert.LocalTxToPbTx(tx); var reqTx = new Pb.TxStatus { Txid = pbtx.Txid, Tx = pbtx, Bcname = bcname, }; var postRes = client.PostTx(reqTx); if (postRes.Header.Error != Pb.XChainErrorEnum.Success) { Console.WriteLine("post tx failed, txid=" + tx.Txid + " err=" + (int)postRes.Header.Error); return(null); } return(postRes); }
// Query a block with block id /// <summary>Query a block with block id</summary> /// <param name="bcname">the name of blockchain</param> /// <param name="blockid">block id</param> /// <return>the request response, the <c>Data</c> field is a Block type</return> public Response QueryBlock(string bcname, string blockid) { var req = new Pb.BlockID { Header = GetDefaultHeader(), Bcname = bcname, Blockid = ByteString.CopyFrom(XConvert.HexStringToByteArray(blockid)), NeedContent = true, }; var res = client.GetBlock(req); if (res.Header.Error != Pb.XChainErrorEnum.Success) { Console.WriteLine("query block failed. errcode=" + (int)res.Header.Error + ", logid=" + req.Header.Logid); return(MakeErrorResponse(ErrorCode.ConnectFailed, null)); } var block = XConvert.PbBlockToLocalBlock(res); return(MakeResponse("", block)); }
// Transfer given amount of UTXO resource to another address /// <summary>Transfer given amount of UTXO resource to another address</summary> /// <param name="bcname">the name of the blockchain</param> /// <param name="to">the receiver address</param> /// <param name="amount">the amount of UTXO resource</param> /// <param name="desc">addtional information attached to this transaction</param> /// <return>the response contains the Txid of this transaction</return> public Response Transfer(string bcname, string to, BigInteger amount, string desc) { var response = new Response() { Error = new XChainError() { ErrorCode = ErrorCode.Success, ErrorMessage = "Success", } }; if (string.IsNullOrEmpty(bcname) || string.IsNullOrEmpty(to)) { return(MakeErrorResponse(ErrorCode.ParamError, null)); } var utxo = SelectUTXO(bcname, this.Account.Address, this.Account.PublicKey, amount); if (utxo == null) { Console.WriteLine("Select utxo failed"); return(MakeErrorResponse(ErrorCode.SelectUTXOError, null)); } var tx = AssembleTx(utxo, this.Account, null, to, amount, null, desc); if (tx == null) { Console.WriteLine("AssembleTx failed"); return(MakeErrorResponse(ErrorCode.UnknownError, null)); } // post transaction var postRes = PostTx(bcname, tx); if (postRes == null || postRes.Header.Error != Pb.XChainErrorEnum.Success) { Console.WriteLine("InvokeContract: PostTx failed"); return(MakeErrorResponse(ErrorCode.PostError, null)); } response.Txid = XConvert.ByteArrayToHexString(tx.Txid).ToLower(); return(response); }
// Invokes contract method with given args // TODO: multisig is not supported /// <summary>Invokes contract method with given args. /// TODO: multisig is not supported</summary> /// <param name="bcname">the name of blockchain</param> /// <param name="contractName">the name of contract to invoke</param> /// <param name="method">the method name of contract to invoke</param> /// <param name="args">the arguments of contract</param> /// <param name="authRequire">add more address if multisig needed, otherwise keep null</param> /// <param name="contractType">the contract VM type, default to WASM</param> /// <param name="desc">description info, default to empty</param> public Response InvokeContract(string bcname, string contractName, string method, SortedDictionary <string, byte[]> args, List <string> authRequire = null, string desc = "", ContactVMType.Type contractType = ContactVMType.Type.WASM) { if (string.IsNullOrEmpty(bcname) || string.IsNullOrEmpty(method)) { return(MakeErrorResponse(ErrorCode.ParamError, null)); } // pre-execute contract var execRes = PreExecWithSelectUTXO(bcname, this.Account.Address, this.Account.PrivateKey, 0, contractName, method, args, contractType, this.Account.Address, authRequire); if (execRes == null) { Console.WriteLine("InvokeContract: PreExecWithSelectUTXO failed"); return(MakeErrorResponse(ErrorCode.PostError, null)); } // check contract response var contractResult = new Dictionary <string, string>(); for (var i = 0; i < execRes.Response.Responses.Count; i++) { if (execRes.Response.Responses[i].Status >= 400) { Console.WriteLine("Contract execute failed. res=" + JsonConvert.SerializeObject(execRes.Response.Responses[i])); return(new Response { Error = new XChainError { ErrorCode = ErrorCode.Success, ErrorMessage = "Success", }, }); } contractResult.Add(execRes.Response.Requests[i].ContractName + ":" + execRes.Response.Requests[i].MethodName, Encoding.ASCII.GetString(execRes.Response.Responses[i].Body.ToByteArray())); } // assemble transaction var tx = AssembleTx(execRes.UtxoOutput, this.Account, authRequire, "", 0, execRes.Response, desc); if (tx == null) { Console.WriteLine("InvokeContract: AssembleTx failed"); return(MakeErrorResponse(ErrorCode.UnknownError, null)); } // post transaction var postRes = PostTx(bcname, tx); if (postRes == null || postRes.Header.Error != Pb.XChainErrorEnum.Success) { Console.WriteLine("InvokeContract: PostTx failed"); return(MakeErrorResponse(ErrorCode.PostError, null)); } var res = new Response { Error = new XChainError { ErrorCode = ErrorCode.Success, }, Txid = XConvert.ByteArrayToHexString(tx.Txid), Data = contractResult, }; return(res); }