Search content
Sort by

Showing 9 of 9 results by zhengjianxun
Post
Topic
Board Development & Technical Discussion
how ThreadOpenConnections work?
by
zhengjianxun
on 14/08/2018, 10:33:59 UTC
in bitcoin 0.10 and the net.cpp, i don't koow how ThreadOpenConnectionswork,because the red code is a inite loop , so why it can excute the next code

void ThreadOpenConnections()
{
    // Connect to specific addresses
    if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0)
    {
        for (int64_t nLoop = 0;; nLoop++)
        {
            ProcessOneShot();
            BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"])
            {
                CAddress addr;
                OpenNetworkConnection(addr, NULL, strAddr.c_str());
                for (int i = 0; i < 10 && i < nLoop; i++)
                {
                    MilliSleep(500);
                }
            }
            MilliSleep(500);
        }
    }


    // Initiate network connections
    int64_t nStart = GetTime();
    while (true)
    {
        ProcessOneShot();

        MilliSleep(500);

        CSemaphoreGrant grant(*semOutbound);
        boost::this_thread::interruption_point();

        // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
        if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
            static bool done = false;
            if (!done) {
                LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
                addrman.Add(Params().FixedSeeds(), CNetAddr("127.0.0.1"));
                done = true;
            }
        }

        //
        // Choose an address to connect to based on most recently seen
        //
        CAddress addrConnect;

        // Only connect out to one peer per network group (/16 for IPv4).
        // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
        int nOutbound = 0;
        set > setConnected;
        {
            LOCK(cs_vNodes);
            BOOST_FOREACH(CNode* pnode, vNodes) {
                if (!pnode->fInbound) {
                    setConnected.insert(pnode->addr.GetGroup());
                    nOutbound++;
                }
            }
        }

        int64_t nANow = GetAdjustedTime();

        int nTries = 0;
        while (true)
        {
            CAddress addr = addrman.Select();

            // if we selected an invalid address, restart
            if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
                break;

            // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
            // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
            // already-connected network ranges, ...) before trying new addrman addresses.
            nTries++;
            if (nTries > 100)
                break;

            if (IsLimited(addr))
                continue;

            // only consider very recently tried nodes after 30 failed attempts
            if (nANow - addr.nLastTry < 600 && nTries < 30)
                continue;

            // do not allow non-default ports, unless after 50 invalid addresses selected already
            if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
                continue;

            addrConnect = addr;
            break;
        }

        if (addrConnect.IsValid())
            OpenNetworkConnection(addrConnect, &grant);
    }
}
Post
Topic
Board Bitcoin Technical Support
Re: false to verify ECDSA sinature in golang
by
zhengjianxun
on 14/08/2018, 07:30:11 UTC
i make a mistake just now, now i return false too.  so can you help me find why fault??

According to your edit, you changed that part of the code ?
So now, if the signature can be verified, you'll get true.
And if the signature can not be verified, you'll get false.

Now, if you make a mistake (i assume you mean that you change some characters of the necessary data?), the function has to return false, which also will give you a false as output.


This seems to be right for me. What exactly doesn't work ? Do you mean that the function ecdsa.Verify does always return the same ?


hello friend ,very thinks your reply. don't care about true and false, it just  my error when Copy the go code。very sorry.      but  the real problem is : though the three information(public key、signatrue、and hash), i can not verify it though the go code, but someone tell me the  three information is right because he has verify in other ways.  so i want to kown  what fault in this go code make me get the false result.
i am very anxious
Post
Topic
Board Bitcoin Technical Support
Re: false to verify ECDSA sinature in golang
by
zhengjianxun
on 13/08/2018, 12:45:23 UTC
Without looking too much at it, this seems to be somehow wrong:


   if ecdsa.Verify(&rawPubKey, hash[:], &r, &s) == false {
      fmt.Printf("%s\n", "true")
   }else{
      fmt.Printf("%s\n", "false")
   }
}


It may be the case, if the signature is actually correct and your code runs properly, that you are misinterpreting the output because you are printing true if it the verify function returns false and vice versa.

