Skip to content

Commit

Permalink
Merge pull request #15 from TeknoPT/main
Browse files Browse the repository at this point in the history
Big Upgrade
  • Loading branch information
TeknoPT authored Dec 12, 2023
2 parents 8a0d967 + 26daa1c commit 2f15fb1
Show file tree
Hide file tree
Showing 102 changed files with 536 additions and 2,972 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/Phantasma-Py.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from setuptools import setup, find_packages # noqa: H301

NAME = "phantasma-py"
VERSION = "1.0.6"
VERSION = "1.0.7"
# To install the library, run the following
#
# python setup.py install
Expand Down
6 changes: 3 additions & 3 deletions src/phantasma_py/Interfaces/Signature.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from abc import ABC, abstractmethod
from typing import List
from Interfaces.SignatureKind import SignatureKind
from Types import Address
from Types.Extensions import PBinaryReader, PBinaryWriter
from .SignatureKind import SignatureKind
from ..Types import Address
from ..Types.Extensions import PBinaryReader, PBinaryWriter

class Signature(ABC):
@property
Expand Down
79 changes: 69 additions & 10 deletions src/phantasma_py/Tx/Transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from ecpy.eddsa import EDDSA
import hashlib

from ..Types.Extensions import PBinaryWriter

from ..VM.ScriptBuilder import ScriptBuilder


Expand Down Expand Up @@ -90,20 +92,20 @@ def toString(self, withSignature: bool) -> str:
expirationBytes = [d, c, b, a]

sb = ScriptBuilder()
sb.emitVarString(self.nexusName)
sb.emitVarString(self.chainName)
sb.emitVarInt(int(len(self.script) / 2))
sb.appendHexEncoded(self.script)
sb.emitBytes(expirationBytes)
sb.emitVarString(self.payload)
sb.EmitVarString(self.nexusName)
sb.EmitVarString(self.chainName)
sb.EmitVarInt(int(len(self.script) / 2))
sb.AppendHexEncoded(self.script)
sb.EmitBytes(expirationBytes)
sb.EmitVarString(self.payload)

if (withSignature):
sb.emitVarInt(len(self.signatures))
sb.EmitVarInt(len(self.signatures))
for s in self.signatures:
sb.appendByte(1)
sb.AppendByte(1)
sig = s.decode("utf-8").upper()
sb.emitVarInt(int(len(sig) / 2))
sb.appendHexEncoded(sig)
sb.EmitVarInt(int(len(sig) / 2))
sb.AppendHexEncoded(sig)

return sb.data

Expand All @@ -128,3 +130,60 @@ def getSign(self, msgHex: str, pk: int) -> bytes:
sig = binascii.hexlify(sig)

return(sig)

def to_byte_array(self, with_signature):
writer = PBinaryWriter()
writer.write_string(self.nexusName)
writer.write_string(self.chainName)
writer.append_hex_encoded(self.script)
writer.write_date_time(self.expiration)
writer.append_hex_encoded(self.payload)
if with_signature:
writer.write_var_int(len(self.signatures))
for sig in self.signatures:
writer.write_signature(sig)

return writer.to_uint8_array()


def get_hash(self):
# Convert the transaction to a string representation (without the signature)
transaction_str = self.toString(False)

# Compute SHA256 hash of the string
hash_obj = hashlib.sha256(transaction_str.encode())

# Convert the hash to a hexadecimal string and reverse it
self.hash = hash_obj.hexdigest()[::-1]

return self.hash

def to_string(self, with_signature):
parts = []

# Append the nexus name and chain name
parts.append(self.nexus_name)
parts.append(self.chain_name)

