コード例 #1
0
ファイル: Wallet.cs プロジェクト: erikzhang/IceWallet
 public Transaction SignTransaction(UnsignedTransaction utx)
 {
     const HashType hashType = HashType.SIGHASH_ALL;
     Transaction tx = new Transaction
     {
         Version = utx.Version,
         Inputs = new TransactionInput[utx.Inputs.Length],
         Outputs = utx.Outputs,
         LockTime = utx.LockTime
     };
     for (uint i = 0; i < utx.Inputs.Length; i++)
     {
         tx.Inputs[i] = new TransactionInput
         {
             PrevHash = utx.Inputs[i].TxId,
             PrevIndex = utx.Inputs[i].Index,
             Sequence = uint.MaxValue
         };
         if (utx.Inputs[i].ScriptHash == null) return null;
         WalletEntry entry = FindEntry(utx.Inputs[i].ScriptHash);
         if (entry == null) return null;
         BigInteger[] signature;
         using (entry.Decrypt())
         {
             ECDsa signer = new ECDsa(entry.PrivateKey, ECCurve.Secp256k1);
             signature = signer.GenerateSignature(utx.GetHashForSigning(hashType, i));
         }
         byte[] sigEncoded;
         using (MemoryStream ms = new MemoryStream())
         using (BinaryWriter writer = new BinaryWriter(ms))
         {
             byte[] r = signature[0].ToByteArray().Reverse().ToArray();
             byte[] s = signature[1].ToByteArray().Reverse().ToArray();
             writer.Write((byte)0x30);
             writer.Write((byte)(2 + r.Length + 2 + s.Length));
             writer.Write((byte)0x02);
             writer.Write((byte)r.Length);
             writer.Write(r);
             writer.Write((byte)0x02);
             writer.Write((byte)s.Length);
             writer.Write(s);
             writer.Write((byte)hashType);
             writer.Flush();
             sigEncoded = ms.ToArray();
         }
         using (ScriptBuilder sb = new ScriptBuilder())
         {
             sb.Push(sigEncoded);
             sb.Push(entry.PublicKey.EncodePoint(entry.Compressed));
             tx.Inputs[i].Script = sb.ToArray();
         }
     }
     return tx;
 }
コード例 #2
0
ファイル: Wallet.cs プロジェクト: wuqionglei/IceWallet
        public Transaction SignTransaction(UnsignedTransaction utx)
        {
            const HashType hashType = HashType.SIGHASH_ALL;
            Transaction    tx       = new Transaction
            {
                Version  = utx.Version,
                Inputs   = new TransactionInput[utx.Inputs.Length],
                Outputs  = utx.Outputs,
                LockTime = utx.LockTime
            };

            for (uint i = 0; i < utx.Inputs.Length; i++)
            {
                tx.Inputs[i] = new TransactionInput
                {
                    PrevHash  = utx.Inputs[i].TxId,
                    PrevIndex = utx.Inputs[i].Index,
                    Sequence  = uint.MaxValue
                };
                if (utx.Inputs[i].ScriptHash == null)
                {
                    return(null);
                }
                WalletEntry entry = FindEntry(utx.Inputs[i].ScriptHash);
                if (entry == null)
                {
                    return(null);
                }
                BigInteger[] signature;
                using (entry.Decrypt())
                {
                    ECDsa signer = new ECDsa(entry.PrivateKey, ECCurve.Secp256k1);
                    signature = signer.GenerateSignature(utx.GetHashForSigning(hashType, i));
                }
                byte[] sigEncoded;
                using (MemoryStream ms = new MemoryStream())
                    using (BinaryWriter writer = new BinaryWriter(ms))
                    {
                        byte[] r = signature[0].ToByteArray().Reverse().ToArray();
                        byte[] s = signature[1].ToByteArray().Reverse().ToArray();
                        writer.Write((byte)0x30);
                        writer.Write((byte)(2 + r.Length + 2 + s.Length));
                        writer.Write((byte)0x02);
                        writer.Write((byte)r.Length);
                        writer.Write(r);
                        writer.Write((byte)0x02);
                        writer.Write((byte)s.Length);
                        writer.Write(s);
                        writer.Write((byte)hashType);
                        writer.Flush();
                        sigEncoded = ms.ToArray();
                    }
                using (ScriptBuilder sb = new ScriptBuilder())
                {
                    sb.Push(sigEncoded);
                    sb.Push(entry.PublicKey.EncodePoint(entry.Compressed));
                    tx.Inputs[i].Script = sb.ToArray();
                }
            }
            return(tx);
        }