Exemple #1
0
        private static Transaction SignWithWallet(Transaction tx, params object[] args)
        {
            if (tx == null)
            {
                throw new ArgumentNullException("tx");
            }
            tx.ToJson();

            var wallet = new Neo.Wallets.NEP6.NEP6Wallet(new WalletIndexer("Index_0001E240"), (string)args[0]);

            try
            {
                wallet.Unlock((string)args[1]);
            }
            catch (Exception)
            {
                Console.WriteLine("password error");
            }

            //Signature
            var context = new ContractParametersContext(tx);

            wallet.Sign(context);
            if (context.Completed)
            {
                Console.WriteLine("Sign successful");
                tx.Witnesses = context.GetWitnesses();
            }
            else
            {
                Console.WriteLine("Sign failed");
            }
            return(tx);
        }
 static bool TryGetNEP6Wallet(string path, string password, ProtocolSettings settings, [MaybeNullWhen(false)] out Wallet wallet, [MaybeNullWhen(false)] out UInt160 accountHash)
 {
     try
     {
         var nep6wallet = new Neo.Wallets.NEP6.NEP6Wallet(path, settings);
         using var unlock = nep6wallet.Unlock(password);
         var nep6account = nep6wallet.GetAccounts().SingleOrDefault(a => a.IsDefault)
                           ?? nep6wallet.GetAccounts().SingleOrDefault()
                           ?? throw new InvalidOperationException("Neo-express only supports NEP-6 wallets with a single default account or a single account");
         if (nep6account.IsMultiSigContract())
         {
             throw new Exception("Neo-express doesn't supports multi-sig NEP-6 accounts");
         }
         var keyPair = nep6account.GetKey() ?? throw new Exception("account.GetKey() returned null");
         wallet = new DevWallet(settings, string.Empty);
         var account = wallet.CreateAccount(keyPair.PrivateKey);
         accountHash = account.ScriptHash;
         return(true);
     }
     catch
     {
         wallet      = null;
         accountHash = null;
         return(false);
     }
 }
Exemple #3
0
        public void Export(string filename, string password)
        {
            var nep6Wallet = new Neo.Wallets.NEP6.NEP6Wallet(null, filename, Name);

            nep6Wallet.Unlock(password);
            foreach (var account in GetAccounts())
            {
                nep6Wallet.CreateAccount(account.Contract, account.GetKey());
            }
            nep6Wallet.Save();
        }
Exemple #4
0
        public static void Refund()
        {
            var inputs = new List <CoinReference> {
                new CoinReference()
                {
                    PrevHash  = new UInt256("0x44d5a5ef32c8ec780de59ca59cb799efd1bf3051d9a2c94a2b1d13af34abe7ca".Remove(0, 2).HexToBytes().Reverse().ToArray()),
                    PrevIndex = 0
                }
            }.ToArray();

            var outputs = new List <TransactionOutput> {
                new TransactionOutput()
                {
                    AssetId    = Blockchain.UtilityToken.Hash, //Asset Id, this is GAS
                    ScriptHash = ScriptHash,                   //CGAS 地址
                    Value      = new Fixed8((long)(9.99 * (long)Math.Pow(10, 8)))
                }
            }.ToArray();

            Transaction tx = null;

            var applicationScript = new byte[0];

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitAppCall(ScriptHash, "refund", User);
                sb.Emit(OpCode.THROWIFNOT);
                applicationScript = sb.ToArray();
            }

            tx = new InvocationTransaction
            {
                Version    = 0,
                Script     = applicationScript,
                Outputs    = outputs,
                Inputs     = inputs,
                Attributes = new TransactionAttribute[]
                {
                    new TransactionAttribute
                    {
                        Usage = TransactionAttributeUsage.Script,
                        Data  = User.ToArray()//附加人的 Script Hash
                    }
                }
            };

            //Open wallet
            var wallet = new Neo.Wallets.NEP6.NEP6Wallet(new WalletIndexer("Index_0001E240"), "1.json");

            try
            {
                wallet.Unlock("11111111");
            }
            catch (Exception)
            {
                Console.WriteLine("password error");
            }

            //Sign in wallet 生成附加人的签名
            var context             = new ContractParametersContext(tx);
            var additionalSignature = new byte[0];

            foreach (UInt160 hash in context.ScriptHashes)
            {
                if (hash == User)
                {
                    WalletAccount account = wallet.GetAccount(hash);
                    if (account?.HasKey != true)
                    {
                        continue;
                    }
                    KeyPair key = account.GetKey();
                    additionalSignature = context.Verifiable.Sign(key);
                }
            }
            var additionalVerificationScript = new byte[0];

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitPush(additionalSignature);
                additionalVerificationScript = sb.ToArray();
            }
            var verificationScript = new byte[0];

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitPush(2);
                sb.EmitPush("1");
                verificationScript = sb.ToArray();
            }
            var witness = new Witness
            {
                InvocationScript   = verificationScript,
                VerificationScript = Blockchain.Singleton.Store.GetContracts().TryGet(ScriptHash).Script
            };
            var additionalWitness = new Witness
            {
                InvocationScript   = additionalVerificationScript,
                VerificationScript = UserScript
            };
            var witnesses = new Witness[2] {
                witness, additionalWitness
            };

            tx.Witnesses = witnesses.ToList().OrderBy(p => p.ScriptHash).ToArray();

            Verify(tx);
        }