Skip to content

Commit 6c20f93

Browse files
committed
Add: phantom wallet client
1 parent e5d930e commit 6c20f93

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

clients/phantom.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
3+
from typing import Type
4+
5+
import json
6+
7+
from hdwallet.mnemonics import BIP39Mnemonic
8+
from hdwallet.cryptocurrencies import (
9+
ICryptocurrency, Bitcoin, Ethereum, Solana
10+
)
11+
from hdwallet.hds import (
12+
IHD, BIP32HD, BIP44HD, BIP49HD, BIP84HD
13+
)
14+
from hdwallet.derivations import (
15+
IDerivation, CustomDerivation, BIP44Derivation, BIP49Derivation, BIP84Derivation
16+
)
17+
from hdwallet.const import PUBLIC_KEY_TYPES
18+
from hdwallet.libs.base58 import encode
19+
from hdwallet.utils import get_bytes
20+
from hdwallet import HDWallet
21+
22+
23+
mnemonic: BIP39Mnemonic = BIP39Mnemonic(
24+
mnemonic="abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
25+
)
26+
27+
# Phantom protocols
28+
data: dict = {
29+
"solana": {
30+
"hd": BIP32HD,
31+
"derivation": CustomDerivation(path=f"m/44'/{Solana.COIN_TYPE}'/0'/0'")
32+
},
33+
"ethereum": {
34+
"hd": BIP44HD,
35+
"derivation": BIP44Derivation(coin_type=Ethereum.COIN_TYPE)
36+
},
37+
"bitcoin": {
38+
"legacy": {
39+
"hd": BIP44HD,
40+
"derivation": BIP44Derivation(coin_type=Bitcoin.COIN_TYPE)
41+
},
42+
"nested-segwit": {
43+
"hd": BIP49HD,
44+
"derivation": BIP49Derivation(coin_type=Bitcoin.COIN_TYPE)
45+
},
46+
"native-segwit": {
47+
"hd": BIP84HD,
48+
"derivation": BIP84Derivation(coin_type=Bitcoin.COIN_TYPE)
49+
}
50+
}
51+
}
52+
53+
def generate_phantom_hdwallet(cryptocurrency: Type[ICryptocurrency], hd: Type[IHD], network: str, derivation: IDerivation, **kwargs) -> HDWallet:
54+
return HDWallet(cryptocurrency=cryptocurrency, hd=hd, network=network, kwargs=kwargs).from_mnemonic(mnemonic=mnemonic).from_derivation(derivation=derivation)
55+
56+
print("Mnemonic | Seed phrase:", mnemonic.mnemonic(), "\n")
57+
58+
# Solana
59+
solana_hdwallet: HDWallet = generate_phantom_hdwallet(
60+
cryptocurrency=Solana,
61+
hd=data["solana"]["hd"],
62+
network=Solana.NETWORKS.MAINNET,
63+
derivation=data["solana"]["derivation"]
64+
)
65+
print(f"{solana_hdwallet.cryptocurrency()} ({solana_hdwallet.symbol()}) wallet:", json.dumps(dict(
66+
path=solana_hdwallet.path(),
67+
base58=encode(get_bytes(
68+
solana_hdwallet.private_key() + solana_hdwallet.public_key()
69+
)),
70+
private_key=solana_hdwallet.private_key(),
71+
public_key=solana_hdwallet.public_key(),
72+
address=solana_hdwallet.address()
73+
), indent=4), "\n")
74+
75+
# Ethereum
76+
ethereum_hdwallet: HDWallet = generate_phantom_hdwallet(
77+
cryptocurrency=Ethereum,
78+
hd=data["ethereum"]["hd"],
79+
network=Ethereum.NETWORKS.MAINNET,
80+
derivation=data["ethereum"]["derivation"]
81+
)
82+
print(f"{ethereum_hdwallet.cryptocurrency()} ({ethereum_hdwallet.symbol()}) wallet:", json.dumps(dict(
83+
path=ethereum_hdwallet.path(),
84+
private_key=f"0x{ethereum_hdwallet.private_key()}",
85+
public_key=ethereum_hdwallet.public_key(),
86+
address=ethereum_hdwallet.address()
87+
), indent=4), "\n")
88+
89+
# Bitcoin (Legacy, Nested-SegWit, Native-SegWit)
90+
for address_type in ["legacy", "nested-segwit", "native-segwit"]:
91+
92+
bitcoin_hdwallet: HDWallet = generate_phantom_hdwallet(
93+
cryptocurrency=Bitcoin,
94+
hd=data["bitcoin"][address_type]["hd"],
95+
network=Bitcoin.NETWORKS.MAINNET,
96+
derivation=data["bitcoin"][address_type]["derivation"],
97+
public_key_type=PUBLIC_KEY_TYPES.COMPRESSED
98+
)
99+
print(f"{bitcoin_hdwallet.cryptocurrency()} ({bitcoin_hdwallet.symbol()}) {address_type} wallet:", json.dumps(dict(
100+
path=bitcoin_hdwallet.path(),
101+
wif=bitcoin_hdwallet.wif(),
102+
private_key=bitcoin_hdwallet.private_key(),
103+
public_key=bitcoin_hdwallet.public_key(),
104+
address=bitcoin_hdwallet.address()
105+
), indent=4), "\n")

0 commit comments

Comments
 (0)