Skip to content

Commit

Permalink
[FIX] Wrong network ID in path
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed Dec 11, 2016
1 parent e141ae8 commit df4a066
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -159,7 +159,7 @@ This is a simple wallet implementation using sqlalchemy and sqllite3 to store ke

```python
from bitcoinlib.wallets import HDWallet

# -- Create New Wallet and Generate a some new Keys --
wallet = HDWallet.create(name='Personal', network='testnet')
wallet.new_account()
Expand All @@ -170,7 +170,7 @@ new_key4 = wallet.new_key(change=1)
donations_account = wallet.new_account()
new_key5 = wallet.new_key(account_id=donations_account.account_id)
wallet.info(detail=3)

# -- Create New Wallet with Testnet master key and account ID 251 --
wallet_import = HDWallet.create(
name='TestNetWallet',
Expand All @@ -181,7 +181,7 @@ wallet_import = HDWallet.create(
wallet_import.new_account()
wallet_import.new_key("Faucet gift")
wallet_import.info(detail=3)

# -- Create New Wallet with account (depth=3) private key on bitcoin network and purpose 0 --
wallet_import2 = HDWallet.create(
name='Company Wallet',
Expand Down
32 changes: 21 additions & 11 deletions bitcoinlib/wallets.py
Expand Up @@ -39,16 +39,15 @@ def from_key(name, wallet_id, session, key='', account_id=0, network='bitcoin',
if k.depth() != len(path.split('/'))-1:
if path == 'm' and k.depth() == 3:
# Create path when importing new account-key
networkcode = 0
networkcode = networks.NETWORKS[network]['bip44_cointype']
path = "m/%d'/%d'/%d'" % (purpose, networkcode, account_id)
else:
print(path)
raise WalletError("Key depth of %d does not match path lenght of %d" %
raise WalletError("Key depth of %d does not match path lenght of %d" %
(k.depth(), len(path.split('/')) - 1))

wk = session.query(DbWalletKey).filter(or_(DbWalletKey.key == str(k.private()),
DbWalletKey.key_wif == k.extended_wif(),
DbWalletKey.address == k.public().address())).first()
DbWalletKey.key_wif == k.extended_wif(),
DbWalletKey.address == k.public().address())).first()
if wk:
return HDWalletKey(wk.id, session)

Expand Down Expand Up @@ -217,7 +216,7 @@ def new_key(self, name='', account_id=0, change=0, max_depth=5):
def new_key_change(self, name='', account_id=0):
return self.new_key(name=name, account_id=account_id, change=1)

def new_account(self, name='', account_id=0):
def new_account(self, name='', account_id=0, network='bitcoin'):
if self.keys(account_id=account_id):
last_id = self.session.query(DbWalletKey). \
filter_by(wallet_id=self.wallet_id, purpose=self.purpose). \
Expand Down Expand Up @@ -304,13 +303,24 @@ def info(self, detail=0):
# -- Create New Wallet with Testnet master key and account ID 251 --
wallet_import = HDWallet.create(
name='TestNetWallet',
key='tprv8ZgxMBicQKsPeWn8NtYVK5Hagad84UEPEs85EciCzf8xYWocuJovxsoNoxZAgfSrCp2xa6DdhDrzYVE8UXF75r2dKePy'
'A7irEvBoe4aAn52',
key='tprv8ZgxMBicQKsPeWn8NtYVK5Hagad84UEPEs85EciCzf8xYWocuJovxsoNoxZAgfSrCp2xa6DdhDrzYVE8UXF75r2dKePyA'
'7irEvBoe4aAn52',
network='testnet',
account_id=251,
databasefile=test_database)
wallet_import.new_account()
wallet_import.new_key("Faucet gift")
wallet_import.new_account(account_id=99)
wallet_import.new_key(account_id=99, name="Faucet gift")
wallet_import.new_key_change(account_id=99, name="Faucet gift (Change)")
wallet_import.info(detail=3)

# -- Import Account Bitcoin Testnet key with depth 3
accountkey = 'tprv8h4wEmfC2aSckSCYa68t8MhL7F8p9xAy322B5d6ipzY5ZWGGwksJMoajMCqd73cP4EVRygPQubgJPu9duBzPn3QV' \
'8Y7KbKUnaMzx9nnsSvh'
wallet_import = HDWallet.create(
databasefile=test_database,
name='test_wallet_import_account',
key=accountkey,
network='testnet',
account_id=99)
wallet_import.info(detail=3)

# -- Create New Wallet with account (depth=3) private key on bitcoin network and purpose 0 --
Expand Down
29 changes: 16 additions & 13 deletions tests/wallets_test.py
Expand Up @@ -2,7 +2,7 @@
#
# bitcoinlib - Compact Python Bitcoin Library
# Unit Tests for Wallet Class
# © 2016 November - 1200 Web Development <http://1200wd.com/>
# © 2016 December - 1200 Web Development <http://1200wd.com/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -57,37 +57,40 @@ def test_wallet_import(self):
wallet_import = HDWallet.create(
databasefile=DATABASEFILE_UNITTESTS,
name='test_wallet_import',
network='testnet',
key=keystr)
wallet_import.new_account(account_id=99)
self.assertEqual(wallet_import.main_key.key_wif, keystr)
self.assertEqual(wallet_import.main_key.address, u'n3UKaXBRDhTVpkvgRH7eARZFsYE989bHjw')
self.assertEqual(wallet_import.main_key.path, 'm')
wallet_import.info(detail=3)

def test_wallet_import_account(self):
accountkey = 'tprv8h4wEmfC2aSYN5wABvg9Wucyc4itsK2QSEmKpRyDESvqJAEiFZ18Qmz3xt9oWHBWLoq9Ks36NAkRKyHDYP6KYqYkH' \
'GZpdAqnj61cAgACmUY'
accountkey = 'tprv8h4wEmfC2aSckSCYa68t8MhL7F8p9xAy322B5d6ipzY5ZWGGwksJMoajMCqd73cP4EVRygPQubgJPu9duBzPn3QV' \
'8Y7KbKUnaMzx9nnsSvh'
wallet_import = HDWallet.create(
databasefile=DATABASEFILE_UNITTESTS,
name='test_wallet_import_account',
key=accountkey,
network='testnet',
account_id=99)
self.assertEqual(wallet_import.main_key.key_wif, accountkey)
self.assertEqual(wallet_import.main_key.address, u'mtvLfcWMQPHVjjoKPR2Pyr42tB3BAfXQ4R')
self.assertEqual(wallet_import.main_key.path, "m/44'/0'/99'")
self.assertEqual(wallet_import.main_key.address, u'mowRx2TNXTgRSUmepjqhx5C1TTigmHLGRh')
self.assertEqual(wallet_import.main_key.path, "m/44'/1'/99'")

def test_wallet_import_account_new_keys(self):
accountkey = 'tprv8h4wEmfC2aSYN5wABvg9Wucyc4itsK2QSEmKpRyDESvqJAEiFZ18Qmz3xt9oWHBWLoq9Ks36NAkRKyHDYP6KYqYkH' \
'GZpdAqnj61cAgACmUY'
accountkey = 'tprv8h4wEmfC2aSckSCYa68t8MhL7F8p9xAy322B5d6ipzY5ZWGGwksJMoajMCqd73cP4EVRygPQubgJPu9duBzPn3QV' \
'8Y7KbKUnaMzx9nnsSvh'
wallet_import = HDWallet.create(
databasefile=DATABASEFILE_UNITTESTS,
name='test_wallet_import_account_new_key',
key=accountkey,
network='testnet',
account_id=99)
newkey = wallet_import.new_key(account_id=99)
newkey_change = wallet_import.new_key(account_id=99, change=1, name='change')
wallet_import.info(detail=3)
newkey_change = wallet_import.new_key_change(account_id=99, name='change')
self.assertEqual(wallet_import.main_key.key_wif, accountkey)
self.assertEqual(newkey.address, u'mweZrbny4fmpCmQw9hJH7EVfkuWX8te9jc')
self.assertEqual(newkey.path, "m/44'/0'/99'/0/0")
self.assertEqual(newkey_change.address, u'mrq34rE7e6E5mfhYeS7s2JinUX78e7r5cZ')
self.assertEqual(newkey_change.path, "m/44'/0'/99'/1/0")
self.assertEqual(newkey.address, u'mfvFzusKPZzGBAhS69AWvziRPjamtRhYpZ')
self.assertEqual(newkey.path, "m/44'/1'/99'/0/0")
self.assertEqual(newkey_change.address, u'mkzpsGwaUU7rYzrDZZVXFne7dXEeo6Zpw2')
self.assertEqual(newkey_change.path, "m/44'/1'/99'/1/0")

0 comments on commit df4a066

Please sign in to comment.