• U2_859751
  • 4.9MB
  • zip
  • 0
  • VIP专享
  • 0
  • 2022-06-05 06:49
# Multi-Blockchain Wallet in Python ![newtons-coin-cradle](Images/newtons-coin-cradle.jpg) ## Background Your new startup is focusing on building a portfolio management system that supports not only traditional assets like gold, silver, stocks, etc, but crypto-assets as well! The problem is, there are so many coins out there! It's a good thing you understand how HD wallets work, since you'll need to build out a system that can create them. You're in a race to get to the market. There aren't as many tools available in Python for this sort of thing, yet. Thankfully, you've found a command line tool, `hd-wallet-derive` that supports not only BIP32, BIP39, and BIP44, but also supports non-standard derivation paths for the most popular wallets out there today! However, you need to integrate the script into your backend with your dear old friend, Python. Once you've integrated this "universal" wallet, you can begin to manage billions of addresses across 300+ coins, giving you a serious edge against the competition. In this assignment, however, you will only need to get 2 coins working: Ethereum and Bitcoin Testnet. Ethereum keys are the same format on any network, so the Ethereum keys should work with your custom networks or testnets. ## Dependencies - PHP must be installed on your operating system (any version, 5 or 7). Don't worry, you will *not* need to know any PHP. - You will need to clone the [`hd-wallet-derive`]( tool. - [`bit`]( Python Bitcoin library. - [``]( Python Ethereum library. ## Instructions ### Project setup - Create a project directory called `wallet` and `cd` into it. - Clone the `hd-wallet-derive` tool into this folder and install it using the instructions on its ``. - Create a symlink called `derive` for the `hd-wallet-derive/hd-wallet-derive.php` script into the top level project directory like so: `ln -s hd-wallet-derive/hd-wallet-derive.php derive` This will clean up the command needed to run the script in our code, as we can call `./derive` instead of `./hd-wallet-derive/hd-wallet-derive.php`. - Test that you can run the `./derive` script properly, use one of the examples on the repo's `` - Create a file called `` -- this will be your universal wallet script. You can use [this starter code](Starter-Code/ as a starting point. Your directory tree should look something like this: ![directory-tree](Images/tree.png) ### Setup constants - In a separate file, ``, set the following constants: - `BTC = 'btc'` - `ETH = 'eth'` - `BTCTEST = 'btc-test'` - In ``, import all constants: `from constants import *` - Use these anytime you reference these strings, both in function calls, and in setting object keys. ### Generate a Mnemonic - Generate a new 12 word mnemonic using `hd-wallet-derive` or by using [this tool]( - Set this mnemonic as an environment variable, and include the one you generated as a fallback using: `mnemonic = os.getenv('MNEMONIC', 'insert mnemonic here')` ### Deriving the wallet keys - Use the `subprocess` library to call the `./derive` script from Python. Make sure to properly wait for the process. - The following flags must be passed into the shell command as variables: - Mnemonic (`--mnemonic`) must be set from an environment variable, or default to a test mnemonic - Coin (`--coin`) - Numderive (`--numderive`) to set number of child keys generated - Set the `--format=json` flag, then parse the output into a JSON object using `json.loads(output)` - You should wrap all of this into one function, called `derive_wallets` - Create an object called `coins` that derives `ETH` and `BTCTEST` wallets with this function. When done properly, the final object should look something like this (there are only 3 children each in this image): ![wallet-object](Images/wallet-object.png) You should now be able to select child accounts (and thus, private keys) by calling `coins[COINTYPE][INDEX]['privkey']`. ### Linking the transaction signing libraries Now, we need to use `bit` and `` to leverage the keys we've got in the `coins` object. You will need to create three more functions: - `priv_key_to_account` -- this will convert the `privkey` string in a child key to an account object that `bit` or `` can use to transact. This function needs the following parameters: - `coin` -- the coin type (defined in ``). - `priv_key` -- the `privkey` string will be passed through here. You will need to check the coin, then return one of the following functions based on the library: - For `ETH`, return `Account.privateKeyToAccount(priv_key)` - For `BTCTEST`, return `PrivateKeyTestnet(priv_key)` - `create_tx` -- this will create the raw, unsigned transaction that contains all metadata needed to transact. This function needs the following parameters: - `coin` -- the coin type (defined in ``). - `account` -- the account object from `priv_key_to_account`. - `to` -- the recipient address. - `amount` -- the amount of the coin to send. You will need to check the coin, then return one of the following functions based on the library: - For `ETH`, return an object containing `to`, `from`, `value`, `gas`, `gasPrice`, `nonce`, and `chainID`. Make sure to calculate all of these values properly using ``! - For `BTCTEST`, return `PrivateKeyTestnet.prepare_transaction(account.address, [(to, amount, BTC)])` - `send_tx` -- this will call `create_tx`, sign the transaction, then send it to the designated network. This function needs the following parameters: - `coin` -- the coin type (defined in ``). - `account` -- the account object from `priv_key_to_account`. - `to` -- the recipient address. - `amount` -- the amount of the coin to send. You may notice these are the exact same parameters as `create_tx`. `send_tx` will call `create_tx`, so it needs all of this information available. You will need to check the coin, then create a `raw_tx` object by calling `create_tx`. Then, you will need to sign the `raw_tx` using `bit` or `` (hint: the account objects have a sign transaction function within). Once you've signed the transaction, you will need to send it to the designated blockchain network. - For `ETH`, return `w3.eth.sendRawTransaction(signed.rawTransaction)` - For `BTCTEST`, return `NetworkAPI.broadcast_tx_testnet(signed)` ### Send some transactions! Now, you should be able to fund these wallets using testnet faucets. Open up a new terminal window inside of `wallet`, then run `python`. Within the Python shell, run `from wallet import *` -- you can now access the functions interactively. You'll need to set the account with `priv_key_to_account` and use `send_tx` to send transactions. #### Bitcoin Testnet transaction - Fund a `BTCTEST` address using [this testnet faucet]( - Use a [block explorer]( to watch transactions on the address. - Send a transaction to another testnet address (either one of your own, or the faucet's). - Screenshot the confirmation of the transaction like so: ![btc-test](Images/btc-test.png) #### Local PoA Ethereum transaction - Add one of the `ETH` addresses to the pre-allocated accounts in your `networkname.json`. - Delete the `geth` folder in each node, then re-initialize using `geth --datadir nodeX init networkname.json`. This will create a new chain, and will pre-fund the new account. - [Add the following middleware]( to `` to support the PoA algorithm: ``` from web3.middleware import geth_poa_middleware w3.middleware_onion.inject(geth_poa_middleware, layer=0) ``` - Due to a bug in `
    • Python
    • python
      Python 这是用于大多数基本和简单python代码的示例模板的存储库。
    • Python
      Python项目 Python是一些简单的Python项目,它是一种解释性的高级通用编程语言,我愿意学习它为将来的许多项目做出贡献! 入门 您必须安装Python 3.9.2才能尝试这些项目。 这些项目来自各种书籍,您可以根据需要进行...
    • Python
      Python Dit是Mijn储存库van Python
    • python
      Python 基本的python编程
    • Python
      Python python和pyspark
    • python
    • python学习
      python学习 安装Python 我们转到Python页面进行初始下载:( )在为Mac OSX选择了最新版本(3.9.2)之后,我将其下载到了我的计算机上。 然后,我们搜索了“ Python入门”,并确认VS Code是适用于Python的可行代码...
    • Fluent Python
      This book was written for practicing Python programmers who want to become proficient in Python 3. If you know Python 2 but are willing to migrate to Python 3.4 or later, you should be fine. At the ...
    • Python
      Python 该存储库包含与Python编程研究相关的文档