# Append the script (length and hex encoded)
parts.append(str(len(self.script) // 2))
parts.append(self.script)

# Convert the expiration date to a timestamp and append
expiration_timestamp = int(self.expiration.timestamp())
parts.append(str(expiration_timestamp))

# Append the payload (length and hex encoded)
parts.append(str(len(self.payload) // 2))
parts.append(self.payload)

if with_signature:
# Append the number of signatures
parts.append(str(len(self.signatures)))
for sig in self.signatures:
# You need to adapt this part based on the structure of your Signature object in Python
parts.append(str(sig.kind))
parts.append(str(len(sig.bytes) // 2))
parts.append(binascii.hexlify(sig.bytes).decode())

return ''.join(parts)
32 changes: 9 additions & 23 deletions src/phantasma_py/Types/Address.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#from Types import PhantasmaKeys
from Types.Enums import AddressKind
from Types import Serialization
from .Enums import AddressKind
from .Serialization import Serialization
import base58
import hashlib
from cryptography.hazmat.primitives import serialization
Expand All @@ -19,7 +19,7 @@ class Address:
def __init__(self, public_key: bytes):
if len(public_key) != Address.LengthInBytes:
raise ValueError(f"publicKey length must be {Address.LengthInBytes}, it was {len(public_key)}")
self._bytes = public_key
self._bytes = public_key[0:Address.LengthInBytes]
self._text = None

@staticmethod
Expand All @@ -45,7 +45,7 @@ def Kind(self):
elif self._bytes[0] >= 3:
return AddressKind.Interop
else:
return chr(self._bytes[0])
return self._bytes[0]

def IsSystem(self) -> bool:
return self.Kind == AddressKind.System
Expand Down Expand Up @@ -110,11 +110,11 @@ def Parse(text: str):
bytes_ = base58.b58decode(text)
addr = Address(bytes_)

if prefix == "P" and addr.Kind != AddressKind.User:
if prefix == "P" and addr.Kind() != AddressKind.User:
raise ValueError("Invalid address prefix. Expected 'P'")
elif prefix == "S" and addr.Kind != AddressKind.System:
elif prefix == "S" and addr.Kind() != AddressKind.System:
raise ValueError("Invalid address prefix. Expected 'S'")
elif prefix == "X" and addr.Kind < AddressKind.Interop:
elif prefix == "X" and addr.Kind() < AddressKind.Interop:
raise ValueError("Invalid address prefix. Expected 'X'")

return addr
Expand Down Expand Up @@ -168,8 +168,7 @@ def __str__(self):
if self.is_null():
return self.NullText

prefix = {AddressKind.User: 'P', AddressKind.Interop: 'X'}.get(self.kind(), 'S')
return prefix + base58.b58encode(self._bytes).decode()
return self.Text;

def to_byte_array(self):
return self._bytes
Expand All @@ -184,17 +183,4 @@ def unserialize_data(self, reader):
self._text = None

def is_null(self):
return self._bytes == bytearray(self.LengthInBytes)

def kind(self):
"""
Determine the address kind based on the first byte.
This logic should be aligned with how your application defines different address kinds.
"""
if self._bytes[0] == AddressKind.User:
return "User"
elif self._bytes[0] == AddressKind.Interop:
return "Interop"
# Add more conditions for other address kinds as needed
else:
return "Unknown"
return self._bytes == bytearray(self.LengthInBytes)
2 changes: 1 addition & 1 deletion src/phantasma_py/Types/Ed25519Signature.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from Types.Extensions import PBinaryWriter
from .Extensions import PBinaryWriter
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ed25519
from cryptography.exceptions import InvalidSignature
Expand Down
2 changes: 1 addition & 1 deletion src/phantasma_py/Types/Extensions/PBinaryWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from typing import Union, List

from Interfaces import Signature, SignatureKind
from ...Interfaces import Signature, SignatureKind

# Assuming Signature and SignatureKind are already defined in your Python project

Expand Down
6 changes: 3 additions & 3 deletions src/phantasma_py/Types/PhantasmaKeys.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import base64
import hashlib
from Interfaces.IKeyPair import IKeyPair
from ..Interfaces.IKeyPair import IKeyPair
import base58
from Types import Address, Ed25519Signature
from . import Address, Ed25519Signature
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ed25519
import os
Expand All @@ -25,7 +25,7 @@ def __init__(self, private_key: bytes):
format=serialization.PublicFormat.Raw
)

PublicKey = self._public_key
self.PublicKey = self._public_key
#if ( hasattr(Address, 'FromKey') ):
self.Address = Address.FromKey(self._public_key)
#Address.FromMyKey() # Implement Address class
Expand Down
4 changes: 2 additions & 2 deletions src/phantasma_py/VM/EventKind.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum
from enum import IntEnum

class EventKind(Enum):
class EventKind(IntEnum):
Unknown = 0
ChainCreate = 1
TokenCreate = 2
Expand Down
Loading

0 comments on commit 2f15fb1

Please sign in to comment.