Author Topic: [Request Review] Python Code for PrivKey/Address Creation [ColdStorage]  (Read 7949 times)

0 Members and 1 Guest are viewing this topic.

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc

Offline theoretical

Looks like our code expects addresses to be the full length.  It looks like our address generation code inserts padding, but yours does not.

I'm going to check into whether the cryptocurrency community has anything resembling a standard about whether addresses without padding are OK or not, and update our code to enforce the standard.  For practical purposes the code right now requires padding, but it's only sort-of enforcing it when the checksum fails with high probability.

See https://github.com/BitShares/bitshares/issues/1274
BTS- theoretical / PTS- PZxpdC8RqWsdU3pVJeobZY7JFKVPfNpy5z / BTC- 1NfGejohzoVGffAD1CnCRgo9vApjCU2viY / the delegate formerly known as drltc / Nothing said on these forums is intended to be legally binding / All opinions are my own unless otherwise noted / Take action due to my posts at your own risk

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
Odd .. gonna check on monday when back from holiday .. sorry for the inconvenience ..

deprdoo

  • Guest
The result from convertbtcpubkey.py, the BTS2LPYPSzyM3zPn9Wd2zAHfQBJ9hjDb9eF address (without the 1), isn't a valid address.

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
Interessting ... there an extra 1 after BTS ... not sure what the reason here is ... gonne check with my testunits when back home ..

Maybe @toast knows about what may have caused this ... i have a feeling this is a bug in BTS_KEY_info ... or maybe a change in the adress format that i am unaware of

deprdoo

  • Guest
xeroc,
Today I was testing some cold storage techniques when I came up with what seems like a bug to me.

The private key to produce this problem is:
5HpaxYRdcinkA2HZt47hXrm2D4JWZcc6i2oWYypHtBdThm62BZr

Quote
$ ./convertbtcpubkey.py 0338900D92A42D9D89CB1FE73072E71D97DC7F44C8165E642C35E8AB47A588A896
BTC PubKey      : 0338900D92A42D9D89CB1FE73072E71D97DC7F44C8165E642C35E8AB47A588A896
BTC Address     : 1HoMKWTmxXZDjXHDcqmBoqG9f5XpPpZssW
BTS PubKey      : BTS7G9MAa8kToxvU9zcC84eSAAgtSFvPfYDML2ie2gxUFVY6HMgoQ
BTS Address     : BTS2LPYPSzyM3zPn9Wd2zAHfQBJ9hjDb9eF

yet if I check the BTS Pubkey with bts_key_info I get:
Quote
$ ./bts_key_info BTS7G9MAa8kToxvU9zcC84eSAAgtSFvPfYDML2ie2gxUFVY6HMgoQ
{
  "public_key": "BTS7G9MAa8kToxvU9zcC84eSAAgtSFvPfYDML2ie2gxUFVY6HMgoQ",
  "native_address": "BTS12LPYPSzyM3zPn9Wd2zAHfQBJ9hjDb9eF",
  "pts_address": "Ppj8Tb9uidWHXo64zLR2xrWCuKxeCYmPPH"
}

Should I put this on github or is this the place for it?
« Last Edit: January 17, 2015, 07:10:08 pm by deprdoo »

Offline Strip

  • Full Member
  • ***
  • Posts: 63
    • View Profile
both address are valid with the same pubkey (IMHO):

take a look at the last two lines

My scripts currently only work with COMPRESSED keys .. not sure if it's worth the work to change that

Agree. No need to change anything. I just wanted to be clear for myself in how  scripts work.
Thanks for awesome work and help.
BTS: strip

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
both address are valid with the same pubkey (IMHO):
Code: [Select]
└─(%) ./bitcoin_utils.py 5JEPT35PuKrPWpQXvXaXUeVT1wfQB1tHHDk3Bj23dLcMc9LPebX                                 ─┘
secret exponent: 24752670551704840911898197285133742638496361000728503947115201457657773762440
  hex:           36b983da902fd8095730e9e5bd959c0284accc389b1ddff191b50d8bf3af2788
WIF:             Ky465vAQ5xCzNcEsVFxyX2UZqAGVHCuLrbExeLTcbPZ3x4NMJQfK
  uncompressed:  5JEPT35PuKrPWpQXvXaXUeVT1wfQB1tHHDk3Bj23dLcMc9LPebX
public pair x:   70252587760309173621949825579917202930592538592986610219712637566763860057652
public pair y:   94085665968317647470297178062363365722307526286772903085374573868739763613109
  x as hex:      9b518e3a38f38f813986371d52b24678afb4610ee548e7e161d2b70e33795e34
  y as hex:      d002998d031660ebad1dcee2596da1ca4274bc9e3ca20f1281e97d0b8b87adb5
y parity:        odd
key pair as sec: 039b518e3a38f38f813986371d52b24678afb4610ee548e7e161d2b70e33795e34
  uncompressed:  049b518e3a38f38f813986371d52b24678afb4610ee548e7e161d2b70e33795e34\
                   d002998d031660ebad1dcee2596da1ca4274bc9e3ca20f1281e97d0b8b87adb5
BTS PubKey:     BTS81dwALnisZ5kaDjvt48BUy3f67DM5LcMkqPSWtEmMMrxzGZn9x
BTS Address:    BTSCG3GYSPRrAenNiAsKNt5kUMSQRT7WdgXr
hash160:         11f4c208bc7b3de7b15dccaa9ad7f365454e2d76
  uncompressed:  da827318a633afc92841e037b2e3a85d78d7a6be