So if you are actually getting the output false, the signature has been verified properly.


my friend  , i make a mistake just now, now i return false too.  so can you help me find why fault??
Post
Topic
Board Bitcoin Technical Support
false to verify ECDSA sinature in golang
by
zhengjianxun
on 13/08/2018, 12:09:17 UTC
now i have a signature in sig :3044022014d647cd08f1ea5b31d1e6539b6cbceb9182f6e7b2e29fb969354ef7e3434923022028b b4eda36af410149baa936322e7c0e46cc5540a3aa89c811bc3c360028bfd301

now i have a  hash :f27c6c3aa42563c958292922be1e53fe107f4db0dfadba11122f0b12bf77f3ab

now i have a  pubkey  :04b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576782eba6 68a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b


this is my go code to verify it but i false,it is problem me !   so why it is fault???:

package main

import (
   "crypto/ecdsa"
   "crypto/elliptic"
   "fmt"
   "math/big"
   "encoding/hex"
)


func main(){


   pubkey,_:=hex.DecodeString("b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576782eba668 a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b");



   curve := elliptic.P256()
   //length
   keyLen := len(pubkey)

   x := big.Int{}
   y := big.Int{}
   x.SetBytes(pubkey[:(keyLen / 2)])
   y.SetBytes(pubkey[(keyLen / 2):])

   rawPubKey := ecdsa.PublicKey{curve, &x, &y}


   //hash
   hash,_:=hex.DecodeString("f27c6c3aa42563c958292922be1e53fe107f4db0dfadba11122f0b12bf77f3ab")


  
   r := big.Int{}
   s := big.Int{}
   rr,_:=hex.DecodeString("14d647cd08f1ea5b31d1e6539b6cbceb9182f6e7b2e29fb969354ef7e3434923")
   ss,_:=hex.DecodeString("28bb4eda36af410149baa936322e7c0e46cc5540a3aa89c811bc3c360028bfd3")

   r.SetBytes(rr)
   s.SetBytes(ss)

  

   if ecdsa.Verify(&rawPubKey, hash[:], &r, &s) == false {
      fmt.Printf("%s\n", "false")
   }else{
      fmt.Printf("%s\n", "true")
   }

}
Post
Topic
Board Development & Technical Discussion
Re: very difficult question about real verify the signature
by
zhengjianxun
on 13/08/2018, 07:01:17 UTC
an indirect answer:
I have quickly double checked your data with some of my scripts. The replacement of the transaction and the created double hash value seems to be correct. I am not understanding the pubkey you use in the function main().

When I am using the pubkey of the pubkey script you extracted (of course without the length field and without hex "ac" at the end), then it runs ok.

######################################################################
using the reference from here: http://bitcoin.stackexchange.com/questions/46455/ I get this result:  

pubkey in HEX: 04b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576782eba6 68a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b
pubkey in PEM format:
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEsL1jQjSruxuh6YbohBhcYc9D4AH5E38j
wsQJJz6xbmU3pXZ4Lrpmin74vTs8+x7bcRerZRKbii5oHzweCQjvew==
-----END PUBLIC KEY-----

sha256: f27c6c3aa42563c958292922be1e53fe107f4db0dfadba11122f0b12bf77f3ab

ScriptSig:     3044022014d647cd08f1ea5b31d1e6539b6cbceb9182f6e7b2e29fb969354ef7e3434923022028b b4eda36af410149baa936322e7c0e46cc5540a3aa89c811bc3c360028bfd3

*** Signature Verified Successfully ***  Smiley

very thinks your work , so you make me know  the calculate hash is true、the public key is true, so the real problem is my go code about verify the signature!!
very thinks your patient, Grin Grin
but i also don't konw where my go code fault
Post
Topic
Board Development & Technical Discussion
Re: very difficult question about real verify the signature
by
zhengjianxun
on 13/08/2018, 00:01:03 UTC
very thinks your work , so you make me know  the calculate hash is true、the public key is true, so the real problem is my go code about verify the signature!!

