예제 #1
0
        // Set the data path of the account address and private key
        /// <summary>Set the data path of the account address and private key</summary>
        /// <param name="path">the folder contains user's address and private key</param>
        /// <returns>return true if set success</returns>
        public bool SetAccountByPath(string path)
        {
            try
            {
                using (StreamReader sr = new StreamReader(path + "/address"))
                {
                    var addr = sr.ReadToEnd();
                    this.account.Address = addr;
                }
                using (StreamReader sr = new StreamReader(path + "/private.key"))
                {
                    var privkeyStr = sr.ReadToEnd();
                    var privkey    = new AccountPrivateKey();
                    privkey.ParseJSON(privkeyStr);
                    this.account.PrivateKey = privkey;
                }
                using (StreamReader sr = new StreamReader(path + "/public.key"))
                {
                    var pubkeyStr = sr.ReadToEnd();
                    var pubkey    = new AccountPublicKey();
                    pubkey.ParseJSON(pubkeyStr);
                    this.account.PublicKey = pubkey;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception in SetAccountByPath, err=" + e);
                return(false);
            }

            return(true);
        }
예제 #2
0
        /// <summary>SignHash sign data using given private key and return ASN.1 DER encoded signature</summary>
        public static byte[] SignHash(AccountPrivateKey privateKey, byte[] data)
        {
            var ecdsa   = loadECDSAFromPrivateKey(privateKey);
            var sign    = ecdsa.SignHash(data);
            var halfLen = sign.Length / 2;
            // encode sign using ASN.1
            var r = new byte[halfLen];
            var s = new byte[halfLen];

            Array.Copy(sign, 0, r, 0, halfLen);
            Array.Copy(sign, halfLen, s, 0, halfLen);
            return(Asn1EncodeSign(r, s));
        }
예제 #3
0
        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));
        }
예제 #4
0
        private Pb.PreExecWithSelectUTXOResponse PreExecWithSelectUTXO(
            string bcname, string address, AccountPrivateKey privkey, BigInteger amount, string contractName,
            string method, SortedDictionary <string, byte[]> args, ContactVMType.Type contractType, string initiator,
            List <string> authRequire)
        {
            var header = GetDefaultHeader();
            var req    = new Pb.PreExecWithSelectUTXORequest
            {
                Header      = header,
                Bcname      = bcname,
                Address     = address,
                TotalAmount = (long)amount,
                NeedLock    = false,
                Request     = new Pb.InvokeRPCRequest
                {
                    Header    = header,
                    Bcname    = bcname,
                    Initiator = initiator,
                },
            };

            if (authRequire != null)
            {
                req.Request.AuthRequire.AddRange(authRequire);
            }

            var invoke = new Pb.InvokeRequest
            {
                ModuleName = ContactVMType.GetNameByType(contractType),
                MethodName = method,
            };

            if (contractName != null)
            {
                invoke.ContractName = contractName;
            }
            foreach (var item in args)
            {
                invoke.Args.Add(item.Key, ByteString.CopyFrom(item.Value));
            }
            req.Request.Requests.Add(invoke);
            var res = client.PreExecWithSelectUTXO(req);

            if (res.Header.Error != Pb.XChainErrorEnum.Success)
            {
                Console.WriteLine("PreExecWithSelectUTXO failed, error code=" + (int)res.Header.Error);
                return(null);
            }
            return(res);
        }