Bitcoin address: 12dwhLoEEqyvz8MpuaD6o1GiXW37JiKBbA
  uncompressed:  1LvNaciVSM3EBLTUMtg4yftVxsTLRK6vPP
take a look at the last two lines

My scripts currently only work with COMPRESSED keys .. not sure if it's worth the work to change that

Offline Strip

  • Full Member
  • ***
  • Posts: 63
    • View Profile
Different btc addresses? cant check atm .. but btc addresses for compressed keys look different from noncompressed .. not sure what pycoin ia doing there ..can you give me a pubkey which produces diff. keys?

Here is random BTC Public Key (compressed) from bitaddress.org:
039B518E3A38F38F813986371D52B24678AFB4610EE548E7E161D2B70E33795E34

pycoin gives: 1LvNaciVSM3EBLTUMtg4yftVxsTLRK6vPP (which correspond with  Bitcoin Address on bitadddress.org)
convertbtcpubkey.py: 12dwhLoEEqyvz8MpuaD6o1GiXW37JiKBbA (which correspond with Bitcoin Address Compressed on bitadddress.org)
BTS Pub keys are same: BTS81dwALnisZ5kaDjvt48BUy3f67DM5LcMkqPSWtEmMMrxzGZn9x

Here is private key for checking: 5JEPT35PuKrPWpQXvXaXUeVT1wfQB1tHHDk3Bj23dLcMc9LPebX

I think with some minor changes your code will return Bitcoin Address instead of compressed one.
BTS: strip

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
Different btc addresses? cant check atm .. but btc addresses for compressed keys look different from noncompressed .. not sure what pycoin ia doing there ..can you give me a pubkey which produces diff. keys?

Offline Strip

  • Full Member
  • ***
  • Posts: 63
    • View Profile
related thread: 
https://bitsharestalk.org/index.php?topic=8907.msg160155#msg160155

quote:

here we go .. I just updated the "pytshares" repo on github .. now there are two different scripts:

Code: [Select]
└─(1:%) ./genbtskey.py                                                                                                                                                                                                          ─┘
Secret Exponent         : b3a5255b0567f73da73a27663c0ea5998c67cb6bf860f6dad23af7c1f03290b5
Private Key             : 5KBQNg7LVWcfyZKYzYMVnwsV3d1JS351rD6EhddZvSSzpX2C34X
BTC Address             : 1Bfy6Pv7t1z4hXW89zGHbydnspevDdg9fN
--------------------------------------------------------------------------------
BTC Pubkey (compressed) : 02885d9abff5ca754bdea9c70c9fd95f3d1ea9200b563b2e43081a4d75bc63a1e5
BTC Address             : 1Bfy6Pv7t1z4hXW89zGHbydnspevDdg9fN
--------------------------------------------------------------------------------
BTS PubKey              : BTS5vYdYntohDkUyCCUmzp1QZE2isBwyS6ZARdA6P4gxWgzvFr9qz
BTS Address             : BTSPfYihuV1t7f7HczrY4oJXoX9tULALksLD
--------------------------------------------------------------------------------
┌─(xeroc@hermike:pts/2)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────(~/pytshares)─┐
└─(%) ./convertbtcpubkey.py 02885d9abff5ca754bdea9c70c9fd95f3d1ea9200b563b2e43081a4d75bc63a1e5                                                                                                                                  ─┘
BTC PubKey      : 02885d9abff5ca754bdea9c70c9fd95f3d1ea9200b563b2e43081a4d75bc63a1e5
BTC Address     : 1BSxmaYgptZ2ZZeknMN8wNjmHViyW8SpC4
BTS PubKey      : BTS5vYdYntohDkUyCCUmzp1QZE2isBwyS6ZARdA6P4gxWgzvFr9qz
BTS Address     : BTSPfYihuV1t7f7HczrY4oJXoX9tULALksLD


I've tried both btsxaddr.py (with your patch) and convertvertbtcpubkey.py. Give them BTC public key (compressed) as a parameter and they give same BTS public key BUT different BTC address. btsxaddr.py gives right BTC address.

I like what you're doing here!
BTS: strip

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
I am following the advice from
http://procbits.com/2013/08/27/generating-a-bitcoin-address-with-javascript
and use
window.crypto for picking random numbers ... take a look at the html file .. the code is quite straight forward and simple

Offline monsterer

If you are brave .. you can give this a try:
https://github.com/xeroc/jshares/archive/master.zip
Download the zip file .. unpack it and run the html file in the browser ..
it will randomly choose a private key and print the public key, private key, and bts address for it in text format and as QR code ..

Have you been careful with the RNG? There is a shitstorm at the moment in bitcoin due to private key discovery caused by bad random number generation leading to re-used R values.
My opinions do not represent those of metaexchange unless explicitly stated.
https://metaexchange.info | Bitcoin<->Altcoin exchange | Instant | Safe | Low spreads

Offline arhag

  • Hero Member
  • *****
  • Posts: 1214
    • View Profile
    • My posts on Steem
  • BitShares: arhag
  • GitHub: arhag
the huge js file has be deplpyed by the bitsharesJS library from the bishares github account .. i am just using it ..

Oh I see. Great.