Skip to content

Commit

Permalink
Add support for DeSo
Browse files Browse the repository at this point in the history
- New entries in cryptocurrencies.py;
- New {public,private}_key_base58check fields.
  • Loading branch information
alecov committed Jan 19, 2022
1 parent 01aa683 commit 4b8a610
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
85 changes: 84 additions & 1 deletion hdwallet/cryptocurrencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class Cryptocurrency(NestedNamespace):
COIN_TYPE: CoinType

SCRIPT_ADDRESS: int
PUBLIC_KEY_ADDRESS: int
PUBLIC_KEY_ADDRESS: int = 0
PRIVATE_KEY_ADDRESS: int = 0
SEGWIT_ADDRESS: SegwitAddress

EXTENDED_PRIVATE_KEY: ExtendedPrivateKey
Expand Down Expand Up @@ -714,6 +715,47 @@ class BitcoinMainnet(Cryptocurrency):
WIF_SECRET_KEY = 0x80


class DeSoMainnet(Cryptocurrency):

NAME = "DeSo"
SYMBOL = "DESO"
NETWORK = "mainnet"
SOURCE_CODE = "https://github.com/deso-protocol"
COIN_TYPE = CoinType({
"INDEX": 0,
"HARDENED": True
})

SCRIPT_ADDRESS = 0x05
PUBLIC_KEY_ADDRESS = 0xCD1400
PRIVATE_KEY_ADDRESS = 0x350000
SEGWIT_ADDRESS = SegwitAddress({
"HRP": "bc",
"VERSION": 0x00
})

EXTENDED_PRIVATE_KEY = ExtendedPrivateKey({
"P2PKH": 0x0488ade4,
"P2SH": 0x0488ade4,
"P2WPKH": 0x04b2430c,
"P2WPKH_IN_P2SH": 0x049d7878,
"P2WSH": 0x02aa7a99,
"P2WSH_IN_P2SH": 0x0295b005
})
EXTENDED_PUBLIC_KEY = ExtendedPublicKey({
"P2PKH": 0x0488b21e,
"P2SH": 0x0488b21e,
"P2WPKH": 0x04b24746,
"P2WPKH_IN_P2SH": 0x049d7cb2,
"P2WSH": 0x02aa7ed3,
"P2WSH_IN_P2SH": 0x0295b43f
})

MESSAGE_PREFIX = "\x18DeSo Signed Message:\n"
DEFAULT_PATH = f"m/44'/{str(COIN_TYPE)}/0'/0/0"
WIF_SECRET_KEY = 0x80


class BitcoinPlusMainnet(Cryptocurrency):

NAME = "Bitcoin Plus"
Expand Down Expand Up @@ -834,6 +876,47 @@ class BitcoinTestnet(Cryptocurrency):
WIF_SECRET_KEY = 0xef


class DeSoTestnet(Cryptocurrency):

NAME = "DeSo"
SYMBOL = "DESO"
NETWORK = "mainnet"
SOURCE_CODE = "https://github.com/deso-protocol"
COIN_TYPE = CoinType({
"INDEX": 0,
"HARDENED": True
})

SCRIPT_ADDRESS = 0x05
PUBLIC_KEY_ADDRESS = 0x11C200
PRIVATE_KEY_ADDRESS = 0x4F061B
SEGWIT_ADDRESS = SegwitAddress({
"HRP": "bc",
"VERSION": 0x00
})

EXTENDED_PRIVATE_KEY = ExtendedPrivateKey({
"P2PKH": 0x0488ade4,
"P2SH": 0x0488ade4,
"P2WPKH": 0x04b2430c,
"P2WPKH_IN_P2SH": 0x049d7878,
"P2WSH": 0x02aa7a99,
"P2WSH_IN_P2SH": 0x0295b005
})
EXTENDED_PUBLIC_KEY = ExtendedPublicKey({
"P2PKH": 0x0488b21e,
"P2SH": 0x0488b21e,
"P2WPKH": 0x04b24746,
"P2WPKH_IN_P2SH": 0x049d7cb2,
"P2WSH": 0x02aa7ed3,
"P2WSH_IN_P2SH": 0x0295b43f
})

MESSAGE_PREFIX = "\x18DeSo Signed Message:\n"
DEFAULT_PATH = f"m/44'/{str(COIN_TYPE)}/0'/0/0"
WIF_SECRET_KEY = 0x80


class BitcoinZMainnet(Cryptocurrency):

