-
Notifications
You must be signed in to change notification settings - Fork 0
My notes for Komodo DeFi API
How to build and user Trezor emulator (Note that I tried only legacy emulator "One" version):
- Install and always work from Poetry shell to build emulator
- the executable is
trezor-firmware/legacy/firmware/trezor.elf
(no params) - Note that Emulator requires window GUI (can't run on VPC). Update: you can set env SDL_VIDEODRIVER=dummy and run it nointeractive
- to configure Emulator - create a wallet you need to use Trezor Suite (web or GUI). Trezor Suite needs the bridge which can be downloaded from trezor.io (as dmg for macos. I installed it and the bridge started automatically. I could not connect with the bridge autostarted with no params, so I copied it from the install directory and removed the app). I ran bridge as
./trezord -e 21324
. Note that you need to run trezord before Trezor Suite (if it is desktop version) as it loads its own version which cannot connect to legacy trezor for some reason (wrong -e default param possibly) - When started Trezor Suite wait for some time to let it find the trezor emulator.
- Note that rebuilding emulator overrides emulator flash file emulator.img (with your wallet setting where apparently your wallet seed is stored), so copy it before rebuilding
- When configure the wallet do not set PIN (not supported in adex yet)
- Perform wallet backup when you are recovering it after the emulator.img was lost - emulator will show on its screen wallet words - write them down
- Before running a komodo-defi test for emulator, stop the Trezor Suite. Also be aware emulator may hang sometimes and needs to be restarted.
BTW there is trezor-user-env project which provides a web interface to start all emulators version. It requires Xquartz lib installed. But trezor-user-env is basically not needed, you can run emulator without it just from cmd line.
Yet another concise instruction to build and use Trezor Emulator:
https://github.com/MetaMask/metamask-extension/blob/develop/docs/trezor-emulator.md
Note dumpprivkey is supported only for legacy wallets:
./bitcoin-cli -testnet help dumpprivkey
dumpprivkey "address"
Reveals the private key corresponding to 'address'.
Then the importprivkey can be used with this output
Note: This command is only compatible with legacy wallets.
Stop bitcoind and delete peer files:
~/.bitcoin/testnet3/peers.dat
~/.bitcoin/testnet3/anchors.dat
Run command: ./bitcoind -testnet -port=7777 -dnsseed=0
To clear mempool stop bitcoind and delete ~/.bitcoin/testnet3/mempool.dat
We have segwit and non-segwit utxo coins' configurations in the 'coins' configuration file. F.e. we have BTC and BTC-segwit coin configs for the same blockchain BTC. For users BTC or BTC-segwit means that their address in the wallet is either standard ('1...') or native segwit ('bc1..'). For that this coins param is responsible "address_format": {"format": "segwit"}
: when a coin is activated the address format in its activation param is checked against the address_format in the coin config, so it would not allow to activate a segwit coin with non-segwit address format.
There is another param in the coin config "segwit" param which actually tells to the code that the segwit feature is enabled on this network. So if "segwit" is set to false for some coin then the API code won't allow to send value to segwit addresses for that coin.
If the HD wallet API feature is used then derivation_path should be set in the coins config which is used to generate user addresses based on BIP32 schema. For non-segwit utxo coins BIP44 note is used to for derivation_path which should have "m/44'"
as the purpose field in it. For segwit coins BIP84 is used and derivation_path starts with "m/84'"
. That means that for segwit coins we must ensure the correct 84 purpose field in the config, otherwise accounts will be created differently from other apps, including trezor firmware, which is not good.
I created an issue that would prevent from mistakes when derivation_path in coins file is set incorrectly to m/44' for segwit coins: https://github.com/KomodoPlatform/komodo-defi-framework/issues/1995
There are 2 options for coin initialisation in Komodo DeFi API: legacy and a new one. We will use the standalone coin as an example of both paths:
Before any coin addition the crypto_ctx in MmCtx is initialised at mm2 startup in lp_init
fn. Param "passphrase" must be set in ctx.conf for crypto_ctx init. The crypto_ctx is initialised depending on "enable_hd" ctx.conf param, either as KeyPairPolicy::Iguana or GlobalHDAccount
(see CryptoCtx::init_with_global_hd_account
and CryptoCtx::init_with_iguana_passphrase
fn). So currently MmCtx::crypto_ctx
is always initialised with a keypair (Note the Trezor context cannot be initialised at mm2 startup).
In the legacy init option each coin can be initialised and added by "enable" or "electrum" calls. In this case we still cannot add Trezor policy as the legacy case does not allow user interaction needed for hardware wallet. In legacy case lp_coininit
fn is called which gets PrivKeyBuildPolicy enum from MmCtx and calls utxo_standard_coin_with_policy
fn to build the coin with UtxoArcBuilder::new().build()
object. Note that priv_key_policy
field of PrivKeyActivationPolicy type in the UtxoActivationParams is ignored here.
The new init option is spawned in a thread: the InitStandaloneCoinTask run
fn begins with init_standalone_coin
fn then the result is obtained with get_activation_result
fn. The init_standalone_coin
fn from init_utxo_standard_activation.rs
gets PrivKeyBuildPolicy enum from the priv_key_policy
field of PrivKeyActivationPolicy type in the UtxoActivationParams, and now it can be already Trezor policy.
Then the same UtxoArcBuilder::new().build()
object is used here too to create and add the coin object.
BTW maybe there is a bug in new init option:
For the new init option the initialisation result should be obtained with get_activation_result
fn, which calls enable_coin_balance
fn, which in turn calls enable_hd_wallet
fn for the DerivationMethod::HDWallet path. This enable_hd_wallet
fn, if the wallet accounts list is empty, creates a new account with coin.create_new_account
fn. This coin.create_new_account
creates a new account with INIT_ACCOUNT_ID = 0 and calls coin.extract_extended_pubkey
fn which always extracts a pubkey for trezor coin or throws CoinDoesntSupportTrezor error. So it looks like the new init option will work only from trezor wallet (and would fail if we call it with PrivKeyActivationPolicy::ContextPrivKey (meaning Iguana or HdWallet).
Update: fixed in evm-hd-wallet branch, check the new version of extract_extended_pubkey
fn in lp_coins.rs there