very thinks your patient, Grin Grin
Post
Topic
Board Development & Technical Discussion
very difficult question about real verify the signature
by
zhengjianxun
on 12/08/2018, 12:04:40 UTC
this question maybe some High difficulty , but it about important detail about bitcoin.real bitcoin expert can explain it.

i want to verify the signature in bitcoin.so i usesd the information in
https://blockchain.info/rawtx/5c76eb4dfb0941856a229833ef05b2f5c669dadc98ed2a34ea11974cacba9dc7?format=hex
this website is its hex serialize.

first:
0100000001107f46ae8f3ba0f1f9c5170f950a6fb0d6461a92718b5aa06ee776c4e114afdb00000 00048473044022014d647cd08f1ea5b31d1e6539b6cbceb9182f6e7b2e29fb969354ef7e3434923022 028bb4eda36af410149baa936322e7c0e46cc5540a3aa89c811bc3c360028bfd301ffffffff020a372f2d010000001976a9148ff0d9ee3c2c4b86d19d265cbd360e95c5191e3d88ac3 2ee9f00000000001976a9145c11f917883b927eef77dc57707aeb853f6d389488ac00000000

the red is the sig

second:
delete the sig

third:
add the previous output  script
https://blockchain.info/rawtx/dbaf14e1c476e76ea05a8b71921a46d6b06f0a950f17c5f9f1a03b8fae467f10?format=hex
it is 434104b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576782 eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7bac

four :
add  to posithon of sig :
0100000001107f46ae8f3ba0f1f9c5170f950a6fb0d6461a92718b5aa06ee776c4e114afdb00000 000434104b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576 782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7bacffffffff020a372f2d0 10000001976a9148ff0d9ee3c2c4b86d19d265cbd360e95c5191e3d88ac32ee9f00000000001976 a9145c11f917883b927eef77dc57707aeb853f6d389488ac00000000

five :
add the hashtype:
01000000

so the code is :
0100000001107f46ae8f3ba0f1f9c5170f950a6fb0d6461a92718b5aa06ee776c4e114afdb00000 000434104b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576 782eba668a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7bacffffffff020a372f2d0 10000001976a9148ff0d9ee3c2c4b86d19d265cbd360e95c5191e3d88ac32ee9f00000000001976 a9145c11f917883b927eef77dc57707aeb853f6d389488ac0000000001000000

six:
double hash256 is :
f27c6c3aa42563c958292922be1e53fe107f4db0dfadba11122f0b12bf77f3ab

but when i want to verify it ,false,false,false  so  what happend , i think i am right!!


the verify go code is :

package main

import (
   "crypto/ecdsa"
   "crypto/elliptic"
   "fmt"
   "math/big"
   "encoding/hex"
)


func main(){


   pubkey,_:=hex.DecodeString("b0bd634234abbb1ba1e986e884185c61cf43e001f9137f23c2c409273eb16e6537a576782eba668 a7ef8bd3b3cfb1edb7117ab65129b8a2e681f3c1e0908ef7b");



   curve := elliptic.P256()
   //公钥的长度
   keyLen := len(pubkey)

   x := big.Int{}
   y := big.Int{}
   x.SetBytes(pubkey[:(keyLen / 2)])
   y.SetBytes(pubkey[(keyLen / 2):])

   rawPubKey := ecdsa.PublicKey{curve, &x, &y}


   //hash
   hash,_:=hex.DecodeString("f27c6c3aa42563c958292922be1e53fe107f4db0dfadba11122f0b12bf77f3ab   ")


   
   r := big.Int{}
   s := big.Int{}
   rr,_:=hex.DecodeString("14d647cd08f1ea5b31d1e6539b6cbceb9182f6e7b2e29fb969354ef7e3434923")
   ss,_:=hex.DecodeString("28bb4eda36af410149baa936322e7c0e46cc5540a3aa89c811bc3c360028bfd3")

   r.SetBytes(rr)
   s.SetBytes(ss)

   

   if ecdsa.Verify(&rawPubKey, hash[:], &r, &s) == false {
      fmt.Printf("%s\n", "true")
   }else{
      fmt.Printf("%s\n", "false")
   }

}