NAME = "BitcoinZ"
Expand Down
33 changes: 33 additions & 0 deletions hdwallet/hdwallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def __init__(self, symbol: str = "BTC", cryptocurrency: Any = None,
self._private_key: Optional[bytes] = None
self._public_key: Optional[str] = None
self._chain_code: Optional[bytes] = None

self._private_key_base58check: Optional[str] = None
self._public_key_base58check: Optional[str] = None

self._depth: int = 0
self._index: int = 0

Expand Down Expand Up @@ -211,6 +215,8 @@ def from_seed(self, seed: str) -> "HDWallet":
if self._use_default_path:
self.from_path(path=self._cryptocurrency.DEFAULT_PATH)
self._public_key = self.compressed()
self._public_key_base58check = self.public_key_base58check()
self._private_key_base58check = self.private_key_base58check()
if self._from_class:
self.from_path(path=self._path_class)
return self
Expand Down Expand Up @@ -251,6 +257,8 @@ def from_root_xprivate_key(self, xprivate_key: str, strict: bool = True) -> "HDW
if self._from_class:
self.from_path(path=self._path_class)
self._public_key = self.compressed()
self._public_key_base58check = self.public_key_base58check()
self._private_key_base58check = self.private_key_base58check()
return self

def from_root_xpublic_key(self, xpublic_key: str, strict: bool = True) -> "HDWallet":
Expand Down Expand Up @@ -289,6 +297,7 @@ def from_root_xpublic_key(self, xpublic_key: str, strict: bool = True) -> "HDWal
if self._from_class:
self.from_path(path=self._path_class)
self._public_key = self.compressed()
self._public_key_base58check = self.public_key_base58check()
return self

def from_xprivate_key(self, xprivate_key: str) -> "HDWallet":
Expand Down Expand Up @@ -317,6 +326,8 @@ def from_xprivate_key(self, xprivate_key: str) -> "HDWallet":
self._key = ecdsa.SigningKey.from_string(_deserialize_xprivate_key[5], curve=SECP256k1)
self._verified_key = self._key.get_verifying_key()
self._public_key = self.compressed()
self._public_key_base58check = self.public_key_base58check()
self._private_key_base58check = self.private_key_base58check()
return self

def from_xpublic_key(self, xpublic_key: str) -> "HDWallet":
Expand Down Expand Up @@ -346,6 +357,7 @@ def from_xpublic_key(self, xpublic_key: str) -> "HDWallet":
_deserialize_xpublic_key[5], curve=SECP256k1
)
self._public_key = self.compressed()
self._public_key_base58check = self.public_key_base58check()
return self

def from_wif(self, wif: str) -> "HDWallet":
Expand All @@ -372,6 +384,8 @@ def from_wif(self, wif: str) -> "HDWallet":
self._key = ecdsa.SigningKey.from_string(self._private_key, curve=SECP256k1)
self._verified_key = self._key.get_verifying_key()
self._public_key = self.compressed()
self._public_key_base58check = self.public_key_base58check()
self._private_key_base58check = self.private_key_base58check()
return self

def from_private_key(self, private_key: str) -> "HDWallet":
Expand All @@ -394,6 +408,8 @@ def from_private_key(self, private_key: str) -> "HDWallet":
self._key = ecdsa.SigningKey.from_string(self._private_key, curve=SECP256k1)
self._verified_key = self._key.get_verifying_key()
self._public_key = self.compressed()
self._public_key_base58check = self.public_key_base58check()
self._private_key_base58check = self.private_key_base58check()
return self

def from_public_key(self, public_key: str) -> "HDWallet":
Expand All @@ -416,6 +432,7 @@ def from_public_key(self, public_key: str) -> "HDWallet":
unhexlify(public_key), curve=SECP256k1
)
self._public_key = self.compressed()
self._public_key_base58check = self.public_key_base58check()
return self

def from_path(self, path: Union[str, Derivation]) -> "HDWallet":
Expand Down Expand Up @@ -521,6 +538,7 @@ def _derive_key_by_index(self, index) -> Optional["HDWallet"]:
)
self._key = ecdsa.SigningKey.from_string(self._private_key, curve=SECP256k1)
self._verified_key = self._key.get_verifying_key()
self._private_key_base58check = self.private_key_base58check()
else:
key_point = S256Point.parse(unhexlify(self.public_key()))
left_point = il_int * G
Expand Down Expand Up @@ -730,6 +748,7 @@ def clean_derivation(self) -> "HDWallet":
self._private_key, self._chain_code = self._root_private_key
self._key = ecdsa.SigningKey.from_string(self._private_key, curve=SECP256k1)
self._verified_key = self._key.get_verifying_key()
self._private_key_base58check = self.private_key_base58check()
elif self._root_public_key:
self._path, self._depth, self._parent_fingerprint, self._index = (
"m", 0, b"\0\0\0\0", 0
Expand Down Expand Up @@ -847,6 +866,18 @@ def public_key(self, compressed: bool = True, private_key: Optional[str] = None)
return hexlify(ck).decode() if compressed else self.uncompressed(compressed=hexlify(ck).decode())
return self.compressed() if compressed else self.uncompressed()

def public_key_base58check(self) -> str:
return base58.b58encode_check(
_unhexlify(self._cryptocurrency.PUBLIC_KEY_ADDRESS) +
unhexlify(self.public_key())
)

def private_key_base58check(self) -> str:
return base58.b58encode_check(
_unhexlify(self._cryptocurrency.PRIVATE_KEY_ADDRESS) +
unhexlify(self.private_key())
)

def strength(self) -> Optional[int]:
"""
Get Entropy strength.
Expand Down Expand Up @@ -1281,6 +1312,8 @@ def dumps(self) -> dict:
semantic=self.semantic(),
path=self.path(),
hash=self.hash(),
public_key_base58check=self.public_key_base58check(),
private_key_base58check=self.private_key_base58check(),
addresses=dict(
p2pkh=self.p2pkh_address(),
p2sh=self.p2sh_address(),
Expand Down
3 changes: 3 additions & 0 deletions hdwallet/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
DASH, DASHTEST = "DASH", "DASHTEST"
# Deep Onion
ONION = "ONION"
# DeSo
DESO, DESOTEST = "DESO", "DESOTEST"
# Defcoin
DFC = "DFC"
# Denarius
Expand Down Expand Up @@ -316,6 +318,7 @@
"CRAVE",
"DASH", "DASHTEST",
"ONION",
"DESO", "DESOTEST",
"DFC",
"DNR",
"DMD",
Expand Down

0 comments on commit 4b8a610

Please sign in to comment.