Skip to content

Commit

Permalink
[ASSET-34] Add bitcoin network info (#39)
Browse files Browse the repository at this point in the history
* ASSET-34 Update bitcoin address type

Signed-off-by: jormal <[email protected]>

* ASSET-34 Add bitcoin network info

Signed-off-by: jormal <[email protected]>

* ASSET-34 Update bitcoin asset info

Signed-off-by: jormal <[email protected]>

* ASSET-34 Update version

Signed-off-by: jormal <[email protected]>

---------

Signed-off-by: jormal <[email protected]>
  • Loading branch information
jormal authored Jul 23, 2024
1 parent ddfd17c commit 2706b78
Show file tree
Hide file tree
Showing 28 changed files with 242 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.7
2.0.8
29 changes: 27 additions & 2 deletions assets/btc-0/info.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
{
"contracts": [],
"contracts": [
{
"address": "1111111111111111111114oLvT2",
"decimals": 8,
"name": "Bitcoin",
"network": "bitcoin-1",
"symbol": "BTC",
"tags": [
"mainnet",
"native-coin"
]
},
{
"address": "1111111111111111111114oLvT2",
"decimals": 8,
"name": "Bitcoin",
"network": "bitcoin-2",
"symbol": "BTC",
"tags": [
"native-coin",
"testnet"
]
}
],
"id": "btc-0",
"images": {
"png128": true,
Expand All @@ -19,5 +42,7 @@
"url": "https://www.coingecko.com/en/coins/bitcoin"
}
],
"tags": []
"tags": [
"bitcoin"
]
}
Binary file added assets/unknown-bitcoin/image-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/unknown-bitcoin/image-256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/unknown-bitcoin/image-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/unknown-bitcoin/image-64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions assets/unknown-bitcoin/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions assets/unknown-bitcoin/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"contracts": [],
"id": "unknown-bitcoin",
"images": {
"png128": true,
"png256": true,
"png32": true,
"png64": true,
"svg": true
},
"name": "Unknown Bitcoin Asset",
"references": [],
"tags": [
"bitcoin"
]
}
4 changes: 4 additions & 0 deletions enums/ids/asset.json
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,10 @@
"description": "Unknown Bifrost Asset",
"value": "unknown-bifrost"
},
{
"description": "Unknown Bitcoin Asset",
"value": "unknown-bitcoin"
},
{
"description": "Unknown BNB Asset",
"value": "unknown-bnb"
Expand Down
8 changes: 8 additions & 0 deletions enums/ids/network.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
{
"description": "Bitcoin Mainnet",
"value": "bitcoin-1"
},
{
"description": "Bitcoin Testnet",
"value": "bitcoin-2"
},
{
"description": "Ethereum Mainnet",
"value": "evm-1"
Expand Down
4 changes: 4 additions & 0 deletions enums/tags/asset.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"value": "bifrost",
"description": "Main network of asset is Bifrost"
},
{
"value": "bitcoin",
"description": "Main network of asset is Bitcoin"
},
{
"value": "bnb",
"description": "Main network of asset is BNB chain"
Expand Down
4 changes: 4 additions & 0 deletions enums/tags/network.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"value": "bifrost",
"description": "Bifrost networks"
},
{
"value": "bitcoin",
"description": "Bitcoin networks"
},
{
"value": "bnb",
"description": "BNB networks"
Expand Down
36 changes: 33 additions & 3 deletions libraries/models/terminals/address.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from typing import Union, Self
from typing import Union, Self, get_args

from pydantic import RootModel
from pydantic import RootModel, model_validator

from libraries.models.terminals.address_bitcoin import AddressBitcoin
from libraries.models.terminals.address_evm import AddressEvm

ADDRESS_TYPES = Union[AddressEvm, AddressBitcoin]

class Address(RootModel[Union[AddressEvm]]):

class Address(RootModel[ADDRESS_TYPES]):
"""A union of constrained `str` about each address of blockchain networks."""

def __eq__(self, other: Self) -> bool:
if self.is_evm_address and other.is_evm_address:
return self.root.__eq__(other.root)
elif self.is_bitcoin_address and other.is_bitcoin_address:
return self.root.__eq__(other.root)
else:
raise ValueError(
f"Cannot compare difference addresses {type(self.root)} and {type(other.root)}."
Expand All @@ -22,6 +27,8 @@ def __ne__(self, other: Self) -> bool:
def __lt__(self, other: Self) -> bool:
if self.is_evm_address and other.is_evm_address:
return self.root.__lt__(other.root)
elif self.is_bitcoin_address and other.is_bitcoin_address:
return self.root.__lt__(other.root)
else:
raise ValueError(
f"Cannot compare difference addresses {type(self.root)} and {type(other.root)}."
Expand Down Expand Up @@ -50,3 +57,26 @@ def is_evm_address(self) -> bool:
Whether the address is an EVM address.
"""
return isinstance(self.root, AddressEvm)

@property
def is_bitcoin_address(self) -> bool:
"""Check if the address is a Bitcoin address.
Returns:
Whether the address is a Bitcoin address.
"""
return isinstance(self.root, AddressBitcoin)

@model_validator(mode="after")
def validator(self) -> Self:
root_types = get_args(ADDRESS_TYPES)
if type(self.root) in root_types:
return self
elif isinstance(self.root, str):
for root_type in root_types:
try:
self.root = root_type(self.root)
return self
except ValueError:
continue
raise ValueError(f"Invalid address: {self.root}")
51 changes: 51 additions & 0 deletions libraries/models/terminals/address_bitcoin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import Self

from bitcoinlib.encoding import EncodingError
from bitcoinlib.keys import deserialize_address

from libraries.models.templates.str_model import StrModel


class AddressBitcoin(StrModel):
"""A constrained `str` for the Bitcoin address."""

@property
def __deserialized_result(self) -> dict:
"""The result from the deserialization the Bitcoin address."""
return deserialize_address(self.root)

@property
def public_key_hash(self) -> str:
"""The public key hash of the Bitcoin address."""
return self.__deserialized_result.get("public_key_hash")

@property
def encoding_type(self) -> str:
"""The encoding type of the Bitcoin address."""
return self.__deserialized_result.get("encoding")

@property
def script_type(self) -> str:
"""The script type of the Bitcoin address."""
return self.__deserialized_result.get("script_type")

def __eq__(self, other: Self | str) -> bool:
match other:
case AddressBitcoin():
return self.root.lower() == other.root.lower()
case str():
return self.root.lower() == other.lower()
case _:
raise ValueError(f"Cannot compare {self} with {other}")

def __lt__(self, other: Self) -> bool:
return self.public_key_hash < other.public_key_hash

def __hash__(self) -> int:
return hash(self.public_key_hash)

def validate_str(self) -> Self:
try:
return deserialize_address(self.root).get("address")
except EncodingError:
raise ValueError(f"Invalid Bitcoin address: {self.root}")
10 changes: 10 additions & 0 deletions libraries/models/terminals/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class _EngineEnum(StrEnum):
"""

EVM: str = "evm"
BITCOIN: str = "bitcoin"
UNKNOWN: str = "unknown"


Expand All @@ -28,6 +29,15 @@ def is_evm(self) -> bool:
"""
return self.root == _EngineEnum.EVM

@property
def is_bitcoin(self) -> bool:
"""Check if the engine is a Bitcoin engine.
Returns:
Whether the engine is a Bitcoin engine.
"""
return self.root == _EngineEnum.BITCOIN

@property
def is_unknown(self) -> bool:
"""Check if the engine is an unknown engine.
Expand Down
Binary file added networks/bitcoin-1/image-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added networks/bitcoin-1/image-256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added networks/bitcoin-1/image-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added networks/bitcoin-1/image-64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions networks/bitcoin-1/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions networks/bitcoin-1/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"currency": {
"address": "1111111111111111111114oLvT2",
"decimals": 8,
"id": "btc-0",
"name": "Bitcoin",
"symbol": "BTC"
},
"engine": "bitcoin",
"explorers": [],
"id": "bitcoin-1",
"images": {
"png128": true,
"png256": true,
"png32": true,
"png64": true,
"svg": true
},
"name": "Bitcoin Mainnet",
"network": "mainnet",
"tags": [
"bitcoin"
],
"unknownAssetId": "unknown-bitcoin"
}
Binary file added networks/bitcoin-2/image-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added networks/bitcoin-2/image-256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added networks/bitcoin-2/image-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added networks/bitcoin-2/image-64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions networks/bitcoin-2/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions networks/bitcoin-2/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"currency": {
"address": "1111111111111111111114oLvT2",
"decimals": 8,
"id": "btc-0",
"name": "Bitcoin",
"symbol": "BTC"
},
"engine": "bitcoin",
"explorers": [],
"id": "bitcoin-2",
"images": {
"png128": true,
"png256": true,
"png32": true,
"png64": true,
"svg": true
},
"name": "Bitcoin Testnet",
"network": "testnet",
"tags": [
"bitcoin"
],
"unknownAssetId": "unknown-bitcoin"
}
3 changes: 2 additions & 1 deletion requirements/essential.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Specifies the essential requirements of the project.
bitcoinlib>=0.6.15
build>=1.2.1
hexbytes>=0.3.1
pydantic>=2.5.3
web3>=6.14.0
build>=1.2.1

0 comments on commit 2706b78

Please sign in to comment.