Post
Topic
Board Development & Technical Discussion
Re: different version have different SignatureHash?
by
zhengjianxun
on 30/07/2018, 09:46:00 UTC
ho my god ,you are the real bitcoin expert .   i am reading the  bitcoin source code .it is difficult but exciting for me. database、network  there are so many thing i should to understanding.  thank you . i hope i can  strong like you  Kiss
Post
Topic
Board Development & Technical Discussion
different version have different SignatureHash?
by
zhengjianxun
on 30/07/2018, 07:22:26 UTC
in bitcoin 0.1.5  : SignatureHash (txto-sig+scriptCode),but the scriptCode insert into the position of sig,  but now i find in bitcoin 0.10,i find the scriptCode  is append into the back!!!

in bitcoin 0.1.5  :
1: uint256 SignatureHash(CScript scriptCode, const CTransaction&
txTo, unsigned int nIn, int nHashType)
2: {
3: if (nIn >= txTo.vin.size())
4: {
5: printf("ERROR: SignatureHash() : nIn=%d out of range\n",
nIn);
6: return 1;
7: }
8: CTransaction txTmp(txTo);
9: // In case concatenating two scripts ends up with two codeseparators
10: // or an extra one at the end, this prevents all those
possible incompatibilities.
11: scriptCode.FindAndDelete(CScript(OP_CODESEPARATOR));
12: // Blank out other inputs' signatures
13: for (int i = 0; i < txTmp.vin.size(); i++)
14: txTmp.vin.scriptSig = CScript();
15: txTmp.vin[nIn].scriptSig = scriptCode;

16: // Blank out some of the outputs
17: if ((nHashType & 0x1f) == SIGHASH_NONE)
18: {
19: // Wildcard payee
20: txTmp.vout.clear();
21: // Let the others update at will
22: for (int i = 0; i < txTmp.vin.size(); i++)
23: if (i != nIn)
24: txTmp.vin.nSequence = 0;
25: }
26: else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
27: {
28: // Only lockin the txout payee at same index as txin
29: unsigned int nOut = nIn;
30: if (nOut >= txTmp.vout.size())
31: {
32: printf("ERROR: SignatureHash() : nOut=%d out of
range\n", nOut);
33: return 1;
34: }
35: txTmp.vout.resize(nOut+1);
36: for (int i = 0; i < nOut; i++)
37: txTmp.vout.SetNull();
38: // Let the others update at will
39: for (int i = 0; i < txTmp.vin.size(); i++)
40: if (i != nIn)
41: txTmp.vin.nSequence = 0;
42: }
43: // Blank out other inputs completely, not recommended for open
transactions
44: if (nHashType & SIGHASH_ANYONECANPAY)
45: {
46: txTmp.vin[0] = txTmp.vin[nIn];
47: txTmp.vin.resize(1);
48: }
49: // Serialize and hash
50: CDataStream ss(SER_GETHASH);
51: ss.reserve(10000);
52: ss << txTmp << nHashType;
53: return Hash(ss.begin(), ss.end());
54: }




in  0.10:

uint256 SignatureHash(const CScript& scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
{
    if (nIn >= txTo.vin.size()) {
        //  nIn out of range
        return 1;
    }

    // Check for invalid use of SIGHASH_SINGLE
    if ((nHashType & 0x1f) == SIGHASH_SINGLE) {
        if (nIn >= txTo.vout.size()) {
            //  nOut out of range
            return 1;
        }
    }

    // Wrapper to serialize only the necessary parts of the transaction being signed
    CTransactionSignatureSerializer txTmp(txTo, scriptCode, nIn, nHashType);


    // Serialize and hash
    CHashWriter ss(SER_GETHASH, 0);
    ss << txTmp << nHashType;
    return ss.GetHash();
}


so the diffirent version  mean the Signature is different Signature even though the  everything  is same? that ' s amazy!